pull/4389/head
frozen 2 years ago committed by Casey Gardiner
parent 5e1878aedc
commit 597ba2d6f1
  1. 2
      consensus/engine/consensus_engine.go
  2. 2
      core/blockchain.go
  3. 24
      core/blockchain_impl.go
  4. 4
      core/blockchain_stub.go
  5. 7
      core/epochchain.go
  6. 2
      core/epochchain_test.go
  7. 3
      core/evm_test.go
  8. 3
      core/tx_pool_test.go
  9. 1
      hmy/downloader/adapter.go
  10. 4
      hmy/downloader/downloader.go
  11. 6
      internal/chain/engine.go
  12. 5
      internal/shardchain/shardchains.go
  13. 9
      node/node_handler_test.go
  14. 3
      node/node_newblock_test.go
  15. 3
      node/node_test.go
  16. 5
      node/worker/worker_test.go
  17. 2
      test/chain/main.go
  18. 4
      test/chain/reward/main.go

@ -79,7 +79,7 @@ type Engine interface {
// via the VerifySeal method.
VerifyHeader(chain ChainReader, header *block.Header, seal bool) error
// Similiar to VerifyHeader, which is only for verifying the block headers of one's own chain, this verification
// VerifyHeaderSignature similar to VerifyHeader, which is only for verifying the block headers of one's own chain, this verification
// is used for verifying "incoming" block header against commit signature and bitmap sent from the other chain cross-shard via libp2p.
// i.e. this header verification api is more flexible since the caller specifies which commit signature and bitmap to use
// for verifying the block header, which is necessary for cross-shard block header verification. Example of such is cross-shard transaction.

@ -126,6 +126,8 @@ type BlockChain interface {
GetHeaderByNumber(number uint64) *block.Header
// Config retrieves the blockchain's chain configuration.
Config() *params.ChainConfig
// Engine retrieves the blockchain's consensus engine.
Engine() engine.Engine
// SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent.
SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription
// SubscribeTraceEvent registers a subscription of ChainEvent.

@ -49,8 +49,6 @@ import (
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/chain"
chain2 "github.com/harmony-one/harmony/internal/chain"
harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/tikv"
@ -192,6 +190,7 @@ type BlockChainImpl struct {
// procInterrupt must be atomically called
procInterrupt int32 // interrupt signaler for block processing
engine consensus_engine.Engine
processor Processor // block processor interface
validator Validator // block and state validator interface
vmConfig vm.Config
@ -205,9 +204,9 @@ type BlockChainImpl struct {
// NewBlockChainWithOptions same as NewBlockChain but can accept additional behaviour options.
func NewBlockChainWithOptions(
db ethdb.Database, stateCache state.Database, beaconChain BlockChain, cacheConfig *CacheConfig, chainConfig *params.ChainConfig,
vmConfig vm.Config, options Options,
engine consensus_engine.Engine, vmConfig vm.Config, options Options,
) (*BlockChainImpl, error) {
return newBlockChainWithOptions(db, stateCache, beaconChain, cacheConfig, chainConfig, vmConfig, options)
return newBlockChainWithOptions(db, stateCache, beaconChain, cacheConfig, chainConfig, engine, vmConfig, options)
}
// NewBlockChain returns a fully initialised block chain using information
@ -215,15 +214,15 @@ func NewBlockChainWithOptions(
// Processor.
func NewBlockChain(
db ethdb.Database, stateCache state.Database, beaconChain BlockChain, cacheConfig *CacheConfig, chainConfig *params.ChainConfig,
vmConfig vm.Config,
engine consensus_engine.Engine, vmConfig vm.Config,
) (*BlockChainImpl, error) {
return newBlockChainWithOptions(db, stateCache, beaconChain, cacheConfig, chainConfig, vmConfig, Options{})
return newBlockChainWithOptions(db, stateCache, beaconChain, cacheConfig, chainConfig, engine, vmConfig, Options{})
}
func newBlockChainWithOptions(
db ethdb.Database, stateCache state.Database, beaconChain BlockChain,
cacheConfig *CacheConfig, chainConfig *params.ChainConfig,
vmConfig vm.Config, options Options) (*BlockChainImpl, error) {
engine consensus_engine.Engine, vmConfig vm.Config, options Options) (*BlockChainImpl, error) {
bodyCache, _ := lru.New(bodyCacheLimit)
bodyRLPCache, _ := lru.New(bodyCacheLimit)
@ -267,6 +266,7 @@ func newBlockChainWithOptions(
blockAccumulatorCache: blockAccumulatorCache,
leaderPubKeyFromCoinbase: leaderPubKeyFromCoinbase,
blockchainPruner: newBlockchainPruner(db),
engine: engine,
vmConfig: vmConfig,
badBlocks: badBlocks,
pendingSlashes: slash.Records{},
@ -353,7 +353,7 @@ func VerifyCrossLink(blockchain BlockChain, cl types.CrossLink) error {
if blockchain.ShardID() != shard.BeaconChainShardID {
return errors.New("[VerifyCrossLink] Shard chains should not verify cross links")
}
engine := chain.Engine()
engine := blockchain.Engine()
if err := engine.VerifyCrossLink(blockchain, cl); err != nil {
return errors.Wrap(err, "[VerifyCrossLink]")
@ -424,7 +424,7 @@ func (bc *BlockChainImpl) ValidateNewBlock(block *types.Block, beaconChain Block
Msg("[ValidateNewBlock] Cannot validate header for the new block")
return err
}
if err := chain.Engine().VerifyVRF(
if err := bc.Engine().VerifyVRF(
bc, block.Header(),
); err != nil {
utils.Logger().Error().
@ -436,7 +436,7 @@ func (bc *BlockChainImpl) ValidateNewBlock(block *types.Block, beaconChain Block
"[ValidateNewBlock] Cannot verify vrf for the new block",
)
}
err := chain.Engine().VerifyShardState(bc, beaconChain, block.Header())
err := bc.Engine().VerifyShardState(bc, beaconChain, block.Header())
if err != nil {
utils.Logger().Error().
Str("blockHash", block.Hash().Hex()).
@ -1577,7 +1577,7 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i
seals[i] = true
}
// Note that VerifyHeaders verifies headers in the chain in parallel
abort, results := chain2.Engine().VerifyHeaders(bc, headers, seals)
abort, results := bc.Engine().VerifyHeaders(bc, headers, seals)
verifyHeadersResults = results
defer close(abort)
}
@ -2045,6 +2045,8 @@ func (bc *BlockChainImpl) GetHeaderByNumber(number uint64) *block.Header {
func (bc *BlockChainImpl) Config() *params.ChainConfig { return bc.chainConfig }
func (bc *BlockChainImpl) Engine() consensus_engine.Engine { return bc.engine }
func (bc *BlockChainImpl) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription {
return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch))
}

@ -141,6 +141,10 @@ func (a Stub) Config() *params.ChainConfig {
return nil
}
func (a Stub) Engine() engine.Engine {
return nil
}
func (a Stub) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription {
return nil
}

@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/harmony-one/harmony/block"
"github.com/harmony-one/harmony/consensus/engine"
"github.com/harmony-one/harmony/core/rawdb"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
@ -128,7 +129,7 @@ func (bc *EpochChain) InsertChain(blocks types.Blocks, _ bool) (int, error) {
}
// Signature validation.
err = chain.Engine().VerifyHeaderSignature(bc, block.Header(), sig, bitmap)
err = bc.Engine().VerifyHeaderSignature(bc, block.Header(), sig, bitmap)
if err != nil {
return i, errors.Wrap(err, "failed signature validation")
}
@ -238,6 +239,10 @@ func (bc *EpochChain) Config() *params.ChainConfig {
return bc.chainConfig
}
func (bc *EpochChain) Engine() engine.Engine {
return chain.Engine()
}
func (bc *EpochChain) ReadShardState(epoch *big.Int) (*shard.State, error) {
cacheKey := string(epoch.Bytes())
if cached, ok := bc.shardStateCache.Get(cacheKey); ok {

@ -12,7 +12,7 @@ import (
func TestGenesisBlock(t *testing.T) {
db := rawdb.NewMemoryDatabase()
err := (&core.GenesisInitializer{NetworkType: nodeconfig.Mainnet}).InitChainDB(db, 0)
err := (&core.GenesisInitializer{}).InitChainDB(db, nodeconfig.Mainnet, 0)
require.NoError(t, err)
chain, err := core.NewEpochChain(db, nil, vm.Config{})

@ -21,6 +21,7 @@ import (
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/crypto/hash"
"github.com/harmony-one/harmony/internal/chain"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/numeric"
staking "github.com/harmony-one/harmony/staking/types"
@ -44,7 +45,7 @@ func getTestEnvironment(testBankKey ecdsa.PrivateKey) (*BlockChainImpl, *state.D
genesis := gspec.MustCommit(database)
// fake blockchain
chain, _ := NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, vm.Config{})
chain, _ := NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, chain.Engine(), vm.Config{})
db, _ := chain.StateAt(genesis.Root())
// make a fake block header (use epoch 1 so that locked tokens can be tested)

@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/chain"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
@ -158,7 +159,7 @@ func createBlockChain() *BlockChainImpl {
database := rawdb.NewMemoryDatabase()
genesis := gspec.MustCommit(database)
_ = genesis
blockchain, _ := NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, vm.Config{})
blockchain, _ := NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, chain.Engine(), vm.Config{})
return blockchain
}

@ -25,6 +25,7 @@ type syncProtocol interface {
type blockChain interface {
engine.ChainReader
Engine() engine.Engine
InsertChain(chain types.Blocks, verifyHeaders bool) (int, error)
WriteCommitSig(blockNum uint64, lastCommits []byte) error

@ -308,10 +308,10 @@ func verifyAndInsertBlock(bc blockChain, block *types.Block, nextBlocks ...*type
}
}
if err := chain.Engine().VerifyHeaderSignature(bc, block.Header(), sigBytes, bitmap); err != nil {
if err := bc.Engine().VerifyHeaderSignature(bc, block.Header(), sigBytes, bitmap); err != nil {
return &sigVerifyErr{err}
}
if err := chain.Engine().VerifyHeader(bc, block.Header(), true); err != nil {
if err := bc.Engine().VerifyHeader(bc, block.Header(), true); err != nil {
return errors.Wrap(err, "[VerifyHeader]")
}
if _, err := bc.InsertChain(types.Blocks{block}, false); err != nil {

@ -45,10 +45,10 @@ type engineImpl struct {
verifiedSigCache *lru.Cache // verifiedSigKey -> struct{}{}
}
var internal = NewEngine()
var internal engine.Engine = NewEngine()
// NewEngine creates Engine with some cache
func NewEngine() *engineImpl {
func NewEngine() engine.Engine {
sigCache, _ := lru.New(verifiedSigCache)
epochCtxCache, _ := lru.New(epochCtxCache)
return &engineImpl{
@ -57,7 +57,7 @@ func NewEngine() *engineImpl {
}
}
func Engine() *engineImpl {
func Engine() engine.Engine {
return internal
}

@ -6,6 +6,7 @@ import (
"time"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/internal/chain"
harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/shardchain/tikv_manage"
@ -133,7 +134,7 @@ func (sc *CollectionImpl) ShardChain(shardID uint32, options ...core.Options) (c
}
if shardID == shard.BeaconChainShardID {
bc, err = core.NewBlockChainWithOptions(
db, stateCache, bc, cacheConfig, &chainConfig, vm.Config{}, opts,
db, stateCache, bc, cacheConfig, &chainConfig, chain.Engine(), vm.Config{}, opts,
)
} else {
beacon, ok := sc.pool[shard.BeaconChainShardID]
@ -142,7 +143,7 @@ func (sc *CollectionImpl) ShardChain(shardID uint32, options ...core.Options) (c
}
bc, err = core.NewBlockChainWithOptions(
db, stateCache, beacon, cacheConfig, &chainConfig, vm.Config{}, opts,
db, stateCache, beacon, cacheConfig, &chainConfig, chain.Engine(), vm.Config{}, opts,
)
}
}

@ -33,9 +33,8 @@ func TestAddNewBlock(t *testing.T) {
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, &chainconfig,
nil, testDBFactory, &core.GenesisInitializer{}, nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType(),
)
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
@ -89,9 +88,8 @@ func TestVerifyNewBlock(t *testing.T) {
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, &chainconfig,
nil, testDBFactory, &core.GenesisInitializer{}, nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType(),
)
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
@ -144,9 +142,8 @@ func TestVerifyVRF(t *testing.T) {
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, &chainconfig,
nil, testDBFactory, &core.GenesisInitializer{}, nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType(),
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
if err != nil {

@ -34,9 +34,8 @@ func TestFinalizeNewBlockAsync(t *testing.T) {
t.Fatalf("newhost failure: %v", err)
}
var testDBFactory = &shardchain.MemDBFactory{}
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, &chainconfig,
nil, testDBFactory, &core.GenesisInitializer{}, nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType(),
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
require.NoError(t, err)

@ -36,9 +36,8 @@ func TestNewNode(t *testing.T) {
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
)
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, &chainconfig,
nil, testDBFactory, &core.GenesisInitializer{}, nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType(),
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
if err != nil {

@ -6,6 +6,7 @@ import (
"testing"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/internal/chain"
"github.com/ethereum/go-ethereum/core/rawdb"
@ -43,7 +44,7 @@ func TestNewWorker(t *testing.T) {
genesis := gspec.MustCommit(database)
_ = genesis
chain, err := core.NewBlockChain(database, state.NewDatabase(database), &core.BlockChainImpl{}, nil, gspec.Config, vm.Config{})
chain, err := core.NewBlockChain(database, state.NewDatabase(database), &core.BlockChainImpl{}, nil, gspec.Config, chain.Engine(), vm.Config{})
if err != nil {
t.Error(err)
@ -69,7 +70,7 @@ func TestCommitTransactions(t *testing.T) {
)
gspec.MustCommit(database)
chain, _ := core.NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, vm.Config{})
chain, _ := core.NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, chain.Engine(), vm.Config{})
// Create a new worker
worker := New(params.TestChainConfig, chain, nil)

@ -206,7 +206,7 @@ func playFaucetContract(chain core.BlockChain) {
func main() {
genesis := gspec.MustCommit(database)
chain, _ := core.NewBlockChain(database, harmonyState.NewDatabase(database), nil, nil, gspec.Config, vm.Config{})
chain, _ := core.NewBlockChain(database, harmonyState.NewDatabase(database), nil, nil, gspec.Config, chain.Engine(), vm.Config{})
txpool := core.NewTxPool(core.DefaultTxPoolConfig, chainConfig, chain, types.NewTransactionErrorSink())
backend := &testWorkerBackend{

@ -7,6 +7,7 @@ import (
"time"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/harmony-one/harmony/internal/chain"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/crypto/bls"
@ -107,7 +108,8 @@ func main() {
database := rawdb.NewMemoryDatabase()
genesis := gspec.MustCommit(database)
_ = genesis
bc, _ := core.NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, vm.Config{})
engine := chain.NewEngine()
bc, _ := core.NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, engine, vm.Config{})
statedb, _ := state.New(common2.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()))
msg := createValidator()
statedb.AddBalance(msg.ValidatorAddress, new(big.Int).Mul(big.NewInt(5e18), big.NewInt(2000)))

Loading…
Cancel
Save