173 lines
4.2 KiB
Python
173 lines
4.2 KiB
Python
|
import time
|
||
|
from random import randint, random
|
||
|
|
||
|
import matplotlib.pyplot as plt
|
||
|
import numpy as np
|
||
|
|
||
|
from aucoin.blockchain import Blockchain
|
||
|
from aucoin.mempool import Mempool
|
||
|
from aucoin.miner import find_best_transactions
|
||
|
|
||
|
|
||
|
class Transaction(object):
|
||
|
|
||
|
_fee = 1
|
||
|
_hash = b""
|
||
|
prev_tx_hash = b"sdfasfasdfasdfasdf"
|
||
|
txout_index = 0
|
||
|
signature = b""
|
||
|
public_key = b""
|
||
|
_size = 12
|
||
|
|
||
|
@property
|
||
|
def hash(self):
|
||
|
return self._hash
|
||
|
|
||
|
def __bytes__(self):
|
||
|
return self.prev_tx_hash + \
|
||
|
self.txout_index.to_bytes(4, "big") + \
|
||
|
self.signature + \
|
||
|
self.public_key
|
||
|
|
||
|
@property
|
||
|
def inputs(self):
|
||
|
return [self]
|
||
|
|
||
|
@property
|
||
|
def size(self) -> int:
|
||
|
"""
|
||
|
:return: Size of transaction in bytes.
|
||
|
"""
|
||
|
return self._size
|
||
|
|
||
|
def fee(self, bc=None, mem=None, session=None) -> int:
|
||
|
return self._fee
|
||
|
|
||
|
def __str__(self, *args, **kwargs):
|
||
|
return f"Hash: {self._hash.hex()}"
|
||
|
|
||
|
|
||
|
def generate_transactions(number: int, pct=70):
|
||
|
txs = []
|
||
|
for i in range(number):
|
||
|
tx = Transaction()
|
||
|
tx._hash = i.to_bytes(12, "big")
|
||
|
tx._size = randint(30, 60)
|
||
|
tx._fee = randint(10, 30 ) + tx._size/10
|
||
|
if random() < pct/100 and len(txs) > 0:
|
||
|
tx.prev_tx_hash = txs[randint(0, len(txs)-1)].hash
|
||
|
## tx._fee += randint(0, 100)
|
||
|
pass
|
||
|
else:
|
||
|
tx.prev_tx_hash = b"sdfasfasdfasdfasdf"
|
||
|
|
||
|
|
||
|
txs.append(tx)
|
||
|
return txs
|
||
|
|
||
|
def simple_alg(txs, max_size):
|
||
|
size = 0
|
||
|
total = 0
|
||
|
best = []
|
||
|
sorted_txs = sorted(txs, key=lambda tx: tx._fee / tx.size, reverse=True)
|
||
|
for tx in sorted_txs:
|
||
|
if mempool.transaction(tx.prev_tx_hash) is None:
|
||
|
if size + tx.size < max_size:
|
||
|
size += tx.size
|
||
|
total += tx._fee
|
||
|
best.append(tx)
|
||
|
return best, total
|
||
|
|
||
|
sums_best = [[],[],[],[]]
|
||
|
sums = [[],[],[],[]]
|
||
|
|
||
|
max_size = 2048*2
|
||
|
"""
|
||
|
start = 0
|
||
|
end = 1000
|
||
|
times_best = []
|
||
|
times_simple = []
|
||
|
for j in [0, 1, 2, 3]:
|
||
|
transactions = generate_transactions(end, j*20)
|
||
|
for i in range(start, end):
|
||
|
mempool = Mempool()
|
||
|
blockchain = Blockchain()
|
||
|
|
||
|
txs = transactions[0:i]
|
||
|
for tx in txs:
|
||
|
mempool[tx.hash] = tx
|
||
|
|
||
|
# log time and run alg.
|
||
|
t = time.time()
|
||
|
best, total = find_best_transactions(blockchain, mempool, max_size, None)
|
||
|
times_best.append(time.time() - t)
|
||
|
|
||
|
# log time and run simple alg.
|
||
|
t = time.time()
|
||
|
simple_b, simple_total = simple_alg(txs, max_size)
|
||
|
times_simple.append(time.time() - t)
|
||
|
|
||
|
sums_best[j].append(total)
|
||
|
sums[j].append(simple_total)
|
||
|
|
||
|
|
||
|
print(mean(sums_best[0]), mean(sums[0]))
|
||
|
|
||
|
print(f"Alg: {sum(times_best)}")
|
||
|
print(f"Simple: {sum(times_simple)}")
|
||
|
|
||
|
x = range(start, end)
|
||
|
|
||
|
plt.figure(1)
|
||
|
plt.subplot(141)
|
||
|
|
||
|
plt.plot(x, np.array(sums_best[0]), 'g', x, sums[0], 'r--')
|
||
|
plt.xlabel('transactions in mempool')
|
||
|
plt.ylabel('Total fee')
|
||
|
plt.title('0% dependent')
|
||
|
|
||
|
plt.subplot(142)
|
||
|
plt.plot(x, np.array(sums_best[1]), 'g', x, sums[1], 'r--')
|
||
|
plt.title('20% dependent')
|
||
|
plt.xlabel('transactions in mempool')
|
||
|
|
||
|
plt.subplot(143)
|
||
|
plt.plot(x, np.array(sums_best[2]), 'g', x, sums[2], 'r--')
|
||
|
plt.title('40% dependent')
|
||
|
plt.xlabel('transactions in mempool')
|
||
|
|
||
|
plt.subplot(144)
|
||
|
plt.plot(x, np.array(sums_best[3]), 'g', x, sums[3], 'r--')
|
||
|
plt.title('60% dependent')
|
||
|
plt.xlabel('transactions in mempool')
|
||
|
|
||
|
plt.show()
|
||
|
"""
|
||
|
times = []
|
||
|
times_simple = []
|
||
|
|
||
|
for i in range(0, 100):
|
||
|
mempool = Mempool()
|
||
|
blockchain = Blockchain()
|
||
|
txs = generate_transactions(1000, i)
|
||
|
for tx in txs:
|
||
|
mempool[tx.hash] = tx
|
||
|
|
||
|
t = time.time()
|
||
|
best, total = find_best_transactions(blockchain, mempool, max_size, None)
|
||
|
times.append(time.time() - t)
|
||
|
t = time.time()
|
||
|
simple_b, simple_total = simple_alg(txs, max_size)
|
||
|
times_simple.append(time.time() - t)
|
||
|
|
||
|
print(f"Extended: {sum(times)}")
|
||
|
print(f"Simple: {sum(times_simple)}")
|
||
|
|
||
|
x = range(0, 100)
|
||
|
plt.figure(2)
|
||
|
plt.plot(x, np.array(times)*1000, 'g', x, np.array(times_simple)*1000, 'r--')
|
||
|
plt.title('Runningtime of extended (green) compared to simple (red) algorithm')
|
||
|
plt.xlabel('% of dependent transactions (of 1000 in total)')
|
||
|
plt.ylabel('milliseconds')
|
||
|
|
||
|
plt.show()
|