Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atree Inlining & Deduplication with Cadence v1.0 #5554

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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())
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
4 changes: 2 additions & 2 deletions cmd/util/ledger/migrations/cadence_value_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,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) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/util/ledger/migrations/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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 @@ -291,8 +291,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,11 +60,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
}
Expand Down Expand Up @@ -113,7 +113,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)
}

Expand All @@ -138,12 +138,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 {
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("ef4d7cf4a852985e51bac9fefce8a04aa885fa716cc73814242ce55b51347e82")
expectedStateCommitmentBytes, _ := hex.DecodeString("0d34780e05327422d9a09a4ceae60ae012c1e93bb9a350cfbd204ccd709bc069")
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
Loading