From 8c8675b25e958cc22859ecae28c89e9b677d2ff1 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Fri, 9 Jul 2021 11:28:30 -0700 Subject: [PATCH] Add vrf and proof in header and block rpc (#3820) * Add vrf and proof in header and block rpc * Add vrf and proof to rpc v2 and eth rpc --- rpc/eth/types.go | 11 +++++++++++ rpc/types.go | 11 +++++++++++ rpc/v1/types.go | 24 ++++++++++++++++++++++++ rpc/v2/types.go | 24 ++++++++++++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/rpc/eth/types.go b/rpc/eth/types.go index 3dc68d21c..e7b9a489f 100644 --- a/rpc/eth/types.go +++ b/rpc/eth/types.go @@ -29,6 +29,8 @@ type Block struct { Size hexutil.Uint64 `json:"size"` GasLimit hexutil.Uint64 `json:"gasLimit"` GasUsed hexutil.Uint64 `json:"gasUsed"` + VRF common.Hash `json:"vrf"` + VRFProof hexutil.Bytes `json:"vrfProof"` Timestamp hexutil.Uint64 `json:"timestamp"` TransactionsRoot common.Hash `json:"transactionsRoot"` ReceiptsRoot common.Hash `json:"receiptsRoot"` @@ -173,6 +175,13 @@ func NewBlock(b *types.Block, blockArgs *rpc_common.BlockArgs, leaderAddress str func newBlock(b *types.Block, leader common.Address) *Block { head := b.Header() + vrfAndProof := head.Vrf() + vrf := common.Hash{} + vrfProof := []byte{} + if len(vrfAndProof) == 32+96 { + copy(vrf[:], vrfAndProof[:32]) + vrfProof = vrfAndProof[32:] + } return &Block{ Number: (*hexutil.Big)(head.Number()), Hash: b.Hash(), @@ -188,6 +197,8 @@ func newBlock(b *types.Block, leader common.Address) *Block { Size: hexutil.Uint64(b.Size()), GasLimit: hexutil.Uint64(head.GasLimit()), GasUsed: hexutil.Uint64(head.GasUsed()), + VRF: vrf, + VRFProof: vrfProof, Timestamp: hexutil.Uint64(head.Time().Uint64()), TransactionsRoot: head.TxHash(), ReceiptsRoot: head.ReceiptHash(), diff --git a/rpc/types.go b/rpc/types.go index ad82ab550..e8e7c43a8 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -214,6 +214,8 @@ type HeaderInformation struct { UnixTime uint64 `json:"unixtime"` LastCommitSig string `json:"lastCommitSig"` LastCommitBitmap string `json:"lastCommitBitmap"` + VRF string `json:"vrf"` + VRFProof string `json:"vrfProof"` CrossLinks *types.CrossLinks `json:"crossLinks,omitempty"` } @@ -223,6 +225,13 @@ func NewHeaderInformation(header *block.Header, leader string) *HeaderInformatio return nil } + vrfAndProof := header.Vrf() + vrf := common.Hash{} + vrfProof := []byte{} + if len(vrfAndProof) == 32+96 { + copy(vrf[:], vrfAndProof[:32]) + vrfProof = vrfAndProof[32:] + } result := &HeaderInformation{ BlockHash: header.Hash(), BlockNumber: header.Number().Uint64(), @@ -233,6 +242,8 @@ func NewHeaderInformation(header *block.Header, leader string) *HeaderInformatio UnixTime: header.Time().Uint64(), Timestamp: time.Unix(header.Time().Int64(), 0).UTC().String(), LastCommitBitmap: hex.EncodeToString(header.LastCommitBitmap()), + VRF: hex.EncodeToString(vrf[:]), + VRFProof: hex.EncodeToString(vrfProof), } sig := header.LastCommitSignature() diff --git a/rpc/v1/types.go b/rpc/v1/types.go index dd1734e90..e143cf657 100644 --- a/rpc/v1/types.go +++ b/rpc/v1/types.go @@ -33,6 +33,8 @@ type BlockWithTxHash struct { Size hexutil.Uint64 `json:"size"` GasLimit hexutil.Uint64 `json:"gasLimit"` GasUsed hexutil.Uint64 `json:"gasUsed"` + VRF common.Hash `json:"vrf"` + VRFProof hexutil.Bytes `json:"vrfProof"` Timestamp hexutil.Uint64 `json:"timestamp"` TransactionsRoot common.Hash `json:"transactionsRoot"` ReceiptsRoot common.Hash `json:"receiptsRoot"` @@ -61,6 +63,8 @@ type BlockWithFullTx struct { Size hexutil.Uint64 `json:"size"` GasLimit hexutil.Uint64 `json:"gasLimit"` GasUsed hexutil.Uint64 `json:"gasUsed"` + VRF common.Hash `json:"vrf"` + VRFProof hexutil.Bytes `json:"vrfProof"` Timestamp hexutil.Uint64 `json:"timestamp"` TransactionsRoot common.Hash `json:"transactionsRoot"` ReceiptsRoot common.Hash `json:"receiptsRoot"` @@ -588,6 +592,14 @@ func NewBlockWithTxHash( } head := b.Header() + + vrfAndProof := head.Vrf() + vrf := common.Hash{} + vrfProof := []byte{} + if len(vrfAndProof) == 32+96 { + copy(vrf[:], vrfAndProof[:32]) + vrfProof = vrfAndProof[32:] + } blk := &BlockWithTxHash{ Number: (*hexutil.Big)(head.Number()), ViewID: (*hexutil.Big)(head.ViewID()), @@ -604,6 +616,8 @@ func NewBlockWithTxHash( Size: hexutil.Uint64(b.Size()), GasLimit: hexutil.Uint64(head.GasLimit()), GasUsed: hexutil.Uint64(head.GasUsed()), + VRF: vrf, + VRFProof: vrfProof, Timestamp: hexutil.Uint64(head.Time().Uint64()), TransactionsRoot: head.TxHash(), ReceiptsRoot: head.ReceiptHash(), @@ -639,6 +653,14 @@ func NewBlockWithFullTx( } head := b.Header() + + vrfAndProof := head.Vrf() + vrf := common.Hash{} + vrfProof := []byte{} + if len(vrfAndProof) == 32+96 { + copy(vrf[:], vrfAndProof[:32]) + vrfProof = vrfAndProof[32:] + } blk := &BlockWithFullTx{ Number: (*hexutil.Big)(head.Number()), ViewID: (*hexutil.Big)(head.ViewID()), @@ -655,6 +677,8 @@ func NewBlockWithFullTx( Size: hexutil.Uint64(b.Size()), GasLimit: hexutil.Uint64(head.GasLimit()), GasUsed: hexutil.Uint64(head.GasUsed()), + VRF: vrf, + VRFProof: vrfProof, Timestamp: hexutil.Uint64(head.Time().Uint64()), TransactionsRoot: head.TxHash(), ReceiptsRoot: head.ReceiptHash(), diff --git a/rpc/v2/types.go b/rpc/v2/types.go index bd3beb032..a9eb31633 100644 --- a/rpc/v2/types.go +++ b/rpc/v2/types.go @@ -35,6 +35,8 @@ type BlockWithTxHash struct { Size uint64 `json:"size"` GasLimit uint64 `json:"gasLimit"` GasUsed uint64 `json:"gasUsed"` + VRF common.Hash `json:"vrf"` + VRFProof hexutil.Bytes `json:"vrfProof"` Timestamp *big.Int `json:"timestamp"` TransactionsRoot common.Hash `json:"transactionsRoot"` ReceiptsRoot common.Hash `json:"receiptsRoot"` @@ -63,6 +65,8 @@ type BlockWithFullTx struct { Size uint64 `json:"size"` GasLimit uint64 `json:"gasLimit"` GasUsed uint64 `json:"gasUsed"` + VRF common.Hash `json:"vrf"` + VRFProof hexutil.Bytes `json:"vrfProof"` Timestamp *big.Int `json:"timestamp"` TransactionsRoot common.Hash `json:"transactionsRoot"` ReceiptsRoot common.Hash `json:"receiptsRoot"` @@ -604,6 +608,14 @@ func NewBlockWithTxHash( } head := b.Header() + + vrfAndProof := head.Vrf() + vrf := common.Hash{} + vrfProof := []byte{} + if len(vrfAndProof) == 32+96 { + copy(vrf[:], vrfAndProof[:32]) + vrfProof = vrfAndProof[32:] + } blk := &BlockWithTxHash{ Number: head.Number(), ViewID: head.ViewID(), @@ -620,6 +632,8 @@ func NewBlockWithTxHash( Size: uint64(b.Size()), GasLimit: head.GasLimit(), GasUsed: head.GasUsed(), + VRF: vrf, + VRFProof: vrfProof, Timestamp: head.Time(), TransactionsRoot: head.TxHash(), ReceiptsRoot: head.ReceiptHash(), @@ -655,6 +669,14 @@ func NewBlockWithFullTx( } head := b.Header() + + vrfAndProof := head.Vrf() + vrf := common.Hash{} + vrfProof := []byte{} + if len(vrfAndProof) == 32+96 { + copy(vrf[:], vrfAndProof[:32]) + vrfProof = vrfAndProof[32:] + } blk := &BlockWithFullTx{ Number: head.Number(), ViewID: head.ViewID(), @@ -671,6 +693,8 @@ func NewBlockWithFullTx( Size: uint64(b.Size()), GasLimit: head.GasLimit(), GasUsed: head.GasUsed(), + VRF: vrf, + VRFProof: vrfProof, Timestamp: head.Time(), TransactionsRoot: head.TxHash(), ReceiptsRoot: head.ReceiptHash(),