Duplicate transactions of newBlockHeaders subscription #2726
Description
Description
With the client I subscribe to the newBlockHeaders event, which works correctly, but sometimes
I get duplicate error for the transaction hash, when it tries to save to the database. Also when I check the transactions which went wrong those are contract call transactions (I don't know it's a coincidence or not), but mostly no difference between the old inserted transaction data in my db and the result of etherscan.io.
Expected behavior
Every transaction of the new blocks can be saved to the database without duplicate error.
Actual behavior
Sometimes appears hash duplication at insertion.
Steps to reproduce the behavior
this.web3.eth.subscribe("newBlockHeaders", null, async (error: Error, blockHeader: BlockHeader) => {
if(error) {
console.log("\t[ETH] Error at new block headers event", error);
} else {
const currentBlock = await this.web3.eth.getBlock(blockHeader.hash, true);
// console.log("transaction docs", currentBlock.transactions);
try {
this.storage.NormalTransaction.create(currentBlock.transactions);
} catch(e) {
console.log("the transaction is in the database already, but in the new block we've got once more", e);
}
}
});
Error Logs
{ MongoError: E11000 duplicate key error collection: ethereum.normaltransactions index: hash_1 dup key: { : "0x159938f7729d90d3488685def0bb0c43354055082bf54140085ee6b05e7981fe" }
at Function.create (.../node_modules/mongodb-core/lib/error.js:43:12)
at toError (.../node_modules/mongodb/lib/utils.js:149:22)
at coll.s.topology.insert (.../node_modules/mongodb/lib/operations/collection_ops.js:859:39)
at .../node_modules/mongodb-core/lib/connection/pool.js:532:18
at process._tickCallback (internal/process/next_tick.js:61:11)
driver: true,
name: 'MongoError',
index: 0,
code: 11000,
errmsg:
'E11000 duplicate key error collection: ethereum.normaltransactions index: hash_1 dup key: { : "0x159938f7729d90d3488685def0bb0c43354055082bf54140085ee6b05e7981fe" }',
[Symbol(mongoErrorContextSymbol)]: {} }
unhandledRejection MongoError: E11000 duplicate key error collection: ethereum.normaltransactions index: hash_1 dup key: { : "0x159938f7729d90d3488685def0bb0c43354055082bf54140085ee6b05e7981fe" }
Versions
- web3.js: 1.0.18
- nodejs: 10.11
- ethereum node: geth