github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/block_test.go (about) 1 package flow_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "github.com/vmihailenco/msgpack/v4" 10 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/utils/unittest" 13 ) 14 15 func TestGenesisEncodingJSON(t *testing.T) { 16 genesis := flow.Genesis(flow.Mainnet) 17 genesisID := genesis.ID() 18 data, err := json.Marshal(genesis) 19 require.NoError(t, err) 20 var decoded flow.Block 21 err = json.Unmarshal(data, &decoded) 22 require.NoError(t, err) 23 decodedID := decoded.ID() 24 assert.Equal(t, genesisID, decodedID) 25 assert.Equal(t, genesis, &decoded) 26 } 27 28 func TestGenesisDecodingMsgpack(t *testing.T) { 29 genesis := flow.Genesis(flow.Mainnet) 30 genesisID := genesis.ID() 31 data, err := msgpack.Marshal(genesis) 32 require.NoError(t, err) 33 var decoded flow.Block 34 err = msgpack.Unmarshal(data, &decoded) 35 require.NoError(t, err) 36 decodedID := decoded.ID() 37 assert.Equal(t, genesisID, decodedID) 38 assert.Equal(t, genesis, &decoded) 39 } 40 41 func TestBlockEncodingJSON(t *testing.T) { 42 block := unittest.BlockFixture() 43 blockID := block.ID() 44 data, err := json.Marshal(block) 45 require.NoError(t, err) 46 var decoded flow.Block 47 err = json.Unmarshal(data, &decoded) 48 require.NoError(t, err) 49 decodedID := decoded.ID() 50 assert.Equal(t, blockID, decodedID) 51 assert.Equal(t, block, decoded) 52 } 53 54 func TestBlockEncodingMsgpack(t *testing.T) { 55 block := unittest.BlockFixture() 56 blockID := block.ID() 57 data, err := msgpack.Marshal(block) 58 require.NoError(t, err) 59 var decoded flow.Block 60 err = msgpack.Unmarshal(data, &decoded) 61 require.NoError(t, err) 62 decodedID := decoded.ID() 63 assert.Equal(t, blockID, decodedID) 64 assert.Equal(t, block, decoded) 65 } 66 67 func TestNilProducesSameHashAsEmptySlice(t *testing.T) { 68 69 nilPayload := flow.Payload{ 70 Guarantees: nil, 71 Seals: nil, 72 } 73 74 slicePayload := flow.Payload{ 75 Guarantees: make([]*flow.CollectionGuarantee, 0), 76 Seals: make([]*flow.Seal, 0), 77 } 78 79 assert.Equal(t, nilPayload.Hash(), slicePayload.Hash()) 80 } 81 82 func TestOrderingChangesHash(t *testing.T) { 83 84 seals := unittest.Seal.Fixtures(5) 85 86 payload1 := flow.Payload{ 87 Seals: seals, 88 } 89 90 payload2 := flow.Payload{ 91 Seals: []*flow.Seal{seals[3], seals[2], seals[4], seals[1], seals[0]}, 92 } 93 94 assert.NotEqual(t, payload1.Hash(), payload2.Hash()) 95 } 96 97 func TestBlock_Status(t *testing.T) { 98 statuses := map[flow.BlockStatus]string{ 99 flow.BlockStatusUnknown: "BLOCK_UNKNOWN", 100 flow.BlockStatusFinalized: "BLOCK_FINALIZED", 101 flow.BlockStatusSealed: "BLOCK_SEALED", 102 } 103 104 for status, value := range statuses { 105 assert.Equal(t, status.String(), value) 106 } 107 }