Skip to content

Commit

Permalink
Merge pull request ethereum#299 from nextyio/prioritized
Browse files Browse the repository at this point in the history
Prioritize block with random seed by double the normal block weight
  • Loading branch information
Zergity authored Oct 1, 2019
2 parents ff53681 + 5c9cc8b commit a194f95
Show file tree
Hide file tree
Showing 5 changed files with 12,337 additions and 42 deletions.
21 changes: 14 additions & 7 deletions consensus/dccs/2_dccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
errInvalidExtendedData = errors.New("Extended data does not matches")
errInvalidRandomData = errors.New("Invalid random data in extra data")
errInvalidRandomDataSize = errors.New("Invalid random data size from relayer")
errInvalidPrioritiezedDiff = errors.New("Invalid prioritized block difficulty")

errInvalidPriceData = errors.New("price block contains invalid price value")
errUnexpectPriceData = errors.New("non-price block contains price value")
Expand Down Expand Up @@ -162,8 +163,8 @@ func (c *Context) verifyCascadingFields2() error {
}

// Ensure that the block's timestamp isn't too close to it's parent
parent := c.getHeaderByHash(header.ParentHash)
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
parent := c.getHeader(header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
if parent.Time+c.engine.config.Period > header.Time {
Expand Down Expand Up @@ -269,8 +270,11 @@ func (c *Context) verifySeal2() error {
return errRecentlySigned
}

// block with random seed or the CoLoa hardfork block
prioritized := header.Nonce == types.BlockNonce{}

// Ensure that the difficulty corresponds to the turn-ness of the signer
signerDifficulty := queue.difficulty(signer, c.getHeaderByHash, c.engine.signatures)
signerDifficulty := queue.difficulty(signer, prioritized, c.getHeaderByHash, c.engine.signatures)
if header.Difficulty.Uint64() != signerDifficulty {
return errInvalidDifficulty
}
Expand Down Expand Up @@ -465,10 +469,6 @@ func (c *Context) prepare2(header *types.Header) error {
return err
}

// Set the correct difficulty
difficulty := queue.difficulty(c.engine.signer, c.chain.GetHeaderByHash, c.engine.signatures)
header.Difficulty = new(big.Int).SetUint64(difficulty)

// Ensure the timestamp has the correct delay
header.Time = parent.Time + c.engine.config.Period
if header.Time < uint64(time.Now().Unix()) {
Expand Down Expand Up @@ -503,6 +503,13 @@ func (c *Context) prepare2(header *types.Header) error {
header.Nonce = c.getBlockNonce(parent)
}

// block with random seed or the CoLoa hardfork block
prioritized := header.Nonce == types.BlockNonce{}

// Set the correct difficulty
difficulty := queue.difficulty(c.engine.signer, prioritized, c.chain.GetHeaderByHash, c.engine.signatures)
header.Difficulty = new(big.Int).SetUint64(difficulty)

var price *Price
if c.engine.config.IsPriceBlock(number) {
price = c.engine.PriceEngine().CurrentPrice()
Expand Down
23 changes: 18 additions & 5 deletions consensus/dccs/2_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (q *SealingQueue) offset(signer common.Address,
return offset, nil
}

func (q *SealingQueue) difficulty(address common.Address,
func (q *SealingQueue) difficulty(address common.Address, prioritized bool,
getHeaderByHash func(common.Hash) *types.Header,
sigCache *lru.ARCCache) uint64 {

Expand All @@ -201,9 +201,13 @@ func (q *SealingQueue) difficulty(address common.Address,
return 0
}

n := len(q.active)
diff := uint64(len(q.active) - offset)

return uint64(n - offset)
if prioritized {
diff <<= 1
}

return diff
}

// recents len is MIN(lastActiveLen,activeLen)/2
Expand Down Expand Up @@ -289,8 +293,17 @@ func (c *Context) getSealingQueue(parentHash common.Hash) (*SealingQueue, error)
addActive(sealer)

// use the difficulty for total number of recently active sealer count
if header.Difficulty.Uint64() > maxDiff {
maxDiff = header.Difficulty.Uint64()
diff := header.Difficulty.Uint64()
if header.Nonce == (types.BlockNonce{}) {
if diff&0x01 != 0 {
// prioritized difficulty cannot be odd number
return nil, errInvalidPrioritiezedDiff
}
// correct difficulty for prioritized block
diff >>= 1
}
if diff > maxDiff {
maxDiff = diff
}
// somewhat probabilistically optimized, fairly safe nonetheless
if i < minBlockToScan || len(recents) < int(maxDiff)/2 {
Expand Down
2 changes: 2 additions & 0 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ type blockStats struct {
Miner common.Address `json:"miner"`
GasUsed uint64 `json:"gasUsed"`
GasLimit uint64 `json:"gasLimit"`
Nonce uint64 `json:"nonce"`
Diff string `json:"difficulty"`
TotalDiff string `json:"totalDifficulty"`
Txs []txStats `json:"transactions"`
Expand Down Expand Up @@ -593,6 +594,7 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
Miner: author,
GasUsed: header.GasUsed,
GasLimit: header.GasLimit,
Nonce: header.Nonce.Uint64(),
Diff: header.Difficulty.String(),
TotalDiff: td.String(),
Txs: txs,
Expand Down
Loading

0 comments on commit a194f95

Please sign in to comment.