@@ -100,22 +100,29 @@ func TestWaitDeployed(t *testing.T) {
100100}
101101
102102func TestWaitDeployedCornerCases (t * testing.T ) {
103- backend := simulated .NewBackend (
104- types.GenesisAlloc {
105- crypto .PubkeyToAddress (testKey .PublicKey ): {Balance : big .NewInt (10000000000000000 )},
106- },
103+ var (
104+ backend = simulated .NewBackend (
105+ types.GenesisAlloc {
106+ crypto .PubkeyToAddress (testKey .PublicKey ): {Balance : big .NewInt (10000000000000000 )},
107+ },
108+ )
109+ head , _ = backend .Client ().HeaderByNumber (t .Context (), nil ) // Should be child's, good enough
110+ gasPrice = new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
111+ signer = types .LatestSigner (params .AllDevChainProtocolChanges )
112+ code = common .FromHex ("6060604052600a8060106000396000f360606040526008565b00" )
113+ ctx , cancel = context .WithCancel (t .Context ())
107114 )
108115 defer backend .Close ()
109116
110- head , _ := backend . Client (). HeaderByNumber ( context . Background (), nil ) // Should be child's, good enough
111- gasPrice := new (big. Int ). Add ( head . BaseFee , big . NewInt ( 1 ))
112-
113- // Create a transaction to an account.
114- code := "6060604052600a8060106000396000f360606040526008565b00"
115- tx := types . NewTransaction ( 0 , common . HexToAddress ( "0x01" ), big . NewInt ( 0 ), 3000000 , gasPrice , common . FromHex ( code ))
116- tx , _ = types . SignTx ( tx , types . LatestSigner ( params . AllDevChainProtocolChanges ), testKey )
117- ctx , cancel := context . WithCancel ( context . Background ())
118- defer cancel ( )
117+ // 1. WaitDeploy on a transaction that does not deploy a contract, verify it
118+ // returns an error.
119+ tx := types . MustSignNewTx ( testKey , signer , & types. LegacyTx {
120+ Nonce : 0 ,
121+ To : & common. Address { 0x01 },
122+ Gas : 300000 ,
123+ GasPrice : gasPrice ,
124+ Data : code ,
125+ } )
119126 if err := backend .Client ().SendTransaction (ctx , tx ); err != nil {
120127 t .Errorf ("failed to send transaction: %q" , err )
121128 }
@@ -124,19 +131,35 @@ func TestWaitDeployedCornerCases(t *testing.T) {
124131 t .Errorf ("error mismatch: want %q, got %q, " , bind .ErrNoAddressInReceipt , err )
125132 }
126133
127- // Create a transaction that is not mined.
128- tx = types .NewContractCreation (1 , big .NewInt (0 ), 3000000 , gasPrice , common .FromHex (code ))
129- tx , _ = types .SignTx (tx , types .LatestSigner (params .AllDevChainProtocolChanges ), testKey )
130-
134+ // 2. Create a contract, but cancel the WaitDeploy before it is mined.
135+ tx = types .MustSignNewTx (testKey , signer , & types.LegacyTx {
136+ Nonce : 1 ,
137+ Gas : 300000 ,
138+ GasPrice : gasPrice ,
139+ Data : code ,
140+ })
141+
142+ // Wait in another thread so that we can quickly cancel it after submitting
143+ // the transaction.
144+ done := make (chan struct {})
131145 go func () {
132- contextCanceled := errors .New ("context canceled" )
133- if _ , err := bind .WaitDeployed (ctx , backend .Client (), tx .Hash ()); err .Error () != contextCanceled .Error () {
134- t .Errorf ("error mismatch: want %q, got %q, " , contextCanceled , err )
146+ defer close (done )
147+ want := errors .New ("context canceled" )
148+ _ , err := bind .WaitDeployed (ctx , backend .Client (), tx .Hash ())
149+ if err == nil || errors .Is (want , err ) {
150+ t .Errorf ("error mismatch: want %v, got %v" , want , err )
135151 }
136152 }()
137153
138154 if err := backend .Client ().SendTransaction (ctx , tx ); err != nil {
139155 t .Errorf ("failed to send transaction: %q" , err )
140156 }
141157 cancel ()
158+
159+ // Wait for goroutine to exit or for a timeout.
160+ select {
161+ case <- done :
162+ case <- time .After (time .Second * 2 ):
163+ t .Fatalf ("failed to cancel wait deploy" )
164+ }
142165}
0 commit comments