This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contract, swap: refactor test backend (#2092)
- Loading branch information
1 parent
c79587c
commit 052e345
Showing
9 changed files
with
124 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package chain | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
var ( | ||
// ErrTransactionReverted is given when the transaction that cashes a cheque is reverted | ||
ErrTransactionReverted = errors.New("Transaction reverted") | ||
) | ||
|
||
// Backend is the minimum amount of functionality required by the underlying ethereum backend | ||
type Backend interface { | ||
bind.ContractBackend | ||
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) | ||
TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) | ||
} | ||
|
||
// WaitMined waits until either the transaction with the given hash has been mined or the context is cancelled | ||
// this is an adapted version of go-ethereums bind.WaitMined | ||
func WaitMined(ctx context.Context, b Backend, hash common.Hash) (*types.Receipt, error) { | ||
for { | ||
receipt, err := b.TransactionReceipt(ctx, hash) | ||
if err != nil { | ||
log.Error("receipt retrieval failed", "err", err) | ||
} | ||
if receipt != nil { | ||
if receipt.Status != types.ReceiptStatusSuccessful { | ||
return nil, ErrTransactionReverted | ||
} | ||
return receipt, nil | ||
} | ||
|
||
log.Trace("transaction not yet mined", "tx", hash) | ||
// Wait for the next round. | ||
select { | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
case <-time.After(1 * time.Second): | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
// TestBackend is the backend to use for tests with a simulated backend | ||
type TestBackend struct { | ||
*backends.SimulatedBackend | ||
} | ||
|
||
// SendTransaction adds a commit after a successful send | ||
func (b *TestBackend) SendTransaction(ctx context.Context, tx *types.Transaction) (err error) { | ||
err = b.SimulatedBackend.SendTransaction(ctx, tx) | ||
if err == nil { | ||
b.SimulatedBackend.Commit() | ||
} | ||
return err | ||
} | ||
|
||
// Close overrides the Close function of the underlying SimulatedBackend so that it does nothing | ||
// This allows the same SimulatedBackend backend to be reused across tests | ||
// This is necessary due to some memory leakage issues with the used version of the SimulatedBackend | ||
func (b *TestBackend) Close() { | ||
|
||
} | ||
|
||
// NewTestBackend returns a new TestBackend for the given SimulatedBackend | ||
// It also causes an initial commit to make sure that genesis accounts are set up | ||
func NewTestBackend(backend *backends.SimulatedBackend) *TestBackend { | ||
backend.Commit() | ||
return &TestBackend{ | ||
SimulatedBackend: backend, | ||
} | ||
} |
Oops, something went wrong.