Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
swap: pass depositAmount to testDeploy (#2061)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler authored Dec 23, 2019
1 parent 1a60969 commit 9cdd812
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 56 deletions.
32 changes: 18 additions & 14 deletions swap/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -49,10 +50,10 @@ type swapTester struct {
}

// creates a new protocol tester for swap with a deployed chequebook
func newSwapTester(t *testing.T, backend *swapTestBackend) (*swapTester, func(), error) {
func newSwapTester(t *testing.T, backend *swapTestBackend, depositAmount *big.Int) (*swapTester, func(), error) {
swap, clean := newTestSwap(t, ownerKey, backend)

err := testDeploy(context.Background(), swap)
err := testDeploy(context.Background(), swap, depositAmount)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -134,7 +135,7 @@ func correctSwapHandshakeMsg(swap *Swap) *HandshakeMsg {
// TestHandshake tests the correct handshake scenario
func TestHandshake(t *testing.T) {
// setup the protocolTester, which will allow protocol testing by sending messages
protocolTester, clean, err := newSwapTester(t, nil)
protocolTester, clean, err := newSwapTester(t, nil, big.NewInt(0))
defer clean()
if err != nil {
t.Fatal(err)
Expand All @@ -152,7 +153,7 @@ func TestHandshake(t *testing.T) {
// TestHandshakeInvalidChainID tests that a handshake with the wrong chain id is rejected
func TestHandshakeInvalidChainID(t *testing.T) {
// setup the protocolTester, which will allow protocol testing by sending messages
protocolTester, clean, err := newSwapTester(t, nil)
protocolTester, clean, err := newSwapTester(t, nil, big.NewInt(0))
defer clean()
if err != nil {
t.Fatal(err)
Expand All @@ -174,7 +175,7 @@ func TestHandshakeInvalidChainID(t *testing.T) {
// TestHandshakeEmptyContract tests that a handshake with an empty contract address is rejected
func TestHandshakeEmptyContract(t *testing.T) {
// setup the protocolTester, which will allow protocol testing by sending messages
protocolTester, clean, err := newSwapTester(t, nil)
protocolTester, clean, err := newSwapTester(t, nil, big.NewInt(0))
defer clean()
if err != nil {
t.Fatal(err)
Expand All @@ -196,7 +197,7 @@ func TestHandshakeEmptyContract(t *testing.T) {
// TestHandshakeInvalidContract tests that a handshake with an address that's not a valid chequebook
func TestHandshakeInvalidContract(t *testing.T) {
// setup the protocolTester, which will allow protocol testing by sending messages
protocolTester, clean, err := newSwapTester(t, nil)
protocolTester, clean, err := newSwapTester(t, nil, big.NewInt(0))
defer clean()
if err != nil {
t.Fatal(err)
Expand All @@ -221,7 +222,8 @@ func TestHandshakeInvalidContract(t *testing.T) {
// We send a EmitChequeMsg to the creditor which handles the cheque and sends a ConfirmChequeMsg
func TestEmitCheque(t *testing.T) {
testBackend := newTestBackend(t)
protocolTester, clean, err := newSwapTester(t, testBackend)

protocolTester, clean, err := newSwapTester(t, testBackend, big.NewInt(0))
defer clean()
if err != nil {
t.Fatal(err)
Expand All @@ -238,7 +240,14 @@ func TestEmitCheque(t *testing.T) {
testBackend.cashDone = make(chan struct{})

log.Debug("deploy to simulated backend")
if err := testDeploy(context.Background(), debitorSwap); err != nil {

// cashCheque cashes a cheque when the reward of doing so is twice the transaction costs.
// gasPrice on testBackend == 1
// estimated gas costs == 50000
// cheque should be sent if the accumulated amount of uncashed cheques is worth more than 100000
balance := uint64(100001)

if err := testDeploy(context.Background(), debitorSwap, big.NewInt(int64(balance))); err != nil {
t.Fatal(err)
}

Expand All @@ -250,11 +259,6 @@ func TestEmitCheque(t *testing.T) {
}

debitor := creditorSwap.getPeer(protocolTester.Nodes[0].ID())
// cashCheque cashes a cheque when the reward of doing so is twice the transaction costs.
// gasPrice on testBackend == 1
// estimated gas costs == 50000
// cheque should be sent if the accumulated amount of uncashed cheques is worth more than 100000
balance := uint64(100001)
// set balance artificially
if err = debitor.setBalance(int64(balance)); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -328,7 +332,7 @@ func TestEmitCheque(t *testing.T) {
func TestTriggerPaymentThreshold(t *testing.T) {
testBackend := newTestBackend(t)
log.Debug("create test swap")
protocolTester, clean, err := newSwapTester(t, testBackend)
protocolTester, clean, err := newSwapTester(t, testBackend, big.NewInt(int64(DefaultPaymentThreshold)*2))
defer clean()
if err != nil {
t.Fatal(err)
Expand Down
21 changes: 9 additions & 12 deletions swap/simulations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func newSimServiceMap(params *swapSimulationParams) map[string]simulation.Servic
ts.spec.Hook = protocols.NewAccounting(balance)
ts.swap = balance
// deploy the accounting to the `SimulatedBackend`
err = testDeploy(context.Background(), balance)
err = testDeploy(context.Background(), balance, big.NewInt(100000*int64(RetrieveRequestPrice)))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -190,6 +190,8 @@ func newSharedBackendSwaps(t *testing.T, nodeCount int) (*swapSimulationParams,
alloc := core.GenesisAlloc{}
stores := make(map[int]*state.DBStore)

ethFundingAmount := big.NewInt(9000000000000000000)

// for each node, generate keys, a GenesisAccount and a state store
for i := 0; i < nodeCount; i++ {
key, err := crypto.GenerateKey()
Expand All @@ -198,7 +200,7 @@ func newSharedBackendSwaps(t *testing.T, nodeCount int) (*swapSimulationParams,
}
keys[i] = key
addrs[i] = crypto.PubkeyToAddress(key.PublicKey)
alloc[addrs[i]] = core.GenesisAccount{Balance: big.NewInt(10000 * int64(RetrieveRequestPrice))}
alloc[addrs[i]] = core.GenesisAccount{Balance: ethFundingAmount}
dir, err := ioutil.TempDir("", fmt.Sprintf("swap_test_store_%x", addrs[i].Hex()))
if err != nil {
return nil, err
Expand All @@ -210,32 +212,27 @@ func newSharedBackendSwaps(t *testing.T, nodeCount int) (*swapSimulationParams,
params.dirs[i] = dir
stores[i] = stateStore
}

alloc[ownerAddress] = core.GenesisAccount{Balance: ethFundingAmount}

// then create the single SimulatedBackend
gasLimit := uint64(8000000000)
defaultBackend := backends.NewSimulatedBackend(alloc, gasLimit)
defaultBackend.Commit()

tokenAddress, _, token, err := contractFactory.DeployERC20Mintable(bind.NewKeyedTransactor(keys[0]), defaultBackend)
tokenAddress, _, _, err := contractFactory.DeployERC20Mintable(bind.NewKeyedTransactor(ownerKey), defaultBackend)
if err != nil {
return nil, err
}
defaultBackend.Commit()

for i := 0; i < nodeCount; i++ {
_, err = token.Mint(bind.NewKeyedTransactor(keys[0]), addrs[i], big.NewInt(10000*int64(RetrieveRequestPrice)))
if err != nil {
return nil, err
}
defaultBackend.Commit()
}

factoryAddress, _, _, err := contractFactory.DeploySimpleSwapFactory(bind.NewKeyedTransactor(keys[0]), defaultBackend, tokenAddress)
if err != nil {
t.Fatalf("Error while deploying factory: %v", err)
}
defaultBackend.Commit()

testBackend := &swapTestBackend{SimulatedBackend: defaultBackend, factoryAddress: factoryAddress}
testBackend := &swapTestBackend{SimulatedBackend: defaultBackend, factoryAddress: factoryAddress, tokenAddress: tokenAddress}
// finally, create all Swap instances for each node, which share the same backend
var owner *Owner
defParams := newDefaultParams(t)
Expand Down
63 changes: 33 additions & 30 deletions swap/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,7 @@ func newTestBackend(t *testing.T) *swapTestBackend {

// deploy the ERC20-contract
// ignore receipt because if there is no error, we can assume everything is fine on a simulated backend
tokenAddress, _, token, err := contractFactory.DeployERC20Mintable(bind.NewKeyedTransactor(ownerKey), defaultBackend)
if err != nil {
t.Fatal(err)
}
defaultBackend.Commit()

// mint 1000000000000000000 ERC20-tokens
// ignore receipt because if there is no error, we can assume everything is fine on a simulated backend
_, err = token.Mint(bind.NewKeyedTransactor(ownerKey), ownerAddress, big.NewInt(1000000000000000000))
tokenAddress, _, _, err := contractFactory.DeployERC20Mintable(bind.NewKeyedTransactor(ownerKey), defaultBackend)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -499,7 +491,7 @@ func TestStartChequebookFailure(t *testing.T) {
swap, clean := newTestSwap(t, ownerKey, config.testBackend)
defer clean()
// deploy a chequebook
err := testDeploy(context.TODO(), swap)
err := testDeploy(context.TODO(), swap, big.NewInt(0))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -558,7 +550,7 @@ func TestStartChequebookSuccess(t *testing.T) {
defer clean()

// deploy a chequebook
err := testDeploy(context.TODO(), swap)
err := testDeploy(context.TODO(), swap, big.NewInt(0))
if err != nil {
t.Fatal(err)
}
Expand All @@ -584,7 +576,7 @@ func TestStartChequebookSuccess(t *testing.T) {
defer clean()

// deploy a chequebook
err := testDeploy(context.TODO(), swap)
err := testDeploy(context.TODO(), swap, big.NewInt(0))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -616,7 +608,7 @@ func TestStartChequebookSuccess(t *testing.T) {
func TestDisconnectThreshold(t *testing.T) {
swap, clean := newTestSwap(t, ownerKey, nil)
defer clean()
testDeploy(context.Background(), swap)
testDeploy(context.Background(), swap, big.NewInt(0))

testPeer := newDummyPeer()
swap.addPeer(testPeer.Peer, swap.owner.address, swap.GetParams().ContractAddress)
Expand All @@ -642,7 +634,7 @@ func TestDisconnectThreshold(t *testing.T) {
func TestPaymentThreshold(t *testing.T) {
swap, clean := newTestSwap(t, ownerKey, nil)
defer clean()
testDeploy(context.Background(), swap)
testDeploy(context.Background(), swap, big.NewInt(int64(DefaultPaymentThreshold)))
testPeer := newDummyPeerWithSpec(Spec)
swap.addPeer(testPeer.Peer, swap.owner.address, swap.GetParams().ContractAddress)
if err := swap.Add(-int64(DefaultPaymentThreshold), testPeer.Peer); err != nil {
Expand Down Expand Up @@ -672,12 +664,14 @@ func TestResetBalance(t *testing.T) {
defer clean1()
defer clean2()

testAmount := int64(DefaultPaymentThreshold + 42)

ctx := context.Background()
err := testDeploy(ctx, creditorSwap)
err := testDeploy(ctx, creditorSwap, big.NewInt(0))
if err != nil {
t.Fatal(err)
}
err = testDeploy(ctx, debitorSwap)
err = testDeploy(ctx, debitorSwap, big.NewInt(testAmount))
if err != nil {
t.Fatal(err)
}
Expand All @@ -697,7 +691,6 @@ func TestResetBalance(t *testing.T) {
}

// set balances arbitrarily
testAmount := int64(DefaultPaymentThreshold + 42)
debitor.setBalance(testAmount)
creditor.setBalance(-testAmount)

Expand Down Expand Up @@ -1082,7 +1075,7 @@ func TestVerifyContract(t *testing.T) {
defer clean()

// deploy a new swap contract
err := testDeploy(context.TODO(), swap)
err := testDeploy(context.TODO(), swap, big.NewInt(0))
if err != nil {
t.Fatalf("Error in deploy: %v", err)
}
Expand Down Expand Up @@ -1174,15 +1167,15 @@ func TestContractIntegration(t *testing.T) {

log.Debug("deploy issuer swap")

cheque := newTestCheque()

ctx := context.TODO()
err := testDeploy(ctx, issuerSwap)
err := testDeploy(ctx, issuerSwap, big.NewInt(int64(cheque.CumulativePayout)))
if err != nil {
t.Fatal(err)
}

log.Debug("deployed. signing cheque")

cheque := newTestCheque()
cheque.ChequeParams.Contract = issuerSwap.GetParams().ContractAddress
cheque.Signature, err = cheque.Sign(issuerSwap.owner.privateKey)
if err != nil {
Expand Down Expand Up @@ -1265,7 +1258,7 @@ func testWaitForTx(ctx context.Context, backend cswap.Backend, tx *types.Transac
}

// deploy for testing (needs simulated backend commit)
func testDeploy(ctx context.Context, swap *Swap) (err error) {
func testDeploy(ctx context.Context, swap *Swap, depositAmount *big.Int) (err error) {
opts := bind.NewKeyedTransactor(swap.owner.privateKey)
opts.Context = ctx

Expand All @@ -1290,18 +1283,26 @@ func testDeploy(ctx context.Context, swap *Swap) (err error) {
stb.Commit()

// send money into the new chequebook
token, err := contractFactory.NewERC20(stb.tokenAddress, stb)
token, err := contractFactory.NewERC20Mintable(stb.tokenAddress, stb)
if err != nil {
return err
}

_, err = token.Transfer(opts, swap.contract.ContractParams().ContractAddress, big.NewInt(9000*int64(RetrieveRequestPrice)))
tx, err := token.Mint(bind.NewKeyedTransactor(ownerKey), swap.contract.ContractParams().ContractAddress, depositAmount)
if err != nil {
return err
}
stb.Commit()
receipt, err := stb.TransactionReceipt(ctx, tx.Hash())
if err != nil {
return err
}

return err
if receipt.Status != 1 {
return errors.New("token transfer reverted")
}

return nil
}

// newTestSwapAndPeer is a helper function to create a swap and a peer instance that fit together
Expand Down Expand Up @@ -1550,12 +1551,14 @@ func TestSwapLogToFile(t *testing.T) {
}
defer clean()

testAmount := int64(DefaultPaymentThreshold + 42)

ctx := context.Background()
err = testDeploy(ctx, creditorSwap)
err = testDeploy(ctx, creditorSwap, big.NewInt(testAmount))
if err != nil {
t.Fatal(err)
}
err = testDeploy(ctx, debitorSwap)
err = testDeploy(ctx, debitorSwap, big.NewInt(0))
if err != nil {
t.Fatal(err)
}
Expand All @@ -1575,7 +1578,6 @@ func TestSwapLogToFile(t *testing.T) {
}

// set balances arbitrarily
testAmount := int64(DefaultPaymentThreshold + 42)
debitor.setBalance(testAmount)
creditor.setBalance(-testAmount)

Expand Down Expand Up @@ -1637,8 +1639,10 @@ func TestAvailableBalance(t *testing.T) {
cleanup := setupContractTest()
defer cleanup()

depositAmount := big.NewInt(9000 * int64(RetrieveRequestPrice))

// deploy a chequebook
err := testDeploy(context.TODO(), swap)
err := testDeploy(context.TODO(), swap, depositAmount)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1649,7 +1653,6 @@ func TestAvailableBalance(t *testing.T) {
}

// verify that available balance equals depositAmount (we deposit during deployment)
depositAmount := big.NewInt(9000 * int64(RetrieveRequestPrice))
availableBalance, err := swap.AvailableBalance()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 9cdd812

Please sign in to comment.