Properly handles mempool checks

Transactions were included in the mempool as rawtxs but checked as txids, so effectively every time we checked if a transaction was in mempool it returned false
This commit is contained in:
Sergi Delgado Segura
2019-11-20 12:59:29 +00:00
parent 287dfeeee3
commit f91413ebd8

View File

@@ -3,6 +3,7 @@ import time
import json import json
import logging import logging
import binascii import binascii
from itertools import islice
from threading import Thread, Event from threading import Thread, Event
from flask import Flask, request, Response, abort from flask import Flask, request, Response, abort
@@ -19,7 +20,7 @@ PORT = "18443"
blockchain = [] blockchain = []
blocks = {} blocks = {}
mined_transactions = {} mined_transactions = {}
mempool = [] mempool = {}
mine_new_block = Event() mine_new_block = Event()
@@ -138,7 +139,7 @@ def process_request():
if TX.deserialize(rawtx) is not None: if TX.deserialize(rawtx) is not None:
if txid not in list(mined_transactions.keys()): if txid not in list(mined_transactions.keys()):
mempool.append(rawtx) mempool[txid] = rawtx
response["result"] = {"txid": txid} response["result"] = {"txid": txid}
else: else:
@@ -164,7 +165,7 @@ def process_request():
response["result"] = {"hex": rawtx, "confirmations": len(blockchain) - block.get("height")} response["result"] = {"hex": rawtx, "confirmations": len(blockchain) - block.get("height")}
elif txid in mempool: elif txid in mempool:
response["result"] = {"confirmations": 0} response["result"] = {"confirmations": None}
else: else:
response["error"] = { response["error"] = {
@@ -260,11 +261,9 @@ def simulate_mining(mode, time_between_blocks):
if len(mempool) != 0: if len(mempool) != 0:
# We'll mine up to 100 txs per block # We'll mine up to 100 txs per block
for rawtx in mempool[:99]: for txid, rawtx in dict(islice(mempool.items(), 99)).items():
txid = sha256d(rawtx)
txs_to_mine[txid] = rawtx txs_to_mine[txid] = rawtx
mempool.pop(txid)
mempool = mempool[99:]
# Keep track of the mined transaction (to respond to getrawtransaction) # Keep track of the mined transaction (to respond to getrawtransaction)
for txid, tx in txs_to_mine.items(): for txid, tx in txs_to_mine.items():