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

feat(config-manager): generate script config from genesis #552

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/smart-bags-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/common-scripts": patch
---

fix incorrect `TX_HASH` when generate a deployment transaction
5 changes: 5 additions & 0 deletions .changeset/two-brooms-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/config-manager": minor
---

supported generate `ScriptConfig` from a genesis block
2 changes: 1 addition & 1 deletion packages/common-scripts/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ interface ScriptConfig {

function calculateTxHash(txSkeleton: TransactionSkeletonType): string {
const tx = createTransactionFromSkeleton(txSkeleton);
const txHash = utils.ckbHash(blockchain.Transaction.pack(tx));
const txHash = utils.ckbHash(blockchain.RawTransaction.pack(tx));
return txHash;
}

Expand Down
11 changes: 10 additions & 1 deletion packages/config-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"scripts": {
"fmt": "prettier --write \"{src,tests}/**/*.ts\" package.json",
"lint": "eslint -c ../../.eslintrc.js \"{src,tests}/**/*.ts\"",
"test": "ava --timeout=2m",
"test": "ava **/*.test.{js,ts} --timeout=2m",
"build": "pnpm run build:types && pnpm run build:js",
"build:types": "tsc --declaration --emitDeclarationOnly",
"build:js": "babel --root-mode upward src --out-dir lib --extensions .ts -s",
Expand All @@ -42,5 +42,14 @@
},
"publishConfig": {
"access": "public"
},
"ava": {
"extensions": [
"ts",
"js"
],
"require": [
"ts-node/register"
]
}
}
74 changes: 74 additions & 0 deletions packages/config-manager/src/genesis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Block, utils } from "@ckb-lumos/base";
import { ScriptConfig } from "./types";

// https://github.com/nervosnetwork/ckb-sdk-rust/blob/94ce4379454cdaf046f64b346e18e73e029f0ae6/src/constants.rs#L19C1-L24C62
zhangyouxin marked this conversation as resolved.
Show resolved Hide resolved
// the index of a transaction in a block
type TransactionIndex = number;
// the index of an output in a transaction
type OutputIndex = number;
export const SIGHASH_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [0, 1];
export const MULTISIG_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [0, 4];
export const DAO_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [0, 2];
export const SIGHASH_GROUP_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [1, 0];
// prettier-ignore
export const MULTISIG_GROUP_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [1, 1];

/**
* Generate {@link ScriptConfig} for the genesis block,
* use this function when you are on a testnet,
* or you cannot determine which network you are on
* @example
* const rpc = new RPC('http://localhost:8114')
* const genesisBlock = await rpc.getBlockByNumber('0x0')
* const scriptConfig = generateGenesisScriptConfigs(genesisBlock)
* @param genesisBlock
*/
export function generateGenesisScriptConfigs(
genesisBlock: Block
): Record<
"SECP256K1_BLAKE160" | "SECP256K1_BLAKE160_MULTISIG" | "DAO",
ScriptConfig
> {
if (!genesisBlock) throw new Error("cannot load genesis block");

return {
SECP256K1_BLAKE160: {
CODE_HASH: utils.computeScriptHash(
genesisBlock.transactions[SIGHASH_OUTPUT_LOC[0]].outputs[
SIGHASH_OUTPUT_LOC[1]
].type!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input parameter is genesisBlock: Block, which is assuming user to pass in a genesis block here.

But we should handle a situation when user feeds a non-genesis block, here using genesisBlock.transactions.outputs[x].type! it will throw error like: invalid script.

It will be better to throw Not a genesis block, please check the input block number like, then add a test case to cover that

),
INDEX: toHexNumber(SIGHASH_GROUP_OUTPUT_LOC[1]),
DEP_TYPE: "depGroup",
HASH_TYPE: "type",
TX_HASH: genesisBlock.transactions[SIGHASH_GROUP_OUTPUT_LOC[0]].hash!,
SHORT_ID: 0,
},
SECP256K1_BLAKE160_MULTISIG: {
CODE_HASH: utils.computeScriptHash(
genesisBlock.transactions[MULTISIG_OUTPUT_LOC[0]].outputs[
MULTISIG_OUTPUT_LOC[1]
].type!
),
INDEX: toHexNumber(MULTISIG_GROUP_OUTPUT_LOC[1]),
DEP_TYPE: "depGroup",
HASH_TYPE: "type",
TX_HASH: genesisBlock.transactions[MULTISIG_GROUP_OUTPUT_LOC[0]].hash!,
SHORT_ID: 1,
},
DAO: {
CODE_HASH: utils.computeScriptHash(
genesisBlock.transactions[DAO_OUTPUT_LOC[0]].outputs[DAO_OUTPUT_LOC[1]]
.type!
),
INDEX: toHexNumber(DAO_OUTPUT_LOC[1]),
DEP_TYPE: "code",
HASH_TYPE: "type",
TX_HASH: genesisBlock.transactions[DAO_OUTPUT_LOC[0]].hash!,
},
};
}

function toHexNumber(number: number): string {
return `0x${number.toString(16)}`;
}
1 change: 1 addition & 0 deletions packages/config-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./types";
export { initializeConfig, getConfig, validateConfig } from "./manager";
export * as helpers from "./helpers";
export { predefined, createConfig } from "./predefined";
export { generateGenesisScriptConfigs } from "./genesis";
1 change: 1 addition & 0 deletions packages/config-manager/tests/genesis-mainnet-block.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions packages/config-manager/tests/genesis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from "ava";
import { readFile } from "fs/promises";
import { join } from "path";
import { generateGenesisScriptConfigs, predefined } from "../src";

test("generateFromGenesisBlock", async (t) => {
const buf = await readFile(join(__dirname, "genesis-mainnet-block.json"));
const genesisBlock = JSON.parse(buf.toString());

const config = generateGenesisScriptConfigs(genesisBlock);

const predefinedConfig = predefined.LINA.SCRIPTS;
t.deepEqual(config, {
SECP256K1_BLAKE160: predefinedConfig.SECP256K1_BLAKE160,
SECP256K1_BLAKE160_MULTISIG: predefinedConfig.SECP256K1_BLAKE160_MULTISIG,
DAO: predefinedConfig.DAO,
});
});