Repository navigation
Expand file tree
/
Copy pathtransact.go
More file actions
133 lines (110 loc) · 2.99 KB
/
Copy pathtransact.go
File metadata and controls
133 lines (110 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"fmt"
"math/rand/v2"
"sync"
atb "offchaindata/contract"
"github.com/google/uuid"
"github.com/hyperledger/fabric-gateway/pkg/client"
"google.golang.org/grpc"
)
var owners = []string{"alice", "bob", "charlie"}
func transact(clientConnection grpc.ClientConnInterface) error {
id, options := newConnectOptions(clientConnection)
gateway, err := client.Connect(id, options...)
if err != nil {
return err
}
defer func() {
_ = gateway.Close()
fmt.Println("Gateway closed.")
}()
contract := gateway.GetNetwork(channelName).GetContract(chaincodeName)
smartContract := atb.NewAssetTransferBasic(contract)
app := newTransactApp(smartContract)
return app.run()
}
type transactApp struct {
smartContract *atb.AssetTransferBasic
batchSize int
}
func newTransactApp(smartContract *atb.AssetTransferBasic) *transactApp {
return &transactApp{smartContract, 10}
}
func (t *transactApp) run() error {
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(nil)
var wg sync.WaitGroup
for range t.batchSize {
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
if err := t.transact(); err != nil {
cancel(err)
return
}
}
}()
}
wg.Wait()
return context.Cause(ctx)
}
func (t *transactApp) transact() error {
anAsset, err := newAsset()
if err != nil {
return err
}
if err := t.smartContract.CreateAsset(anAsset); err != nil {
return err
}
fmt.Println("Created asset", anAsset.ID)
// Transfer randomly 1 in 2 assets to a new owner.
if rand.N(2) == 0 { //nolint:gosec // non-security use
newOwner := differentElement(owners, anAsset.Owner)
oldOwner, err := t.smartContract.TransferAsset(anAsset.ID, newOwner)
if err != nil {
return err
}
fmt.Printf("Transferred asset %s from %s to %s\n", anAsset.ID, oldOwner, newOwner)
}
// Delete randomly 1 in 4 created assets.
if rand.N(4) == 0 { //nolint:gosec // non-security use
if err := t.smartContract.DeleteAsset(anAsset.ID); err != nil {
return err
}
fmt.Println("Deleted asset", anAsset.ID)
}
return nil
}
func newAsset() (atb.Asset, error) {
id, err := uuid.NewRandom()
if err != nil {
return atb.Asset{}, err
}
return atb.Asset{
ID: id.String(),
Color: randomElement([]string{"red", "green", "blue"}),
Size: uint64(rand.N(10) + 1), //nolint:gosec // non-security use
Owner: randomElement(owners),
AppraisedValue: uint64(rand.N(1000) + 1), //nolint:gosec // non-security use
}, nil
}
// Pick a random element from an array.
func randomElement(values []string) string {
return values[rand.N(len(values))] //nolint:gosec // non-security use
}
// Pick a random element from an array, excluding the current value.
func differentElement(values []string, currentValue string) string {
candidateValues := []string{}
for _, v := range values {
if v != currentValue {
candidateValues = append(candidateValues, v)
}
}
return randomElement(candidateValues)
}