Add RandPreimage in block; Add pRnd into new block

pull/471/head
Rongjian Lan 6 years ago
parent 0418e663a6
commit edf06d1fda
  1. 3
      consensus/consensus_leader.go
  2. 12
      consensus/consensus_validator_test.go
  3. 9
      core/blockchain.go
  4. 10
      core/types/block.go
  5. 4
      node/node.go
  6. 3
      node/node_handler.go

@ -1,6 +1,7 @@
package consensus
import (
"encoding/binary"
"encoding/hex"
"strconv"
"time"
@ -61,7 +62,7 @@ func (consensus *Consensus) WaitForNewBlock(blockChannel chan *types.Block, stop
vrfBitmap.SetMask(bitmap)
// TODO: check validity of pRnd
_ = pRnd
newBlock.AddRandPreimage(binary.BigEndian.Uint32(pRnd))
}
startTime = time.Now()
utils.GetLogInstance().Debug("STARTING CONSENSUS", "numTxs", len(newBlock.Transactions()), "consensus", consensus, "startTime", startTime, "publicKeys", len(consensus.PublicKeys))

@ -44,9 +44,9 @@ func TestProcessMessageValidatorAnnounce(test *testing.T) {
test.Fatalf("newhost failure: %v", err)
}
consensusLeader := New(host, "0", []p2p.Peer{validator1, validator2, validator3}, leader)
blockBytes, err := hex.DecodeString("f90264f9025fa00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a02b418211410ee3e75b32abd925bbeba215172afa509d65c1953d4b4e505a4a2aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808080a000000000000000000000000000000000000000000000000000000000000000008800000000000000008400000001b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008080a00000000000000000000000000000000000000000000000000000000000000000c0c0")
blockBytes, err := hex.DecodeString("f90265f90260a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a02b418211410ee3e75b32abd925bbeba215172afa509d65c1953d4b4e505a4a2aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808080a000000000000000000000000000000000000000000000000000000000000000008800000000000000008400000000b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080a00000000000000000000000000000000000000000000000000000000000000000c0c0")
consensusLeader.block = blockBytes
hashBytes, err := hex.DecodeString("26d7cdbbaf6cedcaf946ad1e8c0bc2567e17418ce63026db4160a7cc32d9e488")
hashBytes, err := hex.DecodeString("4bb43b0f1eaf617808b781c2402147e187e9e1ce90fc713d4867646379d3cdb0")
copy(consensusLeader.blockHash[:], hashBytes[:])
@ -98,9 +98,9 @@ func TestProcessMessageValidatorPrepared(test *testing.T) {
test.Fatalf("newhost failure: %v", err)
}
consensusLeader := New(host, "0", []p2p.Peer{validator1, validator2, validator3}, leader)
blockBytes, err := hex.DecodeString("f90264f9025fa00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a02b418211410ee3e75b32abd925bbeba215172afa509d65c1953d4b4e505a4a2aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808080a000000000000000000000000000000000000000000000000000000000000000008800000000000000008400000001b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008080a00000000000000000000000000000000000000000000000000000000000000000c0c0")
blockBytes, err := hex.DecodeString("f90265f90260a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a02b418211410ee3e75b32abd925bbeba215172afa509d65c1953d4b4e505a4a2aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808080a000000000000000000000000000000000000000000000000000000000000000008800000000000000008400000000b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080a00000000000000000000000000000000000000000000000000000000000000000c0c0")
consensusLeader.block = blockBytes
hashBytes, err := hex.DecodeString("26d7cdbbaf6cedcaf946ad1e8c0bc2567e17418ce63026db4160a7cc32d9e488")
hashBytes, err := hex.DecodeString("4bb43b0f1eaf617808b781c2402147e187e9e1ce90fc713d4867646379d3cdb0")
copy(consensusLeader.blockHash[:], hashBytes[:])
@ -158,9 +158,9 @@ func TestProcessMessageValidatorCommitted(test *testing.T) {
test.Fatalf("newhost failure: %v", err)
}
consensusLeader := New(host, "0", []p2p.Peer{validator1, validator2, validator3}, leader)
blockBytes, err := hex.DecodeString("f90264f9025fa00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a02b418211410ee3e75b32abd925bbeba215172afa509d65c1953d4b4e505a4a2aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808080a000000000000000000000000000000000000000000000000000000000000000008800000000000000008400000001b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008080a00000000000000000000000000000000000000000000000000000000000000000c0c0")
blockBytes, err := hex.DecodeString("f90265f90260a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a02b418211410ee3e75b32abd925bbeba215172afa509d65c1953d4b4e505a4a2aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808080a000000000000000000000000000000000000000000000000000000000000000008800000000000000008400000000b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080a00000000000000000000000000000000000000000000000000000000000000000c0c0")
consensusLeader.block = blockBytes
hashBytes, err := hex.DecodeString("26d7cdbbaf6cedcaf946ad1e8c0bc2567e17418ce63026db4160a7cc32d9e488")
hashBytes, err := hex.DecodeString("4bb43b0f1eaf617808b781c2402147e187e9e1ce90fc713d4867646379d3cdb0")
copy(consensusLeader.blockHash[:], hashBytes[:])

@ -1693,6 +1693,15 @@ func (bc *BlockChain) GetRandSeedByNumber(number uint64) uint32 {
return header.RandSeed
}
// GetRandPreimageByNumber retrieves the randomness preimage given the block number, return 0 if not exist
func (bc *BlockChain) GetRandPreimageByNumber(number uint64) uint32 {
header := bc.GetHeaderByNumber(number)
if header == nil {
return 0
}
return header.RandPreimage
}
// GetNewShardState will calculate (if not exist) and get the new shard state for epoch block or nil if block is not epoch block
// epoch block is where the new shard state stored
func (bc *BlockChain) GetNewShardState(block *types.Block) types.ShardState {

@ -98,7 +98,8 @@ type Header struct {
CommitSignature [48]byte `json:"signature" gencodec:"required"`
CommitBitmap []byte `json:"bitmap" gencodec:"required"` // Contains which validator signed
RandSeed uint32 `json:"randomSeed"`
RandPreimage uint32 `json:"randPreimage"`
RandSeed uint32 `json:"randSeed"`
ShardStateHash common.Hash `json:"shardStateRoot"`
}
@ -453,7 +454,12 @@ func Number(b1, b2 *Block) bool {
// AddRandSeed add random seed into block header
func (b *Block) AddRandSeed(randSeed uint32) {
b.header.RandSeed = uint32(randSeed)
b.header.RandSeed = randSeed
}
// AddRandPreimage add randomness preimage into block header
func (b *Block) AddRandPreimage(pRnd uint32) {
b.header.RandPreimage = pRnd
}
// AddShardStateHash add shardStateHash into block header

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/ecdsa"
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"os"
@ -296,6 +297,9 @@ func New(host p2p.Host, consensus *bft.Consensus, db ethdb.Database) *Node {
if node.Role == BeaconLeader || node.Role == BeaconValidator {
node.CurrentStakes = make(map[common.Address]int64)
}
bytes, _ := rlp.EncodeToBytes(chain.GetBlockByNumber(0))
utils.GetLogInstance().Debug("TESTTEST", "block", hex.EncodeToString(bytes))
utils.GetLogInstance().Debug("Received", "blockHash", chain.GetBlockByNumber(0).Hash().Hex())
node.Consensus.ConsensusBlock = make(chan *bft.BFTBlockInfo)
node.Consensus.VerifiedNewBlock = make(chan *types.Block)
}

@ -246,6 +246,9 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) bool {
return false
}
// TODO: verify the vrf randomness
_ = newBlock.Header().RandPreimage
err = node.blockchain.ValidateNewShardState(newBlock)
if err != nil {
utils.GetLogInstance().Debug("Failed to verify new sharding state", "err", err)

Loading…
Cancel
Save