Skip to content

Commit

Permalink
Merge branch 'master' into illia-malachyn/763-refactor-code-duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-malachyn committed Oct 22, 2024
2 parents 6b6ef84 + a0793fd commit 8e71cfc
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 55 deletions.
210 changes: 191 additions & 19 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,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(),
Expand All @@ -176,6 +179,7 @@ func BlockToMessage(b flow.Block) (*entities.Block, error) {
Timestamp: t,
CollectionGuarantees: CollectionGuaranteesToMessages(b.BlockPayload.CollectionGuarantees),
BlockSeals: BlockSealsToMessages(b.BlockPayload.Seals),
BlockHeader: header,
}, nil
}

Expand All @@ -187,11 +191,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())
Expand All @@ -217,12 +235,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
}

Expand All @@ -237,11 +268,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
}

Expand Down Expand Up @@ -385,15 +486,73 @@ func MessageToFullCollection(m []*entities.Transaction) (flow.FullCollection, er

func CollectionGuaranteeToMessage(g flow.CollectionGuarantee) *entities.CollectionGuarantee {
return &entities.CollectionGuarantee{
CollectionId: g.CollectionID.Bytes(),
CollectionId: g.CollectionID.Bytes(),
ReferenceBlockId: g.ReferenceBlockID.Bytes(),
Signature: g.Signature,
SignerIndices: g.SignerIndices,
}
}

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) {
Expand All @@ -402,7 +561,10 @@ func MessageToCollectionGuarantee(m *entities.CollectionGuarantee) (flow.Collect
}

return flow.CollectionGuarantee{
CollectionID: flow.HashToID(m.CollectionId),
CollectionID: flow.HashToID(m.CollectionId),
ReferenceBlockID: flow.HashToID(m.ReferenceBlockId),
Signature: m.Signature,
SignerIndices: m.SignerIndices,
}, nil
}

Expand All @@ -411,9 +573,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
2 changes: 1 addition & 1 deletion access/http/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func ToCollectionGuarantees(guarantees []models.CollectionGuarantee) []*flow.Col

for i, guarantee := range guarantees {
flowGuarantees[i] = &flow.CollectionGuarantee{
flow.HexToID(guarantee.CollectionId),
CollectionID: flow.HexToID(guarantee.CollectionId),
}
}

Expand Down
50 changes: 43 additions & 7 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 All @@ -28,11 +30,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.
Expand Down Expand Up @@ -74,7 +100,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
5 changes: 4 additions & 1 deletion collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (c Collection) Encode() []byte {

// A CollectionGuarantee is an attestation signed by the nodes that have guaranteed a collection.
type CollectionGuarantee struct {
CollectionID Identifier
CollectionID Identifier
ReferenceBlockID Identifier
Signature []byte
SignerIndices []byte
}

type FullCollection struct {
Expand Down
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// NOTE: always import Cadence's stdlib package,
// as it registers the type ID decoder for the Flow types,
// e.g. `flow.AccountCreated`
_ "github.com/onflow/cadence/runtime/stdlib"
_ "github.com/onflow/cadence/stdlib"
"github.com/onflow/flow/protobuf/go/flow/entities"
)

Expand Down
4 changes: 2 additions & 2 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ package flow_test
import (
"testing"

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/stdlib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/cadence/stdlib"
"github.com/onflow/crypto/hash"

"github.com/onflow/flow-go-sdk/crypto"
Expand Down
Loading

0 comments on commit 8e71cfc

Please sign in to comment.