Skip to content

Commit

Permalink
Add ReentryAttackTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Aug 2, 2021
1 parent 23e6f1e commit 775ae96
Show file tree
Hide file tree
Showing 12 changed files with 992 additions and 87 deletions.
12 changes: 8 additions & 4 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -106,7 +107,7 @@ type rawWallet struct {
type ethFetcher interface {
accounts() []*accounts.Account
addPeer(ctx context.Context, peer string) error
balance(ctx context.Context, acct *accounts.Account) (*big.Int, error)
balance(ctx context.Context, addr common.Address) (*big.Int, error)
bestBlockHash(ctx context.Context) (common.Hash, error)
bestHeader(ctx context.Context) (*types.Header, error)
block(ctx context.Context, hash common.Hash) (*types.Block, error)
Expand Down Expand Up @@ -235,7 +236,10 @@ func (eth *ExchangeWallet) OwnsAddress(address string) (bool, error) {
//
// TODO: Return Immature and Locked values.
func (eth *ExchangeWallet) Balance() (*asset.Balance, error) {
bigBal, err := eth.node.balance(eth.ctx, eth.acct)
if eth.acct == nil {
return nil, errors.New("account not set")
}
bigBal, err := eth.node.balance(eth.ctx, eth.acct.Address)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -400,12 +404,12 @@ func (*ExchangeWallet) PayFee(address string, regFee uint64) (asset.Coin, error)
}

// sendToAddr sends funds from acct to addr.
func (eth *ExchangeWallet) sendToAddr(addr common.Address, amt, gasFee *big.Int) (common.Hash, error) {
func (eth *ExchangeWallet) sendToAddr(addr common.Address, amt, gasPrice *big.Int) (common.Hash, error) {
tx := map[string]string{
"from": fmt.Sprintf("0x%x", eth.acct.Address),
"to": fmt.Sprintf("0x%x", addr),
"value": fmt.Sprintf("0x%x", amt),
"gasPrice": fmt.Sprintf("0x%x", gasFee),
"gasPrice": fmt.Sprintf("0x%x", gasPrice),
}
return eth.node.sendTransaction(eth.ctx, tx)
}
Expand Down
3 changes: 2 additions & 1 deletion client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (n *testNode) block(ctx context.Context, hash common.Hash) (*types.Block, e
func (n *testNode) accounts() []*accounts.Account {
return nil
}
func (n *testNode) balance(ctx context.Context, acct *accounts.Account) (*big.Int, error) {
func (n *testNode) balance(ctx context.Context, acct common.Address) (*big.Int, error) {
return n.bal, n.balErr
}
func (n *testNode) sendTransaction(ctx context.Context, tx map[string]string) (common.Hash, error) {
Expand Down Expand Up @@ -330,6 +330,7 @@ func TestBalance(t *testing.T) {
node: node,
ctx: ctx,
log: tLogger,
acct: new(accounts.Account),
}
bal, err := eth.Balance()
cancel()
Expand Down
32 changes: 17 additions & 15 deletions client/asset/eth/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func (c *rpcclient) accounts() []*accounts.Account {
return accts
}

// balance gets the current balance of an account.
func (c *rpcclient) balance(ctx context.Context, acct *accounts.Account) (*big.Int, error) {
return c.ec.BalanceAt(ctx, acct.Address, nil)
// balance gets the current balance of an address.
func (c *rpcclient) balance(ctx context.Context, addr common.Address) (*big.Int, error) {
return c.ec.BalanceAt(ctx, addr, nil)
}

// unlock uses a raw request to unlock an account indefinitely.
Expand Down Expand Up @@ -234,44 +234,46 @@ func (c *rpcclient) wallet(acct accounts.Account) (accounts.Wallet, error) {
return wallet, nil
}

func (c *rpcclient) addSignerToOpts(txOpts *bind.TransactOpts, netID int64) error {
wallet, err := c.wallet(accounts.Account{Address: txOpts.From})
if err != nil {
return err
}
txOpts.Signer = func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
return wallet.SignTx(accounts.Account{Address: addr}, tx, big.NewInt(netID))
}
return nil
}

// initiate creates a swap contract. The initiator will be the account at
// txOpts.From. Any on-chain failure, such as this secret hash already existing
// in the swaps map, will not cause this to error.
func (c *rpcclient) initiate(txOpts *bind.TransactOpts, netID int64, refundTimestamp int64, secretHash [32]byte, participant common.Address) (*types.Transaction, error) {
wallet, err := c.wallet(accounts.Account{Address: txOpts.From})
err := c.addSignerToOpts(txOpts, netID)
if err != nil {
return nil, err
}
txOpts.Signer = func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
return wallet.SignTx(accounts.Account{Address: addr}, tx, big.NewInt(netID))
}
return c.es.Initiate(txOpts, big.NewInt(refundTimestamp), secretHash, participant)
}

// redeem redeems a swap contract. The redeemer will be the account at txOpts.From.
// Any on-chain failure, such as this secret not matching the hash, will not cause
// this to error.
func (c *rpcclient) redeem(txOpts *bind.TransactOpts, netID int64, secret, secretHash [32]byte) (*types.Transaction, error) {
wallet, err := c.wallet(accounts.Account{Address: txOpts.From})
err := c.addSignerToOpts(txOpts, netID)
if err != nil {
return nil, err
}
txOpts.Signer = func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
return wallet.SignTx(accounts.Account{Address: addr}, tx, big.NewInt(netID))
}
return c.es.Redeem(txOpts, secret, secretHash)
}

// refund refunds a swap contract. The refunder will be the account at txOpts.From.
// Any on-chain failure, such as the locktime not being past, will not cause
// this to error.
func (c *rpcclient) refund(txOpts *bind.TransactOpts, netID int64, secretHash [32]byte) (*types.Transaction, error) {
wallet, err := c.wallet(accounts.Account{Address: txOpts.From})
err := c.addSignerToOpts(txOpts, netID)
if err != nil {
return nil, err
}
txOpts.Signer = func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
return wallet.SignTx(accounts.Account{Address: addr}, tx, big.NewInt(netID))
}
return c.es.Refund(txOpts, secretHash)
}
Loading

0 comments on commit 775ae96

Please sign in to comment.