Skip to content

Commit

Permalink
Integrate Atree inlining & Cadence v0.42 to flow-go
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Jan 29, 2024
1 parent f0b7f2a commit 46a0017
Show file tree
Hide file tree
Showing 24 changed files with 145 additions and 132 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 @@ -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_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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
}
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
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 @@ -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) {
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 @@ -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,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)
}

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) {
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
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
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
Loading

0 comments on commit 46a0017

Please sign in to comment.