/
parallel.go
190 lines (163 loc) · 5.41 KB
/
parallel.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package workload
import (
"context"
"fmt"
"math/rand"
"sync"
"time"
"google.golang.org/grpc"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
memorySigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/memory"
"github.com/oasisprotocol/oasis-core/go/common/logging"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
// NameParallel is the name of the parallel workload.
const NameParallel = "parallel"
// Parallel is the parallel workload.
var Parallel = ¶llel{
BaseWorkload: NewBaseWorkload(NameParallel),
}
const (
parallelSendWaitTimeout = 120 * time.Second
parallelSendTimeoutInterval = 60 * time.Second
parallelConcurency = 200
parallelTxTransferAmount = 100
parallelTxFundInterval = 10
)
var parallelLogger = logging.GetLogger("cmd/txsource/workload/parallel")
type parallel struct {
BaseWorkload
}
// Implements Workload.
func (p *parallel) NeedsFunds() bool {
return true
}
// Implements workload.
func (p *parallel) Run(
gracefulExit context.Context,
rng *rand.Rand,
conn *grpc.ClientConn,
cnsc consensus.ClientBackend,
sm consensus.SubmissionManager,
fundingAccount signature.Signer,
validatorEntities []signature.Signer,
) error {
// Initialize base workload.
p.BaseWorkload.Init(cnsc, sm, fundingAccount)
ctx := context.Background()
var err error
// Estimate gas needed for the used transfer transaction.
var txGasAmount transaction.Gas
xfer := &staking.Transfer{
To: staking.NewAddress(fundingAccount.Public()),
}
if err = xfer.Amount.FromInt64(parallelTxTransferAmount); err != nil {
return fmt.Errorf("transfer base units FromInt64 %d: %w", parallelTxTransferAmount, err)
}
txGasAmount, err = cnsc.EstimateGas(ctx, &consensus.EstimateGasRequest{
Signer: fundingAccount.Public(),
Transaction: staking.NewTransferTx(0, nil, xfer),
})
if err != nil {
return fmt.Errorf("failed to estimate gas: %w", err)
}
gasPrice := p.GasPrice()
accounts := make([]signature.Signer, parallelConcurency)
fac := memorySigner.NewFactory()
for i := range accounts {
accounts[i], err = fac.Generate(signature.SignerEntity, rng)
if err != nil {
return fmt.Errorf("memory signer factory Generate account %d: %w", i, err)
}
// Initial funding of accounts.
fundAmount := parallelTxTransferAmount + // self transfer amount
parallelTxFundInterval*uint64(txGasAmount)*gasPrice // gas for `parallelTxFundInterval` transfers.
addr := staking.NewAddress(accounts[i].Public())
if err = p.TransferFunds(ctx, fundingAccount, addr, fundAmount); err != nil {
return fmt.Errorf("account funding failure: %w", err)
}
}
// A single global nonce is enough as we wait for all submissions to
// complete before proceeding with a new batch.
var nonce uint64
fee := transaction.Fee{
Gas: txGasAmount,
}
if err = fee.Amount.FromUint64(uint64(txGasAmount) * gasPrice); err != nil {
return fmt.Errorf("fee amount error: %w", err)
}
for i := uint64(1); ; i++ {
errCh := make(chan error, parallelConcurency)
var wg sync.WaitGroup
wg.Add(parallelConcurency)
for c := 0; c < parallelConcurency; c++ {
go func(txSigner signature.Signer, nonce uint64) {
defer wg.Done()
addr := staking.NewAddress(txSigner.Public())
// Transfer tx.
transfer := staking.Transfer{
To: addr,
}
if err = transfer.Amount.FromInt64(parallelTxTransferAmount); err != nil {
errCh <- fmt.Errorf("transfer base units FromInt64 %d: %w", parallelTxTransferAmount, err)
return
}
tx := staking.NewTransferTx(nonce, &fee, &transfer)
var signedTx *transaction.SignedTransaction
signedTx, err = transaction.Sign(txSigner, tx)
if err != nil {
parallelLogger.Error("transaction.Sign error", "err", err)
errCh <- fmt.Errorf("transaction.Sign: %w", err)
return
}
parallelLogger.Debug("submitting self transfer",
"account", addr,
)
if err = cnsc.SubmitTx(ctx, signedTx); err != nil {
parallelLogger.Error("SubmitTx error", "err", err)
errCh <- fmt.Errorf("cnsc.SubmitTx: %w", err)
return
}
}(accounts[c], nonce)
}
// Wait for transactions.
waitC := make(chan struct{})
go func() {
defer close(waitC)
wg.Wait()
nonce++
}()
select {
case <-time.After(parallelSendWaitTimeout):
parallelLogger.Error("transactions not completed within timeout")
return fmt.Errorf("workload parallel: transactions not completed within timeout")
case err = <-errCh:
parallelLogger.Error("error subimit transaction",
"err", err,
)
return fmt.Errorf("workload parallel: error submiting transaction: %w", err)
case <-waitC:
parallelLogger.Debug("all transfers successful",
"concurency", parallelConcurency,
)
}
if i%parallelTxFundInterval == 0 {
// Re-fund accounts for next `parallelTxFundInterval` transfers.
for i := range accounts {
fundAmount := parallelTxFundInterval * uint64(txGasAmount) * gasPrice // gas for `parallelTxFundInterval` transfers.
addr := staking.NewAddress(accounts[i].Public())
if err = p.TransferFunds(ctx, fundingAccount, addr, fundAmount); err != nil {
return fmt.Errorf("account funding failure: %w", err)
}
}
}
select {
case <-time.After(parallelSendTimeoutInterval):
case <-gracefulExit.Done():
parallelLogger.Debug("time's up")
return nil
}
}
}