Skip to content

Commit

Permalink
extend block payload with new fields
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-malachyn committed Oct 22, 2024
1 parent a0793fd commit 54426af
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 17 deletions.
159 changes: 147 additions & 12 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,28 @@ func BlockToMessage(b flow.Block) (*entities.Block, error) {
return nil, err
}

execReceipts, err := ExecutionReceiptMetaListToMessage(b.ExecutionReceiptMetaList)
if err != nil {
return nil, fmt.Errorf("error converting execution receipts: %w", err)
}

execResults, err := ExecutionResultsToMessage(b.ExecutionResultsList)
if err != nil {
return nil, fmt.Errorf("error converting execution results: %w", err)
}

return &entities.Block{
Id: b.BlockHeader.ID.Bytes(),
ParentId: b.BlockHeader.ParentID.Bytes(),
Height: b.BlockHeader.Height,
Timestamp: t,
CollectionGuarantees: CollectionGuaranteesToMessages(b.BlockPayload.CollectionGuarantees),
BlockSeals: BlockSealsToMessages(b.BlockPayload.Seals),
BlockHeader: header,
Id: b.BlockHeader.ID.Bytes(),
ParentId: b.BlockHeader.ParentID.Bytes(),
Height: b.BlockHeader.Height,
Timestamp: t,
CollectionGuarantees: CollectionGuaranteesToMessages(b.BlockPayload.CollectionGuarantees),
BlockSeals: BlockSealsToMessages(b.BlockPayload.Seals),
Signatures: b.Signatures,
ExecutionReceiptMetaList: execReceipts,
ExecutionResultList: execResults,
BlockHeader: header,
ProtocolStateId: b.ProtocolStateID.Bytes(),
}, nil
}

