Add vrf tests and fix travis

pull/3682/head
Rongjian Lan 4 years ago
parent 518f8dadb6
commit 31be87712d
  1. 8
      internal/chain/engine.go
  2. 4
      node/node_handler.go
  3. 61
      node/node_handler_test.go

@ -153,6 +153,14 @@ func (e *engineImpl) VerifyVRF(
"[VerifyVRF] invalid vrf data format or no vrf proposed %x", header.Vrf(), "[VerifyVRF] invalid vrf data format or no vrf proposed %x", header.Vrf(),
) )
} }
if !bc.Config().IsVRF(header.Epoch()) {
if len(header.Vrf()) != 0 {
errors.Errorf(
"[VerifyVRF] vrf data present in pre-vrf epoch %x", header.Vrf(),
)
}
return nil
}
leaderPubKey, err := GetLeaderPubKeyFromCoinbase(bc, header) leaderPubKey, err := GetLeaderPubKeyFromCoinbase(bc, header)

@ -284,7 +284,7 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error {
Str("blockHash", newBlock.Hash().Hex()). Str("blockHash", newBlock.Hash().Hex()).
Err(err). Err(err).
Msg("[VerifyNewBlock] Cannot verify vrf for the new block") Msg("[VerifyNewBlock] Cannot verify vrf for the new block")
return errors.New( return errors.Wrap(err,
"[VerifyNewBlock] Cannot verify vrf for the new block", "[VerifyNewBlock] Cannot verify vrf for the new block",
) )
} }
@ -296,7 +296,7 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error {
Str("blockHash", newBlock.Hash().Hex()). Str("blockHash", newBlock.Hash().Hex()).
Err(err). Err(err).
Msg("[VerifyNewBlock] Cannot verify shard state for the new block") Msg("[VerifyNewBlock] Cannot verify shard state for the new block")
return errors.New( return errors.Wrap(err,
"[VerifyNewBlock] Cannot verify shard state for the new block", "[VerifyNewBlock] Cannot verify shard state for the new block",
) )
} }

@ -90,6 +90,53 @@ func TestVerifyNewBlock(t *testing.T) {
archiveMode[1] = false archiveMode[1] = false
node := New(host, consensus, testDBFactory, nil, archiveMode) node := New(host, consensus, testDBFactory, nil, archiveMode)
txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{}
node.Worker.CommitTransactions(
txs, stks, common.Address{},
)
commitSigs := make(chan []byte)
go func() {
commitSigs <- []byte{}
}()
block, _ := node.Worker.FinalizeNewBlock(
commitSigs, func() uint64 { return 0 }, common.Address{}, nil, nil,
)
// work around vrf verification as it's tested in another test.
node.Blockchain().Config().VRFEpoch = big.NewInt(2)
if err := node.VerifyNewBlock(block); err != nil {
t.Error("New block is not verified successfully:", err)
}
}
func TestVerifyVRF(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,
})
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
)
consensus, err := consensus.New(
host, shard.BeaconChainShardID, leader, multibls.GetPrivateKeys(blsKey), decider,
)
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
archiveMode := make(map[uint32]bool)
archiveMode[0] = true
archiveMode[1] = false
node := New(host, consensus, testDBFactory, nil, archiveMode)
consensus.Blockchain = node.Blockchain()
txs := make(map[common.Address]types.Transactions) txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{} stks := staking.StakingTransactions{}
node.Worker.CommitTransactions( node.Worker.CommitTransactions(
@ -113,13 +160,21 @@ func TestVerifyNewBlock(t *testing.T) {
nil, nil,
} }
com.Slots = append(com.Slots, curNodeID) com.Slots = append(com.Slots, curNodeID)
shardState.Epoch = big.NewInt(0) shardState.Epoch = big.NewInt(1)
shardState.Shards = append(shardState.Shards, com) shardState.Shards = append(shardState.Shards, com)
node.Consensus.LeaderPubKey = &bls.PublicKeyWrapper{spKey, pubKey}
node.Worker.GetCurrentHeader().SetEpoch(big.NewInt(1))
node.Consensus.GenerateVrfAndProof(node.Worker.GetCurrentHeader())
block, _ := node.Worker.FinalizeNewBlock( block, _ := node.Worker.FinalizeNewBlock(
commitSigs, func() uint64 { return 0 }, ecdsaAddr, nil, shardState, commitSigs, func() uint64 { return 0 }, ecdsaAddr, nil, shardState,
) )
if err := node.VerifyNewBlock(block); err != nil { // Write shard state for the new epoch
t.Error("New block is not verified successfully:", err) node.Blockchain().WriteShardStateBytes(node.Blockchain().ChainDb(), big.NewInt(1), node.Worker.GetCurrentHeader().ShardState())
if err := node.Blockchain().Engine().VerifyVRF(
node.Blockchain(), block.Header(),
); err != nil {
t.Error("New vrf is not verified successfully:", err)
} }
} }

Loading…
Cancel
Save