Skip to content
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

36 outgoing server delay #37

Merged
merged 60 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
e7be42b
incoming server processing done & unit tested
proletesseract Jun 12, 2017
bc1e1fe
outgoing scripts done. tests added
proletesseract Jun 13, 2017
58ec7a8
update validation to handle smaller min amount
proletesseract Jun 14, 2017
7674582
log out decrypted data
proletesseract Jun 14, 2017
878c06b
parse both incoming and outgoing encrypted data as json
proletesseract Jun 14, 2017
137cb2b
version logging added
proletesseract Jun 15, 2017
967608e
log unspent data
proletesseract Jun 15, 2017
c0dcbdb
log out grouped partial data
proletesseract Jun 15, 2017
2626cf4
log out currentPending
proletesseract Jun 15, 2017
99ea723
logging out the encrypted data
proletesseract Jun 15, 2017
15e9228
log out the processed partials which arent ready
proletesseract Jun 15, 2017
ce76ee5
log out checked partials
proletesseract Jun 15, 2017
f80ca09
properly logged partial checking
proletesseract Jun 15, 2017
6d44a54
adding confirmations to stored data
proletesseract Jun 15, 2017
5d537b4
logging output
proletesseract Jun 15, 2017
6152de7
added max confs to private settings
proletesseract Jun 15, 2017
ccb3b1d
returned required params to incoming.js
proletesseract Jun 15, 2017
0ebe3ee
removed returned data, corrected condition
proletesseract Jun 15, 2017
f41fe3b
debugging condition
proletesseract Jun 15, 2017
cbf5807
added pause check in check-node api.
proletesseract Jun 15, 2017
6e8f468
added status readout and more logs
proletesseract Jun 15, 2017
9487d9c
used wrong variable
proletesseract Jun 15, 2017
a3d0263
push whole transaction into the partials list
proletesseract Jun 15, 2017
da19271
cant concatenate objecs in this verison of node.
proletesseract Jun 15, 2017
c7fc87a
tests updated.
proletesseract Jun 17, 2017
3bac819
log out decrypted for testing
proletesseract Jun 20, 2017
3351580
api logging
proletesseract Jun 20, 2017
bb15d4b
also log request
proletesseract Jun 20, 2017
0bf97cb
debug for prepare incoming
proletesseract Jun 20, 2017
1fd3406
fixed flattening logic to add the decimal
proletesseract Jun 22, 2017
be7be95
debug logging
proletesseract Jun 22, 2017
106fdb5
prep incoming debug
proletesseract Jun 22, 2017
3e97cd9
more debug
proletesseract Jun 22, 2017
eba7bee
cloned arrays to slice
proletesseract Jun 22, 2017
ccd10e8
more debug
proletesseract Jun 22, 2017
ef1fbb4
debug spend to holding runtime
proletesseract Jun 22, 2017
e9729a6
debug successful tx groups
proletesseract Jun 22, 2017
993ecfe
lodash each txgroup member
proletesseract Jun 22, 2017
599193d
updated decrypted data param
proletesseract Jun 22, 2017
5575a84
disabled mail
proletesseract Jun 22, 2017
96c5bd9
log to check blockheights
proletesseract Jun 22, 2017
3f07928
more debug in prepare outgoing
proletesseract Jun 22, 2017
04b6346
fixed decrypted value param name
proletesseract Jun 22, 2017
4e5d4b7
removed extra log
proletesseract Jun 22, 2017
f479c5d
added extra conditions to some outgoing logs
proletesseract Jun 22, 2017
6196e0f
test refill data
proletesseract Jun 22, 2017
6a2b620
removed un needed json parse.
proletesseract Jun 22, 2017
4a73799
added queue config param
proletesseract Jun 22, 2017
fa821f2
fixed tx-fee take
proletesseract Jun 22, 2017
d9ec664
check data
proletesseract Jun 22, 2017
8e48c69
case fixed to stop outgoing pausing
proletesseract Jun 22, 2017
89b35b1
fixed multiple tx peperation
proletesseract Jun 22, 2017
a942958
added safe rounding to amount additions
proletesseract Jun 23, 2017
77004ef
using the reduce function
proletesseract Jun 23, 2017
cf00227
fixed logs
proletesseract Jun 23, 2017
9464a10
correct amount being pushed
proletesseract Jun 23, 2017
33beac8
removed some of the debug
proletesseract Jun 23, 2017
c97052a
trying in satoshis
proletesseract Jun 23, 2017
1d17fb0
also track non satoshi
proletesseract Jun 23, 2017
557fa09
removed additional rounding tests
proletesseract Jun 23, 2017
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"prefer-template": 0,
"no-restricted-syntax": 0,
"strict": 0,
"no-underscore-dangle": ["error", {"allow": ["__set__"]}]
"no-underscore-dangle": ["warn", {"allow": ["__set__", "_*"]}]
}
}
22 changes: 21 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ const apiInit = () => {
}))
return
}
// Logger.writeLog('APP_TEST_001', 'success get-addresses', { data })
NavtechApi.runtime.res.send(JSON.stringify({
status: 200,
type: 'SUCCESS',
Expand Down Expand Up @@ -324,6 +325,18 @@ const apiInit = () => {
return
}

if (globalSettings.serverType === 'INCOMING' && IncomingServer.paused
|| globalSettings.serverType === 'OUTGOING' && OutgoingServer.paused) {
Logger.writeLog('APP_026A', 'this server is paused for manual recovery', { body: req.body })
NavtechApi.runtime.res.send(JSON.stringify({
status: 200,
type: 'FAIL',
code: 'APP_026A',
message: 'server is not accepting transactions',
}))
return
}

NavtechApi.runtime.numAddresses = parseInt(NavtechApi.runtime.req.body.num_addresses, 10)

if (globalSettings.serverType === 'INCOMING') {
Expand Down Expand Up @@ -452,7 +465,7 @@ const apiInit = () => {
highestConf = pending.confirmations
}
}
if (highestConf > 60) {
if (highestConf > privateSettings.maxQueue) {
Logger.writeLog('APP_030B', 'the queue is too long', { highestConf }, true)
NavtechApi.runtime.res.send(JSON.stringify({
status: 200,
Expand Down Expand Up @@ -571,13 +584,20 @@ const apiInit = () => {

returnData.nav_addresses = NavtechApi.runtime.navAddresses

// Logger.writeLog('APP_TEST_002', 'success check-node', {
// returnData,
// request: NavtechApi.runtime.req.body,
// })

NavtechApi.runtime.res.send(JSON.stringify({
status: 200,
type: 'SUCCESS',
data: returnData,
}))
}

// @TODO check if server paused before returning as valid incoming server

// -------------- CHECK IF SERVER IS PROCESSING ------------------------------------------------------------------------------------------

app.get('/api/status', (req, res) => {
Expand Down
108 changes: 90 additions & 18 deletions src/incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

const Client = require('bitcoin-core')
const config = require('config')
const lodash = require('lodash')

const EncryptionKeys = require('./lib/EncryptionKeys.js')
const Logger = require('./lib/Logger.js')
const PreFlight = require('./lib/PreFlight.js')
const RefillOutgoing = require('./lib/RefillOutgoing.js')
const SelectOutgoing = require('./lib/SelectOutgoing.js')
const ReturnAllToSenders = require('./lib/ReturnAllToSenders.js')
const PrepareIncoming = require('./lib/PrepareIncoming.js')
const RetrieveSubchainAddresses = require('./lib/RetrieveSubchainAddresses.js')
const ProcessIncoming = require('./lib/ProcessIncoming.js')
const SpendToHolding = require('./lib/SpendToHolding.js')
let EncryptionKeys = require('./lib/EncryptionKeys.js') //eslint-disable-line
let Logger = require('./lib/Logger.js') //eslint-disable-line
let PreFlight = require('./lib/PreFlight.js') //eslint-disable-line
let RefillOutgoing = require('./lib/RefillOutgoing.js') //eslint-disable-line
let SelectOutgoing = require('./lib/SelectOutgoing.js') //eslint-disable-line
let ReturnAllToSenders = require('./lib/ReturnAllToSenders.js') //eslint-disable-line
let PrepareIncoming = require('./lib/PrepareIncoming.js') //eslint-disable-line
let RetrieveSubchainAddresses = require('./lib/RetrieveSubchainAddresses.js') //eslint-disable-line
let ProcessIncoming = require('./lib/ProcessIncoming.js') //eslint-disable-line
let SpendToHolding = require('./lib/SpendToHolding.js') //eslint-disable-line

const settings = config.get('INCOMING')

Expand Down Expand Up @@ -41,10 +42,11 @@ IncomingServer.init = () => {

Logger.writeLog('INC_000', 'server starting')
EncryptionKeys.findKeysToRemove({ type: 'private' }, IncomingServer.startProcessing)
setInterval(() => {
IncomingServer.cron = setInterval(() => {
if (IncomingServer.paused === false) {
EncryptionKeys.findKeysToRemove({ type: 'private' }, IncomingServer.startProcessing)
} else {
clearInterval(IncomingServer.cron)
Logger.writeLog('INC_001', 'processing paused', { paused: IncomingServer.paused })
}
}, settings.scriptInterval)
Expand Down Expand Up @@ -80,6 +82,7 @@ IncomingServer.holdingProcessed = (success, data) => {
if (!success) {
Logger.writeLog('INC_004', 'failed to process the holding account', { success, data }, true)
IncomingServer.processing = false
IncomingServer.paused = true
return
}
SelectOutgoing.run({
Expand All @@ -96,6 +99,9 @@ IncomingServer.outgoingSelected = (success, data) => {
}

if (data.returnAllToSenders) {
if (data.pause) {
IncomingServer.paused = true
}
ReturnAllToSenders.run({
navClient: IncomingServer.navClient,
}, IncomingServer.allPendingReturned)
Expand All @@ -116,10 +122,10 @@ IncomingServer.outgoingSelected = (success, data) => {
}

IncomingServer.allPendingReturned = (success, data) => {
console.log('STATUS: IncomingServer.allPendingReturned', success, data)
if (!success) {
Logger.writeLog('INC_006', 'failed to return all pending to sender', { success, data }, true)
IncomingServer.processing = false
IncomingServer.paused = true
return
}
Logger.writeLog('INC_007', 'returned all pending to sender', { success, data }, true)
Expand All @@ -128,17 +134,55 @@ IncomingServer.allPendingReturned = (success, data) => {
}

IncomingServer.currentBatchPrepared = (success, data) => {
if (!success || !data || !data.currentBatch || !data.currentFlattened || !data.numFlattened) {
if (!success || !data || ((!data.currentBatch || !data.currentFlattened || !data.numFlattened) && !data.pendingToReturn)) {
Logger.writeLog('INC_011D', 'prepareIncoming returned bad data', { success, data })
IncomingServer.processing = false
return
}

IncomingServer.runtime.currentBatch = data.currentBatch
IncomingServer.runtime.currentFlattened = data.currentFlattened
IncomingServer.runtime.numFlattened = data.numFlattened
IncomingServer.runtime.pendingToReturn = data.pendingToReturn

if (IncomingServer.runtime.pendingToReturn && IncomingServer.runtime.pendingToReturn.length > 0) {
Logger.writeLog('INC_011', 'failed to process some transactions', { success, data }, true)
ReturnAllToSenders.fromList({
navClient: IncomingServer.navClient,
transactionsToReturn: IncomingServer.runtime.pendingToReturn,
}, IncomingServer.pendingFailedReturned)
return
}

if (!IncomingServer.runtime.currentBatch || lodash.size(IncomingServer.runtime.currentBatch) === 0) {
Logger.writeLog('INC_011B', 'no currentBatch to process', { currentBatch: IncomingServer.runtime.currentBatch })
IncomingServer.processing = false
return
}
RetrieveSubchainAddresses.run({
subClient: IncomingServer.subClient,
chosenOutgoing: IncomingServer.runtime.chosenOutgoing,
numAddresses: data.numFlattened,
numAddresses: IncomingServer.runtime.numFlattened,
}, IncomingServer.retrievedSubchainAddresses)
}

IncomingServer.pendingFailedReturned = (success, data) => {
if (!success) {
Logger.writeLog('INC_011A', 'failed to return failed pending to sender', { success, data }, true)
IncomingServer.paused = true
ReturnAllToSenders.run({
navClient: IncomingServer.navClient,
}, IncomingServer.allPendingReturned)
}
if (!IncomingServer.runtime.currentBatch || lodash.size(IncomingServer.runtime.currentBatch) === 0) {
Logger.writeLog('INC_011C', 'no currentBatch to process', { currentBatch: IncomingServer.runtime.currentBatch })
IncomingServer.processing = false
return
}
RetrieveSubchainAddresses.run({
subClient: IncomingServer.subClient,
chosenOutgoing: IncomingServer.runtime.chosenOutgoing,
numAddresses: IncomingServer.runtime.numFlattened,
}, IncomingServer.retrievedSubchainAddresses)
}

Expand All @@ -150,6 +194,7 @@ IncomingServer.retrievedSubchainAddresses = (success, data) => {
}, IncomingServer.allPendingReturned)
return
}
// @TODO compile the correct transactions to return
ProcessIncoming.run({
currentBatch: IncomingServer.runtime.currentBatch,
currentFlattened: IncomingServer.runtime.currentFlattened,
Expand All @@ -163,21 +208,38 @@ IncomingServer.retrievedSubchainAddresses = (success, data) => {

IncomingServer.transactionsProcessed = (success, data) => {
if (!success || !data) {
if (data && data.partialFailure) {
Logger.writeLog('INC_010A', 'failed part way through processing subchain transactions', { success, data }, true)
IncomingServer.paused = true
IncomingServer.processing = false
return
}
Logger.writeLog('INC_010', 'failed to process transactions', { success, data }, true)
IncomingServer.paused = true
ReturnAllToSenders.run({
navClient: IncomingServer.navClient,
}, IncomingServer.allPendingReturned)
return
}

IncomingServer.runtime.successfulSubTransactions = data.successfulSubTransactions
IncomingServer.runtime.transactionsToReturn = data.transactionsToReturn
IncomingServer.runtime.successfulTxGroups = data.successfulTxGroups
IncomingServer.runtime.txGroupsToReturn = data.txGroupsToReturn
IncomingServer.runtime.transactionsToReturn = []

if (IncomingServer.runtime.transactionsToReturn && IncomingServer.runtime.transactionsToReturn.length > 0) {
if (IncomingServer.runtime.txGroupsToReturn && IncomingServer.runtime.txGroupsToReturn.length > 0) {
Logger.writeLog('INC_011', 'failed to process some transactions', { success, data }, true)

// extract the relevant transactions to return from the txGroupsToReturn
for (let i = 0; i < IncomingServer.runtime.txGroupsToReturn.length; i++) {
const txGroup = IncomingServer.runtime.txGroupsToReturn[i]
for (let j = 0; j < txGroup.transactions.length; j++) {
IncomingServer.runtime.transactionsToReturn.push(txGroup.transactions[j])
}
}

ReturnAllToSenders.fromList({
navClient: IncomingServer.navClient,
transactionsToReturn: data.transactionsToReturn,
transactionsToReturn: IncomingServer.runtime.transactionsToReturn,
}, IncomingServer.failedTransactionsReturned)
return
}
Expand All @@ -186,8 +248,18 @@ IncomingServer.transactionsProcessed = (success, data) => {

IncomingServer.failedTransactionsReturned = (success, data) => {
if (!success) {
IncomingServer.paused = true
Logger.writeLog('INC_012', 'failed to return failed transactions to sender', { success, data }, true)
}
IncomingServer.runtime.successfulSubTransactions = []
// extract the relevant transactions to return from the txGroupsToReturn
for (let i = 0; i < IncomingServer.runtime.successfulTxGroups.length; i++) {
const txGroup = IncomingServer.runtime.successfulTxGroups[i]
lodash.forEach(txGroup.transactions, (transaction) => {
IncomingServer.runtime.successfulSubTransactions.push(transaction)
})
}

SpendToHolding.run({
successfulSubTransactions: IncomingServer.runtime.successfulSubTransactions,
holdingEncrypted: IncomingServer.runtime.holdingEncrypted,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/EncryptedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ EncryptedData.decryptData = (options, callback) => {
const key = ursa.createPrivateKey(fs.readFileSync(keyFile))
const msg = key.decrypt(options.encryptedData, 'base64', 'utf8', ursa.RSA_PKCS1_PADDING)
successfulDecryption = true
decrypted = (globalSettings.serverType === 'INCOMING') ? msg : JSON.parse(msg)
// Logger.writeLog('ECD_TEST', 'decrypted', { msg })
decrypted = JSON.parse(msg)
} catch (err2) {
// do nothing
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/FlattenTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FlattenTransactions.incoming = (options, callback) => {
callback(false, { message: 'invalid options provided to FlattenTransactions.incoming' })
return
}
const unsafeAmount = options.amountToFlatten - (options.amountToFlatten * options.anonFeePercent / 100)
const unsafeAmount = options.amountToFlatten / (1 + (options.anonFeePercent / 100))
FlattenTransactions.runtime = {
callback,
amountToFlatten: FlattenTransactions.satoshiParser(unsafeAmount),
Expand All @@ -32,7 +32,7 @@ FlattenTransactions.flattenIncoming = () => {
const factor = 1 * Math.pow(10, totalIntString.length - (i + 1))
const numFactors = parseInt(totalIntString[i], 10)
for (let j = 0; j < numFactors; j++) {
if (safeDecimal > 0 && i === totalIntString.length - 1 && j === numFactors - 1) {
if (safeDecimal > 0 && lodash.sum(flattened) === totalInt - factor) {
flattened.push(FlattenTransactions.satoshiParser(parseInt(factor, 10) + safeDecimal))
} else {
flattened.push(parseInt(factor, 10))
Expand Down
Loading