-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_mempool.py
2866 lines (2389 loc) · 122 KB
/
test_mempool.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import dataclasses
import logging
import random
from typing import Callable, Dict, List, Optional, Tuple
import pytest
from blspy import G2Element
from clvm.casts import int_to_bytes
from clvm_tools import binutils
from chia.consensus.condition_costs import ConditionCost
from chia.consensus.cost_calculator import NPCResult
from chia.full_node.bitcoin_fee_estimator import create_bitcoin_fee_estimator
from chia.full_node.fee_estimation import EmptyMempoolInfo, MempoolInfo
from chia.full_node.full_node_api import FullNodeAPI
from chia.full_node.mempool import Mempool
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions, get_puzzle_and_solution_for_coin
from chia.full_node.mempool_manager import MEMPOOL_MIN_FEE_INCREASE
from chia.full_node.pending_tx_cache import ConflictTxCache, PendingTxCache
from chia.protocols import full_node_protocol, wallet_protocol
from chia.protocols.wallet_protocol import TransactionAck
from chia.server.outbound_message import Message
from chia.server.ws_connection import WSChiaConnection
from chia.simulator.block_tools import test_constants
from chia.simulator.simulator_protocol import FarmNewBlockProtocol
from chia.simulator.time_out_assert import time_out_assert
from chia.simulator.wallet_tools import WalletTool
from chia.types.announcement import Announcement
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.serialized_program import SerializedProgram
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.clvm_cost import CLVMCost
from chia.types.coin_spend import CoinSpend
from chia.types.condition_opcodes import ConditionOpcode
from chia.types.condition_with_args import ConditionWithArgs
from chia.types.eligible_coin_spends import run_for_cost
from chia.types.fee_rate import FeeRate
from chia.types.generator_types import BlockGenerator
from chia.types.mempool_inclusion_status import MempoolInclusionStatus
from chia.types.mempool_item import MempoolItem
from chia.types.spend_bundle import SpendBundle
from chia.types.spend_bundle_conditions import SpendBundleConditions
from chia.util.api_decorators import api_request
from chia.util.errors import Err
from chia.util.hash import std_hash
from chia.util.ints import uint32, uint64
from chia.util.recursive_replace import recursive_replace
from tests.blockchain.blockchain_test_utils import _validate_and_add_block
from tests.connection_utils import add_dummy_connection, connect_and_get_peer
from tests.core.mempool.test_mempool_manager import (
IDENTITY_PUZZLE_HASH,
TEST_COIN,
make_test_coins,
mempool_item_from_spendbundle,
mk_item,
spend_bundle_from_conditions,
)
from tests.core.node_height import node_height_at_least
from tests.util.misc import assert_runtime
BURN_PUZZLE_HASH = bytes32(b"0" * 32)
BURN_PUZZLE_HASH_2 = bytes32(b"1" * 32)
log = logging.getLogger(__name__)
def new_mi(mi: MempoolInfo, max_mempool_cost: int, min_replace_fee_per_cost: int) -> MempoolInfo:
return dataclasses.replace(
mi,
minimum_fee_per_cost_to_replace=FeeRate(uint64(min_replace_fee_per_cost)),
max_size_in_cost=CLVMCost(uint64(max_mempool_cost)),
)
@pytest.fixture(scope="module")
def wallet_a(bt):
return bt.get_pool_wallet_tool()
def generate_test_spend_bundle(
wallet_a: WalletTool,
coin: Coin,
condition_dic: Dict[ConditionOpcode, List[ConditionWithArgs]] = None,
fee: uint64 = uint64(0),
amount: uint64 = uint64(1000),
new_puzzle_hash=BURN_PUZZLE_HASH,
) -> SpendBundle:
if condition_dic is None:
condition_dic = {}
transaction = wallet_a.generate_signed_transaction(amount, new_puzzle_hash, coin, condition_dic, fee)
assert transaction is not None
return transaction
def make_item(idx: int, cost: uint64 = uint64(80), assert_height=100) -> MempoolItem:
spend_bundle_name = bytes32([idx] * 32)
return MempoolItem(
SpendBundle([], G2Element()),
uint64(0),
NPCResult(None, SpendBundleConditions([], 0, 0, 0, None, None, [], cost, 0, 0), cost),
spend_bundle_name,
uint32(0),
assert_height,
)
class TestConflictTxCache:
def test_recall(self):
c = ConflictTxCache(100)
item = make_item(1)
c.add(item)
assert c.get(item.name) == item
tx = c.drain()
assert tx == {item.spend_bundle_name: item}
def test_fifo_limit(self):
c = ConflictTxCache(200)
# each item has cost 80
items = [make_item(i) for i in range(1, 4)]
for i in items:
c.add(i)
# the max cost is 200, only two transactions will fit
# we evict items FIFO, so the to most recently added will be left
tx = c.drain()
assert tx == {items[-2].spend_bundle_name: items[-2], items[-1].spend_bundle_name: items[-1]}
def test_item_limit(self):
c = ConflictTxCache(1000000, 2)
# each item has cost 80
items = [make_item(i) for i in range(1, 4)]
for i in items:
c.add(i)
# the max size is 2, only two transactions will fit
# we evict items FIFO, so the to most recently added will be left
tx = c.drain()
assert tx == {items[-2].spend_bundle_name: items[-2], items[-1].spend_bundle_name: items[-1]}
def test_drain(self):
c = ConflictTxCache(100)
item = make_item(1)
c.add(item)
tx = c.drain()
assert tx == {item.spend_bundle_name: item}
# drain will clear the cache, so a second call will be empty
tx = c.drain()
assert tx == {}
def test_cost(self):
c = ConflictTxCache(200)
assert c.cost() == 0
item1 = make_item(1)
c.add(item1)
# each item has cost 80
assert c.cost() == 80
item2 = make_item(2)
c.add(item2)
assert c.cost() == 160
# the first item is evicted, so the cost stays the same
item3 = make_item(3)
c.add(item3)
assert c.cost() == 160
tx = c.drain()
assert tx == {item2.spend_bundle_name: item2, item3.spend_bundle_name: item3}
assert c.cost() == 0
item4 = make_item(4)
c.add(item4)
assert c.cost() == 80
tx = c.drain()
assert tx == {item4.spend_bundle_name: item4}
class TestPendingTxCache:
def test_recall(self):
c = PendingTxCache(100)
item = make_item(1)
c.add(item)
assert c.get(item.name) == item
tx = c.drain(101)
assert tx == {item.spend_bundle_name: item}
def test_fifo_limit(self):
c = PendingTxCache(200)
# each item has cost 80
items = [make_item(i) for i in range(1, 4)]
for i in items:
c.add(i)
# the max cost is 200, only two transactions will fit
# the eviction is FIFO because all items have the same assert_height
tx = c.drain(101)
assert tx == {items[-2].spend_bundle_name: items[-2], items[-1].spend_bundle_name: items[-1]}
def test_item_limit(self):
c = PendingTxCache(1000000, 2)
# each item has cost 80
items = [make_item(i) for i in range(1, 4)]
for i in items:
c.add(i)
# the max size is 2, only two transactions will fit
# the eviction is FIFO because all items have the same assert_height
tx = c.drain(101)
assert tx == {items[-2].spend_bundle_name: items[-2], items[-1].spend_bundle_name: items[-1]}
def test_drain(self):
c = PendingTxCache(100)
item = make_item(1)
c.add(item)
tx = c.drain(101)
assert tx == {item.spend_bundle_name: item}
# drain will clear the cache, so a second call will be empty
tx = c.drain(101)
assert tx == {}
def test_cost(self):
c = PendingTxCache(200)
assert c.cost() == 0
item1 = make_item(1)
c.add(item1)
# each item has cost 80
assert c.cost() == 80
item2 = make_item(2)
c.add(item2)
assert c.cost() == 160
# the first item is evicted, so the cost stays the same
item3 = make_item(3)
c.add(item3)
assert c.cost() == 160
tx = c.drain(101)
assert tx == {item2.spend_bundle_name: item2, item3.spend_bundle_name: item3}
assert c.cost() == 0
item4 = make_item(4)
c.add(item4)
assert c.cost() == 80
tx = c.drain(101)
assert tx == {item4.spend_bundle_name: item4}
def test_drain_height(self):
c = PendingTxCache(20000, 1000)
# each item has cost 80
# heights are 100-109
items = [make_item(i, 80, 100 + i) for i in range(10)]
for i in items:
c.add(i)
tx = c.drain(101)
assert tx == {items[0].spend_bundle_name: items[0]}
tx = c.drain(105)
assert tx == {
items[1].spend_bundle_name: items[1],
items[2].spend_bundle_name: items[2],
items[3].spend_bundle_name: items[3],
items[4].spend_bundle_name: items[4],
}
tx = c.drain(105)
assert tx == {}
tx = c.drain(110)
assert tx == {
items[5].spend_bundle_name: items[5],
items[6].spend_bundle_name: items[6],
items[7].spend_bundle_name: items[7],
items[8].spend_bundle_name: items[8],
items[9].spend_bundle_name: items[9],
}
class TestMempool:
@pytest.mark.asyncio
async def test_basic_mempool(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
_ = await next_block(full_node_1, wallet_a, bt)
_ = await next_block(full_node_1, wallet_a, bt)
max_block_cost_clvm = uint64(40000000)
max_mempool_cost = max_block_cost_clvm * 5
mempool_info = new_mi(EmptyMempoolInfo, max_mempool_cost, uint64(5))
fee_estimator = create_bitcoin_fee_estimator(max_block_cost_clvm)
mempool = Mempool(mempool_info, fee_estimator)
assert mempool.get_min_fee_rate(104000) == 0
with pytest.raises(ValueError):
mempool.get_min_fee_rate(max_mempool_cost + 1)
coin = await next_block(full_node_1, wallet_a, bt)
spend_bundle = generate_test_spend_bundle(wallet_a, coin)
assert spend_bundle is not None
@api_request(peer_required=True, bytes_required=True)
async def respond_transaction(
self: FullNodeAPI,
tx: full_node_protocol.RespondTransaction,
peer: WSChiaConnection,
tx_bytes: bytes = b"",
test: bool = False,
) -> Tuple[MempoolInclusionStatus, Optional[Err]]:
"""
Receives a full transaction from peer.
If tx is added to mempool, send tx_id to others. (new_transaction)
"""
assert tx_bytes != b""
spend_name = std_hash(tx_bytes)
if spend_name in self.full_node.full_node_store.pending_tx_request:
self.full_node.full_node_store.pending_tx_request.pop(spend_name)
if spend_name in self.full_node.full_node_store.peers_with_tx:
self.full_node.full_node_store.peers_with_tx.pop(spend_name)
return await self.full_node.add_transaction(tx.transaction, spend_name, peer, test)
async def next_block(full_node_1, wallet_a, bt) -> Coin:
blocks = await full_node_1.get_all_full_blocks()
# we have to farm a new block here, to ensure every test has a unique coin to test spending.
# all this could be simplified if the tests did not share a simulation
start_height = blocks[-1].height
reward_ph = wallet_a.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
1,
block_list_input=blocks,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
genesis_timestamp=10000,
time_per_block=10,
)
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, start_height + 1)
return list(blocks[-1].get_included_reward_coins())[0]
co = ConditionOpcode
mis = MempoolInclusionStatus
class TestMempoolManager:
@pytest.mark.asyncio
async def test_basic_mempool_manager(self, two_nodes_one_block, wallet_a, self_hostname):
full_node_1, full_node_2, server_1, server_2, bt = two_nodes_one_block
peer = await connect_and_get_peer(server_1, server_2, self_hostname)
_ = await next_block(full_node_1, wallet_a, bt)
coin = await next_block(full_node_1, wallet_a, bt)
spend_bundle = generate_test_spend_bundle(wallet_a, coin)
assert spend_bundle is not None
tx: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(spend_bundle)
await full_node_1.respond_transaction(tx, peer, test=True)
await time_out_assert(
10,
full_node_1.full_node.mempool_manager.get_spendbundle,
spend_bundle,
spend_bundle.name(),
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"opcode,lock_value,expected",
[
# the mempool rules don't allow relative height- or time conditions on
# ephemeral spends
(co.ASSERT_MY_BIRTH_HEIGHT, -1, mis.FAILED),
(co.ASSERT_MY_BIRTH_HEIGHT, 0x100000000, mis.FAILED),
(co.ASSERT_MY_BIRTH_HEIGHT, 5, mis.FAILED),
(co.ASSERT_MY_BIRTH_HEIGHT, 6, mis.FAILED),
(co.ASSERT_MY_BIRTH_SECONDS, -1, mis.FAILED),
(co.ASSERT_MY_BIRTH_SECONDS, 0x10000000000000000, mis.FAILED),
(co.ASSERT_MY_BIRTH_SECONDS, 10049, mis.FAILED),
(co.ASSERT_MY_BIRTH_SECONDS, 10050, mis.FAILED),
(co.ASSERT_MY_BIRTH_SECONDS, 10051, mis.FAILED),
(co.ASSERT_SECONDS_RELATIVE, -2, mis.FAILED),
(co.ASSERT_SECONDS_RELATIVE, -1, mis.FAILED),
(co.ASSERT_SECONDS_RELATIVE, 0, mis.FAILED),
(co.ASSERT_SECONDS_RELATIVE, 1, mis.FAILED),
(co.ASSERT_HEIGHT_RELATIVE, -2, mis.FAILED),
(co.ASSERT_HEIGHT_RELATIVE, -1, mis.FAILED),
(co.ASSERT_HEIGHT_RELATIVE, 0, mis.FAILED),
(co.ASSERT_HEIGHT_RELATIVE, 1, mis.FAILED),
# the absolute height and seconds tests require fresh full nodes to
# run the test on. The fixture (one_node_one_block) creates a block,
# then condition_tester2 creates another 3 blocks
(co.ASSERT_HEIGHT_ABSOLUTE, 4, mis.SUCCESS),
(co.ASSERT_HEIGHT_ABSOLUTE, 5, mis.SUCCESS),
(co.ASSERT_HEIGHT_ABSOLUTE, 6, mis.PENDING),
(co.ASSERT_HEIGHT_ABSOLUTE, 7, mis.PENDING),
# genesis timestamp is 10000 and each block is 10 seconds
(co.ASSERT_SECONDS_ABSOLUTE, 10049, mis.SUCCESS),
(co.ASSERT_SECONDS_ABSOLUTE, 10050, mis.SUCCESS),
(co.ASSERT_SECONDS_ABSOLUTE, 10051, mis.FAILED),
(co.ASSERT_SECONDS_ABSOLUTE, 10052, mis.FAILED),
],
)
async def test_ephemeral_timelock(self, one_node_one_block, wallet_a, opcode, lock_value, expected):
def test_fun(coin_1: Coin, coin_2: Coin) -> SpendBundle:
conditions = {opcode: [ConditionWithArgs(opcode, [int_to_bytes(lock_value)])]}
tx1 = wallet_a.generate_signed_transaction(uint64(1000000), wallet_a.get_new_puzzlehash(), coin_2)
ephemeral_coin: Coin = tx1.additions()[0]
tx2 = wallet_a.generate_signed_transaction(
uint64(1000000), wallet_a.get_new_puzzlehash(), ephemeral_coin, conditions.copy(), uint64(0)
)
bundle = SpendBundle.aggregate([tx1, tx2])
return bundle
full_node_1, server_1, bt = one_node_one_block
_ = await next_block(full_node_1, wallet_a, bt)
_ = await next_block(full_node_1, wallet_a, bt)
blocks, bundle, status, err = await self.condition_tester2(one_node_one_block, wallet_a, test_fun)
mempool_bundle = full_node_1.full_node.mempool_manager.get_spendbundle(bundle.name())
print(f"opcode={opcode} timelock_value={lock_value} expected={expected} status={status}")
print(f"status: {status}")
print(f"error: {err}")
assert status == expected
if expected == MempoolInclusionStatus.SUCCESS:
assert mempool_bundle == bundle
assert err is None
else:
assert mempool_bundle is None
assert err is not None
# this test makes sure that one spend successfully asserts the announce from
# another spend, even though the assert condition is duplicated 100 times
@pytest.mark.asyncio
async def test_coin_announcement_duplicate_consumed(self, one_node_one_block, wallet_a):
def test_fun(coin_1: Coin, coin_2: Coin) -> SpendBundle:
announce = Announcement(coin_2.name(), b"test")
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_COIN_ANNOUNCEMENT, [announce.name()])
dic = {cvp.opcode: [cvp] * 100}
cvp2 = ConditionWithArgs(ConditionOpcode.CREATE_COIN_ANNOUNCEMENT, [b"test"])
dic2 = {cvp.opcode: [cvp2]}
spend_bundle1 = generate_test_spend_bundle(wallet_a, coin_1, dic)
spend_bundle2 = generate_test_spend_bundle(wallet_a, coin_2, dic2)
bundle = SpendBundle.aggregate([spend_bundle1, spend_bundle2])
return bundle
full_node_1, server_1, bt = one_node_one_block
blocks, bundle, status, err = await self.condition_tester2(one_node_one_block, wallet_a, test_fun)
mempool_bundle = full_node_1.full_node.mempool_manager.get_spendbundle(bundle.name())
assert err is None
assert mempool_bundle == bundle
assert status == MempoolInclusionStatus.SUCCESS
# this test makes sure that one spend successfully asserts the announce from
# another spend, even though the create announcement is duplicated 100 times
@pytest.mark.asyncio
async def test_coin_duplicate_announcement_consumed(self, one_node_one_block, wallet_a):
def test_fun(coin_1: Coin, coin_2: Coin) -> SpendBundle:
announce = Announcement(coin_2.name(), b"test")
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_COIN_ANNOUNCEMENT, [announce.name()])
dic = {cvp.opcode: [cvp]}
cvp2 = ConditionWithArgs(ConditionOpcode.CREATE_COIN_ANNOUNCEMENT, [b"test"])
dic2 = {cvp.opcode: [cvp2] * 100}
spend_bundle1 = generate_test_spend_bundle(wallet_a, coin_1, dic)
spend_bundle2 = generate_test_spend_bundle(wallet_a, coin_2, dic2)
bundle = SpendBundle.aggregate([spend_bundle1, spend_bundle2])
return bundle
full_node_1, server_1, bt = one_node_one_block
blocks, bundle, status, err = await self.condition_tester2(one_node_one_block, wallet_a, test_fun)
mempool_bundle = full_node_1.full_node.mempool_manager.get_spendbundle(bundle.name())
assert err is None
assert mempool_bundle == bundle
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_double_spend(self, two_nodes_one_block, wallet_a, self_hostname):
reward_ph = wallet_a.get_new_puzzlehash()
full_node_1, full_node_2, server_1, server_2, bt = two_nodes_one_block
blocks = await full_node_1.get_all_full_blocks()
start_height = blocks[-1].height
blocks = bt.get_consecutive_blocks(
3,
block_list_input=blocks,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
)
peer = await connect_and_get_peer(server_1, server_2, self_hostname)
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, start_height + 3)
spend_bundle1 = generate_test_spend_bundle(wallet_a, list(blocks[-1].get_included_reward_coins())[0])
assert spend_bundle1 is not None
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(spend_bundle1)
status, err = await respond_transaction(full_node_1, tx1, peer, test=True)
assert err is None
assert status == MempoolInclusionStatus.SUCCESS
spend_bundle2 = generate_test_spend_bundle(
wallet_a,
list(blocks[-1].get_included_reward_coins())[0],
new_puzzle_hash=BURN_PUZZLE_HASH_2,
)
assert spend_bundle2 is not None
tx2: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(spend_bundle2)
status, err = await respond_transaction(full_node_1, tx2, peer, test=True)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
sb2 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle2.name())
assert err == Err.MEMPOOL_CONFLICT
assert sb1 == spend_bundle1
assert sb2 is None
assert status == MempoolInclusionStatus.PENDING
async def send_sb(self, node: FullNodeAPI, sb: SpendBundle) -> Optional[Message]:
tx = wallet_protocol.SendTransaction(sb)
return await node.send_transaction(tx, test=True)
async def gen_and_send_sb(self, node, peer, *args, **kwargs):
sb = generate_test_spend_bundle(*args, **kwargs)
assert sb is not None
await self.send_sb(node, sb)
return sb
def assert_sb_in_pool(self, node, sb):
assert sb == node.full_node.mempool_manager.get_spendbundle(sb.name())
def assert_sb_not_in_pool(self, node, sb):
assert node.full_node.mempool_manager.get_spendbundle(sb.name()) is None
@pytest.mark.asyncio
async def test_double_spend_with_higher_fee(self, two_nodes_one_block, wallet_a, self_hostname):
reward_ph = wallet_a.get_new_puzzlehash()
full_node_1, full_node_2, server_1, server_2, bt = two_nodes_one_block
blocks = await full_node_1.get_all_full_blocks()
start_height = blocks[-1].height if len(blocks) > 0 else -1
blocks = bt.get_consecutive_blocks(
3,
block_list_input=blocks,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
)
peer = await connect_and_get_peer(server_1, server_2, self_hostname)
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, start_height + 3)
coins = iter(blocks[-1].get_included_reward_coins())
coin1, coin2 = next(coins), next(coins)
coins = iter(blocks[-2].get_included_reward_coins())
coin3, coin4 = next(coins), next(coins)
sb1_1 = await self.gen_and_send_sb(full_node_1, peer, wallet_a, coin1)
sb1_2 = await self.gen_and_send_sb(full_node_1, peer, wallet_a, coin1, fee=uint64(1))
# Fee increase is insufficient, the old spendbundle must stay
self.assert_sb_in_pool(full_node_1, sb1_1)
self.assert_sb_not_in_pool(full_node_1, sb1_2)
sb1_3 = await self.gen_and_send_sb(full_node_1, peer, wallet_a, coin1, fee=MEMPOOL_MIN_FEE_INCREASE)
# Fee increase is sufficiently high, sb1_1 gets replaced with sb1_3
self.assert_sb_not_in_pool(full_node_1, sb1_1)
self.assert_sb_in_pool(full_node_1, sb1_3)
sb2 = generate_test_spend_bundle(wallet_a, coin2, fee=MEMPOOL_MIN_FEE_INCREASE)
sb12 = SpendBundle.aggregate((sb2, sb1_3))
await self.send_sb(full_node_1, sb12)
# Aggregated spendbundle sb12 replaces sb1_3 since it spends a superset
# of coins spent in sb1_3
self.assert_sb_in_pool(full_node_1, sb12)
self.assert_sb_not_in_pool(full_node_1, sb1_3)
sb3 = generate_test_spend_bundle(wallet_a, coin3, fee=uint64(MEMPOOL_MIN_FEE_INCREASE * 2))
sb23 = SpendBundle.aggregate((sb2, sb3))
await self.send_sb(full_node_1, sb23)
# sb23 must not replace existing sb12 as the former does not spend all
# coins that are spent in the latter (specifically, coin1)
self.assert_sb_in_pool(full_node_1, sb12)
self.assert_sb_not_in_pool(full_node_1, sb23)
await self.send_sb(full_node_1, sb3)
# Adding non-conflicting sb3 should succeed
self.assert_sb_in_pool(full_node_1, sb3)
sb4_1 = generate_test_spend_bundle(wallet_a, coin4, fee=MEMPOOL_MIN_FEE_INCREASE)
sb1234_1 = SpendBundle.aggregate((sb12, sb3, sb4_1))
await self.send_sb(full_node_1, sb1234_1)
# sb1234_1 should not be in pool as it decreases total fees per cost
self.assert_sb_not_in_pool(full_node_1, sb1234_1)
sb4_2 = generate_test_spend_bundle(wallet_a, coin4, fee=uint64(MEMPOOL_MIN_FEE_INCREASE * 2))
sb1234_2 = SpendBundle.aggregate((sb12, sb3, sb4_2))
await self.send_sb(full_node_1, sb1234_2)
# sb1234_2 has a higher fee per cost than its conflicts and should get
# into mempool
self.assert_sb_in_pool(full_node_1, sb1234_2)
self.assert_sb_not_in_pool(full_node_1, sb12)
self.assert_sb_not_in_pool(full_node_1, sb3)
@pytest.mark.asyncio
async def test_invalid_signature(self, one_node_one_block, wallet_a):
reward_ph = wallet_a.get_new_puzzlehash()
full_node_1, server_1, bt = one_node_one_block
blocks = await full_node_1.get_all_full_blocks()
start_height = blocks[-1].height if len(blocks) > 0 else -1
blocks = bt.get_consecutive_blocks(
3,
block_list_input=blocks,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
)
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, start_height + 3)
coins = iter(blocks[-1].get_included_reward_coins())
coin1 = next(coins)
sb: SpendBundle = generate_test_spend_bundle(wallet_a, coin1)
assert sb.aggregated_signature != G2Element.generator()
sb = dataclasses.replace(sb, aggregated_signature=G2Element.generator())
res: Optional[Message] = await self.send_sb(full_node_1, sb)
assert res is not None
ack: TransactionAck = TransactionAck.from_bytes(res.data)
assert ack.status == MempoolInclusionStatus.FAILED.value
assert ack.error == Err.BAD_AGGREGATE_SIGNATURE.name
async def condition_tester(
self,
one_node_one_block,
wallet_a,
dic: Dict[ConditionOpcode, List[ConditionWithArgs]],
fee: int = 0,
num_blocks: int = 3,
coin: Optional[Coin] = None,
):
reward_ph = wallet_a.get_new_puzzlehash()
full_node_1, server_1, bt = one_node_one_block
blocks = await full_node_1.get_all_full_blocks()
start_height = blocks[-1].height
blocks = bt.get_consecutive_blocks(
num_blocks,
block_list_input=blocks,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
)
_, dummy_node_id = await add_dummy_connection(server_1, bt.config["self_hostname"], 100)
for node_id, wsc in server_1.all_connections.items():
if node_id == dummy_node_id:
dummy_peer = wsc
break
else:
raise Exception("dummy peer not found")
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, start_height + num_blocks)
spend_bundle1 = generate_test_spend_bundle(
wallet_a, coin or list(blocks[-num_blocks + 2].get_included_reward_coins())[0], dic, uint64(fee)
)
assert spend_bundle1 is not None
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(spend_bundle1)
status, err = await respond_transaction(full_node_1, tx1, dummy_peer, test=True)
return blocks, spend_bundle1, dummy_peer, status, err
@pytest.mark.asyncio
async def condition_tester2(self, node_server_bt, wallet_a, test_fun: Callable[[Coin, Coin], SpendBundle]):
reward_ph = wallet_a.get_new_puzzlehash()
full_node_1, server_1, bt = node_server_bt
blocks = await full_node_1.get_all_full_blocks()
start_height = blocks[-1].height if len(blocks) > 0 else -1
blocks = bt.get_consecutive_blocks(
3,
block_list_input=blocks,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
time_per_block=10,
)
_, dummy_node_id = await add_dummy_connection(server_1, bt.config["self_hostname"], 100)
for node_id, wsc in server_1.all_connections.items():
if node_id == dummy_node_id:
dummy_peer = wsc
break
else:
raise Exception("dummy peer not found")
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, start_height + 3)
coin_1 = list(blocks[-2].get_included_reward_coins())[0]
coin_2 = list(blocks[-1].get_included_reward_coins())[0]
bundle = test_fun(coin_1, coin_2)
tx1: full_node_protocol.RespondTransaction = full_node_protocol.RespondTransaction(bundle)
status, err = await respond_transaction(full_node_1, tx1, dummy_peer, test=True)
return blocks, bundle, status, err
@pytest.mark.asyncio
async def test_invalid_block_index(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
blocks = await full_node_1.get_all_full_blocks()
start_height = blocks[-1].height
cvp = ConditionWithArgs(
ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE,
[int_to_bytes(start_height + 5)],
)
dic = {ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert sb1 is None
# the transaction may become valid later
assert err == Err.ASSERT_HEIGHT_ABSOLUTE_FAILED
assert status == MempoolInclusionStatus.PENDING
@pytest.mark.asyncio
async def test_block_index_missing_arg(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE, [])
dic = {ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert sb1 is None
# the transaction may become valid later
assert err == Err.INVALID_CONDITION
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_correct_block_index(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE, [int_to_bytes(1)])
dic = {ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_block_index_garbage(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
# garbage at the end of the argument list is ignored in consensus mode,
# but not in mempool-mode
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE, [int_to_bytes(1), b"garbage"])
dic = {ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is Err.INVALID_CONDITION
assert sb1 is None
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_negative_block_index(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE, [int_to_bytes(-1)])
dic = {ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_invalid_block_age(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_RELATIVE, [int_to_bytes(5)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err == Err.ASSERT_HEIGHT_RELATIVE_FAILED
assert sb1 is None
# the transaction may become valid later
assert status == MempoolInclusionStatus.PENDING
@pytest.mark.asyncio
async def test_block_age_missing_arg(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_RELATIVE, [])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err == Err.INVALID_CONDITION
assert sb1 is None
# the transaction may become valid later
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_correct_block_age(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_RELATIVE, [int_to_bytes(1)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(
one_node_one_block, wallet_a, dic, num_blocks=4
)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_block_age_garbage(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
# garbage at the end of the argument list is ignored in consensus mode,
# but not in mempool mode
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_RELATIVE, [int_to_bytes(1), b"garbage"])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(
one_node_one_block, wallet_a, dic, num_blocks=4
)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is Err.INVALID_CONDITION
assert sb1 is None
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_negative_block_age(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_RELATIVE, [int_to_bytes(-1)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(
one_node_one_block, wallet_a, dic, num_blocks=4
)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_correct_my_id(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
_ = await next_block(full_node_1, wallet_a, bt)
_ = await next_block(full_node_1, wallet_a, bt)
coin = await next_block(full_node_1, wallet_a, bt)
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_MY_COIN_ID, [coin.name()])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(
one_node_one_block, wallet_a, dic, coin=coin
)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_my_id_garbage(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
_ = await next_block(full_node_1, wallet_a, bt)
_ = await next_block(full_node_1, wallet_a, bt)
coin = await next_block(full_node_1, wallet_a, bt)
# garbage at the end of the argument list is ignored in consensus mode,
# but not in mempool mode
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_MY_COIN_ID, [coin.name(), b"garbage"])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(
one_node_one_block, wallet_a, dic, coin=coin
)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is Err.INVALID_CONDITION
assert sb1 is None
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_invalid_my_id(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
_ = await next_block(full_node_1, wallet_a, bt)
_ = await next_block(full_node_1, wallet_a, bt)
coin = await next_block(full_node_1, wallet_a, bt)
coin_2 = await next_block(full_node_1, wallet_a, bt)
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_MY_COIN_ID, [coin_2.name()])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(
one_node_one_block, wallet_a, dic, coin=coin
)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err == Err.ASSERT_MY_COIN_ID_FAILED
assert sb1 is None
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_my_id_missing_arg(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_MY_COIN_ID, [])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err == Err.INVALID_CONDITION
assert sb1 is None
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_assert_time_exceeds(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
# 5 seconds should be before the next block
time_now = full_node_1.full_node.blockchain.get_peak().timestamp + 5
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_SECONDS_ABSOLUTE, [int_to_bytes(time_now)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_assert_time_fail(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
time_now = full_node_1.full_node.blockchain.get_peak().timestamp + 1000
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_SECONDS_ABSOLUTE, [int_to_bytes(time_now)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err == Err.ASSERT_SECONDS_ABSOLUTE_FAILED
assert sb1 is None
assert status == MempoolInclusionStatus.FAILED
@pytest.mark.asyncio
async def test_assert_height_pending(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
print(full_node_1.full_node.blockchain.get_peak())
current_height = full_node_1.full_node.blockchain.get_peak().height
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_HEIGHT_ABSOLUTE, [int_to_bytes(current_height + 4)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err == Err.ASSERT_HEIGHT_ABSOLUTE_FAILED
assert sb1 is None
assert status == MempoolInclusionStatus.PENDING
@pytest.mark.asyncio
async def test_assert_time_negative(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
time_now = -1
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_SECONDS_ABSOLUTE, [int_to_bytes(time_now)])
dic = {cvp.opcode: [cvp]}
blocks, spend_bundle1, peer, status, err = await self.condition_tester(one_node_one_block, wallet_a, dic)
sb1 = full_node_1.full_node.mempool_manager.get_spendbundle(spend_bundle1.name())
assert err is None
assert sb1 == spend_bundle1
assert status == MempoolInclusionStatus.SUCCESS
@pytest.mark.asyncio
async def test_assert_time_missing_arg(self, one_node_one_block, wallet_a):
full_node_1, server_1, bt = one_node_one_block
cvp = ConditionWithArgs(ConditionOpcode.ASSERT_SECONDS_ABSOLUTE, [])
dic = {cvp.opcode: [cvp]}