UNPKG

4.08 kBJavaScriptView Raw
1/*
2 * ISC License (ISC)
3 * Copyright (c) 2018 aeternity developers
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17import { describe, it, before } from 'mocha'
18import { getSdk } from './'
19import { generateKeyPair } from '../../es/utils/crypto'
20
21describe('Node Chain', function () {
22 let client
23 const { publicKey } = generateKeyPair()
24
25 before(async function () {
26 client = await getSdk()
27 })
28
29 it('determines the height', async () => {
30 return client.height().should.eventually.be.a('number')
31 })
32
33 it('waits for specified heights', async () => {
34 const target = await client.height() + 1
35 await client.awaitHeight(target, { attempts: 120 }).should.eventually.be.at.least(target)
36 return client.height().should.eventually.be.at.least(target)
37 })
38 it('Can verify transaction from broadcast error', async () => {
39 try {
40 await client.spend(0, publicKey, { fee: 100, verify: false })
41 } catch (e) {
42 const validation = await e.verifyTx()
43 validation.should.has.property('validation')
44 }
45 })
46 it('Get top block', async () => {
47 const top = await client.topBlock()
48 top.should.has.property('hash')
49 top.should.has.property('height')
50 })
51 it('Get pending transaction', async () => {
52 const mempool = await client.mempool()
53 mempool.should.has.property('transactions')
54 })
55 it('Get current generation', async () => {
56 const generation = await client.getCurrentGeneration()
57 generation.should.has.property('keyBlock')
58 })
59 it('Get key block', async () => {
60 const { keyBlock } = await client.getCurrentGeneration()
61 const keyBlockByHash = await client.getKeyBlock(keyBlock.hash)
62 const keyBlockByHeight = await client.getKeyBlock(keyBlock.height)
63 const keyBlockError = await client.getKeyBlock(false).catch(e => true)
64 keyBlockByHash.should.be.an('object')
65 keyBlockByHeight.should.be.an('object')
66 keyBlockError.should.be.equal(true)
67 })
68 it('Get generation', async () => {
69 const { keyBlock } = await client.getCurrentGeneration()
70 const genByHash = await client.getGeneration(keyBlock.hash)
71 const genByHeight = await client.getGeneration(keyBlock.height)
72 const genArgsError = await client.getGeneration(true).catch(e => true)
73 genByHash.should.be.an('object')
74 genByHeight.should.be.an('object')
75 genArgsError.should.be.equal(true)
76 })
77 it('polls for transactions', async () => {
78 const sender = await client.address()
79 const receiver = publicKey
80 const tx = await client.spendTx({
81 amount: 1,
82 senderId: sender,
83 recipientId: receiver,
84 payload: '',
85 ttl: Number.MAX_SAFE_INTEGER
86 })
87 const signed = await client.signTransaction(tx)
88 const { txHash } = await client.api.postTransaction({ tx: signed })
89
90 await client.poll(txHash).should.eventually.be.fulfilled
91 return client.poll('th_xxx', { blocks: 1 }).should.eventually.be.rejected
92 })
93
94 it('Wait for transaction confirmation', async () => {
95 const txData = await client.spend(1000, await client.address(), { confirm: true })
96 const isConfirmed = (await client.height()) >= txData.blockHeight + 3
97
98 isConfirmed.should.be.equal(true)
99
100 const txData2 = await client.spend(1000, await client.address(), { confirm: 4 })
101 const isConfirmed2 = (await client.height()) >= txData2.blockHeight + 4
102 isConfirmed2.should.be.equal(true)
103 })
104})