util_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(10000000000000000)},
  54. },
  55. 10000000,
  56. )
  57. defer backend.Close()
  58. // Create the transaction
  59. head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
  60. gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
  61. tx := types.NewContractCreation(0, big.NewInt(0), test.gas, gasPrice, common.FromHex(test.code))
  62. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  63. // Wait for it to get mined in the background.
  64. var (
  65. err error
  66. address common.Address
  67. mined = make(chan struct{})
  68. ctx = context.Background()
  69. )
  70. go func() {
  71. address, err = bind.WaitDeployed(ctx, backend, tx)
  72. close(mined)
  73. }()
  74. // Send and mine the transaction.
  75. backend.SendTransaction(ctx, tx)
  76. backend.Commit()
  77. select {
  78. case <-mined:
  79. if err != test.wantErr {
  80. t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err)
  81. }
  82. if address != test.wantAddress {
  83. t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
  84. }
  85. case <-time.After(2 * time.Second):
  86. t.Errorf("test %q: timeout", name)
  87. }
  88. }
  89. }
  90. func TestWaitDeployedCornerCases(t *testing.T) {
  91. backend := backends.NewSimulatedBackend(
  92. core.GenesisAlloc{
  93. crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},
  94. },
  95. 10000000,
  96. )
  97. defer backend.Close()
  98. head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
  99. gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
  100. // Create a transaction to an account.
  101. code := "6060604052600a8060106000396000f360606040526008565b00"
  102. tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
  103. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  104. ctx, cancel := context.WithCancel(context.Background())
  105. defer cancel()
  106. backend.SendTransaction(ctx, tx)
  107. backend.Commit()
  108. notContentCreation := errors.New("tx is not contract creation")
  109. if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() {
  110. t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err)
  111. }
  112. // Create a transaction that is not mined.
  113. tx = types.NewContractCreation(1, big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
  114. tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
  115. go func() {
  116. contextCanceled := errors.New("context canceled")
  117. if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() {
  118. t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err)
  119. }
  120. }()
  121. backend.SendTransaction(ctx, tx)
  122. cancel()
  123. }