/
deploy_contract.go
106 lines (88 loc) · 2.96 KB
/
deploy_contract.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
package loadbot
import (
"context"
"fmt"
"sync/atomic"
"time"
"github.com/alveycoin/alveychain/command/loadbot/generator"
"github.com/alveycoin/alveychain/helper/tests"
txpoolOp "github.com/alveycoin/alveychain/txpool/proto"
"github.com/alveycoin/alveychain/types"
"github.com/umbracle/ethgo/jsonrpc"
)
func (l *Loadbot) deployContract(
grpcClient txpoolOp.TxnPoolOperatorClient,
jsonClient *jsonrpc.Client,
receiptTimeout time.Duration) error {
start := time.Now()
_, ok := l.generator.(generator.ContractTxnGenerator)
if !ok {
return fmt.Errorf("invalid generator type, it needs to be a generator.ContractTxnGenerator interface")
}
// deploy SC
txHash, err := l.executeTxn(grpcClient)
if err != nil {
//nolint:forcetypeassert
l.generator.(generator.ContractTxnGenerator).MarkFailedContractTxn(&generator.FailedContractTxnInfo{
TxHash: txHash.String(),
Error: &generator.TxnError{
Error: err,
ErrorType: generator.AddErrorType,
},
})
atomic.AddUint64(&l.metrics.ContractMetrics.FailedContractTransactionsCount, 1)
return fmt.Errorf("could not execute transaction, %w", err)
}
// set timeout
ctx, cancel := context.WithTimeout(context.Background(), receiptTimeout)
defer cancel()
// and wait for receipt
receipt, err := tests.WaitForReceipt(ctx, jsonClient.Eth(), txHash)
if err != nil {
//nolint:forcetypeassert
l.generator.(generator.ContractTxnGenerator).MarkFailedContractTxn(&generator.FailedContractTxnInfo{
TxHash: txHash.String(),
Error: &generator.TxnError{
Error: err,
ErrorType: generator.ReceiptErrorType,
},
})
atomic.AddUint64(&l.metrics.ContractMetrics.FailedContractTransactionsCount, 1)
return fmt.Errorf("could not get the receipt, %w", err)
}
end := time.Now()
// initialize gas metrics map with block number as index
l.metrics.ContractMetrics.ContractGasMetrics, err = getBlockGasMetrics(
jsonClient,
map[uint64]struct{}{
receipt.BlockNumber: {},
},
)
if err != nil {
return fmt.Errorf("unable to fetch gas metrics, %w", err)
}
// fetch contract address
l.metrics.ContractMetrics.ContractAddress = receipt.ContractAddress
// set contract address in order to get new example txn and gas estimate
//nolint:forcetypeassert
l.generator.(generator.ContractTxnGenerator).SetContractAddress(types.StringToAddress(
receipt.ContractAddress.String(),
))
// we're done with SC deployment
// we defined SC address and
// now get new gas estimates for CS token transfers
if err := l.updateGasEstimate(jsonClient); err != nil {
return fmt.Errorf("unable to get gas estimate, %w", err)
}
// record contract deployment metrics
l.metrics.ContractMetrics.ContractDeploymentDuration.reportTurnAroundTime(
txHash,
&metadata{
turnAroundTime: end.Sub(start),
blockNumber: receipt.BlockNumber,
},
)
l.metrics.ContractMetrics.ContractDeploymentDuration.calcTurnAroundMetrics()
l.metrics.ContractMetrics.ContractDeploymentDuration.TotalExecTime = end.Sub(start)
return nil
}