Skip to content

Commit 46df9ea

Browse files
committed
fix serlization
1 parent 2d9cf4d commit 46df9ea

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

types/serialization.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ func (h *Header) ToProto() *pb.Header {
162162
ChainId: h.BaseHeader.ChainID,
163163
ValidatorHash: h.ValidatorHash,
164164
}
165+
if unknown := encodeLegacyUnknownFields(h.Legacy); len(unknown) > 0 {
166+
pHeader.ProtoReflect().SetUnknown(unknown)
167+
}
165168
return pHeader
166169
}
167170

@@ -509,6 +512,28 @@ func decodeLegacyHeaderFields(pHeader *pb.Header) (*LegacyHeaderFields, error) {
509512
return &legacy, nil
510513
}
511514

515+
func encodeLegacyUnknownFields(legacy *LegacyHeaderFields) []byte {
516+
if legacy == nil || legacy.IsZero() {
517+
return nil
518+
}
519+
520+
var payload []byte
521+
522+
if len(legacy.LastCommitHash) > 0 {
523+
payload = appendBytesField(payload, legacyLastCommitHashField, legacy.LastCommitHash)
524+
}
525+
526+
if len(legacy.ConsensusHash) > 0 {
527+
payload = appendBytesField(payload, legacyConsensusHashField, legacy.ConsensusHash)
528+
}
529+
530+
if len(legacy.LastResultsHash) > 0 {
531+
payload = appendBytesField(payload, legacyLastResultsHashField, legacy.LastResultsHash)
532+
}
533+
534+
return payload
535+
}
536+
512537
func appendBytesField(buf []byte, number protowire.Number, value []byte) []byte {
513538
buf = protowire.AppendTag(buf, number, protowire.BytesType)
514539
buf = protowire.AppendVarint(buf, uint64(len(value)))

types/serialization_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"bytes"
45
"crypto/rand"
56
"testing"
67
"time"
@@ -355,6 +356,53 @@ func TestHeader_HashFields_NilAndEmpty(t *testing.T) {
355356
assert.Nil(t, h2.ValidatorHash)
356357
}
357358

359+
func TestHeaderMarshalBinary_PreservesLegacyFields(t *testing.T) {
360+
t.Parallel()
361+
362+
header := &Header{
363+
Version: Version{Block: 2, App: 3},
364+
BaseHeader: BaseHeader{Height: 42, Time: 123456789, ChainID: "chain-legacy"},
365+
LastHeaderHash: []byte{
366+
0x01, 0x02, 0x03, 0x04,
367+
},
368+
DataHash: []byte{0x05, 0x06, 0x07, 0x08},
369+
AppHash: []byte{0x09, 0x0A, 0x0B, 0x0C},
370+
ProposerAddress: []byte{0x0D, 0x0E, 0x0F, 0x10},
371+
ValidatorHash: []byte{0x11, 0x12, 0x13, 0x14},
372+
Legacy: &LegacyHeaderFields{
373+
LastCommitHash: bytes.Repeat([]byte{0x21}, 32),
374+
ConsensusHash: bytes.Repeat([]byte{0x22}, 32),
375+
LastResultsHash: bytes.Repeat([]byte{0x23}, 32),
376+
},
377+
}
378+
379+
unknown := header.ToProto().ProtoReflect().GetUnknown()
380+
require.NotEmpty(t, unknown, "legacy fields should be serialized into unknown proto fields")
381+
382+
raw, err := header.MarshalBinary()
383+
require.NoError(t, err)
384+
require.NotEmpty(t, raw)
385+
386+
var decoded Header
387+
require.NoError(t, decoded.UnmarshalBinary(raw))
388+
require.NotNil(t, decoded.Legacy)
389+
require.False(t, decoded.Legacy.IsZero())
390+
391+
assert.Equal(t, header.Legacy.LastCommitHash, decoded.Legacy.LastCommitHash)
392+
assert.Equal(t, header.Legacy.ConsensusHash, decoded.Legacy.ConsensusHash)
393+
assert.Equal(t, header.Legacy.LastResultsHash, decoded.Legacy.LastResultsHash)
394+
395+
origHash := header.Hash()
396+
decodedHash := decoded.Hash()
397+
require.NotNil(t, origHash)
398+
require.NotNil(t, decodedHash)
399+
assert.Equal(t, origHash, decodedHash)
400+
401+
legacyHash, err := header.HashLegacy()
402+
require.NoError(t, err)
403+
assert.Equal(t, legacyHash, origHash)
404+
}
405+
358406
// TestProtoConversionConsistency_AllTypes checks that ToProto/FromProto are true inverses for all major types.
359407
func TestProtoConversionConsistency_AllTypes(t *testing.T) {
360408
// Header

0 commit comments

Comments
 (0)