@@ -42,26 +42,27 @@ import (
4242 "github.com/ava-labs/subnet-evm/params"
4343)
4444
45- var testKey , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
46-
47- var waitDeployedTests = map [string ]struct {
48- code string
49- gas uint64
50- wantAddress common.Address
51- wantErr error
52- }{
53- "successful deploy" : {
54- code : `6060604052600a8060106000396000f360606040526008565b00` ,
55- gas : 3000000 ,
56- wantAddress : common .HexToAddress ("0x3a220f351252089d385b29beca14e27f204c296a" ),
57- },
58- "empty code" : {
59- code : `` ,
60- gas : 300000 ,
61- wantErr : bind .ErrNoCodeAfterDeploy ,
62- wantAddress : common .HexToAddress ("0x3a220f351252089d385b29beca14e27f204c296a" ),
63- },
64- }
45+ var (
46+ testKey , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
47+ waitDeployedTests = map [string ]struct {
48+ code string
49+ gas uint64
50+ wantAddress common.Address
51+ wantErr error
52+ }{
53+ "successful deploy" : {
54+ code : `6060604052600a8060106000396000f360606040526008565b00` ,
55+ gas : 3000000 ,
56+ wantAddress : common .HexToAddress ("0x3a220f351252089d385b29beca14e27f204c296a" ),
57+ },
58+ "empty code" : {
59+ code : `` ,
60+ gas : 300000 ,
61+ wantErr : bind .ErrNoCodeAfterDeploy ,
62+ wantAddress : common .HexToAddress ("0x3a220f351252089d385b29beca14e27f204c296a" ),
63+ },
64+ }
65+ )
6566
6667func TestWaitDeployed (t * testing.T ) {
6768 t .Parallel ()
@@ -94,7 +95,7 @@ func TestWaitDeployed(t *testing.T) {
9495
9596 // Send and mine the transaction.
9697 if err := backend .Client ().SendTransaction (ctx , tx ); err != nil {
97- t .Fatalf ( "Failed to send transaction: %s" , err )
98+ t .Errorf ( "test %q: failed to send transaction: %v" , name , err )
9899 }
99100 backend .Commit (true )
100101
@@ -113,44 +114,66 @@ func TestWaitDeployed(t *testing.T) {
113114}
114115
115116func TestWaitDeployedCornerCases (t * testing.T ) {
116- backend := simulated .NewBackend (
117- types.GenesisAlloc {
118- crypto .PubkeyToAddress (testKey .PublicKey ): {Balance : big .NewInt (1000000000000000000 )},
119- },
117+ var (
118+ backend = simulated .NewBackend (
119+ types.GenesisAlloc {
120+ crypto .PubkeyToAddress (testKey .PublicKey ): {Balance : big .NewInt (1000000000000000000 )},
121+ },
122+ )
123+ head , _ = backend .Client ().HeaderByNumber (t .Context (), nil ) // Should be child's, good enough
124+ gasPrice = new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
125+ signer = types .LatestSignerForChainID (big .NewInt (1337 ))
126+ code = common .FromHex ("6060604052600a8060106000396000f360606040526008565b00" )
127+ ctx , cancel = context .WithCancel (t .Context ())
120128 )
121129 defer backend .Close ()
122130
123- head , _ := backend . Client (). HeaderByNumber ( context . Background (), nil ) // Should be child's, good enough
124- gasPrice := new (big. Int ). Add ( head . BaseFee , big . NewInt ( 1 ))
125-
126- // Create a transaction to an account.
127- code := "6060604052600a8060106000396000f360606040526008565b00"
128- tx := types . NewTransaction ( 0 , common . HexToAddress ( "0x01" ), big . NewInt ( 0 ), 3000000 , gasPrice , common . FromHex ( code ))
129- tx , _ = types . SignTx ( tx , types . LatestSignerForChainID ( big . NewInt ( 1337 )), testKey )
130- ctx , cancel := context . WithCancel ( context . Background ())
131- defer cancel ( )
131+ // 1. WaitDeploy on a transaction that does not deploy a contract, verify it
132+ // returns an error.
133+ tx := types . MustSignNewTx ( testKey , signer , & types. LegacyTx {
134+ Nonce : 0 ,
135+ To : & common. Address { 0x01 },
136+ Gas : 300000 ,
137+ GasPrice : gasPrice ,
138+ Data : code ,
139+ } )
132140 if err := backend .Client ().SendTransaction (ctx , tx ); err != nil {
133- t .Fatalf ( "Failed to send transaction: %s " , err )
141+ t .Errorf ( "failed to send transaction: %q " , err )
134142 }
135143 backend .Commit (true )
136144 notContractCreation := errors .New ("tx is not contract creation" )
137145 if _ , err := bind .WaitDeployed (ctx , backend .Client (), tx ); err .Error () != notContractCreation .Error () {
138146 t .Errorf ("error mismatch: want %q, got %q, " , notContractCreation , err )
139147 }
140148
141- // Create a transaction that is not mined.
142- tx = types .NewContractCreation (1 , big .NewInt (0 ), 3000000 , gasPrice , common .FromHex (code ))
143- tx , _ = types .SignTx (tx , types .LatestSignerForChainID (big .NewInt (1337 )), testKey )
144-
149+ // 2. Create a contract, but cancel the WaitDeploy before it is mined.
150+ tx = types .MustSignNewTx (testKey , signer , & types.LegacyTx {
151+ Nonce : 1 ,
152+ Gas : 300000 ,
153+ GasPrice : gasPrice ,
154+ Data : code ,
155+ })
156+
157+ // Wait in another thread so that we can quickly cancel it after submitting
158+ // the transaction.
159+ done := make (chan struct {})
145160 go func () {
146- contextCanceled := errors .New ("context canceled" )
147- if _ , err := bind .WaitDeployed (ctx , backend .Client (), tx ); err .Error () != contextCanceled .Error () {
148- t .Errorf ("error mismatch: want %q, got %q, " , contextCanceled , err )
161+ defer close (done )
162+ _ , err := bind .WaitDeployed (ctx , backend .Client (), tx )
163+ if ! errors .Is (err , context .Canceled ) {
164+ t .Errorf ("error mismatch: want %v, got %v" , context .Canceled , err )
149165 }
150166 }()
151167
152168 if err := backend .Client ().SendTransaction (ctx , tx ); err != nil {
153- t .Fatalf ( "Failed to send transaction: %s " , err )
169+ t .Errorf ( "failed to send transaction: %q " , err )
154170 }
155171 cancel ()
172+
173+ // Wait for goroutine to exit or for a timeout.
174+ select {
175+ case <- done :
176+ case <- time .After (time .Second * 2 ):
177+ t .Fatalf ("failed to cancel wait deploy" )
178+ }
156179}
0 commit comments