Skip to content

Commit

Permalink
Update unit tests to check unique register reads
Browse files Browse the repository at this point in the history
Currently, tests for register reads only check sequence of
register reading.  However, same register can be read multiple
times and all register reads are included in the sequence.

This commit updates tests to explicitly checks unique registers
being read in addition to sequence of registers being read.
  • Loading branch information
fxamacker committed Nov 25, 2024
1 parent f562727 commit 9af92f6
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions runtime/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8096,6 +8096,7 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
expectedDomainStorageMapIsNil bool
expectedReadsFor1stGetDomainStorageMapCall []ownerKeyPair
expectedReadsFor2ndGetDomainStorageMapCall []ownerKeyPair
expectedReadsSet map[string]struct{}
}{
// Test cases with storageFormatV2Enabled = false
{
Expand All @@ -8118,6 +8119,9 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
key: []byte(common.StorageDomainPathStorage.Identifier()),
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
},
},
{
name: "storageFormatV2Enabled = false, domain storage map does not exist, createIfNotExists = true",
Expand All @@ -8136,6 +8140,9 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
// No register reads from the second GetDomainStorageMap() because
// domain storage map is created and cached in the first GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
},
},
// Test cases with storageFormatV2Enabled = true
{
Expand Down Expand Up @@ -8171,6 +8178,10 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
key: []byte(common.StorageDomainPathStorage.Identifier()),
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
},
},
{
name: "storageFormatV2Enabled = true, domain storage map does not exist, createIfNotExists = true",
Expand Down Expand Up @@ -8236,13 +8247,26 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
// No register reads from the second GetDomainStorageMap() because
// 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(): {},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

var ledgerReads []ownerKeyPair
ledgerReadsSet := make(map[string]struct{})

// Create empty storage
ledger := NewTestLedger(
Expand All @@ -8254,6 +8278,7 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
key: key,
},
)
ledgerReadsSet[string(owner)+"|"+string(key)] = struct{}{}
},
nil)

