github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/header_test.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package types_test 18 19 import ( 20 "testing" 21 22 . "github.com/stafiprotocol/go-substrate-rpc-client/types" 23 ) 24 25 var exampleHeader = Header{ 26 ParentHash: Hash{1, 2, 3, 4, 5}, 27 Number: 42, 28 StateRoot: Hash{2, 3, 4, 5, 6}, 29 ExtrinsicsRoot: Hash{3, 4, 5, 6, 7}, 30 Digest: Digest{ 31 {IsOther: true, AsOther: Bytes{4, 5}}, 32 {IsAuthoritiesChange: true, AsAuthoritiesChange: []AuthorityID{{5, 6}}}, 33 {IsChangesTrieRoot: true, AsChangesTrieRoot: Hash{6, 7}}, 34 {IsSealV0: true, AsSealV0: SealV0{7, Signature{8, 9, 10}}}, 35 {IsConsensus: true, AsConsensus: Consensus{9, Bytes{10, 11, 12}}}, 36 {IsSeal: true, AsSeal: Seal{11, Bytes{12, 13, 14}}}, 37 {IsPreRuntime: true, AsPreRuntime: PreRuntime{13, Bytes{14, 15, 16}}}, 38 }, 39 } 40 41 func TestHeader_EncodeDecode(t *testing.T) { 42 assertRoundtrip(t, exampleHeader) 43 } 44 45 func TestHeader_EncodedLength(t *testing.T) { 46 assertEncodedLength(t, []encodedLengthAssert{{exampleHeader, 269}}) 47 } 48 49 func TestHeader_Encode(t *testing.T) { 50 assertEncode(t, []encodingAssert{ 51 {exampleHeader, MustHexDecodeString("0x0102030405000000000000000000000000000000000000000000000000000000a8020304050600000000000000000000000000000000000000000000000000000003040506070000000000000000000000000000000000000000000000000000001c000804050104050600000000000000000000000000000000000000000000000000000000000002060700000000000000000000000000000000000000000000000000000000000003070000000000000008090a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004090000000c0a0b0c050b0000000c0c0d0e060d0000000c0e0f10")}, //nolint:lll 52 }) 53 } 54 55 func TestHeader_Hash(t *testing.T) { 56 assertHash(t, []hashAssert{ 57 {exampleHeader, MustHexDecodeString("0x4172ec8c10ce00ea6152bb9fb4c63b98305953ebdfa229e47d6b7425842528e4")}, 58 }) 59 } 60 61 func TestHeader_Hex(t *testing.T) { 62 assertEncodeToHex(t, []encodeToHexAssert{ 63 {exampleHeader, "0x0102030405000000000000000000000000000000000000000000000000000000a8020304050600000000000000000000000000000000000000000000000000000003040506070000000000000000000000000000000000000000000000000000001c000804050104050600000000000000000000000000000000000000000000000000000000000002060700000000000000000000000000000000000000000000000000000000000003070000000000000008090a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004090000000c0a0b0c050b0000000c0c0d0e060d0000000c0e0f10"}, //nolint:lll 64 }) 65 } 66 67 func TestHeader_String(t *testing.T) { 68 assertString(t, []stringAssert{ 69 {exampleHeader, "{[1 2 3 4 5 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] 42 [2 3 4 5 6 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] [3 4 5 6 7 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] [{true [4 5] false [] false [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 0 0 0 0 0] false {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 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 0 0 0 0 0 0 0 0 0 0 0]} false {0 []} false {0 []} false {0 []}} {false [] true [[5 6 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 0 0 0]] false [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 0 0 0 0 0] false {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 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 0 0 0 0 0 0 0 0 0 0 0]} false {0 []} false {0 []} false {0 []}} {false [] false [] true [6 7 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 0 0 0] false {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 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 0 0 0 0 0 0 0 0 0 0 0]} false {0 []} false {0 []} false {0 []}} {false [] false [] false [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 0 0 0 0 0] true {7 [8 9 10 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 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 0 0 0 0 0 0 0]} false {0 []} false {0 []} false {0 []}} {false [] false [] false [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 0 0 0 0 0] false {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 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 0 0 0 0 0 0 0 0 0 0 0]} true {9 [10 11 12]} false {0 []} false {0 []}} {false [] false [] false [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 0 0 0 0 0] false {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 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 0 0 0 0 0 0 0 0 0 0 0]} false {0 []} true {11 [12 13 14]} false {0 []}} {false [] false [] false [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 0 0 0 0 0] false {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 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 0 0 0 0 0 0 0 0 0 0 0]} false {0 []} false {0 []} true {13 [14 15 16]}}]}"}, //nolint:lll 70 }) 71 } 72 73 func TestHeader_Eq(t *testing.T) { 74 assertEq(t, []eqAssert{ 75 {exampleHeader, exampleHeader, true}, 76 {exampleHeader, NewBytes(hash64), false}, 77 {exampleHeader, NewBool(false), false}, 78 }) 79 }