-
Notifications
You must be signed in to change notification settings - Fork 2
/
proof_test.go
107 lines (98 loc) · 4.44 KB
/
proof_test.go
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
package c_polygonid
import (
"encoding/json"
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
func sToI(in string) *big.Int {
i, ok := new(big.Int).SetString(in, 10)
if !ok {
panic(in)
}
return i
}
func TestSmartContractProof_UnmarshalJSON(t *testing.T) {
in := []byte(`{
"root": "21886963837209295645237802338392436657831338184576939563308136414029471402751",
"existence": false,
"siblings": [
"6191108438878887313089858257093071890109247751515733437254731402827751511135",
"15252492506357248921683010422171850003902177151921011494591704698371153612891",
"15018961596920043594893017979264344615788586832976540723682652925892284477641",
"6227396484016430116096507254505961435403208655685024204321272676352630808661",
"1675767912446047403475822485075507021783309024163334664263442124838492785381",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"
],
"index": "7620678129862513343043520404117038257929715417156701395854762685236968707286",
"value": "12531801231521618639953387176086811127809839642129894598674415337045749557906",
"auxExistence": true,
"auxIndex": "19503047644893154503061167588860147737124125029994335888880441528414786195286",
"auxValue": "12531801231521618639953387176086811127809839642129894598674415337045749557906"
}`)
want := SmartContractProof{
Root: sToI("21886963837209295645237802338392436657831338184576939563308136414029471402751"),
Existence: false,
Siblings: []*big.Int{
sToI("6191108438878887313089858257093071890109247751515733437254731402827751511135"),
sToI("15252492506357248921683010422171850003902177151921011494591704698371153612891"),
sToI("15018961596920043594893017979264344615788586832976540723682652925892284477641"),
sToI("6227396484016430116096507254505961435403208655685024204321272676352630808661"),
sToI("1675767912446047403475822485075507021783309024163334664263442124838492785381"),
big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0),
big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0),
big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0),
big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0),
big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0),
big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0),
big.NewInt(0), big.NewInt(0), big.NewInt(0),
},
AuxExistence: true,
AuxIndex: sToI("19503047644893154503061167588860147737124125029994335888880441528414786195286"),
AuxValue: sToI("12531801231521618639953387176086811127809839642129894598674415337045749557906"),
}
var res SmartContractProof
err := res.UnmarshalJSON(in)
require.NoError(t, err)
require.Equal(t, want, res)
}
func TestProofFromSmartContract(t *testing.T) {
in := `{
"root": "17039823904837071705763545555283546217751326723169195059364451777353741017328",
"existence": false,
"siblings": [
"14989532119404983961115670288381063073891118401716735992353404523801340288158",
"15817549995119513546413395894800310537308858548528902759332598606866792105384",
"20955911300871905860419417343337237575819647673394656670247178513070221579793",
"7345857457589225232320640926291449425076936633178262764678572453063445218154",
"13941064550735375985967548290421702932981128763694428458881182266843384273940",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"
],
"auxExistence": true,
"auxIndex": "10",
"auxValue": "20",
"index": "13625694351531357880063798347796487002182037278253017013343168668336623401886",
"value": "0"
}`
var scp SmartContractProof
err := scp.UnmarshalJSON([]byte(in))
require.NoError(t, err)
proof, _, err := ProofFromSmartContract(scp)
require.NoError(t, err)
proofJson, err := json.Marshal(proof)
require.NoError(t, err)
wantResponse := `{
"existence": false,
"siblings": [
"14989532119404983961115670288381063073891118401716735992353404523801340288158",
"15817549995119513546413395894800310537308858548528902759332598606866792105384",
"20955911300871905860419417343337237575819647673394656670247178513070221579793",
"7345857457589225232320640926291449425076936633178262764678572453063445218154",
"13941064550735375985967548290421702932981128763694428458881182266843384273940"
],
"node_aux": {"key": "10", "value": "20"}
}`
require.JSONEq(t, wantResponse, string(proofJson))
}