github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/inmem/encodable_test.go (about) 1 package inmem_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/flow-go/model/encodable" 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/state/protocol/inmem" 13 "github.com/onflow/flow-go/utils/unittest" 14 ) 15 16 // test that we have the same snapshot after an encode/decode cycle 17 // in particular with differing public key implementations 18 func TestEncodeDecode(t *testing.T) { 19 20 participants := unittest.IdentityListFixture(10, unittest.WithAllRoles()) 21 // add a partner, which has its key represented as an encodable wrapper 22 // type rather than the direct crypto type 23 partner := unittest.IdentityFixture(unittest.WithKeys, func(identity *flow.Identity) { 24 identity.NetworkPubKey = encodable.NetworkPubKey{PublicKey: identity.NetworkPubKey} 25 }) 26 participants = append(participants, partner) 27 initialSnapshot := unittest.RootSnapshotFixture(participants) 28 29 // encode then decode the snapshot 30 var decodedSnapshot inmem.EncodableSnapshot 31 bz, err := json.Marshal(initialSnapshot.Encodable()) 32 require.NoError(t, err) 33 err = json.Unmarshal(bz, &decodedSnapshot) 34 require.NoError(t, err) 35 36 // check that the computed and stored result IDs are consistent 37 decodedResult, decodedSeal := decodedSnapshot.LatestResult, decodedSnapshot.LatestSeal 38 assert.Equal(t, decodedResult.ID(), decodedSeal.ResultID) 39 }