Skip to content

Commit

Permalink
Merge pull request #5554 from onflow/fxamacker/atree-register-inlinin…
Browse files Browse the repository at this point in the history
…g-v1.0

Atree Inlining & Deduplication with Cadence v1.0
  • Loading branch information
fxamacker authored Apr 19, 2024
2 parents b4864e0 + 2b70dab commit 53b0738
Show file tree
Hide file tree
Showing 30 changed files with 162 additions and 143 deletions.
2 changes: 1 addition & 1 deletion cmd/util/ledger/migrations/atree_register_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,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(nil)
keys := make([]interpreter.StorageMapKey, 0, storageMap.Count())
Expand Down
4 changes: 2 additions & 2 deletions cmd/util/ledger/migrations/cadence_value_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,13 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue(
}

oldKeys := make([]interpreter.Value, 0, v.Count())
v.IterateKeys(vInterpreter, func(key interpreter.Value) (resume bool) {
v.IterateKeys(vInterpreter, interpreter.EmptyLocationRange, func(key interpreter.Value) (resume bool) {
oldKeys = append(oldKeys, key)
return true
})

newKeys := make([]interpreter.Value, 0, otherDictionary.Count())
otherDictionary.IterateKeys(otherInterpreter, func(key interpreter.Value) (resume bool) {
otherDictionary.IterateKeys(otherInterpreter, interpreter.EmptyLocationRange, func(key interpreter.Value) (resume bool) {
newKeys = append(newKeys, key)
return true
})
Expand Down
8 changes: 4 additions & 4 deletions cmd/util/ledger/migrations/cadence_values_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ func (m *CadenceBaseMigrator) MigrateAccount(
}

// Convert the register ID to a storage ID.
storageID := atree.StorageID{
Address: atree.Address([]byte(registerID.Owner)),
}
copy(storageID.Index[:], registerID.Key[1:])
storageID := atree.NewSlabID(
atree.Address([]byte(registerID.Owner)),
atree.SlabIndex([]byte(registerID.Key[1:])),
)

// Retrieve the slab.
_, _, err = storage.Retrieve(storageID)
Expand Down
4 changes: 2 additions & 2 deletions cmd/util/ledger/migrations/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func checkStorageHealth(
}

// Convert the register ID to a storage ID.
slabID := atree.NewStorageID(
slabID := atree.NewSlabID(
atree.Address([]byte(registerID.Owner)),
atree.StorageIndex([]byte(registerID.Key[1:])))
atree.SlabIndex([]byte(registerID.Key[1:])))

// Retrieve the slab.
_, _, err = storage.Retrieve(slabID)
Expand Down
4 changes: 2 additions & 2 deletions cmd/util/ledger/util/migration_runtime_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,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) {
Expand Down
14 changes: 7 additions & 7 deletions cmd/util/ledger/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ 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
}

type PayloadsReadonlyLedger struct {
Snapshot snapshot.MigrationSnapshot

AllocateStorageIndexFunc func(owner []byte) (atree.StorageIndex, error)
AllocateStorageIndexFunc func(owner []byte) (atree.SlabIndex, error)
SetValueFunc func(owner, key, value []byte) (err error)
}

Expand All @@ -97,12 +97,12 @@ func (p *PayloadsReadonlyLedger) ValueExists(owner, key []byte) (bool, error) {
return exists, 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 snapshot.MigrationSnapshot) *PayloadsReadonlyLedger {
Expand Down
2 changes: 1 addition & 1 deletion engine/execution/state/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
}

func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
expectedStateCommitmentBytes, _ := hex.DecodeString("42aae9eb1f9238cac7b0cdb01515fa6df78435ef59c470484a63b74b8eb68911")
expectedStateCommitmentBytes, _ := hex.DecodeString("df32a75f2c69e477775ae223781e95345c0e9fa680359e1ae66d5550cfb562d0")
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion fvm/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ func TestAccountBalanceFields(t *testing.T) {
_, output, err = vm.Run(ctx, script, snapshotTree)
assert.NoError(t, err)
assert.NoError(t, output.Err)
assert.Equal(t, cadence.UFix64(99_989_590), output.Value)
assert.Equal(t, cadence.UFix64(99_990_950), output.Value)
}),
)

Expand Down
4 changes: 2 additions & 2 deletions fvm/environment/account_key_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,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
Expand Down
12 changes: 6 additions & 6 deletions fvm/environment/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions fvm/environment/accounts_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions fvm/environment/accounts_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions fvm/environment/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
}
12 changes: 6 additions & 6 deletions fvm/environment/mock/accounts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions fvm/environment/mock/environment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions fvm/environment/mock/value_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions fvm/environment/value_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions fvm/evm/backends/wrappedEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (we *WrappedEnvironment) ValueExists(owner, key []byte) (bool, error) {
return b, handleEnvironmentError(err)
}

func (we *WrappedEnvironment) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) {
index, err := we.env.AllocateStorageIndex(owner)
func (we *WrappedEnvironment) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) {
index, err := we.env.AllocateSlabIndex(owner)
return index, handleEnvironmentError(err)
}

Expand Down
Loading

0 comments on commit 53b0738

Please sign in to comment.