Skip to content

Commit

Permalink
move logic into imports for better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj committed Aug 23, 2024
1 parent 87df58f commit 5f96878
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
15 changes: 0 additions & 15 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"golang.org/x/exp/slices"
)

var childStorageKeyPrefix = []byte(":child_storage:")

// TrieState relies on `storageDiff` to perform changes over the current state.
// It has support for transactions using "nested" storageDiff changes
// If the execution of the call is successful, the changes will be applied to
Expand Down Expand Up @@ -212,20 +210,11 @@ func (t *TrieState) NextKey(key []byte) []byte {
return t.state.NextKey(key)
}

// Check if the given prefix starts with the child storage key prefix
func startsWithChildStorageKey(prefix []byte) bool {
return bytes.HasPrefix(prefix, childStorageKeyPrefix)
}

// ClearPrefix deletes all key-value pairs from the trie where the key starts with the given prefix
func (t *TrieState) ClearPrefix(prefix []byte) error {
t.mtx.Lock()
defer t.mtx.Unlock()

if startsWithChildStorageKey(prefix) {
return fmt.Errorf("cannot clear prefix that is part of or contains a child storage key")
}

if currentTx := t.getCurrentTransaction(); currentTx != nil {
keysOnState := make([]string, 0)

Expand All @@ -247,10 +236,6 @@ func (t *TrieState) ClearPrefixLimit(prefix []byte, limit uint32) (
t.mtx.Lock()
defer t.mtx.Unlock()

if startsWithChildStorageKey(prefix) {
return deleted, allDeleted, fmt.Errorf("cannot clear prefix that is part of or contains a child storage key")
}

if currentTx := t.getCurrentTransaction(); currentTx != nil {
keysOnState := make([]string, 0)

Expand Down
50 changes: 31 additions & 19 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
emptyByteVectorEncoded []byte = scale.MustMarshal([]byte{})
noneEncoded []byte = []byte{0x00}
allZeroesBytes = [32]byte{}

childStorageKeyPrefix = []byte(":child_storage:")
)

const (
Expand Down Expand Up @@ -2065,10 +2067,15 @@ func ext_storage_clear_prefix_version_1(ctx context.Context, m api.Module, prefi
prefix := read(m, prefixSpan)
logger.Debugf("prefix: 0x%x", prefix)

err := storage.ClearPrefix(prefix)
if err != nil {
panic(err)
if !bytes.HasPrefix(prefix, childStorageKeyPrefix) {
err := storage.ClearPrefix(prefix)
if err != nil {
panic(err)
}
} else {
logger.Warnf("cannot clear prefix that is part of or contains a child storage key")
}

}

// toKillStorageResultEnum encodes the `allRemoved` flag and
Expand Down Expand Up @@ -2117,25 +2124,30 @@ func ext_storage_clear_prefix_version_2(ctx context.Context, m api.Module, prefi
limitPtr = &maxLimit
}

numRemoved, all, err := storage.ClearPrefixLimit(prefix, *limitPtr)
if err != nil {
logger.Errorf("failed to clear prefix limit: %s", err)
panic(err)
}
if !bytes.HasPrefix(prefix, childStorageKeyPrefix) {
numRemoved, all, err := storage.ClearPrefixLimit(prefix, *limitPtr)
if err != nil {
logger.Errorf("failed to clear prefix limit: %s", err)
panic(err)
}

encBytes, err := toKillStorageResultEnum(all, numRemoved)
if err != nil {
logger.Errorf("failed to allocate memory: %s", err)
panic(err)
}
encBytes, err := toKillStorageResultEnum(all, numRemoved)
if err != nil {
logger.Errorf("failed to allocate memory: %s", err)
panic(err)
}

valueSpan, err := write(m, rtCtx.Allocator, encBytes)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
}
valueSpan, err := write(m, rtCtx.Allocator, encBytes)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
}

return valueSpan
return valueSpan
} else {
logger.Warnf("cannot clear prefix that is part of or contains a child storage key")
}
return 0
}

func ext_storage_exists_version_1(ctx context.Context, m api.Module, keySpan uint64) uint32 {
Expand Down

0 comments on commit 5f96878

Please sign in to comment.