From 8f23720d82b866949504aaf9a356a93edd3fd220 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 8 Jun 2019 23:07:25 -0700 Subject: [PATCH 1/3] Update vrf vdf and crosslink fields --- consensus/consensus_v2.go | 4 ++-- core/blockchain.go | 14 ++++++++------ core/resharding.go | 2 +- core/types/block.go | 19 +++++++++++-------- core/types/transaction.go | 5 +++-- drand/drand_leader.go | 2 +- node/node_handler.go | 2 +- 7 files changed, 27 insertions(+), 21 deletions(-) diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index f03183643..ffbd20ae6 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -819,7 +819,7 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan // vrfBitmap.SetMask(bitmap) // // // TODO: check validity of pRnd - // newBlock.AddRandPreimage(pRnd) + // newBlock.AddVrf(pRnd) //} rnd, blockHash, err := consensus.GetNextRnd() @@ -827,7 +827,7 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan // Verify the randomness _ = blockHash utils.GetLogger().Info("Adding randomness into new block", "rnd", rnd) - newBlock.AddRandSeed(rnd) + newBlock.AddVdf([258]byte{}) // TODO(HB): add real vdf } else { utils.GetLogger().Info("Failed to get randomness", "error", err) } diff --git a/core/blockchain.go b/core/blockchain.go index d2d1a0a93..4c0a92408 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1703,22 +1703,24 @@ func (bc *BlockChain) WriteShardState( return nil } -// GetRandSeedByNumber retrieves the rand seed given the block number, return 0 if not exist -func (bc *BlockChain) GetRandSeedByNumber(number uint64) [32]byte { +// GetVdfByNumber retrieves the rand seed given the block number, return 0 if not exist +func (bc *BlockChain) GetVdfByNumber(number uint64) [32]byte { header := bc.GetHeaderByNumber(number) if header == nil { return [32]byte{} } - return header.RandSeed + result := [32]byte{} + copy(result[:], header.Vdf[:32]) + return result } -// GetRandPreimageByNumber retrieves the randomness preimage given the block number, return 0 if not exist -func (bc *BlockChain) GetRandPreimageByNumber(number uint64) [32]byte { +// GetVrfByNumber retrieves the randomness preimage given the block number, return 0 if not exist +func (bc *BlockChain) GetVrfByNumber(number uint64) [32]byte { header := bc.GetHeaderByNumber(number) if header == nil { return [32]byte{} } - return header.RandPreimage + return header.Vrf } // GetShardState returns the shard state for the given epoch, diff --git a/core/resharding.go b/core/resharding.go index 415156fe6..c0c21435e 100644 --- a/core/resharding.go +++ b/core/resharding.go @@ -166,7 +166,7 @@ func GetShardingStateFromBlockChain(bc *BlockChain, epoch *big.Int) (*ShardingSt shardState = shardState.DeepCopy() blockNumber := GetBlockNumberFromEpoch(epoch.Uint64()) - rndSeedBytes := bc.GetRandSeedByNumber(blockNumber) + rndSeedBytes := bc.GetVdfByNumber(blockNumber) rndSeed := binary.BigEndian.Uint64(rndSeedBytes[:]) return &ShardingState{epoch: epoch.Uint64(), rnd: rndSeed, shardState: shardState, numShards: len(shardState)}, nil diff --git a/core/types/block.go b/core/types/block.go index 45c62fd5f..4bb604b07 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -90,10 +90,13 @@ type Header struct { PrepareBitmap []byte `json:"prepareBitmap" gencodec:"required"` // Contains which validator signed CommitSignature [96]byte `json:"commitSignature" gencodec:"required"` CommitBitmap []byte `json:"commitBitmap" gencodec:"required"` // Contains which validator signed - RandPreimage [32]byte `json:"randPreimage"` - RandSeed [32]byte `json:"randSeed"` + Vrf [32]byte `json:"vrf"` + VrfProof [96]byte `json:"vrfProof"` + Vdf [258]byte `json:"vdf"` + VdfProof [258]byte `json:"vdfProof"` ShardStateHash common.Hash `json:"shardStateRoot"` ShardState ShardState `json:"shardState"` + CrossLinks [][]byte `json:"crossLinks"` } // field type overrides for gencodec @@ -471,14 +474,14 @@ func Number(b1, b2 *Block) bool { return b1.header.Number.Cmp(b2.header.Number) < 0 } -// AddRandSeed add random seed into block header -func (b *Block) AddRandSeed(randSeed [32]byte) { - b.header.RandSeed = randSeed +// AddVdf add vdf into block header +func (b *Block) AddVdf(vdf [258]byte) { + b.header.Vdf = vdf } -// AddRandPreimage add randomness preimage into block header -func (b *Block) AddRandPreimage(pRnd [32]byte) { - b.header.RandPreimage = pRnd +// AddVrf add vrf into block header +func (b *Block) AddVrf(vrf [32]byte) { + b.header.Vrf = vrf } // AddShardState add shardState into block header diff --git a/core/types/transaction.go b/core/types/transaction.go index 239b1ff06..bb66fbf61 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -50,7 +50,8 @@ type txdata struct { Price *big.Int `json:"gasPrice" gencodec:"required"` GasLimit uint64 `json:"gas" gencodec:"required"` ShardID uint32 `json:"shardID" gencodec:"required"` - Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation + ToShardID uint32 `json:"toShardID" rlp:"nil"` // nil means cross-shard tx + Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation Amount *big.Int `json:"value" gencodec:"required"` Payload []byte `json:"input" gencodec:"required"` @@ -235,7 +236,7 @@ func (tx *Transaction) Hash() common.Hash { } // Size returns the true RLP encoded storage size of the transaction, either by -// encoding and returning it, or returning a previsouly cached value. +// encoding and returning it, or returning a previously cached value. func (tx *Transaction) Size() common.StorageSize { if size := tx.size.Load(); size != nil { return size.(common.StorageSize) diff --git a/drand/drand_leader.go b/drand/drand_leader.go index 3db6776ac..88dfbe778 100644 --- a/drand/drand_leader.go +++ b/drand/drand_leader.go @@ -33,7 +33,7 @@ func (dRand *DRand) WaitForEpochBlock(blockChannel chan *types.Block, stopChan c if core.IsEpochLastBlock(newBlock) { dRand.init(newBlock) } - pRnd := newBlock.Header().RandPreimage + pRnd := newBlock.Header().Vrf zeros := [32]byte{} if core.IsEpochBlock(newBlock) && !bytes.Equal(pRnd[:], zeros[:]) { // The epoch block should contain the randomness preimage pRnd diff --git a/node/node_handler.go b/node/node_handler.go index 7b2823b17..6da97669c 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -262,7 +262,7 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error { } // TODO: verify the vrf randomness - _ = newBlock.Header().RandPreimage + _ = newBlock.Header().Vrf err = node.validateNewShardState(newBlock, &node.CurrentStakes) if err != nil { From 5c9bd3283486342ca1555873ffb36e53e1280771 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 8 Jun 2019 23:10:47 -0700 Subject: [PATCH 2/3] Update comment --- core/types/transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index bb66fbf61..9e8ed1651 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -50,7 +50,7 @@ type txdata struct { Price *big.Int `json:"gasPrice" gencodec:"required"` GasLimit uint64 `json:"gas" gencodec:"required"` ShardID uint32 `json:"shardID" gencodec:"required"` - ToShardID uint32 `json:"toShardID" rlp:"nil"` // nil means cross-shard tx + ToShardID uint32 `json:"toShardID" rlp:"nil"` // for cross-shard tx's destination shard ID; nil means intra-shard tx Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation Amount *big.Int `json:"value" gencodec:"required"` Payload []byte `json:"input" gencodec:"required"` From 2f3c485526cd5ac3056051de9c52a6468615f77d Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 8 Jun 2019 23:16:02 -0700 Subject: [PATCH 3/3] Revert shard size --- core/resharding.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/resharding.go b/core/resharding.go index c0c21435e..7eee1680a 100644 --- a/core/resharding.go +++ b/core/resharding.go @@ -27,7 +27,7 @@ const ( // GenesisShardNum is the number of shard at genesis GenesisShardNum = 4 // GenesisShardSize is the size of each shard at genesis - GenesisShardSize = 10 + GenesisShardSize = 100 // CuckooRate is the percentage of nodes getting reshuffled in the second step of cuckoo resharding. CuckooRate = 0.1 )