mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 15:35:09 +00:00
- switch to ClientConnInterface - use command type alias for map of commands - add error return to command functions and handle in app.go - inline formatJSON function in getAllAssets.go - replace most panics with error returns - remove error wrapping in listen.go and further down the line - use strconv.ParseUint instead of ParseFloat - use WithCancelCause in transact.go to grab and propagate error from go routine - unmarshal and return []Asset in atb.GetAllAssets - switch to rand package - remove dependency to protobuf reflect package - switch to sync.OnceValues for caching parser - fixed typo in events sample connect.go Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package parser
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/hyperledger/fabric-protos-go-apiv2/common"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type Block struct {
|
|
block *common.Block
|
|
transactions func() ([]*Transaction, error)
|
|
}
|
|
|
|
func ParseBlock(block *common.Block) *Block {
|
|
result := &Block{block, nil}
|
|
result.transactions = sync.OnceValues(result.unmarshalTransactions)
|
|
return result
|
|
}
|
|
|
|
func (b *Block) Number() uint64 {
|
|
return b.block.GetHeader().GetNumber()
|
|
}
|
|
|
|
func (b *Block) Transactions() ([]*Transaction, error) {
|
|
return b.transactions()
|
|
}
|
|
|
|
func (b *Block) unmarshalTransactions() ([]*Transaction, error) {
|
|
envelopes, err := b.unmarshalEnvelopes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commonPayloads, err := b.unmarshalPayloadsFrom(envelopes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
payloads, err := b.parse(commonPayloads)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b.createTransactionsFrom(payloads), nil
|
|
}
|
|
|
|
func (b *Block) unmarshalEnvelopes() ([]*common.Envelope, error) {
|
|
result := []*common.Envelope{}
|
|
for _, blockData := range b.block.GetData().GetData() {
|
|
envelope := &common.Envelope{}
|
|
if err := proto.Unmarshal(blockData, envelope); err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, envelope)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (*Block) unmarshalPayloadsFrom(envelopes []*common.Envelope) ([]*common.Payload, error) {
|
|
result := []*common.Payload{}
|
|
for _, envelope := range envelopes {
|
|
commonPayload := &common.Payload{}
|
|
if err := proto.Unmarshal(envelope.GetPayload(), commonPayload); err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, commonPayload)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (b *Block) parse(commonPayloads []*common.Payload) ([]*payload, error) {
|
|
validationCodes := b.block.GetMetadata().GetMetadata()[common.BlockMetadataIndex_TRANSACTIONS_FILTER]
|
|
|
|
result := []*payload{}
|
|
for i, commonPayload := range commonPayloads {
|
|
statusCode := validationCodes[i]
|
|
|
|
payload := parsePayload(commonPayload, int32(statusCode))
|
|
is, err := payload.isEndorserTransaction()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if is {
|
|
result = append(result, payload)
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (*Block) createTransactionsFrom(payloads []*payload) []*Transaction {
|
|
result := []*Transaction{}
|
|
for _, payload := range payloads {
|
|
result = append(result, newTransaction(payload))
|
|
}
|
|
return result
|
|
}
|