github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/collections.go (about) 1 package convert 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/flow/protobuf/go/flow/entities" 7 8 "github.com/onflow/flow-go/model/flow" 9 ) 10 11 // CollectionToMessage converts a collection to a protobuf message 12 func CollectionToMessage(c *flow.Collection) (*entities.Collection, error) { 13 if c == nil || c.Transactions == nil { 14 return nil, fmt.Errorf("invalid collection") 15 } 16 17 transactionsIDs := make([][]byte, len(c.Transactions)) 18 for i, t := range c.Transactions { 19 id := t.ID() 20 transactionsIDs[i] = id[:] 21 } 22 23 collectionID := c.ID() 24 25 ce := &entities.Collection{ 26 Id: collectionID[:], 27 TransactionIds: transactionsIDs, 28 } 29 30 return ce, nil 31 } 32 33 // LightCollectionToMessage converts a light collection to a protobuf message 34 func LightCollectionToMessage(c *flow.LightCollection) (*entities.Collection, error) { 35 if c == nil || c.Transactions == nil { 36 return nil, fmt.Errorf("invalid collection") 37 } 38 39 collectionID := c.ID() 40 41 return &entities.Collection{ 42 Id: collectionID[:], 43 TransactionIds: IdentifiersToMessages(c.Transactions), 44 }, nil 45 } 46 47 // MessageToLightCollection converts a protobuf message to a light collection 48 func MessageToLightCollection(m *entities.Collection) (*flow.LightCollection, error) { 49 transactions := make([]flow.Identifier, 0, len(m.TransactionIds)) 50 for _, txId := range m.TransactionIds { 51 transactions = append(transactions, MessageToIdentifier(txId)) 52 } 53 54 return &flow.LightCollection{ 55 Transactions: transactions, 56 }, nil 57 } 58 59 // CollectionGuaranteeToMessage converts a collection guarantee to a protobuf message 60 func CollectionGuaranteeToMessage(g *flow.CollectionGuarantee) *entities.CollectionGuarantee { 61 id := g.ID() 62 63 return &entities.CollectionGuarantee{ 64 CollectionId: id[:], 65 Signatures: [][]byte{g.Signature}, 66 ReferenceBlockId: IdentifierToMessage(g.ReferenceBlockID), 67 Signature: g.Signature, 68 SignerIndices: g.SignerIndices, 69 } 70 } 71 72 // MessageToCollectionGuarantee converts a protobuf message to a collection guarantee 73 func MessageToCollectionGuarantee(m *entities.CollectionGuarantee) *flow.CollectionGuarantee { 74 return &flow.CollectionGuarantee{ 75 CollectionID: MessageToIdentifier(m.CollectionId), 76 ReferenceBlockID: MessageToIdentifier(m.ReferenceBlockId), 77 SignerIndices: m.SignerIndices, 78 Signature: MessageToSignature(m.Signature), 79 } 80 } 81 82 // CollectionGuaranteesToMessages converts a slice of collection guarantees to a slice of protobuf messages 83 func CollectionGuaranteesToMessages(c []*flow.CollectionGuarantee) []*entities.CollectionGuarantee { 84 cg := make([]*entities.CollectionGuarantee, len(c)) 85 for i, g := range c { 86 cg[i] = CollectionGuaranteeToMessage(g) 87 } 88 return cg 89 } 90 91 // MessagesToCollectionGuarantees converts a slice of protobuf messages to a slice of collection guarantees 92 func MessagesToCollectionGuarantees(m []*entities.CollectionGuarantee) []*flow.CollectionGuarantee { 93 cg := make([]*flow.CollectionGuarantee, len(m)) 94 for i, g := range m { 95 cg[i] = MessageToCollectionGuarantee(g) 96 } 97 return cg 98 }