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 v0.42 #5736

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/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
2 changes: 1 addition & 1 deletion cmd/util/ledger/reporters/atree_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions cmd/util/ledger/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -99,8 +99,8 @@ func (p PayloadSnapshot) Get(id flow.RegisterID) (flow.RegisterValue, error) {
type PayloadsReadonlyLedger struct {
Snapshot *PayloadSnapshot

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

func (p *PayloadsReadonlyLedger) GetValue(owner, key []byte) (value []byte, err error) {
Expand All @@ -124,12 +124,12 @@ func (p *PayloadsReadonlyLedger) ValueExists(owner, key []byte) (exists bool, er
return ok, nil
}

func (p *PayloadsReadonlyLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) {
if p.AllocateStorageIndexFunc != nil {
return p.AllocateStorageIndexFunc(owner)
func (p *PayloadsReadonlyLedger) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) {
if p.AllocateSlabIndexFunc != nil {
return p.AllocateSlabIndexFunc(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("394b02bd8207a8f8b86d3af0fe428bc2b11228311b652037175db7823446bcbe")
expectedStateCommitmentBytes, _ := hex.DecodeString("63460bc100d7d6a2f5dde4eeaba9c65e195d80585ecec77665b2c2ce754c1c24")
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 @@ -1606,7 +1606,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_993_040), output.Value)
assert.Equal(t, cadence.UFix64(99_993_750), 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 @@ -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
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
14 changes: 7 additions & 7 deletions fvm/environment/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func createByteArray(size int) []byte {
return bytes
}

func TestAccounts_AllocateStorageIndex(t *testing.T) {
func TestAccounts_AllocateSlabIndex(t *testing.T) {
txnState := testutils.NewSimpleTransaction(nil)
accounts := environment.NewAccounts(txnState)
address := flow.HexToAddress("01")
Expand All @@ -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