Skip to content

Commit

Permalink
Don't allow older transactions to hijack transactions.latest (#250)
Browse files Browse the repository at this point in the history
* Moving transaction and locks to top level of store

* Updating tests

* Don't allow older transactions to hijack transactions.latest
  • Loading branch information
benwerd committed Sep 19, 2018
1 parent c179410 commit 73e8aaf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('transaction reducer', () => {
latest: transaction,
}

let updatedTransaction = Object.assign({}, transaction)
const updatedTransaction = Object.assign({}, transaction)
updatedTransaction.status = 'mined'
updatedTransaction.createdAt = transaction.createdAt + 100

Expand All @@ -86,6 +86,31 @@ describe('transaction reducer', () => {

expect(transactionsResponse.latest.status).toEqual('pending')
})

it('should not update latest transaction if new transaction was created earlier', () => {
const transaction = {
status: 'pending',
confirmations: 0,
createdAt: new Date().getTime(),
}

const transactions = {
all: {},
lastUpdated: 0,
latest: transaction,
}

const updatedTransaction = Object.assign({}, transaction)
updatedTransaction.status = 'mined'
updatedTransaction.createdAt = transaction.createdAt - 100

let transactionsResponse = reducer(transactions, {
type: UPDATE_TRANSACTION,
transaction: updatedTransaction,
})

expect(transactionsResponse.latest.status).toEqual('pending')
})
})

})
8 changes: 5 additions & 3 deletions unlock-app/src/reducers/transactionReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ const transactionReducer = (transactions = initialState, action) => {

if (action.type === SET_TRANSACTION) {
if (action.transaction) {
newTransactions.latest = Object.assign({}, action.transaction)
if (!newTransactions.latest || newTransactions.latest.createdAt >= action.transaction.createdAt)
newTransactions.latest = Object.assign({}, action.transaction)
if (action.transaction.hash) newTransactions.all[action.transaction.hash] = Object.assign({}, action.transaction)
} else {
newTransactions.latest = null // Unset the latest transaction if SET_TRANSACTION was called with no transaction
}
}
if (action.transaction) {
if (action.type === UPDATE_TRANSACTION && action.transaction.createdAt === transactions.latest.createdAt) {
newTransactions.latest = Object.assign({}, action.transaction)
if (action.type === UPDATE_TRANSACTION && action.transaction.createdAt === transactions.latest.createdAt) {
if (!newTransactions.latest || newTransactions.latest.createdAt >= action.transaction.createdAt)
newTransactions.latest = Object.assign({}, action.transaction)
if (action.transaction.hash) newTransactions.all[action.transaction.hash] = Object.assign({}, action.transaction)
}
}
Expand Down

0 comments on commit 73e8aaf

Please sign in to comment.