Skip to content

Commit

Permalink
merge conflict resolved and pandora engine implemented. waiting for r…
Browse files Browse the repository at this point in the history
…eview
  • Loading branch information
meta-bot committed Jul 30, 2021
2 parents 7042fc2 + 7e792fd commit 5ed628c
Show file tree
Hide file tree
Showing 26 changed files with 353 additions and 2,840 deletions.
1 change: 0 additions & 1 deletion consensus/ethash/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (
cacheInitBytes = 1 << 24 // Bytes in cache at genesis
cacheGrowthBytes = 1 << 17 // Cache growth per epoch
epochLength = 30000 // Blocks per epoch
pandoraEpochLength = 32 // Blocks per pandora epoch
mixBytes = 128 // Width of mix
hashBytes = 64 // Hash length in bytes
hashWords = 16 // Number of 32 bit ints in a hash
Expand Down
88 changes: 1 addition & 87 deletions consensus/ethash/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)

var (
errEthashStopped = errors.New("ethash stopped")
errInvalidParentHash = errors.New("invalid parent hash")
errInvalidBlockNumber = errors.New("invalid block number")
)
var errEthashStopped = errors.New("ethash stopped")

// API exposes ethash related methods for the RPC interface.
type API struct {
Expand Down Expand Up @@ -65,51 +60,6 @@ func (api *API) GetWork() ([4]string, error) {
}
}

// GetShardingWork returns a work package for external miner.
func (api *API) GetShardingWork(parentHash common.Hash, blockNumber uint64) ([4]string, error) {
emptyRes := [4]string{}
if api.ethash.remote == nil {
return [4]string{}, errors.New("not supported")
}

var (
workCh = make(chan [4]string, 1)
errc = make(chan error, 1)
)
select {
case api.ethash.remote.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
case <-api.ethash.remote.exitCh:
return emptyRes, errEthashStopped
}
select {
case work := <-workCh:
curBlockHeader := api.ethash.remote.currentBlock.Header()
if curBlockHeader != nil {
log.Debug("Current Block Header Data", "time", curBlockHeader.Time, "block number", curBlockHeader.Number)
// When producing block #1, validator does not know about hash of block #0
// so do not check the parent hash and block number 1
if blockNumber == 1 {
return work, nil
}
if curBlockHeader.ParentHash != parentHash {
log.Error("Mis-match in parentHash",
"blockNumber", curBlockHeader.Number.Uint64(),
"remoteParentHash", curBlockHeader.ParentHash, "receivedParentHash", parentHash)
return emptyRes, errInvalidParentHash
}

if curBlockHeader.Number.Uint64() != blockNumber {
log.Error("Mis-match in block number",
"remoteBlockNumber", curBlockHeader.Number.Uint64(), "receivedBlockNumber", blockNumber)
return emptyRes, errInvalidBlockNumber
}
}
return work, nil
case err := <-errc:
return emptyRes, err
}
}

// SubmitWork can be used by external miner to submit their POW solution.
// It returns an indication if the work was accepted.
// Note either an invalid solution, a stale work a non-existent work will return false.
Expand All @@ -118,48 +68,12 @@ func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) boo
return false
}

var blsSignature *BlsSignatureBytes

var errc = make(chan error, 1)
select {
case api.ethash.remote.submitWorkCh <- &mineResult{
nonce: nonce,
mixDigest: digest,
hash: hash,
blsSeal: blsSignature,
errc: errc,
}:
case <-api.ethash.remote.exitCh:
return false
}
err := <-errc
if err != nil {
log.Error("SubmitWork: found error while submitting work", "error", err)
}
return err == nil
}

