This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add verify command and optimize relay check (#85)
* feat(cmd): add redeem debug tool * chore(cmd): init dj-verify to binary list * fix(relay): return submitted proposals * feat(proposal): add several pre-check for submit proposals * fix(relay): the check of pendingHeaders * feat(relay): complete proposal check * chore(deps): update the version of util-crypto * fix(lint): the object access of relay check * feat(postinsall): remove old types.json after install * fix(relay): add lastConfirmed check
- Loading branch information
Showing
14 changed files
with
159 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env node | ||
import { Config, log } from "../util"; | ||
import { API, ShadowAPI } from "../api"; | ||
|
||
(async () => { | ||
const args = process.argv.slice(2); | ||
if (args.length !== 1) { | ||
console.log("Usage: dj-verify <tx>/<confirmed> [-d deposit]"); | ||
console.log("Example: dj-verify 0x1d3ef601b9fa4a7f1d6259c658d0a10c77940fa5db9e10ab55397eb0ce88807d/8694126"); | ||
console.log("Example: dj-verify 0x1d3ef601b9fa4a7f1d6259c658d0a10c77940fa5db9e10ab55397eb0ce88807d/8694126 -d"); | ||
return; | ||
} | ||
|
||
/// Init logs | ||
process.env.LOGGER = "ALL"; | ||
|
||
/// Init API | ||
const conf = new Config(); | ||
const api = await API.auto(); | ||
const shadow = new ShadowAPI(conf.shadow); | ||
const target = args[0].split("/"); | ||
|
||
// Trigger relay | ||
try { | ||
await api.redeem( | ||
args.indexOf("-d") > -1? "Deposit": "Token", | ||
await shadow.getReceipt(target[0], Number.parseInt(target[1], 10)) | ||
); | ||
log.ox(`Redeem tx ${target} succeed!`); | ||
} catch (e) { | ||
log.ex(e); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ShadowAPI, API } from "../../api"; | ||
import { log } from "../../util"; | ||
import { DispatchError } from "@polkadot/types/interfaces/types"; | ||
import game from "./game"; | ||
|
||
export function listen(api: API, shadow: ShadowAPI) { | ||
// Subscribe to system events via storage | ||
api._.query.system.events((events: any) => { | ||
events.forEach(async (record: any) => { | ||
const { event, phase } = record; | ||
const types = event.typeDef; | ||
|
||
// Chain events | ||
switch (event.method) { | ||
case "NewRound": | ||
await game(event, phase, types, api, shadow); | ||
} | ||
|
||
if (event.data[0] && (event.data[0] as DispatchError).isModule) { | ||
log.err(api._.registry.findMetaError( | ||
(event.data[0] as DispatchError).asModule.toU8a(), | ||
)); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * as Cache from "./cache"; | ||
export { listen as guard } from "./guard"; | ||
export { listen as relay } from "./relay"; | ||
export { listen as redeem } from "./redeem"; | ||
export { listen as ethereum } from "./eth"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ShadowAPI, API } from "../api"; | ||
import { delay } from "../util"; | ||
import { ITx } from "../types"; | ||
|
||
/// Approved handler | ||
export async function listen( | ||
api: API, | ||
shadow: ShadowAPI, | ||
queue: ITx[], | ||
) { | ||
setInterval(async () => { | ||
const lastConfirmed = await api.lastConfirm(); | ||
for (const tx of queue.filter((t) => t.blockNumber < lastConfirmed)) { | ||
await api.redeem(tx.ty, await shadow.getReceipt(tx.tx, lastConfirmed)); | ||
await delay(10000); | ||
}; | ||
queue = queue.filter((t) => t.blockNumber >= lastConfirmed); | ||
}, 30000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import { ShadowAPI, API } from "../../api"; | ||
import { log } from "../../util"; | ||
import { ITx } from "../../types"; | ||
import { ShadowAPI, API } from "../api"; | ||
import { log } from "../util"; | ||
import { ITx } from "../types"; | ||
|
||
// Listen and submit proposals | ||
export default function relay(api: API, shadow: ShadowAPI, queue: ITx[]) { | ||
const submitted: number[] = []; | ||
|
||
// Trigger relay every 180s | ||
export function listen(api: API, shadow: ShadowAPI, queue: ITx[]) { | ||
setInterval(async () => { | ||
if (queue.length < 1) return; | ||
|
||
// Check last confirm | ||
const lastConfirmed = await api.lastConfirm(); | ||
const maxBlock = queue.sort((p, q) => q.blockNumber - p.blockNumber)[0].blockNumber; | ||
if (lastConfirmed === maxBlock + 1) { | ||
if (lastConfirmed >= maxBlock + 1) { | ||
return; | ||
} | ||
|
||
// Submit new proposal | ||
const target = Math.max(lastConfirmed, maxBlock) + 1; | ||
if (!(await api.shouldRelay(target))) { | ||
return; | ||
}; | ||
|
||
// Relay txs | ||
log(`Currently we have ${queue.length} txs are waiting to be redeemed`); | ||
await api.submitProposal([await shadow.getProposal( | ||
lastConfirmed, | ||
target, | ||
target - 1, | ||
)]).catch(log.err); | ||
|
||
// Refresh target | ||
submitted.push(target); | ||
}, 60000); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.