util_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "math/big"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  22. "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "golang.org/x/net/context"
  28. )
  29. var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  30. var waitDeployedTests = map[string]struct {
  31. code string
  32. gas *big.Int
  33. wantAddress common.Address
  34. wantErr error
  35. }{
  36. "successful deploy": {
  37. code: `6060604052600a8060106000396000f360606040526008565b00`,
  38. gas: big.NewInt(3000000),
  39. wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
  40. },
  41. "empty code": {
  42. code: ``,
  43. gas: big.NewInt(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.GenesisAccount{
  51. Address: crypto.PubkeyToAddress(testKey.PublicKey),
  52. Balance: big.NewInt(10000000000),
  53. })
  54. // Create the transaction.
  55. tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
  56. tx, _ = tx.SignECDSA(types.HomesteadSigner{}, testKey)
  57. // Wait for it to get mined in the background.
  58. var (
  59. err error
  60. address common.Address
  61. mined = make(chan struct{})
  62. ctx = context.Background()
  63. )
  64. go func() {
  65. address, err = bind.WaitDeployed(ctx, backend, tx)
  66. close(mined)
  67. }()
  68. // Send and mine the transaction.
  69. backend.SendTransaction(ctx, tx)
  70. backend.Commit()
  71. select {
  72. case <-mined:
  73. if err != test.wantErr {
  74. t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr)
  75. }
  76. if address != test.wantAddress {
  77. t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
  78. }
  79. case <-time.After(2 * time.Second):
  80. t.Errorf("test %q: timeout", name)
  81. }
  82. }
  83. }