From 1b8de07e49ae38ca6738e9fb5fdf34d4774197b1 Mon Sep 17 00:00:00 2001 From: Illia Malachyn Date: Tue, 15 Oct 2024 17:53:31 +0300 Subject: [PATCH 1/3] align BlockSeal type with protobuf schema --- access/grpc/convert/convert.go | 73 ++++++++++++++++++++++++++++++++-- block.go | 16 +++++++- test/entities.go | 14 ++++++- 3 files changed, 95 insertions(+), 8 deletions(-) diff --git a/access/grpc/convert/convert.go b/access/grpc/convert/convert.go index 93a89b841..b3d8a7c53 100644 --- a/access/grpc/convert/convert.go +++ b/access/grpc/convert/convert.go @@ -333,11 +333,66 @@ func CollectionGuaranteeToMessage(g flow.CollectionGuarantee) *entities.Collecti func BlockSealToMessage(g flow.BlockSeal) *entities.BlockSeal { return &entities.BlockSeal{ - BlockId: g.BlockID.Bytes(), - ExecutionReceiptId: g.ExecutionReceiptID.Bytes(), + BlockId: g.BlockID.Bytes(), + ExecutionReceiptId: g.ExecutionReceiptID.Bytes(), + ExecutionReceiptSignatures: g.ExecutionReceiptSignatures, + ResultApprovalSignatures: g.ResultApprovalSignatures, + FinalState: g.FinalState, + ResultId: g.ResultId.Bytes(), + AggregatedApprovalSigs: AggregatedSignaturesToMessage(g.AggregatedApprovalSigs), } } +func AggregatedSignaturesToMessage(s []*flow.AggregatedSignature) []*entities.AggregatedSignature { + sigs := make([]*entities.AggregatedSignature, len(s)) + for i, sig := range s { + sigs[i] = AggregatedSignatureToMessage(*sig) + } + return sigs +} + +func AggregatedSignatureToMessage(sig flow.AggregatedSignature) *entities.AggregatedSignature { + signerIds := make([][]byte, len(sig.SignerIds)) + for i, id := range sig.SignerIds { + signerIds[i] = id.Bytes() + } + + return &entities.AggregatedSignature{ + VerifierSignatures: sig.VerifierSignatures, + SignerIds: signerIds, + } +} + +func MessageToAggregatedSignatures(m []*entities.AggregatedSignature) ([]*flow.AggregatedSignature, error) { + sigs := make([]*flow.AggregatedSignature, len(m)) + for i, sig := range m { + convertedSig, err := MessageToAggregatedSignature(sig) + if err != nil { + return nil, err + } + + sigs[i] = &convertedSig + } + + return sigs, nil +} + +func MessageToAggregatedSignature(m *entities.AggregatedSignature) (flow.AggregatedSignature, error) { + if m == nil { + return flow.AggregatedSignature{}, ErrEmptyMessage + } + + ids := make([]flow.Identifier, len(m.SignerIds)) + for i, id := range m.SignerIds { + ids[i] = flow.HashToID(id) + } + + return flow.AggregatedSignature{ + VerifierSignatures: m.GetVerifierSignatures(), + SignerIds: ids, + }, nil +} + func MessageToCollectionGuarantee(m *entities.CollectionGuarantee) (flow.CollectionGuarantee, error) { if m == nil { return flow.CollectionGuarantee{}, ErrEmptyMessage @@ -353,9 +408,19 @@ func MessageToBlockSeal(m *entities.BlockSeal) (flow.BlockSeal, error) { return flow.BlockSeal{}, ErrEmptyMessage } + sigs, err := MessageToAggregatedSignatures(m.GetAggregatedApprovalSigs()) + if err != nil { + return flow.BlockSeal{}, err + } + return flow.BlockSeal{ - BlockID: flow.BytesToID(m.BlockId), - ExecutionReceiptID: flow.BytesToID(m.ExecutionReceiptId), + BlockID: flow.BytesToID(m.BlockId), + ExecutionReceiptID: flow.BytesToID(m.ExecutionReceiptId), + ExecutionReceiptSignatures: m.GetExecutionReceiptSignatures(), + ResultApprovalSignatures: m.GetResultApprovalSignatures(), + FinalState: m.GetFinalState(), + ResultId: flow.BytesToID(m.GetResultId()), + AggregatedApprovalSigs: sigs, }, nil } diff --git a/block.go b/block.go index 5ee39fcc9..4e975d8cf 100644 --- a/block.go +++ b/block.go @@ -18,7 +18,9 @@ package flow -import "time" +import ( + "time" +) // Block is a set of state mutations applied to the Flow blockchain. type Block struct { @@ -74,5 +76,15 @@ type BlockSeal struct { // The ID of the execution receipt generated by the Verifier nodes; the work of verifying a // block produces the same receipt among all verifying nodes - ExecutionReceiptID Identifier + ExecutionReceiptID Identifier + ExecutionReceiptSignatures [][]byte + ResultApprovalSignatures [][]byte + FinalState []byte + ResultId Identifier + AggregatedApprovalSigs []*AggregatedSignature +} + +type AggregatedSignature struct { + VerifierSignatures [][]byte + SignerIds []Identifier } diff --git a/test/entities.go b/test/entities.go index b9f607cf3..0c2aa7c87 100644 --- a/test/entities.go +++ b/test/entities.go @@ -250,9 +250,19 @@ func BlockSealGenerator() *BlockSeals { } func (g *BlockSeals) New() *flow.BlockSeal { + sigs := []*flow.AggregatedSignature{{ + VerifierSignatures: [][]byte{[]byte("dummy")}, + SignerIds: []flow.Identifier{g.ids.New()}, + }} + return &flow.BlockSeal{ - BlockID: g.ids.New(), - ExecutionReceiptID: g.ids.New(), + BlockID: g.ids.New(), + ExecutionReceiptID: g.ids.New(), + ExecutionReceiptSignatures: [][]byte{[]byte("dummy")}, + ResultApprovalSignatures: [][]byte{[]byte("dummy")}, + FinalState: []byte("dummy"), + ResultId: g.ids.New(), + AggregatedApprovalSigs: sigs, } } From e8d7248623c7c7319cae12f5a472adf3e2b02ed9 Mon Sep 17 00:00:00 2001 From: Illia Malachyn Date: Thu, 17 Oct 2024 14:54:30 +0300 Subject: [PATCH 2/3] make use of sig generator --- test/entities.go | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/test/entities.go b/test/entities.go index 0c2aa7c87..15100138d 100644 --- a/test/entities.go +++ b/test/entities.go @@ -19,6 +19,7 @@ package test import ( + "crypto/rand" "errors" "fmt" "strconv" @@ -228,7 +229,9 @@ type CollectionGuarantees struct { } type BlockSeals struct { - ids *Identifiers + ids *Identifiers + sigs *Signatures + bytes *BytesGenerator } func CollectionGuaranteeGenerator() *CollectionGuarantees { @@ -245,22 +248,24 @@ func (g *CollectionGuarantees) New() *flow.CollectionGuarantee { func BlockSealGenerator() *BlockSeals { return &BlockSeals{ - ids: IdentifierGenerator(), + ids: IdentifierGenerator(), + sigs: SignaturesGenerator(), + bytes: NewBytesGenerator(), } } func (g *BlockSeals) New() *flow.BlockSeal { sigs := []*flow.AggregatedSignature{{ - VerifierSignatures: [][]byte{[]byte("dummy")}, + VerifierSignatures: g.sigs.New(), SignerIds: []flow.Identifier{g.ids.New()}, }} return &flow.BlockSeal{ BlockID: g.ids.New(), ExecutionReceiptID: g.ids.New(), - ExecutionReceiptSignatures: [][]byte{[]byte("dummy")}, - ResultApprovalSignatures: [][]byte{[]byte("dummy")}, - FinalState: []byte("dummy"), + ExecutionReceiptSignatures: g.sigs.New(), + ResultApprovalSignatures: g.sigs.New(), + FinalState: g.bytes.New(), ResultId: g.ids.New(), AggregatedApprovalSigs: sigs, } @@ -581,3 +586,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 (b *BytesGenerator) New() Bytes { + randomBytes := make([]byte, b.count) + _, err := rand.Read(randomBytes) + if err != nil { + panic("failed to generate random bytes") + } + return Bytes(randomBytes) +} From f5edcfd145a6e6e601285697393b70ad6ed9b151 Mon Sep 17 00:00:00 2001 From: Illia Malachyn Date: Thu, 17 Oct 2024 15:04:30 +0300 Subject: [PATCH 3/3] rename bytes gen --- test/entities.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/entities.go b/test/entities.go index 15100138d..3fcdb34b0 100644 --- a/test/entities.go +++ b/test/entities.go @@ -231,7 +231,7 @@ type CollectionGuarantees struct { type BlockSeals struct { ids *Identifiers sigs *Signatures - bytes *BytesGenerator + bytes *Bytes } func CollectionGuaranteeGenerator() *CollectionGuarantees { @@ -250,7 +250,7 @@ func BlockSealGenerator() *BlockSeals { return &BlockSeals{ ids: IdentifierGenerator(), sigs: SignaturesGenerator(), - bytes: NewBytesGenerator(), + bytes: BytesGenerator(), } } @@ -587,23 +587,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 (b *BytesGenerator) New() Bytes { +func (b *Bytes) New() []byte { randomBytes := make([]byte, b.count) _, err := rand.Read(randomBytes) if err != nil { panic("failed to generate random bytes") } - return Bytes(randomBytes) + return randomBytes }