44package validators_test
55
66import (
7+ "encoding/hex"
8+ "encoding/json"
79 "math"
810 "strconv"
911 "testing"
@@ -12,13 +14,92 @@ import (
1214
1315 "github.com/ava-labs/avalanchego/ids"
1416 "github.com/ava-labs/avalanchego/snow/validators/validatorstest"
17+ "github.com/ava-labs/avalanchego/utils/crypto/bls"
1518 "github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
1619
1720 safemath "github.com/ava-labs/avalanchego/utils/math"
1821
1922 . "github.com/ava-labs/avalanchego/snow/validators"
2023)
2124
25+ func TestWarpJSON (t * testing.T ) {
26+ const pkStr = "88c4760201a451619475ff7d3782d02381826bad5bef306d1ff6b22d3fb2e137bc004d867054efc241463fe4b21c61af"
27+ pkBytes , err := hex .DecodeString (pkStr )
28+ require .NoError (t , err )
29+ pk , err := bls .PublicKeyFromCompressedBytes (pkBytes )
30+ require .NoError (t , err )
31+
32+ w := Warp {
33+ PublicKey : pk ,
34+ PublicKeyBytes : bls .PublicKeyToUncompressedBytes (pk ),
35+ Weight : 12345 ,
36+ NodeIDs : []ids.NodeID {
37+ {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 },
38+ {0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f },
39+ },
40+ }
41+ wJSON , err := json .MarshalIndent (w , "" , "\t " )
42+ require .NoError (t , err )
43+
44+ const expectedJSON = `{
45+ "publicKey": "0x88c4760201a451619475ff7d3782d02381826bad5bef306d1ff6b22d3fb2e137bc004d867054efc241463fe4b21c61af",
46+ "weight": "12345",
47+ "nodeIDs": [
48+ "NodeID-12D2adLM3UJyWgFWD2VXv5vkMT8MoMbg",
49+ "NodeID-jVEbJZmHxdPnxBthD7v8dC96qyUrPMxg"
50+ ]
51+ }`
52+ require .JSONEq (t , expectedJSON , string (wJSON ))
53+
54+ var parsedW Warp
55+ require .NoError (t , json .Unmarshal (wJSON , & parsedW ))
56+ require .Equal (t , w , parsedW )
57+ }
58+
59+ func TestWarpSetJSON (t * testing.T ) {
60+ const pkStr = "88c4760201a451619475ff7d3782d02381826bad5bef306d1ff6b22d3fb2e137bc004d867054efc241463fe4b21c61af"
61+ pkBytes , err := hex .DecodeString (pkStr )
62+ require .NoError (t , err )
63+ pk , err := bls .PublicKeyFromCompressedBytes (pkBytes )
64+ require .NoError (t , err )
65+
66+ ws := WarpSet {
67+ Validators : []* Warp {
68+ {
69+ PublicKey : pk ,
70+ PublicKeyBytes : bls .PublicKeyToUncompressedBytes (pk ),
71+ Weight : 12345 ,
72+ NodeIDs : []ids.NodeID {
73+ {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 },
74+ {0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f },
75+ },
76+ },
77+ },
78+ TotalWeight : 54321 ,
79+ }
80+ wsJSON , err := json .MarshalIndent (ws , "" , "\t " )
81+ require .NoError (t , err )
82+
83+ const expectedJSON = `{
84+ "validators": [
85+ {
86+ "publicKey": "0x88c4760201a451619475ff7d3782d02381826bad5bef306d1ff6b22d3fb2e137bc004d867054efc241463fe4b21c61af",
87+ "weight": "12345",
88+ "nodeIDs": [
89+ "NodeID-12D2adLM3UJyWgFWD2VXv5vkMT8MoMbg",
90+ "NodeID-jVEbJZmHxdPnxBthD7v8dC96qyUrPMxg"
91+ ]
92+ }
93+ ],
94+ "totalWeight": "54321"
95+ }`
96+ require .JSONEq (t , expectedJSON , string (wsJSON ))
97+
98+ var parsedWS WarpSet
99+ require .NoError (t , json .Unmarshal (wsJSON , & parsedWS ))
100+ require .Equal (t , ws , parsedWS )
101+ }
102+
22103func TestFlattenValidatorSet (t * testing.T ) {
23104 var (
24105 vdrs = validatorstest .NewWarpSet (t , 3 )
0 commit comments