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: toolkit test coverage #243

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "@ckb-lumos/toolkit",
"version": "0.17.0-rc7",
"description": "JavaScript toolkits used to build dapps for Nervos CKB",
"main": "lib/ckb-js-toolkit.node.js",
"browser": "lib/ckb-js-toolkit.esm.js",
"main": "lib/index.js",
"types": "index.d.ts",
"engines": {
"node": ">=12.0.0"
Expand All @@ -22,25 +21,18 @@
"lib"
],
"scripts": {
"build": "rollup -c --environment BUILD:production",
"test": "rollup -c --environment BUILD:development && ava",
"build": "yarn run build:js",
"build:js": "babel -s --plugins @babel/plugin-proposal-export-namespace-from --plugins @babel/plugin-transform-modules-commonjs src --out-dir lib",
"clean": "rm -rf lib",
"test": "ava",
"fmt": "prettier --write \"{src,tests}/**/*.js\" index.d.ts",
"lint": "eslint -c ../../.eslintrc.js \"{src,tests}/**/*.js\"",
"update-test-files": "curl -L https://raw.githubusercontent.com/nervosnetwork/ckb/27c36a55e6358fd04153ec3da4638b6e10660da6/util/types/schemas/blockchain.mol -o testfiles/blockchain.mol && moleculec --language - --schema-file testfiles/blockchain.mol --format json > testfiles/blockchain.json && moleculec-es -inputFile testfiles/blockchain.json -outputFile testfiles/blockchain.esm.js && rollup -f umd -i testfiles/blockchain.esm.js -o testfiles/blockchain.umd.js --name Blockchain && rm testfiles/blockchain.mol testfiles/blockchain.json testfiles/blockchain.esm.js"
},
"author": "Xuejie Xiao",
"license": "MIT",
"dependencies": {
"cross-fetch": "^3.0.6",
"jsbi": "^3.1.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-replace": "^2.3.3",
"ava": "^3.5.0",
"prettier": "1.19.1",
"rollup": "^2.22.0",
"rollup-plugin-terser": "^7.0.2"
"peerDependencies": {
"cross-fetch": "^3.1.4",
"jsbi": "^4.1.0"
}
}
57 changes: 0 additions & 57 deletions packages/toolkit/rollup.config.js

This file was deleted.

6 changes: 3 additions & 3 deletions packages/toolkit/src/cell_collectors/rpc_collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class RPCCollector {
{
skipCellWithContent = true,
loadData = false,
loadBlockNumber = true
loadBlockNumber = true,
} = {}
) {
this.rpc = rpc;
Expand Down Expand Up @@ -62,13 +62,13 @@ export class RPCCollector {
cell_output: {
capacity: cell.capacity,
lock: cell.lock,
type: cell.type
type: cell.type,
},
out_point: cell.out_point,
block_hash: cell.block_hash,
data: data,
output_data_len: cell.output_data_len,
block_number
block_number,
};
}
currentFrom = JSBI.add(currentTo, JSBI.BigInt(1));
Expand Down
49 changes: 25 additions & 24 deletions packages/toolkit/src/normalizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ import { Reader } from "./reader";
import { BigIntToHexString } from "./rpc";

