Skip to content

Commit

Permalink
Merge pull request #780 from The-K-R-O-K/illia-malachyn/766-block-sea…
Browse files Browse the repository at this point in the history
…l-response-object

align BlockSeal type with protobuf schema
  • Loading branch information
franklywatson authored Oct 21, 2024
2 parents 7d51fee + 3ae9ebc commit fb225b9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 10 deletions.
73 changes: 69 additions & 4 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,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
Expand All @@ -407,9 +462,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
}

Expand Down
16 changes: 14 additions & 2 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -74,7 +76,17 @@ 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
}

// BlockDigest holds lightweight block information which includes only block id, block height and block timestamp
Expand Down
42 changes: 38 additions & 4 deletions test/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package test

import (
"crypto/rand"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -228,7 +229,9 @@ type CollectionGuarantees struct {
}

type BlockSeals struct {
ids *Identifiers
ids *Identifiers
sigs *Signatures
bytes *Bytes
}

func CollectionGuaranteeGenerator() *CollectionGuarantees {
Expand All @@ -245,14 +248,26 @@ func (g *CollectionGuarantees) New() *flow.CollectionGuarantee {

func BlockSealGenerator() *BlockSeals {
return &BlockSeals{
ids: IdentifierGenerator(),
ids: IdentifierGenerator(),
sigs: SignaturesGenerator(),
bytes: BytesGenerator(),
}
}

func (g *BlockSeals) New() *flow.BlockSeal {
sigs := []*flow.AggregatedSignature{{
VerifierSignatures: g.sigs.New(),
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: g.sigs.New(),
ResultApprovalSignatures: g.sigs.New(),
FinalState: g.bytes.New(),
ResultId: g.ids.New(),
AggregatedApprovalSigs: sigs,
}
}

Expand Down Expand Up @@ -571,3 +586,22 @@ func (g *LightTransactionResults) New() *flow.LightTransactionResult {
ComputationUsed: uint64(42),
}
}

type Bytes struct {
count int
}

func BytesGenerator() *Bytes {
return &Bytes{
count: 64,
}
}

func (b *Bytes) New() []byte {
randomBytes := make([]byte, b.count)
_, err := rand.Read(randomBytes)
if err != nil {
panic("failed to generate random bytes")
}
return randomBytes
}

0 comments on commit fb225b9

Please sign in to comment.