Skip to content

Commit

Permalink
change Proposal type to have DAHeader instead of BlockId
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Mar 10, 2021
1 parent 591d92b commit a7467c7
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 184 deletions.
2 changes: 1 addition & 1 deletion proto/tendermint/types/canonical.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ message CanonicalProposal {
sfixed64 height = 2; // canonicalization requires fixed size encoding here
sfixed64 round = 3; // canonicalization requires fixed size encoding here
int64 pol_round = 4 [(gogoproto.customname) = "POLRound"];
CanonicalBlockID block_id = 5 [(gogoproto.customname) = "BlockID"];
CanonicalDAHeader da_header = 5 [(gogoproto.customname) = "DAHeader"];
google.protobuf.Timestamp timestamp = 6 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
string chain_id = 7 [(gogoproto.customname) = "ChainID"];
}
Expand Down
255 changes: 128 additions & 127 deletions proto/tendermint/types/types.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion proto/tendermint/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ message Proposal {
int64 height = 2;
int32 round = 3;
int32 pol_round = 4;
BlockID block_id = 5 [(gogoproto.customname) = "BlockID", (gogoproto.nullable) = false];
DataAvailabilityHeader da_header = 5 [(gogoproto.customname) = "DAHeader", (gogoproto.nullable) = false];
google.protobuf.Timestamp timestamp = 6
[(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
bytes signature = 7;
Expand Down
45 changes: 29 additions & 16 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -64,14 +65,6 @@ type DataAvailabilityHeader struct {

type NmtRoots []namespace.IntervalDigest

func (roots NmtRoots) Bytes() [][]byte {
res := make([][]byte, len(roots))
for i := 0; i < len(roots); i++ {
res[i] = roots[i].Bytes()
}
return res
}

func NmtRootsFromBytes(in [][]byte) NmtRoots {
roots := make([]namespace.IntervalDigest, len(in))
for i := 0; i < len(in); i++ {
Expand All @@ -80,6 +73,22 @@ func NmtRootsFromBytes(in [][]byte) NmtRoots {
return roots
}

func (roots NmtRoots) String() string {
sb := new(strings.Builder)
for _, rs := range roots {
sb.WriteString( + "\n")
}
return sb.String()
}

func (roots NmtRoots) Bytes() [][]byte {
res := make([][]byte, len(roots))
for i := 0; i < len(roots); i++ {
res[i] = roots[i].Bytes()
}
return res
}

// Hash computes the root of the row and column roots
func (dah *DataAvailabilityHeader) Hash() []byte {
if dah == nil {
Expand All @@ -99,17 +108,25 @@ func (dah *DataAvailabilityHeader) Hash() []byte {
return merkle.HashFromByteSlices(slices)
}

func (dah *DataAvailabilityHeader) ToProto() (*tmproto.DataAvailabilityHeader, error) {
func (dah *DataAvailabilityHeader) String() string {
return fmt.Sprintf(
"DAHeader{Rows: %s, Colomns: %s}",
dah.RowsRoots,
dah.ColumnRoots,
)
}

func (dah *DataAvailabilityHeader) ToProto() *tmproto.DataAvailabilityHeader {
if dah == nil {
return nil, errors.New("nil DataAvailabilityHeader")
return &tmproto.DataAvailabilityHeader{}
}

dahp := new(tmproto.DataAvailabilityHeader)

dahp.RowRoots = dah.RowsRoots.Bytes()
dahp.ColumnRoots = dah.ColumnRoots.Bytes()

return dahp, nil
return dahp
}

func DataAvailabilityHeaderFromProto(dahp *tmproto.DataAvailabilityHeader) (*DataAvailabilityHeader, error) {
Expand Down Expand Up @@ -374,11 +391,7 @@ func (b *Block) ToProto() (*tmproto.Block, error) {
return nil, err
}
pb.Data.Evidence = *protoEvidence
dah, err := b.DataAvailabilityHeader.ToProto()
if err != nil {
return nil, err
}
pb.DataAvailabilityHeader = dah
pb.DataAvailabilityHeader = b.DataAvailabilityHeader.ToProto()

return pb, nil
}
Expand Down
7 changes: 7 additions & 0 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ func TestBlockString(t *testing.T) {
assert.NotEqual(t, "nil-Block", block.StringShort())
}

func makeDAHeaderRandom() DataAvailabilityHeader {
return DataAvailabilityHeader{
RowsRoots: NmtRootsFromBytes([][]byte{tmrand.Bytes(2*NamespaceSize+tmhash.Size)}),
ColumnRoots: NmtRootsFromBytes([][]byte{tmrand.Bytes(2*NamespaceSize+tmhash.Size)}),
}
}

func makeBlockIDRandom() BlockID {
var (
blockHash = make([]byte, tmhash.Size)
Expand Down
2 changes: 1 addition & 1 deletion types/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func CanonicalizeProposal(chainID string, proposal *tmproto.Proposal) tmproto.Ca
Height: proposal.Height, // encoded as sfixed64
Round: int64(proposal.Round), // encoded as sfixed64
POLRound: int64(proposal.PolRound),
BlockID: CanonicalizeBlockID(proposal.BlockID),
// BlockID: CanonicalizeBlockID(proposal.BlockID),
Timestamp: proposal.Timestamp,
ChainID: chainID,
}
Expand Down
37 changes: 16 additions & 21 deletions types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ var (
// If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound.
type Proposal struct {
Type tmproto.SignedMsgType
Height int64 `json:"height"`
Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds
POLRound int32 `json:"pol_round"` // -1 if null.
BlockID BlockID `json:"block_id"`
Timestamp time.Time `json:"timestamp"`
Signature []byte `json:"signature"`
Height int64 `json:"height"`
Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds
POLRound int32 `json:"pol_round"` // -1 if null.
DAHeader DataAvailabilityHeader `json:"block_id"`
Timestamp time.Time `json:"timestamp"`
Signature []byte `json:"signature"`
}

// NewProposal returns a new Proposal.
// If there is no POLRound, polRound should be -1.
func NewProposal(height int64, round int32, polRound int32, blockID BlockID) *Proposal {
func NewProposal(height int64, round int32, polRound int32, daH DataAvailabilityHeader) *Proposal {
return &Proposal{
Type: tmproto.ProposalType,
Height: height,
Round: round,
BlockID: blockID,
DAHeader: daH,
POLRound: polRound,
Timestamp: tmtime.Now(),
}
Expand All @@ -59,15 +59,10 @@ func (p *Proposal) ValidateBasic() error {
if p.POLRound < -1 {
return errors.New("negative POLRound (exception: -1)")
}
if err := p.BlockID.ValidateBasic(); err != nil {
return fmt.Errorf("wrong BlockID: %v", err)
}
// ValidateBasic above would pass even if the BlockID was empty:
if !p.BlockID.IsComplete() {
return fmt.Errorf("expected a complete, non-empty BlockID, got: %v", p.BlockID)
}

// NOTE: Timestamp validation is subtle and handled elsewhere.
// NOTE:
// * Timestamp validation is subtle and handled elsewhere.
// * DAHeader is validated within deserialization

if len(p.Signature) == 0 {
return errors.New("signature is missing")
Expand All @@ -90,10 +85,10 @@ func (p *Proposal) ValidateBasic() error {
//
// See BlockID#String.
func (p *Proposal) String() string {
return fmt.Sprintf("Proposal{%v/%v (%v, %v) %X @ %s}",
return fmt.Sprintf("Proposal{%v/%v, %s, %v %X @ %s}",
p.Height,
p.Round,
p.BlockID,
p.DAHeader,
p.POLRound,
tmbytes.Fingerprint(p.Signature),
CanonicalTime(p.Timestamp))
Expand Down Expand Up @@ -124,7 +119,7 @@ func (p *Proposal) ToProto() *tmproto.Proposal {
}
pb := new(tmproto.Proposal)

pb.BlockID = p.BlockID.ToProto()
pb.DAHeader = *p.DAHeader.ToProto()
pb.Type = p.Type
pb.Height = p.Height
pb.Round = p.Round
Expand All @@ -144,12 +139,12 @@ func ProposalFromProto(pp *tmproto.Proposal) (*Proposal, error) {

p := new(Proposal)

blockID, err := BlockIDFromProto(&pp.BlockID)
dah, err := DataAvailabilityHeaderFromProto(&pp.DAHeader)
if err != nil {
return nil, err
}

p.BlockID = *blockID
p.DAHeader = *dah
p.Type = pp.Type
p.Height = pp.Height
p.Round = pp.Round
Expand Down
25 changes: 8 additions & 17 deletions types/proposal_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package types

import (
"math"
"testing"
"time"

"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lazyledger/lazyledger-core/crypto/tmhash"
"github.com/lazyledger/lazyledger-core/libs/protoio"
tmrand "github.com/lazyledger/lazyledger-core/libs/rand"
tmproto "github.com/lazyledger/lazyledger-core/proto/tendermint/types"
)

Expand All @@ -28,8 +25,9 @@ func init() {
testProposal = &Proposal{
Height: 12345,
Round: 23456,
BlockID: BlockID{Hash: []byte("--June_15_2020_amino_was_removed"),
PartSetHeader: PartSetHeader{Total: 111, Hash: []byte("--June_15_2020_amino_was_removed")}},
DAHeader: DataAvailabilityHeader{
RowsRoots: NmtRootsFromBytes([][]byte{[]byte("--June_15_2020_amino_was_removed")}),
},
POLRound: -1,
Timestamp: stamp,
}
Expand Down Expand Up @@ -59,9 +57,7 @@ func TestProposalVerifySignature(t *testing.T) {
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)

prop := NewProposal(
4, 2, 2,
BlockID{tmrand.Bytes(tmhash.Size), PartSetHeader{777, tmrand.Bytes(tmhash.Size)}})
prop := NewProposal(4, 2, 2, makeDAHeaderRandom())
p := prop.ToProto()
signBytes := ProposalSignBytes("test_chain_id", p)

Expand Down Expand Up @@ -135,24 +131,19 @@ func TestProposalValidateBasic(t *testing.T) {
{"Invalid Height", func(p *Proposal) { p.Height = -1 }, true},
{"Invalid Round", func(p *Proposal) { p.Round = -1 }, true},
{"Invalid POLRound", func(p *Proposal) { p.POLRound = -2 }, true},
{"Invalid BlockId", func(p *Proposal) {
p.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
}, true},
{"Invalid Signature", func(p *Proposal) {
p.Signature = make([]byte, 0)
}, true},
{"Too big Signature", func(p *Proposal) {
p.Signature = make([]byte, MaxSignatureSize+1)
}, true},
}
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
dah := makeDAHeaderRandom()

for _, tc := range testCases {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
prop := NewProposal(
4, 2, 2,
blockID)
prop := NewProposal(4, 2, 2, dah)
p := prop.ToProto()
err := privVal.SignProposal("test_chain_id", p)
prop.Signature = p.Signature
Expand All @@ -164,9 +155,9 @@ func TestProposalValidateBasic(t *testing.T) {
}

func TestProposalProtoBuf(t *testing.T) {
proposal := NewProposal(1, 2, 3, makeBlockID([]byte("hash"), 2, []byte("part_set_hash")))
proposal := NewProposal(1, 2, 3, makeDAHeaderRandom())
proposal.Signature = []byte("sig")
proposal2 := NewProposal(1, 2, 3, BlockID{})
proposal2 := NewProposal(1, 2, 3, DataAvailabilityHeader{})

testCases := []struct {
msg string
Expand Down

0 comments on commit a7467c7

Please sign in to comment.