mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-18 07:55:10 +00:00
Starting from the processor.Block.Process all methods now return errors if something goes wrong with unpacking of the blocks and reading the transactions. In each function where the error is being propagated back to client it is wrapped in a message with the function name. This makes it easier to track down the error and see the propagation chain. Finally the error is logged to the terminal and the go routine shuts down gracefully. The graceful shutdown executes all deferred functions which close the context, the checkpointer and the gateway. Before panics were used everywhere which was an issue because the unpacking of the blocks happened in a go routine. When a panic happens in a go routine only the deferred functions of the go routine are called but not those of the client which lead to unexpected behavior. The transact function is also executed in a go routine therefore the same typo of error handling was implemented there. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
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 payload struct {
|
|
commonPayload *common.Payload
|
|
statusCode int32
|
|
}
|
|
|
|
func parsePayload(commonPayload *common.Payload, statusCode int32) *payload {
|
|
return &payload{commonPayload, statusCode}
|
|
}
|
|
|
|
func (p *payload) channelHeader() (*common.ChannelHeader, error) {
|
|
return utils.Cache(func() (*common.ChannelHeader, error) {
|
|
funcName := "channelHeader"
|
|
|
|
header, err := utils.AssertDefined(p.commonPayload.GetHeader(), "missing payload header")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
result := &common.ChannelHeader{}
|
|
if err := proto.Unmarshal(header.GetChannelHeader(), result); err != nil {
|
|
return nil, fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
return result, nil
|
|
})()
|
|
}
|
|
|
|
func (p *payload) endorserTransaction() (*endorserTransaction, error) {
|
|
funcName := "endorserTransaction"
|
|
|
|
is, err := p.isEndorserTransaction()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
if !is {
|
|
channelHeader, err := p.channelHeader()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
return nil, fmt.Errorf("unexpected payload type: %d", channelHeader.GetType())
|
|
}
|
|
|
|
result := &peer.Transaction{}
|
|
if err := proto.Unmarshal(p.commonPayload.GetData(), result); err != nil {
|
|
return nil, fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
return parseEndorserTransaction(result), nil
|
|
}
|
|
|
|
func (p *payload) isEndorserTransaction() (bool, error) {
|
|
channelHeader, err := p.channelHeader()
|
|
if err != nil {
|
|
return false, fmt.Errorf("in isEndorserTransaction: %w", err)
|
|
}
|
|
|
|
return channelHeader.GetType() == int32(common.HeaderType_ENDORSER_TRANSACTION), nil
|
|
}
|
|
|
|
func (p *payload) isValid() bool {
|
|
return p.statusCode == int32(peer.TxValidationCode_VALID)
|
|
}
|