github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/serialization_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package block 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/MetalBlockchain/metalgo/ids" 14 "github.com/MetalBlockchain/metalgo/utils/constants" 15 "github.com/MetalBlockchain/metalgo/vms/components/avax" 16 "github.com/MetalBlockchain/metalgo/vms/platformvm/txs" 17 ) 18 19 func TestBanffBlockSerialization(t *testing.T) { 20 type test struct { 21 block BanffBlock 22 bytes []byte 23 } 24 25 tests := []test{ 26 { 27 block: &BanffProposalBlock{ 28 ApricotProposalBlock: ApricotProposalBlock{ 29 Tx: &txs.Tx{ 30 Unsigned: &txs.AdvanceTimeTx{}, 31 }, 32 }, 33 }, 34 bytes: []byte{ 35 // Codec version 36 0x00, 0x00, 37 // Type ID 38 0x00, 0x00, 0x00, 0x1d, 39 // Rest 40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 0x00, 0x00, 0x00, 0x00, 49 }, 50 }, 51 { 52 block: &BanffCommitBlock{ 53 ApricotCommitBlock: ApricotCommitBlock{}, 54 }, 55 bytes: []byte{ 56 // Codec version 57 0x00, 0x00, 58 // Type ID 59 0x00, 0x00, 0x00, 0x1f, 60 // Rest 61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 67 }, 68 }, 69 { 70 block: &BanffAbortBlock{ 71 ApricotAbortBlock: ApricotAbortBlock{}, 72 }, 73 bytes: []byte{ 74 // Codec version 75 0x00, 0x00, 76 // Type ID 77 0x00, 0x00, 0x00, 0x1e, 78 // Rest 79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 85 }, 86 }, 87 { 88 block: &BanffStandardBlock{ 89 ApricotStandardBlock: ApricotStandardBlock{ 90 Transactions: []*txs.Tx{}, 91 }, 92 }, 93 bytes: []byte{ 94 // Codec version 95 0x00, 0x00, 96 // Type ID 97 0x00, 0x00, 0x00, 0x20, 98 // Rest 99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 105 0x00, 0x00, 0x00, 0x00, 106 }, 107 }, 108 } 109 110 for _, test := range tests { 111 testName := fmt.Sprintf("%T", test.block) 112 block := test.block 113 t.Run(testName, func(t *testing.T) { 114 require := require.New(t) 115 116 got, err := Codec.Marshal(CodecVersion, &block) 117 require.NoError(err) 118 require.Equal(test.bytes, got) 119 }) 120 } 121 } 122 123 func TestBanffProposalBlockJSON(t *testing.T) { 124 require := require.New(t) 125 126 simpleBanffProposalBlock := &BanffProposalBlock{ 127 Time: 123456, 128 ApricotProposalBlock: ApricotProposalBlock{ 129 CommonBlock: CommonBlock{ 130 PrntID: ids.ID{'p', 'a', 'r', 'e', 'n', 't', 'I', 'D'}, 131 Hght: 1337, 132 BlockID: ids.ID{'b', 'l', 'o', 'c', 'k', 'I', 'D'}, 133 }, 134 Tx: &txs.Tx{ 135 Unsigned: &txs.AdvanceTimeTx{ 136 Time: 123457, 137 }, 138 }, 139 }, 140 } 141 142 simpleBanffProposalBlockBytes, err := json.MarshalIndent(simpleBanffProposalBlock, "", "\t") 143 require.NoError(err) 144 145 require.Equal(`{ 146 "time": 123456, 147 "txs": null, 148 "parentID": "rVcYrvnGXdoJBeYQRm5ZNaCGHeVyqcHHJu8Yd89kJcef6V5Eg", 149 "height": 1337, 150 "id": "kM6h4d2UKYEDzQXm7KNqyeBJLjhb42J24m4L4WACB5didf3pk", 151 "tx": { 152 "unsignedTx": { 153 "time": 123457 154 }, 155 "credentials": null, 156 "id": "11111111111111111111111111111111LpoYY" 157 } 158 }`, string(simpleBanffProposalBlockBytes)) 159 160 complexBanffProposalBlock := simpleBanffProposalBlock 161 complexBanffProposalBlock.Transactions = []*txs.Tx{ 162 { 163 Unsigned: &txs.BaseTx{ 164 BaseTx: avax.BaseTx{ 165 NetworkID: constants.MainnetID, 166 BlockchainID: constants.PlatformChainID, 167 Outs: []*avax.TransferableOutput{}, 168 Ins: []*avax.TransferableInput{}, 169 Memo: []byte("KilroyWasHere"), 170 }, 171 }, 172 }, 173 { 174 Unsigned: &txs.BaseTx{ 175 BaseTx: avax.BaseTx{ 176 NetworkID: constants.MainnetID, 177 BlockchainID: constants.PlatformChainID, 178 Outs: []*avax.TransferableOutput{}, 179 Ins: []*avax.TransferableInput{}, 180 Memo: []byte("KilroyWasHere2"), 181 }, 182 }, 183 }, 184 } 185 186 complexBanffProposalBlockBytes, err := json.MarshalIndent(complexBanffProposalBlock, "", "\t") 187 require.NoError(err) 188 189 require.Equal(`{ 190 "time": 123456, 191 "txs": [ 192 { 193 "unsignedTx": { 194 "networkID": 1, 195 "blockchainID": "11111111111111111111111111111111LpoYY", 196 "outputs": [], 197 "inputs": [], 198 "memo": "0x4b696c726f7957617348657265" 199 }, 200 "credentials": null, 201 "id": "11111111111111111111111111111111LpoYY" 202 }, 203 { 204 "unsignedTx": { 205 "networkID": 1, 206 "blockchainID": "11111111111111111111111111111111LpoYY", 207 "outputs": [], 208 "inputs": [], 209 "memo": "0x4b696c726f795761734865726532" 210 }, 211 "credentials": null, 212 "id": "11111111111111111111111111111111LpoYY" 213 } 214 ], 215 "parentID": "rVcYrvnGXdoJBeYQRm5ZNaCGHeVyqcHHJu8Yd89kJcef6V5Eg", 216 "height": 1337, 217 "id": "kM6h4d2UKYEDzQXm7KNqyeBJLjhb42J24m4L4WACB5didf3pk", 218 "tx": { 219 "unsignedTx": { 220 "time": 123457 221 }, 222 "credentials": null, 223 "id": "11111111111111111111111111111111LpoYY" 224 } 225 }`, string(complexBanffProposalBlockBytes)) 226 }