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

Make sends more reliable #206

Merged
merged 5 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 46 additions & 5 deletions app/src/renderer/vuex/modules/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default ({ commit, node }) => {
sequence: 0,
key: { address: '' },
history: [],
denoms: []
denoms: [],
sendQueue: [],
sending: false
}

let mutations = {
Expand All @@ -31,6 +33,15 @@ export default ({ commit, node }) => {
},
setDenoms (state, denoms) {
state.denoms = denoms
},
queueSend (state, sendReq) {
state.sendQueue.push(sendReq)
},
shiftSendQueue (state) {
state.sendQueue = state.sendQueue.shift(1)
},
setSending (state, sending) {
state.sending = sending
}
}

Expand Down Expand Up @@ -70,9 +81,32 @@ export default ({ commit, node }) => {
if (!res) return
commit('setWalletHistory', res)
},
async walletSend ({ state, dispatch }, args) {
async walletSend ({ state, dispatch, commit }, args) {
// wait until the current send operation is done
if (state.sending) {
commit('queueSend', args)
return
}
commit('setSending', true)

let cb = args.cb
delete args.cb

// once done, do next send in queue
function done (err, res) {
if (state.sendQueue.length > 0) {
// do next send
let send = state.sendQueue[0]
commit('shiftSendQueue')
dispatch('walletSend', send)
} else {
// no sends in queue
commit('setSending', false)
}

cb(err, res)
}

try {
if (args.sequence == null) {
args.sequence = state.sequence + 1
Expand All @@ -93,11 +127,18 @@ export default ({ commit, node }) => {
password: KEY_PASSWORD,
tx
})
await node.postTx(signedTx)
let res = await node.postTx(signedTx)
// check response code
if (res.check_tx.code !== 0 || res.deliver_tx.code !== 0) {
let message = res.check_tx.log || res.deliver_tx.log
return done(new Error('Error sending tx: ' + message))
}

commit('setWalletSequence', args.sequence)
} catch (err) {
return cb(err)
return done(err)
}
if (cb) cb(null, args)
done(null, args)
dispatch('queryWalletBalances')
},
async loadDenoms () {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/specs/PageSend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const wallet = require('renderer/vuex/modules/wallet').default({
if (args.to.addr.indexOf('fail') !== -1) return Promise.reject('Failed on purpose')
return Promise.resolve(null)
},
postTx: () => Promise.resolve(null),
postTx: () => Promise.resolve({
check_tx: { code: 0 },
deliver_tx: { code: 0 }
}),
sign: () => Promise.resolve(null),
queryAccount: () => {}
}
Expand Down