From 46a001725eb9d8adbf32d66f972d939fde08b679 Mon Sep 17 00:00:00 2001 From: Faye Amacker <33205765+fxamacker@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:58:15 -0600 Subject: [PATCH] Integrate Atree inlining & Cadence v0.42 to flow-go --- .../migrations/atree_register_migration.go | 2 +- .../migrations/cadence_value_validation.go | 4 +- cmd/util/ledger/migrations/utils.go | 8 +- .../ledger/reporters/atree_decode_test.go | 2 +- .../util/migration_runtime_interface.go | 4 +- cmd/util/ledger/util/util.go | 14 +-- fvm/environment/account_key_updater_test.go | 4 +- fvm/environment/accounts.go | 12 +-- fvm/environment/accounts_status.go | 6 +- fvm/environment/accounts_status_test.go | 4 +- fvm/environment/accounts_test.go | 12 +-- fvm/environment/mock/accounts.go | 12 +-- fvm/environment/mock/environment.go | 12 +-- fvm/environment/mock/value_store.go | 12 +-- fvm/environment/value_store.go | 20 ++-- fvm/evm/emulator/state/collection.go | 97 +++++++++++-------- fvm/evm/emulator/state/stateDB_test.go | 8 +- fvm/evm/testutils/backend.go | 8 +- fvm/evm/testutils/cadence.go | 10 +- go.mod | 4 +- go.sum | 8 +- insecure/go.mod | 4 +- insecure/go.sum | 8 +- model/flow/ledger_test.go | 2 +- 24 files changed, 145 insertions(+), 132 deletions(-) diff --git a/cmd/util/ledger/migrations/atree_register_migration.go b/cmd/util/ledger/migrations/atree_register_migration.go index 1b0873e7a2b..dbe5141b50a 100644 --- a/cmd/util/ledger/migrations/atree_register_migration.go +++ b/cmd/util/ledger/migrations/atree_register_migration.go @@ -176,7 +176,7 @@ func (m *AtreeRegisterMigrator) convertStorageDomain( // no storage for this domain return nil } - storageMapIds[string(atree.SlabIndexToLedgerKey(storageMap.StorageID().Index))] = struct{}{} + storageMapIds[string(atree.SlabIndexToLedgerKey(storageMap.SlabID().Index()))] = struct{}{} iterator := storageMap.Iterator(util.NopMemoryGauge{}) keys := make([]interpreter.StringStorageMapKey, 0, storageMap.Count()) diff --git a/cmd/util/ledger/migrations/cadence_value_validation.go b/cmd/util/ledger/migrations/cadence_value_validation.go index ff45b2e2c97..bb4da8257b7 100644 --- a/cmd/util/ledger/migrations/cadence_value_validation.go +++ b/cmd/util/ledger/migrations/cadence_value_validation.go @@ -546,8 +546,8 @@ func (NoopRuntimeInterface) GetAccountContractNames(_ runtime.Address) ([]string panic("unexpected GetAccountContractNames call") } -func (NoopRuntimeInterface) AllocateStorageIndex(_ []byte) (atree.StorageIndex, error) { - panic("unexpected AllocateStorageIndex call") +func (NoopRuntimeInterface) AllocateSlabIndex(_ []byte) (atree.SlabIndex, error) { + panic("unexpected AllocateSlabIndex call") } func (NoopRuntimeInterface) ComputationUsed() (uint64, error) { diff --git a/cmd/util/ledger/migrations/utils.go b/cmd/util/ledger/migrations/utils.go index e747b3dc508..4ef30eb315c 100644 --- a/cmd/util/ledger/migrations/utils.go +++ b/cmd/util/ledger/migrations/utils.go @@ -51,11 +51,11 @@ func (a *AccountsAtreeLedger) ValueExists(owner, key []byte) (exists bool, err e return len(v) > 0, nil } -// AllocateStorageIndex allocates new storage index under the owner accounts to store a new register -func (a *AccountsAtreeLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - v, err := a.Accounts.AllocateStorageIndex(flow.BytesToAddress(owner)) +// AllocateSlabIndex allocates new storage index under the owner accounts to store a new register +func (a *AccountsAtreeLedger) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { + v, err := a.Accounts.AllocateSlabIndex(flow.BytesToAddress(owner)) if err != nil { - return atree.StorageIndex{}, fmt.Errorf("storage address allocation failed: %w", err) + return atree.SlabIndex{}, fmt.Errorf("storage address allocation failed: %w", err) } return v, nil } diff --git a/cmd/util/ledger/reporters/atree_decode_test.go b/cmd/util/ledger/reporters/atree_decode_test.go index 73b69a7e54b..8d06f1e28fa 100644 --- a/cmd/util/ledger/reporters/atree_decode_test.go +++ b/cmd/util/ledger/reporters/atree_decode_test.go @@ -145,7 +145,7 @@ func TestMapDataSlabCollisionCount(t *testing.T) { 0x82, 0x76, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x76, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, - // element: [hhhhhhhhhhhhhhhhhhhhhh:StorageID(1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,4)] + // element: [hhhhhhhhhhhhhhhhhhhhhh:SlabID(1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,4)] 0x82, 0x76, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0xd8, 0xff, 0x50, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, diff --git a/cmd/util/ledger/util/migration_runtime_interface.go b/cmd/util/ledger/util/migration_runtime_interface.go index c72d8493095..d3020e3ec71 100644 --- a/cmd/util/ledger/util/migration_runtime_interface.go +++ b/cmd/util/ledger/util/migration_runtime_interface.go @@ -242,8 +242,8 @@ func (m MigrationRuntimeInterface) GetAccountContractNames(_ runtime.Address) ([ panic("unexpected GetAccountContractNames call") } -func (m MigrationRuntimeInterface) AllocateStorageIndex(_ []byte) (atree.StorageIndex, error) { - panic("unexpected AllocateStorageIndex call") +func (m MigrationRuntimeInterface) AllocateSlabIndex(_ []byte) (atree.SlabIndex, error) { + panic("unexpected AllocateSlabIndex call") } func (m MigrationRuntimeInterface) ComputationUsed() (uint64, error) { diff --git a/cmd/util/ledger/util/util.go b/cmd/util/ledger/util/util.go index 46cc54e6850..a097a193bc7 100644 --- a/cmd/util/ledger/util/util.go +++ b/cmd/util/ledger/util/util.go @@ -55,11 +55,11 @@ func (a *AccountsAtreeLedger) ValueExists(owner, key []byte) (exists bool, err e return len(v) > 0, nil } -// AllocateStorageIndex allocates new storage index under the owner accounts to store a new register -func (a *AccountsAtreeLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - v, err := a.Accounts.AllocateStorageIndex(flow.BytesToAddress(owner)) +// AllocateSlabIndex allocates new storage index under the owner accounts to store a new register +func (a *AccountsAtreeLedger) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { + v, err := a.Accounts.AllocateSlabIndex(flow.BytesToAddress(owner)) if err != nil { - return atree.StorageIndex{}, fmt.Errorf("storage index allocation failed: %w", err) + return atree.SlabIndex{}, fmt.Errorf("storage index allocation failed: %w", err) } return v, nil } @@ -108,7 +108,7 @@ var _ common.MemoryGauge = (*NopMemoryGauge)(nil) type PayloadsReadonlyLedger struct { Snapshot *PayloadSnapshot - AllocateStorageIndexFunc func(owner []byte) (atree.StorageIndex, error) + AllocateStorageIndexFunc func(owner []byte) (atree.SlabIndex, error) SetValueFunc func(owner, key, value []byte) (err error) } @@ -133,12 +133,12 @@ func (p *PayloadsReadonlyLedger) ValueExists(owner, key []byte) (exists bool, er return ok, nil } -func (p *PayloadsReadonlyLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { +func (p *PayloadsReadonlyLedger) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { if p.AllocateStorageIndexFunc != nil { return p.AllocateStorageIndexFunc(owner) } - panic("AllocateStorageIndex not expected to be called") + panic("AllocateSlabIndex not expected to be called") } func NewPayloadsReadonlyLedger(snapshot *PayloadSnapshot) *PayloadsReadonlyLedger { diff --git a/fvm/environment/account_key_updater_test.go b/fvm/environment/account_key_updater_test.go index 09cb1ba9491..106bcde1959 100644 --- a/fvm/environment/account_key_updater_test.go +++ b/fvm/environment/account_key_updater_test.go @@ -217,8 +217,8 @@ func (f FakeAccounts) Create(_ []flow.AccountPublicKey, _ flow.Address) error { func (f FakeAccounts) GetValue(_ flow.RegisterID) (flow.RegisterValue, error) { return nil, nil } func (f FakeAccounts) GetStorageUsed(_ flow.Address) (uint64, error) { return 0, nil } func (f FakeAccounts) SetValue(_ flow.RegisterID, _ []byte) error { return nil } -func (f FakeAccounts) AllocateStorageIndex(_ flow.Address) (atree.StorageIndex, error) { - return atree.StorageIndex{}, nil +func (f FakeAccounts) AllocateSlabIndex(_ flow.Address) (atree.SlabIndex, error) { + return atree.SlabIndex{}, nil } func (f FakeAccounts) GenerateAccountLocalID(address flow.Address) (uint64, error) { return 0, nil diff --git a/fvm/environment/accounts.go b/fvm/environment/accounts.go index 01041f19a3f..2cf63b9f582 100644 --- a/fvm/environment/accounts.go +++ b/fvm/environment/accounts.go @@ -36,7 +36,7 @@ type Accounts interface { GetValue(id flow.RegisterID) (flow.RegisterValue, error) GetStorageUsed(address flow.Address) (uint64, error) SetValue(id flow.RegisterID, value flow.RegisterValue) error - AllocateStorageIndex(address flow.Address) (atree.StorageIndex, error) + AllocateSlabIndex(address flow.Address) (atree.SlabIndex, error) GenerateAccountLocalID(address flow.Address) (uint64, error) } @@ -52,16 +52,16 @@ func NewAccounts(txnState state.NestedTransactionPreparer) *StatefulAccounts { } } -func (a *StatefulAccounts) AllocateStorageIndex( +func (a *StatefulAccounts) AllocateSlabIndex( address flow.Address, ) ( - atree.StorageIndex, + atree.SlabIndex, error, ) { // get status status, err := a.getAccountStatus(address) if err != nil { - return atree.StorageIndex{}, err + return atree.SlabIndex{}, err } // get and increment the index @@ -79,7 +79,7 @@ func (a *StatefulAccounts) AllocateStorageIndex( []byte{}) }) if err != nil { - return atree.StorageIndex{}, fmt.Errorf( + return atree.SlabIndex{}, fmt.Errorf( "failed to allocate an storage index: %w", err) } @@ -88,7 +88,7 @@ func (a *StatefulAccounts) AllocateStorageIndex( status.SetStorageIndex(newIndexBytes) err = a.setAccountStatus(address, status) if err != nil { - return atree.StorageIndex{}, fmt.Errorf( + return atree.SlabIndex{}, fmt.Errorf( "failed to allocate an storage index: %w", err) } diff --git a/fvm/environment/accounts_status.go b/fvm/environment/accounts_status.go index a420051550f..93c9a81db6e 100644 --- a/fvm/environment/accounts_status.go +++ b/fvm/environment/accounts_status.go @@ -105,13 +105,13 @@ func (a *AccountStatus) StorageUsed() uint64 { } // SetStorageIndex updates the storage index of the account -func (a *AccountStatus) SetStorageIndex(index atree.StorageIndex) { +func (a *AccountStatus) SetStorageIndex(index atree.SlabIndex) { copy(a[storageIndexStartIndex:storageIndexStartIndex+storageIndexSize], index[:storageIndexSize]) } // StorageIndex returns the storage index of the account -func (a *AccountStatus) StorageIndex() atree.StorageIndex { - var index atree.StorageIndex +func (a *AccountStatus) StorageIndex() atree.SlabIndex { + var index atree.SlabIndex copy(index[:], a[storageIndexStartIndex:storageIndexStartIndex+storageIndexSize]) return index } diff --git a/fvm/environment/accounts_status_test.go b/fvm/environment/accounts_status_test.go index 543ee2b05f1..4ab3c9c1ee5 100644 --- a/fvm/environment/accounts_status_test.go +++ b/fvm/environment/accounts_status_test.go @@ -15,7 +15,7 @@ func TestAccountStatus(t *testing.T) { s := environment.NewAccountStatus() t.Run("test setting values", func(t *testing.T) { - index := atree.StorageIndex{1, 2, 3, 4, 5, 6, 7, 8} + index := atree.SlabIndex{1, 2, 3, 4, 5, 6, 7, 8} s.SetStorageIndex(index) s.SetPublicKeyCount(34) s.SetStorageUsed(56) @@ -58,7 +58,7 @@ func TestAccountStatus(t *testing.T) { migrated, err := environment.AccountStatusFromBytes(oldBytes) require.NoError(t, err) - require.Equal(t, atree.StorageIndex{0, 0, 0, 0, 0, 0, 0, 6}, migrated.StorageIndex()) + require.Equal(t, atree.SlabIndex{0, 0, 0, 0, 0, 0, 0, 6}, migrated.StorageIndex()) require.Equal(t, uint64(5), migrated.PublicKeyCount()) require.Equal(t, uint64(7)+increaseInSize, migrated.StorageUsed()) require.Equal(t, uint64(0), migrated.AccountIdCounter()) diff --git a/fvm/environment/accounts_test.go b/fvm/environment/accounts_test.go index c6ef3cce467..aa44f0b9c2c 100644 --- a/fvm/environment/accounts_test.go +++ b/fvm/environment/accounts_test.go @@ -422,17 +422,17 @@ func TestAccounts_AllocateStorageIndex(t *testing.T) { require.NoError(t, err) // no register set case - i, err := accounts.AllocateStorageIndex(address) + i, err := accounts.AllocateSlabIndex(address) require.NoError(t, err) - require.Equal(t, i, atree.StorageIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 1})) + require.Equal(t, i, atree.SlabIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 1})) // register already set case - i, err = accounts.AllocateStorageIndex(address) + i, err = accounts.AllocateSlabIndex(address) require.NoError(t, err) - require.Equal(t, i, atree.StorageIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 2})) + require.Equal(t, i, atree.SlabIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 2})) // register update successful - i, err = accounts.AllocateStorageIndex(address) + i, err = accounts.AllocateSlabIndex(address) require.NoError(t, err) - require.Equal(t, i, atree.StorageIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 3})) + require.Equal(t, i, atree.SlabIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 3})) } diff --git a/fvm/environment/mock/accounts.go b/fvm/environment/mock/accounts.go index ee4656a4be8..413a25b9b4e 100644 --- a/fvm/environment/mock/accounts.go +++ b/fvm/environment/mock/accounts.go @@ -15,20 +15,20 @@ type Accounts struct { mock.Mock } -// AllocateStorageIndex provides a mock function with given fields: address -func (_m *Accounts) AllocateStorageIndex(address flow.Address) (atree.StorageIndex, error) { +// AllocateSlabIndex provides a mock function with given fields: address +func (_m *Accounts) AllocateSlabIndex(address flow.Address) (atree.SlabIndex, error) { ret := _m.Called(address) - var r0 atree.StorageIndex + var r0 atree.SlabIndex var r1 error - if rf, ok := ret.Get(0).(func(flow.Address) (atree.StorageIndex, error)); ok { + if rf, ok := ret.Get(0).(func(flow.Address) (atree.SlabIndex, error)); ok { return rf(address) } - if rf, ok := ret.Get(0).(func(flow.Address) atree.StorageIndex); ok { + if rf, ok := ret.Get(0).(func(flow.Address) atree.SlabIndex); ok { r0 = rf(address) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(atree.StorageIndex) + r0 = ret.Get(0).(atree.SlabIndex) } } diff --git a/fvm/environment/mock/environment.go b/fvm/environment/mock/environment.go index 63b52c751a4..63975b196fc 100644 --- a/fvm/environment/mock/environment.go +++ b/fvm/environment/mock/environment.go @@ -134,20 +134,20 @@ func (_m *Environment) AddEncodedAccountKey(address common.Address, publicKey [] return r0 } -// AllocateStorageIndex provides a mock function with given fields: owner -func (_m *Environment) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { +// AllocateSlabIndex provides a mock function with given fields: owner +func (_m *Environment) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { ret := _m.Called(owner) - var r0 atree.StorageIndex + var r0 atree.SlabIndex var r1 error - if rf, ok := ret.Get(0).(func([]byte) (atree.StorageIndex, error)); ok { + if rf, ok := ret.Get(0).(func([]byte) (atree.SlabIndex, error)); ok { return rf(owner) } - if rf, ok := ret.Get(0).(func([]byte) atree.StorageIndex); ok { + if rf, ok := ret.Get(0).(func([]byte) atree.SlabIndex); ok { r0 = rf(owner) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(atree.StorageIndex) + r0 = ret.Get(0).(atree.SlabIndex) } } diff --git a/fvm/environment/mock/value_store.go b/fvm/environment/mock/value_store.go index acfc3918545..59c54d7dac2 100644 --- a/fvm/environment/mock/value_store.go +++ b/fvm/environment/mock/value_store.go @@ -13,20 +13,20 @@ type ValueStore struct { mock.Mock } -// AllocateStorageIndex provides a mock function with given fields: owner -func (_m *ValueStore) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { +// AllocateSlabIndex provides a mock function with given fields: owner +func (_m *ValueStore) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { ret := _m.Called(owner) - var r0 atree.StorageIndex + var r0 atree.SlabIndex var r1 error - if rf, ok := ret.Get(0).(func([]byte) (atree.StorageIndex, error)); ok { + if rf, ok := ret.Get(0).(func([]byte) (atree.SlabIndex, error)); ok { return rf(owner) } - if rf, ok := ret.Get(0).(func([]byte) atree.StorageIndex); ok { + if rf, ok := ret.Get(0).(func([]byte) atree.SlabIndex); ok { r0 = rf(owner) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(atree.StorageIndex) + r0 = ret.Get(0).(atree.SlabIndex) } } diff --git a/fvm/environment/value_store.go b/fvm/environment/value_store.go index 8113de6762c..4f768378500 100644 --- a/fvm/environment/value_store.go +++ b/fvm/environment/value_store.go @@ -20,7 +20,7 @@ type ValueStore interface { ValueExists(owner []byte, key []byte) (bool, error) - AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) + AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) } type ParseRestrictedValueStore struct { @@ -82,16 +82,16 @@ func (store ParseRestrictedValueStore) ValueExists( key) } -func (store ParseRestrictedValueStore) AllocateStorageIndex( +func (store ParseRestrictedValueStore) AllocateSlabIndex( owner []byte, ) ( - atree.StorageIndex, + atree.SlabIndex, error, ) { return parseRestrict1Arg1Ret( store.txnState, trace.FVMEnvAllocateStorageIndex, - store.impl.AllocateStorageIndex, + store.impl.AllocateSlabIndex, owner) } @@ -189,26 +189,26 @@ func (store *valueStore) ValueExists( return len(v) > 0, nil } -// AllocateStorageIndex allocates new storage index under the owner accounts +// AllocateSlabIndex allocates new storage index under the owner accounts // to store a new register. -func (store *valueStore) AllocateStorageIndex( +func (store *valueStore) AllocateSlabIndex( owner []byte, ) ( - atree.StorageIndex, + atree.SlabIndex, error, ) { defer store.tracer.StartChildSpan(trace.FVMEnvAllocateStorageIndex).End() err := store.meter.MeterComputation(ComputationKindAllocateStorageIndex, 1) if err != nil { - return atree.StorageIndex{}, fmt.Errorf( + return atree.SlabIndex{}, fmt.Errorf( "allocate storage index failed: %w", err) } - v, err := store.accounts.AllocateStorageIndex(flow.BytesToAddress(owner)) + v, err := store.accounts.AllocateSlabIndex(flow.BytesToAddress(owner)) if err != nil { - return atree.StorageIndex{}, fmt.Errorf( + return atree.SlabIndex{}, fmt.Errorf( "storage address allocation failed: %w", err) } diff --git a/fvm/evm/emulator/state/collection.go b/fvm/evm/emulator/state/collection.go index 780d81652df..80b8c9621e6 100644 --- a/fvm/evm/emulator/state/collection.go +++ b/fvm/evm/emulator/state/collection.go @@ -47,16 +47,16 @@ func NewCollectionProvider( // calling twice for the same collection might result in odd-behaviours // currently collection provider doesn't do any internal caching to protect aginast these cases func (cp *CollectionProvider) CollectionByID(collectionID []byte) (*Collection, error) { - storageID, err := atree.NewStorageIDFromRawBytes(collectionID) + slabID, err := atree.NewSlabIDFromRawBytes(collectionID) if err != nil { return nil, err } - // sanity check the storage ID address - if storageID.Address != cp.rootAddr { - return nil, fmt.Errorf("root address mismatch %x != %x", storageID.Address, cp.rootAddr) - } + // TODO: sanity check the slab ID address + // if slabID.Address != cp.rootAddr { + // return nil, fmt.Errorf("root address mismatch %x != %x", storageID.Address, cp.rootAddr) + // } - omap, err := atree.NewMapWithRootID(cp.storage, storageID, atree.NewDefaultDigesterBuilder()) + omap, err := atree.NewMapWithRootID(cp.storage, slabID, atree.NewDefaultDigesterBuilder()) if err != nil { return nil, err } @@ -74,7 +74,7 @@ func (cp *CollectionProvider) NewCollection() (*Collection, error) { return nil, err } storageIDBytes := make([]byte, storageIDSize) - _, err = omap.StorageID().ToRawBytes(storageIDBytes) + _, err = omap.SlabID().ToRawBytes(storageIDBytes) if err != nil { return nil, err } @@ -110,7 +110,7 @@ func (c *Collection) CollectionID() []byte { // // if key doesn't exist it returns nil (no error) func (c *Collection) Get(key []byte) ([]byte, error) { - data, err := c.omap.Get(compare, hashInputProvider, NewByteStringValue(key)) + value, err := c.omap.Get(compare, hashInputProvider, NewByteStringValue(key)) if err != nil { var keyNotFoundError *atree.KeyNotFoundError if errors.As(err, &keyNotFoundError) { @@ -119,11 +119,6 @@ func (c *Collection) Get(key []byte) ([]byte, error) { return nil, err } - value, err := data.StoredValue(c.omap.Storage) - if err != nil { - return nil, err - } - return value.(ByteStringValue).Bytes(), nil } @@ -136,9 +131,9 @@ func (c *Collection) Set(key, value []byte) error { return err } - if id, ok := existingValueStorable.(atree.StorageIDStorable); ok { + if id, ok := existingValueStorable.(atree.SlabIDStorable); ok { // NOTE: deep remove isn't necessary because value is ByteStringValue (not container) - err := c.storage.Remove(atree.StorageID(id)) + err := c.storage.Remove(atree.SlabID(id)) if err != nil { return err } @@ -159,9 +154,9 @@ func (c *Collection) Remove(key []byte) error { return err } - if id, ok := existingValueStorable.(atree.StorageIDStorable); ok { + if id, ok := existingValueStorable.(atree.SlabIDStorable); ok { // NOTE: deep remove isn't necessary because value is ByteStringValue (not container) - err := c.storage.Remove(atree.StorageID(id)) + err := c.storage.Remove(atree.SlabID(id)) if err != nil { return err } @@ -175,8 +170,8 @@ func (c *Collection) Destroy() ([][]byte, error) { keys := make([][]byte, c.omap.Count()) i := 0 err := c.omap.PopIterate(func(keyStorable atree.Storable, valueStorable atree.Storable) { - if id, ok := valueStorable.(atree.StorageIDStorable); ok { - err := c.storage.Remove(atree.StorageID(id)) + if id, ok := valueStorable.(atree.SlabIDStorable); ok { + err := c.storage.Remove(atree.SlabID(id)) if err != nil && cachedErr == nil { cachedErr = err } @@ -194,7 +189,7 @@ func (c *Collection) Destroy() ([][]byte, error) { if err != nil { return keys, err } - return keys, c.storage.Remove(c.omap.StorageID()) + return keys, c.storage.Remove(c.omap.SlabID()) } // Size returns the number of items in the collection @@ -229,24 +224,7 @@ func (v ByteStringValue) Storable(storage atree.SlabStorage, address atree.Addre } // Create StorableSlab - id, err := storage.GenerateStorageID(address) - if err != nil { - return nil, err - } - - slab := &atree.StorableSlab{ - StorageID: id, - Storable: v, - } - - // Store StorableSlab in storage - err = storage.Store(id, slab) - if err != nil { - return nil, err - } - - // Return storage id as storable - return atree.StorageIDStorable(id), nil + return atree.NewStorableSlab(storage, address, v) } func (v ByteStringValue) Encode(enc *atree.Encoder) error { @@ -311,7 +289,7 @@ func (v ByteStringValue) Bytes() []byte { return v.data } -func decodeStorable(dec *cbor.StreamDecoder, _ atree.StorageID) (atree.Storable, error) { +func decodeStorable(dec *cbor.StreamDecoder, slabID atree.SlabID, inlinedExtraData []atree.ExtraData) (atree.Storable, error) { t, err := dec.NextType() if err != nil { return nil, err @@ -333,8 +311,31 @@ func decodeStorable(dec *cbor.StreamDecoder, _ atree.StorageID) (atree.Storable, switch tagNumber { - case atree.CBORTagStorageID: - return atree.DecodeStorageIDStorable(dec) + case atree.CBORTagSlabID: + return atree.DecodeSlabIDStorable(dec) + + case atree.CBORTagInlinedArray: + return atree.DecodeInlinedArrayStorable( + dec, + decodeStorable, + slabID, + inlinedExtraData) + + case atree.CBORTagInlinedMap: + return atree.DecodeInlinedMapStorable( + dec, + decodeStorable, + slabID, + inlinedExtraData, + ) + + case atree.CBORTagInlinedCompactMap: + return atree.DecodeInlinedCompactMapStorable( + dec, + decodeStorable, + slabID, + inlinedExtraData, + ) default: return nil, fmt.Errorf("invalid tag number %d", tagNumber) @@ -403,11 +404,23 @@ type emptyTypeInfo struct{} var _ atree.TypeInfo = emptyTypeInfo{} +func (emptyTypeInfo) IsComposite() bool { + return false +} + +func (emptyTypeInfo) Identifier() string { + return "" +} + +func (e emptyTypeInfo) Copy() atree.TypeInfo { + return e +} + func (emptyTypeInfo) Encode(e *cbor.StreamEncoder) error { return e.EncodeNil() } -func (i emptyTypeInfo) Equal(other atree.TypeInfo) bool { +func (emptyTypeInfo) Equal(other atree.TypeInfo) bool { _, ok := other.(emptyTypeInfo) return ok } diff --git a/fvm/evm/emulator/state/stateDB_test.go b/fvm/evm/emulator/state/stateDB_test.go index 2d45395a72e..d137fdd7b19 100644 --- a/fvm/evm/emulator/state/stateDB_test.go +++ b/fvm/evm/emulator/state/stateDB_test.go @@ -247,8 +247,8 @@ func TestStateDB(t *testing.T) { SetValueFunc: func(owner, key, value []byte) error { return atree.NewUserError(fmt.Errorf("key not found")) }, - AllocateStorageIndexFunc: func(owner []byte) (atree.StorageIndex, error) { - return atree.StorageIndex{}, nil + AllocateStorageIndexFunc: func(owner []byte) (atree.SlabIndex, error) { + return atree.SlabIndex{}, nil }, } db, err := state.NewStateDB(ledger, rootAddr) @@ -271,8 +271,8 @@ func TestStateDB(t *testing.T) { SetValueFunc: func(owner, key, value []byte) error { return atree.NewFatalError(fmt.Errorf("key not found")) }, - AllocateStorageIndexFunc: func(owner []byte) (atree.StorageIndex, error) { - return atree.StorageIndex{}, nil + AllocateStorageIndexFunc: func(owner []byte) (atree.SlabIndex, error) { + return atree.SlabIndex{}, nil }, } db, err := state.NewStateDB(ledger, rootAddr) diff --git a/fvm/evm/testutils/backend.go b/fvm/evm/testutils/backend.go index 8c73fbf02f3..93a743195d5 100644 --- a/fvm/evm/testutils/backend.go +++ b/fvm/evm/testutils/backend.go @@ -76,14 +76,14 @@ func GetSimpleValueStore() *TestValueStore { bytesRead += len(fk) + len(value) return len(value) > 0, nil }, - AllocateStorageIndexFunc: func(owner []byte) (atree.StorageIndex, error) { + AllocateStorageIndexFunc: func(owner []byte) (atree.SlabIndex, error) { index := allocator[string(owner)] var data [8]byte allocator[string(owner)] = index + 1 binary.BigEndian.PutUint64(data[:], index) bytesRead += len(owner) + 8 bytesWritten += len(owner) + 8 - return atree.StorageIndex(data), nil + return atree.SlabIndex(data), nil }, TotalStorageSizeFunc: func() int { size := 0 @@ -179,7 +179,7 @@ type TestValueStore struct { GetValueFunc func(owner, key []byte) ([]byte, error) SetValueFunc func(owner, key, value []byte) error ValueExistsFunc func(owner, key []byte) (bool, error) - AllocateStorageIndexFunc func(owner []byte) (atree.StorageIndex, error) + AllocateStorageIndexFunc func(owner []byte) (atree.SlabIndex, error) TotalStorageSizeFunc func() int TotalBytesReadFunc func() int TotalBytesWrittenFunc func() int @@ -210,7 +210,7 @@ func (vs *TestValueStore) ValueExists(owner, key []byte) (bool, error) { return vs.ValueExistsFunc(owner, key) } -func (vs *TestValueStore) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { +func (vs *TestValueStore) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { if vs.AllocateStorageIndexFunc == nil { panic("method not set") } diff --git a/fvm/evm/testutils/cadence.go b/fvm/evm/testutils/cadence.go index a35070c3f69..d559f2ebe50 100644 --- a/fvm/evm/testutils/cadence.go +++ b/fvm/evm/testutils/cadence.go @@ -84,7 +84,7 @@ type TestLedger struct { OnValueExists func(owner, key []byte) (exists bool, err error) OnGetValue func(owner, key []byte) (value []byte, err error) OnSetValue func(owner, key, value []byte) (err error) - OnAllocateStorageIndex func(owner []byte) (atree.StorageIndex, error) + OnAllocateStorageIndex func(owner []byte) (atree.SlabIndex, error) } var _ atree.Ledger = TestLedger{} @@ -101,7 +101,7 @@ func (s TestLedger) ValueExists(owner, key []byte) (exists bool, err error) { return s.OnValueExists(owner, key) } -func (s TestLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { +func (s TestLedger) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { return s.OnAllocateStorageIndex(owner) } @@ -147,7 +147,7 @@ func NewTestLedger( } return nil }, - OnAllocateStorageIndex: func(owner []byte) (result atree.StorageIndex, err error) { + OnAllocateStorageIndex: func(owner []byte) (result atree.SlabIndex, err error) { index := storageIndices[string(owner)] + 1 storageIndices[string(owner)] = index binary.BigEndian.PutUint64(result[:], index) @@ -330,8 +330,8 @@ func (i *TestRuntimeInterface) SetValue(owner, key, value []byte) (err error) { return i.Storage.SetValue(owner, key, value) } -func (i *TestRuntimeInterface) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - return i.Storage.AllocateStorageIndex(owner) +func (i *TestRuntimeInterface) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { + return i.Storage.AllocateSlabIndex(owner) } func (i *TestRuntimeInterface) CreateAccount(payer runtime.Address) (address runtime.Address, err error) { diff --git a/go.mod b/go.mod index 964a9ac2761..f419c5c6bc3 100644 --- a/go.mod +++ b/go.mod @@ -50,8 +50,8 @@ require ( github.com/multiformats/go-multiaddr v0.9.0 github.com/multiformats/go-multiaddr-dns v0.3.1 github.com/multiformats/go-multihash v0.2.3 - github.com/onflow/atree v0.6.0 - github.com/onflow/cadence v0.42.7 + github.com/onflow/atree v0.6.1-0.20240124211117-2fbf86010ae6 + github.com/onflow/cadence v0.42.8-0.20240124212703-3e2e257416db github.com/onflow/crypto v0.25.0 github.com/onflow/flow v0.3.4 github.com/onflow/flow-core-contracts/lib/go/contracts v0.15.1-0.20231219201108-fbdb10b0a2da diff --git a/go.sum b/go.sum index d416631722f..a33155423a1 100644 --- a/go.sum +++ b/go.sum @@ -1341,11 +1341,11 @@ github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1 github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x071HgCF/0v5hQcaE5qqjc2UqN5gCU8h5Mk6uqpOg= -github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c= -github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc= +github.com/onflow/atree v0.6.1-0.20240124211117-2fbf86010ae6 h1:FwSfDsR7ipLoL5Iq0TR5Oe7PwOftjbksodIoY1nx6bM= +github.com/onflow/atree v0.6.1-0.20240124211117-2fbf86010ae6/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A= github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA= -github.com/onflow/cadence v0.42.7 h1:Qp9VYX901saO7wPwF/rwV4cMS+0mfWxnm9EqbYElYy4= -github.com/onflow/cadence v0.42.7/go.mod h1:raU8va8QRyTa/eUbhej4mbyW2ETePfSaywoo36MddgE= +github.com/onflow/cadence v0.42.8-0.20240124212703-3e2e257416db h1:YfH6CL5JrE1vbXCBkhx5apdq6QK6mXsYvfpvyLQkdNY= +github.com/onflow/cadence v0.42.8-0.20240124212703-3e2e257416db/go.mod h1:/0kNF0UGnFEJhWaPqVH7zZAA82sSh9udBgCGACw4PEM= github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/flow v0.3.4 h1:FXUWVdYB90f/rjNcY0Owo30gL790tiYff9Pb/sycXYE= diff --git a/insecure/go.mod b/insecure/go.mod index 7e6bc5a1bee..446d8eff7a9 100644 --- a/insecure/go.mod +++ b/insecure/go.mod @@ -204,8 +204,8 @@ require ( github.com/multiformats/go-varint v0.0.7 // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect - github.com/onflow/atree v0.6.0 // indirect - github.com/onflow/cadence v0.42.7 // indirect + github.com/onflow/atree v0.6.1-0.20240124211117-2fbf86010ae6 // indirect + github.com/onflow/cadence v0.42.8-0.20240124212703-3e2e257416db // indirect github.com/onflow/flow-core-contracts/lib/go/contracts v0.15.1-0.20231219201108-fbdb10b0a2da // indirect github.com/onflow/flow-core-contracts/lib/go/templates v0.15.1-0.20231219201108-fbdb10b0a2da // indirect github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20230711213910-baad011d2b13 // indirect diff --git a/insecure/go.sum b/insecure/go.sum index 4872f6e5f04..ff2c31cb39b 100644 --- a/insecure/go.sum +++ b/insecure/go.sum @@ -1317,11 +1317,11 @@ github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1 github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x071HgCF/0v5hQcaE5qqjc2UqN5gCU8h5Mk6uqpOg= -github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c= -github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc= +github.com/onflow/atree v0.6.1-0.20240124211117-2fbf86010ae6 h1:FwSfDsR7ipLoL5Iq0TR5Oe7PwOftjbksodIoY1nx6bM= +github.com/onflow/atree v0.6.1-0.20240124211117-2fbf86010ae6/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A= github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA= -github.com/onflow/cadence v0.42.7 h1:Qp9VYX901saO7wPwF/rwV4cMS+0mfWxnm9EqbYElYy4= -github.com/onflow/cadence v0.42.7/go.mod h1:raU8va8QRyTa/eUbhej4mbyW2ETePfSaywoo36MddgE= +github.com/onflow/cadence v0.42.8-0.20240124212703-3e2e257416db h1:YfH6CL5JrE1vbXCBkhx5apdq6QK6mXsYvfpvyLQkdNY= +github.com/onflow/cadence v0.42.8-0.20240124212703-3e2e257416db/go.mod h1:/0kNF0UGnFEJhWaPqVH7zZAA82sSh9udBgCGACw4PEM= github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/flow-core-contracts/lib/go/contracts v0.15.1-0.20231219201108-fbdb10b0a2da h1:8CEioYNnP0rwjnRbKDgs8SmiQTsdaroeX4d/Q3pQuh4= diff --git a/model/flow/ledger_test.go b/model/flow/ledger_test.go index b287c3d3bb0..f188e1625cf 100644 --- a/model/flow/ledger_test.go +++ b/model/flow/ledger_test.go @@ -87,7 +87,7 @@ func TestRegisterID_IsInternalState(t *testing.T) { func TestRegisterID_String(t *testing.T) { t.Run("atree slab", func(t *testing.T) { // slab with 189 should result in \\xbd - slabIndex := atree.StorageIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 189}) + slabIndex := atree.SlabIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 189}) id := flow.NewRegisterID( flow.BytesToAddress([]byte{1, 2, 3, 10}),