Skip to content

Commit

Permalink
Update @defichain/testcontainers with chainparams QOL changes (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh authored Jun 2, 2021
1 parent 23d0cb4 commit 0629109
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"publish:next": "lerna exec -- npm publish --tag next --access public",
"publish:latest": "lerna exec -- npm publish --tag latest --access public",
"standard": "ts-standard --fix",
"test": "jest",
"test": "jest --maxWorkers=100%",
"test:ci": "jest --ci --coverage --forceExit --maxWorkers=4",
"all": "npm run build && npm run standard && npm run test"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Block', () => {
expect(block.tx[0].vin[0].sequence).toBeGreaterThanOrEqual(4294967295)

expect(block.tx[0].vout[0].n).toStrictEqual(0)
expect(block.tx[0].vout[0].value.toString(10)).toStrictEqual('38')
expect(block.tx[0].vout[0].value.toString(10)).toStrictEqual('76')
expect(block.tx[0].vout[0].value instanceof BigNumber).toBeTruthy()

expect(block.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import waitForExpect from 'wait-for-expect'

describe('BlockHash', () => {
const container = new MasterNodeRegTestContainer()
Expand All @@ -9,20 +8,21 @@ describe('BlockHash', () => {
beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForBlock(1)
})

afterAll(async () => {
await container.stop()
})

it('should getBlockHash', async () => {
await waitForExpect(async () => {
const info = await client.blockchain.getBlockchainInfo()
expect(info.blocks).toBeGreaterThan(1)
})

const blockHash: string = await client.blockchain.getBlockHash(1)
expect(typeof blockHash).toStrictEqual('string')
expect(blockHash.length).toStrictEqual(64)
})

it('should get regtest genesis block hash', async () => {
const genesisHash = await client.blockchain.getBlockHash(0)
expect(genesisHash).toStrictEqual('d744db74fb70ed42767ae028a129365fb4d7de54ba1b6575fb047490554f8a7b')
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { GenesisKeys, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { BigNumber } from '../../../src'

Expand All @@ -9,25 +9,63 @@ describe('TxOut', () => {
beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForBlock(1)
})

afterAll(async () => {
await container.stop()
})

it('should getTxOut', async () => {
const txId = '00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44'
it('should getTxOut of regtest 100M DFI - 0', async () => {
const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7'
const txOut = await client.blockchain.getTxOut(txId, 0)

expect(txOut).toHaveProperty('bestblock')
expect(txOut.confirmations).toBeGreaterThanOrEqual(1)
expect(txOut.value instanceof BigNumber).toStrictEqual(true)
expect(txOut.value.toString()).toStrictEqual('38')
expect(txOut.value.toString()).toStrictEqual('100000000')
expect(txOut.scriptPubKey).toHaveProperty('asm')
expect(txOut.scriptPubKey).toHaveProperty('hex')
expect(txOut.scriptPubKey.reqSigs).toBeGreaterThanOrEqual(1)
expect(txOut.scriptPubKey.type).toStrictEqual('pubkeyhash')
expect(txOut.scriptPubKey.addresses.length).toBeGreaterThanOrEqual(1)
expect(txOut.scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU')
expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[0].owner.address)
expect(txOut.coinbase).toStrictEqual(true)
})

it('should getTxOut of regtest 100M DFI - 1', async () => {
const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7'
const txOut = await client.blockchain.getTxOut(txId, 1)

expect(txOut.value.toString()).toStrictEqual('100000000')
expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[0].operator.address)
expect(txOut.coinbase).toStrictEqual(true)
})

it('should getTxOut of regtest 100M DFI - 2', async () => {
const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7'
const txOut = await client.blockchain.getTxOut(txId, 2)

expect(txOut.value.toString()).toStrictEqual('100000000')
expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[1].owner.address)
expect(txOut.coinbase).toStrictEqual(true)
})

it('should getTxOut of regtest 100M DFI - 3', async () => {
const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7'
const txOut = await client.blockchain.getTxOut(txId, 3)

expect(txOut.value.toString()).toStrictEqual('100000000')
expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[1].operator.address)
expect(txOut.coinbase).toStrictEqual(true)
})

it('should getTxOut of regtest 100M DFI - 4', async () => {
const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7'
const txOut = await client.blockchain.getTxOut(txId, 4)

expect(txOut.value.toString()).toStrictEqual('100')
expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[5].owner.address)
expect(txOut.coinbase).toStrictEqual(true)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ import { P2WPKHTransactionBuilder } from '../../src'
import { calculateTxid, fundEllipticPair, sendTransaction } from '../test.utils'
import { WIF } from '@defichain/jellyfish-crypto'

const container = new MasterNodeRegTestContainer()
let providers: MockProviders
let builder: P2WPKHTransactionBuilder
describe('appoint oracle', () => {
const container = new MasterNodeRegTestContainer()
let providers: MockProviders
let builder: P2WPKHTransactionBuilder

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()

providers = await getProviders(container)
providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey))
builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic)
providers = await getProviders(container)
providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey))
builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic)

// Prep 1000 DFI Token for testing
await container.waitForWalletBalanceGTE(1001)
})
// Prep 1000 DFI Token for testing
await container.waitForWalletBalanceGTE(1001)
})

afterAll(async () => {
await container.stop()
})
afterAll(async () => {
await container.stop()
})

describe('appoint oracle', () => {
beforeEach(async () => {
await container.waitForWalletBalanceGTE(1)
})
await container.waitForWalletBalanceGTE(11)

it('should appoint oracle(s)', async () => {
// Fund 10 DFI UTXO
await fundEllipticPair(container, providers.ellipticPair, 10)
await providers.setupMocks() // required to move utxos
})

it('should appoint oracle(s)', async () => {
// Appoint Oracle
const script = await providers.elliptic.script()
const txn = await builder.oracles.appointOracle({
Expand Down Expand Up @@ -72,11 +72,53 @@ describe('appoint oracle', () => {
expect(getOracleDataResult.priceFeeds[0].currency).toStrictEqual('USD')
})

it('should appoint oracle with multiple currencies', async () => {
it('should reject invalid appoint oracle arg - weightage over 100', async () => {
// Appoint Oracle
const script = await providers.elliptic.script()
await expect(builder.oracles.appointOracle({
script: script,
weightage: 200,
priceFeeds: [
{
token: 'TEST',
currency: 'USD'
}
]
}, script)).rejects.toThrow('Conversion input `appointOracle.weightage` must be above `0` and below `101`')
})
})

describe('appoint oracle', () => {
const container = new MasterNodeRegTestContainer()
let providers: MockProviders
let builder: P2WPKHTransactionBuilder

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()

providers = await getProviders(container)
providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey))
builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic)

// Prep 1000 DFI Token for testing
await container.waitForWalletBalanceGTE(1001)
})

afterAll(async () => {
await container.stop()
})

beforeEach(async () => {
await container.waitForWalletBalanceGTE(11)

// Fund 10 DFI UTXO
await fundEllipticPair(container, providers.ellipticPair, 10)
await providers.setupMocks() // required to move utxos
})

it('should appoint oracle with multiple currencies', async () => {
// Appoint Oracle
const script = await providers.elliptic.script()
const txn = await builder.oracles.appointOracle({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ afterAll(async () => {

beforeEach(async () => {
await providers.randomizeEllipticPair()
await providers.setupMocks() // required to move utxos
builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic)

await container.waitForWalletBalanceGTE(1)
Expand All @@ -64,6 +63,7 @@ beforeEach(async () => {

// Fund 1 DFI UTXOS for fee
await fundEllipticPair(container, providers.ellipticPair, 1)
await providers.setupMocks() // required to move utxos

// Ensure starting balances
await container.generate(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ afterAll(async () => {
describe('remove oracle', () => {
beforeEach(async () => {
await container.waitForWalletBalanceGTE(1)
})

it('should appoint and then remove oracle', async () => {
// Fund 10 DFI UTXO
await fundEllipticPair(container, providers.ellipticPair, 10)
await providers.setupMocks() // required to move utxos
})

it('should appoint and then remove oracle', async () => {
// Appoint Oracle
const script = await providers.elliptic.script()
const appointTxn = await builder.oracles.appointOracle({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ afterAll(async () => {

describe('set oracle data', () => {
beforeEach(async () => {
await container.waitForWalletBalanceGTE(1)
})
await container.waitForWalletBalanceGTE(11)

it('should appoint and then set oracle data', async () => {
// Fund 10 DFI UTXO
await fundEllipticPair(container, providers.ellipticPair, 10)
await providers.setupMocks() // required to move utxos
})

it('should appoint and then set oracle data', async () => {
// Appoint Oracle
const script = await providers.elliptic.script()
const appointTxn = await builder.oracles.appointOracle({
Expand Down Expand Up @@ -93,5 +93,3 @@ describe('set oracle data', () => {
expect(getOracleDataResult.tokenPrices[0].timestamp).toStrictEqual(1621567932)
})
})

// TODO(monstrobishi): test account state once RPC calls are in place
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ afterAll(async () => {

describe('update oracle', () => {
beforeEach(async () => {
await container.waitForWalletBalanceGTE(1)
})
await container.waitForWalletBalanceGTE(11)

it('should appoint and then update oracle', async () => {
// Fund 10 DFI UTXO
await fundEllipticPair(container, providers.ellipticPair, 10)
await providers.setupMocks() // required to move utxos
})

it('should appoint and then update oracle', async () => {
// Appoint Oracle
const script = await providers.elliptic.script()
const appointTxn = await builder.oracles.appointOracle({
Expand Down
2 changes: 1 addition & 1 deletion packages/testcontainers/src/chains/defid_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface StartOptions {
export abstract class DeFiDContainer extends DockerContainer {
/* eslint-disable @typescript-eslint/no-non-null-assertion, no-void */
public static readonly PREFIX = 'defichain-testcontainers-'
public static readonly image = 'defi/defichain:1.7.3'
public static readonly image = 'defi/defichain:HEAD-643194e' // fast-tracked https://github.com/DeFiCh/ain/pull/464 >1.7.3

public static readonly DefaultStartOptions = {
user: 'testcontainers-user',
Expand Down
8 changes: 4 additions & 4 deletions packages/testing/__tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForWalletBalanceGTE(300)
await container.waitForWalletBalanceGTE(1000000)
})

afterAll(async () => {
Expand All @@ -22,12 +22,12 @@ afterAll(async () => {
describe('utxosToAccount', () => {
it('should utxosToAccount', async () => {
const balanceBefore = await container.call('getbalance')
expect(balanceBefore).toBeGreaterThanOrEqual(300)
expect(balanceBefore).toBeGreaterThanOrEqual(1000000)

await utxosToAccount(container, 100)
await utxosToAccount(container, 1000000)

const balanceAfter = await container.call('getbalance')
expect(balanceAfter).toBeLessThan(300)
expect(balanceAfter).toBeLessThan(balanceBefore - 1000000 + 1000) // extra 1000 due to block gen
})
})

Expand Down

0 comments on commit 0629109

Please sign in to comment.