// SubmitWorkBLS can be used by external miner to submit their POS solution.
// It returns an indication if the work was accepted.
// Note either an invalid solution, a stale work a non-existent work will return false.
// This submit work contains BLS storing feature.
func (api *API) SubmitWorkBLS(nonce types.BlockNonce, hash common.Hash, hexSignatureString string) bool {
if api.ethash.remote == nil {
return false
}

signatureBytes := hexutil.MustDecode(hexSignatureString)
blsSignatureBytes := new(BlsSignatureBytes)
copy(blsSignatureBytes[:], signatureBytes[:])

var errc = make(chan error, 1)

select {
case api.ethash.remote.submitWorkCh <- &mineResult{
nonce: nonce,
mixDigest: common.BytesToHash(blsSignatureBytes[:32]),
hash: hash,
blsSeal: blsSignatureBytes,
errc: errc,
}:
case <-api.ethash.remote.exitCh:
Expand Down
35 changes: 2 additions & 33 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ var (
// Specification EIP-2384: https://eips.ethereum.org/EIPS/eip-2384
calcDifficultyEip2384 = makeDifficultyCalculator(big.NewInt(9000000))

// Difficulty is not used anymore in pandora vanguard symbiotic relation flow
calcDifficultyPandora = func() *big.Int { return big.NewInt(1) }

// calcDifficultyConstantinople is the difficulty adjustment algorithm for Constantinople.
// It returns the difficulty that a new block should have when created at time given the
// parent block's time and difficulty. The calculation uses the Byzantium rules, but with
Expand Down Expand Up @@ -247,7 +244,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo
// See YP section 4.3.4. "Block Header Validity"
func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header, uncle bool, seal bool, unixNow int64) error {
// Ensure that the header's extra-data section is of a reasonable size
if uint64(len(header.Extra)) > params.MaximumExtraDataSize && ModePandora != ethash.config.PowMode {
if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
}
// Verify the header's timestamp
Expand Down Expand Up @@ -318,8 +315,6 @@ func (ethash *Ethash) CalcDifficulty(chain consensus.ChainHeaderReader, time uin
func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
next := new(big.Int).Add(parent.Number, big1)
switch {
case config.IsSilesia(next):
return calcDifficultyPandora()
case config.IsMuirGlacier(next):
return calcDifficultyEip2384(time, parent)
case config.IsConstantinople(next):
Expand Down Expand Up @@ -520,11 +515,6 @@ func (ethash *Ethash) verifySeal(chain consensus.ChainHeaderReader, header *type
digest []byte
result []byte
)
if ModePandora == ethash.config.PowMode {
// Here we should check if header hash was sealed by desired validator for a slot
return ethash.verifyPandoraHeader(header)
}

// If fast-but-heavy PoW verification was requested, use an ethash dataset
if fulldag {
dataset := ethash.dataset(number, true)
Expand Down Expand Up @@ -553,7 +543,6 @@ func (ethash *Ethash) verifySeal(chain consensus.ChainHeaderReader, header *type
// until after the call to hashimotoLight so it's not unmapped while being used.
runtime.KeepAlive(cache)
}

// Verify the calculated values against the ones provided in the header
if !bytes.Equal(header.MixDigest[:], digest) {
return errInvalidMixDigest
Expand All @@ -573,11 +562,6 @@ func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.H
return consensus.ErrUnknownAncestor
}
header.Difficulty = ethash.CalcDifficulty(chain, header.Time, parent)

if ModePandora == ethash.config.PowMode {
return ethash.PreparePandoraHeader(header)
}

return nil
}

Expand All @@ -603,21 +587,6 @@ func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewLegacyKeccak256()

extraData := header.Extra
extraDataLen := len(extraData)

// Bls signature is 96 bytes long and will be inserted at the bottom of the extraData field
if ModePandora == ethash.config.PowMode && extraDataLen > signatureSize {
//extraData = extraData[:extraDataLen-signatureSize]
pandoraExtraData := new(PandoraExtraDataSealed)
pandoraExtraData.FromHeader(header)
headerExtra := new(PandoraExtraData)
headerExtra.Epoch = pandoraExtraData.Epoch
headerExtra.Turn = pandoraExtraData.Turn
headerExtra.Slot = pandoraExtraData.Slot
extraData, _ = rlp.EncodeToBytes(headerExtra)
}

rlp.Encode(hasher, []interface{}{
header.ParentHash,
header.UncleHash,
Expand All @@ -631,7 +600,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
header.GasLimit,
header.GasUsed,
header.Time,
extraData,
header.Extra,
})
hasher.Sum(hash[:0])
return hash
Expand Down
4 changes: 1 addition & 3 deletions consensus/ethash/ethash.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (c *cache) finalizer() {

// dataset wraps an ethash dataset with some metadata to allow easier concurrent use.
type dataset struct {
epoch uint64 // epoch for which this cache is relevant
epoch uint64 // Epoch for which this cache is relevant
dump *os.File // File descriptor of the memory mapped cache
mmap mmap.MMap // Memory map itself to unmap before releasing
dataset []uint32 // The actual cache data content
Expand Down Expand Up @@ -397,7 +397,6 @@ const (
ModeTest
ModeFake
ModeFullFake
ModePandora
)

// Config are the configuration parameters of the ethash.
Expand All @@ -422,7 +421,6 @@ type Ethash struct {

caches *lru // In memory caches to avoid regenerating too often
datasets *lru // In memory datasets to avoid regenerating too often
mci *lru // In memory minimal consensus info. 0 index should contain all info needed for derivation

// Mining related fields
rand *rand.Rand // Properly seeded random source for nonces
Expand Down
Loading

0 comments on commit 5ed628c

Please sign in to comment.