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

Roll back #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 26 additions & 8 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ import {LightClientCli} from "../service/light-cli";

const CKB_CLIENT_CLI_PATH = "tmp/ckb-cli-light-client"
const CKB_LIGHT_CLIENT_PATH = "tmp/startBlockchain/ckbLightClient/ckb-light-client"
const RPC_DEBUG = false
const DEV_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient"
const CKB_DEV_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient/ckb/target/release"
const CKB_DEV_INDEX_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient/ckb-indexer/target/release"
const CKB_DEV_LIGHT_CLIENT_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient/ckb-light-client/target/release"

const CKB_DEV_RPC_URL = "http://localhost:8114";
const CKB_DEV_RPC_INDEX_URL = "http://localhost:8116";

const RPC_DEBUG = true
const CKB_RPC_URL = "https://testnet.ckb.dev";
// const CKB_RPC_URL = CKB_DEV_RPC_URL;
const CKB_RPC_INDEX_URL = "https://testnet.ckb.dev/indexer";
// const CKB_RPC_INDEX_URL = CKB_DEV_RPC_INDEX_URL;
const CKB_LIGHT_RPC_URL = "http://localhost:9000";

export enum FeeRate {
SLOW = 1000,
NORMAL = 100000,
NORMAL = 10000000,
FAST = 10000000
}

const uint = 100000000
const uint = 100000000

