Skip to content

Commit

Permalink
improve tests (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni authored Jul 10, 2023
1 parent b77c83b commit cf9b8e5
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 263 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@knuth/bch",
"version": "1.22.0",
"version": "1.23.0",
"description": "Bitcoin Cash development platform for Javascript and Typescript applications",
"main": "src/kth.js",
"scripts": {
"test": "jest --runInBand",
"test": "jest --runInBand --verbose",
"lint": "eslint **/*.js",
"lint:fix": "npm run lint -- --fix",
"lint:github-action": "node ./node_modules/eslint/bin/eslint . --ignore-path .gitignore"
Expand Down Expand Up @@ -39,7 +39,7 @@
"homepage": "https://github.com/k-nuth/js-api#readme",
"dependencies": {
"bluebird": "^3.7.2",
"@knuth/bch-native": "^0.54.0",
"@knuth/bch-native": "^0.56.0",
"memoizee": "^0.4.14"
},
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions src/chain/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ const fromNative = (native, destroy = false) => {
destruct(native);
}
return obj;

// console.log(native);

// const headerNative = kth.chain_block_header(native);
// console.log(headerNative);
// const version = kth.chain_header_version(headerNative);
// console.log(version);
// // const prevHash = kth.chain_header_previous_block_hash(headerNative);
// // console.log(prevHash);
// // const merkle = kth.chain_header_merkle(headerNative);
// // console.log(merkle);
// // const timestamp = kth.chain_header_timestamp(headerNative);
// // console.log(timestamp);
// // const bits = kth.chain_header_bits(headerNative);
// // console.log(bits);
// // const nonce = kth.chain_header_nonce(headerNative);
// // console.log(nonce);


// // const h = header.fromNative(kth.chain_block_header(native));
// // console.log(h);
// // const txs = transactionList.fromNative(kth.chain_block_transactions(native));
// // console.log(txs);
// // const obj = new Block(
// // header.fromNative(kth.chain_block_header(native)),
// // transactionList.fromNative(kth.chain_block_transactions(native))
// // );
// if (destroy) {
// destruct(native);
// }
// // return obj;
// return undefined;
};

const fromData = (version, data) => {
Expand Down
22 changes: 17 additions & 5 deletions src/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const header = require('./header');
const block = require('./block');
const transaction = require('./transaction');
const blockList = require('./blockList');
const err = require('../errors');


const async_chain = {
// fetch_last_height: Promise.promisify(kth.chain_fetch_last_height),
Expand Down Expand Up @@ -103,6 +105,11 @@ const async_chain = {
},
};

function objOrNull(ec, native, fromNative) {
const obj = ec == err.errors.success ? fromNative(native) : null;
return obj;
}

class Chain {
constructor(native, nodeNative) {
this.native = native;
Expand All @@ -123,27 +130,32 @@ class Chain {

async getBlockHeaderByHeight(height) {
const res = await async_chain.fetch_block_header_by_height(this.native, height);
return [res[0], header.fromNative(res[1]), res[2]];
const obj = objOrNull(res[0], res[1], header.fromNative);
return [res[0], obj, res[2]];
}

async getBlockHeaderByHash(hash) {
const res = await async_chain.fetch_block_header_by_hash(this.native, hash);
return [res[0], header.fromNative(res[1]), res[2]];
const obj = objOrNull(res[0], res[1], header.fromNative);
return [res[0], obj, res[2]];
}

async getBlockByHeight(height) {
const res = await async_chain.fetch_block_by_height(this.native, height);
return [res[0], block.fromNative(res[1]), res[2]];
const obj = objOrNull(res[0], res[1], block.fromNative);
return [res[0], obj, res[2]];
}

async getBlockByHash(hash) {
const res = await async_chain.fetch_block_by_hash(this.native, hash);
return [res[0], block.fromNative(res[1]), res[2]];
const obj = objOrNull(res[0], res[1], block.fromNative);
return [res[0], obj, res[2]];
}

async getTransaction(hash, requireConfirmed) {
const res = await async_chain.fetch_transaction(this.native, hash, requireConfirmed);
return [res[0], transaction.fromNative(res[1]), res[2], res[3]];
const obj = objOrNull(res[0], res[1], transaction.fromNative);
return [res[0], obj, res[2], res[3]];
}

async getTransactionPosition(hash, requireConfirmed) {
Expand Down
Loading

0 comments on commit cf9b8e5

Please sign in to comment.