Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Cleans up syncing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-miller-0 committed Feb 10, 2018
1 parent 1f50d18 commit 6e7a250
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 34 deletions.
25 changes: 1 addition & 24 deletions src/lib/Bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ class Bridge {
// Get current data on the bridges
getBridgeData(queryAddr, bridgedAddr, client, cb) {
bridges.getLastBlock(queryAddr, bridgedAddr, client, (err, lastBlock) => {
console.log('lastBlock', lastBlock)
if (err) { cb(err); }
else {
this.bridgeData[queryAddr].lastBlocks[bridgedAddr] = lastBlock;
bridges.getProposer(queryAddr, client, (err, proposer) => {
console.log('proposer', proposer)
if (err) { cb(err); }
else {
this.bridgeData[queryAddr].proposer = proposer;
Expand Down Expand Up @@ -180,7 +178,7 @@ class Bridge {
if (i != j) {
const chain = this.addrs[j];
const lastBlock = bdata.lastBlocks[chain];
const currentBlock = parseInt(this.cache[j][this.cache[j].length - 2][1]);
const currentBlock = parseInt(this.cache[j][this.cache[j].length - 1][0]);
const start = lastBlock + 1;
const end = lastBlock + 1 + util.lastPowTwo(currentBlock - lastBlock - 1);
if (end - start > this.proposeThreshold) {
Expand All @@ -200,26 +198,9 @@ class Bridge {
}
}

/*propose(queryAddr, bridgedAddr, client, cb) {
const d = this.bridgeData[queryAddr][bridgedAddr];
const currentN = this.cache[this.cache.length - 1].n;
if (d.proposer != this.wallet.getAddress() || this.proposeThreshold - 1 > currentN - d.lastBlock) {
// Do nothing if you're not the propose and/or not enough blocks have elapsed
cb(null, null);
} else {
// Get the root
const range = util.lastPowTwo(currentN - d.lastBlock - 1);
console.log('range', range)
getProposalRoot(queryAddr, d.lastBlock + 1, d.lastBlock + 1 + range, (err, headerRoot) => {
// Broadcast root with metadata to all known peers
})
}
}*/

// If this client is elected as the proposer, get the relevant data and form
// the block header Merkle root.
getProposalRoot(chain, startBlock, endBlock, cb) {
console.log('startBlock', startBlock, 'endBlock', endBlock)
sync.loadHeaders(startBlock, endBlock, `${this.datadir}/${chain}/headers`, (err, headers, n) => {
if (n < endBlock) { cb('Not synced to that block. Try again later.'); }
else {
Expand All @@ -229,8 +210,6 @@ class Bridge {
})
}



// Handle an incoming socket message
handleMsg(data) {
const msg = JSON.parse(data.toString('utf8'));
Expand All @@ -239,10 +218,8 @@ class Bridge {
this.verifyProposedRoot(msg.data, (err, sig) => {
if (err) { logger.log('warn', `Error with SIGREQ: ${err}`); }
else {
console.log('got sig', 'peer', this.peers[msg.from])
if (!this.peers[msg.from] || this.peers[msg.from].state == 'closed') { this.addPeer(msg.from); }
msg.data.sig = sig;
console.log('broadcasting sig', msg.data)
this.broadcastMsg({ type: 'SIGPASS', data: msg.data });
}
});
Expand Down
3 changes: 0 additions & 3 deletions src/lib/util/bridges.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ exports.propose = function(sigs, bridge, mappedChain, wallet, client, cb, gasPri
const from = wallet.getAddress();
let sigData = '0x';
Object.keys(sigs).forEach((i) => {
console.log('\nsig', sigs[i], '\n')
sigData += `${leftPad(sigs[i].sig.r, 64, '0')}${leftPad(sigs[i].sig.s, 64, '0')}${leftPad(sigs[i].sig.v.toString(16), 64, '0')}`;
})
// Ensure there are enough signatures from stakers to meet the threshold
Expand Down Expand Up @@ -65,7 +64,6 @@ exports.propose = function(sigs, bridge, mappedChain, wallet, client, cb, gasPri
gas: 500000,
nonce: nonce,
};
console.log('tx', tx)
const signedTx = wallet.signTx(tx);
// Send the proposal tx!
client.eth.sendSignedTransaction(signedTx, (err, h) => {
Expand Down Expand Up @@ -120,7 +118,6 @@ function _checkSigsContract(hRoot, chain, start, end, sigData, bridge, client, c
[ hRoot, chain, start, end, sigData ]
)
client.eth.call({ to: bridge, data: call }, (err, nSigs) => {
console.log('nSigs', nSigs)
if (err) { cb(err); }
else {
getThreshold(bridge, client, (err, thresh) => {
Expand Down
15 changes: 8 additions & 7 deletions src/lib/util/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ lastHeader=`0x${leftPad(0, 64, '0')}`) {
if (currentBlockN == lastBlockN) {
fStream.end(_stringify(cache));
cb(null, _zipCache(cache));
}
else {
} else {
client.eth.getBlock(lastBlockN + 1, (err, block) => {
if (err) { cb(err); }
else {
Expand Down Expand Up @@ -69,16 +68,18 @@ exports.loadHeaders = function(startN, endN, fPath, cb) {
reader.on('error', (err) => { cb(err); reader.close(); })
reader.on('line', (_line) => {
line = _line.split(',').splice(1); // Every line has a leading comma
if (line[0] + 99 > startN) { // 100 items per line
if (parseInt(line[0]) + 99 > parseInt(startN)) { // 100 items per line
for (let i = 0; i < line.length; i += 5) {
if (line[i] >= startN && line[i] <= endN) {
// Save the header if we are in the range of desired values
headers.push(line[i + 4]);
lastN = line[i];
} else {
} else if (line[i] > endN){
reader.close();
}
}
} else {
reader.close()
}
})
reader.on('close', () => { cb(null, headers, lastN); })
Expand All @@ -96,15 +97,15 @@ function _stringify(data) {
function _zipCache(data) {
let c = [];
data.forEach((d) => {
c.push([ d.n, d.timestamp, d.transactionsRoot, d.receiptsRoot ]);
c.push([ d.n, d.timestamp, d.transactionsRoot, d.receiptsRoot, d.header ]);
})
return c;
}

function _zipLineToCache(line) {
let c = [];
for (let i = 0; i < line.length; i += 4) {
c.push([ line[i], line[i + 1], line[i + 2], line[i + 3] ]);
for (let i = 0; i < line.length; i += 5) {
c.push([ line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4] ]);
}
return c;
}
Expand Down

0 comments on commit 6e7a250

Please sign in to comment.