From 5f968787482150358bb0dbe418c592feb22e7a87 Mon Sep 17 00:00:00 2001 From: jimboj Date: Fri, 23 Aug 2024 16:18:20 +0800 Subject: [PATCH] move logic into imports for better logging --- lib/runtime/storage/trie.go | 15 ----------- lib/runtime/wazero/imports.go | 50 ++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/lib/runtime/storage/trie.go b/lib/runtime/storage/trie.go index bd55a469787..074c6dba7d5 100644 --- a/lib/runtime/storage/trie.go +++ b/lib/runtime/storage/trie.go @@ -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 @@ -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) @@ -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) diff --git a/lib/runtime/wazero/imports.go b/lib/runtime/wazero/imports.go index 71ef19928a0..32535f5774d 100644 --- a/lib/runtime/wazero/imports.go +++ b/lib/runtime/wazero/imports.go @@ -37,6 +37,8 @@ var ( emptyByteVectorEncoded []byte = scale.MustMarshal([]byte{}) noneEncoded []byte = []byte{0x00} allZeroesBytes = [32]byte{} + + childStorageKeyPrefix = []byte(":child_storage:") ) const ( @@ -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 @@ -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 {