fixes clique signerFn return type

and also add test related to this signerFn
This commit is contained in:
jangko 2021-08-19 19:00:30 +07:00
parent 18b26a0089
commit c99153df22
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 58 additions and 6 deletions

View File

@ -41,11 +41,13 @@ when enableCliqueAsyncLock:
import chronos
type
RawSignature* = array[RawSignatureSize, byte]
# clique/clique.go(142): type SignerFn func(signer [..]
CliqueSignerFn* = ## Hashes and signs the data to be signed by
## a backing account
proc(signer: EthAddress;
message: openArray[byte]): Result[Hash256,cstring] {.gcsafe.}
message: openArray[byte]): Result[RawSignature, cstring] {.gcsafe.}
Proposals = Table[EthAddress,bool]

View File

@ -9,12 +9,20 @@
# according to those terms.
import
std/[algorithm, os, sequtils, strformat, strutils, times],
std/[algorithm, os, sequtils, strformat,
strutils, times, parseopt, tables],
../nimbus/db/db_chain,
../nimbus/p2p/[chain, clique, clique/clique_snapshot],
../nimbus/p2p/[chain,
clique,
clique/clique_snapshot,
clique/clique_desc,
clique/clique_helpers
],
../nimbus/utils/ec_recover,
../nimbus/[config, utils, constants],
./test_clique/[pool, undump],
eth/[common, keys],
stint,
stint, stew/byteutils,
unittest2
const
@ -229,6 +237,45 @@ proc runGoerliBaybySteps(noisy = true;
test &"Runner stopped after reaching #{stopThreshold}":
discard
proc cliqueMiscTests() =
suite "clique misc":
test "signer func":
const
engineSigner = "658bdf435d810c91414ec09147daa6db62406379"
privateKey = "tests" / "test_clique" / "private.key"
var msg: string
var opt = initOptParser("--engine-signer:$1 --import-key:$2" % [engineSigner, privateKey])
let res = processArguments(msg, opt)
check res == Success
let signer = hexToByteArray[20](engineSigner)
let conf = getConfiguration()
check signer in conf.accounts
proc signFunc(signer: EthAddress, message: openArray[byte]): Result[RawSignature, cstring] {.gcsafe.} =
let
hashData = keccakHash(message)
conf = getConfiguration()
acc = conf.accounts[signer]
rawSign = sign(acc.privateKey, SkMessage(hashData.data)).toRaw
ok(rawSign)
let signerFn: CliqueSignerFn = signFunc
var header: BlockHeader
header.extraData.setLen(EXTRA_VANITY)
header.extraData.add 0.byte.repeat(EXTRA_SEAL)
let signature = signerFn(signer, header.encodeSealHeader).get()
let extraLen = header.extraData.len
if EXTRA_SEAL < extraLen:
header.extraData.setLen(extraLen - EXTRA_SEAL)
header.extraData.add signature
let resAddr = ecRecover(header)
check resAddr.isOk
check resAddr.value == signer
# ------------------------------------------------------------------------------
# Main function(s)
# ------------------------------------------------------------------------------
@ -238,6 +285,7 @@ proc cliqueMain*(noisy = defined(debug)) =
noisy.runCliqueSnapshot(false)
noisy.runGoerliBaybySteps
noisy.runGoerliReplay(startAtBlock = 31100u64)
cliqueMiscTests()
when isMainModule:
let
@ -257,13 +305,14 @@ when isMainModule:
dir = dir, captureFile = captureFile,
startAtBlock = startAtBlock, stopAfterBlock = stopAfterBlock)
let noisy = defined(debug)
#[let noisy = defined(debug)
noisy.runCliqueSnapshot(true)
noisy.runCliqueSnapshot(false)
noisy.runGoerliBaybySteps(dir = ".")
noisy.runGoerliReplay(dir = ".", startAtBlock = 31100u64)
noisy.runGoerliReplay(dir = ".", startAtBlock = 31100u64)]#
#noisy.goerliReplay(startAtBlock = 31100u64)
#noisy.goerliReplay(startAtBlock = 194881u64, stopAfterBlock = 198912u64)
cliqueMiscTests()
# ------------------------------------------------------------------------------
# End

View File

@ -0,0 +1 @@
9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c