mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-18 16:05:10 +00:00
Created project structure, fixed typos. Implemented connect.go and getAllAssets.go. The latter uses an assetTransferBasic struct which provides a simple API for basic asset operations like create, transfer, etc. Added transact.go with some util functions. Using google uuid package to generate random UUIDs for the transactions. Implemented pretty printing of JSON results. Implemented app.go entry point with error handling. The existing commands are getAllAssets, transact and listen. They can be called from the command line via: "go run . <command> <command> ...". They will be executed in order and if a command is not known an the application panics and aborts before executing any of the commands. Implementing listen.go. Added checkpointer, context setups, call to BlockEvents and all the interfaces needed for parsing. Started implementing the interfaces needed to represent a block bottom up in structs. Finished NamespaceReadWriteSet, ReadWriteSet and EndorserTransaction. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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 gateway.Close()
|
|
|
|
contract := gateway.GetNetwork(channelName).GetContract(chaincodeName)
|
|
|
|
smartContract := newAssetTransferBasic(contract)
|
|
app := newTransactApp(smartContract)
|
|
app.run()
|
|
}
|
|
|
|
type transactApp struct {
|
|
smartContract *assetTransferBasic
|
|
batchSize uint
|
|
}
|
|
|
|
func newTransactApp(smartContract *assetTransferBasic) *transactApp {
|
|
return &transactApp{smartContract, 10}
|
|
}
|
|
|
|
var (
|
|
colors = []string{"red", "green", "blue"}
|
|
owners = []string{"alice", "bob", "charlie"}
|
|
)
|
|
|
|
const (
|
|
maxInitialValue = 1000
|
|
maxInitialSize = 10
|
|
)
|
|
|
|
func (t *transactApp) run() {
|
|
for i := 0; i < int(t.batchSize); i++ {
|
|
go t.transact()
|
|
}
|
|
}
|
|
|
|
func (t *transactApp) transact() {
|
|
anAsset := NewAsset()
|
|
|
|
t.smartContract.createAsset(anAsset)
|
|
fmt.Printf("\nCreated asset %s\n", anAsset.ID)
|
|
|
|
// Transfer randomly 1 in 2 assets to a new owner.
|
|
if randomInt(2) == 0 {
|
|
newOwner := differentElement(owners, anAsset.Owner)
|
|
oldOwner := t.smartContract.transferAsset(anAsset.ID, newOwner)
|
|
fmt.Printf("Transferred asset %s from %s to %s\n", anAsset.ID, oldOwner, newOwner)
|
|
}
|
|
|
|
// Delete randomly 1 in 4 created assets.
|
|
if randomInt(4) == 0 {
|
|
t.smartContract.deleteAsset(anAsset.ID)
|
|
fmt.Printf("Deleted asset %s\n", anAsset.ID)
|
|
}
|
|
}
|