Skip to content

Commit

Permalink
Merge pull request godwokenrises#24 from nervosnetwork/deposition-fixes
Browse files Browse the repository at this point in the history
fix: Deposition logic bug fixes
  • Loading branch information
xxuejie authored Dec 25, 2020
2 parents 779d5f3 + 79ad5cc commit 3f89d12
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
41 changes: 36 additions & 5 deletions packages/base/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hash, HexNumber, Script } from "@ckb-lumos/base";
import { Hash, HexNumber, Script, denormalizers } from "@ckb-lumos/base";
import { normalizers, BigIntToHexString, Reader } from "ckb-js-toolkit";
import * as schemas from "../schemas/godwoken";

// Taken for now from https://github.com/xxuejie/ckb-js-toolkit/blob/68f5ff709f78eb188ee116b2887a362123b016cc/src/normalizers.js#L17-L69,
// later we can think about exposing those functions directly.
Expand Down Expand Up @@ -69,7 +70,7 @@ function toNormalize(normalize: Function) {
export interface DepositionRequest {
capacity: HexNumber;
amount: HexNumber;
sudtScript: Script;
sudt_script: Script;
script: Script;
}

Expand All @@ -80,7 +81,7 @@ export function NormalizeDepositionRequest(
return normalizeObject(debugPath, request, {
capacity: normalizeHexNumber(8),
amount: normalizeHexNumber(16),
sudtScript: toNormalize(normalizers.NormalizeScript),
sudt_script: toNormalize(normalizers.NormalizeScript),
script: toNormalize(normalizers.NormalizeScript),
});
}
Expand All @@ -100,8 +101,38 @@ export function NormalizeHeaderInfo(
});
}

export interface CustodianLockArgs {
export interface DepositionLockArgs {
owner_lock_hash: Hash;
layer2_lock: Script;
cancel_timeout: HexNumber;
}

export function DenormalizeDepositionLockArgs(
lockArgs: schemas.DepositionLockArgs
) {
return {
owner_lock_hash: new Reader(
lockArgs.getOwnerLockHash().raw()
).serializeJson(),
layer2_lock: denormalizers.DenormalizeScript(lockArgs.getLayer2Lock()),
cancel_timeout:
"0x" + lockArgs.getCancelTimeout().toLittleEndianBigUint64().toString(16),
};
}

export function NormalizeDepositionLockArgs(
depositionLockArgs: object,
{ debugPath = "deposition_lock_args" } = {}
) {
return normalizeObject(debugPath, depositionLockArgs, {
owner_lock_hash: normalizeRawData(32),
layer2_lock: toNormalize(normalizers.NormalizeScript),
cancel_timeout: normalizeHexNumber(8),
});
}

export interface CustodianLockArgs {
deposition_lock_args: DepositionLockArgs;
deposition_block_hash: Hash;
deposition_block_number: HexNumber;
}
Expand All @@ -111,7 +142,7 @@ export function NormalizeCustodianLockArgs(
{ debugPath = "custondian_lock_args" } = {}
) {
return normalizeObject(debugPath, args, {
owner_lock_hash: normalizeRawData(32),
deposition_lock_args: toNormalize(NormalizeDepositionLockArgs),
deposition_block_hash: normalizeRawData(32),
deposition_block_number: normalizeHexNumber(8),
});
Expand Down
10 changes: 7 additions & 3 deletions packages/runner/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ export class Runner {
}

async start() {
this.logger("debug", `Rollup Type Hash: ${this.rollupTypeHash}`);
this.logger("debug", `CKB Address: ${this._ckbAddress()}`);
// Wait for indexer sync
await this.indexer.waitForSync();

Expand Down Expand Up @@ -319,9 +321,7 @@ export class Runner {
"0x" + rawL2Block.getNumber().toLittleEndianBigUint64().toString(16);
return depositionEntries.map(({ cell, lockArgs }) => {
const custodianLockArgs = {
owner_lock_hash: new Reader(
lockArgs.getOwnerLockHash().raw()
).serializeJson(),
deposition_lock_args: types.DenormalizeDepositionLockArgs(lockArgs),
deposition_block_hash: l2BlockHash,
deposition_block_number: l2BlockNumber,
};
Expand Down Expand Up @@ -365,6 +365,10 @@ export class Runner {
if (medianTime - this.lastProduceBlockTime >= 20n * 1000n) {
this.logger("info", "Generating new block!");
const depositionEntries = await this._queryValidDepositionRequests();
this.logger(
"debug",
`Valid deposition entries: ${depositionEntries.length}`
);
const depositionRequests = depositionEntries.map(
({ packedRequest }) => packedRequest
);
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export async function tryExtractDepositionRequest(
amount,
capacity: cell.cell_output.capacity,
script: DenormalizeScript(lockArgs.getLayer2Lock()),
sudtScript,
sudt_script: sudtScript,
};
const packedRequest = new Reader(
schemas.SerializeDepositionRequest(
Expand Down

0 comments on commit 3f89d12

Please sign in to comment.