aboutsummaryrefslogtreecommitdiffstats
path: root/consensus/clique/clique.go
diff options
context:
space:
mode:
authorgary rong <garyrong0905@gmail.com>2017-12-22 20:37:50 +0800
committerPéter Szilágyi <peterke@gmail.com>2017-12-22 20:37:50 +0800
commit5f8888e11606296c9582496974c0f6b96a882146 (patch)
treeef8139ff20ae33eb5a236290d5107dd808e34340 /consensus/clique/clique.go
parent9dbb8ef4aadb8e40aef8b681cf86acd20789abdc (diff)
downloaddexon-5f8888e11606296c9582496974c0f6b96a882146.tar.gz
dexon-5f8888e11606296c9582496974c0f6b96a882146.tar.zst
dexon-5f8888e11606296c9582496974c0f6b96a882146.zip
accounts, consensus, core, eth: make chain maker consensus agnostic (#15497)
* accounts, consensus, core, eth: make chain maker consensus agnostic * consensus, core: move CalcDifficulty to Engine interface * consensus: add docs for calcDifficulty function * consensus, core: minor comment fixups
Diffstat (limited to 'consensus/clique/clique.go')
-rw-r--r--consensus/clique/clique.go28
1 files changed, 23 insertions, 5 deletions
diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go
index a98058141..2bdad9092 100644
--- a/consensus/clique/clique.go
+++ b/consensus/clique/clique.go
@@ -510,7 +510,6 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
header.Nonce = types.BlockNonce{}
number := header.Number.Uint64()
-
// Assemble the voting snapshot to check which votes make sense
snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
if err != nil {
@@ -538,10 +537,8 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
c.lock.RUnlock()
}
// Set the correct difficulty
- header.Difficulty = diffNoTurn
- if snap.inturn(header.Number.Uint64(), c.signer) {
- header.Difficulty = diffInTurn
- }
+ header.Difficulty = CalcDifficulty(snap, c.signer)
+
// Ensure the extra data has all it's components
if len(header.Extra) < extraVanity {
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...)
@@ -655,6 +652,27 @@ func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, stop <-ch
return block.WithSeal(header), nil
}
+// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
+// that a new block should have based on the previous blocks in the chain and the
+// current signer.
+func (c *Clique) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
+ snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil)
+ if err != nil {
+ return nil
+ }
+ return CalcDifficulty(snap, c.signer)
+}
+
+// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
+// that a new block should have based on the previous blocks in the chain and the
+// current signer.
+func CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int {
+ if snap.inturn(snap.Number+1, signer) {
+ return new(big.Int).Set(diffInTurn)
+ }
+ return new(big.Int).Set(diffNoTurn)
+}
+
// APIs implements consensus.Engine, returning the user facing RPC API to allow
// controlling the signer voting.
func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API {