util_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "errors"
  20. "math/big"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  24. "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. )
  30. var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  31. var waitDeployedTests = map[string]struct {
  32. code string
  33. gas uint64
  34. wantAddress common.Address
  35. wantErr error
  36. }{
  37. "successful deploy": {
  38. code: `6060604052600a8060106000396000f360606040526008565b00`,
  39. gas: 3000000,
  40. wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
  41. },
  42. "empty code": {
  43. code: ``,
  44. gas: 300000,
  45. wantErr: bind.ErrNoCodeAfterDeploy,
  46. wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
  47. },
  48. }
  49. func TestWaitDeployed(t *testing.T) {
  50. for name, test := range waitDeployedTests {
  51. backend := backends.NewSimulatedBackend(
  52. core.GenesisAlloc{
  53. crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
  54. },
  55. 10000000,
  56. )
  57. defer backend.Close()
  58. // Create the transaction.
  59. tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
  60. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  61. // Wait for it to get mined in the background.
  62. var (
  63. err error
  64. address common.Address
  65. mined = make(chan struct{})
  66. ctx = context.Background()
  67. )
  68. go func() {
  69. address, err = bind.WaitDeployed(ctx, backend, tx)
  70. close(mined)
  71. }()
  72. // Send and mine the transaction.
  73. backend.SendTransaction(ctx, tx, bind.PrivateTxArgs{})
  74. backend.Commit()
  75. select {
  76. case <-mined:
  77. if err != test.wantErr {
  78. t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err)
  79. }
  80. if address != test.wantAddress {
  81. t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
  82. }
  83. case <-time.After(2 * time.Second):
  84. t.Errorf("test %q: timeout", name)
  85. }
  86. }
  87. }
  88. func TestWaitDeployedCornerCases(t *testing.T) {
  89. backend := backends.NewSimulatedBackend(
  90. core.GenesisAlloc{
  91. crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
  92. },
  93. 10000000,
  94. )
  95. defer backend.Close()
  96. // Create a transaction to an account.
  97. code := "6060604052600a8060106000396000f360606040526008565b00"
  98. tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, big.NewInt(1), common.FromHex(code))
  99. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  100. ctx, cancel := context.WithCancel(context.Background())
  101. defer cancel()
  102. backend.SendTransaction(ctx, tx, bind.PrivateTxArgs{})
  103. backend.Commit()
  104. notContentCreation := errors.New("tx is not contract creation")
  105. if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() {
  106. t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err)
  107. }
  108. // Create a transaction that is not mined.
  109. tx = types.NewContractCreation(1, big.NewInt(0), 3000000, big.NewInt(1), common.FromHex(code))
  110. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  111. go func() {
  112. contextCanceled := errors.New("context canceled")
  113. if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() {
  114. t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err)
  115. }
  116. }()
  117. backend.SendTransaction(ctx, tx, bind.PrivateTxArgs{})
  118. cancel()
  119. }