util_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(
  51. core.GenesisAlloc{
  52. crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
  53. },
  54. 10000000,
  55. )
  56. defer backend.Close()
  57. // Create the transaction.
  58. tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
  59. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  60. // Wait for it to get mined in the background.
  61. var (
  62. err error
  63. address common.Address
  64. mined = make(chan struct{})
  65. ctx = context.Background()
  66. )
  67. go func() {
  68. address, err = bind.WaitDeployed(ctx, backend, tx)
  69. close(mined)
  70. }()
  71. // Send and mine the transaction.
  72. backend.SendTransaction(ctx, tx)
  73. backend.Commit()
  74. select {
  75. case <-mined:
  76. if err != test.wantErr {
  77. t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr)
  78. }
  79. if address != test.wantAddress {
  80. t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
  81. }
  82. case <-time.After(2 * time.Second):
  83. t.Errorf("test %q: timeout", name)
  84. }
  85. }
  86. }