Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 484cb7a

Browse files
committed
getDriftedPosition fix; test added
1 parent 81e715a commit 484cb7a

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

mpt-witness-generator/witness/branch.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,27 @@ func prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []b
154154
func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {
155155
var nibbles []byte
156156
if leafKeyRow[0] != 248 {
157-
keyLen := int(leafKeyRow[1] - 128)
158-
if (leafKeyRow[2] != 32) && (leafKeyRow[2] != 0) { // second term is for extension node
159-
if leafKeyRow[2] < 32 { // extension node
160-
nibbles = append(nibbles, leafKeyRow[2]-16)
161-
} else { // leaf
162-
nibbles = append(nibbles, leafKeyRow[2]-48)
157+
var keyLen int
158+
if leafKeyRow[1] > 128 {
159+
keyLen = int(leafKeyRow[1] - 128)
160+
if (leafKeyRow[2] != 32) && (leafKeyRow[2] != 0) { // second term is for extension node
161+
if leafKeyRow[2] < 32 { // extension node
162+
nibbles = append(nibbles, leafKeyRow[2]-16)
163+
} else { // leaf
164+
nibbles = append(nibbles, leafKeyRow[2]-48)
165+
}
163166
}
164-
}
165-
for i := 0; i < keyLen-1; i++ { // -1 because the first byte doesn't have any nibbles
166-
b := leafKeyRow[3+i]
167-
n1 := b / 16
168-
n2 := b - n1*16
169-
nibbles = append(nibbles, n1)
170-
nibbles = append(nibbles, n2)
171-
}
167+
for i := 0; i < keyLen-1; i++ { // -1 because the first byte doesn't have any nibbles
168+
b := leafKeyRow[3+i]
169+
n1 := b / 16
170+
n2 := b - n1*16
171+
nibbles = append(nibbles, n1)
172+
nibbles = append(nibbles, n2)
173+
}
174+
} else {
175+
keyLen = 1
176+
nibbles = append(nibbles, leafKeyRow[1]-16)
177+
}
172178
} else {
173179
keyLen := int(leafKeyRow[2] - 128)
174180
if (leafKeyRow[3] != 32) && (leafKeyRow[3] != 0) { // second term is for extension node

mpt-witness-generator/witness/gen_witness_from_local_blockchain_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,59 @@ func TestExtNodeDeletedExtShortIsBranchFirstLevel(t *testing.T) {
920920

921921
ExtNodeDeleted(key1, key2, key3, "ExtNodeDeletedExtShortIsBranchFirstLevel")
922922
}
923+
924+
func TestExtensionIntoBranch(t *testing.T) {
925+
SkipIfNoGeth(t)
926+
oracle.NodeUrl = oracle.LocalUrl
927+
928+
blockNum := 0
929+
blockNumberParent := big.NewInt(int64(blockNum))
930+
blockHeaderParent := oracle.PrefetchBlock(blockNumberParent, true, nil)
931+
database := state.NewDatabase(blockHeaderParent)
932+
statedb, _ := state.New(blockHeaderParent.Root, database, nil)
933+
addr := common.HexToAddress("0x50efbf12580138bc623c95757286df4e24eb81c9")
934+
935+
statedb.DisableLoadingRemoteAccounts()
936+
937+
statedb.CreateAccount(addr)
938+
939+
oracle.PreventHashingInSecureTrie = true // to store the unchanged key
940+
941+
val0 := common.BigToHash(big.NewInt(int64(1)))
942+
key0 := common.HexToHash("0x1200000000000000000000000000000000000000000000000000000000000000")
943+
statedb.SetState(addr, key0, val0)
944+
945+
key00 := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
946+
statedb.SetState(addr, key00, val0)
947+
// After insertion of key0 and key00, we have a branch B with two leaves - at position 1 and 0.
948+
949+
key1 := common.HexToHash("0x1234561000000000000000000000000000000000000000000000000000000000")
950+
951+
// make the value long to have a hashed branch
952+
v1 := common.FromHex("0xbbefaa12580138bc263c95757826df4e24eb81c9aaaaaaaaaaaaaaaaaaaaaaaa")
953+
val1 := common.BytesToHash(v1)
954+
statedb.SetState(addr, key1, val1)
955+
// After insertion of key1, we have a branch B with a leaf at position 0
956+
// and an extension node E1 at position 1 (with one nibble: 2)
957+
// The underlying branch B1 of E1 has two leaves - at position 0 and 3.
958+
959+
key2 := common.HexToHash("0x1434563000000000000000000000000000000000000000000000000000000000")
960+
// After inserting key2, we have a branch B with two nodes - a leaf at position 0 and branch B1
961+
// at position 1. At position 1 we have a branch B2 at position 2 (used to be E1's nibble)
962+
// and a leaf at position 4 (newly added leaf).
963+
// Branch B2 has two leaves - at position 0 and 3.
964+
965+
v1 = common.FromHex("0xbb")
966+
val := common.BytesToHash(v1)
967+
trieMod := TrieModification{
968+
Type: StorageChanged,
969+
Key: key2,
970+
Value: val,
971+
Address: addr,
972+
}
973+
trieModifications := []TrieModification{trieMod}
974+
975+
prepareWitness("ExtensionIntoBranch", trieModifications, statedb)
976+
977+
oracle.PreventHashingInSecureTrie = false
978+
}

0 commit comments

Comments
 (0)