Expand All @@ -8278,6 +8303,12 @@ func TestGetDomainStorageMapRegisterReadsForNewAccount(t *testing.T) {
domainStorageMap = storage.GetDomainStorageMap(inter, address, tc.domain, tc.createIfNotExists)
require.Equal(t, tc.expectedDomainStorageMapIsNil, domainStorageMap == nil)
require.Equal(t, tc.expectedReadsFor2ndGetDomainStorageMapCall, ledgerReads)

// Check underlying ledger reads
require.Equal(t, len(ledgerReadsSet), len(tc.expectedReadsSet))
for k := range ledgerReadsSet {
require.Contains(t, tc.expectedReadsSet, k)
}
})
}
}
Expand Down Expand Up @@ -8346,6 +8377,7 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
expectedDomainStorageMapIsNil bool
expectedReadsFor1stGetDomainStorageMapCall []ownerKeyPair
expectedReadsFor2ndGetDomainStorageMapCall []ownerKeyPair
expectedReadsSet map[string]struct{}
}{
// Test cases with storageFormatV2Enabled = false
{
Expand All @@ -8369,6 +8401,9 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
key: []byte(common.StorageDomainPathStorage.Identifier()),
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
},
},
{
name: "storageFormatV2Enabled = false, domain storage map does not exist, createIfNotExists = true",
Expand All @@ -8389,6 +8424,9 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// domain storage map is created and cached in the first
// GetDomainStorageMap(0).
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
},
},
{
name: "storageFormatV2Enabled = false, domain storage map exists, createIfNotExists = false",
Expand All @@ -8414,6 +8452,10 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// domain storage map is loaded and cached in the first
// GetDomainStorageMap(0).
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
name: "storageFormatV2Enabled = false, domain storage map exists, createIfNotExists = true",
Expand All @@ -8439,6 +8481,10 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
// domain storage map is loaded and cached in the first
// GetDomainStorageMap(0).
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
// Test cases with storageFormatV2Enabled = true
{
Expand Down Expand Up @@ -8472,6 +8518,10 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
key: []byte(common.StorageDomainPathStorage.Identifier()),
},
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + common.StorageDomainPathStorage.Identifier(): {},
},
},
{
name: "storageFormatV2Enabled = true, domain storage map does not exist, createIfNotExists = true",
Expand Down Expand Up @@ -8515,6 +8565,12 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(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(): {},
},
},
{
name: "storageFormatV2Enabled = true, domain storage map exists, createIfNotExists = false",
Expand Down Expand Up @@ -8550,6 +8606,11 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(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[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
name: "storageFormatV2Enabled = true, domain storage map exists, createIfNotExists = true",
Expand Down Expand Up @@ -8585,6 +8646,11 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(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[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
}

Expand All @@ -8594,6 +8660,7 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
storedValues, storedIndices := tc.getStorageData()

var ledgerReads []ownerKeyPair
ledgerReadsSet := make(map[string]struct{})

ledger := NewTestLedgerWithData(
func(owner, key, _ []byte) {
Expand All @@ -8604,6 +8671,7 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
key: key,
},
)
ledgerReadsSet[string(owner)+"|"+string(key)] = struct{}{}
},
nil,
storedValues,
Expand All @@ -8629,6 +8697,12 @@ func TestGetDomainStorageMapRegisterReadsForV1Account(t *testing.T) {
domainStorageMap = storage.GetDomainStorageMap(inter, address, tc.domain, tc.createIfNotExists)
require.Equal(t, tc.expectedDomainStorageMapIsNil, domainStorageMap == nil)
require.Equal(t, tc.expectedReadsFor2ndGetDomainStorageMapCall, ledgerReads)

// Check underlying ledger reads
require.Equal(t, len(ledgerReadsSet), len(tc.expectedReadsSet))
for k := range ledgerReadsSet {
require.Contains(t, tc.expectedReadsSet, k)
}
})
}
}
Expand Down Expand Up @@ -8714,6 +8788,7 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
expectedDomainStorageMapIsNil bool
expectedReadsFor1stGetDomainStorageMapCall []ownerKeyPair
expectedReadsFor2ndGetDomainStorageMapCall []ownerKeyPair
expectedReadsSet map[string]struct{}
}{
{
name: "domain storage map does not exist, createIfNotExists = false",
Expand Down Expand Up @@ -8743,6 +8818,10 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// account storage map is loaded and cached from first
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
name: "domain storage map does not exist, createIfNotExists = true",
Expand Down Expand Up @@ -8772,6 +8851,10 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// domain storage map is created and cached in the first
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
name: "domain storage map exists, createIfNotExists = false",
Expand Down Expand Up @@ -8801,6 +8884,10 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// domain storage map is created and cached in the first
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
{
name: "domain storage map exists, createIfNotExists = true",
Expand Down Expand Up @@ -8830,6 +8917,10 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
// domain storage map is created and cached in the first
// GetDomainStorageMap().
},
expectedReadsSet: map[string]struct{}{
string(address[:]) + "|" + AccountStorageKey: {},
string(address[:]) + "|" + string([]byte{'$', 0, 0, 0, 0, 0, 0, 0, 1}): {},
},
},
}

Expand All @@ -8839,6 +8930,7 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
storedValues, storedIndices := tc.getStorageData()

var ledgerReads []ownerKeyPair
ledgerReadsSet := make(map[string]struct{})

ledger := NewTestLedgerWithData(
func(owner, key, _ []byte) {
Expand All @@ -8849,6 +8941,7 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
key: key,
},
)
ledgerReadsSet[string(owner)+"|"+string(key)] = struct{}{}
},
nil,
storedValues,
Expand All @@ -8874,6 +8967,12 @@ func TestGetDomainStorageMapRegisterReadsForV2Account(t *testing.T) {
domainStorageMap = storage.GetDomainStorageMap(inter, address, tc.domain, tc.createIfNotExists)
require.Equal(t, tc.expectedDomainStorageMapIsNil, domainStorageMap == nil)
require.Equal(t, tc.expectedReadsFor2ndGetDomainStorageMapCall, ledgerReads)

// Check underlying ledger reads
require.Equal(t, len(ledgerReadsSet), len(tc.expectedReadsSet))
for k := range ledgerReadsSet {
require.Contains(t, tc.expectedReadsSet, k)
}
})
}
}
Expand Down

0 comments on commit 9af92f6

Please sign in to comment.