mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-18 16:05: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>
89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
atb "offChainData/contract"
|
|
"offChainData/utils"
|
|
|
|
"github.com/hyperledger/fabric-gateway/pkg/client"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
func transact(clientConnection *grpc.ClientConn) {
|
|
id, options := newConnectOptions(clientConnection)
|
|
gateway, err := client.Connect(id, options...)
|
|
if err != nil {
|
|
panic((err))
|
|
}
|
|
defer func() {
|
|
gateway.Close()
|
|
fmt.Println("Gateway closed.")
|
|
}()
|
|
|
|
contract := gateway.GetNetwork(channelName).GetContract(chaincodeName)
|
|
|
|
smartContract := atb.NewAssetTransferBasic(contract)
|
|
app := newTransactApp(smartContract)
|
|
app.run()
|
|
}
|
|
|
|
type transactApp struct {
|
|
smartContract *atb.AssetTransferBasic
|
|
batchSize int
|
|
}
|
|
|
|
func newTransactApp(smartContract *atb.AssetTransferBasic) *transactApp {
|
|
return &transactApp{smartContract, 10}
|
|
}
|
|
|
|
func (t *transactApp) run() {
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < t.batchSize; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
|
|
if err := t.transact(); err != nil {
|
|
fmt.Println("\033[31m[ERROR]\033[0m", err)
|
|
return
|
|
}
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func (t *transactApp) transact() error {
|
|
funcName := "transact"
|
|
|
|
anAsset := atb.NewAsset()
|
|
|
|
err := t.smartContract.CreateAsset(anAsset)
|
|
if err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
fmt.Println("Created asset", anAsset.ID)
|
|
|
|
// Transfer randomly 1 in 2 assets to a new owner.
|
|
if utils.RandomInt(2) == 0 {
|
|
newOwner := utils.DifferentElement(atb.Owners, anAsset.Owner)
|
|
oldOwner, err := t.smartContract.TransferAsset(anAsset.ID, newOwner)
|
|
if err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
fmt.Printf("Transferred asset %s from %s to %s\n", anAsset.ID, oldOwner, newOwner)
|
|
}
|
|
|
|
// Delete randomly 1 in 4 created assets.
|
|
if utils.RandomInt(4) == 0 {
|
|
err := t.smartContract.DeleteAsset(anAsset.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
fmt.Println("Deleted asset", anAsset.ID)
|
|
}
|
|
return nil
|
|
}
|