Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(whale-api): added e2e to check indexing of evmTx txn block #2150

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
115 changes: 115 additions & 0 deletions apps/whale-api/src/module.indexer/model/transaction.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { TransactionVinMapper } from '../../module.model/transaction.vin'
import { TransactionVoutMapper } from '../../module.model/transaction.vout'
import { NestFastifyApplication } from '@nestjs/platform-fastify'
import { createTestingApp, stopTestingApp, waitForIndexedHeight } from '../../e2e.module'
import { Testing } from '@defichain/jellyfish-testing'
import { TransferDomainType } from '@defichain/jellyfish-api-core/dist/category/account'
import BigNumber from 'bignumber.js'

const container = new MasterNodeRegTestContainer()
let app: NestFastifyApplication
Expand Down Expand Up @@ -73,3 +76,115 @@ it('should wait for block 20', async () => {

await expectTransactions(block!.hash, block!.transactionCount)
})

describe('EVM txn indexing', () => {
const testing = Testing.create(container)
let dfiAddress: string, ethAddress: string, toEthAddress: string
const amount = {
ONE: 1,
HUNDRED: 100
}

beforeAll(async () => {
await container.generate(100)
await waitForIndexedHeight(app, 100)
await testing.rpc.masternode.setGov({
ATTRIBUTES: {
// Enable evm
'v0/params/feature/evm': 'true',
'v0/params/feature/transferdomain': 'true',
'v0/transferdomain/dvm-evm/enabled': 'true',
'v0/transferdomain/evm-dvm/enabled': 'true',
'v0/transferdomain/dvm-evm/dat-enabled': 'true',
'v0/transferdomain/evm-dvm/dat-enabled': 'true',
'v0/transferdomain/dvm-evm/src-formats': ['p2pkh', 'bech32'],
'v0/transferdomain/dvm-evm/dest-formats': ['erc55'],
'v0/transferdomain/evm-dvm/src-formats': ['erc55'],
'v0/transferdomain/evm-dvm/auth-formats': ['bech32-erc55'],
'v0/transferdomain/evm-dvm/dest-formats': ['p2pkh', 'bech32']
}
})
await container.generate(2)
dfiAddress = await container.call('getnewaddress', ['', 'legacy'])
await container.call('utxostoaccount', [{ [dfiAddress]: '105@DFI' }])
await container.generate(1)
ethAddress = await container.call('getnewaddress', ['', 'erc55'])
toEthAddress = await container.call('getnewaddress', ['', 'erc55'])
})

it('should index evm txn', async () => {
const dvmToEvmTransfer = [
{
src: {
address: dfiAddress,
amount: `${amount.HUNDRED}@DFI`,
domain: TransferDomainType.DVM
},
dst: {
address: ethAddress,
amount: `${amount.HUNDRED}@DFI`,
domain: TransferDomainType.EVM
}
}
]
await container.call('transferdomain', [dvmToEvmTransfer])
await container.generate(1)

const tx4 = await createEVMTx({ testing, from: ethAddress, to: toEthAddress, value: amount.ONE, nonce: 4 })
const tx0 = await createEVMTx({ testing, from: ethAddress, to: toEthAddress, value: amount.ONE, nonce: 0 })
const tx2 = await createEVMTx({ testing, from: ethAddress, to: toEthAddress, value: amount.ONE, nonce: 2 })
const tx1 = await createEVMTx({ testing, from: ethAddress, to: toEthAddress, value: amount.ONE, nonce: 1 })
const tx3 = await createEVMTx({ testing, from: ethAddress, to: toEthAddress, value: amount.ONE, nonce: 3 })

await container.generate(1)
const blockHash: string = await testing.rpc.blockchain.getBestBlockHash()
const blockDetails = await testing.rpc.blockchain.getBlock(blockHash, 1)

// validate sorted txn list
expect(blockDetails.tx[1]).toStrictEqual(tx0)
expect(blockDetails.tx[2]).toStrictEqual(tx1)
expect(blockDetails.tx[3]).toStrictEqual(tx2)
expect(blockDetails.tx[4]).toStrictEqual(tx3)
expect(blockDetails.tx[5]).toStrictEqual(tx4)

await container.generate(1)
await waitForIndexedHeight(app, blockDetails.height)

const transactionMapper = app.get(TransactionMapper)
const blockMapper = app.get(BlockMapper)
const block = await blockMapper.getByHeight(blockDetails.height)
await expectTransactions(block!.hash, block!.transactionCount)
const transactions = await transactionMapper.queryByBlockHash(block!.hash, 100)

// validate mapped txn list
expect(transactions[1].id).toStrictEqual(tx0)
expect(transactions[2].id).toStrictEqual(tx1)
expect(transactions[3].id).toStrictEqual(tx2)
expect(transactions[4].id).toStrictEqual(tx3)
expect(transactions[5].id).toStrictEqual(tx4)

// Wait for next block to get indexed
await container.generate(1)
const newBlockHash: string = await testing.rpc.blockchain.getBestBlockHash()
const newBlockDetails = await testing.rpc.blockchain.getBlock(newBlockHash, 1)
await container.generate(1)
await waitForIndexedHeight(app, newBlockDetails.height)
})
})

async function createEVMTx ({ testing, from, to, value, nonce }: {
testing: Testing
from: string
to: string
value: number
nonce: number
}): Promise<string> {
return await testing.rpc.evm.evmtx({
from,
to,
nonce,
gasPrice: 21,
gasLimit: 21000,
value: new BigNumber(value)
})
}
Loading