From 84318fe7dcba7c944ddfc4ef5353393a30ab4cb6 Mon Sep 17 00:00:00 2001 From: Illia Malachyn Date: Tue, 15 Oct 2024 15:32:46 +0300 Subject: [PATCH 1/3] align block header with protobuf schema --- access/grpc/convert/convert.go | 127 +++++++++++++++++++++++++++++---- block.go | 34 +++++++-- test/entities.go | 33 +++++++-- 3 files changed, 172 insertions(+), 22 deletions(-) diff --git a/access/grpc/convert/convert.go b/access/grpc/convert/convert.go index 93a89b841..93f90d9fe 100644 --- a/access/grpc/convert/convert.go +++ b/access/grpc/convert/convert.go @@ -128,8 +128,11 @@ func MessageToAccountKeys(m []*entities.AccountKey) ([]flow.AccountKey, error) { } func BlockToMessage(b flow.Block) (*entities.Block, error) { - t := timestamppb.New(b.BlockHeader.Timestamp) + header, err := BlockHeaderToMessage(b.BlockHeader) + if err != nil { + return nil, err + } return &entities.Block{ Id: b.BlockHeader.ID.Bytes(), @@ -138,6 +141,7 @@ func BlockToMessage(b flow.Block) (*entities.Block, error) { Timestamp: t, CollectionGuarantees: CollectionGuaranteesToMessages(b.BlockPayload.CollectionGuarantees), BlockSeals: BlockSealsToMessages(b.BlockPayload.Seals), + BlockHeader: header, }, nil } @@ -149,11 +153,25 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) { timestamp = m.GetTimestamp().AsTime() } + tc, err := MessageToTimeoutCertificate(m.BlockHeader.GetLastViewTc()) + if err != nil { + return flow.Block{}, err + } + header := &flow.BlockHeader{ - ID: flow.HashToID(m.GetId()), - ParentID: flow.HashToID(m.GetParentId()), - Height: m.GetHeight(), - Timestamp: timestamp, + ID: flow.HashToID(m.GetId()), + ParentID: flow.HashToID(m.GetParentId()), + Height: m.GetHeight(), + Timestamp: timestamp, + PayloadHash: m.BlockHeader.GetPayloadHash(), + View: m.BlockHeader.GetView(), + ParentVoterSigData: m.BlockHeader.GetParentVoterSigData(), + ProposerID: flow.HashToID(m.BlockHeader.GetProposerId()), + ProposerSigData: m.BlockHeader.GetProposerSigData(), + ChainID: flow.HashToID([]byte(m.BlockHeader.GetChainId())), + ParentVoterIndices: m.BlockHeader.GetParentVoterIndices(), + LastViewTimeoutCertificate: tc, + ParentView: m.BlockHeader.GetParentView(), } guarantees, err := MessagesToCollectionGuarantees(m.GetCollectionGuarantees()) @@ -179,12 +197,25 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) { func BlockHeaderToMessage(b flow.BlockHeader) (*entities.BlockHeader, error) { t := timestamppb.New(b.Timestamp) + tc, err := TimeoutCertificateToMessage(b.LastViewTimeoutCertificate) + if err != nil { + return nil, err + } return &entities.BlockHeader{ - Id: b.ID.Bytes(), - ParentId: b.ParentID.Bytes(), - Height: b.Height, - Timestamp: t, + Id: b.ID.Bytes(), + ParentId: b.ParentID.Bytes(), + Height: b.Height, + Timestamp: t, + PayloadHash: b.PayloadHash, + View: b.View, + ParentVoterSigData: b.ParentVoterSigData, + ProposerId: b.ProposerID.Bytes(), + ProposerSigData: b.ProposerSigData, + ChainId: string(b.ChainID.Bytes()), + ParentVoterIndices: b.ParentVoterIndices, + LastViewTc: tc, + ParentView: b.ParentView, }, nil } @@ -199,11 +230,81 @@ func MessageToBlockHeader(m *entities.BlockHeader) (flow.BlockHeader, error) { timestamp = m.GetTimestamp().AsTime() } + tc, err := MessageToTimeoutCertificate(m.GetLastViewTc()) + if err != nil { + return flow.BlockHeader{}, err + } + return flow.BlockHeader{ - ID: flow.HashToID(m.GetId()), - ParentID: flow.HashToID(m.GetParentId()), - Height: m.GetHeight(), - Timestamp: timestamp, + ID: flow.HashToID(m.GetId()), + ParentID: flow.HashToID(m.GetParentId()), + Height: m.GetHeight(), + Timestamp: timestamp, + PayloadHash: m.GetPayloadHash(), + View: m.GetView(), + ParentVoterSigData: m.GetParentVoterSigData(), + ProposerID: flow.HashToID(m.GetProposerId()), + ProposerSigData: m.GetProposerSigData(), + ChainID: flow.HashToID([]byte(m.GetChainId())), + ParentVoterIndices: m.GetParentVoterIndices(), + LastViewTimeoutCertificate: tc, + ParentView: m.GetParentView(), + }, nil +} + +func MessageToTimeoutCertificate(m *entities.TimeoutCertificate) (flow.TimeoutCertificate, error) { + if m == nil { + return flow.TimeoutCertificate{}, ErrEmptyMessage + } + + qc, err := MessageToQuorumCertificate(m.GetHighestQc()) + if err != nil { + return flow.TimeoutCertificate{}, err + } + + return flow.TimeoutCertificate{ + View: m.GetView(), + HighQCViews: m.GetHighQcViews(), + HighestQC: qc, + SignerIndices: m.GetSignerIndices(), + SigData: m.GetSigData(), + }, nil +} + +func TimeoutCertificateToMessage(tc flow.TimeoutCertificate) (*entities.TimeoutCertificate, error) { + qc, err := QuorumCertificateToMessage(tc.HighestQC) + if err != nil { + return nil, err + } + + return &entities.TimeoutCertificate{ + View: tc.View, + HighQcViews: tc.HighQCViews, + HighestQc: qc, + SignerIndices: tc.SignerIndices, + SigData: tc.SigData, + }, nil +} + +func MessageToQuorumCertificate(m *entities.QuorumCertificate) (flow.QuorumCertificate, error) { + if m == nil { + return flow.QuorumCertificate{}, ErrEmptyMessage + } + + return flow.QuorumCertificate{ + View: m.GetView(), + BlockID: flow.HashToID(m.GetBlockId()), + SignerIndices: m.GetSignerIndices(), + SigData: m.GetSigData(), + }, nil +} + +func QuorumCertificateToMessage(qc flow.QuorumCertificate) (*entities.QuorumCertificate, error) { + return &entities.QuorumCertificate{ + View: qc.View, + BlockId: qc.BlockID.Bytes(), + SignerIndices: qc.SignerIndices, + SigData: qc.SigData, }, nil } diff --git a/block.go b/block.go index 5ee39fcc9..b14380cda 100644 --- a/block.go +++ b/block.go @@ -28,11 +28,35 @@ type Block struct { // BlockHeader is a summary of a full block. type BlockHeader struct { - ID Identifier - ParentID Identifier - Height uint64 - Timestamp time.Time - Status BlockStatus + ID Identifier + ParentID Identifier + Height uint64 + Timestamp time.Time + Status BlockStatus + PayloadHash []byte + View uint64 + ParentVoterSigData []byte + ProposerID Identifier + ProposerSigData []byte + ChainID Identifier + ParentVoterIndices []byte + LastViewTimeoutCertificate TimeoutCertificate + ParentView uint64 +} + +type TimeoutCertificate struct { + View uint64 + HighQCViews []uint64 + HighestQC QuorumCertificate + SignerIndices []byte + SigData []byte +} + +type QuorumCertificate struct { + View uint64 + BlockID Identifier + SignerIndices []byte + SigData []byte } // BlockStatus represents the status of a block. diff --git a/test/entities.go b/test/entities.go index b9f607cf3..644e4241b 100644 --- a/test/entities.go +++ b/test/entities.go @@ -180,11 +180,36 @@ func BlockHeaderGenerator() *BlockHeaders { func (g *BlockHeaders) New() flow.BlockHeader { defer func() { g.count++ }() + qc := flow.QuorumCertificate{ + View: 42, + BlockID: g.ids.New(), + SignerIndices: []byte("dummy"), + SigData: []byte("dummy"), + } + + tc := flow.TimeoutCertificate{ + View: 42, + HighQCViews: []uint64{42}, + HighestQC: qc, + SignerIndices: []byte("dummy"), + SigData: []byte("dummy"), + } + return flow.BlockHeader{ - ID: g.ids.New(), - ParentID: g.ids.New(), - Height: uint64(g.count), - Timestamp: g.startTime.Add(time.Hour * time.Duration(g.count)), + ID: g.ids.New(), + ParentID: g.ids.New(), + Height: uint64(g.count), + Timestamp: g.startTime.Add(time.Hour * time.Duration(g.count)), + Status: flow.BlockStatusUnknown, + PayloadHash: []byte("dummy"), + View: 42, + ParentVoterSigData: []byte("dummy"), + ProposerID: g.ids.New(), + ProposerSigData: []byte("dummy"), + ChainID: g.ids.New(), + ParentVoterIndices: []byte("dummy"), + LastViewTimeoutCertificate: tc, + ParentView: 42, } } From a22835d7c929110a8336095f7ae6c41482d758cb Mon Sep 17 00:00:00 2001 From: Illia Malachyn Date: Thu, 17 Oct 2024 14:48:10 +0300 Subject: [PATCH 2/3] add random bytes generator --- test/entities.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/test/entities.go b/test/entities.go index 644e4241b..6d6f110aa 100644 --- a/test/entities.go +++ b/test/entities.go @@ -19,6 +19,7 @@ package test import ( + "crypto/rand" "errors" "fmt" "strconv" @@ -165,6 +166,7 @@ type BlockHeaders struct { count int ids *Identifiers startTime time.Time + bytesGen *BytesGenerator } func BlockHeaderGenerator() *BlockHeaders { @@ -174,6 +176,7 @@ func BlockHeaderGenerator() *BlockHeaders { count: 1, ids: IdentifierGenerator(), startTime: startTime.UTC(), + bytesGen: NewBytesGenerator(), } } @@ -183,16 +186,16 @@ func (g *BlockHeaders) New() flow.BlockHeader { qc := flow.QuorumCertificate{ View: 42, BlockID: g.ids.New(), - SignerIndices: []byte("dummy"), - SigData: []byte("dummy"), + SignerIndices: g.bytesGen.New(), + SigData: g.bytesGen.New(), } tc := flow.TimeoutCertificate{ View: 42, HighQCViews: []uint64{42}, HighestQC: qc, - SignerIndices: []byte("dummy"), - SigData: []byte("dummy"), + SignerIndices: g.bytesGen.New(), + SigData: g.bytesGen.New(), } return flow.BlockHeader{ @@ -201,13 +204,13 @@ func (g *BlockHeaders) New() flow.BlockHeader { Height: uint64(g.count), Timestamp: g.startTime.Add(time.Hour * time.Duration(g.count)), Status: flow.BlockStatusUnknown, - PayloadHash: []byte("dummy"), + PayloadHash: g.bytesGen.New(), View: 42, - ParentVoterSigData: []byte("dummy"), + ParentVoterSigData: g.bytesGen.New(), ProposerID: g.ids.New(), - ProposerSigData: []byte("dummy"), + ProposerSigData: g.bytesGen.New(), ChainID: g.ids.New(), - ParentVoterIndices: []byte("dummy"), + ParentVoterIndices: g.bytesGen.New(), LastViewTimeoutCertificate: tc, ParentView: 42, } @@ -596,3 +599,24 @@ func (g *LightTransactionResults) New() *flow.LightTransactionResult { ComputationUsed: uint64(42), } } + +type Bytes []byte + +type BytesGenerator struct { + count int +} + +func NewBytesGenerator() *BytesGenerator { + return &BytesGenerator{ + count: 64, + } +} + +func (g *BytesGenerator) New() Bytes { + randomBytes := make([]byte, g.count) + _, err := rand.Read(randomBytes) + if err != nil { + panic("failed to generate random bytes") + } + return Bytes(randomBytes) +} From d070f5e7dfa472738443a8b024619c4380d6be1b Mon Sep 17 00:00:00 2001 From: Illia Malachyn Date: Thu, 17 Oct 2024 15:03:09 +0300 Subject: [PATCH 3/3] rename bytes gen --- test/entities.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/test/entities.go b/test/entities.go index 6d6f110aa..fffee80a8 100644 --- a/test/entities.go +++ b/test/entities.go @@ -166,7 +166,7 @@ type BlockHeaders struct { count int ids *Identifiers startTime time.Time - bytesGen *BytesGenerator + bytes *Bytes } func BlockHeaderGenerator() *BlockHeaders { @@ -176,7 +176,7 @@ func BlockHeaderGenerator() *BlockHeaders { count: 1, ids: IdentifierGenerator(), startTime: startTime.UTC(), - bytesGen: NewBytesGenerator(), + bytes: BytesGenerator(), } } @@ -186,16 +186,16 @@ func (g *BlockHeaders) New() flow.BlockHeader { qc := flow.QuorumCertificate{ View: 42, BlockID: g.ids.New(), - SignerIndices: g.bytesGen.New(), - SigData: g.bytesGen.New(), + SignerIndices: g.bytes.New(), + SigData: g.bytes.New(), } tc := flow.TimeoutCertificate{ View: 42, HighQCViews: []uint64{42}, HighestQC: qc, - SignerIndices: g.bytesGen.New(), - SigData: g.bytesGen.New(), + SignerIndices: g.bytes.New(), + SigData: g.bytes.New(), } return flow.BlockHeader{ @@ -204,13 +204,13 @@ func (g *BlockHeaders) New() flow.BlockHeader { Height: uint64(g.count), Timestamp: g.startTime.Add(time.Hour * time.Duration(g.count)), Status: flow.BlockStatusUnknown, - PayloadHash: g.bytesGen.New(), + PayloadHash: g.bytes.New(), View: 42, - ParentVoterSigData: g.bytesGen.New(), + ParentVoterSigData: g.bytes.New(), ProposerID: g.ids.New(), - ProposerSigData: g.bytesGen.New(), + ProposerSigData: g.bytes.New(), ChainID: g.ids.New(), - ParentVoterIndices: g.bytesGen.New(), + ParentVoterIndices: g.bytes.New(), LastViewTimeoutCertificate: tc, ParentView: 42, } @@ -600,23 +600,21 @@ func (g *LightTransactionResults) New() *flow.LightTransactionResult { } } -type Bytes []byte - -type BytesGenerator struct { +type Bytes struct { count int } -func NewBytesGenerator() *BytesGenerator { - return &BytesGenerator{ +func BytesGenerator() *Bytes { + return &Bytes{ count: 64, } } -func (g *BytesGenerator) New() Bytes { +func (g *Bytes) New() []byte { randomBytes := make([]byte, g.count) _, err := rand.Read(randomBytes) if err != nil { panic("failed to generate random bytes") } - return Bytes(randomBytes) + return randomBytes }