block witness: add test for unwanted multiple identical keys when constructing witness from trie

This commit is contained in:
jangko 2020-06-02 11:24:41 +07:00 committed by zah
parent fc271b3e8d
commit 7d062e171b
3 changed files with 15 additions and 5 deletions

View File

@ -148,6 +148,6 @@ func isValidMatch(mg: MatchGroup): bool {.inline.} =
result = mg.match and mg.group.first == mg.group.last
proc visitMatch*(m: var MultikeysRef, mg: MatchGroup, depth: int): KeyData =
doAssert(mg.isValidMatch)
doAssert(mg.isValidMatch, "Multiple identical keys are not allowed")
m.keys[mg.group.first].visited = true
result = m.keys[mg.group.first]

View File

@ -65,7 +65,9 @@ proc randAccount(db: DB): AccountDef =
proc randAddress(): EthAddress =
discard randomBytes(result.addr, sizeof(result))
proc runTest(numPairs: int, testStatusIMPL: var TestStatus, addInvalidKeys: static[bool] = false) =
proc runTest(numPairs: int, testStatusIMPL: var TestStatus,
addIdenticalKeys: bool = false, addInvalidKeys: static[bool] = false) =
var memDB = newMemoryDB()
var trie = initSecureHexaryTrie(memDB)
var addrs = newSeq[AccountKey](numPairs)
@ -82,6 +84,10 @@ proc runTest(numPairs: int, testStatusIMPL: var TestStatus, addInvalidKeys: stat
let invalidAddress = randAddress()
addrs.add((invalidAddress, false, MultikeysRef(nil)))
if addIdenticalKeys:
let invalidAddress = addrs[0].address
addrs.add((invalidAddress, false, MultikeysRef(nil)))
var mkeys = newMultiKeys(addrs)
let rootHash = trie.rootHash
@ -146,7 +152,11 @@ proc witnessKeysMain*() =
check rlpBytes.len > 32
test "invalid address ignored":
runTest(rand(1..30), testStatusIMPL, addInvalidKeys = true)
runTest(rand(1..30), testStatusIMPL, false, addInvalidKeys = true)
test "multiple identical addresses rejected":
expect AssertionError:
runTest(rand(5..30), testStatusIMPL, true)
test "case 1: all keys is a match":
let keys = [

View File

@ -129,7 +129,7 @@ proc readUVarint32(t: var TreeBuilder): uint32 =
break
inc(shift, 7)
if shift > 28:
raise newException(ParsingError, "Failed when try to uvarint32")
raise newException(ParsingError, "Failed when try to parse uvarint32")
proc readUVarint256(t: var TreeBuilder): UInt256 =
# LEB128 varint encoding
@ -141,7 +141,7 @@ proc readUVarint256(t: var TreeBuilder): UInt256 =
break
inc(shift, 7)
if shift > 252:
raise newException(ParsingError, "Failed when try to uvarint256")
raise newException(ParsingError, "Failed when try to parse uvarint256")
proc toKeccak(r: var NodeKey, x: openArray[byte]) {.inline.} =
r.data[0..31] = x[0..31]