mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 15:35:09 +00:00
Implement block parsing
Removed Block and Transaction interfaces and unused statusCode function. Using the struct instead of the interfaces now. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
This commit is contained in:
parent
2c43e03591
commit
8ae2909b9b
4 changed files with 67 additions and 67 deletions
|
|
@ -1,23 +1,20 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"offChainData/utils"
|
||||
|
||||
"github.com/hyperledger/fabric-protos-go-apiv2/common"
|
||||
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
block *common.Block
|
||||
validationCodes []byte
|
||||
transactions []Transaction
|
||||
block *common.Block
|
||||
transactions []*Transaction
|
||||
}
|
||||
|
||||
func ParseBlock(block *common.Block) Block {
|
||||
validationCodes := extractTransactionValidationCodes(block)
|
||||
|
||||
return Block{block, validationCodes, nil}
|
||||
func ParseBlock(block *common.Block) *Block {
|
||||
return &Block{block, nil}
|
||||
}
|
||||
|
||||
func (b *Block) Number() uint64 {
|
||||
|
|
@ -25,58 +22,53 @@ func (b *Block) Number() uint64 {
|
|||
return header.GetNumber()
|
||||
}
|
||||
|
||||
// TODO: needs cache, getPayloads, parsePayload
|
||||
func (b *Block) Transactions() []Transaction {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Block) ToProto() *common.Block {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Block) payloads() []*common.Payload {
|
||||
var payloads []*common.Payload
|
||||
|
||||
for _, envelopeBytes := range b.block.GetData().GetData() {
|
||||
// TODO: needs cache; decompose
|
||||
func (b *Block) Transactions() []*Transaction {
|
||||
envelopes := []*common.Envelope{}
|
||||
for _, blockData := range b.block.GetData().GetData() {
|
||||
envelope := &common.Envelope{}
|
||||
if err := proto.Unmarshal(envelopeBytes, envelope); err != nil {
|
||||
if err := proto.Unmarshal(blockData, envelope); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
envelopes = append(envelopes, envelope)
|
||||
}
|
||||
|
||||
payload := &common.Payload{}
|
||||
if err := proto.Unmarshal(envelope.Payload, payload); err != nil {
|
||||
commonPayloads := []*common.Payload{}
|
||||
for _, envelope := range envelopes {
|
||||
commonPayload := &common.Payload{}
|
||||
if err := proto.Unmarshal(envelope.GetPayload(), commonPayload); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
payloads = append(payloads, payload)
|
||||
commonPayloads = append(commonPayloads, commonPayload)
|
||||
}
|
||||
|
||||
return payloads
|
||||
validationCodes := b.extractTransactionValidationCodes()
|
||||
payloads := []*PayloadImpl{}
|
||||
for i, commonPayload := range commonPayloads {
|
||||
payload := ParsePayload(
|
||||
commonPayload,
|
||||
int32(utils.AssertDefined(
|
||||
validationCodes[i],
|
||||
fmt.Sprint("missing validation code index", i),
|
||||
),
|
||||
),
|
||||
)
|
||||
if payload.IsEndorserTransaction() {
|
||||
payloads = append(payloads, payload)
|
||||
}
|
||||
}
|
||||
|
||||
result := []*Transaction{}
|
||||
for _, payload := range payloads {
|
||||
result = append(result, NewTransaction(payload))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// TODO not sure about this
|
||||
func (pb *Block) statusCode(txIndex int) peer.TxValidationCode {
|
||||
blockMetadata := utils.AssertDefined(
|
||||
pb.block.GetMetadata(),
|
||||
"missing block metadata",
|
||||
)
|
||||
|
||||
metadata := blockMetadata.GetMetadata()
|
||||
if int(common.BlockMetadataIndex_TRANSACTIONS_FILTER) >= len(metadata) {
|
||||
return peer.TxValidationCode_INVALID_OTHER_REASON
|
||||
}
|
||||
|
||||
statusCodes := metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER]
|
||||
if txIndex >= len(statusCodes) {
|
||||
return peer.TxValidationCode_INVALID_OTHER_REASON
|
||||
}
|
||||
|
||||
return peer.TxValidationCode(statusCodes[txIndex])
|
||||
}
|
||||
|
||||
func extractTransactionValidationCodes(block *common.Block) []byte {
|
||||
func (b *Block) extractTransactionValidationCodes() []byte {
|
||||
metadata := utils.AssertDefined(
|
||||
block.GetMetadata(),
|
||||
b.block.GetMetadata(),
|
||||
"missing block metadata",
|
||||
)
|
||||
|
||||
|
|
@ -85,3 +77,8 @@ func extractTransactionValidationCodes(block *common.Block) []byte {
|
|||
"missing transaction validation code",
|
||||
)
|
||||
}
|
||||
|
||||
// TODO remove unused?
|
||||
func (b *Block) ToProto() *common.Block {
|
||||
return b.block
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// TODO remove interface, use struct; encapsulate
|
||||
type Payload interface {
|
||||
ChannelHeader() *common.ChannelHeader
|
||||
EndorserTransaction() EndorserTransaction
|
||||
|
|
@ -77,6 +78,7 @@ func (p *PayloadImpl) IsValid() bool {
|
|||
return p.statusCode == int32(peer.TxValidationCode_VALID)
|
||||
}
|
||||
|
||||
// TODO remove unused
|
||||
func (p *PayloadImpl) ToProto() *common.Payload {
|
||||
return p.payload
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// TODO remove interface, use struct; encapsulate; extract into file
|
||||
type ReadWriteSet interface {
|
||||
NamespaceReadWriteSets() []NamespaceReadWriteSet
|
||||
ToProto() *rwset.TxReadWriteSet
|
||||
|
|
@ -28,10 +29,12 @@ func (p *ReadWriteSetImpl) NamespaceReadWriteSets() []NamespaceReadWriteSet {
|
|||
return result
|
||||
}
|
||||
|
||||
// TODO remove unused
|
||||
func (p *ReadWriteSetImpl) ToProto() *rwset.TxReadWriteSet {
|
||||
return p.readWriteSet
|
||||
}
|
||||
|
||||
// TODO remove interface, use struct
|
||||
type NamespaceReadWriteSet interface {
|
||||
Namespace() string
|
||||
ReadWriteSet() *kvrwset.KVRWSet
|
||||
|
|
@ -60,6 +63,7 @@ func (p *NamespaceReadWriteSetImpl) ReadWriteSet() *kvrwset.KVRWSet {
|
|||
return &result
|
||||
}
|
||||
|
||||
// TODO remove unused
|
||||
func (p *NamespaceReadWriteSetImpl) ToProto() *rwset.NsReadWriteSet {
|
||||
return p.nsReadWriteSet
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,28 +11,20 @@ import (
|
|||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Transaction interface {
|
||||
ChannelHeader() *common.ChannelHeader
|
||||
Creator() identity.Identity
|
||||
ValidationCode() int32
|
||||
IsValid() bool
|
||||
NamespaceReadWriteSets() []NamespaceReadWriteSet
|
||||
ToProto() *common.Payload
|
||||
}
|
||||
|
||||
type TransactionImpl struct {
|
||||
type Transaction struct {
|
||||
payload Payload
|
||||
}
|
||||
|
||||
func NewTransactionImpl(payload Payload) *TransactionImpl {
|
||||
return &TransactionImpl{payload}
|
||||
func NewTransaction(payload Payload) *Transaction {
|
||||
return &Transaction{payload}
|
||||
}
|
||||
|
||||
func (t *TransactionImpl) ChannelHeader() *common.ChannelHeader {
|
||||
func (t *Transaction) ChannelHeader() *common.ChannelHeader {
|
||||
return t.payload.ChannelHeader()
|
||||
}
|
||||
|
||||
func (t *TransactionImpl) Creator() identity.Identity {
|
||||
// TODO remove unused?
|
||||
func (t *Transaction) Creator() identity.Identity {
|
||||
creator := &msp.SerializedIdentity{}
|
||||
if err := proto.Unmarshal(t.payload.SignatureHeader().GetCreator(), creator); err != nil {
|
||||
panic(err)
|
||||
|
|
@ -41,7 +33,7 @@ func (t *TransactionImpl) Creator() identity.Identity {
|
|||
return &identityImpl{creator}
|
||||
}
|
||||
|
||||
func (t *TransactionImpl) NamespaceReadWriteSets() []NamespaceReadWriteSet {
|
||||
func (t *Transaction) NamespaceReadWriteSets() []NamespaceReadWriteSet {
|
||||
result := []NamespaceReadWriteSet{}
|
||||
for _, readWriteSet := range t.payload.EndorserTransaction().ReadWriteSets() {
|
||||
result = append(result, readWriteSet.NamespaceReadWriteSets()...)
|
||||
|
|
@ -50,18 +42,22 @@ func (t *TransactionImpl) NamespaceReadWriteSets() []NamespaceReadWriteSet {
|
|||
return result
|
||||
}
|
||||
|
||||
func (t *TransactionImpl) ValidationCode() int32 {
|
||||
// TODO remove unused?
|
||||
func (t *Transaction) ValidationCode() int32 {
|
||||
return t.payload.TransactionValidationCode()
|
||||
}
|
||||
|
||||
func (t *TransactionImpl) IsValid() bool {
|
||||
// TODO remove unused?
|
||||
func (t *Transaction) IsValid() bool {
|
||||
return t.payload.IsValid()
|
||||
}
|
||||
|
||||
func (t *TransactionImpl) ToProto() *common.Payload {
|
||||
// TODO remove unused?
|
||||
func (t *Transaction) ToProto() *common.Payload {
|
||||
return t.payload.ToProto()
|
||||
}
|
||||
|
||||
// TODO remove interface, use struct; encapsulate; extract into file
|
||||
type EndorserTransaction interface {
|
||||
ReadWriteSets() []ReadWriteSet
|
||||
ToProto() *peer.Transaction
|
||||
|
|
@ -164,6 +160,7 @@ func (*EndorserTransactionImpl) parseReadWriteSets(txReadWriteSets []*rwset.TxRe
|
|||
return result
|
||||
}
|
||||
|
||||
// TODO remove unused
|
||||
func (p *EndorserTransactionImpl) ToProto() *peer.Transaction {
|
||||
return p.transaction
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue