Skip to content

Commit

Permalink
Merge pull request #3 from keep-network/e2e-test
Browse files Browse the repository at this point in the history
tBTC end-to-end test script
  • Loading branch information
nkuba authored Jun 4, 2020
2 parents accc237 + 1ce8113 commit 59f5c1e
Show file tree
Hide file tree
Showing 23 changed files with 30,445 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
/ethereum/data/geth/
/storage/
/storage/
node_modules
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "tbtc.js"]
path = tbtc.js
url = https://github.com/keep-network/tbtc.js.git
[submodule "relays"]
path = relays
url = https://github.com/keep-network/relays.git
60 changes: 59 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ If you are not on macOS or you don't have Homebrew, you need to install:
- `protoc-gen-gogoslick` toolchain
- `jq`

Regardless you use `macos-setup.sh` or not, you should also install:

- https://github.com/pyenv/pyenv[pyenv], at least 1.2.18
- https://github.com/pypa/pipenv[pipenv], at least 2018.11.26

== Node.js version notice

Please use Node `v11.15.0` for everything but E2E test script.
To run the test script, please use Node `v14.3.0`.

We recommend using https://github.com/nvm-sh/nvm[nvm] to manage node versions easily.
You can install both versions by doing:
```
nvm install 11.15.0
nvm install 14.3.0
```
And use the specific version:
```
nvm use 11.15.0
nvm use 14.3.0
```

== VPN connection

In order to install and run the system, you should be connected to the
`keep-dev` VPN. This requirement is here because we use a bitcoin
testnet node which is hosted on the `keep-dev` cluster. We plan to use
a local bitcoin network soon so this requirement will be abolished.

== Installing the system
. Initialize local `geth` using:
Expand Down Expand Up @@ -85,8 +113,38 @@ cd keep-core
KEEP_ETHEREUM_PASSWORD="password" ./keep-core --config configs/config.local.1.toml relay request
```

== Relay maintainer

To interact with the system properly, you should also run a relay-maintainer
instance by doing:
```
./run-testnet-relay.sh
```

= How to interact with the system

You can interact with the system through the tBTC dApp or semi-automated
end-to-end tests. Before you start interacting, make sure you:

- Have a connection with the `keep-dev` VPN
- Installed all system components using `install.sh` script
- Have a local Geth instance (`run-geth.sh`) working
- Have a working relay maintainer (`run-testnet-relay.sh`)
- Have 1 `keep-core` and 3 `keep-ecdsa` clients up and running

== tBTC dApp

WORK IN PROGRESS
To run the tBtc dApp invoke:
```
./run-tbtc-dapp.sh
```
The application will be available on `http://localhost:3000`.

== E2E tests

To run the semi-automated end-to-end scenario invoke:
```
./run-e2e-test.sh
```
Please follow the emerging prompts to complete the scenario. Full automation
will be available in the future.
7 changes: 7 additions & 0 deletions configs/relay-genesis/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"protocol": "http",
"host": "10.102.100.252",
"port": "18332",
"user": "x",
"pass": "backwardskeymyisthis"
}
19 changes: 19 additions & 0 deletions configs/relays/.my_env_file.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SUMMA_RELAY_ETHER_HOST=localhost

SUMMA_RELAY_ETHER_PORT=8545

SUMMA_RELAY_OPERATOR_KEY=d6ce6a6ca295bbef9ddee79e583fabdcdd98290bbea2dc03ff8317ccf70d3f87

SUMMA_RELAY_ETH_NETWORK=internal

SUMMA_RELAY_ETH_CHAIN_ID=1101

SUMMA_RELAY_BCOIN_HOST=10.102.100.252

SUMMA_RELAY_BCOIN_API_KEY=backwardskeymyisthis

SUMMA_RELAY_BCOIN_PORT=18332

SUMMA_RELAY_INFURA_KEY=""

SUMMA_RELAY_CONTRACT=RELAYCONTRACT
135 changes: 135 additions & 0 deletions e2e/e2e-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env node --experimental-modules

import Web3 from "web3"
import ProviderEngine from "web3-provider-engine"
import WebsocketSubprovider from "web3-provider-engine/subproviders/websocket.js"
import TBTC from "@keep-network/tbtc.js"
import Subproviders from "@0x/subproviders"

const depositsCount = 2
const satoshiLotSize = 100000 // 0.001 BTC
const btcAddress = '2N6L4Q6fphMzuWqERQYTwgEMmQTpcqgdVFK'

const engine = new ProviderEngine({ pollingInterval: 1000 })

engine.addProvider(
// Private key of address 0xd6b0a1ca8f0641b97efec0f1ed73d72e58b38fa5
// which corresponds to the account[5].
new Subproviders.PrivateKeyWalletSubprovider(
"f95e1da038f1fd240cb0c966d8826fb5c0369407f76f34736a5c381da7ca0ecd"
)
)
engine.addProvider(
// Local geth websocket endpoint.
new WebsocketSubprovider(
{rpcUrl: "ws://127.0.0.1:8546"}
)
)

const web3 = new Web3(engine)

engine.start()

async function run() {
// Set 0xd6b0a1ca8f0641b97efec0f1ed73d72e58b38fa5 as default account.
web3.eth.defaultAccount = (await web3.eth.getAccounts())[0]

const tbtc = await TBTC.withConfig({
web3: web3,
bitcoinNetwork: "testnet",
electrum: {
testnet: {
server: "10.102.100.24",
port: 443,
protocol: "ssl"
},
testnetWS: {
server: "10.102.100.24",
port: 8080,
protocol: "ws"
}
}
})


const deposits = []
for (let i = 1; i <= depositsCount; i++) {
console.log(`\nStarting deposit number [${i}]...\n`)
const deposit = await createDeposit(tbtc, satoshiLotSize)
deposits.push(deposit)
console.log(`\nDeposit ${deposit.address} has been created successfully.`)
}

console.log(`\nStarting redemption of the first deposit...\n`)
const message = await redeemDeposit(tbtc, deposits[0].address, btcAddress)
console.log(`\nRedemption outcome: ${message}\n`)
}

async function createDeposit(tbtc, satoshiLotSize) {
const deposit = await tbtc.Deposit.withSatoshiLotSize(
web3.utils.toBN(satoshiLotSize)
)

deposit.autoSubmit()

return new Promise(async (resolve, reject) => {
deposit.onBitcoinAddressAvailable(async address => {
try {
const lotSize = await deposit.getSatoshiLotSize()
console.log(
"\tGot deposit address:",
address,
"; fund with:",
lotSize.toString(),
"satoshis please."
)
console.log("Now monitoring for deposit transaction...")
} catch (err) {
reject(err)
}
})

deposit.onActive(async () => {
try {
console.log("Deposit is active, minting...")
await deposit.mintTBTC()
resolve(deposit)
} catch (err) {
reject(err)
}
})
})
}

async function redeemDeposit(tbtc, depositAddress, redeemerAddress) {
return new Promise(async (resolve, reject) => {
try {
const deposit = await tbtc.Deposit.withAddress(depositAddress)
const redemption = await deposit.requestRedemption(redeemerAddress)
redemption.autoSubmit()

redemption.onWithdrawn(transactionID => {
console.log()

resolve(
`Redeemed deposit ${deposit.address} with Bitcoin transaction ` +
`${transactionID}.`
)
})
} catch (err) {
reject(err)
}
})
}

run()
.then(result => {
console.log("Test completed successfully")

process.exit(0)
})
.catch(error => {
console.error("Test errored out with error: ", error)

process.exit(1)
})
Loading

0 comments on commit 59f5c1e

Please sign in to comment.