package posa import ( "bytes" "errors" "fmt" "io" "math/big" "math/rand" "sort" "strings" "sync" "time" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" lru "github.com/ethereum/go-ethereum/common/lru" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/consensus/misc/eip1559" "github.com/ethereum/go-ethereum/consensus/misc/eip4844" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/trie" "github.com/holiman/uint256" "golang.org/x/crypto/sha3" ) const ( checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory inmemorySignatures = 4096 // Number of recent block signatures to keep in memory wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers noturnMinimumDelay = 1000 * time.Millisecond // Min value of delay for no turn signers. It makes time for syncing signed block. ) // PoSA protocol constants. var ( epochLength = uint64(30000) // Default number of blocks after which to checkpoint and reset the signer list extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity extraSeal = crypto.SignatureLength // Fixed number of extra-data suffix bytes reserved for signer seal nonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number for normal block nonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number for checkpoint block uncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures Phase1BlockReward = new(uint256.Int).Mul(uint256.NewInt(1e+17), uint256.NewInt(1125)) // Block reward in wei for successfully mining a block RewardTreeForkBlockReward = new(uint256.Int).Mul(uint256.NewInt(1e+17), uint256.NewInt(300)) ) // Various error messages to mark blocks invalid. These should be private to // prevent engine specific errors from being referenced in the remainder of the // codebase, inherently breaking if the engine is swapped out. Please put common // error types into the consensus package. var ( // errUnknownBlock is returned when the list of signers is requested for a block // that is not part of the local blockchain. errUnknownBlock = errors.New("unknown block") // errInvalidNormalNonce is returned if a normal block has a vote nonce not set // to 0xff..f. errInvalidNormalNonce = errors.New("vote nonce in normal block not 0xff..f") // errInvalidCheckpointNonce is returned if a checkpoint/epoch transition block // has a vote nonce set to non-zeroes. errInvalidCheckpointNonce = errors.New("vote nonce in checkpoint block non-zero") // errMismatchingCoinbase is returned if a block's coinbase address is not the // reward contract. errMismatchingCoinbase = errors.New("coinbase address not reward contract") // errMissingVanity is returned if a block's extra-data section is shorter than // 32 bytes, which is required to store the signer vanity. errMissingVanity = errors.New("extra-data 32 byte vanity prefix missing") // errMissingSignature is returned if a block's extra-data section doesn't seem // to contain a 65 byte secp256k1 signature. errMissingSignature = errors.New("extra-data 65 byte signature suffix missing") // errExtraSigners is returned if non-checkpoint block contain signer data in // their extra-data fields. errExtraSigners = errors.New("non-checkpoint block contains extra signer list") // errInvalidCheckpointSigners is returned if a checkpoint block contains an // invalid list of signers (i.e. non divisible by 20 bytes). errInvalidCheckpointSigners = errors.New("invalid signer list on checkpoint block") // errMismatchingCheckpointSigners is returned if a checkpoint block contains a // list of signers different than the one the local node calculated. errMismatchingCheckpointSigners = errors.New("mismatching signer list on checkpoint block") // errInvalidMixDigest is returned if a block's mix digest is non-zero. errInvalidMixDigest = errors.New("non-zero mix digest") // errInvalidUncleHash is returned if a block contains an non-empty uncle list. errInvalidUncleHash = errors.New("non empty uncle hash") // errInvalidDifficulty is returned if the difficulty of a block neither 1 or 2. errInvalidDifficulty = errors.New("invalid difficulty") // errWrongDifficulty is returned if the difficulty of a block doesn't match the // turn of the signer. errWrongDifficulty = errors.New("wrong difficulty") // errInvalidTimestamp is returned if the timestamp of a block is lower than // the previous block's timestamp + the minimum block period. errInvalidTimestamp = errors.New("invalid timestamp") // errInvalidChain is returned if an authorization list is attempted to // be modified via out-of-range or non-contiguous headers. errInvalidChain = errors.New("invalid chain") // errUnauthorizedSigner is returned if a header is signed by a non-authorized entity. errUnauthorizedSigner = errors.New("unauthorized signer") // errRecentlySigned is returned if a header is signed by an authorized entity // that already signed a header recently, thus is temporarily not allowed to. errRecentlySigned = errors.New("recently signed") ) // SignerFn hashes and signs the data to be signed by a backing account. type SignerFn func(signer accounts.Account, mimeType string, message []byte) ([]byte, error) // ecrecover extracts the Ethereum account address from a signed header. func ecrecover(header *types.Header, sigcache *sigLRU) (common.Address, error) { // If the signature's already cached, return that hash := header.Hash() if address, known := sigcache.Get(hash); known { return address, nil } // Retrieve the signature from the header extra-data if len(header.Extra) < extraSeal { return common.Address{}, errMissingSignature } signature := header.Extra[len(header.Extra)-extraSeal:] // Recover the public key and the Ethereum address pubkey, err := crypto.Ecrecover(SealHash(header).Bytes(), signature) if err != nil { return common.Address{}, err } var signer common.Address copy(signer[:], crypto.Keccak256(pubkey[1:])[12:]) sigcache.Add(hash, signer) return signer, nil } // PoSA is the proof-of-authority consensus engine proposed to support the MemeCore. type PoSA struct { config *params.PoSAConfig // Consensus engine configuration parameters db ethdb.Database // Database to store and retrieve snapshot checkpoints recents *lru.Cache[common.Hash, *Snapshot] // Snapshots for recent block to speed up reorgs signatures *sigLRU // Signatures of recent blocks to speed up mining signer common.Address // Ethereum address of the signing key signFn SignerFn // Signer function to authorize hashes with lock sync.RWMutex // Protects the signer fields ethAPI *ethapi.BlockChainAPI // Blockchain API for contract calling vmConfig vm.Config // EVM config for EVM initialization validatorSetABI abi.ABI // System contract that maintains validator list rewardABI abi.ABI // System contract that distribute rewards // The fields below are for testing only fakeDiff bool // Skip difficulty verifications enableEventLogging bool // Flag to control event logging poSASignerRetryInterval time.Duration // Interval between retries to signer poSASignerRetryCount int // Number of retries to signer } // SetEventLoggingEnabled sets whether event logging is enabled or disabled. func (p *PoSA) SetEventLoggingEnabled(enabled bool) { p.lock.Lock() defer p.lock.Unlock() p.enableEventLogging = enabled } func (p *PoSA) SetSignerRetry(interval time.Duration, count int) { p.lock.Lock() defer p.lock.Unlock() p.poSASignerRetryInterval = interval p.poSASignerRetryCount = count } // New creates a PoSA consensus engine with the initial signers set to the // ones provided by the user. func New(config *params.PoSAConfig, db ethdb.Database) *PoSA { // Set any missing consensus parameters to their defaults conf := *config if conf.Epoch == 0 { conf.Epoch = epochLength } // Allocate the snapshot caches and create the engine recents := lru.NewCache[common.Hash, *Snapshot](inmemorySnapshots) signatures := lru.NewCache[common.Hash, common.Address](inmemorySignatures) // Load system contract ABI vABI, err := abi.JSON(strings.NewReader(validatorSetABI)) if err != nil { panic(err) } rABI, err := abi.JSON(strings.NewReader(rewardABI)) if err != nil { panic(err) } return &PoSA{ config: &conf, db: db, recents: recents, signatures: signatures, validatorSetABI: vABI, rewardABI: rABI, } } // WithEthAPI initializes Eth blockchain API for proper consensus module work. func (p *PoSA) WithEthAPI(api *ethapi.BlockChainAPI) { p.ethAPI = api } func (p *PoSA) WithVMConfig(config vm.Config) { p.vmConfig = config } // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. func (p *PoSA) Author(header *types.Header) (common.Address, error) { return ecrecover(header, p.signatures) } // VerifyHeader checks whether a header conforms to the consensus rules. func (p *PoSA) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header) error { return p.verifyHeader(chain, header, nil) } // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The // method returns a quit channel to abort the operations and a results channel to // retrieve the async verifications (the order is that of the input slice). func (p *PoSA) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error) { abort := make(chan struct{}) results := make(chan error, len(headers)) go func() { for i, header := range headers { err := p.verifyHeader(chain, header, headers[:i]) select { case <-abort: return case results <- err: } } }() return abort, results } // verifyHeader checks whether a header conforms to the consensus rules.The // caller may optionally pass in a batch of parents (ascending order) to avoid // looking those up from the database. This is useful for concurrently verifying // a batch of new headers. func (p *PoSA) verifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { if header.Number == nil { return errUnknownBlock } number := header.Number.Uint64() // Don't waste time checking blocks from the future if header.Time > uint64(time.Now().Unix()) { return consensus.ErrFutureBlock } // Checkpoint blocks need to enforce zero beneficiary checkpoint := (number % p.config.Epoch) == 0 // Nonces must be 0x00..0 or 0xff..f, zeroes enforced on checkpoints if !checkpoint && !bytes.Equal(header.Nonce[:], nonceAuthVote) { return errInvalidNormalNonce } if checkpoint && !bytes.Equal(header.Nonce[:], nonceDropVote) { return errInvalidCheckpointNonce } // Check that the extra-data contains both the vanity and signature if len(header.Extra) < extraVanity { return errMissingVanity } if len(header.Extra) < extraVanity+extraSeal { return errMissingSignature } // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise signersBytes := len(header.Extra) - extraVanity - extraSeal if !checkpoint && signersBytes != 0 { return errExtraSigners } if checkpoint && signersBytes%common.AddressLength != 0 { return errInvalidCheckpointSigners } // Ensure that the mix digest is zero as we don't have fork protection currently if header.MixDigest != (common.Hash{}) { return errInvalidMixDigest } // Ensure that the block doesn't contain any uncles which are meaningless in PoA if header.UncleHash != uncleHash { return errInvalidUncleHash } // Ensure that the block's difficulty is meaningful (may not be correct at this point) if number > 0 { if header.Difficulty == nil || (header.Difficulty.Cmp(diffInTurn) != 0 && header.Difficulty.Cmp(diffNoTurn) != 0) { return errInvalidDifficulty } } // Verify that the gas limit is <= 2^63-1 if header.GasLimit > params.MaxGasLimit { return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, params.MaxGasLimit) } // Verify existence / non-existence of withdrawalsHash. shanghai := chain.Config().IsShanghai(header.Number, header.Time) if shanghai && header.WithdrawalsHash == nil { return errors.New("missing withdrawalsHash") } if shanghai && header.WithdrawalsHash.Cmp(types.EmptyWithdrawalsHash) != 0 { return fmt.Errorf("invalid withdrawalsHash: have %x, expected %x", header.WithdrawalsHash, &types.EmptyWithdrawalsHash) } if !shanghai && header.WithdrawalsHash != nil { return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash) } // Verify existence / non-existence of parentBeaconRoot. cancun := chain.Config().IsCancun(header.Number, header.Time) if cancun && header.ParentBeaconRoot == nil { return errors.New("missing parentBeaconRoot") } if cancun && header.ParentBeaconRoot.Cmp(types.EmptyRootHash) != 0 { return fmt.Errorf("invalid parentBeaconRoot: have %x, expected %x", *header.ParentBeaconRoot, &types.EmptyRootHash) } if !cancun && header.ParentBeaconRoot != nil { return fmt.Errorf("invalid parentBeaconRoot, have %x, expected nil", header.ParentBeaconRoot) } // Verify existence / non-existence of requestsHash. prague := chain.Config().IsPrague(header.Number, header.Time) if prague && header.RequestsHash == nil { return errors.New("missing requestsHash") } if prague && header.RequestsHash.Cmp(types.EmptyRequestsHash) != 0 { return fmt.Errorf("invalid requestsHash: have %x, expected %x", *header.RequestsHash, &types.EmptyRequestsHash) } if !prague && header.RequestsHash != nil { return fmt.Errorf("invalid requestsHash, have %x, expected nil", header.RequestsHash) } // All basic checks passed, verify cascading fields return p.verifyCascadingFields(chain, header, parents) } // verifyCascadingFields verifies all the header fields that are not standalone, // rather depend on a batch of previous headers. The caller may optionally pass // in a batch of parents (ascending order) to avoid looking those up from the // database. This is useful for concurrently verifying a batch of new headers. func (p *PoSA) verifyCascadingFields(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { // The genesis block is the always valid dead-end number := header.Number.Uint64() if number == 0 { return nil } // Ensure that the block's timestamp isn't too close to its parent var parent *types.Header if len(parents) > 0 { parent = parents[len(parents)-1] } else { parent = chain.GetHeader(header.ParentHash, number-1) } if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { return consensus.ErrUnknownAncestor } if parent.Time+p.config.Period > header.Time { return errInvalidTimestamp } // Verify that the gasUsed is <= gasLimit if header.GasUsed > header.GasLimit { return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit) } if !chain.Config().IsLondon(header.Number) { // Verify BaseFee not present before EIP-1559 fork. if header.BaseFee != nil { return fmt.Errorf("invalid baseFee before fork: have %d, want ", header.BaseFee) } if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil { return err } } else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil { // Verify the header's EIP-1559 attributes. return err } if !chain.Config().IsCancun(header.Number, header.Time) { switch { case header.ExcessBlobGas != nil: return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas) case header.BlobGasUsed != nil: return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed) case header.ParentBeaconRoot != nil: return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", header.ParentBeaconRoot) } } else if err := eip4844.VerifyEIP4844Header(chain.Config(), parent, header); err != nil { // Verify cancun-specific header fields return err } // Check the coinbase address if header.Coinbase.Cmp(common.HexToAddress(rewardAddr)) != 0 { return errMismatchingCoinbase } // Retrieve the snapshot needed to verify this header and cache it snap, err := p.snapshot(chain, number-1, header.ParentHash, parents) if err != nil { return err } // All basic checks passed, verify the seal and return return p.verifySeal(snap, header, parents) } // verifyValidators gets and verifies the current validator list in a header func (p *PoSA) verifyValidators(header *types.Header) error { if header.Number.Uint64()%p.config.Epoch != 0 { return nil } validators, err := p.getCurrentValidators(header.ParentHash) if err != nil { return err } sort.Sort(validatorsAscending(validators)) signers := make([]byte, len(validators)*common.AddressLength) for i, validator := range validators { copy(signers[i*common.AddressLength:], validator[:]) } extraSuffix := len(header.Extra) - extraSeal if !bytes.Equal(header.Extra[extraVanity:extraSuffix], signers) { return errMismatchingCheckpointSigners } return nil } // snapshot retrieves the authorization snapshot at a given point in time. func (p *PoSA) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { // Search for a snapshot in memory or on disk for checkpoints var ( headers []*types.Header snap *Snapshot ) for snap == nil { // If an in-memory snapshot was found, use that if s, ok := p.recents.Get(hash); ok { snap = s break } // If an on-disk checkpoint snapshot can be found, use that if number%checkpointInterval == 0 { if s, err := loadSnapshot(p.config, p.signatures, p.db, hash); err == nil { log.Trace("Loaded voted snapshot from disk", "number", number, "hash", hash) snap = s break } } // If we're at the genesis, snapshot the initial state. Alternatively if we're // at a checkpoint block without a parent (light client CHT), or we have piled // up more headers than allowed to be reorged (chain reinit from a freezer), // consider the checkpoint trusted and snapshot it. if number == 0 || (number%p.config.Epoch == 0 && (len(headers) > params.FullImmutabilityThreshold || chain.GetHeaderByNumber(number-1) == nil)) { checkpoint := chain.GetHeaderByNumber(number) if checkpoint != nil { hash := checkpoint.Hash() signers := make([]common.Address, (len(checkpoint.Extra)-extraVanity-extraSeal)/common.AddressLength) for i := 0; i < len(signers); i++ { copy(signers[i][:], checkpoint.Extra[extraVanity+i*common.AddressLength:]) } snap = newSnapshot(p.config, p.signatures, number, hash, signers) if err := snap.store(p.db); err != nil { return nil, err } log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash) break } } // No snapshot for this header, gather the header and move backward var header *types.Header if len(parents) > 0 { // If we have explicit parents, pick from there (enforced) header = parents[len(parents)-1] if header.Hash() != hash || header.Number.Uint64() != number { return nil, consensus.ErrUnknownAncestor } parents = parents[:len(parents)-1] } else { // No explicit parents (or no more left), reach out to the database header = chain.GetHeader(hash, number) if header == nil { return nil, consensus.ErrUnknownAncestor } } headers = append(headers, header) number, hash = number-1, header.ParentHash } // Previous snapshot found, apply any pending headers on top of it for i := 0; i < len(headers)/2; i++ { headers[i], headers[len(headers)-1-i] = headers[len(headers)-1-i], headers[i] } snap, err := snap.apply(headers) if err != nil { return nil, err } p.recents.Add(snap.Hash, snap) // If we've generated a new checkpoint snapshot, save to disk if snap.Number%checkpointInterval == 0 && len(headers) > 0 { if err = snap.store(p.db); err != nil { return nil, err } log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash) } return snap, err } // VerifyUncles implements consensus.Engine, always returning an error for any // uncles as this consensus mechanism doesn't permit uncles. func (p *PoSA) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { if len(block.Uncles()) > 0 { return errors.New("uncles not allowed") } return nil } // verifySeal checks whether the signature contained in the header satisfies the // consensus protocol requirements. The method accepts an optional list of parent // headers that aren't yet part of the local blockchain to generate the snapshots // from. func (p *PoSA) verifySeal(snap *Snapshot, header *types.Header, parents []*types.Header) error { // Verifying the genesis block is not supported number := header.Number.Uint64() if number == 0 { return errUnknownBlock } // Resolve the authorization key and check against signers signer, err := ecrecover(header, p.signatures) if err != nil { return err } if _, ok := snap.Signers[signer]; !ok { return errUnauthorizedSigner } for seen, recent := range snap.Recents { if recent == signer { // Signer is among recents, only fail if the current block doesn't shift it out if limit := uint64(len(snap.Signers)/2 + 1); seen > number-limit { return errRecentlySigned } } } // Ensure that the difficulty corresponds to the turn-ness of the signer if !p.fakeDiff { inturn := snap.inturn(header.Number.Uint64(), signer) if inturn && header.Difficulty.Cmp(diffInTurn) != 0 { return errWrongDifficulty } if !inturn && header.Difficulty.Cmp(diffNoTurn) != 0 { return errWrongDifficulty } } return nil } // Prepare implements consensus.Engine, preparing all the consensus fields of the // header for running the transactions on top. func (p *PoSA) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { // If the block isn't a checkpoint, cast a random vote (good enough for now) header.Nonce = types.BlockNonce{} // Set the coinbase address to reward contract header.Coinbase = common.HexToAddress(rewardAddr) number := header.Number.Uint64() // Assemble the voting snapshot to check which votes make sense snap, err := p.snapshot(chain, number-1, header.ParentHash, nil) if err != nil { return err } p.lock.RLock() if number%p.config.Epoch != 0 { copy(header.Nonce[:], nonceAuthVote) } // Copy signer protected by mutex to avoid race condition signer := p.signer p.lock.RUnlock() // Set the correct difficulty header.Difficulty = calcDifficulty(snap, signer) // Ensure the extra data has all its components if len(header.Extra) < extraVanity { header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...) } header.Extra = header.Extra[:extraVanity] // Update signer list manually in checkpoint blocks if err := p.prepareValidators(header); err != nil { return err } header.Extra = append(header.Extra, make([]byte, extraSeal)...) // Mix digest is reserved for now, set to empty header.MixDigest = common.Hash{} // Ensure the timestamp has the correct delay parent := chain.GetHeader(header.ParentHash, number-1) if parent == nil { return consensus.ErrUnknownAncestor } header.Time = parent.Time + p.config.Period if header.Time < uint64(time.Now().Unix()) { header.Time = uint64(time.Now().Unix()) } return nil } // prepareValidators gets and appends current validator list to a block header func (p *PoSA) prepareValidators(header *types.Header) error { if header.Number.Uint64()%p.config.Epoch != 0 { return nil } validators, err := p.getCurrentValidators(header.ParentHash) if err != nil { return err } sort.Sort(validatorsAscending(validators)) for _, validator := range validators { header.Extra = append(header.Extra, validator.Bytes()...) } return nil } // Finalize implements consensus.Engine, accumulating the block rewards and // beacon withdraws. func (p *PoSA) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body) error { // If the block is a checkpoint block, verify the signer listst // The verification can only be done when the state is ready, it can't be done in VerifyHeader. err := p.verifyValidators(header) if err != nil { return err } // Accumulate any block rewards err = accumulateRewards(chain.Config(), state, header, body.Uncles) if err != nil { return err } snap, err := p.snapshot(chain, header.Number.Uint64()-1, header.ParentHash, nil) if err != nil { return err } return p.settleRewardsAndUpdateValidators(chain, header, state, snap.Signers) } // FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set, // but block rewards are given, and returns the final block. func (p *PoSA) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) { shanghai := chain.Config().IsShanghai(header.Number, header.Time) if shanghai { // All blocks after Shanghai must include a withdrawals root. if body.Withdrawals == nil { body.Withdrawals = make([]*types.Withdrawal, 0) } } else { if len(body.Withdrawals) > 0 { return nil, errors.New("withdrawals set before Shanghai activation") } } cancun := chain.Config().IsCancun(header.Number, header.Time) if cancun { // All headers after Cancun must include a beacon root. if header.ParentBeaconRoot == nil { header.ParentBeaconRoot = &types.EmptyRootHash } } else { if header.ParentBeaconRoot != nil { return nil, errors.New("parentBeaconRoot set before Cancun activation") } } prague := chain.Config().IsPrague(header.Number, header.Time) if prague { // All headers after Prague must include a requests hash. if header.RequestsHash == nil { header.RequestsHash = &types.EmptyRequestsHash } } else { if header.RequestsHash != nil { return nil, errors.New("requestsHash set before Prague activation") } } // Finalize block err := p.Finalize(chain, header, state, body) if err != nil { return nil, err } // Assign the final state root to header. header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) // Assemble and return the final block for sealing. return types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)), nil } // Authorize injects a private key into the consensus engine to mint new blocks // with. func (p *PoSA) Authorize(signer common.Address, signFn SignerFn) { p.lock.Lock() defer p.lock.Unlock() p.signer = signer p.signFn = signFn } // Seal implements consensus.Engine, attempting to create a sealed block using // the local signing credentials. func (p *PoSA) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { header := block.Header() // Sealing the genesis block is not supported number := header.Number.Uint64() if number == 0 { return errUnknownBlock } // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing) if p.config.Period == 0 && len(block.Transactions()) == 0 { return errors.New("sealing paused while waiting for transactions") } // Don't hold the signer fields for the entire sealing procedure p.lock.RLock() signer, signFn := p.signer, p.signFn p.lock.RUnlock() // Bail out if we're unauthorized to sign a block snap, err := p.snapshot(chain, number-1, header.ParentHash, nil) if err != nil { return err } if _, authorized := snap.Signers[signer]; !authorized { return errUnauthorizedSigner } // If we're amongst the recent signers, wait for the next block for seen, recent := range snap.Recents { if recent == signer { // Signer is among recents, only wait if the current block doesn't shift it out if limit := uint64(len(snap.Signers)/2 + 1); number < limit || seen > number-limit { return errors.New("signed recently, must wait for others") } } } // Sweet, the protocol permits us to sign the block, wait for our time delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple if header.Difficulty.Cmp(diffNoTurn) == 0 { // It's not our turn explicitly to sign, delay it a bit wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime delay += noturnMinimumDelay + time.Duration(rand.Int63n(int64(wiggle))) log.Trace("Out-of-turn signing requested", "wiggle", common.PrettyDuration(wiggle)) } // Sign all the things! var ( sighash []byte retryCount int = 0 ) for loop := true; loop && (retryCount < p.poSASignerRetryCount+1 || p.poSASignerRetryCount == -1); retryCount++ { curBlockNumber := chain.CurrentHeader().Number if curBlockNumber.Cmp(header.Number) >= 0 { return fmt.Errorf("current block number is greater than or equal to signing block number. current block number: %d, signing block number: %d", curBlockNumber, header.Number) } sighash, err = signFn(accounts.Account{Address: signer}, accounts.MimetypePoSA, PoSARLP(header)) if err == nil { loop = false } else { log.Warn("PoSA signer failed to sign block.", "number", header.Number.Uint64(), "retry", retryCount, "err", err) select { case <-stop: log.Warn("PoSA signing aborted", "number", header.Number.Uint64()) return nil case <-time.After(p.poSASignerRetryInterval): continue } } } if sighash == nil { return fmt.Errorf("failed to sign block. number: %d, retry count: %d", header.Number.Uint64(), retryCount-1) } copy(header.Extra[len(header.Extra)-extraSeal:], sighash) // Wait until sealing is terminated or delay timeout. log.Trace("Waiting for slot to sign and propagate", "delay", common.PrettyDuration(delay)) go func() { select { case <-stop: return case <-time.After(delay): // Prevent commit block which signed already. if header.Difficulty.Cmp(diffNoTurn) == 0 && chain.CurrentHeader().Number.Uint64() >= header.Number.Uint64() { log.Info("Current header has updated to abort this seal", "number", header.Number.Uint64(), "sealhash", SealHash(header)) return } } select { case results <- block.WithSeal(header): default: log.Warn("Sealing result is not read by miner", "sealhash", SealHash(header)) } }() return nil } // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // that a new block should have: // * DIFF_NOTURN(2) if BLOCK_NUMBER % SIGNER_COUNT != SIGNER_INDEX // * DIFF_INTURN(1) if BLOCK_NUMBER % SIGNER_COUNT == SIGNER_INDEX func (p *PoSA) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { snap, err := p.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil) if err != nil { return nil } p.lock.RLock() signer := p.signer p.lock.RUnlock() return calcDifficulty(snap, 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) } // SealHash returns the hash of a block prior to it being sealed. func (p *PoSA) SealHash(header *types.Header) common.Hash { return SealHash(header) } // Close implements consensus.Engine. It's a noop for posa as there are no background threads. func (p *PoSA) Close() error { return nil } // APIs implements consensus.Engine, returning the user facing RPC API to allow // controlling the signer voting. func (p *PoSA) APIs(chain consensus.ChainHeaderReader) []rpc.API { return []rpc.API{{ Namespace: "posa", Service: &API{chain: chain, posa: p}, }} } // SealHash returns the hash of a block prior to it being sealed. func SealHash(header *types.Header) (hash common.Hash) { hasher := sha3.NewLegacyKeccak256() encodeSigHeader(hasher, header) hasher.(crypto.KeccakState).Read(hash[:]) return hash } // PoSARLP returns the rlp bytes which needs to be signed for the proof-of-authority // sealing. The RLP to sign consists of the entire header apart from the 65 byte signature // contained at the end of the extra data. // // Note, the method requires the extra data to be at least 65 bytes, otherwise it // panics. This is done to avoid accidentally using both forms (signature present // or not), which could be abused to produce different hashes for the same header. func PoSARLP(header *types.Header) []byte { b := new(bytes.Buffer) encodeSigHeader(b, header) return b.Bytes() } func encodeSigHeader(w io.Writer, header *types.Header) { enc := []interface{}{ header.ParentHash, header.UncleHash, header.Coinbase, header.Root, header.TxHash, header.ReceiptHash, header.Bloom, header.Difficulty, header.Number, header.GasLimit, header.GasUsed, header.Time, header.Extra[:len(header.Extra)-crypto.SignatureLength], // Yes, this will panic if extra is too short header.MixDigest, header.Nonce, } if header.BaseFee != nil { enc = append(enc, header.BaseFee) } if header.WithdrawalsHash != nil { enc = append(enc, header.WithdrawalsHash) } if header.BlobGasUsed != nil { enc = append(enc, header.BlobGasUsed) } if header.ExcessBlobGas != nil { enc = append(enc, header.ExcessBlobGas) } if header.ParentBeaconRoot != nil { enc = append(enc, header.ParentBeaconRoot) } if header.RequestsHash != nil { enc = append(enc, header.RequestsHash) } if err := rlp.Encode(w, enc); err != nil { panic("can't encode: " + err.Error()) } } // AccumulateRewards credits the coinbase of the given block with the mining // reward. The total reward only consists of the static block reward. func accumulateRewards(config *params.ChainConfig, stateDB vm.StateDB, header *types.Header, uncles []*types.Header) error { // Select the correct block reward based on chain progression blockReward := Phase1BlockReward // Check if the RewardTreeFork is active for this block if config.IsRewardTreeFork(header.Number) { blockReward = RewardTreeForkBlockReward } // Check potential overflow without changing any balance balance := stateDB.GetBalance(header.Coinbase) _, overflow := new(uint256.Int).AddOverflow(balance, blockReward) if overflow { log.Error("Balance overflow detected") return errors.New("validator contract balance overflow") } // Add the block reward to the coinbase address stateDB.AddBalance(header.Coinbase, blockReward, tracing.BalanceIncreaseRewardMineBlock) return nil }