8
8
"crypto/ecdsa"
9
9
"crypto/rand"
10
10
"fmt"
11
- "log"
12
11
"math/big"
13
12
"path/filepath"
14
13
"time"
@@ -18,6 +17,7 @@ import (
18
17
"github.com/ethereum/go-ethereum/common"
19
18
"github.com/ethereum/go-ethereum/crypto"
20
19
"github.com/stretchr/testify/require"
20
+ "go.uber.org/zap"
21
21
22
22
"github.com/ava-labs/avalanchego/ids"
23
23
"github.com/ava-labs/avalanchego/tests/antithesis"
@@ -31,13 +31,15 @@ import (
31
31
"github.com/ava-labs/subnet-evm/tests/utils"
32
32
33
33
ago_tests "github.com/ava-labs/avalanchego/tests"
34
+ "github.com/ava-labs/avalanchego/utils/logging"
34
35
timerpkg "github.com/ava-labs/avalanchego/utils/timer"
35
36
)
36
37
37
38
const NumKeys = 5
38
39
39
40
func main () {
40
- tc := ago_tests .NewTestContext ()
41
+ logger := ago_tests .NewDefaultLogger ("" )
42
+ tc := ago_tests .NewTestContext (logger )
41
43
defer tc .Cleanup ()
42
44
require := require .New (tc )
43
45
@@ -60,7 +62,9 @@ func main() {
60
62
ctx := ago_tests .DefaultNotifyContext (c .Duration , tc .DeferCleanup )
61
63
62
64
require .Len (c .ChainIDs , 1 )
63
- log .Printf ("CHAIN IDS: %v" , c .ChainIDs )
65
+ logger .Info ("Starting testing" ,
66
+ zap .Strings ("chainIDs" , c .ChainIDs ),
67
+ )
64
68
chainID , err := ids .FromString (c .ChainIDs [0 ])
65
69
require .NoError (err , "failed to parse chainID" )
66
70
@@ -69,6 +73,7 @@ func main() {
69
73
genesisKey := tmpnet .HardhatKey .ToECDSA ()
70
74
genesisWorkload := & workload {
71
75
id : 0 ,
76
+ log : ago_tests .NewDefaultLogger (fmt .Sprintf ("worker %d" , 0 )),
72
77
client : genesisClient ,
73
78
key : genesisKey ,
74
79
uris : c .URIs ,
@@ -82,13 +87,14 @@ func main() {
82
87
key , err := crypto .ToECDSA (crypto .Keccak256 ([]byte {uint8 (i )}))
83
88
require .NoError (err , "failed to generate key" )
84
89
85
- require .NoError (transferFunds (ctx , genesisClient , genesisKey , crypto .PubkeyToAddress (key .PublicKey ), initialAmount ))
90
+ require .NoError (transferFunds (ctx , genesisClient , genesisKey , crypto .PubkeyToAddress (key .PublicKey ), initialAmount , logger ))
86
91
87
92
client , err := ethclient .Dial (getChainURI (c .URIs [i % len (c .URIs )], chainID .String ()))
88
93
require .NoError (err , "failed to dial chain" )
89
94
90
95
workloads [i ] = & workload {
91
96
id : i ,
97
+ log : ago_tests .NewDefaultLogger (fmt .Sprintf ("worker %d" , i )),
92
98
client : client ,
93
99
key : key ,
94
100
uris : c .URIs ,
@@ -109,14 +115,15 @@ func main() {
109
115
type workload struct {
110
116
id int
111
117
client ethclient.Client
118
+ log logging.Logger
112
119
key * ecdsa.PrivateKey
113
120
uris []string
114
121
}
115
122
116
123
func (w * workload ) run (ctx context.Context ) {
117
124
timer := timerpkg .StoppedTimer ()
118
125
119
- tc := ago_tests .NewTestContext ()
126
+ tc := ago_tests .NewTestContext (w . log )
120
127
defer tc .Cleanup ()
121
128
require := require .New (tc )
122
129
@@ -132,12 +139,14 @@ func (w *workload) run(ctx context.Context) {
132
139
for {
133
140
// TODO(marun) Exercise a wider variety of transactions
134
141
recipientEthAddress := crypto .PubkeyToAddress (w .key .PublicKey )
135
- err := transferFunds (ctx , w .client , w .key , recipientEthAddress , txAmount )
142
+ err := transferFunds (ctx , w .client , w .key , recipientEthAddress , txAmount , w . log )
136
143
if err != nil {
137
144
// Log the error and continue since the problem may be
138
145
// transient. require.NoError is only for errors that should stop
139
146
// execution.
140
- log .Printf ("failed to transfer funds: %s" , err )
147
+ w .log .Info ("failed to transfer funds" ,
148
+ zap .Error (err ),
149
+ )
141
150
}
142
151
143
152
val , err := rand .Int (rand .Reader , big .NewInt (int64 (time .Second )))
@@ -156,7 +165,7 @@ func getChainURI(nodeURI string, blockchainID string) string {
156
165
return fmt .Sprintf ("%s/ext/bc/%s/rpc" , nodeURI , blockchainID )
157
166
}
158
167
159
- func transferFunds (ctx context.Context , client ethclient.Client , key * ecdsa.PrivateKey , recipientAddress common.Address , txAmount uint64 ) error {
168
+ func transferFunds (ctx context.Context , client ethclient.Client , key * ecdsa.PrivateKey , recipientAddress common.Address , txAmount uint64 , log logging. Logger ) error {
160
169
chainID , err := client .ChainID (ctx )
161
170
if err != nil {
162
171
return fmt .Errorf ("failed to fetch chainID: %w" , err )
@@ -188,17 +197,17 @@ func transferFunds(ctx context.Context, client ethclient.Client, key *ecdsa.Priv
188
197
return fmt .Errorf ("failed to format transaction: %w" , err )
189
198
}
190
199
191
- log .Printf ("sending transaction with ID %s and nonce %d \n " , tx .Hash (), acceptedNonce )
200
+ log .Info ("sending transaction" , zap . Stringer ( "txID" , tx .Hash ()), zap . Uint64 ( "nonce" , acceptedNonce ) )
192
201
err = client .SendTransaction (ctx , tx )
193
202
if err != nil {
194
203
return fmt .Errorf ("failed to send transaction: %w" , err )
195
204
}
196
205
197
- log .Printf ("waiting for acceptance of transaction with ID %s \n " , tx .Hash ())
206
+ log .Info ("waiting for acceptance of transaction" , zap . Stringer ( "txID" , tx .Hash () ))
198
207
if _ , err := bind .WaitMined (ctx , client , tx ); err != nil {
199
208
return fmt .Errorf ("failed to wait for receipt: %v" , err )
200
209
}
201
- log .Printf ("confirmed acceptance of transaction with ID %s \n " , tx .Hash ())
210
+ log .Info ("confirmed acceptance of transaction" , zap . Stringer ( "txID" , tx .Hash () ))
202
211
203
212
return nil
204
213
}
0 commit comments