Skip to content

Commit

Permalink
Merge pull request #5972 from onflow/fxamacker/add-atree-inlining-to-…
Browse files Browse the repository at this point in the history
…v0.33
  • Loading branch information
turbolent authored May 23, 2024
2 parents af72343 + b57feb5 commit ec1e8bd
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 157 deletions.
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
}
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
2 changes: 1 addition & 1 deletion cmd/util/ledger/util/migration_runtime_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (m MigrationRuntimeInterface) GetAccountContractNames(_ runtime.Address) ([
panic("unexpected GetAccountContractNames call")
}

func (m MigrationRuntimeInterface) AllocateStorageIndex(_ []byte) (atree.StorageIndex, error) {
func (m MigrationRuntimeInterface) AllocateStorageIndex(_ []byte) (atree.SlabIndex, error) {
panic("unexpected AllocateStorageIndex call")
}

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 @@ -108,8 +108,8 @@ var _ common.MemoryGauge = (*NopMemoryGauge)(nil)
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 @@ -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) {
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("8d9d52a66a832898f6f2416b703759b7ecd1eb390db6d5e727c2daeec001ffc6")
expectedStateCommitmentBytes, _ := hex.DecodeString("392f542db3a5e08acbbd96a84d40e79b3883f9ede2e8a04de9b9ae49d5446997")
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
Loading

0 comments on commit ec1e8bd

Please sign in to comment.