remove clique unused code

This commit is contained in:
jangko 2022-12-09 11:25:47 +07:00
parent b16b35caea
commit 93725bdc02
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 15 additions and 145 deletions

View File

@ -27,17 +27,6 @@ import
eth/[keys, rlp],
stew/[keyed_queue, results]
const
enableCliqueAsyncLock* = ##\
## Async locks are currently unused by `Clique` but were part of the Go
## reference implementation. The unused code fragment from the reference
## implementation are buried in the file `clique_unused.nim` and not used
## otherwise.
defined(clique_async_lock)
when enableCliqueAsyncLock:
import chronos
type
RawSignature* = array[RawSignatureSize, byte]
@ -70,9 +59,6 @@ type
## Ethereum address of the current signing key
signFn*: CliqueSignerFn ## Signer function to authorize hashes with
stopSealReq*: bool ## Stop running `seal()` function
stopVHeaderReq*: bool ## Stop running `verifyHeader()` function
# signatures => see CliqueCfg
cfg: CliqueCfg ##\
## Common engine parameters to fine tune behaviour
@ -102,10 +88,6 @@ type
## before have been vetted already regardless of the current branch. So
## the nearest `epoch` header is used.
when enableCliqueAsyncLock:
asyncLock: AsyncLock ##\
## Protects the signer fields
{.push raises: [Defect].}
logScope:
@ -122,8 +104,6 @@ proc newClique*(cfg: CliqueCfg): Clique =
result = Clique(cfg: cfg,
snapshot: cfg.newSnapshot(BlockHeader()),
proposals: initTable[EthAddress,bool]())
when enableCliqueAsyncLock:
result.asyncLock = newAsyncLock()
# ------------------------------------------------------------------------------
# Public debug/pretty print
@ -197,29 +177,6 @@ proc `applySnapsMinBacklog=`*(c: Clique; value: bool) =
## Setter
c.applySnapsMinBacklog = value
# ------------------------------------------------------------------------------
# Public lock/unlock
# ------------------------------------------------------------------------------
when enableCliqueAsyncLock:
proc lock*(c: Clique) {.gcsafe, raises: [Defect,CatchableError].} =
## Lock descriptor
waitFor c.asyncLock.acquire
proc unLock*(c: Clique) {.gcsafe, raises: [Defect,AsyncLockError].} =
## Unlock descriptor
c.asyncLock.release
template doExclusively*(c: Clique; action: untyped) =
## Handy helper
c.lock
action
c.unlock
else:
template doExclusively*(c: Clique; action: untyped) =
action
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------

View File

