Tx handler gets called twice per every incoming tx
Closed this issue · 2 comments
I have some problems with the latest version of Lotion.js:
Tx handler called twice per each incoming tx.
How to reproduce:
The code for “app.js”:
let lotion = require('lotion')
let app = lotion({ devMode: true, initialState: { count: 0 }})
function txHandler(state, tx, chainInfo) {
console.log(`\ntxHandler`, new Date(), { state, chainInfo, tx })
state.count++
}
app.use(txHandler)
app.listen(2000)
Terminal 1:
$ node app.js
Terminal 2:
$ curl http://localhost:2000/txs -d '{"x": 1}'
app.js
output from terminal 1:
From the output you can see that the handler was called twice while the state remains the same.
txHandler 2018-06-24T16:35:52.671Z { state: { count: 0 },
chainInfo:
{ height: 16,
validators:
{ '01c2f9eb1d8a47a24d7edd483a728e7761ef856e964cc882eea541706540269170': 10 } },
tx: { x: 1 } }
txHandler 2018-06-24T16:35:53.320Z { state: { count: 0 },
chainInfo:
{ height: 16,
validators:
{ '01c2f9eb1d8a47a24d7edd483a728e7761ef856e964cc882eea541706540269170': 10 } },
tx: { x: 1 } }
hi Alex! this is the intended behavior for transaction handlers.
any mutations you make to the state tree will only be applied once. before putting a transaction into a block, Lotion first checks the validity of the transaction by running it through your app's middleware stack locally, then immediately reversing any state mutations.
for this reason, it's important to make sure your transaction handlers are free of side effects apart from mutating the state tree. I'll make this clearer in the docs because you're not the first person to be confused by this!
thanks Alex!
I got you. But I want to use side effects: I want transactions to update a state in MongoDB so UI can easily read data from Mongo. I want to use a state only for a small number of things like nones and coin balances. What if to add a type of tx handler like CheckTx
and DeliverTx
? Then it will be clear what is going on.