util_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bind_test
  17. import (
  18. "context"
  19. "math/big"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  23. "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. )
  29. var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  30. var waitDeployedTests = map[string]struct {
  31. code string
  32. gas uint64
  33. wantAddress common.Address
  34. wantErr error
  35. }{
  36. "successful deploy": {
  37. code: `6060604052600a8060106000396000f360606040526008565b00`,
  38. gas: 3000000,
  39. wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
  40. },
  41. "empty code": {
  42. code: ``,
  43. gas: 300000,
  44. wantErr: bind.ErrNoCodeAfterDeploy,
  45. wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
  46. },
  47. }
  48. func TestWaitDeployed(t *testing.T) {
  49. for name, test := range waitDeployedTests {
  50. backend := backends.NewSimulatedBackend(core.GenesisAlloc{
  51. crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
  52. })
  53. // Create the transaction.
  54. tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
  55. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  56. // Wait for it to get mined in the background.
  57. var (
  58. err error
  59. address common.Address
  60. mined = make(chan struct{})
  61. ctx = context.Background()
  62. )
  63. go func() {
  64. address, err = bind.WaitDeployed(ctx, backend, tx)
  65. close(mined)
  66. }()
  67. // Send and mine the transaction.
  68. backend.SendTransaction(ctx, tx)
  69. backend.Commit()
  70. select {
  71. case <-mined:
  72. if err != test.wantErr {
  73. t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr)
  74. }
  75. if address != test.wantAddress {
  76. t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
  77. }
  78. case <-time.After(2 * time.Second):
  79. t.Errorf("test %q: timeout", name)
  80. }
  81. }
  82. }