Skip to content

Commit

Permalink
Merge branch 'feature/combine-domain-payloads-and-domain-storage-maps…
Browse files Browse the repository at this point in the history
…' into fxamacker/reduce-register-reads-for-new-accounts
  • Loading branch information
fxamacker committed Nov 25, 2024
2 parents b51053d + 157d322 commit 9b3df51
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 76 deletions.
16 changes: 12 additions & 4 deletions runtime/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ type Storage struct {
var _ atree.SlabStorage = &Storage{}
var _ interpreter.Storage = &Storage{}

func NewStorage(
func NewPersistentSlabStorage(
ledger atree.Ledger,
memoryGauge common.MemoryGauge,
config StorageConfig,
) *Storage {
) *atree.PersistentSlabStorage {
decodeStorable := func(
decoder *cbor.StreamDecoder,
slabID atree.SlabID,
Expand All @@ -103,13 +102,22 @@ func NewStorage(
}

ledgerStorage := atree.NewLedgerBaseStorage(ledger)
persistentSlabStorage := atree.NewPersistentSlabStorage(

return atree.NewPersistentSlabStorage(
ledgerStorage,
interpreter.CBOREncMode,
interpreter.CBORDecMode,
decodeStorable,
decodeTypeInfo,
)
}

func NewStorage(
ledger atree.Ledger,
memoryGauge common.MemoryGauge,
config StorageConfig,
) *Storage {
persistentSlabStorage := NewPersistentSlabStorage(ledger, memoryGauge)

accountStorageV1 := NewAccountStorageV1(
ledger,
Expand Down
125 changes: 53 additions & 72 deletions runtime/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"strings"
"testing"

"github.com/fxamacker/cbor/v2"
"github.com/onflow/atree"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -8120,7 +8119,7 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
},
},
{
Expand All @@ -8141,7 +8140,7 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
// domain storage map is created and cached in the first GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
},
},
// Test cases with storageFormatV2Enabled = true
Expand Down Expand Up @@ -8179,8 +8178,8 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
},
},
{
Expand Down Expand Up @@ -8248,16 +8247,16 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
// domain storage map is created and cached in the first GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainPathPrivate.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainPathPublic.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainContract.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainInbox.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainCapabilityController.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainCapabilityControllerTag.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainPathCapability.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainAccountCapability.Identifier(): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathPrivate): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathPublic): {},
concatRegisterAddressAndDomain(address, common.StorageDomainContract): {},
concatRegisterAddressAndDomain(address, common.StorageDomainInbox): {},
concatRegisterAddressAndDomain(address, common.StorageDomainCapabilityController): {},
concatRegisterAddressAndDomain(address, common.StorageDomainCapabilityControllerTag): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathCapability): {},
concatRegisterAddressAndDomain(address, common.StorageDomainAccountCapability): {},
},
},
}
Expand Down Expand Up @@ -8328,7 +8327,7 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
return func() (storedValues map[string][]byte, StorageIndices map[string]uint64) {
ledger := NewTestLedger(nil, nil)

persistentSlabStorage := newSlabStorage(ledger)
persistentSlabStorage := NewPersistentSlabStorage(ledger, nil)

orderedMap, err := atree.NewMap(
persistentSlabStorage,
Expand Down Expand Up @@ -8402,7 +8401,7 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
},
},
{
Expand All @@ -8425,7 +8424,7 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// GetDomainStorageMap(0).
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
},
},
{
Expand Down Expand Up @@ -8453,8 +8452,8 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// GetDomainStorageMap(0).
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
Expand Down Expand Up @@ -8482,8 +8481,8 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// GetDomainStorageMap(0).
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
// Test cases with storageFormatV2Enabled = true
Expand Down Expand Up @@ -8519,8 +8518,8 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
},
},
{
Expand Down Expand Up @@ -8566,10 +8565,10 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainPathPrivate.Identifier(): {},
string(address[:]) + "|" + common.StorageDomainPathPublic.Identifier(): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathPrivate): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathPublic): {},
},
},
{
Expand Down Expand Up @@ -8607,9 +8606,9 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
Expand Down Expand Up @@ -8647,9 +8646,9 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndDomain(address, common.StorageDomainPathStorage): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
}
Expand Down Expand Up @@ -8721,7 +8720,7 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
return func() (storedValues map[string][]byte, StorageIndices map[string]uint64) {
ledger := NewTestLedger(nil, nil)

persistentSlabStorage := newSlabStorage(ledger)
persistentSlabStorage := NewPersistentSlabStorage(ledger, nil)

accountOrderedMap, err := atree.NewMap(
persistentSlabStorage,
Expand Down Expand Up @@ -8819,8 +8818,8 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
Expand Down Expand Up @@ -8852,8 +8851,8 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
Expand Down Expand Up @@ -8885,8 +8884,8 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
Expand Down Expand Up @@ -8918,8 +8917,8 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
concatRegisterAddressAndKey(address, []byte(AccountStorageKey)): {},
concatRegisterAddressAndKey(address, []byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
}
Expand Down Expand Up @@ -9104,34 +9103,16 @@ func checkAccountStorageMapData(
require.Contains(tb, rootSlabIDs, accountSlabID)
}

func newSlabStorage(ledger atree.Ledger) *atree.PersistentSlabStorage {
decodeStorable := func(
decoder *cbor.StreamDecoder,
slabID atree.SlabID,
inlinedExtraData []atree.ExtraData,
) (
atree.Storable,
error,
) {
return interpreter.DecodeStorable(
decoder,
slabID,
inlinedExtraData,
nil,
)
}

decodeTypeInfo := func(decoder *cbor.StreamDecoder) (atree.TypeInfo, error) {
return interpreter.DecodeTypeInfo(decoder, nil)
}

ledgerStorage := atree.NewLedgerBaseStorage(ledger)
func concatRegisterAddressAndKey(
address common.Address,
key []byte,
) string {
return string(address[:]) + "|" + string(key)
}

return atree.NewPersistentSlabStorage(
ledgerStorage,
interpreter.CBOREncMode,
interpreter.CBORDecMode,
decodeStorable,
decodeTypeInfo,
)
func concatRegisterAddressAndDomain(
address common.Address,
domain common.StorageDomain,
) string {
return string(address[:]) + "|" + domain.Identifier()
}

0 comments on commit 9b3df51

Please sign in to comment.