Skip to content

separate btc nodes for master and confirmation and no dependence. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions controller/bitcoinCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class BitcoinCtrl {
this.api = new BitcoinNodeWrapper(conf.node);
this.network = this.isMainNet ? networks.bitcoin : networks.testnet;

// stores unknown labels => the times we've complained about them
this.unknownLabels = new Map();
// stores unknown addresses => the times we've complained about them
this.unknownAddresses = new Map();
}

getDerivedPubKeys(index) {
Expand Down Expand Up @@ -98,18 +98,22 @@ class BitcoinCtrl {
return;
}

label = user.label;

let added = await dbCtrl.getDeposit(txId, label, vout);

// try finding one with -1 vout, so that we do not mess
// the original database!
if (added == null) {
added = await dbCtrl.getDeposit(txId, label, -1);
if (added) {
console.warn(
console.error(
"found a deposit tx %s for label %s without vout fix",
txId,
label
);

throw new Error("found a deposit tx without vout fix");
}
}

Expand Down Expand Up @@ -167,7 +171,7 @@ class BitcoinCtrl {
* Otherwise check it is confirmed when has more than [thresholdConfirmations] confirmations
*/
async checkDepositTxs() {
const addrLabels = new Set(await dbCtrl.getUserLabels(-1, -1));
const addresses = new Set(await dbCtrl.getUserAddresses(-1, -1));
const blockBookmark = await dbCtrl.getBookmark(
"block_bookmark",
null
Expand All @@ -179,8 +183,8 @@ class BitcoinCtrl {
);

const txList = (since.transactions || []).filter(tx => {
if (! addrLabels.has(tx.label)) {
this.complainAboutUnknownLabel(tx.label);
if (! addresses.has(tx.address)) {
this.complainAboutAddressLabel(tx.address);
return false;
}
return true;
Expand All @@ -204,8 +208,8 @@ class BitcoinCtrl {
for (const tx of txList) {
const confirmations = tx && tx.confirmations;

if (! addrLabels.has(tx.label)) {
console.log("ignoring unknown label %s", tx.label);
if (! addresses.has(tx.address)) {
console.log("ignoring unknown address %s", tx.address);
continue;
}

Expand Down Expand Up @@ -266,14 +270,14 @@ class BitcoinCtrl {
});
}

complainAboutUnknownLabel(label) {
const timeout = this.unknownLabels.get(label)
complainAboutUnknownAddress(addresse) {
const timeout = this.unknownAddresses.get(address)

if (! timeout || timeout < Date.now()) {
console.log("ignoring unknown label %s", label);
console.log("ignoring unknown address %s", address);

// 1.5 hours
this.unknownLabels.set(label, Date.now() + 1.5 * 60 * 60 * 1000);
this.unknownAddresses.set(address, Date.now() + 1.5 * 60 * 60 * 1000);
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions controller/dbCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ class DbCtrl {
vout: vout,
};

if (label) {
criteria.userAdrLabel = label;
}
// if (label) {
// criteria.userAdrLabel = label;
// }

const found = await this.transactionRepository.findOne(criteria);
if (found || vout === -1) {
Expand Down Expand Up @@ -248,10 +248,15 @@ class DbCtrl {
}

async confirmDeposit(txHash, label, vout) {
if (label != null) {
throw new Error("label parameter no longer used but was specified");
}
if (vout == -1) {
throw new Error("vout must be defined in order to confirm deposit");
}
try {
return await this.transactionRepository.update({
txHash: txHash,
userAdrLabel: label,
type: "deposit",
vout: vout,
}, {status: 'confirmed'});
Expand Down Expand Up @@ -326,6 +331,16 @@ class DbCtrl {
return (users || []).map(u => u.label);
}

async getUserAddresses(skip = 0, size = 10) {
const users = await this.userRepository.find({}, {
offset: skip,
limit: size
});

return (users || []).map(u => u.btcadr);
}


// /**
// * Use with caution, most likely you need to search for both txHash and userAdrLabel to make sure item is unique
// * @param { string[] } txHashList
Expand Down
29 changes: 14 additions & 15 deletions controller/mainCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ class MainController {
}

async processDeposits(d) {
let depositFound = await dbCtrl.getDeposit(d.txHash, d.label, d.vout);
// the label parameter is not used any more
let depositFound = await dbCtrl.getDeposit(d.txHash, null, d.vout);

if (depositFound) {
if (depositFound.status === 'confirmed') {
Expand All @@ -320,16 +321,13 @@ class MainController {

// stopgap fix to ensure that it goes to db.
if (depositFound.vout === -1) {
await dbCtrl.confirmDeposit(d.txHash, d.label, depositFound.vout);

telegramBot.sendMessage(`OLD ADDRESS / VOUT -1 SEEN: ${d.txHash}, ${d.label} - NOT PROCESSING FURTHER`);
return;
throw new Error(`This should not happen, transactions with vout -1 no longer confirmable`);
} else {
if (depositFound.vout !== d.vout) {
throw new Error(`This should not happen, vouts not matching ${depositFound.vout} != ${d.vout}, ${depositFound.id}`);
}

await dbCtrl.confirmDeposit(d.txHash, d.label, d.vout);
await dbCtrl.confirmDeposit(d.txHash, null, d.vout);
}
}

Expand All @@ -338,21 +336,22 @@ class MainController {
telegramBot.sendMessage("Deposit outside the limit!");
}

const user = await dbCtrl.getUserByBtcAddress(d.address);
if (!user) {
console.error("Error finding user");
return;
}

if (depositFound == null) {
const resDb = await dbCtrl.addDeposit(d.label, d.txHash, d.val, true, d.vout);
// real user label from user.label
const resDb = await dbCtrl.addDeposit(user.label, d.txHash, d.val, true, d.vout);

if (!resDb) {
console.error("Error adding deposit to db");
return;
}
}

const user = await dbCtrl.getUserByLabel(d.label);
if (!user) {
console.error("Error finding user");
return;
}

this.emitToUserSocket(user.label, 'depositTx', {
txHash: d.txHash,
value: (d.val / 1e8).toFixed(8),
Expand All @@ -372,8 +371,8 @@ class MainController {
return;
}

await dbCtrl.updateDeposit(d.txHash, d.vout, resTx.txId, d.label);
await dbCtrl.addTransferTx(d.label, resTx.txHash, d.val, resTx.txId);
await dbCtrl.updateDeposit(d.txHash, d.vout, resTx.txId, user.label);
await dbCtrl.addTransferTx(user.label, resTx.txHash, d.val, resTx.txId);

console.log("Successfully sent " + d.val + " to " + user.web3adr);
console.log(resTx);
Expand Down