const FEE = FeeRate.NORMAL
config.initializeConfig(
Expand All @@ -32,25 +42,27 @@ const CkbClientNode = new LightClient(CKB_LIGHT_CLIENT_PATH)
const deprecatedAddr = helpers.generateAddress(script);
const newFullAddr = helpers.encodeToAddress(script);
const rpcCLient = new RPC(CKB_RPC_URL);
const rpcDevCLient = new RPC(CKB_DEV_RPC_URL);

const {AGGRON4} = config.predefined;
const RPC_NETWORK = AGGRON4;
const ACCOUNT_PRIVATE = "0xdd50cac37ec6dd12539a968c1a2cbedda75bd8724f7bcad486548eaabb87fc8b"
const ACCOUNT_PRIVATE2 = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
const indexer = new Indexer(CKB_RPC_INDEX_URL, CKB_RPC_URL);
const LightCli = new LightClientCli(CKB_CLIENT_CLI_PATH,CKB_LIGHT_RPC_URL)
const LightCli = new LightClientCli(CKB_CLIENT_CLI_PATH, CKB_LIGHT_RPC_URL)


const EVERY_ONE_CAN_PAY_DATA = {
"CODE_HASH": '0xe683b04139344768348499c23eb1326d5a52d6db006c0d2fece00a831f3660d7',
"HASH_TYPE":"data",
"HASH_TYPE": "data",
"TX_HASH": '0xbe5d236f316f5608c53aa682351be4114db07b2e843435e843661ded30924c04',
"INDEX": '0x0',
"DEP_TYPE": 'code'
}

const EVERY_ONE_CAN_PAY_DATA1 = {
"CODE_HASH": '0xe683b04139344768348499c23eb1326d5a52d6db006c0d2fece00a831f3660d7',
"HASH_TYPE":"data1",
"HASH_TYPE": "data1",
"TX_HASH": '0xbe5d236f316f5608c53aa682351be4114db07b2e843435e843661ded30924c04',
"INDEX": '0x0',
"DEP_TYPE": 'code'
Expand Down Expand Up @@ -86,6 +98,12 @@ export {
ACCOUNT_PRIVATE2,
LightCli,
EVERY_ONE_CAN_PAY_TYPE_ID,
EVERY_ONE_CAN_PAY_DATA

EVERY_ONE_CAN_PAY_DATA,
CKB_DEV_PATH,
rpcDevCLient,
CKB_DEV_RPC_URL,
CKB_DEV_RPC_INDEX_URL,
DEV_PATH,
CKB_DEV_INDEX_PATH,
CKB_DEV_LIGHT_CLIENT_PATH
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"test-load": "mocha --config test/runners/mocha/.mocharc.load.jsonc --reporter mochawesome",
"test-data": "mocha --config test/runners/mocha/.mocharc.data.jsonc --reporter mochawesome",
"test-cli": "mocha --config test/runners/mocha/.mocharc.cli.jsonc --reporter mochawesome",
"test-transfer-issue": "mocha --config test/runners/mocha/.mocharc.issue.jsonc --reporter mochawesome"
"test-transfer-issue": "mocha --config test/runners/mocha/.mocharc.issue.jsonc --reporter mochawesome",
"test-index-panic": "mocha --config test/runners/mocha/.mocharc.index-panic.jsonc --reporter mochawesome",
"test-roll-back": "mocha --config test/runners/mocha/.mocharc.light.rollback.jsonc --reporter mochawesome",
"test-roll-back:transfer": "mocha --config test/runners/mocha/.mocharc.light.rollback.jsonc --grep \"transfer\"--reporter mochawesome",
"test-roll-back:cut10": "mocha --config test/runners/mocha/.mocharc.light.rollback.jsonc --grep \"cut 10\"--reporter mochawesome",
"test-roll-back:cut300": "mocha --config test/runners/mocha/.mocharc.light.rollback.jsonc --grep \"cut 300\"--reporter mochawesome"
}
}
39 changes: 17 additions & 22 deletions resource/cli/example-search-key.withFilter.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
{
"script": {
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type",
"args": "0x00010203"
},
"script_type": "lock",
"filter": {
"script": {
"code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
"hash_type": "type",
"args": "0x00010203"
},
"script_len_range": null,
"output_data_len_range": [
"0x16",
"0x378"
],
"output_capacity_range": [
"0xf4240",
"0x5f5e100"
],
"block_range": [
"0x21",
"0x3e7"
],
"script": {
"args": "0x00010203",
"code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
"hash_type": "type"
},
"script_len_range": [
"0x0",
"0x6f"
]
},
"with_data": false,
"group_by_transaction": null
"group_by_transaction": false,
"script": {
"args": "0x00010203",
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type"
},
"script_type": "lock",
"with_data": false
}
19 changes: 19 additions & 0 deletions rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ export async function getScripts(ckbLightClient: string = ckbLightClientRPC) {
return res;
}

export async function checkScriptsInLightClient(scripts:ScriptObject[],ckbLightClient:string = ckbLightClientRPC){
let res = await getScripts(ckbLightClient)
let getScriptList = res.map(result=>{
return {
code_hash:result.script.code_hash,
hash_type:result.script.hash_type,
args:result.script.args
}
})

return scripts.some(checkScript=>{
return getScriptList.some(getScript=>{
return getScript.code_hash == checkScript.code_hash &&
getScript.hash_type == checkScript.hash_type &&
getScript.args == checkScript.args
})
})
}

export async function waitScriptsUpdate(block_num: BI, ckbLightClient: string = ckbLightClientRPC) {
while (true) {
let res = await getScripts(ckbLightClient)
Expand Down
178 changes: 178 additions & 0 deletions service/CkbDevService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import {
ACCOUNT_PRIVATE, ACCOUNT_PRIVATE2, CKB_DEV_INDEX_PATH, CKB_DEV_LIGHT_CLIENT_PATH, CKB_DEV_PATH,
CKB_DEV_RPC_INDEX_URL,
CKB_DEV_RPC_URL,
CKB_LIGHT_RPC_URL, CKB_RPC_INDEX_URL, DEV_PATH,
rpcDevCLient
} from "../config/config";
import {BI} from "@ckb-lumos/bi";
import {getCellsCapacityRequest, ScriptObject, waitScriptsUpdate} from "../rpc";
import {request} from "./index";
import {getCellsByRange} from "./txService";
import {generateAccountFromPrivateKey, TransferService} from "./transfer";
import {sh, shWithTimeout, shWithTimeOutNotErr} from "./node";
import {Sleep} from "./util";

export const transferDevService = new TransferService(CKB_DEV_RPC_URL,CKB_DEV_RPC_INDEX_URL)
export async function cut_miner_and_wait_lightClient_sync(cut_num: number, miner_num: number) {
const before_cut_tip_height = await rpcDevCLient.get_tip_block_number()
if (BI.from(before_cut_tip_height).toNumber() < cut_num) {
await miner_block_number(cut_num - BI.from(before_cut_tip_height).toNumber())
}

await cut_number(cut_num)
await miner_block_number(miner_num)
let tip_num = await rpcDevCLient.get_tip_block_number()
await waitScriptsUpdate(BI.from(tip_num))
}

export async function compare_cells_result(scriptObject1: ScriptObject) {
let compare = true;
const indexCells = await getCellsMsg(scriptObject1, CKB_DEV_RPC_INDEX_URL)
console.log("lightCells:")
const lightCells = await getCellsMsg(scriptObject1, CKB_LIGHT_RPC_URL)
// get indexCells but not in light
const indexNotInLightCells = indexCells.filter(cell => !lightCells.some(lightCell => {
return lightCell.block_number == cell.block_number &&
lightCell.out_point?.tx_hash == cell.out_point?.tx_hash
}))

// get lightCells but not in index
const lightNotInIndexCells = lightCells.filter(cell => !indexCells.some(indexCell => {
return indexCell.block_number == cell.block_number &&
indexCell.out_point?.tx_hash == cell.out_point?.tx_hash
}))
if (indexNotInLightCells.length != 0) {

compare = false
console.log("indexNotInLightCells")
indexNotInLightCells.forEach(cell => {
console.log(
"blockNum:", BI.from(cell.block_number).toNumber(),
" hash:", cell.out_point?.tx_hash,
" index:", cell.out_point?.index
)
})

}
if (lightNotInIndexCells.length != 0) {
compare = false
console.log("lightNotInIndexCells")
lightNotInIndexCells.forEach(cell => {
console.log(
"blockNum:", BI.from(cell.block_number).toNumber(),
" hash:", cell.out_point?.tx_hash,
" index:", cell.out_point?.index
)
})
}
return compare
}

export async function cut_number(cut_number: number) {
const tip_number = BI.from(await rpcDevCLient.get_tip_block_number()).toNumber()
const reset_num = tip_number - cut_number
return truncate_to_block(reset_num)
}
export async function truncate_to_block(block_number:number){
const hash = await rpcDevCLient.get_block_hash(BI.from(block_number).toHexString())
await truncate(CKB_DEV_RPC_URL, hash)
}

export async function truncate(url, hash: string) {
await request(100, url, "truncate", [hash])
}

export async function miner_block_until_number(end_number: number) {

for (let i = 0; i < 10000; i++) {
await miner_block()
const tip_number = BI.from(await rpcDevCLient.get_tip_block_number()).toNumber()
if (tip_number > end_number) {
return
}
console.log("[miner]current:" + tip_number + ", expected:", end_number)
}

}

export async function miner_block_number(height: number) {
const begin_number = BI.from(await rpcDevCLient.get_tip_block_number()).toNumber()
const end_number = begin_number + height
await miner_block_until_number(end_number)
}

export async function getCellsMsg(scriptObject1: ScriptObject, url: string) {
const tip_number = await rpcDevCLient.get_tip_block_number();
let cells = await getCellsByRange(scriptObject1, "lock", undefined, url, ["0x0", BI.from(tip_number).toHexString()])
cells.forEach(tx => console.log(
"blockNum:", BI.from(tx.block_number).toNumber(),
" hash:", tx.out_point?.tx_hash,
" index:", tx.out_point?.index
))
return cells

}



export async function miner_block(kill_port: boolean = true) {
if (kill_port) {
await shWithTimeOutNotErr("lsof -i:8888 | grep LIS | awk '{print $2}'| xargs -n1 kill -9", 2000)
}
await shWithTimeOutNotErr(" cd " + CKB_DEV_PATH + " && ./ckb miner -C dev -l 40 > block.log", 2000)
if(kill_port){
await shWithTimeOutNotErr("lsof -i:8888 | grep LIS | awk '{print $2}'| xargs -n1 kill -9", 2000)
}
await shWithTimeOutNotErr("cat " + CKB_DEV_PATH + "/block.log | grep Found", 1000)
}

export async function cleanCkbLightClientEnv() {
await sh("cd " + DEV_PATH + " && rm -rf ckb-light-client/target/release/data")
}

export async function stopCkbLightClient() {
await shWithTimeOutNotErr("pkill ckb-light-client",1000)
}

export async function startCkbLightClient() {
await sh("cd " + CKB_DEV_LIGHT_CLIENT_PATH +" && RUST_LOG=info,ckb_light_client=trace ./ckb-light-client run --config-file ./config.toml > node.log 2>&1 &")
}

export async function cleanAndRestartCkbLightClientEnv() {
await stopCkbLightClient()
await cleanCkbLightClientEnv()
await startCkbLightClient()
}

export async function cleanAllEnv() {
await sh("cd " + DEV_PATH + " && sh clean.sh")
}

export async function startEnv() {
await shWithTimeout("cd " + DEV_PATH + " sh start.sh", 100_000)
}

export async function restartAndSyncCkbIndex() {
await shWithTimeOutNotErr(" pkill ckb-indexer",1000)
await sh("cd " + CKB_DEV_INDEX_PATH + " && rm -rf ckb-test && RUST_LOG=info ./ckb-indexer -s ckb-test > ckb-indexer.log 2>&1 &")
await checkCKbIndexSync()
}

export async function checkCKbIndexSync() {
let acc1 = generateAccountFromPrivateKey(ACCOUNT_PRIVATE);
let tip_num = await rpcDevCLient.get_tip_block_number()
for (let i = 0; i < 1000; i++) {
let cap = await getCellsCapacityRequest({
search_key: {
script: acc1.lockScript,
script_type: "lock"
}
},CKB_DEV_RPC_INDEX_URL)
if (BI.from(tip_num).lte(BI.from(cap.block_number))) {
return
}
console.log("current sync:", BI.from(cap.block_number).toNumber(), " expected:", BI.from(tip_num).toNumber())
await Sleep(1000)
}
}
29 changes: 27 additions & 2 deletions service/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,43 @@ export {
}


async function sh(cmd: string) {
export async function shWithTimeOutNotErr(cmd:string,timeout:number){
console.log('sh:', cmd)
return new Promise(function (resolve, reject) {
exec(cmd, { timeout: 10000},(err, stdout, stderr) => {
let c = exec(cmd, { timeout: timeout},(err, stdout, stderr) => {
if (err) {
// console.log(err)
if(!c.killed){
c.kill()
}
resolve(err);
} else {
console.log('response:', stdout)
resolve({stdout, stderr});
}
});
});
}
export async function shWithTimeout(cmd:string,timeout:number){
console.log('sh:', cmd)
return new Promise(function (resolve, reject) {
let c = exec(cmd, { timeout: timeout},(err, stdout, stderr) => {
if (err) {
console.log(err)
console.log(c.pid)
console.log("killed:",c.killed)
// c.kill()
console.log("killed:",c.killed)
reject(err);
} else {
console.log('response:', stdout)
resolve({stdout, stderr});
}
});
});
}
export async function sh(cmd: string) {
return await shWithTimeout(cmd,10000)

}

Expand Down
Loading