taker.orderbook
taker.orderbook
Orderbook management and order selection for taker.
Implements: - Orderbook fetching from directory nodes - Order filtering by fee limits and amount ranges - Maker selection algorithms (fidelity bond weighted, random, cheapest) - Fee calculation for CoinJoin transactions
Attributes
DEFAULT_MAKER_REPEAT_PENALTY = 0.8
module-attribute
Classes
OrderbookManager
Manages orderbook state and maker selection.
Source code in taker/src/taker/orderbook.py
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 | |
Attributes
bondless_makers_allowance = bondless_makers_allowance
instance-attribute
bondless_require_zero_fee = bondless_require_zero_fee
instance-attribute
bonds: dict[str, Any] = {}
instance-attribute
equalize_cj_fees = equalize_cj_fees
instance-attribute
honest_makers: set[str] = set()
instance-attribute
ignored_makers: set[str] = set()
instance-attribute
ignored_makers_path = get_ignored_makers_path(data_dir)
instance-attribute
max_cj_fee = max_cj_fee
instance-attribute
offers: list[Offer] = []
instance-attribute
own_wallet_nicks: set[str] = own_wallet_nicks or set()
instance-attribute
require_quantized_cj_fees = require_quantized_cj_fees
instance-attribute
round_up_cj_fees = round_up_cj_fees
instance-attribute
Methods:
__init__(max_cj_fee: MaxCjFee, bondless_makers_allowance: float = 0.05, bondless_require_zero_fee: bool = True, data_dir: Any = None, own_wallet_nicks: set[str] | None = None, require_quantized_cj_fees: bool = False, round_up_cj_fees: bool = False, equalize_cj_fees: bool = False)
Source code in taker/src/taker/orderbook.py
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 | |
add_honest_maker(maker: str) -> None
Mark a maker as honest (completed a CoinJoin successfully).
Source code in taker/src/taker/orderbook.py
1143 1144 1145 1146 | |
add_ignored_maker(maker: str) -> None
Add a maker to the ignored list and persist to disk.
Source code in taker/src/taker/orderbook.py
1121 1122 1123 1124 1125 | |
clear_ignored_makers() -> None
Clear all ignored makers and delete the persistence file.
Source code in taker/src/taker/orderbook.py
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 | |
select_makers(cj_amount: int, n: int, honest_only: bool = False, min_nick_version: int | None = None, exclude_nicks: set[str] | None = None, hard_exclude_nicks: set[str] | None = None, required_features: set[str] | None = None, penalized_maker_keys: set[str] | None = None) -> tuple[dict[str, Offer], int]
Select makers for a CoinJoin.
Args:
cj_amount: Target amount
n: Number of makers
honest_only: Only select from honest makers
min_nick_version: Minimum required nick version (e.g., 6 for neutrino takers)
exclude_nicks: Caller-supplied soft exclusion nicks. If not enough eligible
makers remain after applying both hard and soft exclusions, this set is
relaxed rather than failing. ignored_makers is treated the same way.
hard_exclude_nicks: Strict exclusion nicks. Never relaxed. Use this for
makers that just rejected/failed inside the current CoinJoin
attempt (re-asking them would just fail again) and for any caller
that genuinely cannot include the nick.
required_features: Feature names that makers must support
penalized_maker_keys: Recent maker nick and bond keys to penalize with
:data:DEFAULT_MAKER_REPEAT_PENALTY while retaining full support
Returns: (selected offers dict, total fee)
Source code in taker/src/taker/orderbook.py
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 | |
select_makers_for_sweep(total_input_value: int, my_txfee: int, n: int, honest_only: bool = False, min_nick_version: int | None = None, exclude_nicks: set[str] | None = None, hard_exclude_nicks: set[str] | None = None, required_features: set[str] | None = None, penalized_maker_keys: set[str] | None = None) -> tuple[dict[str, Offer], int, int]
Select makers for a sweep CoinJoin.
Args:
total_input_value: Total input value
my_txfee: Taker's tx fee portion
n: Number of makers
honest_only: Only select from honest makers
min_nick_version: Minimum required nick version (e.g., 6 for neutrino takers)
exclude_nicks: Soft exclusion nicks (best-effort; relaxed if not enough
makers remain). See :meth:select_makers for full semantics.
hard_exclude_nicks: Strict exclusion nicks (never relaxed).
required_features: Feature names that makers must support
penalized_maker_keys: Recent maker nick and bond keys to penalize while
retaining full support
Returns: (selected offers dict, cj_amount, total fee)
Source code in taker/src/taker/orderbook.py
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 | |
update_offers(offers: list[Offer]) -> None
Update orderbook with new offers.
Source code in taker/src/taker/orderbook.py
1116 1117 1118 1119 | |
Functions:
calculate_cj_fee(offer: Offer, cj_amount: int, round_up_cj_fees: bool = False) -> int
Calculate the CoinJoin fee for a specific offer and amount.
Convenience wrapper around jmcore.models.calculate_cj_fee that accepts an Offer object directly.
Args: offer: The maker's offer cj_amount: The CoinJoin amount in satoshis
Returns: Fee in satoshis
Source code in taker/src/taker/orderbook.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
calculate_cj_fee_plan(offers: Iterable[Offer], cj_amount: int, round_up_cj_fees: bool = False, equalize_cj_fees: bool = False) -> dict[str, int]
Return the paid fee for each selected maker.
Equalization compares realized satoshi fees, which keeps absolute and relative offers comparable at the actual CoinJoin amount.
Source code in taker/src/taker/orderbook.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
cheapest_order_choose(offers: list[Offer], n: int, cj_amount: int = 0) -> list[Offer]
Choose n cheapest offers.
Args: offers: Eligible offers n: Number of offers to choose cj_amount: CoinJoin amount for fee calculation (default uses 1 BTC)
Returns: Selected offers (sorted by fee, cheapest first)
Source code in taker/src/taker/orderbook.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
choose_orders(offers: list[Offer], cj_amount: int, n: int, max_cj_fee: MaxCjFee, choose_fn: Callable[[list[Offer], int], list[Offer]] | None = None, ignored_makers: set[str] | None = None, min_nick_version: int | None = None, bondless_makers_allowance: float = 0.05, bondless_require_zero_fee: bool = True, required_features: set[str] | None = None, penalized_maker_keys: set[str] | None = None, maker_repeat_penalty: float = DEFAULT_MAKER_REPEAT_PENALTY, require_quantized_cj_fees: bool = False, round_up_cj_fees: bool = False, equalize_cj_fees: bool = False) -> tuple[dict[str, Offer], int]
Choose n orders from the orderbook for a CoinJoin.
Args: offers: All offers from orderbook cj_amount: Target CoinJoin amount n: Number of makers to select max_cj_fee: Fee limits choose_fn: Selection algorithm (default: fidelity_bond_weighted_choose) ignored_makers: Makers to exclude min_nick_version: Minimum required nick version (e.g., 6 for neutrino takers) bondless_makers_allowance: Probability of uniform zero-fee selection vs bond weighting bondless_require_zero_fee: If True, allowance spots only select zero-fee offers required_features: Feature names that makers must support (passed to filter_offers) penalized_maker_keys: Recent maker nick and bond keys to probabilistically penalize maker_repeat_penalty: Per-repeated-maker acceptance multiplier
Returns: (dict of counterparty -> offer, total_cj_fee)
Source code in taker/src/taker/orderbook.py
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 | |
choose_sweep_orders(offers: list[Offer], total_input_value: int, my_txfee: int, n: int, max_cj_fee: MaxCjFee, choose_fn: Callable[[list[Offer], int], list[Offer]] | None = None, ignored_makers: set[str] | None = None, min_nick_version: int | None = None, bondless_makers_allowance: float = 0.05, bondless_require_zero_fee: bool = True, required_features: set[str] | None = None, penalized_maker_keys: set[str] | None = None, maker_repeat_penalty: float = DEFAULT_MAKER_REPEAT_PENALTY, require_quantized_cj_fees: bool = False, round_up_cj_fees: bool = False, equalize_cj_fees: bool = False) -> tuple[dict[str, Offer], int, int]
Choose n orders for a sweep transaction (no change).
For sweeps, we need to solve for cj_amount such that: my_change = total_input - cj_amount - sum(cjfees) - my_txfee = 0
Args: offers: All offers from orderbook total_input_value: Total value of taker's inputs my_txfee: Taker's portion of transaction fee n: Number of makers to select max_cj_fee: Fee limits choose_fn: Selection algorithm ignored_makers: Makers to exclude min_nick_version: Minimum required nick version (e.g., 6 for neutrino takers) bondless_makers_allowance: Probability of uniform zero-fee selection vs bond weighting bondless_require_zero_fee: If True, allowance spots only select zero-fee offers required_features: Feature names that makers must support (passed to filter_offers) penalized_maker_keys: Recent maker nick and bond keys to probabilistically penalize maker_repeat_penalty: Per-repeated-maker acceptance multiplier
Returns: (dict of counterparty -> offer, cj_amount, total_cj_fee)
Source code in taker/src/taker/orderbook.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 | |
choose_with_repeat_penalty(offers: list[Offer], n: int, choose_fn: Callable[[list[Offer], int], list[Offer]], penalized_maker_keys: set[str] | None, repeat_penalty: float = DEFAULT_MAKER_REPEAT_PENALTY) -> list[Offer]
Tilt a baseline maker-set chooser away from recent counterparties.
A baseline set with k avoidable repeated makers is accepted with
probability repeat_penalty ** k. This preserves full support and leaves
canonical fidelity-bond values untouched. Repeats forced by a thin pool do
not count toward k because their common factor cannot change the
normalized selection distribution.
The draw bound prevents pathological custom maker counts or chooser implementations from stalling selection. On exhaustion, the final valid baseline draw is returned, so diversity policy can never fail a CoinJoin.
Source code in taker/src/taker/orderbook.py
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 | |
dedupe_offers_by_bond(offers: list[Offer], cj_amount: int, round_up_cj_fees: bool = False) -> list[Offer]
Deduplicate offers by fidelity bond UTXO, keeping only the cheapest per bond.
This is a sybil protection measure: if two different counterparties (nicks) share the same fidelity bond UTXO, we should only select one of them. Otherwise, an attacker could create multiple nicks backed by the same bond and get selected multiple times in the same CoinJoin.
Offers without a verified, positive-value fidelity bond are passed through unchanged. Unverified or expired proofs must not displace a verified offer that uses the same UTXO.
Args: offers: List of offers (possibly from different makers using same bond) cj_amount: The actual CoinJoin amount for accurate fee comparison round_up_cj_fees: Whether comparisons include taker fee rounding
Returns: List with at most one offer per bond UTXO (the cheapest), plus all unbonded offers
Source code in taker/src/taker/orderbook.py
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 | |
dedupe_offers_by_maker(offers: list[Offer], cj_amount: int = 100000000, round_up_cj_fees: bool = False) -> list[Offer]
Keep only the cheapest offer from each maker.
Args: offers: List of offers (possibly multiple per maker) cj_amount: CoinJoin amount used to compare realized fees round_up_cj_fees: Whether comparisons include taker fee rounding
Returns: List with at most one offer per maker (the cheapest)
Source code in taker/src/taker/orderbook.py
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 | |
fidelity_bond_weighted_choose(offers: list[Offer], n: int, bondless_makers_allowance: float = 0.05, bondless_require_zero_fee: bool = True, cj_amount: int = 0) -> list[Offer]
Choose n offers using per-slot probabilistic selection.
Fee policy (when bondless_require_zero_fee is True):
Bondless offers (fidelity_bond_value == 0) that charge a non-zero
advertised CoinJoin fee are removed before selection. This prevents an
attacker from flooding the orderbook with fee-charging bondless offers to
steal fees while still allowing genuine zero-fee bondless makers to
participate. Allowance slots then select only from zero-fee offers, whether
bonded or bondless.
Per-slot selection (for each of the n slots independently):
- With probability
bondless_makers_allowance: pick uniformly at random from the zero-fee offers. Bonded and bondless zero-fee makers compete equally for these slots. - Otherwise: pick from the bonded pool (
fidelity_bond_value > 0) weighted by bond value.
Fallback: if the chosen pool is empty, the other pool is tried.
This mirrors the reference JoinMarket implementation and ensures:
- High-bond makers are strongly favored (~95% of slots with the default 0.05 allowance).
- Zero-fee makers compete uniformly for the remaining allowance slots.
Args: offers: Eligible offers (already filtered and deduped). n: Number of offers to choose. bondless_makers_allowance: Per-slot probability of uniform zero-fee selection (0.0-1.0). bondless_require_zero_fee: If True, pre-filter removes bondless offers with a non-zero advertised CoinJoin fee and allowance slots only select zero-fee offers. If False, allowance slots select uniformly from all offers. cj_amount: CoinJoin amount (reserved for future fee filtering).
Returns: Selected offers.
Source code in taker/src/taker/orderbook.py
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 | |
filter_offers(offers: list[Offer], cj_amount: int, max_cj_fee: MaxCjFee, ignored_makers: set[str] | None = None, allowed_types: set[OfferType] | None = None, min_nick_version: int | None = None, required_features: set[str] | None = None, require_quantized_cj_fees: bool = False, round_up_cj_fees: bool = False) -> list[Offer]
Filter offers based on amount range, fee limits, and other criteria.
Args: offers: List of all offers cj_amount: Target CoinJoin amount max_cj_fee: Fee limits ignored_makers: Set of maker nicks to exclude allowed_types: Set of allowed offer types (default: all sw0* types) min_nick_version: Minimum nick version for reference compatibility (not used for neutrino detection - that uses handshake features instead) required_features: Feature names that makers must support. Offers from makers that are known NOT to support a required feature are filtered out. Offers with unknown feature status (empty features dict) pass through, since compatibility will be verified later during the handshake.
Returns: List of eligible offers
Source code in taker/src/taker/orderbook.py
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 | |
is_fee_within_limits(offer: Offer, cj_amount: int, max_cj_fee: MaxCjFee, round_up_cj_fees: bool = False) -> bool
Check if an offer's fee is within the configured limits.
For absolute offers: check cjfee <= abs_fee For relative offers: check cjfee <= rel_fee
It's a logical OR - an offer passes if it meets either limit for its type.
Args: offer: The maker's offer cj_amount: The CoinJoin amount (not used in the new logic) max_cj_fee: Fee limits configuration
Returns: True if fee is acceptable
Source code in taker/src/taker/orderbook.py
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 | |
is_quantized_cj_fee(offer: Offer) -> bool
Return whether an offer advertises a fee exactly on its public grid.
Source code in taker/src/taker/orderbook.py
108 109 110 111 112 | |
maker_nick_selection_key(nick: str) -> str
Return the history key used to recognize a maker nickname.
Source code in taker/src/taker/orderbook.py
34 35 36 | |
maker_selection_keys(offer: Offer) -> set[str]
Return stable selection-history keys for an offer.
A verified positive-value bond is the strongest public identity available across nickname changes. The nickname remains useful for bondless makers and for a bonded maker that rotates its advertised bond.
Source code in taker/src/taker/orderbook.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
prefer_offers_with_confirmed_features(offers: list[Offer], n: int, required_features: set[str] | None) -> list[Offer]
Prefer offers whose makers are confirmed to support the required features.
filter_offers only drops offers known to lack a required feature;
offers with unknown feature status (empty features dict, e.g. relayed
by a directory without peerlist_features) pass through so they can be
revalidated during the handshake. But when enough confirmed-compatible
offers exist to fill all n slots, selecting an unknown-status maker is
an avoidable risk: if it turns out to be incompatible the round needs a
replacement pass (or fails). Restrict the pool to confirmed offers in that
case; otherwise keep the unknown-status offers as fallback candidates.
Source code in taker/src/taker/orderbook.py
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 | |
random_order_choose(offers: list[Offer], n: int) -> list[Offer]
Choose n offers randomly.
Args: offers: Eligible offers n: Number of offers to choose
Returns: Selected offers
Source code in taker/src/taker/orderbook.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
weighted_order_choose(offers: list[Offer], n: int, cj_amount: int = 0, exponent: float = 3.0) -> list[Offer]
Choose n offers with exponential weighting by inverse fee.
Cheaper offers are more likely to be selected.
Args: offers: Eligible offers n: Number of offers to choose cj_amount: CoinJoin amount for fee calculation exponent: Higher values favor cheaper offers more strongly
Returns: Selected offers
Source code in taker/src/taker/orderbook.py
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 | |