This commit is contained in:
Josh Harvey 2017-06-24 18:52:55 +03:00
parent 45e6e2b82d
commit 2779dcd6f3
5 changed files with 34 additions and 20 deletions

View file

@ -4,6 +4,7 @@ const db = require('./db')
const BN = require('./bn')
const plugins = require('./plugins')
const logger = require('./logger')
const pp = require('./pp')
module.exports = {post, monitorPending}
@ -27,9 +28,11 @@ function atomic (machineTx, pi) {
return preProcess(dbTx, machineTx, pi)
.then(preProcessedTx => upsert(dbTx, preProcessedTx))
.then(vector => {
.then(r => {
pp('DEBUG701.5')(r)
return insertNewBills(billRows, machineTx)
.then(_.constant(_.concat(vector, machineTx.bills)))
.then(_.constant(_.set('bills', machineTx.bills, r)))
.then(pp('DEBUG702'))
})
})
})
@ -41,11 +44,12 @@ function atomic (machineTx, pi) {
}
function post (machineTx, pi) {
console.log('DEBUG700: %j', machineTx)
return db.tx(atomic(machineTx, pi))
.then(txVector => {
const [, updatedTx] = txVector
.then(r => {
const updatedTx = r.tx
return postProcess(txVector, pi)
return postProcess(r, pi)
.then(changes => update(updatedTx, changes))
.then(tx => _.set('bills', machineTx.bills, tx))
})
@ -168,11 +172,11 @@ function insertNewBills (billRows, machineTx) {
function upsert (dbTx, preProcessedTx) {
if (!dbTx) {
return insert(preProcessedTx)
.then(tx => [dbTx, tx])
.then(tx => ({dbTx, tx}))
}
return update(dbTx, diff(dbTx, preProcessedTx))
.then(tx => [dbTx, tx])
.then(tx => ({dbTx, tx}))
}
function insert (tx) {
@ -195,6 +199,7 @@ function update (tx, changes) {
}
function registerTrades (pi, newBills) {
console.log('DEBUG600: %j', newBills)
_.forEach(bill => pi.buy(bill), newBills)
}
@ -218,13 +223,12 @@ function isClearToSend (oldTx, newTx) {
(!oldTx || (!oldTx.sendPending && !oldTx.sendConfirmed))
}
function postProcess (txVector, pi) {
const [dbTx, updatedTx, newBills] = txVector
function postProcess (r, pi) {
console.log('DEBUG701: %j', r)
registerTrades(pi, r.bills)
registerTrades(pi, newBills)
if (isClearToSend(dbTx, updatedTx)) {
return pi.sendCoins(updatedTx)
if (isClearToSend(r.dbTx, r.tx)) {
return pi.sendCoins(r.tx)
.then(txHash => ({
txHash,
sendConfirmed: true,
@ -248,7 +252,7 @@ function postProcess (txVector, pi) {
sendPending
}
})
.then(r => logAction(r, updatedTx))
.then(sendRec => logAction(sendRec, r.tx))
}
return Promise.resolve({})