From 39f784992e32523f2c6cfaa945f6d38cbae1bb72 Mon Sep 17 00:00:00 2001 From: jimboj Date: Fri, 20 Sep 2024 00:25:01 +0900 Subject: [PATCH] reduce diff --- lib/runtime/storage/storagediff.go | 1 - lib/runtime/storage/trie.go | 5 ----- lib/runtime/wazero/imports.go | 4 ++++ pkg/trie/inmemory/child_storage.go | 4 ---- pkg/trie/inmemory/in_memory.go | 4 +++- pkg/trie/layout.go | 1 + scripts/retrieve_state/retrieve_state.go | 2 -- 7 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/runtime/storage/storagediff.go b/lib/runtime/storage/storagediff.go index 04035bf3c5..a4ccf14341 100644 --- a/lib/runtime/storage/storagediff.go +++ b/lib/runtime/storage/storagediff.go @@ -141,7 +141,6 @@ func (cs *storageDiff) clearPrefixInChild(keyToChild string, prefix []byte, // optional limit. It returns the number of keys deleted and a boolean // indicating if all keys with the prefix were removed. func (cs *storageDiff) clearPrefix(prefix []byte, trieKeys []string, limit int) (deleted uint32, allDeleted bool) { - //newKeys := maps.Keys(cs.upserts) keysToClear := maps.Keys(cs.upserts) for _, k := range trieKeys { if _, ok := cs.upserts[k]; !ok { diff --git a/lib/runtime/storage/trie.go b/lib/runtime/storage/trie.go index 989252cefa..2bf58a7a87 100644 --- a/lib/runtime/storage/trie.go +++ b/lib/runtime/storage/trie.go @@ -165,11 +165,6 @@ func (t *TrieState) Delete(key []byte) (err error) { return nil } - err = t.state.Delete(key) - if err != nil { - return err - } - return t.state.Delete(key) } diff --git a/lib/runtime/wazero/imports.go b/lib/runtime/wazero/imports.go index 275acbfd48..f8be921fdd 100644 --- a/lib/runtime/wazero/imports.go +++ b/lib/runtime/wazero/imports.go @@ -96,6 +96,7 @@ func ext_logging_log_version_1(_ context.Context, m api.Module, level int32, tar msg := string(read(m, msgData)) line := fmt.Sprintf("target=%s message=%s", target, msg) + switch int(level) { case 0: logger.Critical(line) @@ -858,6 +859,7 @@ func ext_trie_blake2_256_ordered_root_version_2( } var entries trie.Entries + for i, value := range values { key, err := scale.Marshal(big.NewInt(int64(i))) if err != nil { @@ -2048,6 +2050,7 @@ func ext_storage_clear_version_1(ctx context.Context, m api.Module, keySpan uint storage := rtCtx.Storage key := read(m, keySpan) + logger.Debugf("key: 0x%x", key) err := storage.Delete(key) if err != nil { @@ -2202,6 +2205,7 @@ func ext_storage_next_key_version_1(ctx context.Context, m api.Module, keySpan u storage := rtCtx.Storage key := read(m, keySpan) + next := storage.NextKey(key) logger.Debugf( "key: 0x%x; next key 0x%x", diff --git a/pkg/trie/inmemory/child_storage.go b/pkg/trie/inmemory/child_storage.go index 9283a02e21..18976669e2 100644 --- a/pkg/trie/inmemory/child_storage.go +++ b/pkg/trie/inmemory/child_storage.go @@ -4,7 +4,6 @@ package inmemory import ( - "bytes" "errors" "fmt" @@ -70,9 +69,6 @@ func (t *InMemoryTrie) GetChildTries() map[common.Hash]trie.Trie { // PutIntoChild puts a key-value pair into the child trie located in the main trie at key :child_storage:[keyToChild] func (t *InMemoryTrie) PutIntoChild(keyToChild, key, value []byte) error { - if bytes.Equal(common.MustHexToBytes("0x0000000007a9b3851a0966360500"), value) { - fmt.Println("INSERTING OUR VALUE INTO CHILD") - } child, err := t.getInternalChildTrie(keyToChild) if err != nil { if errors.Is(err, trie.ErrChildTrieDoesNotExist) { diff --git a/pkg/trie/inmemory/in_memory.go b/pkg/trie/inmemory/in_memory.go index 8d215516d2..bb9f553b7c 100644 --- a/pkg/trie/inmemory/in_memory.go +++ b/pkg/trie/inmemory/in_memory.go @@ -6,13 +6,14 @@ package inmemory import ( "bytes" "fmt" + "reflect" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/pkg/trie" "github.com/ChainSafe/gossamer/pkg/trie/codec" "github.com/ChainSafe/gossamer/pkg/trie/db" "github.com/ChainSafe/gossamer/pkg/trie/node" "github.com/ChainSafe/gossamer/pkg/trie/tracking" - "reflect" ) // InMemoryTrie is a base 16 modified Merkle Patricia trie. @@ -463,6 +464,7 @@ func (t *InMemoryTrie) insertInBranch(parentBranch *node.Node, key, value []byte } if bytes.HasPrefix(key, parentBranch.PartialKey) { + // key is included in parent branch key commonPrefixLength := lenCommonPrefix(key, parentBranch.PartialKey) childIndex := key[commonPrefixLength] remainingKey := key[commonPrefixLength+1:] diff --git a/pkg/trie/layout.go b/pkg/trie/layout.go index b8a1352d6b..389488ed8a 100644 --- a/pkg/trie/layout.go +++ b/pkg/trie/layout.go @@ -86,6 +86,7 @@ func (v TrieLayout) Root(t Trie, entries Entries) (common.Hash, error) { // TODO: is there any way to calculate the hash without building a trie? // Eg: https://github.com/paritytech/trie/blob/542829a8195c12b67eef05e9020ec7a6d9313c3f/trie-root/src/lib.rs#L273-L388 t.SetVersion(v) + for _, kv := range entries { err := t.Put(kv.Key, kv.Value) if err != nil { diff --git a/scripts/retrieve_state/retrieve_state.go b/scripts/retrieve_state/retrieve_state.go index 457640ee1d..236d410b35 100644 --- a/scripts/retrieve_state/retrieve_state.go +++ b/scripts/retrieve_state/retrieve_state.go @@ -126,8 +126,6 @@ func (s *StateRequestProvider) buildTrie(expectedStorageRootHash common.Hash, de return err } -// go run ./scripts/retrieve_state/retrieve_state.go 0x00ab8c6e55a9951698052d9d999eb0c440c525af05867df39cb55863632b4ad9 0x804403dad405e595bdca5f068ae91a75d455c90f3e7672a2ee62d3b506a93c21 ./chain/paseo/chain-spec-raw.json ./lib/runtime/test_data/paseo/block1789153.out - func main() { if len(os.Args) != 5 { log.Fatalf(`