Skip to content

Commit

Permalink
Added logic to derive mixHash in consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Jul 6, 2023
1 parent fe42b23 commit 047449f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 54 deletions.
11 changes: 11 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type Engine interface {
// DeltaLogS returns the log of the entropy delta for a chain since its prior coincidence
DeltaLogS(header *types.Header) *big.Int

ComputePowLight(header *types.Header) (mixHash, powHash common.Hash)

// VerifyHeader checks whether a header conforms to the consensus rules of a
// given engine. Verifying the seal may be done optionally here, or explicitly
// via the VerifySeal method.
Expand Down Expand Up @@ -137,6 +139,15 @@ type Engine interface {
Close() error
}

func TargetToDifficulty(target *big.Int) *big.Int {
big2e256 := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256
return new(big.Int).Div(big2e256, target)
}

func DifficultyToTarget(difficulty *big.Int) *big.Int {
return TargetToDifficulty(difficulty)
}

// PoW is a consensus engine based on proof-of-work.
type PoW interface {
Engine
Expand Down
55 changes: 27 additions & 28 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,31 @@ func (progpow *Progpow) IsDomCoincident(chain consensus.ChainHeaderReader, heade
return order < common.NodeLocation.Context()
}

func (progpow *Progpow) ComputePowLight(header *types.Header) (mixHash, powHash common.Hash) {
powLight := func(size uint64, cache []uint32, hash []byte, nonce uint64, blockNumber uint64) ([]byte, []byte) {
ethashCache := progpow.cache(blockNumber)
if ethashCache.cDag == nil {
cDag := make([]uint32, progpowCacheWords)
generateCDag(cDag, ethashCache.cache, blockNumber/epochLength)
ethashCache.cDag = cDag
}
return progpowLight(size, cache, hash, nonce, blockNumber, ethashCache.cDag)
}
cache := progpow.cache(header.NumberU64())
size := datasetSize(header.NumberU64())
digest, result := powLight(size, cache.cache, header.SealHash().Bytes(), header.NonceU64(), header.NumberU64(common.ZONE_CTX))
mixHash = common.BytesToHash(digest)
powHash = common.BytesToHash(result)
header.PowDigest.Store(mixHash)
header.PowHash.Store(powHash)

// Caches are unmapped in a finalizer. Ensure that the cache stays alive
// until after the call to hashimotoLight so it's not unmapped while being used.
runtime.KeepAlive(cache)

return mixHash, powHash
}

// verifySeal checks whether a block satisfies the PoW difficulty requirements,
// either using the usual progpow cache for it, or alternatively using a full DAG
// to make remote mining fast.
Expand All @@ -404,28 +429,10 @@ func (progpow *Progpow) verifySeal(header *types.Header) (common.Hash, error) {
return common.Hash{}, errInvalidDifficulty
}
// Check progpow
powHash := header.PowHash.Load()
mixHash := header.PowDigest.Load()
powHash := header.PowHash.Load()
if powHash == nil || mixHash == nil {
powLight := func(size uint64, cache []uint32, hash []byte, nonce uint64, blockNumber uint64) ([]byte, []byte) {
ethashCache := progpow.cache(blockNumber)
if ethashCache.cDag == nil {
cDag := make([]uint32, progpowCacheWords)
generateCDag(cDag, ethashCache.cache, blockNumber/epochLength)
ethashCache.cDag = cDag
}
return progpowLight(size, cache, hash, nonce, blockNumber, ethashCache.cDag)
}
cache := progpow.cache(header.NumberU64())
size := datasetSize(header.NumberU64())
digest, result := powLight(size, cache.cache, header.SealHash().Bytes(), header.NonceU64(), header.NumberU64(common.ZONE_CTX))
mixHash = common.BytesToHash(digest)
powHash = common.BytesToHash(result)
header.PowDigest.Store(mixHash)
header.PowHash.Store(powHash)
// Caches are unmapped in a finalizer. Ensure that the cache stays alive
// until after the call to hashimotoLight so it's not unmapped while being used.
runtime.KeepAlive(cache)
mixHash, powHash = progpow.ComputePowLight(header)
}
// Verify the calculated values against the ones provided in the header
if !bytes.Equal(header.MixHash().Bytes(), mixHash.(common.Hash).Bytes()) {
Expand Down Expand Up @@ -519,11 +526,3 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
}
state.AddBalance(coinbase, reward)
}

func TargetToDifficulty(target *big.Int) *big.Int {
return new(big.Int).Div(big2e256, target)
}

func DifficultyToTarget(difficulty *big.Int) *big.Int {
return TargetToDifficulty(difficulty)
}
2 changes: 1 addition & 1 deletion consensus/progpow/sealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ search:
header = types.CopyHeader(header)
header.SetNonce(types.EncodeNonce(nonce))
hashBytes := common.BytesToHash(digest)
header.SetMixHash(&hashBytes)
header.SetMixHash(hashBytes)
found <- header
break search
}
Expand Down
4 changes: 2 additions & 2 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ func (h *Header) SetExtra(val []byte) {
copy(h.extra, val)
}
}
func (h *Header) SetMixHash(val *common.Hash) {
func (h *Header) SetMixHash(val common.Hash) {
h.hash = atomic.Value{} // clear hash cache
h.mixHash = *val
h.mixHash = val
}
func (h *Header) SetNonce(val BlockNonce) {
h.hash = atomic.Value{} // clear hash cache, but NOT sealHash
Expand Down
46 changes: 23 additions & 23 deletions core/types/gen_header_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 047449f

Please sign in to comment.