function normalizeHexNumber(length) {
return function(debugPath, value) {
return function (debugPath, value) {
if (!(value instanceof ArrayBuffer)) {
let intValue = BigIntToHexString(JSBI.BigInt(value)).substr(2);
if (intValue.length % 2 !== 0) {
intValue = "0" + intValue;
}
if (intValue.length / 2 > length) {
throw new Error(
`${debugPath} is ${intValue.length /
2} bytes long, expected length is ${length}!`
`${debugPath} is ${
intValue.length / 2
} bytes long, expected length is ${length}!`
);
}
const view = new DataView(new ArrayBuffer(length));
Expand All @@ -44,7 +45,7 @@ function normalizeHexNumber(length) {
}

function normalizeRawData(length) {
return function(debugPath, value) {
return function (debugPath, value) {
value = new Reader(value).toArrayBuffer();
if (length > 0 && value.byteLength !== length) {
throw new Error(
Expand All @@ -71,7 +72,7 @@ function normalizeObject(debugPath, object, keys) {
export function NormalizeScript(script, { debugPath = "script" } = {}) {
return normalizeObject(debugPath, script, {
code_hash: normalizeRawData(32),
hash_type: function(debugPath, value) {
hash_type: function (debugPath, value) {
switch (value) {
case "data":
return 0;
Expand All @@ -85,21 +86,21 @@ export function NormalizeScript(script, { debugPath = "script" } = {}) {
throw new Error(`${debugPath}.hash_type has invalid value: ${value}`);
}
},
args: normalizeRawData(-1)
args: normalizeRawData(-1),
});
}

export function NormalizeOutPoint(outPoint, { debugPath = "out_point" } = {}) {
return normalizeObject(debugPath, outPoint, {
tx_hash: normalizeRawData(32),
index: normalizeHexNumber(4)
index: normalizeHexNumber(4),
});
}

function toNormalize(normalize) {
return function(debugPath, value) {
return function (debugPath, value) {
return normalize(value, {
debugPath
debugPath,
});
};
}
Expand All @@ -110,7 +111,7 @@ export function NormalizeCellInput(
) {
return normalizeObject(debugPath, cellInput, {
since: normalizeHexNumber(8),
previous_output: toNormalize(NormalizeOutPoint)
previous_output: toNormalize(NormalizeOutPoint),
});
}

Expand All @@ -120,11 +121,11 @@ export function NormalizeCellOutput(
) {
const result = normalizeObject(debugPath, cellOutput, {
capacity: normalizeHexNumber(8),
lock: toNormalize(NormalizeScript)
lock: toNormalize(NormalizeScript),
});
if (cellOutput.type) {
result.type_ = NormalizeScript(cellOutput.type, {
debugPath: `${debugPath}.type`
debugPath: `${debugPath}.type`,
});
}
return result;
Expand All @@ -133,7 +134,7 @@ export function NormalizeCellOutput(
export function NormalizeCellDep(cellDep, { debugPath = "cell_dep" } = {}) {
return normalizeObject(debugPath, cellDep, {
out_point: toNormalize(NormalizeOutPoint),
dep_type: function(debugPath, value) {
dep_type: function (debugPath, value) {
switch (value) {
case "code":
return 0;
Expand All @@ -146,12 +147,12 @@ export function NormalizeCellDep(cellDep, { debugPath = "cell_dep" } = {}) {
default:
throw new Error(`${debugPath}.dep_type has invalid value: ${value}`);
}
}
},
});
}

function toNormalizeArray(normalizeFunction) {
return function(debugPath, array) {
return function (debugPath, array) {
return array.map((item, i) => {
return normalizeFunction(`${debugPath}[${i}]`, item);
});
Expand All @@ -168,7 +169,7 @@ export function NormalizeRawTransaction(
header_deps: toNormalizeArray(normalizeRawData(32)),
inputs: toNormalizeArray(toNormalize(NormalizeCellInput)),
outputs: toNormalizeArray(toNormalize(NormalizeCellOutput)),
outputs_data: toNormalizeArray(normalizeRawData(-1))
outputs_data: toNormalizeArray(normalizeRawData(-1)),
});
}

Expand All @@ -177,10 +178,10 @@ export function NormalizeTransaction(
{ debugPath = "transaction" } = {}
) {
const rawTransaction = NormalizeRawTransaction(transaction, {
debugPath: `(raw)${debugPath}`
debugPath: `(raw)${debugPath}`,
});
const result = normalizeObject(debugPath, transaction, {
witnesses: toNormalizeArray(normalizeRawData(-1))
witnesses: toNormalizeArray(normalizeRawData(-1)),
});
result.raw = rawTransaction;
return result;
Expand All @@ -200,16 +201,16 @@ export function NormalizeRawHeader(
transactions_root: normalizeRawData(32),
proposals_hash: normalizeRawData(32),
uncles_hash: normalizeRawData(32),
dao: normalizeRawData(32)
dao: normalizeRawData(32),
});
}

export function NormalizeHeader(header, { debugPath = "header" } = {}) {
const rawHeader = NormalizeRawHeader(header, {
debugPath: `(raw)${debugPath}`
debugPath: `(raw)${debugPath}`,
});
const result = normalizeObject(debugPath, header, {
nonce: normalizeHexNumber(16)
nonce: normalizeHexNumber(16),
});
result.raw = rawHeader;
return result;
Expand All @@ -221,7 +222,7 @@ export function NormalizeUncleBlock(
) {
return normalizeObject(debugPath, uncleBlock, {
header: toNormalize(NormalizeHeader),
proposals: toNormalizeArray(normalizeRawData(10))
proposals: toNormalizeArray(normalizeRawData(10)),
});
}

Expand All @@ -230,7 +231,7 @@ export function NormalizeBlock(block, { debugPath = "block" } = {}) {
header: toNormalize(NormalizeHeader),
uncles: toNormalizeArray(toNormalize(NormalizeUncleBlock)),
transactions: toNormalizeArray(toNormalize(NormalizeTransaction)),
proposals: toNormalizeArray(normalizeRawData(10))
proposals: toNormalizeArray(normalizeRawData(10)),
});
}

Expand All @@ -240,7 +241,7 @@ export function NormalizeCellbaseWitness(
) {
return normalizeObject(debugPath, cellbaseWitness, {
lock: toNormalize(NormalizeScript),
message: normalizeRawData(-1)
message: normalizeRawData(-1),
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ArrayBufferReader {
return (
"0x" +
Array.prototype.map
.call(new Uint8Array(this.view.buffer), x =>
.call(new Uint8Array(this.view.buffer), (x) =>
("00" + x.toString(16)).slice(-2)
)
.join("")
Expand Down
20 changes: 10 additions & 10 deletions packages/toolkit/src/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function mergeOptions(overrideOptions, defaultOptions) {
overrideOptions.headers || {}
);
return Object.assign({}, defaultOptions, overrideOptions, {
headers: headers
headers: headers,
});
}

Expand All @@ -23,9 +23,9 @@ const batchHandler = {
{
method: "post",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify(target.payload)
body: JSON.stringify(target.payload),
},
target.defaultOptions
)
Expand All @@ -40,11 +40,11 @@ const batchHandler = {
jsonrpc: "2.0",
id: id,
method: method,
params: params
params: params,
});
return receiver;
};
}
},
};

const handler = {
Expand All @@ -56,7 +56,7 @@ const handler = {
id: Math.round(Math.random() * 10000000),
payload: [],
uri: target.uri,
defaultOptions: target.defaultOptions
defaultOptions: target.defaultOptions,
},
batchHandler
);
Expand All @@ -70,14 +70,14 @@ const handler = {
{
method: "post",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: id,
method: method,
params: params
})
params: params,
}),
},
target.defaultOptions
)
Expand All @@ -93,7 +93,7 @@ const handler = {
}
return data.result;
};
}
},
};

export class RPC {
Expand Down
Loading