forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger-state-test.coffee
More file actions
152 lines (128 loc) · 4.29 KB
/
ledger-state-test.coffee
File metadata and controls
152 lines (128 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
################################################################################
async = require 'async'
simple_assert = require 'assert'
deep_eq = require 'deep-equal'
testutils = require './testutils'
{
LedgerVerifier
Balance
} = require './ledger-state'
#################################### CONFIG ####################################
config = testutils.init_config()
#################################### HELPERS ###################################
assert = simple_assert
prettyj = pretty_json = (v) -> JSON.stringify(v, undefined, 2)
describe 'Balance', ->
it 'parses native balances', ->
bal = new Balance("1.000")
assert.equal bal.is_native, true
assert.equal bal.limit, null
it 'parses iou balances', ->
bal = new Balance("1.000/USD/bob")
assert.equal bal.is_native, false
assert.equal bal.limit, null
assert.equal bal.amount.currency().to_json(), 'USD'
it 'parses iou balances with limits', ->
bal = new Balance("1-500/USD/bob")
assert.equal bal.is_native, false
assert.equal bal.amount.currency().to_json(), 'USD'
assert.equal bal.limit.to_json().value, '500'
assert.equal bal.amount.to_json().value, '1'
describe 'LedgerVerifier', ->
lv = null
declaration=
accounts:
bob:
balance: ['100.0', '200-500/USD/alice']
offers: [['89.0', '100/USD/alice'], ['89.0', '100/USD/alice']]
# We are using this because mocha and coffee-script is a retarded combination
# unfortunately, which terminates the program silently upon any require time
# exceptions. TODO: investigate obviously, but for the moment this is an
# acceptable workaround.
suiteSetup ->
remote_dummy = {set_secret: (->)}
lv = new LedgerVerifier(declaration, remote_dummy, config, assert)
it 'tracks xrp balances', ->
assert.equal lv.xrp_balances['bob'].to_json(), '100000000'
it 'tracks iou balances', ->
assert.equal lv.iou_balances['bob']['USD/alice'].to_json().value, '200'
it 'tracks iou trust limits', ->
assert.equal lv.trusts['bob']['USD/alice'].to_json().value, '500'
it 'can verify', ->
account_offers = [
{
"account": "bob",
"offers": [
{
"flags": 65536,
"seq": 2,
"taker_gets": {
"currency": "USD",
"issuer": "alice",
"value": "100"
},
"taker_pays": "88000000"
}
]
}
]
account_lines = [{
"account": "bob",
"lines": [
{
"account": "alice",
"balance": "201",
"currency": "USD",
"limit": "500",
"limit_peer": "0",
"quality_in": 0,
"quality_out": 0
},
]
}]
account_infos = [{
"account_data": {
"Account": "bob",
"Balance": "999"+ "999"+ "970",
"Flags": 0,
"LedgerEntryType": "AccountRoot",
"OwnerCount": 0,
"PreviousTxnID": "3D7823B577A5AF5860273B3DD13CA82D072B63B3B095DE1784604A5B41D7DD1D",
"PreviousTxnLgrSeq": 5,
"Sequence": 3,
"index": "59BEA57D1A27B6A560ECA226ABD10DE80C3ADC6961039908087ACDFA92F71489"
},
"ledger_current_index": 8
}]
errors = lv.verify account_infos, account_lines, account_offers
assert.equal errors.bob.balance['USD/alice'].expected, '200'
assert.equal errors.bob.balance['USD/alice'].actual, '201'
assert.equal errors.bob.balance['XRP'].expected, '100'
assert.equal errors.bob.balance['XRP'].actual, '999.99997'
assert.equal errors.bob.offers[0].taker_pays.actual, '88/XRP'
assert.equal errors.bob.offers[0].taker_pays.expected, '89/XRP'
# {"expected":["89.0","100/USD/alice"],"actual":"missing"}
assert.equal errors.bob.offers[1].actual, 'missing'
expected = {
"bob": {
"balance": {
"XRP": {
"actual": "999.99997",
"expected": "100"
},
"USD/alice": {
"actual": "201",
"expected": "200"
}
},
"offers": [
{
"taker_pays": {
"expected": "89/XRP",
"actual": "88/XRP"
}
},
"missing"
]
}
}