-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_LpSolve.py
66 lines (53 loc) · 2.49 KB
/
test_LpSolve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
import candidate_builder as bb
import LpSolve as lp
from collections import Counter
def checkBlockValditiy(mempool, block, MAX_BLOCK_WEIGHT):
print('checking Block Validity')
duplicate_txs = [k for k, count in Counter(block).items() if count > 1]
block_weight = 0
block_fees = 0
txs_in_block = []
if duplicate_txs:
print("Invalid block!")
count = len(duplicate_txs)
print("{} duplicate txs found: {}".format(count, duplicate_txs))
return 1
for tx in block:
if tx not in mempool.txs.keys():
print("Invalid tx {} in block!".format(tx))
sys.exit(1)
for parent in mempool.txs[tx].parents:
if parent not in txs_in_block:
print("Block contains transaction {} with unconfirmed parent {}!".format(tx, parent))
return 1
txs_in_block.append(tx)
block_weight += mempool.txs[tx].weight
block_fees += mempool.txs[tx].fee
if block_weight > MAX_BLOCK_WEIGHT:
print("Too large block! Weight: {}".format(block_weight))
return 1
print("Valid block!\nTotal fees: {}\nTotal weight: {}".format(block_fees, block_weight))
return 0
class TestLpSolver(unittest.TestCase):
def setUp(self):
self.mempool = bb.Mempool()
self.mempool.txs = {'234': bb.Transaction('234', 100, 100, ['10423']),
'10423': bb.Transaction('10423', 10, 10)
}
def test_blockBuildingIsCorrect(self):
self.fee, self.weight, self.blockTxs, self.opt = lp.LinearProgrammingSolve(self.mempool.txs, 10, 'CBC', 1000)
self.assertEqual(self.fee, 10)
def test_OrderTxsForBlock(self):
self.max_weight = 1000
self.fee, self.weight, self.blockTxs, self.opt = lp.LinearProgrammingSolve(self.mempool.txs, self.max_weight,
'SAT', 1000)
self.block = lp.create_block(self.blockTxs)
self.assertEqual(checkBlockValditiy(self.mempool, self.block, self.max_weight), 0)
def test_printToFile(self):
self.max_weight = 1000
self.fee, self.weight, self.blockTxs, self.opt = lp.LinearProgrammingSolve(self.mempool.txs, self.max_weight,
'CBC', 1000)
lp.printToFile(self.blockTxs,self.weight,self.fee, 'test.txt', 'CBC', 'opt_test', '123')
if __name__ == '__main__':
unittest.main()