github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/accounts_status_test.go (about) 1 package environment_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/onflow/atree" 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/flow-go/fvm/environment" 11 ) 12 13 func TestAccountStatus(t *testing.T) { 14 15 s := environment.NewAccountStatus() 16 17 t.Run("test setting values", func(t *testing.T) { 18 index := atree.SlabIndex{1, 2, 3, 4, 5, 6, 7, 8} 19 s.SetStorageIndex(index) 20 s.SetPublicKeyCount(34) 21 s.SetStorageUsed(56) 22 s.SetAccountIdCounter(78) 23 24 require.Equal(t, uint64(56), s.StorageUsed()) 25 returnedIndex := s.StorageIndex() 26 require.True(t, bytes.Equal(index[:], returnedIndex[:])) 27 require.Equal(t, uint64(34), s.PublicKeyCount()) 28 require.Equal(t, uint64(78), s.AccountIdCounter()) 29 30 }) 31 32 t.Run("test serialization", func(t *testing.T) { 33 b := append([]byte(nil), s.ToBytes()...) 34 clone, err := environment.AccountStatusFromBytes(b) 35 require.NoError(t, err) 36 require.Equal(t, s.StorageIndex(), clone.StorageIndex()) 37 require.Equal(t, s.PublicKeyCount(), clone.PublicKeyCount()) 38 require.Equal(t, s.StorageUsed(), clone.StorageUsed()) 39 require.Equal(t, s.AccountIdCounter(), clone.AccountIdCounter()) 40 41 // invalid size bytes 42 _, err = environment.AccountStatusFromBytes([]byte{1, 2}) 43 require.Error(t, err) 44 }) 45 46 t.Run("test serialization - old format", func(t *testing.T) { 47 // TODO: remove this test when we remove support for the old format 48 oldBytes := []byte{ 49 0, // flags 50 0, 0, 0, 0, 0, 0, 0, 7, // storage used 51 0, 0, 0, 0, 0, 0, 0, 6, // storage index 52 0, 0, 0, 0, 0, 0, 0, 5, // public key counts 53 } 54 55 // The new format has an extra 8 bytes for the account id counter 56 // so we need to increase the storage used by 8 bytes while migrating it 57 increaseInSize := uint64(8) 58 59 migrated, err := environment.AccountStatusFromBytes(oldBytes) 60 require.NoError(t, err) 61 require.Equal(t, atree.SlabIndex{0, 0, 0, 0, 0, 0, 0, 6}, migrated.StorageIndex()) 62 require.Equal(t, uint64(5), migrated.PublicKeyCount()) 63 require.Equal(t, uint64(7)+increaseInSize, migrated.StorageUsed()) 64 require.Equal(t, uint64(0), migrated.AccountIdCounter()) 65 }) 66 }