From c99153df22c475e71262c5b4c6bf4ef022726082 Mon Sep 17 00:00:00 2001 From: jangko Date: Thu, 19 Aug 2021 19:00:30 +0700 Subject: [PATCH] fixes clique signerFn return type and also add test related to this signerFn --- nimbus/p2p/clique/clique_desc.nim | 4 ++- tests/test_clique.nim | 59 ++++++++++++++++++++++++++++--- tests/test_clique/private.key | 1 + 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 tests/test_clique/private.key diff --git a/nimbus/p2p/clique/clique_desc.nim b/nimbus/p2p/clique/clique_desc.nim index 7bd2730ea..b1fc615ef 100644 --- a/nimbus/p2p/clique/clique_desc.nim +++ b/nimbus/p2p/clique/clique_desc.nim @@ -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] diff --git a/tests/test_clique.nim b/tests/test_clique.nim index fead5a7e7..8642e2896 100644 --- a/tests/test_clique.nim +++ b/tests/test_clique.nim @@ -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 diff --git a/tests/test_clique/private.key b/tests/test_clique/private.key new file mode 100644 index 000000000..7f43fa8aa --- /dev/null +++ b/tests/test_clique/private.key @@ -0,0 +1 @@ +9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c \ No newline at end of file