The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/node/node_newblock_test.go

90 lines
2.8 KiB

4 years ago
package node
import (
"strings"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/consensus/quorum"
"github.com/harmony-one/harmony/core"
4 years ago
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/chain"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/registry"
"github.com/harmony-one/harmony/internal/shardchain"
4 years ago
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/multibls"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/shard"
staking "github.com/harmony-one/harmony/staking/types"
"github.com/stretchr/testify/require"
4 years ago
)
func TestFinalizeNewBlockAsync(t *testing.T) {
blsKey := bls.RandPrivateKey()
pubKey := blsKey.GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2p.NewHost(p2p.HostConfig{
Self: &leader,
BLSKey: priKey,
})
4 years ago
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
var testDBFactory = &shardchain.MemDBFactory{}
engine := chain.NewEngine()
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, engine, &chainconfig,
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
require.NoError(t, err)
4 years ago
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
)
consensus, err := consensus.New(
host, shard.BeaconChainShardID, multibls.GetPrivateKeys(blsKey), nil, decider, 3, false,
4 years ago
)
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
node := New(host, consensus, engine, collection, nil, nil, nil, nil, nil, registry.New().SetBlockchain(blockchain))
4 years ago
node.Worker.UpdateCurrent()
txs := make(map[common.Address]types.Transactions)
4 years ago
stks := staking.StakingTransactions{}
node.Worker.CommitTransactions(
txs, stks, common.Address{},
4 years ago
)
commitSigs := make(chan []byte)
go func() {
commitSigs <- []byte{}
}()
block, _ := node.Worker.FinalizeNewBlock(
commitSigs, func() uint64 { return 0 }, common.Address{}, nil, nil,
4 years ago
)
if err := VerifyNewBlock(nil, blockchain, nil)(block); err != nil {
4 years ago
t.Error("New block is not verified successfully:", err)
}
node.Blockchain().InsertChain(types.Blocks{block}, false)
node.Worker.UpdateCurrent()
_, err = node.Worker.FinalizeNewBlock(
commitSigs, func() uint64 { return 0 }, common.Address{}, nil, nil,
4 years ago
)
if !strings.Contains(err.Error(), "cannot finalize block") {
t.Error("expect timeout on FinalizeNewBlock")
}
}