Skip to content

Commit

Permalink
fix most of the conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
danwt committed Dec 20, 2024
1 parent de84eb0 commit e873050
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 196 deletions.
3 changes: 2 additions & 1 deletion block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func (e *Executor) CreateBlock(
},
LastCommit: *lastCommit,
}
block.Header.Dym = types.NewDymHeader(block.Data.ConsensusMessages)

block.Header.SetDymHeader(types.MakeDymHeader(block.Data.ConsensusMessages))
copy(block.Header.LastCommitHash[:], types.GetLastCommitHash(lastCommit, &block.Header))
copy(block.Header.DataHash[:], types.GetDataHash(block))
copy(block.Header.SequencerHash[:], state.GetProposerHash())
Expand Down
2 changes: 1 addition & 1 deletion da/celestia/celestia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func getRandomBlock(height uint64, nTxs int) *types.Block {
block := &types.Block{
Header: types.Header{
Height: height,
Dym: types.NewDymHeader(nil),
Dym: types.ConsMessagesHash(nil),
},
Data: types.Data{
Txs: make(types.Txs, nTxs),
Expand Down
2 changes: 1 addition & 1 deletion da/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func getRandomBlock(height uint64, nTxs int) *types.Block {
block := &types.Block{
Header: types.Header{
Height: height,
Dym: types.NewDymHeader(nil),
Dym: types.ConsMessagesHash(nil),
},
Data: types.Data{
Txs: make(types.Txs, nTxs),
Expand Down
9 changes: 4 additions & 5 deletions rpc/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,10 @@ func TestValidatorSetHandling(t *testing.T) {
func getRandomBlock(height uint64, nTxs int) *types.Block {
block := &types.Block{
Header: types.Header{
Height: height,
Version: types.Version{Block: testutil.BlockVersion},
ProposerAddress: getRandomBytes(20),
Dym: types.NewDymHeader(nil),
},
Height: height,
Version: types.Version{Block: testutil.BlockVersion},
ProposerAddress: getRandomBytes(20),
ConsensusMessagesHash: types.ConsMessagesHash(nil)},
Data: types.Data{
Txs: make(types.Txs, nTxs),
IntermediateStateRoots: types.IntermediateStateRoots{
Expand Down
29 changes: 14 additions & 15 deletions testutil/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,19 @@ func generateBlock(height uint64, proposerHash []byte, lastHeaderHash [32]byte)
Block: BlockVersion,
App: AppVersion,
},
Height: height,
Time: 4567,
LastHeaderHash: lastHeaderHash,
LastCommitHash: h[0],
DataHash: h[1],
ConsensusHash: h[2],
AppHash: [32]byte{},
LastResultsHash: GetEmptyLastResultsHash(),
ProposerAddress: []byte{4, 3, 2, 1},
SequencerHash: [32]byte(proposerHash),
NextSequencersHash: [32]byte(proposerHash),
ChainID: "test-chain",
Dym: types.NewDymHeader(nil),
},
Height: height,
Time: 4567,
LastHeaderHash: lastHeaderHash,
LastCommitHash: h[0],
DataHash: h[1],
ConsensusHash: h[2],
AppHash: [32]byte{},
LastResultsHash: GetEmptyLastResultsHash(),
ProposerAddress: []byte{4, 3, 2, 1},
SequencerHash: [32]byte(proposerHash),
NextSequencersHash: [32]byte(proposerHash),
ChainID: "test-chain",
ConsensusMessagesHash: types.ConsMessagesHash(nil)},
Data: types.Data{
Txs: nil,
IntermediateStateRoots: types.IntermediateStateRoots{RawRootsList: [][]byte{{0x1}}},
Expand Down Expand Up @@ -390,7 +389,7 @@ func GetRandomBlock(height uint64, nTxs int) *types.Block {
block := &types.Block{
Header: types.Header{
Height: height,
Dym: types.NewDymHeader(nil),
Dym: types.ConsMessagesHash(nil),
},
Data: types.Data{
Txs: make(types.Txs, nTxs),
Expand Down
2 changes: 1 addition & 1 deletion types/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ToABCIHeader(header *Header) tmtypes.Header {
ConsensusHash: header.ConsensusHash[:],
AppHash: header.AppHash[:],
LastResultsHash: header.LastResultsHash[:],
EvidenceHash: header.Dym.Hash(), // Overloaded, we don't need the evidence field because we don't use comet.
EvidenceHash: header.DymHash(), // Overloaded, we don't need the evidence field because we don't use comet.
ProposerAddress: header.ProposerAddress,
ChainID: header.ChainID,
}
Expand Down
34 changes: 23 additions & 11 deletions types/dym_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,39 @@ import (
_ "github.com/tendermint/tendermint/types"
)

func NewDymHeader(cons []*proto.Any) *DymHeader {
return &DymHeader{
ConsensusMessagesHash: consMessagesHash(cons),
}
// a convenience struct to make computing easier
// persisted and over the wire types use a flat representation
type DymHeader struct {
ConsensusMessagesHash [32]byte
}

func (d *DymHeader) ValidateBasic() error {
if d == nil {
return ErrInvalidDymHeaderNil
func MakeDymHeader(consMessages []*proto.Any) DymHeader {
return DymHeader{
ConsensusMessagesHash: ConsMessagesHash(consMessages),
}
return nil
}

func (d *DymHeader) Hash() cmtbytes.HexBytes {
func (h *Header) SetDymHeader(dh DymHeader) {
h.ConsensusMessagesHash = dh.ConsensusMessagesHash
}

func (h *Header) DymHash() cmtbytes.HexBytes {
// 32 bytes long
return merkle.HashFromByteSlices([][]byte{
d.ConsensusMessagesHash[:],
h.ConsensusMessagesHash[:],
// can be extended with other things if we need to later
})
}

func dymHashFr(blocks []*Block) cmtbytes.HexBytes {
// 32 bytes long
bzz := make([][]byte, len(blocks))
for i, block := range blocks {
bzz[i] = block.Header.DymHash()
}
return merkle.HashFromByteSlices(bzz)
}

func (d *DymHeader) ToProto() *pb.DymHeader {
if d == nil {
// responsibility of caller to validate
Expand All @@ -51,7 +63,7 @@ func (d *DymHeader) FromProto(o *pb.DymHeader) error {
return nil // just return error anyway to stick with pattern
}

func consMessagesHash(msgs []*proto.Any) [32]byte {
func ConsMessagesHash(msgs []*proto.Any) [32]byte {
bzz := make([][]byte, len(msgs))
for i, msg := range msgs {
var err error
Expand Down
37 changes: 17 additions & 20 deletions types/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ func (c *Commit) UnmarshalBinary(data []byte) error {
// ToProto converts Header into protobuf representation and returns it.
func (h *Header) ToProto() *pb.Header {
return &pb.Header{
Version: &pb.Version{Block: h.Version.Block, App: h.Version.App},
NamespaceId: []byte{},
Height: h.Height,
Time: h.Time,
LastHeaderHash: h.LastHeaderHash[:],
LastCommitHash: h.LastCommitHash[:],
DataHash: h.DataHash[:],
ConsensusHash: h.ConsensusHash[:],
AppHash: h.AppHash[:],
LastResultsHash: h.LastResultsHash[:],
ProposerAddress: h.ProposerAddress[:],
SequencerHash: h.SequencerHash[:],
NextSequencerHash: h.NextSequencersHash[:],
ChainId: h.ChainID,
Dym: h.Dym.ToProto(),
Version: &pb.Version{Block: h.Version.Block, App: h.Version.App},
NamespaceId: []byte{},
Height: h.Height,
Time: h.Time,
LastHeaderHash: h.LastHeaderHash[:],
LastCommitHash: h.LastCommitHash[:],
DataHash: h.DataHash[:],
ConsensusHash: h.ConsensusHash[:],
AppHash: h.AppHash[:],
LastResultsHash: h.LastResultsHash[:],
ProposerAddress: h.ProposerAddress[:],
SequencerHash: h.SequencerHash[:],
NextSequencerHash: h.NextSequencersHash[:],
ChainId: h.ChainID,
ConsensusMessagesHash: h.ConsensusMessagesHash[:],
}
}

Expand Down Expand Up @@ -137,11 +137,8 @@ func (h *Header) FromProto(other *pb.Header) error {
h.ProposerAddress = make([]byte, len(other.ProposerAddress))
copy(h.ProposerAddress, other.ProposerAddress)
}
if h.Dym != nil {
h.Dym = &DymHeader{}
if err := h.Dym.FromProto(other.Dym); err != nil {
return err
}
if !safeCopy(h.ConsensusMessagesHash[:], other.ConsensusMessagesHash) {
return errors.New("invalid length of 'SequencersHash'")
}
return nil
}
Expand Down
23 changes: 11 additions & 12 deletions types/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@ func TestBlockSerializationRoundTrip(t *testing.T) {
Block: 1,
App: 2,
},
Height: 3,
Time: 4567,
LastHeaderHash: h[0],
LastCommitHash: h[1],
DataHash: h[2],
ConsensusHash: h[3],
AppHash: h[4],
LastResultsHash: h[5],
ProposerAddress: []byte{4, 3, 2, 1},
NextSequencersHash: h[6],
Dym: types.NewDymHeader(nil),
},
Height: 3,
Time: 4567,
LastHeaderHash: h[0],
LastCommitHash: h[1],
DataHash: h[2],
ConsensusHash: h[3],
AppHash: h[4],
LastResultsHash: h[5],
ProposerAddress: []byte{4, 3, 2, 1},
NextSequencersHash: h[6],
ConsensusMessagesHash: types.ConsMessagesHash(nil)},
Data: types.Data{
Txs: nil,
IntermediateStateRoots: types.IntermediateStateRoots{RawRootsList: [][]byte{{0x1}}},
Expand Down
1 change: 1 addition & 0 deletions types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (b *Block) ValidateBasic() error {
}

func (b *Block) validateDymHeader() error {
if !bytes.Equal(b.Header.DymHash(), NewDymHeader(b.Data.ConsensusMessages).Hash()) {
if !bytes.Equal(b.Header.Dym.Hash(), NewDymHeader(b.Data.ConsensusMessages).Hash()) {
return ErrInvalidDymHeaderHash
}
Expand Down
Loading

0 comments on commit e873050

Please sign in to comment.