Skip to content

Commit

Permalink
continue debug root mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj committed Sep 20, 2024
1 parent 18abff0 commit 098d25e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 32 deletions.
10 changes: 6 additions & 4 deletions lib/runtime/storage/storagediff.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ func (cs *storageDiff) upsert(key string, value []byte) {
delete(cs.deletes, key)
}

if string(common.MustHexToBytes("0x26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96")) == key {
fmt.Printf("upserting our key with value: %v\n", common.BytesToHex(value))
}

cs.upserts[key] = value
cs.insertSortedKey(key)
}
Expand Down Expand Up @@ -259,8 +255,13 @@ func (cs *storageDiff) applyToTrie(t trie.Trie) {
panic("trying to apply nil change set")
}

fmt.Println("TIME TO APPLY: we need to make sure all updates here reflect what was put in")

// Apply trie upserts
for k, v := range cs.upserts {
if bytes.Equal(common.MustHexToBytes("0x0000000007a9b3851a0966360500"), v) {
fmt.Println("val in upsert")
}
err := t.Put([]byte(k), v)
if err != nil {
panic("Error applying upserts changes to trie")
Expand All @@ -269,6 +270,7 @@ func (cs *storageDiff) applyToTrie(t trie.Trie) {

// Apply child trie upserts
for childKeyString, childChangeSet := range cs.childChangeSet {
fmt.Println("there are some kids")
childKey := []byte(childKeyString)

for k, v := range childChangeSet.upserts {
Expand Down
5 changes: 2 additions & 3 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"encoding/binary"
"errors"
"fmt"
"sort"
"sync"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/trie"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"sort"
"sync"
)

// TrieState relies on `storageDiff` to perform changes over the current state.
Expand Down
32 changes: 16 additions & 16 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,21 @@ 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)
fmt.Println(line)
//switch int(level) {
//case 0:
// logger.Critical(line)
//case 1:
// logger.Warn(line)
//case 2:
// logger.Info(line)
//case 3:
// logger.Debug(line)
//case 4:
// logger.Trace(line)
//default:
// logger.Errorf("level=%d target=%s message=%s", int(level), target, msg)
//}
//fmt.Println(line)
switch int(level) {
case 0:
logger.Critical(line)
case 1:
logger.Warn(line)
case 2:
logger.Info(line)
case 3:
logger.Debug(line)
case 4:
logger.Trace(line)
default:
logger.Errorf("level=%d target=%s message=%s", int(level), target, msg)
}
}

func ext_crypto_ecdsa_generate_version_1(ctx context.Context, m api.Module, _ uint32, _ uint64) uint32 {
Expand Down Expand Up @@ -832,6 +832,7 @@ func ext_trie_blake2_256_root_version_2(ctx context.Context, m api.Module, dataS
}

func ext_trie_blake2_256_ordered_root_version_1(ctx context.Context, m api.Module, dataSpan uint64) uint32 {
// Why is this hardcoded to 0??
return ext_trie_blake2_256_ordered_root_version_2(ctx, m, dataSpan, 0)
}

Expand All @@ -858,7 +859,6 @@ 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 {
Expand Down
21 changes: 18 additions & 3 deletions lib/runtime/wazero/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,16 +1040,18 @@ func TestInstance_ExecuteBlock_PaseoRuntime_PaseoBlock1008649(t *testing.T) {
}

func TestInstance_ExecuteBlock_PaseoRuntime_PaseoBlock1789153(t *testing.T) {
fmt.Println("building 52 trie")
paseoTrie := newTrieFromKeyValueListV1(t, "../test_data/paseo/block1789152.out")
expectedRoot := common.MustHexToHash("0xf74a4a94758ac505a0bacdd52d7739d62b616eb03840d24d4a2df42df295c31d")
require.Equal(t, expectedRoot, trie.V1.MustHash(paseoTrie))

fmt.Println("building 53 trie")
paseoTrieActual := newTrieFromKeyValueListV1(t, "../test_data/paseo/block1789153.out")
expectedRootActual := common.MustHexToHash("0xc29a9d4465400c980cca388963461755040f2ba4c5ed722afc204014426e9080")
require.Equal(t, expectedRootActual, trie.V1.MustHash(paseoTrieActual))

fmt.Println("Time to execute block")
state := storage.NewTrieState(paseoTrie)

db, err := database.NewPebble("", true)
require.NoError(t, err)

Expand Down Expand Up @@ -1081,9 +1083,8 @@ func TestInstance_ExecuteBlock_PaseoRuntime_PaseoBlock1789153(t *testing.T) {
badEntries := state.Trie().Entries()
goodEntries := paseoTrieActual.Entries()

require.Equal(t, len(goodEntries), len(badEntries))
// require.Equal(t, len(goodEntries), len(badEntries))

fmt.Println("go through good")
for key, val := range goodEntries {
badVal, ok := badEntries[key]
if !ok {
Expand All @@ -1097,6 +1098,20 @@ func TestInstance_ExecuteBlock_PaseoRuntime_PaseoBlock1789153(t *testing.T) {
}
}

got := state.Trie().Get(common.MustHexToBytes("0x26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96"))
fmt.Printf("got: %x\n", got)

for key, val := range badEntries {
if bytes.Equal(common.MustHexToBytes("0x0000000007a9b3851a0966360500"), val) {
fmt.Println("Found value in bad entries with key")
fmt.Println(key)
}

if bytes.Equal(common.MustHexToBytes("0x26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96"), []byte(key)) {
fmt.Println("Found our key")
}
}

// 0x0000000007a9b3851a0966360500

//require.Equal(t, paseoTrieActual.Entries(), state.Trie().Entries())
Expand Down
4 changes: 4 additions & 0 deletions pkg/trie/inmemory/child_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package inmemory

import (
"bytes"
"errors"
"fmt"

Expand Down Expand Up @@ -69,6 +70,9 @@ 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) {
Expand Down
41 changes: 36 additions & 5 deletions pkg/trie/inmemory/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ 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.
Expand Down Expand Up @@ -272,9 +271,9 @@ func (t *InMemoryTrie) Put(keyLE, value []byte) (err error) {
t.HandleTrackedDeltas(success, pendingDeltas)
}()

if bytes.Equal(common.MustHexToBytes("0x26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96"), keyLE) {
fmt.Println("here")
}
//if bytes.Equal(common.MustHexToBytes("0x26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96"), keyLE) {
// fmt.Println("applying our upsert")
//}

err = t.InsertKeyLE(keyLE, value, pendingDeltas)
if err != nil {
Expand All @@ -293,6 +292,12 @@ func (t *InMemoryTrie) InsertKeyLE(keyLE, value []byte,
value = []byte{}
}

//if bytes.Equal(common.MustHexToBytes("0x26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96"), keyLE) {
// fmt.Println("they call me mr nibbles")
// fmt.Println(common.BytesToHex(nibblesKey))
// fmt.Println(common.BytesToHex(value))
//}

root, _, _, err := t.insert(t.root, nibblesKey, value, pendingDeltas)
if err != nil {
return err
Expand All @@ -306,6 +311,15 @@ func (t *InMemoryTrie) InsertKeyLE(keyLE, value []byte,
func (t *InMemoryTrie) insert(parent *node.Node, key, value []byte,
pendingDeltas tracking.DeltaRecorder) (newParent *node.Node,
mutated bool, nodesCreated uint32, err error) {

if bytes.Equal(common.MustHexToBytes("0x0000000007a9b3851a0966360500"), value) {
fmt.Println("INSERTING OUR VALUE")
fmt.Println(common.MustBlake2bHash(value))
}

if bytes.Equal(common.MustHexToBytes("0x02060a0a0309040e0e0a050603000e00070c04080a0e000c090505080c0e0f0703040a0b0f050c0b03040d060204040307080c0d0d0b0f01080e0804090d0906"), key) {
fmt.Println("INSERTING OUR KEY")
}
if parent == nil {
mutated = true
nodesCreated = 1
Expand Down Expand Up @@ -342,6 +356,9 @@ func (t *InMemoryTrie) insert(parent *node.Node, key, value []byte,
func (t *InMemoryTrie) insertInLeaf(parentLeaf *node.Node, key, value []byte,
pendingDeltas tracking.DeltaRecorder) (
newParent *node.Node, mutated bool, nodesCreated uint32, err error) {
if bytes.Equal(common.MustHexToBytes("0x0000000007a9b3851a0966360500"), value) {
fmt.Println("value found 5")
}

if bytes.Equal(parentLeaf.PartialKey, key) {
nodesCreated = 0
Expand Down Expand Up @@ -467,12 +484,22 @@ func (t *InMemoryTrie) insertInBranch(parentBranch *node.Node, key, value []byte
}

if bytes.HasPrefix(key, parentBranch.PartialKey) {
if bytes.Equal(common.MustHexToBytes("0x02060a0a0309040e0e0a050603000e00070c04080a0e000c090505080c0e0f0703040a0b0f050c0b03040d060204040307080c0d0d0b0f01080e0804090d0906"), key) {
fmt.Println("insert in branch: has prefix")
fmt.Println(parentBranch.PartialKey)
}
// key is included in parent branch key
commonPrefixLength := lenCommonPrefix(key, parentBranch.PartialKey)
childIndex := key[commonPrefixLength]
remainingKey := key[commonPrefixLength+1:]
child := parentBranch.Children[childIndex]

if bytes.Equal(common.MustHexToBytes("0x02060a0a0309040e0e0a050603000e00070c04080a0e000c090505080c0e0f0703040a0b0f050c0b03040d060204040307080c0d0d0b0f01080e0804090d0906"), key) {
fmt.Printf("prefLen: %v\n", commonPrefixLength)
fmt.Printf("chiInd: %v\n", childIndex)
fmt.Printf("remKey: %v\n", common.BytesToHex(remainingKey))
}

if child == nil {
child = &node.Node{
PartialKey: remainingKey,
Expand Down Expand Up @@ -1160,6 +1187,10 @@ func (t *InMemoryTrie) deleteAtNode(parent *node.Node, key []byte,
return nil, false, nodesRemoved, nil
}

if bytes.Equal(common.MustHexToBytes("0x02060a0a0309040e0e0a050603000e00070c04080a0e000c090505080c0e0f0703040a0b0f050c0b03040d060204040307080c0d0d0b0f01080e0804090d0906"), key) {
fmt.Println("Deleting our key")
}

if parent.Kind() == node.Leaf {
newParent, err = t.deleteLeaf(parent, key, pendingDeltas)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/trie/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ 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 {
Expand Down

0 comments on commit 098d25e

Please sign in to comment.