@ -38,36 +38,10 @@ import
logScope:
topics = "clique PoA Mining"
type
CliqueSyncDefect* = object of Defect
## Defect raised with lock/unlock problem
# ------------------------------------------------------------------------------
# Private Helpers
# ------------------------------------------------------------------------------
template syncExceptionWrap(action: untyped) =
try:
action
except:
raise (ref CliqueSyncDefect)(msg: getCurrentException().msg)
# clique/clique.go(217): func (c *Clique) VerifyHeader(chain [..]
proc verifyHeader(c: Clique; com: CommonRef; header: BlockHeader): CliqueOkResult
{.gcsafe, raises: [Defect,CatchableError].} =
## See `clique.cliqueVerify()`
var blind: seq[BlockHeader]
c.cliqueVerifySeq(com, header, blind)
proc verifyHeader(c: Clique; com: CommonRef; header: BlockHeader;
parents: openArray[BlockHeader]): CliqueOkResult
{.gcsafe, raises: [Defect,CatchableError].} =
## See `clique.cliqueVerify()`
var list = toSeq(parents)
c.cliqueVerifySeq(com, header, list)
proc isValidVote(s: Snapshot; a: EthAddress; authorize: bool): bool =
s.ballot.isValidVote(a, authorize)
@ -106,63 +80,6 @@ proc recentBlockNumber*(s: Snapshot;
# Public functions
# ------------------------------------------------------------------------------
# clique/clique.go(212): func (c *Clique) Author(header [..]
proc author*(c: Clique; header: BlockHeader): Result[EthAddress,UtilsError]
{.gcsafe, raises: [Defect,CatchableError].} =
## For the Consensus Engine, `author()` retrieves the Ethereum address of the
## account that minted the given block, which may be different from the
## header's coinbase if a consensus engine is based on signatures.
##
## This implementation returns the Ethereum address recovered from the
## signature in the header's extra-data section.
c.cfg.ecRecover(header)
# clique/clique.go(224): func (c *Clique) VerifyHeader(chain [..]
proc verifyHeaders*(c: Clique; com: CommonRef; headers: openArray[BlockHeader]):
Future[seq[CliqueOkResult]] {.async,gcsafe.} =
## For the Consensus Engine, `verifyHeader()` s similar to VerifyHeader, but
## verifies a batch of headers concurrently. This method is accompanied
## by a `stopVerifyHeader()` method that can abort the operations.
##
## This implementation checks whether a header conforms to the consensus
## rules. It verifies a batch of headers. If running in the background,
## the process can be stopped by calling the `stopVerifyHeader()` function.
syncExceptionWrap:
c.doExclusively:
c.stopVHeaderReq = false
for n in 0 ..< headers.len:
c.doExclusively:
let isStopRequest = c.stopVHeaderReq
if isStopRequest:
result.add cliqueResultErr((errCliqueStopped,""))
break
result.add c.verifyHeader(com, headers[n], headers[0 ..< n])
c.doExclusively:
c.stopVHeaderReq = false
proc stopVerifyHeader*(c: Clique): bool {.discardable.} =
## Activate the stop flag for running `verifyHeader()` function.
## Returns `true` if the stop flag could be activated.
syncExceptionWrap:
c.doExclusively:
if not c.stopVHeaderReq:
c.stopVHeaderReq = true
result = true
# clique/clique.go(450): func (c *Clique) VerifyUncles(chain [..]
proc verifyUncles*(c: Clique; ethBlock: EthBlock): CliqueOkResult =
## For the Consensus Engine, `verifyUncles()` verifies that the given
## block's uncles conform to the consensus rules of a given engine.
##
## This implementation always returns an error for existing uncles as this
## consensus mechanism doesn't permit uncles.
if 0 < ethBlock.uncles.len:
return err((errCliqueUnclesNotAllowed,""))
result = ok()
# clique/clique.go(506): func (c *Clique) Prepare(chain [..]
proc prepare*(c: Clique; parent: BlockHeader, header: var BlockHeader): CliqueOkResult
{.gcsafe, raises: [Defect, CatchableError].} =
@ -186,18 +103,17 @@ proc prepare*(c: Clique; parent: BlockHeader, header: var BlockHeader): CliqueOk
let modEpoch = (parent.blockNumber+1) mod c.cfg.epoch
if modEpoch != 0:
c.doExclusively:
# Gather all the proposals that make sense voting on
var addresses: seq[EthAddress]
for (address,authorize) in c.proposals.pairs:
if c.snapshot.isValidVote(address, authorize):
addresses.add address
# Gather all the proposals that make sense voting on
var addresses: seq[EthAddress]
for (address,authorize) in c.proposals.pairs:
if c.snapshot.isValidVote(address, authorize):
addresses.add address
# If there's pending proposals, cast a vote on them
if 0 < addresses.len:
header.coinbase = addresses[c.cfg.rand(addresses.len-1)]
header.nonce = if header.coinbase in c.proposals: NONCE_AUTH
else: NONCE_DROP
# If there's pending proposals, cast a vote on them
if 0 < addresses.len:
header.coinbase = addresses[c.cfg.rand(addresses.len-1)]
header.nonce = if header.coinbase in c.proposals: NONCE_AUTH
else: NONCE_DROP
# Set the correct difficulty
header.difficulty = c.snapshot.calcDifficulty(c.signer)
@ -227,10 +143,8 @@ proc prepareForSeal*(c: Clique; prepHeader: BlockHeader; header: var BlockHeader
# clique/clique.go(589): func (c *Clique) Authorize(signer [..]
proc authorize*(c: Clique; signer: EthAddress; signFn: CliqueSignerFn) =
## Injects private key into the consensus engine to mint new blocks with.
syncExceptionWrap:
c.doExclusively:
c.signer = signer
c.signFn = signFn
c.signer = signer
c.signFn = signFn
# clique/clique.go(724): func CliqueRLP(header [..]
proc cliqueRlp*(header: BlockHeader): seq[byte] =
@ -273,10 +187,9 @@ proc seal*(c: Clique; ethBlock: var EthBlock):
return err((nilCliqueSealNoBlockYet, ""))
# Don't hold the signer fields for the entire sealing procedure
c.doExclusively:
let
signer = c.signer
signFn = c.signFn
let
signer = c.signer
signFn = c.signFn
# Bail out if we're unauthorized to sign a block
let rc = c.cliqueSnapshot(header.parentHash)