This commit is contained in:
andri lim 2020-05-06 10:36:00 +07:00
parent 9c999ffa9c
commit e5c4c7d16f
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 16 additions and 13 deletions

View File

@ -65,7 +65,7 @@ proc randAccount(db: DB): AccountDef =
proc randAddress(): EthAddress =
discard randomBytes(result.addr, sizeof(result))
proc runTest(numPairs: int, testStatusIMPL: var TestStatus, useRogueKeys: static[bool] = false) =
proc runTest(numPairs: int, testStatusIMPL: var TestStatus, addInvalidKeys: static[bool] = false) =
var memDB = newMemoryDB()
var trie = initSecureHexaryTrie(memDB)
var addrs = newSeq[AccountKey](numPairs)
@ -77,10 +77,10 @@ proc runTest(numPairs: int, testStatusIMPL: var TestStatus, useRogueKeys: static
accs[i] = acc.account
trie.put(addrs[i].address, rlp.encode(accs[i]))
when useRogueKeys:
# rogueAddress should not end up in block witness
let rogueAddress = randAddress()
addrs.add((rogueAddress, false, MultikeysRef(nil)))
when addInvalidKeys:
# invalidAddress should not end up in block witness
let invalidAddress = randAddress()
addrs.add((invalidAddress, false, MultikeysRef(nil)))
var mkeys = newMultiKeys(addrs)
let rootHash = trie.rootHash
@ -106,9 +106,9 @@ proc runTest(numPairs: int, testStatusIMPL: var TestStatus, useRogueKeys: static
debugEcho "BUG IN TREE BUILDER ", i
check false
when useRogueKeys:
when addInvalidKeys:
for kd in mkeys.keys:
if kd.address == rogueAddress:
if kd.address == invalidAddress:
check kd.visited == false
else:
check kd.visited == true
@ -116,7 +116,7 @@ proc runTest(numPairs: int, testStatusIMPL: var TestStatus, useRogueKeys: static
for kd in mkeys.keys:
check kd.visited == true
proc witnesKeysMain() =
proc witnessKeysMain() =
suite "random keys block witness roundtrip test":
randomize()
@ -129,8 +129,8 @@ proc witnesKeysMain() =
let rlpBytes = rlp.encode(acc)
check rlpBytes.len > 32
test "rogue address ignored":
runTest(rand(1..30), testStatusIMPL, useRogueKeys = true)
test "invalid address ignored":
runTest(rand(1..30), testStatusIMPL, addInvalidKeys = true)
when isMainModule:
witnessKeysMain()

View File

@ -365,6 +365,9 @@ proc accountNode(t: var TreeBuilder, depth: int): NodeKey =
doAssert(codeHash.usedBytes == 32)
acc.codeHash.data = codeHash.data
# readCodeLen already save the codeLen
# along with recovered address
# we could discard it here
discard t.readCodeLen()
let storageRoot = t.treeNode(0, storageMode = true)
@ -424,7 +427,7 @@ proc accountStorageLeafNode(t: var TreeBuilder, depth: int): NodeKey =
result = t.toNodeKey(r.finish)
when defined(debugHash):
doAssert(result == nodeKey, "account storage no parsing error")
doAssert(result == nodeKey, "account storage leaf node parsing error")
proc hashNode(t: var TreeBuilder): NodeKey =
safeReadBytes(t, 32):

View File

@ -227,9 +227,9 @@ proc getBranchRecurse(wb: var WitnessBuilder, z: var StackElem) =
else:
# this should be only one match
# if there is more than one match
# it means we encounter a rogue address
# it means we encounter an invalid address
for kd in keyDatas(z.keys, mg.group):
if not match(kd, k, z.depth): continue # this is the rogue address
if not match(kd, k, z.depth): continue # skip the invalid address
kd.visited = true
if z.storageMode:
doAssert(kd.storageMode)