Expand All @@ -193,7 +207,7 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) {

tc, err := MessageToTimeoutCertificate(m.BlockHeader.GetLastViewTc())
if err != nil {
return flow.Block{}, err
return flow.Block{}, fmt.Errorf("error converting timeout certificate: %w", err)
}

header := &flow.BlockHeader{
Expand All @@ -214,17 +228,31 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) {

guarantees, err := MessagesToCollectionGuarantees(m.GetCollectionGuarantees())
if err != nil {
return flow.Block{}, err
return flow.Block{}, fmt.Errorf("error converting collection guarantees: %w", err)
}

seals, err := MessagesToBlockSeals(m.GetBlockSeals())
if err != nil {
return flow.Block{}, err
return flow.Block{}, fmt.Errorf("error converting block seals: %w", err)
}

executionReceiptsMeta, err := MessageToExecutionReceiptMetaList(m.GetExecutionReceiptMetaList())
if err != nil {
return flow.Block{}, fmt.Errorf("error converting execution receipt meta list: %w", err)
}

executionResults, err := MessageToExecutionResults(m.GetExecutionResultList())
if err != nil {
return flow.Block{}, fmt.Errorf("error converting execution results: %w", err)
}

payload := &flow.BlockPayload{
CollectionGuarantees: guarantees,
Seals: seals,
CollectionGuarantees: guarantees,
Seals: seals,
Signatures: m.GetSignatures(),
ExecutionReceiptMetaList: executionReceiptsMeta,
ExecutionResultsList: executionResults,
ProtocolStateID: flow.HashToID(m.GetProtocolStateId()),
}

return flow.Block{
Expand All @@ -233,6 +261,54 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) {
}, nil
}

func MessageToExecutionReceiptMeta(m *entities.ExecutionReceiptMeta) (*flow.ExecutionReceiptMeta, error) {
if m == nil {
return nil, ErrEmptyMessage
}

return &flow.ExecutionReceiptMeta{
ExecutorID: flow.HashToID(m.GetExecutorId()),
ResultID: flow.HashToID(m.GetResultId()),
Spocks: m.GetSpocks(),
ExecutorSignature: m.GetExecutorSignature(),
}, nil
}

func ExecutionReceiptMetaToMessage(receipt flow.ExecutionReceiptMeta) (*entities.ExecutionReceiptMeta, error) {
return &entities.ExecutionReceiptMeta{
ExecutorId: receipt.ExecutorID.Bytes(),
ResultId: receipt.ResultID.Bytes(),
Spocks: receipt.Spocks,
ExecutorSignature: receipt.ExecutorSignature,
}, nil
}

func MessageToExecutionReceiptMetaList(m []*entities.ExecutionReceiptMeta) ([]*flow.ExecutionReceiptMeta, error) {
results := make([]*flow.ExecutionReceiptMeta, len(m))

for i, entity := range m {
executionReceiptMeta, err := MessageToExecutionReceiptMeta(entity)
if err != nil {
return nil, err
}
results[i] = executionReceiptMeta
}

return results, nil
}

func ExecutionReceiptMetaListToMessage(receipts []*flow.ExecutionReceiptMeta) ([]*entities.ExecutionReceiptMeta, error) {
results := make([]*entities.ExecutionReceiptMeta, len(receipts))
for i, receipt := range receipts {
executionReceiptMeta, err := ExecutionReceiptMetaToMessage(*receipt)
if err != nil {
return nil, err
}
results[i] = executionReceiptMeta
}
return results, nil
}

func BlockHeaderToMessage(b flow.BlockHeader) (*entities.BlockHeader, error) {
t := timestamppb.New(b.Timestamp)
tc, err := TimeoutCertificateToMessage(b.LastViewTimeoutCertificate)
Expand Down Expand Up @@ -891,6 +967,65 @@ func MessageToExecutionResult(execResult *entities.ExecutionResult) (*flow.Execu
}, nil
}

func ExecutionResultToMessage(result flow.ExecutionResult) (*entities.ExecutionResult, error) {
chunks := make([]*entities.Chunk, len(result.Chunks))
for i, chunk := range result.Chunks {
chunks[i] = &entities.Chunk{
CollectionIndex: uint32(chunk.CollectionIndex),
StartState: IdentifierToMessage(flow.Identifier(chunk.StartState)),
EventCollection: chunk.EventCollection,
BlockId: chunk.BlockID.Bytes(),
TotalComputationUsed: chunk.TotalComputationUsed,
NumberOfTransactions: uint32(chunk.NumberOfTransactions),
Index: chunk.Index,
EndState: IdentifierToMessage(flow.Identifier(chunk.EndState)),
}
}

serviceEvents := make([]*entities.ServiceEvent, len(result.ServiceEvents))
for i, event := range result.ServiceEvents {
serviceEvents[i] = &entities.ServiceEvent{
Type: event.Type,
Payload: event.Payload,
}
}

return &entities.ExecutionResult{
PreviousResultId: result.PreviousResultID.Bytes(),
BlockId: result.BlockID.Bytes(),
Chunks: chunks,
ServiceEvents: serviceEvents,
}, nil
}

func MessageToExecutionResults(m []*entities.ExecutionResult) ([]*flow.ExecutionResult, error) {
results := make([]*flow.ExecutionResult, len(m))

for i, result := range m {
res, err := MessageToExecutionResult(result)
if err != nil {
return nil, err
}
results[i] = res
}

return results, nil
}

func ExecutionResultsToMessage(execResults []*flow.ExecutionResult) ([]*entities.ExecutionResult, error) {
results := make([]*entities.ExecutionResult, len(execResults))

for i, result := range execResults {
res, err := ExecutionResultToMessage(*result)
if err != nil {
return nil, err
}
results[i] = res
}

return results, nil
}

func BlockExecutionDataToMessage(
execData *flow.ExecutionData,
) (*entities.BlockExecutionData, error) {
Expand Down
15 changes: 13 additions & 2 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,19 @@ func BlockStatusFromString(s string) BlockStatus {
//
// A payload contains the collection guarantees and seals for a block.
type BlockPayload struct {
CollectionGuarantees []*CollectionGuarantee
Seals []*BlockSeal
CollectionGuarantees []*CollectionGuarantee
Seals []*BlockSeal
Signatures [][]byte
ExecutionReceiptMetaList []*ExecutionReceiptMeta
ExecutionResultsList []*ExecutionResult
ProtocolStateID Identifier
}

type ExecutionReceiptMeta struct {
ExecutorID Identifier
ResultID Identifier
Spocks [][]byte
ExecutorSignature []byte
}

// BlockSeal is the attestation by verification nodes that the transactions in a previously
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.4
replace github.com/onflow/flow-go-sdk => ../

require (
github.com/onflow/cadence v1.0.1-0.20241018173327-2e72919b18ac
github.com/onflow/cadence v1.2.1
github.com/onflow/flow-cli/flowkit v1.11.0
github.com/onflow/flow-go-sdk v0.41.17
github.com/spf13/afero v1.11.0
Expand Down
1 change: 1 addition & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ github.com/onflow/cadence v1.0.0-preview.38/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmp
github.com/onflow/cadence v1.0.0-preview.52/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/cadence v1.0.0/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/cadence v1.0.1-0.20241018173327-2e72919b18ac/go.mod h1:fJxxOAp1wnWDfOHT8GOc1ypsU0RR5E3z51AhG8Yf5jg=
github.com/onflow/cadence v1.2.1/go.mod h1:fJxxOAp1wnWDfOHT8GOc1ypsU0RR5E3z51AhG8Yf5jg=
github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
Expand Down
58 changes: 56 additions & 2 deletions test/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ type Blocks struct {
guarantees *CollectionGuarantees
seals *BlockSeals
signatures *Signatures
ids *Identifiers
bytes *Bytes
chunks *ChuckExecutionResult
}

func BlockGenerator() *Blocks {
Expand All @@ -135,6 +138,9 @@ func BlockGenerator() *Blocks {
guarantees: CollectionGuaranteeGenerator(),
seals: BlockSealGenerator(),
signatures: SignaturesGenerator(),
ids: IdentifierGenerator(),
bytes: BytesGenerator(),
chunks: ChuckExecutionResultGenerator(),
}
}

Expand All @@ -151,9 +157,32 @@ func (g *Blocks) New() *flow.Block {
g.seals.New(),
}

executionReceiptMetaList := []*flow.ExecutionReceiptMeta{{
ExecutorID: g.ids.New(),
ResultID: g.ids.New(),
Spocks: [][]byte{g.bytes.New()},
ExecutorSignature: g.bytes.New(),
}}

serviceEvents := []*flow.ServiceEvent{{
Type: string(g.bytes.New()),
Payload: g.bytes.New(),
}}

executionResults := []*flow.ExecutionResult{{
PreviousResultID: g.ids.New(),
BlockID: g.ids.New(),
Chunks: []*flow.Chunk{g.chunks.New()},
ServiceEvents: serviceEvents,
}}

payload := flow.BlockPayload{
CollectionGuarantees: guarantees,
Seals: seals,
CollectionGuarantees: guarantees,
Seals: seals,
Signatures: g.signatures.New(),
ExecutionReceiptMetaList: executionReceiptMetaList,
ExecutionResultsList: executionResults,
ProtocolStateID: g.ids.New(),
}

return &flow.Block{
Expand Down Expand Up @@ -639,3 +668,28 @@ func (g *Bytes) New() []byte {
}
return randomBytes
}

type ChuckExecutionResult struct {
ids *Identifiers
bytes *Bytes
}

func ChuckExecutionResultGenerator() *ChuckExecutionResult {
return &ChuckExecutionResult{
ids: IdentifierGenerator(),
bytes: BytesGenerator(),
}
}

func (g *ChuckExecutionResult) New() *flow.Chunk {
return &flow.Chunk{
CollectionIndex: 42,
StartState: flow.StateCommitment(g.ids.New()),
EventCollection: g.bytes.New(),
BlockID: g.ids.New(),
TotalComputationUsed: 42,
NumberOfTransactions: 42,
Index: 42,
EndState: flow.StateCommitment(g.ids.New()),
}
}

0 comments on commit 54426af

Please sign in to comment.