Replace coded unit test PRNG for Clique test address generation (#1446)

why:
  Clique relies on the even/odd position of an address after sorting. For
  address generation, the Nim PRNG was used which seems to have changed
  with Nim 1.6.11 (Linux, Windoes only.)

  As a replace, the Posix.1-2001 example (two-liner calculation) generator
  is used.
This commit is contained in:
Jordan Hrycaj 2023-01-24 14:52:02 +00:00 committed by GitHub
parent e093fa452d
commit 1b441a485b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 28 deletions

View File

@ -9,7 +9,7 @@
# according to those terms.
import
std/[algorithm, os, sequtils, strformat, strutils, times, tables],
std/[algorithm, os, sequtils, strformat, strutils, times],
chronicles,
eth/keys,
stint,

View File

@ -9,7 +9,7 @@
# according to those terms.
import
std/[algorithm, random, sequtils, strformat, strutils, tables, times],
std/[algorithm, sequtils, strformat, strutils, tables, times],
eth/[keys, rlp],
ethash,
secp256k1/abi,
@ -31,6 +31,7 @@ export
const
prngSeed = 42
## The `TestSpecs` sample depends on this seed,
type
XSealKey = array[EXTRA_SEAL,byte]
@ -42,7 +43,7 @@ type
## mapped from textual names used in the tests below
## to actual Ethereum private keys capable of signing
## transactions.
prng: Rand
prng: uint32 ## random state
accounts: Table[string,PrivateKey] ## accounts table
networkId: NetworkId
boot: NetworkParams ## imported Genesis configuration
@ -53,6 +54,25 @@ type
xSeals: Table[XSealKey,XSealValue] ## collect signatures for debugging
noisy*: bool
# ------------------------------------------------------------------------------
# Private Prng (Clique keeps generated addresses sorted)
# ------------------------------------------------------------------------------
proc posixPrngInit(state: var uint32; seed: uint32) =
state = seed
proc posixPrngRand(state: var uint32): byte =
## POSIX.1-2001 example of a rand() implementation, see manual page rand(3).
##
## Clique relies on the even/odd position of an address after sorting. For
## address generation, the Nim PRNG was used which seems to have changed
## with Nim 1.6.11 (Linux, Windoes only.)
##
## The `TestSpecs` sample depends on `prngSeed` and `posixPrngRand()`.
state = state * 1103515245 + 12345;
let val = (state shr 16) and 32767 # mod 2^31
(val shr 8).byte # Extract second byte
# ------------------------------------------------------------------------------
# Private Helpers
# ------------------------------------------------------------------------------
@ -72,7 +92,7 @@ proc isZero(a: openArray[byte]): bool =
return false
proc rand(ap: TesterPool): byte =
ap.prng.rand(255).byte
ap.prng.posixPrngRand().byte
proc newPrivateKey(ap: TesterPool): PrivateKey =
## Roughly modelled after `random(PrivateKey,getRng()[])` with
@ -257,7 +277,7 @@ proc resetChainDb(ap: TesterPool; extraData: Blob; debug = false) =
proc initTesterPool(ap: TesterPool): TesterPool {.discardable.} =
result = ap
result.prng = initRand(prngSeed)
result.prng.posixPrngInit(prngSeed)
result.batch = @[newSeq[BlockHeader]()]
result.accounts = initTable[string,PrivateKey]()
result.xSeals = initTable[XSealKey,XSealValue]()

View File

@ -101,7 +101,7 @@ const
votes: @[TesterVote(signer: "A", voted: "C", auth: true),
TesterVote(signer: "B", voted: "C", auth: true),
TesterVote(signer: "A", voted: "D", auth: true, noTurn: true),
TesterVote(signer: "B", voted: "D", auth: true, noTurn: true),
TesterVote(signer: "B", voted: "D", auth: true),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A", voted: "E", auth: true, noTurn: true),
TesterVote(signer: "B", voted: "E", auth: true, noTurn: true)],
@ -201,10 +201,10 @@ const
TesterVote(signer: "B", noTurn: true),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A", voted: "D", noTurn: true),
TesterVote(signer: "B", noTurn: true),
TesterVote(signer: "C"),
TesterVote(signer: "B"),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A"),
TesterVote(signer: "B", voted: "D"),
TesterVote(signer: "B", voted: "D", noTurn: true),
TesterVote(signer: "C", voted: "D", noTurn: true),
TesterVote(signer: "A", noTurn: true),
TesterVote(signer: "B", voted: "C", noTurn: true)],
@ -215,9 +215,9 @@ const
info: "Votes from deauthorized signers are discarded immediately " &
"(deauth votes)",
signers: @["A", "B", "C"],
votes: @[TesterVote(signer: "C", voted: "B"),
votes: @[TesterVote(signer: "C", voted: "B", noTurn: true),
TesterVote(signer: "A", voted: "C"),
TesterVote(signer: "B", voted: "C"),
TesterVote(signer: "B", voted: "C", noTurn: true),
TesterVote(signer: "A", voted: "B", noTurn: true)],
results: @["A", "B"]),
@ -226,9 +226,9 @@ const
info: "Votes from deauthorized signers are discarded immediately " &
"(auth votes)",
signers: @["A", "B", "C"],
votes: @[TesterVote(signer: "C", voted: "D", auth: true),
votes: @[TesterVote(signer: "C", voted: "D", auth: true, noTurn: true),
TesterVote(signer: "A", voted: "C"),
TesterVote(signer: "B", voted: "C"),
TesterVote(signer: "B", voted: "C", noTurn: true),
TesterVote(signer: "A", voted: "D", auth: true, noTurn: true)],
results: @["A", "B"]),
@ -241,10 +241,10 @@ const
TesterVote(signer: "B", noTurn: true),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A", voted: "D", noTurn: true),
TesterVote(signer: "B", voted: "C", noTurn: true),
TesterVote(signer: "C"),
TesterVote(signer: "B", voted: "C"),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A"),
TesterVote(signer: "B", voted: "D"),
TesterVote(signer: "B", voted: "D", noTurn: true),
TesterVote(signer: "C", voted: "D", noTurn: true)],
results: @["A", "B", "C"]),
@ -257,10 +257,10 @@ const
TesterVote(signer: "B", noTurn: true),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A", voted: "D", noTurn: true),
TesterVote(signer: "B", voted: "C", noTurn: true),
TesterVote(signer: "C"),
TesterVote(signer: "B", voted: "C"),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A"),
TesterVote(signer: "B", voted: "D"),
TesterVote(signer: "B", voted: "D", noTurn: true),
TesterVote(signer: "C", voted: "D", noTurn: true),
TesterVote(signer: "A", noTurn: true),
TesterVote(signer: "C", voted: "C", auth: true, noTurn: true)],
@ -275,10 +275,10 @@ const
TesterVote(signer: "B", noTurn: true),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A", voted: "D", noTurn: true),
TesterVote(signer: "B", voted: "C", noTurn: true),
TesterVote(signer: "C"),
TesterVote(signer: "B", voted: "C"),
TesterVote(signer: "C", noTurn: true),
TesterVote(signer: "A"),
TesterVote(signer: "B", voted: "D"),
TesterVote(signer: "B", voted: "D", noTurn: true),
TesterVote(signer: "C", voted: "D", noTurn: true),
TesterVote(signer: "A", noTurn: true),
TesterVote(signer: "B", voted: "C", auth: true, noTurn: true)],
@ -296,22 +296,22 @@ const
votes: @[
# Authorize F, 3 votes needed
TesterVote(signer: "A", voted: "F", auth: true, noTurn: true),
TesterVote(signer: "B", voted: "F", auth: true, noTurn: true),
TesterVote(signer: "B", voted: "F", auth: true),
TesterVote(signer: "C", voted: "F", auth: true, noTurn: true),
# Deauthorize F, 4 votes needed (leave A's previous vote "unchanged")
TesterVote(signer: "D", voted: "F", noTurn: true),
TesterVote(signer: "E", voted: "F", noTurn: true),
TesterVote(signer: "B", voted: "F"),
TesterVote(signer: "C", voted: "F", noTurn: true),
TesterVote(signer: "B", voted: "F", noTurn: true),
TesterVote(signer: "C", voted: "F"),
# Almost authorize F, 2/3 votes needed
TesterVote(signer: "D", voted: "F", auth: true, noTurn: true),
TesterVote(signer: "D", voted: "F", auth: true),
TesterVote(signer: "E", voted: "F", auth: true, noTurn: true),
# Deauthorize A, 3 votes needed
TesterVote(signer: "B", voted: "A"),
TesterVote(signer: "C", voted: "A", noTurn: true),
TesterVote(signer: "B", voted: "A", noTurn: true),
TesterVote(signer: "C", voted: "A"),
TesterVote(signer: "D", voted: "A", noTurn: true),
# Finish authorizing F, 3/3 votes needed