Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dead code and cleanup. #4477

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ import (
//
// BlockValidator implements validator.
type BlockValidator struct {
config *params.ChainConfig // Chain configuration options
bc BlockChain // Canonical blockchain
engine consensus_engine.Engine // Consensus engine used for validating
bc BlockChain // Canonical blockchain
}

// NewBlockValidator returns a new block validator which is safe for re-use
func NewBlockValidator(config *params.ChainConfig, blockchain BlockChain, engine consensus_engine.Engine) *BlockValidator {
func NewBlockValidator(blockchain BlockChain) *BlockValidator {
validator := &BlockValidator{
config: config,
engine: engine,
bc: blockchain,
bc: blockchain,
}
return validator
}
Expand Down Expand Up @@ -103,7 +99,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.DB, re
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash(), receiptSha)
}

if v.config.AcceptsCrossTx(block.Epoch()) {
if v.bc.Config().AcceptsCrossTx(block.Epoch()) {
cxsSha := cxReceipts.ComputeMerkleRoot()
if cxsSha != header.OutgoingReceiptHash() {
legacySha := types.DeriveMultipleShardsSha(cxReceipts)
Expand All @@ -115,7 +111,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.DB, re

// Validate the state root against the received state root and throw
// an error if they don't match.
if root := statedb.IntermediateRoot(v.config.IsS3(header.Epoch())); header.Root() != root {
if root := statedb.IntermediateRoot(v.bc.Config().IsS3(header.Epoch())); header.Root() != root {
dump, _ := rlp.EncodeToBytes(header)
const msg = "invalid merkle root (remote: %x local: %x, rlp dump %s)"
return fmt.Errorf(msg, header.Root(), root, hex.EncodeToString(dump))
Expand All @@ -131,25 +127,11 @@ func (v *BlockValidator) ValidateHeader(block *types.Block, seal bool) error {
return errors.New("block is nil")
}
if h := block.Header(); h != nil {
return v.engine.VerifyHeader(v.bc, h, true)
return v.bc.Engine().VerifyHeader(v.bc, h, true)
}
return errors.New("header field was nil")
}

// ValidateHeaders verifies a batch of blocks' headers concurrently. The method returns a quit channel
// to abort the operations and a results channel to retrieve the async verifications
func (v *BlockValidator) ValidateHeaders(chain []*types.Block) (chan<- struct{}, <-chan error) {
// Start the parallel header verifier
headers := make([]*block.Header, len(chain))
seals := make([]bool, len(chain))

for i, block := range chain {
headers[i] = block.Header()
seals[i] = true
}
return v.engine.VerifyHeaders(v.bc, headers, seals)
}

// CalcGasLimit computes the gas limit of the next block after parent. It aims
// to keep the baseline gas above the provided floor, and increase it towards the
// ceil if the blocks are full. If the ceil is exceeded, it will always decrease
Expand Down Expand Up @@ -189,7 +171,7 @@ func CalcGasLimit(parent *block.Header, gasFloor, gasCeil uint64) uint64 {

// ValidateCXReceiptsProof checks whether the given CXReceiptsProof is consistency with itself
func (v *BlockValidator) ValidateCXReceiptsProof(cxp *types.CXReceiptsProof) error {
if !v.config.AcceptsCrossTx(cxp.Header.Epoch()) {
if !v.bc.Config().AcceptsCrossTx(cxp.Header.Epoch()) {
return errors.New("[ValidateCXReceiptsProof] cross shard receipt received before cx fork")
}

Expand Down Expand Up @@ -249,5 +231,5 @@ func (v *BlockValidator) ValidateCXReceiptsProof(cxp *types.CXReceiptsProof) err
// (4) verify blockHeader with seal
var commitSig bls.SerializedSignature
copy(commitSig[:], cxp.CommitSig)
return v.engine.VerifyHeaderSignature(v.bc, cxp.Header, commitSig, cxp.CommitBitmap)
return v.bc.Engine().VerifyHeaderSignature(v.bc, cxp.Header, commitSig, cxp.CommitBitmap)
}
2 changes: 0 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ type BlockChain interface {
// CurrentBlock retrieves the current head block of the canonical chain. The
// block is retrieved from the blockchain's internal cache.
CurrentBlock() *types.Block
// Validator returns the current validator.
Validator() Validator
// Processor returns the current processor.
Processor() Processor
// State returns a new mutable state based on the current HEAD block.
Expand Down
36 changes: 9 additions & 27 deletions core/blockchain_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ type BlockChainImpl struct {
procInterrupt int32 // interrupt signaler for block processing

engine consensus_engine.Engine
processor Processor // block processor interface
validator Validator // block and state validator interface
processor *StateProcessor // block processor interface
validator *BlockValidator
vmConfig vm.Config
badBlocks *lru.Cache // Bad block cache
pendingSlashes slash.Records
Expand Down Expand Up @@ -334,8 +334,8 @@ func newBlockChainWithOptions(
beaconChain = bc
}

bc.SetValidator(NewBlockValidator(chainConfig, bc, engine))
bc.SetProcessor(NewStateProcessor(chainConfig, bc, beaconChain, engine))
bc.validator = NewBlockValidator(bc)
bc.processor = NewStateProcessor(bc, beaconChain)

// Load any existing snapshot, regenerating it if loading failed
if bc.cacheConfig.SnapshotLimit > 0 {
Expand Down Expand Up @@ -447,7 +447,7 @@ func VerifyIncomingReceipts(blockchain BlockChain, block *types.Block) error {
}
}

if err := blockchain.Validator().ValidateCXReceiptsProof(cxp); err != nil {
if err := NewBlockValidator(blockchain).ValidateCXReceiptsProof(cxp); err != nil {
Frozen marked this conversation as resolved.
Show resolved Hide resolved
return errors.Wrapf(err, "[verifyIncomingReceipts] verification failed")
}
}
Expand Down Expand Up @@ -479,7 +479,7 @@ func (bc *BlockChainImpl) ValidateNewBlock(block *types.Block, beaconChain Block
if block.NumberU64() <= bc.CurrentBlock().NumberU64() {
return errors.Errorf("block with the same block number is already committed: %d", block.NumberU64())
}
if err := bc.Validator().ValidateHeader(block, true); err != nil {
if err := bc.validator.ValidateHeader(block, true); err != nil {
utils.Logger().Error().
Str("blockHash", block.Hash().Hex()).
Err(err).
Expand Down Expand Up @@ -541,7 +541,7 @@ func (bc *BlockChainImpl) validateNewBlock(block *types.Block) error {
}

// Verify all the hash roots (state, txns, receipts, cross-shard)
if err := bc.Validator().ValidateState(
if err := bc.validator.ValidateState(
block, state, receipts, cxReceipts, usedGas,
); err != nil {
bc.reportBlock(block, receipts, err)
Expand Down Expand Up @@ -727,24 +727,6 @@ func (bc *BlockChainImpl) CurrentFastBlock() *types.Block {
return bc.currentFastBlock.Load().(*types.Block)
}

func (bc *BlockChainImpl) SetProcessor(processor Processor) {
bc.procmu.Lock()
defer bc.procmu.Unlock()
bc.processor = processor
}

func (bc *BlockChainImpl) SetValidator(validator Validator) {
bc.procmu.Lock()
defer bc.procmu.Unlock()
bc.validator = validator
}

func (bc *BlockChainImpl) Validator() Validator {
bc.procmu.RLock()
defer bc.procmu.RUnlock()
return bc.validator
}

func (bc *BlockChainImpl) Processor() Processor {
bc.procmu.RLock()
defer bc.procmu.RUnlock()
Expand Down Expand Up @@ -1764,7 +1746,7 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i
err = <-verifyHeadersResults
}
if err == nil {
err = bc.Validator().ValidateBody(block)
err = NewBlockValidator(bc).ValidateBody(block)
ONECasey marked this conversation as resolved.
Show resolved Hide resolved
}
switch {
case err == ErrKnownBlock:
Expand Down Expand Up @@ -1882,7 +1864,7 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i

// Validate the state using the default validator
substart = time.Now()
if err := bc.Validator().ValidateState(
if err := bc.validator.ValidateState(
block, state, receipts, cxReceipts, usedGas,
); err != nil {
bc.reportBlock(block, receipts, err)
Expand Down
19 changes: 11 additions & 8 deletions core/staking_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ var (
)

func checkDuplicateFields(
bc ChainContext, state vm.StateDB,
addrs []common.Address, state vm.StateDB,
ONECasey marked this conversation as resolved.
Show resolved Hide resolved
validator common.Address, identity string, blsKeys []bls.SerializedPublicKey,
) error {
addrs, err := bc.ReadValidatorList()
if err != nil {
return err
}

checkIdentity := identity != ""
checkBlsKeys := len(blsKeys) != 0

Expand Down Expand Up @@ -99,8 +94,12 @@ func VerifyAndCreateValidatorFromMsg(
errValidatorExist, common2.MustAddressToBech32(msg.ValidatorAddress),
)
}
addrs, err := chainContext.ReadValidatorList()
if err != nil {
return nil, err
}
if err := checkDuplicateFields(
chainContext, stateDB,
addrs, stateDB,
msg.ValidatorAddress,
msg.Identity,
msg.SlotPubKeys); err != nil {
Expand Down Expand Up @@ -151,8 +150,12 @@ func VerifyAndEditValidatorFromMsg(
if msg.SlotKeyToAdd != nil {
newBlsKeys = append(newBlsKeys, *msg.SlotKeyToAdd)
}
addrs, err := chainContext.ReadValidatorList()
if err != nil {
return nil, err
}
if err := checkDuplicateFields(
chainContext, stateDB,
addrs, stateDB,
msg.ValidatorAddress,
msg.Identity,
newBlsKeys); err != nil {
Expand Down
16 changes: 5 additions & 11 deletions core/staking_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,6 @@ func TestCheckDuplicateFields(t *testing.T) {

expErr: nil,
},
{
// chain error
bc: &fakeErrChainContext{},
sdb: makeStateDBForStake(t),
validator: createValidatorAddr,
identity: makeIdentityStr("new validator"),
pubs: []bls.SerializedPublicKey{blsKeys[11].pub},

expErr: errors.New("error intended"),
},
{
// validators read from chain not in state
bc: func() *fakeChainContext {
Expand Down Expand Up @@ -201,7 +191,11 @@ func TestCheckDuplicateFields(t *testing.T) {
},
}
for i, test := range tests {
err := checkDuplicateFields(test.bc, test.sdb, test.validator, test.identity, test.pubs)
addrs, err := test.bc.ReadValidatorList()
if err != nil {
t.Fatal(err)
}
err = checkDuplicateFields(addrs, test.sdb, test.validator, test.identity, test.pubs)

if assErr := assertError(err, test.expErr); assErr != nil {
t.Errorf("Test %v: %v", i, assErr)
Expand Down
34 changes: 16 additions & 18 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/block"
consensus_engine "github.com/harmony-one/harmony/consensus/engine"
"github.com/harmony-one/harmony/consensus/reward"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types"
Expand All @@ -50,11 +49,9 @@ const (
//
// StateProcessor implements Processor.
type StateProcessor struct {
config *params.ChainConfig // Chain configuration options
bc BlockChain // Canonical blockchain
beacon BlockChain // Beacon chain
engine consensus_engine.Engine // Consensus engine used for block rewards
resultCache *lru.Cache // Cache for result after a certain block is processed
bc BlockChain // Canonical blockchain
beacon BlockChain // Beacon chain
resultCache *lru.Cache // Cache for result after a certain block is processed
}

// this structure is cached, and each individual element is returned
Expand All @@ -70,7 +67,7 @@ type ProcessorResult struct {

// NewStateProcessor initialises a new StateProcessor.
func NewStateProcessor(
config *params.ChainConfig, bc BlockChain, beacon BlockChain, engine consensus_engine.Engine,
bc BlockChain, beacon BlockChain,
) *StateProcessor {
if bc == nil {
panic("bc is nil")
Expand All @@ -80,14 +77,14 @@ func NewStateProcessor(
}
resultCache, _ := lru.New(resultCacheLimit)
return &StateProcessor{
config: config,
bc: bc,
beacon: beacon,
engine: engine,
resultCache: resultCache,
}
}

type UsedGas = uint64

// Process processes the state changes according to the Ethereum rules by running
// the transaction messages using the statedb and applying any rewards to both
// the processor (coinbase) and any included uncles.
Expand All @@ -99,7 +96,7 @@ func (p *StateProcessor) Process(
block *types.Block, statedb *state.DB, cfg vm.Config, readCache bool,
) (
types.Receipts, types.CXReceipts, []staking.StakeMsg,
[]*types.Log, uint64, reward.Reader, *state.DB, error,
[]*types.Log, UsedGas, reward.Reader, *state.DB, error,
) {
cacheKey := block.Hash()
if readCache {
Expand Down Expand Up @@ -133,7 +130,7 @@ func (p *StateProcessor) Process(
for i, tx := range block.Transactions() {
statedb.Prepare(tx.Hash(), block.Hash(), i)
receipt, cxReceipt, stakeMsgs, _, err := ApplyTransaction(
p.config, p.bc, &beneficiary, gp, statedb, header, tx, usedGas, cfg,
p.bc, &beneficiary, gp, statedb, header, tx, usedGas, cfg,
)
if err != nil {
return nil, nil, nil, nil, 0, nil, statedb, err
Expand All @@ -155,7 +152,7 @@ func (p *StateProcessor) Process(
for i, tx := range block.StakingTransactions() {
statedb.Prepare(tx.Hash(), block.Hash(), i+L)
receipt, _, err := ApplyStakingTransaction(
p.config, p.bc, &beneficiary, gp, statedb, header, tx, usedGas, cfg,
p.bc, &beneficiary, gp, statedb, header, tx, usedGas, cfg,
)
if err != nil {
return nil, nil, nil, nil, 0, nil, statedb, err
Expand All @@ -169,7 +166,7 @@ func (p *StateProcessor) Process(
// after transactions (to be consistent with the block proposal)
for _, cx := range block.IncomingReceipts() {
if err := ApplyIncomingReceipt(
p.config, statedb, header, cx,
p.bc.Config(), statedb, header, cx,
); err != nil {
return nil, nil,
nil, nil, 0, nil, statedb, errors.New("[Process] Cannot apply incoming receipts")
Expand All @@ -195,7 +192,7 @@ func (p *StateProcessor) Process(
// Block processing don't need to block on reward computation as in block proposal
sigsReady <- true
}()
_, payout, err := p.engine.Finalize(
_, payout, err := p.bc.Engine().Finalize(
p.bc,
p.beacon,
header, statedb, block.Transactions(),
Expand Down Expand Up @@ -246,8 +243,9 @@ func getTransactionType(
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB, header *block.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *types.CXReceipt, []staking.StakeMsg, uint64, error) {
txType := getTransactionType(config, header, tx)
func ApplyTransaction(bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB, header *block.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *types.CXReceipt, []staking.StakeMsg, uint64, error) {
config := bc.Config()
txType := getTransactionType(bc.Config(), header, tx)
if txType == types.InvalidTx {
return nil, nil, nil, 0, errors.New("Invalid Transaction Type")
}
Expand Down Expand Up @@ -350,9 +348,9 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
// indicating the block was invalid.
// staking transaction will use the code field in the account to store the staking information
func ApplyStakingTransaction(
config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB,
bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB,
header *block.Header, tx *staking.StakingTransaction, usedGas *uint64, cfg vm.Config) (receipt *types.Receipt, gas uint64, err error) {

config := bc.Config()
msg, err := StakingToMessage(tx, header.Number())
if err != nil {
return nil, 0, err
Expand Down
4 changes: 0 additions & 4 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ type Validator interface {
// via the VerifySeal method.
ValidateHeader(block *types.Block, seal bool) error

// ValidateHeaders verifies a batch of blocks' headers concurrently. The method returns a quit channel
// to abort the operations and a results channel to retrieve the async verifications
ValidateHeaders(chain []*types.Block) (chan<- struct{}, <-chan error)

// ValidateCXReceiptsProof checks whether the given CXReceiptsProof is consistency with itself
ValidateCXReceiptsProof(cxp *types.CXReceiptsProof) error
}
Expand Down
Loading