Skip to content

Commit

Permalink
part of the fix for ensuring block ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
epheph committed Nov 26, 2017
1 parent 87769ac commit 4e32c67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
13 changes: 4 additions & 9 deletions src/blockchain/start-augur-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { makeLogListener } from "./make-log-listener";
import { processBlock, processBlockRemoval } from "./process-block";
import { logError } from "../utils/log-error";

export function startAugurListeners(db: Knex, augur: Augur, callback: (block: Block) => void): void {
let seenFirstBlock = false;
export function startAugurListeners(db: Knex, augur: Augur): void {
augur.events.startListeners({
Augur: {
MarketCreated: makeLogListener(db, augur, "Augur", "MarketCreated"),
Expand All @@ -27,11 +26,7 @@ export function startAugurListeners(db: Knex, augur: Augur, callback: (block: Bl
Transfer: makeLogListener(db, augur, "LegacyReputationToken", "Transfer"),
Approval: makeLogListener(db, augur, "LegacyReputationToken", "Approval"),
},
}, (block: Block): void => {
if (!seenFirstBlock) {
callback(block);
seenFirstBlock = true;
}
processBlock(db, augur, block);
}, (block: Block): void => processBlockRemoval(db, block), (): void => {});
}, (block: Block): void => processBlock(db, augur, block),
(block: Block): void => processBlockRemoval(db, block),
(): void => {});
}
20 changes: 13 additions & 7 deletions src/blockchain/sync-augur-node-with-blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ function getNetworkID(db: Knex, augur: Augur, callback: (err: Error|null, networ
export function syncAugurNodeWithBlockchain(db: Knex, augur: Augur, ethereumNodeEndpoints: EthereumNodeEndpoints, uploadBlockNumbers: UploadBlockNumbers, callback: ErrorCallback): void {
augur.connect({ ethereumNode: ethereumNodeEndpoints }, (): void => {
console.log("Waiting for first block...");
startAugurListeners(db, augur, (block: Block): void => {
getNetworkID(db, augur, (err: Error|null, networkID: string|null) => {
if (err) {
augur.events.stopListeners();
return callback(err);
}
db("blockchain_sync_history").max("blockNumber as highestBlockNumber").asCallback((err: Error|null, rows?: Array<HighestBlockNumberRow>): void => {
getNetworkID(db, augur, (err: Error|null, networkID: string|null) => {
if (err) {
augur.events.stopListeners();
return callback(err);
}
augur.rpc.eth.getBlockByNumber(["latest", false], (block: any): void => {
db.max("block as highestBlockNumber").from(function(this: Knex.QueryBuilder): void {
this.max("highestBlockNumber as block").from("blockchain_sync_history").unionAll(function(this: Knex.QueryBuilder): void {
this.max("blockNumber as block").from("blocks");
}).as("maxBlocks");
}).asCallback((err: Error|null, rows?: Array<HighestBlockNumberRow>): void => {
if (err) return callback(err);
if (!rows || !rows.length || !rows[0]) return callback(new Error("blockchain_sync_history lookup failed"));
const row: HighestBlockNumberRow = rows[0];
Expand All @@ -50,6 +54,8 @@ export function syncAugurNodeWithBlockchain(db: Knex, augur: Augur, ethereumNod
downloadAugurLogs(db, augur, fromBlock, highestBlockNumber, (err?: Error|null): void => {
if (err) return callback(err);
db.insert({ highestBlockNumber }).into("blockchain_sync_history").asCallback(callback);
// TODO: Modify this function (and all calls down to blockstream) to take highestBlockNumber
startAugurListeners(db, augur);
});
} else {
callback(new Error("Please clear your augur.db and start over (must sync from scratch until issue #4386 is resolved)"));
Expand Down

0 comments on commit 4e32c67

Please sign in to comment.