Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Fix e2e tests (paritytech#640)
Browse files Browse the repository at this point in the history
* added logging and updated metadata

* separated tests which bootstrap the bridge

* tested with v0.9.23

* add channel variables to example

* revert example envrc
  • Loading branch information
alistair-singh authored Jun 13, 2022
1 parent abf5d2f commit 73eec0b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 45 deletions.
Binary file modified parachain/tools/query-events/metadata.scale
Binary file not shown.
16 changes: 13 additions & 3 deletions relayer/relays/parachain/query_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os/exec"

log "github.com/sirupsen/logrus"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
)

Expand Down Expand Up @@ -50,15 +51,24 @@ func NewQueryClient() QueryClient {
func (q *QueryClient) QueryEvents(ctx context.Context, api string, blockHash types.Hash) (*Events, error) {
name, args := q.NameArgs(api, blockHash.Hex())
cmd := exec.CommandContext(ctx, name, args...)
var out bytes.Buffer
cmd.Stdout = &out

var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf

err := cmd.Run()
if err != nil {
log.WithFields(log.Fields{
"name": name,
"args": fmt.Sprintf("%v", args),
"stdErr": errBuf.String(),
"stdOut": outBuf.String(),
}).Error("Failed to query events.")
return nil, err
}

var items inputItems
err = json.Unmarshal(out.Bytes(), &items)
err = json.Unmarshal(outBuf.Bytes(), &items)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ yarn install
### Polkadot

* Clone the polkadot repository somewhere on your machine
* Checkout tag `v0.9.22-rc4`.
* Checkout tag `v0.9.23`.

Example:
```bash
git clone -n https://github.com/paritytech/polkadot.git
cd /path/to/polkadot
git fetch --tags
git checkout v0.9.22
git checkout v0.9.23
cargo build --release
```

Expand Down
67 changes: 67 additions & 0 deletions test/test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const Web3 = require('web3');

const BigNumber = require('bignumber.js');

const { expect } = require("chai")
.use(require("chai-as-promised"))
.use(require("chai-bignumber")(BigNumber));

const { treasuryAddressSS58, polkadotSenderSS58,
polkadotRecipientSS58, polkadotRecipient, bootstrap } = require('../src/fixtures');

const { ChannelId } = require("../src/helpers");

describe('Bridge', function () {
let ethClient, subClient, testSubClient;

before(async function () {
const clients = await bootstrap();
ethClient = clients.ethClient;
subClient = clients.subClient;
testSubClient = clients.testSubClient;
this.testParaEthAssetId = 0;
});

describe('Bootstrap', function () {
it('should transfer DOT from Substrate to Ethereum (basic channel)', async function () {
const amount = BigNumber('100000000000000'); // 100 DOT (12 decimal places in this environment)
const amountWrapped = BigNumber(Web3.utils.toWei('100', "ether")); // 100 SnowDOT (18 decimal places)
const ethAccount = ethClient.accounts[1];

const beforeEthBalance = await ethClient.getDotBalance(ethAccount);
const beforeSubBalance = await subClient.queryAccountBalance(polkadotSenderSS58);

// lock DOT using basic channel
await subClient.lockDOT(subClient.alice, ethAccount, amount.toFixed(), ChannelId.BASIC)
await ethClient.waitForNextEventData({ appName: 'snowDOT', eventName: 'Minted' });

const afterEthBalance = await ethClient.getDotBalance(ethAccount);
const afterSubBalance = await subClient.queryAccountBalance(polkadotSenderSS58);

expect(afterEthBalance.minus(beforeEthBalance)).to.be.bignumber.equal(amountWrapped);
expect(beforeSubBalance.minus(afterSubBalance)).to.be.bignumber.greaterThan(amount);
});

it('should transfer ETH from Ethereum to Substrate (incentivized channel)', async function () {
const amount = BigNumber(Web3.utils.toWei('1', "ether"));
const ethAccount = ethClient.accounts[1];

const subBalances = await subClient.subscribeAssetsAccountBalances(
this.testParaEthAssetId, polkadotRecipientSS58, 2
);

const beforeEthBalance = await ethClient.getEthBalance(ethAccount);
const beforeSubBalance = await subBalances[0];

const { gasCost } = await ethClient.lockETH(ethAccount, amount, polkadotRecipient, ChannelId.INCENTIVIZED, 0, 0);

const afterEthBalance = await ethClient.getEthBalance(ethAccount);
const afterSubBalance = await subBalances[1];

expect(beforeEthBalance.minus(afterEthBalance)).to.be.bignumber.equal(amount.plus(gasCost));
expect(afterSubBalance.minus(beforeSubBalance)).to.be.bignumber.equal(amount);
// conservation of value
expect(beforeEthBalance.plus(beforeSubBalance)).to.be.bignumber.equal(afterEthBalance.plus(afterSubBalance).plus(gasCost));
});
});
});
19 changes: 0 additions & 19 deletions test/test/dotapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ describe('Bridge', function () {

describe('DOT App', function () {

it('should transfer DOT from Substrate to Ethereum (basic channel)', async function () {
const amount = BigNumber('100000000000000'); // 100 DOT (12 decimal places in this environment)
const amountWrapped = BigNumber(Web3.utils.toWei('100', "ether")); // 100 SnowDOT (18 decimal places)
const ethAccount = ethClient.accounts[1];

const beforeEthBalance = await ethClient.getDotBalance(ethAccount);
const beforeSubBalance = await subClient.queryAccountBalance(polkadotSenderSS58);

// lock DOT using basic channel
await subClient.lockDOT(subClient.alice, ethAccount, amount.toFixed(), ChannelId.BASIC)
await ethClient.waitForNextEventData({ appName: 'snowDOT', eventName: 'Minted' });

const afterEthBalance = await ethClient.getDotBalance(ethAccount);
const afterSubBalance = await subClient.queryAccountBalance(polkadotSenderSS58);

expect(afterEthBalance.minus(beforeEthBalance)).to.be.bignumber.equal(amountWrapped);
expect(beforeSubBalance.minus(afterSubBalance)).to.be.bignumber.greaterThan(amount);
})

it('should transfer DOT from Substrate to Ethereum (incentivized channel)', async function () {
const amount = BigNumber('100000000000000'); // 100 DOT (12 decimal places in this environment)
const amountWrapped = BigNumber(Web3.utils.toWei('100', "ether")); // 100 SnowDOT (18 decimal places)
Expand Down
21 changes: 0 additions & 21 deletions test/test/ethapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@ describe('Bridge', function () {
});

describe('ETH App', function () {
it('should transfer ETH from Ethereum to Substrate (incentivized channel)', async function () {
const amount = BigNumber(Web3.utils.toWei('1', "ether"));
const ethAccount = ethClient.accounts[1];

const subBalances = await subClient.subscribeAssetsAccountBalances(
this.testParaEthAssetId, polkadotRecipientSS58, 2
);

const beforeEthBalance = await ethClient.getEthBalance(ethAccount);
const beforeSubBalance = await subBalances[0];

const { gasCost } = await ethClient.lockETH(ethAccount, amount, polkadotRecipient, ChannelId.INCENTIVIZED, 0, 0);

const afterEthBalance = await ethClient.getEthBalance(ethAccount);
const afterSubBalance = await subBalances[1];

expect(beforeEthBalance.minus(afterEthBalance)).to.be.bignumber.equal(amount.plus(gasCost));
expect(afterSubBalance.minus(beforeSubBalance)).to.be.bignumber.equal(amount);
// conservation of value
expect(beforeEthBalance.plus(beforeSubBalance)).to.be.bignumber.equal(afterEthBalance.plus(afterSubBalance).plus(gasCost));
});

it('should transfer ETH from Substrate to Ethereum (incentivized channel)', async function () {
// Wait for new substrate block before tests, as queries may go to old block
Expand Down

0 comments on commit 73eec0b

Please sign in to comment.