Skip to content

Commit

Permalink
Return last error to client in all cases when digesting tx
Browse files Browse the repository at this point in the history
  • Loading branch information
EggPool committed Aug 4, 2019
1 parent 18ad7fc commit 1f7e792
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
Mempool module for Bismuth nodes
"""

import base64
import hashlib
import functools
# import base64
# import hashlib
import os
import sqlite3
import sys
import threading
import time
import functools

from Cryptodome.Hash import SHA
#from Cryptodome.PublicKey import RSA
#from Cryptodome.Signature import PKCS1_v1_5
from polysign.signerfactory import SignerFactory

import essentials
from quantizer import *
from polysign.signerfactory import SignerFactory

__version__ = "0.0.6b"
# from Cryptodome.Hash import SHA
# from Cryptodome.PublicKey import RSA
# from Cryptodome.Signature import PKCS1_v1_5

__version__ = "0.0.6c"

"""
0.0.5g - Add default param to mergedts for compatibility
Expand All @@ -28,6 +29,7 @@
quicker unfreeze
less strict freezing
0.0.6b - Raise freeze tolerance to > 15 minutes old txs.
0.0.6c - Return last exception to client in all cases
"""

MEMPOOL = None
Expand Down Expand Up @@ -429,7 +431,7 @@ def space_left_for_tx(self, transaction, mempool_size):
# Sorry, no space left for this tx type.
return False

def merge(self, data, peer_ip, c, size_bypass=False, wait=False, revert=False):
def merge(self, data: list, peer_ip: str, c, size_bypass: bool=False, wait: bool=False, revert: bool =False) -> list:
"""
Checks and merge the tx list in out mempool
:param data:
Expand All @@ -443,7 +445,7 @@ def merge(self, data, peer_ip, c, size_bypass=False, wait=False, revert=False):
global REFUSE_OLDER_THAN
# Easy cases of empty or invalid data
if not data:
return "Mempool from {} was empty".format(peer_ip)
return ["Mempool from {} was empty".format(peer_ip)]
mempool_result = []
if data == '*':
raise ValueError("Connection lost")
Expand Down Expand Up @@ -621,17 +623,18 @@ def merge(self, data, peer_ip, c, size_bypass=False, wait=False, revert=False):
for entry in essentials.execute_param_c(c,
"SELECT sum(reward) FROM transactions WHERE recipient = ?",
(mempool_address,), self.app_log):

rewards = quantize_eight(rewards) + quantize_eight(entry[0])
# error conversion from NoneType to Decimal is not supported

balance = quantize_eight(credit - debit - fees + rewards - quantize_eight(mempool_amount))
balance_pre = quantize_eight(credit - debit_ledger - fees + rewards)

fee = essentials.fee_calculate(mempool_openfield, mempool_operation, last_block)

#print("Balance", balance, fee)
# print("Balance", balance, fee)

if quantize_eight(mempool_amount) > quantize_eight(balance_pre): #mp amount is already included in "balance" var! also, that tx might already be in the mempool
if quantize_eight(mempool_amount) > quantize_eight(balance_pre):
# mp amount is already included in "balance" var! also, that tx might already be in the mempool
mempool_result.append("Mempool: Sending more than owned")
continue
if quantize_eight(balance) - quantize_eight(fee) < 0:
Expand All @@ -644,7 +647,7 @@ def merge(self, data, peer_ip, c, size_bypass=False, wait=False, revert=False):
mempool_signature_enc, mempool_public_key_b64encoded, mempool_operation,
mempool_openfield, int(time_now)))
mempool_result.append("Mempool updated with a received transaction from {}".format(peer_ip))
mempool_result.append("Success")
mempool_result.append("Success") # WARNING: Do not change string or case ever!
self.commit() # Save (commit) the changes to mempool db

mempool_size += sys.getsizeof(str(transaction)) / 1000000.0
Expand All @@ -658,5 +661,10 @@ def merge(self, data, peer_ip, c, size_bypass=False, wait=False, revert=False):
except Exception as e:
self.app_log.warning("Mempool: Error processing: {} {}".format(data, e))
if self.config.debug:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
self.app_log.warning("{} {} {}".format(exc_type, fname, exc_tb.tb_lineno))
mempool_result.append("Exception: {}".format(str(e)))
# if left there, means debug can *not* be used in production, or exception is not sent back to the client.
raise
return mempool_result

0 comments on commit 1f7e792

Please sign in to comment.