-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move zero into processor and add redis to the existing docker image
- Loading branch information
Showing
13 changed files
with
358 additions
and
250 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# How to add new processors? | ||
|
||
1. Read our `lite.ts`(simple version) or the `standard.ts`(more complex version) as examples | ||
2. Assume you plan to add a new processor called `XXX`. First create the file `XXX.ts` under current directory. | ||
3. Add `export class XXX implements ProcessorInterface` where all our processors has some common methods such as `init` and `reset`. Implement those methods. | ||
4. Go to `index.ts` in this directory, register your processors with `enum Mode`, `private processors` (class property), `reset` method and add your custom start up condition in `getRunningModes` method | ||
5. Done |
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,80 @@ | ||
import Bull from "bull"; | ||
import bunyan from "bunyan"; | ||
import { ProcessorInterface } from "./type"; | ||
import { ZeroService } from "../service/zero"; | ||
import { config } from "../config"; | ||
|
||
const NAME = "ZERO"; | ||
|
||
export class Zero implements ProcessorInterface { | ||
private queue: Bull.Queue; | ||
private logger: bunyan; | ||
private zeroService: ZeroService; | ||
|
||
constructor(logger: bunyan) { | ||
this.logger = logger; | ||
this.queue = new Bull(NAME); | ||
this.zeroService = new ZeroService(logger); | ||
} | ||
init() { | ||
this.logger.info("Initialising XDC-Zero"); | ||
this.queue.process(async (_, done) => { | ||
this.logger.info("⏰ Executing xdc-zero periodically"); | ||
try { | ||
done(null, await this.processEvent()); | ||
} catch (error) { | ||
this.logger.error("Fail to process xdc-zero job", { | ||
message: error.message, | ||
}); | ||
// Report the error | ||
done(error); | ||
await this.reset(); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
async reset(): Promise<void> { | ||
await this.queue.add({}, { jobId: NAME, repeat: { cron: config.cronJob.zeroJobExpression}}); | ||
} | ||
|
||
async processEvent() { | ||
const payloads = await this.zeroService.getPayloads(); | ||
if (payloads.length) { | ||
this.logger.info("Nothing to process in xdc-zero, wait for the next event log"); | ||
return; | ||
} | ||
const lastPayload = payloads[payloads.length - 1]; | ||
const lastIndexFromSubnet = lastPayload[0]; | ||
|
||
const lastIndexfromParentnet = await this.zeroService.getIndexFromParentnet(); | ||
|
||
const lastBlockNumber = lastPayload[7]; | ||
const cscBlockNumber = await this.zeroService.getLatestBlockNumberFromCsc(); | ||
if (cscBlockNumber < lastBlockNumber) { | ||
this.logger.info( | ||
"wait for csc block lastBlockNumber:" + | ||
lastBlockNumber + | ||
" cscBlockNumber:" + | ||
cscBlockNumber | ||
); | ||
return; | ||
} | ||
|
||
if (lastIndexFromSubnet > lastIndexfromParentnet) { | ||
for (let i = lastIndexfromParentnet; i < lastIndexFromSubnet; i++) { | ||
if (payloads?.[i]?.[6]) { | ||
const proof = await this.zeroService.getProof(payloads[i][6]); | ||
await this.zeroService.validateTransactionProof( | ||
proof.key, | ||
proof.receiptProofValues, | ||
proof.txProofValues, | ||
proof.blockHash | ||
); | ||
this.logger.info("sync zero index " + i + " success"); | ||
} | ||
} | ||
} | ||
this.logger.info("Completed the xdc-zero sync, wait for the next cycle"); | ||
} | ||
} |
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,21 @@ | ||
import Web3 from "web3"; | ||
import { NetworkInformation } from "../types"; | ||
|
||
const MAINNET_EXTENSION_NAME = "xdcMainnet"; | ||
|
||
export interface Web3WithExtension extends Web3 { | ||
xdcMainnet: { | ||
getNetworkInformation: () => Promise<NetworkInformation> | ||
} | ||
} | ||
|
||
export const mainnetExtensions = { | ||
property: MAINNET_EXTENSION_NAME, | ||
methods: [ | ||
{ | ||
name: "networkInformation", | ||
call: "XDPoS_networkInformation" | ||
} | ||
] | ||
}; | ||
|
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,5 @@ | ||
export interface NetworkInformation { | ||
Denom: string; | ||
NetworkId: number; | ||
NetworkName: string; | ||
} |
Oops, something went wrong.