mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-04-25 18:29:09 -04:00
Compare commits
544 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
81b89d63c2 | ||
![]() |
a2cc3591b1 | ||
![]() |
44fafc792e | ||
![]() |
050d4f5f48 | ||
![]() |
0fb411c8a6 | ||
![]() |
d69cf30efe | ||
![]() |
fac5df93fa | ||
![]() |
02a3711cc9 | ||
![]() |
4afd8a44e4 | ||
![]() |
8e6327a2e6 | ||
![]() |
cb42f7ae27 | ||
![]() |
d5d2663442 | ||
![]() |
7b1b5ec3a5 | ||
![]() |
de7c8aec0a | ||
![]() |
d521815e91 | ||
![]() |
ec567ccba1 | ||
![]() |
61c7afb358 | ||
![]() |
d841741d3c | ||
![]() |
f2ee1004e1 | ||
![]() |
f151653c65 | ||
![]() |
9e68fd0d9f | ||
![]() |
5d88d33243 | ||
![]() |
23980df163 | ||
![]() |
a837d1c22b | ||
![]() |
3d4bc971a2 | ||
![]() |
19710e296d | ||
![]() |
851086c5a2 | ||
![]() |
88e6112736 | ||
![]() |
a3ffd35fc0 | ||
![]() |
12c6974458 | ||
![]() |
6abd951b46 | ||
![]() |
bfb0053919 | ||
![]() |
c78ffa7351 | ||
![]() |
523adc6d26 | ||
![]() |
e01986bb50 | ||
![]() |
a979871610 | ||
![]() |
e3f31af88a | ||
![]() |
fe77b5af95 | ||
![]() |
f20d71f74b | ||
![]() |
0584256622 | ||
![]() |
dcb4d02988 | ||
![]() |
545ef8fb30 | ||
![]() |
433ed87263 | ||
![]() |
8022d8b423 | ||
![]() |
4114772bb7 | ||
![]() |
ff9f349889 | ||
![]() |
a8a99db738 | ||
![]() |
29ee6acb95 | ||
![]() |
aef729331f | ||
![]() |
2faf7561bd | ||
![]() |
7871cf256b | ||
![]() |
55bfd95694 | ||
![]() |
3cebddf593 | ||
![]() |
c3831e93cc | ||
![]() |
4eff6fe503 | ||
![]() |
018e803f88 | ||
![]() |
504d05bfad | ||
![]() |
33901a2ea9 | ||
![]() |
c8cbd27b79 | ||
![]() |
f755993fc6 | ||
![]() |
ae614b5567 | ||
![]() |
f799da5203 | ||
![]() |
952fb71a6a | ||
![]() |
180458d587 | ||
![]() |
6a76e9efbe | ||
![]() |
7634fe1702 | ||
![]() |
9aaa7d358f | ||
![]() |
204aa85e59 | ||
![]() |
7c509d3ea3 | ||
![]() |
cc854be8f4 | ||
![]() |
af6bc47ed3 | ||
![]() |
587212abc7 | ||
![]() |
b18ba95e8c | ||
![]() |
6e09c73cf3 | ||
![]() |
132cfe782c | ||
![]() |
3611a1aced | ||
![]() |
21f26a5b44 | ||
![]() |
9700d192b2 | ||
![]() |
254874276c | ||
![]() |
011aa0cb9c | ||
![]() |
8bf4ea08be | ||
![]() |
f3640aceb2 | ||
![]() |
45e14c5a1e | ||
![]() |
f29bf20e8d | ||
![]() |
b52e07ace9 | ||
![]() |
77a43ba28c | ||
![]() |
49cae19059 | ||
![]() |
7b5929deb5 | ||
![]() |
8284bea778 | ||
![]() |
0ad78e4f30 | ||
![]() |
fc6bb336c8 | ||
![]() |
33ad3c374a | ||
![]() |
2fe428779a | ||
![]() |
2eda2476eb | ||
![]() |
aaa52e9559 | ||
![]() |
ce8d3afe60 | ||
![]() |
75cfc6b0d4 | ||
![]() |
c80bdb2d8c | ||
![]() |
ef49b471d8 | ||
![]() |
cf87f19e82 | ||
![]() |
410fcf2117 | ||
![]() |
8ba8e3bd5c | ||
![]() |
eef78cddb2 | ||
![]() |
f24c11f0a3 | ||
![]() |
8e6d1e37e8 | ||
![]() |
f4630fa9d1 | ||
![]() |
cb85439905 | ||
![]() |
74af379f80 | ||
![]() |
b3813f3769 | ||
![]() |
7c9af191bc | ||
![]() |
97788e6f54 | ||
![]() |
85c4db1d75 | ||
![]() |
612784b3e0 | ||
![]() |
7c54fb2848 | ||
![]() |
f5bda640a0 | ||
![]() |
d8f84cbad9 | ||
![]() |
c430e89502 | ||
![]() |
eab4b00478 | ||
![]() |
d55c6e8c1a | ||
![]() |
60a3177710 | ||
![]() |
1f322b78c8 | ||
![]() |
12b9ceebcf | ||
![]() |
77f7222f71 | ||
![]() |
31d76cbdf4 | ||
![]() |
845b9428b7 | ||
![]() |
ee04ff8a3b | ||
![]() |
f8c3276642 | ||
![]() |
073baa9752 | ||
![]() |
f72005312c | ||
![]() |
4115a452e3 | ||
![]() |
c385059138 | ||
![]() |
c283d6a911 | ||
![]() |
dcb4edf585 | ||
![]() |
47f8a65209 | ||
![]() |
bf9abd4101 | ||
![]() |
2af22c1aba | ||
![]() |
e2198d49a0 | ||
![]() |
f0abb4396d | ||
![]() |
537672df08 | ||
![]() |
cd29870e11 | ||
![]() |
ab1001a18c | ||
![]() |
294b658f43 | ||
![]() |
2824ebc893 | ||
![]() |
07f788eb81 | ||
![]() |
ef75019ac6 | ||
![]() |
26e66ce9b9 | ||
![]() |
d7b649b7a6 | ||
![]() |
c5aa7edb6b | ||
![]() |
5c9185876d | ||
![]() |
6c1b85e408 | ||
![]() |
26109d6d8a | ||
![]() |
5e735ef4f7 | ||
![]() |
aa9b3c4687 | ||
![]() |
415323e4fc | ||
![]() |
173d077751 | ||
![]() |
23a27680a4 | ||
![]() |
4c9d1e8d8d | ||
![]() |
21fed8c291 | ||
![]() |
5390a50eda | ||
![]() |
5551f513d5 | ||
![]() |
fbf1c3dabf | ||
![]() |
23a4d624da | ||
![]() |
3fc934a189 | ||
![]() |
194fa1b1cb | ||
![]() |
58baa48eba | ||
![]() |
6f57517c05 | ||
![]() |
2068a4cf78 | ||
![]() |
dabe1abb21 | ||
![]() |
cca0d0027e | ||
![]() |
4d557d1ef9 | ||
![]() |
1d5df5498c | ||
![]() |
787edbbca2 | ||
![]() |
543f2748b5 | ||
![]() |
7da996ab7b | ||
![]() |
ad212c8663 | ||
![]() |
f4792174da | ||
![]() |
cbab8647fd | ||
![]() |
122aee022a | ||
![]() |
a3b1e772b2 | ||
![]() |
90494ba4a5 | ||
![]() |
9594b0c46b | ||
![]() |
72202b714e | ||
![]() |
55b67d31d4 | ||
![]() |
b99978879d | ||
![]() |
49a7f7eed6 | ||
![]() |
2eb9e01f97 | ||
![]() |
16d5ffc07e | ||
![]() |
9635c0b551 | ||
![]() |
1930540c1f | ||
![]() |
2932abc9ec | ||
![]() |
41687ffab9 | ||
![]() |
6c487d4a5f | ||
![]() |
21e6bbe681 | ||
![]() |
e09401b9f7 | ||
![]() |
81d1f7e1bb | ||
![]() |
66bc59892a | ||
![]() |
6399343de9 | ||
![]() |
7b9ca3b420 | ||
![]() |
268a24083f | ||
![]() |
1d4a536bb1 | ||
![]() |
52d56ae254 | ||
![]() |
2ef2863e89 | ||
![]() |
bbf8f84312 | ||
![]() |
a471a17a88 | ||
![]() |
e6d37c01a6 | ||
![]() |
796863359f | ||
![]() |
1e2dddb61b | ||
![]() |
dc8e1e53f2 | ||
![]() |
32ca0b1a4a | ||
![]() |
c433bd2389 | ||
![]() |
0ca98cd0b7 | ||
![]() |
2f28ef9401 | ||
![]() |
0c4b7d50c2 | ||
![]() |
838a7ab5fb | ||
![]() |
db78a1e99e | ||
![]() |
b443a96469 | ||
![]() |
97210739a1 | ||
![]() |
5ff46be279 | ||
![]() |
7d4a6c90f3 | ||
![]() |
2fba218429 | ||
![]() |
928fe33532 | ||
![]() |
3bb6f060a0 | ||
![]() |
8d8c938a94 | ||
![]() |
0b5a26a5e8 | ||
![]() |
0e5241787f | ||
![]() |
4b8f7987f7 | ||
![]() |
c8e5768955 | ||
![]() |
6edcc28b8d | ||
![]() |
8cd0d3cd17 | ||
![]() |
d9de1b6cf3 | ||
![]() |
0a2a32a31b | ||
![]() |
71ea249d73 | ||
![]() |
081bb965ee | ||
![]() |
f1d7859334 | ||
![]() |
6acabda6ab | ||
![]() |
38c8fd76a4 | ||
![]() |
84b5a6f7e9 | ||
![]() |
252c394ef6 | ||
![]() |
d40059192a | ||
![]() |
ed8deeff57 | ||
![]() |
98b5335231 | ||
![]() |
417bfe34a5 | ||
![]() |
7661b19300 | ||
![]() |
ca5bfed392 | ||
![]() |
58b775bdf2 | ||
![]() |
cc3abf647c | ||
![]() |
73bb1b88c9 | ||
![]() |
ad6b00beec | ||
![]() |
61a19c35a7 | ||
![]() |
026fcd13f2 | ||
![]() |
1aca4462e7 | ||
![]() |
79b4d8efbc | ||
![]() |
0beb91f537 | ||
![]() |
24466c0234 | ||
![]() |
2b850924b1 | ||
![]() |
8a88bde4f5 | ||
![]() |
1fecc687a7 | ||
![]() |
afa683b7e0 | ||
![]() |
5997453525 | ||
![]() |
e389027439 | ||
![]() |
aeefd8f271 | ||
![]() |
effeabd007 | ||
![]() |
1ff67ec943 | ||
![]() |
6ba11b1738 | ||
![]() |
18ec158d2e | ||
![]() |
b0dffc5370 | ||
![]() |
76c72cceb2 | ||
![]() |
f0f197de20 | ||
![]() |
f380d0928e | ||
![]() |
877001549a | ||
![]() |
23216dea07 | ||
![]() |
a250940ff1 | ||
![]() |
a449d9b600 | ||
![]() |
0072fbe0c6 | ||
![]() |
4767c68467 | ||
![]() |
0d63087ab9 | ||
![]() |
d230ed2705 | ||
![]() |
19705becc5 | ||
![]() |
cf0b9aa601 | ||
![]() |
a87ffaa631 | ||
![]() |
bad0117e58 | ||
![]() |
b67ee29bf7 | ||
![]() |
f76dee4c30 | ||
![]() |
af70589eba | ||
![]() |
73370ce7d9 | ||
![]() |
2c27bb8f68 | ||
![]() |
c231a34399 | ||
![]() |
4801098601 | ||
![]() |
be8b3c1dde | ||
![]() |
f575a93bbb | ||
![]() |
870911bd58 | ||
![]() |
bd07bde535 | ||
![]() |
91460a29cd | ||
![]() |
d6cac9fc1b | ||
![]() |
f337708877 | ||
![]() |
36edac8ae3 | ||
![]() |
50ff0b1e91 | ||
![]() |
9d426066a9 | ||
![]() |
a19501a002 | ||
![]() |
d8dacbdee9 | ||
![]() |
bfc1e829dc | ||
![]() |
85cee51eee | ||
![]() |
8eaf437d74 | ||
![]() |
eb15f477fa | ||
![]() |
c0d3a02beb | ||
![]() |
1901594d28 | ||
![]() |
06bbb89631 | ||
![]() |
993e68edca | ||
![]() |
5d1b9cf4c3 | ||
![]() |
d77f208aae | ||
![]() |
d059ed8644 | ||
![]() |
3b83822a71 | ||
![]() |
b11733dea9 | ||
![]() |
fea34cc35b | ||
![]() |
c69acfbeb1 | ||
![]() |
a61f7809d9 | ||
![]() |
98920d97e4 | ||
![]() |
2cde21a5a1 | ||
![]() |
bb1df6b987 | ||
![]() |
d1b10efdee | ||
![]() |
46eee5ccfc | ||
![]() |
ccff348cdd | ||
![]() |
bf262acf00 | ||
![]() |
28937b8693 | ||
![]() |
c05597970d | ||
![]() |
82eac08bfe | ||
![]() |
efc06e7401 | ||
![]() |
d15a4476f9 | ||
![]() |
6af198743a | ||
![]() |
397aa972bb | ||
![]() |
81fabdd451 | ||
![]() |
bb596fcc09 | ||
![]() |
76a59e8d85 | ||
![]() |
3037bbcbe4 | ||
![]() |
00abded1c5 | ||
![]() |
b2ca1b5f8c | ||
![]() |
9c5914ff7a | ||
![]() |
005361c833 | ||
![]() |
c3b474d7db | ||
![]() |
abc0897948 | ||
![]() |
344440bb54 | ||
![]() |
d8d1c4178e | ||
![]() |
d3797bf50a | ||
![]() |
2c11f38659 | ||
![]() |
82e22f0eea | ||
![]() |
fbe1244392 | ||
![]() |
640290023a | ||
![]() |
3d2b51998e | ||
![]() |
f12cf36364 | ||
![]() |
2bc8d0062e | ||
![]() |
1507a0787a | ||
![]() |
0c7e84449a | ||
![]() |
039192aeb6 | ||
![]() |
b1a72940df | ||
![]() |
0dcf6d887c | ||
![]() |
4cfef2f1fd | ||
![]() |
dd30566c26 | ||
![]() |
ecad2150c7 | ||
![]() |
1b0a6d22fe | ||
![]() |
ac2a13b1d0 | ||
![]() |
140dc6dc04 | ||
![]() |
4a758f7c8a | ||
![]() |
11517a64c9 | ||
![]() |
fb4468a458 | ||
![]() |
ac15963e13 | ||
![]() |
e08770f405 | ||
![]() |
9a024db955 | ||
![]() |
83080abe30 | ||
![]() |
45d6326dc1 | ||
![]() |
4fd52b74e8 | ||
![]() |
db7fe0dc99 | ||
![]() |
ab3720d51a | ||
![]() |
14d83b1792 | ||
![]() |
8ff8c95228 | ||
![]() |
779fe93279 | ||
![]() |
9d9e408ee0 | ||
![]() |
1ad6af6d5c | ||
![]() |
44875e2274 | ||
![]() |
b6453315c2 | ||
![]() |
fada511c9a | ||
![]() |
6f60117aa4 | ||
![]() |
85712aa95f | ||
![]() |
06bfada867 | ||
![]() |
b945fa3ee6 | ||
![]() |
da86cf4f58 | ||
![]() |
aedabcbcbb | ||
![]() |
d7a19f1450 | ||
![]() |
f50ee16bf1 | ||
![]() |
979e97c40a | ||
![]() |
afe0060c4a | ||
![]() |
dd9d5246c3 | ||
![]() |
e5c776e52e | ||
![]() |
47d9313e5c | ||
![]() |
57d19ad852 | ||
![]() |
f88ba46323 | ||
![]() |
e868d6b2b3 | ||
![]() |
72cab2fed1 | ||
![]() |
5381117a92 | ||
![]() |
b31779dfe1 | ||
![]() |
10ba9203fa | ||
![]() |
36c4e4fa6e | ||
![]() |
ed2d0c9ddc | ||
![]() |
d675626477 | ||
![]() |
81bb856808 | ||
![]() |
dfe02beb00 | ||
![]() |
0264c0edb1 | ||
![]() |
717b960d53 | ||
![]() |
81119af523 | ||
![]() |
5de4d1dfa7 | ||
![]() |
1c8c9034c0 | ||
![]() |
4f6f6b8054 | ||
![]() |
8387fb8c3e | ||
![]() |
a8991f571e | ||
![]() |
bd3e6136ce | ||
![]() |
48abcd5b43 | ||
![]() |
07101deab1 | ||
![]() |
9e33e8b1d1 | ||
![]() |
05b460a2c9 | ||
![]() |
8704d263c5 | ||
![]() |
844bf23ed5 | ||
![]() |
1d860b2794 | ||
![]() |
0f68415129 | ||
![]() |
3ec7f78c79 | ||
![]() |
2026bb9fdf | ||
![]() |
9279af837f | ||
![]() |
012260e361 | ||
![]() |
712327ed03 | ||
![]() |
f29ff00feb | ||
![]() |
771809a226 | ||
![]() |
7f2b265ceb | ||
![]() |
3b9fbc866d | ||
![]() |
7f2a0334d7 | ||
![]() |
0f7a415a6a | ||
![]() |
89312fe09d | ||
![]() |
bdd8231a92 | ||
![]() |
3298313d51 | ||
![]() |
3d88102cd9 | ||
![]() |
c9757caf1e | ||
![]() |
cf89da7e84 | ||
![]() |
38ba2fc387 | ||
![]() |
ec9b162585 | ||
![]() |
d68fccba56 | ||
![]() |
336fb658f3 | ||
![]() |
298d44fa09 | ||
![]() |
e6a3157913 | ||
![]() |
b813679b91 | ||
![]() |
85b5c40384 | ||
![]() |
46d93b67cb | ||
![]() |
839d74be05 | ||
![]() |
07db057b34 | ||
![]() |
ab43a51fbf | ||
![]() |
f5c2d63527 | ||
![]() |
cab4dc0a6a | ||
![]() |
9cd080f026 | ||
![]() |
df7aff938f | ||
![]() |
c2d71f1616 | ||
![]() |
c8412761e0 | ||
![]() |
55f95c970e | ||
![]() |
4d200f0c78 | ||
![]() |
e44134c96d | ||
![]() |
b4eb49e6ce | ||
![]() |
999adf8215 | ||
![]() |
8d95643fa7 | ||
![]() |
5408a02072 | ||
![]() |
dc1ad6df7d | ||
![]() |
64674f230a | ||
![]() |
87be3a121b | ||
![]() |
9ba607b98b | ||
![]() |
7c5b1b3abd | ||
![]() |
b7b54e8c5d | ||
![]() |
9e4303c196 | ||
![]() |
e21043be4e | ||
![]() |
d39c9eb738 | ||
![]() |
ab2db98c79 | ||
![]() |
6d87781a68 | ||
![]() |
1db391dc6a | ||
![]() |
b54938ff7b | ||
![]() |
9f401cd471 | ||
![]() |
f9c4a4e344 | ||
![]() |
9481f54fe0 | ||
![]() |
b015397400 | ||
![]() |
641c107012 | ||
![]() |
69751d204d | ||
![]() |
ae67e6386b | ||
![]() |
8ff683f815 | ||
![]() |
66283194e5 | ||
![]() |
8a3eed18b0 | ||
![]() |
3fb4926b6b | ||
![]() |
38e248ce3a | ||
![]() |
2877a6bc60 | ||
![]() |
7b3374f29d | ||
![]() |
a3555fe733 | ||
![]() |
0ea8ce71b6 | ||
![]() |
8b8c69a9cc | ||
![]() |
81ea313d4e | ||
![]() |
42e0cdcae9 | ||
![]() |
9c7d33acbe | ||
![]() |
c7bf84d936 | ||
![]() |
f602bec096 | ||
![]() |
b7347da27a | ||
![]() |
c8034c58a8 | ||
![]() |
141b9a99fc | ||
![]() |
8570e2b658 | ||
![]() |
46a73e2a63 | ||
![]() |
73162adcd7 | ||
![]() |
564302b184 | ||
![]() |
7ccb9d61be | ||
![]() |
3ae7e581f0 | ||
![]() |
c8802bad3d | ||
![]() |
db400ad4c4 | ||
![]() |
dbb66fe6b7 | ||
![]() |
53972cdeb3 | ||
![]() |
e1c3a5d991 | ||
![]() |
9819f74c12 | ||
![]() |
d6c1353ed5 | ||
![]() |
dcf9f24b77 | ||
![]() |
04e618feaa | ||
![]() |
eac6a6be5c | ||
![]() |
c93eaa8057 | ||
![]() |
5182b4f53a | ||
![]() |
2de492f23a | ||
![]() |
7824078975 | ||
![]() |
a3455d6032 | ||
![]() |
332fd39164 | ||
![]() |
7d6b1c2244 | ||
![]() |
3467523849 | ||
![]() |
1f793c5d98 | ||
![]() |
a96527fc2a | ||
![]() |
191247efeb | ||
![]() |
16c1daea78 | ||
![]() |
5b162368ca | ||
![]() |
2e1909685d | ||
![]() |
a34c112622 | ||
![]() |
844ebc7de3 | ||
![]() |
d664f1e71d | ||
![]() |
dd388d471a | ||
![]() |
a57c53434d | ||
![]() |
ca90c97b5d | ||
![]() |
f0e94c60cd | ||
![]() |
d0a35ab21b | ||
![]() |
5aadfbf996 | ||
![]() |
4c69013c1b | ||
![]() |
2e181dac9c | ||
![]() |
d7d87d96ac |
@ -1,2 +1,6 @@
|
|||||||
[target.armv7-unknown-linux-gnueabihf]
|
[target.armv7-unknown-linux-gnueabihf]
|
||||||
linker = "arm-linux-gnueabihf-gcc"
|
linker = "arm-linux-gnueabihf-gcc"
|
||||||
|
|
||||||
|
# windows defaults to smaller stack sizes which isn't enough
|
||||||
|
[target.'cfg(windows)']
|
||||||
|
rustflags = ["-C", "link-args=/STACK:8388608"]
|
||||||
|
42
.github/workflows/build-release-binaries.yml
vendored
42
.github/workflows/build-release-binaries.yml
vendored
@ -6,8 +6,9 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build_binaries:
|
build_binaries:
|
||||||
name: Build swap and asb binaries
|
name: Build
|
||||||
strategy:
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- bin: swap
|
- bin: swap
|
||||||
@ -20,6 +21,10 @@ jobs:
|
|||||||
archive_ext: tar
|
archive_ext: tar
|
||||||
- bin: swap
|
- bin: swap
|
||||||
target: x86_64-apple-darwin
|
target: x86_64-apple-darwin
|
||||||
|
os: macos-12
|
||||||
|
archive_ext: tar
|
||||||
|
- bin: swap
|
||||||
|
target: aarch64-apple-darwin
|
||||||
os: macos-latest
|
os: macos-latest
|
||||||
archive_ext: tar
|
archive_ext: tar
|
||||||
- bin: swap
|
- bin: swap
|
||||||
@ -36,6 +41,10 @@ jobs:
|
|||||||
archive_ext: tar
|
archive_ext: tar
|
||||||
- bin: asb
|
- bin: asb
|
||||||
target: x86_64-apple-darwin
|
target: x86_64-apple-darwin
|
||||||
|
os: macos-12
|
||||||
|
archive_ext: tar
|
||||||
|
- bin: asb
|
||||||
|
target: aarch64-apple-darwin
|
||||||
os: macos-latest
|
os: macos-latest
|
||||||
archive_ext: tar
|
archive_ext: tar
|
||||||
- bin: asb
|
- bin: asb
|
||||||
@ -45,34 +54,33 @@ jobs:
|
|||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout tagged commit
|
- name: Checkout tagged commit
|
||||||
uses: actions/checkout@v3.5.3
|
uses: actions/checkout@v4.2.1
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.event.release.target_commitish }}
|
ref: ${{ github.event.release.target_commitish }}
|
||||||
token: ${{ secrets.BOTTY_GITHUB_TOKEN }}
|
token: ${{ secrets.BOTTY_GITHUB_TOKEN }}
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@master
|
- uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: 1.63
|
toolchain: "1.74"
|
||||||
targets: armv7-unknown-linux-gnueabihf
|
|
||||||
|
- name: Cross Build ${{ matrix.target }} ${{ matrix.bin }} binary
|
||||||
|
if: matrix.target == 'armv7-unknown-linux-gnueabihf'
|
||||||
|
run: |
|
||||||
|
curl -L "https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-x86_64-unknown-linux-gnu.tar.gz" | tar xzv
|
||||||
|
sudo mv cross /usr/bin
|
||||||
|
sudo mv cross-util /usr/bin
|
||||||
|
cross build --target=${{ matrix.target }} --release --package swap --bin ${{ matrix.bin }}
|
||||||
|
|
||||||
- name: Build ${{ matrix.target }} ${{ matrix.bin }} release binary
|
- name: Build ${{ matrix.target }} ${{ matrix.bin }} release binary
|
||||||
uses: actions-rs/cargo@v1
|
if: matrix.target != 'armv7-unknown-linux-gnueabihf'
|
||||||
with:
|
run: cargo build --target=${{ matrix.target }} --release --package swap --bin ${{ matrix.bin }}
|
||||||
command: build
|
|
||||||
args: --target=${{ matrix.target }} --release --package swap --bin ${{ matrix.bin }}
|
|
||||||
use-cross: true
|
|
||||||
|
|
||||||
- name: Smoke test the binary
|
- name: Smoke test the binary
|
||||||
if: matrix.target != 'armv7-unknown-linux-gnueabihf' # armv7-unknown-linux-gnueabihf is only cross-compiled, no smoke test
|
if: matrix.target != 'armv7-unknown-linux-gnueabihf'
|
||||||
run: target/${{ matrix.target }}/release/${{ matrix.bin }} --help
|
run: target/${{ matrix.target }}/release/${{ matrix.bin }} --help
|
||||||
|
|
||||||
# Remove once python 3 is the default
|
|
||||||
- uses: actions/setup-python@v4
|
|
||||||
with:
|
|
||||||
python-version: "3.x"
|
|
||||||
|
|
||||||
- id: create-archive-name
|
- id: create-archive-name
|
||||||
shell: python # Use python to have a prettier name for the archive on Windows.
|
shell: python # Use python to have a prettier name for the archive on Windows.
|
||||||
run: |
|
run: |
|
||||||
@ -89,7 +97,7 @@ jobs:
|
|||||||
print(f'::set-output name=archive::{archive_name}')
|
print(f'::set-output name=archive::{archive_name}')
|
||||||
|
|
||||||
- name: Pack macos archive
|
- name: Pack macos archive
|
||||||
if: matrix.os == 'macos-latest'
|
if: startsWith(matrix.os, 'macos')
|
||||||
shell: bash
|
shell: bash
|
||||||
run: gtar -C ./target/${{ matrix.target }}/release --create --file=${{ steps.create-archive-name.outputs.archive }} ${{ matrix.bin }}
|
run: gtar -C ./target/${{ matrix.target }}/release --create --file=${{ steps.create-archive-name.outputs.archive }} ${{ matrix.bin }}
|
||||||
|
|
||||||
|
80
.github/workflows/ci.yml
vendored
80
.github/workflows/ci.yml
vendored
@ -4,8 +4,6 @@ on:
|
|||||||
pull_request: # Need to run on pull-requests, otherwise PRs from forks don't run
|
pull_request: # Need to run on pull-requests, otherwise PRs from forks don't run
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- "staging" # Bors uses this branch
|
|
||||||
- "trying" # Bors uses this branch
|
|
||||||
- "master" # Always build head of master for the badge in the README
|
- "master" # Always build head of master for the badge in the README
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@ -13,14 +11,14 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v3.5.3
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@master
|
- uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: 1.67
|
toolchain: "1.74"
|
||||||
components: clippy,rustfmt
|
components: clippy,rustfmt
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- name: Check formatting
|
- name: Check formatting
|
||||||
uses: dprint/check@v2.2
|
uses: dprint/check@v2.2
|
||||||
@ -37,9 +35,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v3.5.3
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- name: Build swap
|
- name: Build swap
|
||||||
run: cargo build --bin swap
|
run: cargo build --bin swap
|
||||||
@ -51,12 +49,12 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v3.5.3
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- name: Install sqlx-cli
|
- name: Install sqlx-cli
|
||||||
run: cargo install sqlx-cli
|
run: cargo install --locked --version 0.6.3 sqlx-cli
|
||||||
|
|
||||||
- name: Run sqlite_dev_setup.sh script
|
- name: Run sqlite_dev_setup.sh script
|
||||||
run: |
|
run: |
|
||||||
@ -72,19 +70,21 @@ jobs:
|
|||||||
- target: armv7-unknown-linux-gnueabihf
|
- target: armv7-unknown-linux-gnueabihf
|
||||||
os: ubuntu-latest
|
os: ubuntu-latest
|
||||||
- target: x86_64-apple-darwin
|
- target: x86_64-apple-darwin
|
||||||
|
os: macos-12
|
||||||
|
- target: aarch64-apple-darwin
|
||||||
os: macos-latest
|
os: macos-latest
|
||||||
- target: x86_64-pc-windows-msvc
|
- target: x86_64-pc-windows-msvc
|
||||||
os: windows-latest
|
os: windows-latest
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v3.5.3
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@master
|
- uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: 1.67
|
toolchain: "1.74"
|
||||||
targets: armv7-unknown-linux-gnueabihf
|
targets: armv7-unknown-linux-gnueabihf
|
||||||
|
|
||||||
- name: Build binary
|
- name: Build binary
|
||||||
@ -100,13 +100,13 @@ jobs:
|
|||||||
run: cross build -p swap --target ${{ matrix.target }}
|
run: cross build -p swap --target ${{ matrix.target }}
|
||||||
|
|
||||||
- name: Upload swap binary
|
- name: Upload swap binary
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: swap-${{ matrix.target }}
|
name: swap-${{ matrix.target }}
|
||||||
path: target/${{ matrix.target }}/debug/swap
|
path: target/${{ matrix.target }}/debug/swap
|
||||||
|
|
||||||
- name: Upload asb binary
|
- name: Upload asb binary
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: asb-${{ matrix.target }}
|
name: asb-${{ matrix.target }}
|
||||||
path: target/${{ matrix.target }}/debug/asb
|
path: target/${{ matrix.target }}/debug/asb
|
||||||
@ -117,10 +117,23 @@ jobs:
|
|||||||
os: [ubuntu-latest, macos-latest]
|
os: [ubuntu-latest, macos-latest]
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: (Free disk space on Ubuntu)
|
||||||
uses: actions/checkout@v3.5.3
|
if: matrix.os == 'ubuntu-latest'
|
||||||
|
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be
|
||||||
|
with:
|
||||||
|
# removing all of these takes ~10 mins, so just do as needed
|
||||||
|
android: true
|
||||||
|
dotnet: true
|
||||||
|
haskell: true
|
||||||
|
docker-images: false
|
||||||
|
large-packages: false
|
||||||
|
swap-storage: false
|
||||||
|
tool-cache: false
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- name: Checkout sources
|
||||||
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- name: Build tests
|
- name: Build tests
|
||||||
run: cargo build --tests --workspace --all-features
|
run: cargo build --tests --workspace --all-features
|
||||||
@ -144,20 +157,47 @@ jobs:
|
|||||||
alice_and_bob_refund_using_cancel_and_refund_command,
|
alice_and_bob_refund_using_cancel_and_refund_command,
|
||||||
alice_and_bob_refund_using_cancel_then_refund_command,
|
alice_and_bob_refund_using_cancel_then_refund_command,
|
||||||
alice_and_bob_refund_using_cancel_and_refund_command_timelock_not_expired,
|
alice_and_bob_refund_using_cancel_and_refund_command_timelock_not_expired,
|
||||||
|
alice_manually_punishes_after_bob_dead_and_bob_cancels,
|
||||||
punish,
|
punish,
|
||||||
alice_punishes_after_restart_bob_dead,
|
alice_punishes_after_restart_bob_dead,
|
||||||
alice_manually_punishes_after_bob_dead,
|
alice_manually_punishes_after_bob_dead,
|
||||||
alice_refunds_after_restart_bob_refunded,
|
alice_refunds_after_restart_bob_refunded,
|
||||||
ensure_same_swap_id,
|
ensure_same_swap_id,
|
||||||
concurrent_bobs_before_xmr_lock_proof_sent,
|
concurrent_bobs_before_xmr_lock_proof_sent,
|
||||||
|
concurrent_bobs_after_xmr_lock_proof_sent,
|
||||||
alice_manually_redeems_after_enc_sig_learned,
|
alice_manually_redeems_after_enc_sig_learned,
|
||||||
|
happy_path_bob_offline_while_alice_redeems_btc,
|
||||||
]
|
]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v3.5.3
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2.6.0
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
- name: Run test ${{ matrix.test_name }}
|
- name: Run test ${{ matrix.test_name }}
|
||||||
run: cargo test --package swap --all-features --test ${{ matrix.test_name }} -- --nocapture
|
run: cargo test --package swap --all-features --test ${{ matrix.test_name }} -- --nocapture
|
||||||
|
|
||||||
|
rpc_tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout sources
|
||||||
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
|
- name: Run RPC server tests
|
||||||
|
run: cargo test --package swap --all-features --test rpc -- --nocapture
|
||||||
|
|
||||||
|
check_stable:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout sources
|
||||||
|
uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
|
|
||||||
|
- uses: Swatinem/rust-cache@v2.7.5
|
||||||
|
|
||||||
|
- name: Run cargo check on stable rust
|
||||||
|
run: cargo check --all-targets
|
||||||
|
2
.github/workflows/create-release.yml
vendored
2
.github/workflows/create-release.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
|||||||
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release/')
|
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release/')
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3.5.3
|
- uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- name: Extract version from branch name
|
- name: Extract version from branch name
|
||||||
id: extract-version
|
id: extract-version
|
||||||
|
10
.github/workflows/draft-new-release.yml
vendored
10
.github/workflows/draft-new-release.yml
vendored
@ -12,7 +12,7 @@ jobs:
|
|||||||
name: "Draft a new release"
|
name: "Draft a new release"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3.5.3
|
- uses: actions/checkout@v4.2.1
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.BOTTY_GITHUB_TOKEN }}
|
token: ${{ secrets.BOTTY_GITHUB_TOKEN }}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ jobs:
|
|||||||
run: git checkout -b release/${{ github.event.inputs.version }}
|
run: git checkout -b release/${{ github.event.inputs.version }}
|
||||||
|
|
||||||
- name: Update changelog
|
- name: Update changelog
|
||||||
uses: thomaseizinger/keep-a-changelog-new-release@1.3.0
|
uses: thomaseizinger/keep-a-changelog-new-release@3.1.0
|
||||||
with:
|
with:
|
||||||
version: ${{ github.event.inputs.version }}
|
version: ${{ github.event.inputs.version }}
|
||||||
changelogPath: CHANGELOG.md
|
changelogPath: CHANGELOG.md
|
||||||
@ -42,8 +42,8 @@ jobs:
|
|||||||
- name: Commit changelog and manifest files
|
- name: Commit changelog and manifest files
|
||||||
id: make-commit
|
id: make-commit
|
||||||
env:
|
env:
|
||||||
DPRINT_VERSION: 0.39.1
|
DPRINT_VERSION: "0.39.1"
|
||||||
RUST_TOOLCHAIN: 1.67
|
RUST_TOOLCHAIN: "1.74"
|
||||||
run: |
|
run: |
|
||||||
rustup component add rustfmt --toolchain "$RUST_TOOLCHAIN-x86_64-unknown-linux-gnu"
|
rustup component add rustfmt --toolchain "$RUST_TOOLCHAIN-x86_64-unknown-linux-gnu"
|
||||||
curl -fsSL https://dprint.dev/install.sh | sh -s $DPRINT_VERSION
|
curl -fsSL https://dprint.dev/install.sh | sh -s $DPRINT_VERSION
|
||||||
@ -58,7 +58,7 @@ jobs:
|
|||||||
run: git push origin release/${{ github.event.inputs.version }} --force
|
run: git push origin release/${{ github.event.inputs.version }} --force
|
||||||
|
|
||||||
- name: Create pull request
|
- name: Create pull request
|
||||||
uses: thomaseizinger/create-pull-request@1.3.1
|
uses: thomaseizinger/create-pull-request@1.4.0
|
||||||
with:
|
with:
|
||||||
GITHUB_TOKEN: ${{ secrets.BOTTY_GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.BOTTY_GITHUB_TOKEN }}
|
||||||
head: release/${{ github.event.inputs.version }}
|
head: release/${{ github.event.inputs.version }}
|
||||||
|
2
.github/workflows/preview-release.yml
vendored
2
.github/workflows/preview-release.yml
vendored
@ -10,7 +10,7 @@ jobs:
|
|||||||
name: Create preview release
|
name: Create preview release
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3.5.3
|
- uses: actions/checkout@v4.2.1
|
||||||
|
|
||||||
- name: Delete 'preview' release
|
- name: Delete 'preview' release
|
||||||
uses: larryjoelane/delete-release-action@v1.0.24
|
uses: larryjoelane/delete-release-action@v1.0.24
|
||||||
|
41
CHANGELOG.md
41
CHANGELOG.md
@ -7,6 +7,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
- ASB + CLI: You can now use the `logs` command to retrieve logs stored in the past, redacting addresses and id's using `logs --redact`.
|
||||||
|
- ASB: The `--disable-timestamp` flag has been removed
|
||||||
|
|
||||||
|
## [0.13.4] - 2024-07-25
|
||||||
|
|
||||||
|
- ASB: The `history` command can now be used while the asb is running.
|
||||||
|
- ASB: Retry locking of Monero if it fails on first attempt
|
||||||
|
|
||||||
|
## [0.13.3] - 2024-07-15
|
||||||
|
|
||||||
|
- Introduced a cooperative Monero redeem feature for Bob to request from Alice if Bob is punished for not refunding in time. Alice can choose to cooperate but is not obligated to do so. This change is backwards compatible. To attempt recovery, resume a swap in the "Bitcoin punished" state. Success depends on Alice being active and still having a record of the swap. Note that Alice's cooperation is voluntary and recovery is not guaranteed
|
||||||
|
- CLI: `--change-address` can now be omitted. In that case, any change is refunded to the internal bitcoin wallet.
|
||||||
|
|
||||||
|
## [0.13.2] - 2024-07-02
|
||||||
|
|
||||||
|
- CLI: Buffer received transfer proofs for later processing if we're currently running a different swap
|
||||||
|
- CLI: We now display the reason for a failed cancel-refund operation to the user (#683)
|
||||||
|
|
||||||
|
## [0.13.1] - 2024-06-10
|
||||||
|
|
||||||
|
- Add retry logic to monero-wallet-rpc wallet refresh
|
||||||
|
|
||||||
|
## [0.13.0] - 2024-05-29
|
||||||
|
|
||||||
|
- Minimum Supported Rust Version (MSRV) bumped to 1.74
|
||||||
|
- Lowered default Bitcoin confirmation target for Bob to 1 to make sure Bitcoin transactions get confirmed in time
|
||||||
|
- Added support for starting the CLI (using the `start-daemon` subcommand) as a Daemon that accepts JSON-RPC requests
|
||||||
|
- Update monero-wallet-rpc version to v0.18.3.1
|
||||||
|
|
||||||
|
## [0.12.3] - 2023-09-20
|
||||||
|
|
||||||
|
- Swap: If no Monero daemon is manually specified, we will automatically choose one from a list of public daemons by connecting to each and checking their availability.
|
||||||
|
|
||||||
## [0.12.2] - 2023-08-08
|
## [0.12.2] - 2023-08-08
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
@ -345,7 +378,13 @@ It is possible to migrate critical data from the old db to the sqlite but there
|
|||||||
- Fixed an issue where Alice would not verify if Bob's Bitcoin lock transaction is semantically correct, i.e. pays the agreed upon amount to an output owned by both of them.
|
- Fixed an issue where Alice would not verify if Bob's Bitcoin lock transaction is semantically correct, i.e. pays the agreed upon amount to an output owned by both of them.
|
||||||
Fixing this required a **breaking change** on the network layer and hence old versions are not compatible with this version.
|
Fixing this required a **breaking change** on the network layer and hence old versions are not compatible with this version.
|
||||||
|
|
||||||
[Unreleased]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.2...HEAD
|
[unreleased]: https://github.com/comit-network/xmr-btc-swap/compare/0.13.4...HEAD
|
||||||
|
[0.13.4]: https://github.com/comit-network/xmr-btc-swap/compare/0.13.3...0.13.4
|
||||||
|
[0.13.3]: https://github.com/comit-network/xmr-btc-swap/compare/0.13.2...0.13.3
|
||||||
|
[0.13.2]: https://github.com/comit-network/xmr-btc-swap/compare/0.13.1...0.13.2
|
||||||
|
[0.13.1]: https://github.com/comit-network/xmr-btc-swap/compare/0.13.0...0.13.1
|
||||||
|
[0.13.0]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.3...0.13.0
|
||||||
|
[0.12.3]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.2...0.12.3
|
||||||
[0.12.2]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.1...0.12.2
|
[0.12.2]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.1...0.12.2
|
||||||
[0.12.1]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.0...0.12.1
|
[0.12.1]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.0...0.12.1
|
||||||
[0.12.0]: https://github.com/comit-network/xmr-btc-swap/compare/0.11.0...0.12.0
|
[0.12.0]: https://github.com/comit-network/xmr-btc-swap/compare/0.11.0...0.12.0
|
||||||
|
2074
Cargo.lock
generated
2074
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,8 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
|
resolver = "2"
|
||||||
members = [ "monero-harness", "monero-rpc", "swap", "monero-wallet" ]
|
members = [ "monero-harness", "monero-rpc", "swap", "monero-wallet" ]
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
# patch until new release https://github.com/thomaseizinger/rust-jsonrpc-client/pull/51
|
||||||
|
jsonrpc_client = { git = "https://github.com/delta1/rust-jsonrpc-client.git", rev = "3b6081697cd616c952acb9c2f02d546357d35506" }
|
||||||
monero = { git = "https://github.com/comit-network/monero-rs", rev = "818f38b" }
|
monero = { git = "https://github.com/comit-network/monero-rs", rev = "818f38b" }
|
||||||
|
22
Dockerfile
Normal file
22
Dockerfile
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# This Dockerfile builds the asb binary
|
||||||
|
|
||||||
|
FROM rust:1.79-slim AS builder
|
||||||
|
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
RUN apt-get update
|
||||||
|
RUN apt-get install -y git clang cmake libsnappy-dev
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
WORKDIR /build/swap
|
||||||
|
|
||||||
|
RUN cargo build --release --bin=asb
|
||||||
|
|
||||||
|
FROM debian:bookworm-slim
|
||||||
|
|
||||||
|
WORKDIR /data
|
||||||
|
|
||||||
|
COPY --from=builder /build/target/release/asb /bin/asb
|
||||||
|
|
||||||
|
ENTRYPOINT ["asb"]
|
@ -9,6 +9,10 @@ Currently, swaps are only offered in one direction with the `swap` CLI on the bu
|
|||||||
We are working on implementing a protocol where XMR moves first, but are currently blocked by advances on Monero itself.
|
We are working on implementing a protocol where XMR moves first, but are currently blocked by advances on Monero itself.
|
||||||
You can read [this blogpost](https://comit.network/blog/2021/07/02/transaction-presigning) for more information.
|
You can read [this blogpost](https://comit.network/blog/2021/07/02/transaction-presigning) for more information.
|
||||||
|
|
||||||
|
## Maintenance
|
||||||
|
|
||||||
|
**This repository is unmaintained**. The original developers (@comit-network) have moved on to other projects. Community volunteers are continuing development at [UnstoppableSwap/core](https://github.com/UnstoppableSwap/core), which includes a graphical user interface. Please note that the fork has introduced network-level breaking changes, making it incompatible with peers running this repository - you will not be able to initiate swaps with them.
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
1. Download the [latest `swap` binary release](https://github.com/comit-network/xmr-btc-swap/releases/latest) for your operating system.
|
1. Download the [latest `swap` binary release](https://github.com/comit-network/xmr-btc-swap/releases/latest) for your operating system.
|
||||||
@ -50,7 +54,7 @@ Please have a look at the [contribution guidelines](./CONTRIBUTING.md).
|
|||||||
## Rust Version Support
|
## Rust Version Support
|
||||||
|
|
||||||
Please note that only the latest stable Rust toolchain is supported.
|
Please note that only the latest stable Rust toolchain is supported.
|
||||||
All stable toolchains since 1.67 _should_ work.
|
All stable toolchains since 1.74 _should_ work.
|
||||||
|
|
||||||
## Contact
|
## Contact
|
||||||
|
|
||||||
|
25
bors.toml
25
bors.toml
@ -1,25 +0,0 @@
|
|||||||
status = [
|
|
||||||
"static_analysis",
|
|
||||||
"bdk_test",
|
|
||||||
"sqlx_test",
|
|
||||||
"build (x86_64-unknown-linux-gnu, ubuntu-latest)",
|
|
||||||
"build (armv7-unknown-linux-gnueabihf, ubuntu-latest)",
|
|
||||||
"build (x86_64-apple-darwin, macos-latest)",
|
|
||||||
"build (x86_64-pc-windows-msvc, windows-latest)",
|
|
||||||
"test (ubuntu-latest)",
|
|
||||||
"test (macos-latest)",
|
|
||||||
"docker_tests (happy_path)",
|
|
||||||
"docker_tests (happy_path_restart_bob_after_xmr_locked)",
|
|
||||||
"docker_tests (happy_path_restart_alice_after_xmr_locked)",
|
|
||||||
"docker_tests (happy_path_restart_bob_before_xmr_locked)",
|
|
||||||
"docker_tests (alice_and_bob_refund_using_cancel_and_refund_command)",
|
|
||||||
"docker_tests (alice_and_bob_refund_using_cancel_then_refund_command)",
|
|
||||||
"docker_tests (alice_and_bob_refund_using_cancel_and_refund_command_timelock_not_expired)",
|
|
||||||
"docker_tests (punish)",
|
|
||||||
"docker_tests (alice_punishes_after_restart_bob_dead)",
|
|
||||||
"docker_tests (alice_manually_punishes_after_bob_dead)",
|
|
||||||
"docker_tests (alice_refunds_after_restart_bob_refunded)",
|
|
||||||
"docker_tests (ensure_same_swap_id)",
|
|
||||||
"docker_tests (concurrent_bobs_before_xmr_lock_proof_sent)",
|
|
||||||
"docker_tests (alice_manually_redeems_after_enc_sig_learned)"
|
|
||||||
]
|
|
@ -110,7 +110,7 @@ The minimum and maximum amount as well as a spread, that is added on top of the
|
|||||||
In order to be able to trade, the ASB must define a price to be able to agree on the amounts to be swapped with a CLI.
|
In order to be able to trade, the ASB must define a price to be able to agree on the amounts to be swapped with a CLI.
|
||||||
The `XMR<>BTC` price is currently determined by the price from the central exchange Kraken.
|
The `XMR<>BTC` price is currently determined by the price from the central exchange Kraken.
|
||||||
Upon startup the ASB connects to the Kraken price websocket and listens on the stream for price updates.
|
Upon startup the ASB connects to the Kraken price websocket and listens on the stream for price updates.
|
||||||
You can plug in a different price ticker websocket using the the `price_ticker_ws_url` configuration option.
|
You can plug in a different price ticker websocket using the `price_ticker_ws_url` configuration option.
|
||||||
You will have to make sure that the format returned is the same as the format used by Kraken.
|
You will have to make sure that the format returned is the same as the format used by Kraken.
|
||||||
|
|
||||||
Currently, we use a spot-price model, i.e. the ASB dictates the price to the CLI.
|
Currently, we use a spot-price model, i.e. the ASB dictates the price to the CLI.
|
||||||
@ -164,3 +164,38 @@ May 01 01:32:05.018 INFO Tor found. Setting up hidden service.
|
|||||||
May 01 01:32:07.475 INFO /onion3/z4findrdwtfbpoq64ayjtmxvr52vvxnsynerlenlfkmm52dqxsl4deyd:9939
|
May 01 01:32:07.475 INFO /onion3/z4findrdwtfbpoq64ayjtmxvr52vvxnsynerlenlfkmm52dqxsl4deyd:9939
|
||||||
May 01 01:32:07.476 INFO /onion3/z4findrdwtfbpoq64ayjtmxvr52vvxnsynerlenlfkmm52dqxsl4deyd:9940
|
May 01 01:32:07.476 INFO /onion3/z4findrdwtfbpoq64ayjtmxvr52vvxnsynerlenlfkmm52dqxsl4deyd:9940
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Exporting the Bitcoin wallet descriptor
|
||||||
|
|
||||||
|
First use `swap` or `asb` with the `export-bitcoin-wallet` subcommand.
|
||||||
|
|
||||||
|
Output example:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"descriptor":"wpkh(tprv8Zgredacted.../84'/1'/0'/0/*)","blockheight":2415616,"label":"asb-testnet"}
|
||||||
|
```
|
||||||
|
|
||||||
|
The wallet can theoretically be directly imported into
|
||||||
|
[bdk-cli](https://bitcoindevkit.org/bdk-cli/installation/) but it is easier to
|
||||||
|
use Sparrow Wallet.
|
||||||
|
|
||||||
|
Sparrow wallet import works as follows:
|
||||||
|
|
||||||
|
- File -> New wallet -> Give it a name
|
||||||
|
- Select "New or Imported Software Wallet"
|
||||||
|
- Click "Enter Private Key" for "Master Private Key (BIP32)"
|
||||||
|
- Enter the `xprv...` or `tprv...` part of the descriptor (example above is `tprv8Zgredacted...`:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
- Click "Import"
|
||||||
|
- Leave the derivation path as `m/84'/0'/0'` and click "Import Keystore" button
|
||||||
|
- Click "Apply" and then supply password
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
- Click Transactions tab
|
||||||
|
- ???
|
||||||
|
- Profit!
|
||||||
|
|
||||||
|

|
||||||
|
BIN
docs/asb/enter-master-private-key.png
Normal file
BIN
docs/asb/enter-master-private-key.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
BIN
docs/asb/import-keystore.png
Normal file
BIN
docs/asb/import-keystore.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 182 KiB |
BIN
docs/asb/transactions-tab.png
Normal file
BIN
docs/asb/transactions-tab.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 109 KiB |
@ -75,7 +75,7 @@ OPTIONS:
|
|||||||
|
|
||||||
This command has three core options:
|
This command has three core options:
|
||||||
|
|
||||||
- `--change-address`: A Bitcoin address you control. Will be used for refunds of any kind.
|
- `--change-address`: A Bitcoin address you control. Will be used for refunds of any kind. You can also omit this flag which will refund any change to the internal wallet.
|
||||||
- `--receive-address`: A Monero address you control. This is where you will receive the Monero after the swap.
|
- `--receive-address`: A Monero address you control. This is where you will receive the Monero after the swap.
|
||||||
- `--seller`: The multiaddress of the seller you want to swap with.
|
- `--seller`: The multiaddress of the seller you want to swap with.
|
||||||
|
|
||||||
|
@ -10,8 +10,7 @@ anyhow = "1"
|
|||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
monero-rpc = { path = "../monero-rpc" }
|
monero-rpc = { path = "../monero-rpc" }
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
spectral = "0.6"
|
testcontainers = "0.15"
|
||||||
testcontainers = "0.12"
|
|
||||||
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "macros" ] }
|
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "macros" ] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "tracing-log" ] }
|
tracing-subscriber = { version = "0.3", default-features = false, features = [ "fmt", "ansi", "env-filter", "tracing-log" ] }
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
use std::collections::HashMap;
|
use testcontainers::{core::WaitFor, Image, ImageArgs};
|
||||||
use testcontainers::core::{Container, Docker, WaitForMessage};
|
|
||||||
use testcontainers::Image;
|
|
||||||
|
|
||||||
pub const MONEROD_DAEMON_CONTAINER_NAME: &str = "monerod";
|
pub const MONEROD_DAEMON_CONTAINER_NAME: &str = "monerod";
|
||||||
pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
|
pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
|
||||||
@ -13,43 +11,22 @@ pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
|
|||||||
/// this doesn't matter.
|
/// this doesn't matter.
|
||||||
pub const RPC_PORT: u16 = 18081;
|
pub const RPC_PORT: u16 = 18081;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
pub struct Monerod {
|
pub struct Monerod;
|
||||||
args: MonerodArgs,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Image for Monerod {
|
impl Image for Monerod {
|
||||||
type Args = MonerodArgs;
|
type Args = MonerodArgs;
|
||||||
type EnvVars = HashMap<String, String>;
|
|
||||||
type Volumes = HashMap<String, String>;
|
|
||||||
type EntryPoint = str;
|
|
||||||
|
|
||||||
fn descriptor(&self) -> String {
|
fn name(&self) -> String {
|
||||||
"rinocommunity/monero:v0.18.1.2".to_owned()
|
"rinocommunity/monero".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
|
fn tag(&self) -> String {
|
||||||
container
|
"v0.18.1.2".into()
|
||||||
.logs()
|
|
||||||
.stdout
|
|
||||||
.wait_for_message("RPC server started ok")
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args(&self) -> <Self as Image>::Args {
|
fn ready_conditions(&self) -> Vec<WaitFor> {
|
||||||
self.args.clone()
|
vec![WaitFor::message_on_stdout("RPC server started ok")]
|
||||||
}
|
|
||||||
|
|
||||||
fn volumes(&self) -> Self::Volumes {
|
|
||||||
HashMap::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn env_vars(&self) -> Self::EnvVars {
|
|
||||||
HashMap::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_args(self, args: <Self as Image>::Args) -> Self {
|
|
||||||
Self { args }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entrypoint(&self) -> Option<String> {
|
fn entrypoint(&self) -> Option<String> {
|
||||||
@ -58,43 +35,22 @@ impl Image for Monerod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct MoneroWalletRpc {
|
pub struct MoneroWalletRpc;
|
||||||
args: MoneroWalletRpcArgs,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Image for MoneroWalletRpc {
|
impl Image for MoneroWalletRpc {
|
||||||
type Args = MoneroWalletRpcArgs;
|
type Args = MoneroWalletRpcArgs;
|
||||||
type EnvVars = HashMap<String, String>;
|
|
||||||
type Volumes = HashMap<String, String>;
|
|
||||||
type EntryPoint = str;
|
|
||||||
|
|
||||||
fn descriptor(&self) -> String {
|
fn name(&self) -> String {
|
||||||
"rinocommunity/monero:v0.18.1.2".to_owned()
|
"rinocommunity/monero".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
|
fn tag(&self) -> String {
|
||||||
container
|
"v0.18.1.2".into()
|
||||||
.logs()
|
|
||||||
.stdout
|
|
||||||
.wait_for_message("Run server thread name: RPC")
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args(&self) -> <Self as Image>::Args {
|
fn ready_conditions(&self) -> Vec<WaitFor> {
|
||||||
self.args.clone()
|
vec![WaitFor::message_on_stdout("Run server thread name: RPC")]
|
||||||
}
|
|
||||||
|
|
||||||
fn volumes(&self) -> Self::Volumes {
|
|
||||||
HashMap::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn env_vars(&self) -> Self::EnvVars {
|
|
||||||
HashMap::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_args(self, args: <Self as Image>::Args) -> Self {
|
|
||||||
Self { args }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entrypoint(&self) -> Option<String> {
|
fn entrypoint(&self) -> Option<String> {
|
||||||
@ -104,10 +60,9 @@ impl Image for MoneroWalletRpc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MoneroWalletRpc {
|
impl MoneroWalletRpc {
|
||||||
pub fn new(name: &str, daemon_address: String) -> Self {
|
pub fn new(name: &str, daemon_address: String) -> (Self, MoneroWalletRpcArgs) {
|
||||||
Self {
|
let args = MoneroWalletRpcArgs::new(name, daemon_address);
|
||||||
args: MoneroWalletRpcArgs::new(name, daemon_address),
|
(Self, args)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,6 +146,12 @@ impl IntoIterator for MonerodArgs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ImageArgs for MonerodArgs {
|
||||||
|
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
|
||||||
|
Box::new(self.into_iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MoneroWalletRpcArgs {
|
pub struct MoneroWalletRpcArgs {
|
||||||
pub disable_rpc_login: bool,
|
pub disable_rpc_login: bool,
|
||||||
@ -200,12 +161,6 @@ pub struct MoneroWalletRpcArgs {
|
|||||||
pub daemon_address: String,
|
pub daemon_address: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MoneroWalletRpcArgs {
|
|
||||||
fn default() -> Self {
|
|
||||||
unimplemented!("A default instance for `MoneroWalletRpc` doesn't make sense because we always need to connect to a node.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MoneroWalletRpcArgs {
|
impl MoneroWalletRpcArgs {
|
||||||
pub fn new(wallet_name: &str, daemon_address: String) -> Self {
|
pub fn new(wallet_name: &str, daemon_address: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -247,3 +202,9 @@ impl IntoIterator for MoneroWalletRpcArgs {
|
|||||||
args.into_iter()
|
args.into_iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ImageArgs for MoneroWalletRpcArgs {
|
||||||
|
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
|
||||||
|
Box::new(self.into_iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -20,17 +20,20 @@
|
|||||||
//! every BLOCK_TIME_SECS seconds.
|
//! every BLOCK_TIME_SECS seconds.
|
||||||
//!
|
//!
|
||||||
//! Also provides standalone JSON RPC clients for monerod and monero-wallet-rpc.
|
//! Also provides standalone JSON RPC clients for monerod and monero-wallet-rpc.
|
||||||
pub mod image;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};
|
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
|
use testcontainers::clients::Cli;
|
||||||
|
use testcontainers::{Container, RunnableImage};
|
||||||
|
use tokio::time;
|
||||||
|
|
||||||
use monero_rpc::monerod;
|
use monero_rpc::monerod;
|
||||||
use monero_rpc::monerod::MonerodRpc as _;
|
use monero_rpc::monerod::MonerodRpc as _;
|
||||||
use monero_rpc::wallet::{self, GetAddress, MoneroWalletRpc as _, Refreshed, Transfer};
|
use monero_rpc::wallet::{self, GetAddress, MoneroWalletRpc as _, Refreshed, Transfer};
|
||||||
use std::time::Duration;
|
|
||||||
use testcontainers::clients::Cli;
|
use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};
|
||||||
use testcontainers::{Container, Docker, RunArgs};
|
|
||||||
use tokio::time;
|
pub mod image;
|
||||||
|
|
||||||
/// How often we mine a block.
|
/// How often we mine a block.
|
||||||
const BLOCK_TIME_SECS: u64 = 1;
|
const BLOCK_TIME_SECS: u64 = 1;
|
||||||
@ -56,8 +59,8 @@ impl<'c> Monero {
|
|||||||
additional_wallets: Vec<&'static str>,
|
additional_wallets: Vec<&'static str>,
|
||||||
) -> Result<(
|
) -> Result<(
|
||||||
Self,
|
Self,
|
||||||
Container<'c, Cli, image::Monerod>,
|
Container<'c, image::Monerod>,
|
||||||
Vec<Container<'c, Cli, image::MoneroWalletRpc>>,
|
Vec<Container<'c, image::MoneroWalletRpc>>,
|
||||||
)> {
|
)> {
|
||||||
let prefix = format!("{}_", random_prefix());
|
let prefix = format!("{}_", random_prefix());
|
||||||
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
|
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
|
||||||
@ -221,15 +224,14 @@ impl<'c> Monerod {
|
|||||||
cli: &'c Cli,
|
cli: &'c Cli,
|
||||||
name: String,
|
name: String,
|
||||||
network: String,
|
network: String,
|
||||||
) -> Result<(Self, Container<'c, Cli, image::Monerod>)> {
|
) -> Result<(Self, Container<'c, image::Monerod>)> {
|
||||||
let image = image::Monerod::default();
|
let image = image::Monerod;
|
||||||
let run_args = RunArgs::default()
|
let image: RunnableImage<image::Monerod> = RunnableImage::from(image)
|
||||||
.with_name(name.clone())
|
.with_container_name(name.clone())
|
||||||
.with_network(network.clone());
|
.with_network(network.clone());
|
||||||
let container = cli.run_with_args(image, run_args);
|
|
||||||
let monerod_rpc_port = container
|
let container = cli.run(image);
|
||||||
.get_host_port(RPC_PORT)
|
let monerod_rpc_port = container.get_host_port_ipv4(RPC_PORT);
|
||||||
.context("port not exposed")?;
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Self {
|
Self {
|
||||||
@ -262,19 +264,15 @@ impl<'c> MoneroWalletRpc {
|
|||||||
name: &str,
|
name: &str,
|
||||||
monerod: &Monerod,
|
monerod: &Monerod,
|
||||||
prefix: String,
|
prefix: String,
|
||||||
) -> Result<(Self, Container<'c, Cli, image::MoneroWalletRpc>)> {
|
) -> Result<(Self, Container<'c, image::MoneroWalletRpc>)> {
|
||||||
let daemon_address = format!("{}:{}", monerod.name, RPC_PORT);
|
let daemon_address = format!("{}:{}", monerod.name, RPC_PORT);
|
||||||
let image = image::MoneroWalletRpc::new(name, daemon_address);
|
let (image, args) = image::MoneroWalletRpc::new(name, daemon_address);
|
||||||
|
let image = RunnableImage::from((image, args))
|
||||||
|
.with_container_name(format!("{}{}", prefix, name))
|
||||||
|
.with_network(monerod.network.clone());
|
||||||
|
|
||||||
let network = monerod.network.clone();
|
let container = cli.run(image);
|
||||||
let run_args = RunArgs::default()
|
let wallet_rpc_port = container.get_host_port_ipv4(RPC_PORT);
|
||||||
// prefix the container name so we can run multiple tests
|
|
||||||
.with_name(format!("{}{}", prefix, name))
|
|
||||||
.with_network(network.clone());
|
|
||||||
let container = cli.run_with_args(image, run_args);
|
|
||||||
let wallet_rpc_port = container
|
|
||||||
.get_host_port(RPC_PORT)
|
|
||||||
.context("port not exposed")?;
|
|
||||||
|
|
||||||
let client = wallet::Client::localhost(wallet_rpc_port)?;
|
let client = wallet::Client::localhost(wallet_rpc_port)?;
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use monero_harness::Monero;
|
use monero_harness::Monero;
|
||||||
use monero_rpc::monerod::MonerodRpc as _;
|
use monero_rpc::monerod::MonerodRpc as _;
|
||||||
use spectral::prelude::*;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
@ -21,12 +20,12 @@ async fn init_miner_and_mine_to_miner_address() {
|
|||||||
let miner_wallet = monero.wallet("miner").unwrap();
|
let miner_wallet = monero.wallet("miner").unwrap();
|
||||||
|
|
||||||
let got_miner_balance = miner_wallet.balance().await.unwrap();
|
let got_miner_balance = miner_wallet.balance().await.unwrap();
|
||||||
assert_that!(got_miner_balance).is_greater_than(0);
|
assert!(got_miner_balance > 0);
|
||||||
|
|
||||||
time::sleep(Duration::from_millis(1010)).await;
|
time::sleep(Duration::from_millis(1010)).await;
|
||||||
|
|
||||||
// after a bit more than 1 sec another block should have been mined
|
// after a bit more than 1 sec another block should have been mined
|
||||||
let block_height = monerod.client().get_block_count().await.unwrap().count;
|
let block_height = monerod.client().get_block_count().await.unwrap().count;
|
||||||
|
|
||||||
assert_that(&block_height).is_greater_than(70);
|
assert!(block_height > 70);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use monero_harness::{Monero, MoneroWalletRpc};
|
use monero_harness::{Monero, MoneroWalletRpc};
|
||||||
use monero_rpc::wallet::MoneroWalletRpc as _;
|
use monero_rpc::wallet::MoneroWalletRpc as _;
|
||||||
use spectral::prelude::*;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
@ -29,7 +28,7 @@ async fn fund_transfer_and_check_tx_key() {
|
|||||||
|
|
||||||
// check alice balance
|
// check alice balance
|
||||||
let got_alice_balance = alice_wallet.balance().await.unwrap();
|
let got_alice_balance = alice_wallet.balance().await.unwrap();
|
||||||
assert_that(&got_alice_balance).is_equal_to(fund_alice);
|
assert_eq!(got_alice_balance, fund_alice);
|
||||||
|
|
||||||
// transfer from alice to bob
|
// transfer from alice to bob
|
||||||
let bob_address = bob_wallet.address().await.unwrap().address;
|
let bob_address = bob_wallet.address().await.unwrap().address;
|
||||||
@ -41,7 +40,7 @@ async fn fund_transfer_and_check_tx_key() {
|
|||||||
wait_for_wallet_to_catch_up(bob_wallet, send_to_bob).await;
|
wait_for_wallet_to_catch_up(bob_wallet, send_to_bob).await;
|
||||||
|
|
||||||
let got_bob_balance = bob_wallet.balance().await.unwrap();
|
let got_bob_balance = bob_wallet.balance().await.unwrap();
|
||||||
assert_that(&got_bob_balance).is_equal_to(send_to_bob);
|
assert_eq!(got_bob_balance, send_to_bob);
|
||||||
|
|
||||||
// check if tx was actually seen
|
// check if tx was actually seen
|
||||||
let tx_id = transfer.tx_hash;
|
let tx_id = transfer.tx_hash;
|
||||||
@ -52,7 +51,7 @@ async fn fund_transfer_and_check_tx_key() {
|
|||||||
.await
|
.await
|
||||||
.expect("failed to check tx by key");
|
.expect("failed to check tx by key");
|
||||||
|
|
||||||
assert_that!(res.received).is_equal_to(send_to_bob);
|
assert_eq!(res.received, send_to_bob);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn wait_for_wallet_to_catch_up(wallet: &MoneroWalletRpc, expected_balance: u64) {
|
async fn wait_for_wallet_to_catch_up(wallet: &MoneroWalletRpc, expected_balance: u64) {
|
||||||
|
@ -12,7 +12,7 @@ jsonrpc_client = { version = "0.7", features = [ "reqwest" ] }
|
|||||||
monero = "0.12"
|
monero = "0.12"
|
||||||
monero-epee-bin-serde = "1"
|
monero-epee-bin-serde = "1"
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
reqwest = { version = "0.11", default-features = false, features = [ "json" ] }
|
reqwest = { version = "0.12", default-features = false, features = [ "json" ] }
|
||||||
rust_decimal = { version = "1", features = [ "serde-float" ] }
|
rust_decimal = { version = "1", features = [ "serde-float" ] }
|
||||||
serde = { version = "1.0", features = [ "derive" ] }
|
serde = { version = "1.0", features = [ "derive" ] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
@ -162,6 +162,12 @@ pub struct BlockHeight {
|
|||||||
pub height: u32,
|
pub height: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for BlockHeight {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Deserialize)]
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||||
#[serde(from = "CheckTxKeyResponse")]
|
#[serde(from = "CheckTxKeyResponse")]
|
||||||
pub struct CheckTxKey {
|
pub struct CheckTxKey {
|
||||||
|
@ -14,6 +14,6 @@ rand = "0.7"
|
|||||||
curve25519-dalek = "3"
|
curve25519-dalek = "3"
|
||||||
monero-harness = { path = "../monero-harness" }
|
monero-harness = { path = "../monero-harness" }
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
testcontainers = "0.12"
|
testcontainers = "0.15"
|
||||||
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs" ] }
|
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs" ] }
|
||||||
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "chrono", "tracing-log" ] }
|
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "chrono", "tracing-log" ] }
|
||||||
|
@ -61,13 +61,12 @@ mod tests {
|
|||||||
use monero_harness::image::Monerod;
|
use monero_harness::image::Monerod;
|
||||||
use monero_rpc::monerod::{Client, GetOutputsOut};
|
use monero_rpc::monerod::{Client, GetOutputsOut};
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
use testcontainers::Docker;
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn get_outs_for_key_offsets() {
|
async fn get_outs_for_key_offsets() {
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let container = cli.run(Monerod::default());
|
let container = cli.run(Monerod);
|
||||||
let rpc_client = Client::localhost(container.get_host_port(18081).unwrap()).unwrap();
|
let rpc_client = Client::localhost(container.get_host_port_ipv4(18081)).unwrap();
|
||||||
rpc_client.generateblocks(150, "498AVruCDWgP9Az9LjMm89VWjrBrSZ2W2K3HFBiyzzrRjUJWUcCVxvY1iitfuKoek2FdX6MKGAD9Qb1G1P8QgR5jPmmt3Vj".to_owned()).await.unwrap();
|
rpc_client.generateblocks(150, "498AVruCDWgP9Az9LjMm89VWjrBrSZ2W2K3HFBiyzzrRjUJWUcCVxvY1iitfuKoek2FdX6MKGAD9Qb1G1P8QgR5jPmmt3Vj".to_owned()).await.unwrap();
|
||||||
let wallet = Wallet {
|
let wallet = Wallet {
|
||||||
client: rpc_client.clone(),
|
client: rpc_client.clone(),
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
[toolchain]
|
[toolchain]
|
||||||
channel = "1.67" # also update this in the readme, changelog, and github actions
|
channel = "1.74" # also update this in the readme, changelog, and github actions
|
||||||
components = ["clippy"]
|
components = ["clippy"]
|
||||||
targets = ["armv7-unknown-linux-gnueabihf"]
|
targets = ["armv7-unknown-linux-gnueabihf"]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "swap"
|
name = "swap"
|
||||||
version = "0.12.2"
|
version = "0.13.4"
|
||||||
authors = [ "The COMIT guys <hello@comit.network>" ]
|
authors = [ "The COMIT guys <hello@comit.network>" ]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "XMR/BTC trustless atomic swaps."
|
description = "XMR/BTC trustless atomic swaps."
|
||||||
@ -14,32 +14,37 @@ async-compression = { version = "0.3", features = [ "bzip2", "tokio" ] }
|
|||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
atty = "0.2"
|
atty = "0.2"
|
||||||
backoff = { version = "0.4", features = [ "tokio" ] }
|
backoff = { version = "0.4", features = [ "tokio" ] }
|
||||||
base64 = "0.21"
|
base64 = "0.22"
|
||||||
bdk = "0.28"
|
bdk = "0.28"
|
||||||
big-bytes = "1"
|
big-bytes = "1"
|
||||||
bitcoin = { version = "0.29", features = [ "rand", "serde" ] }
|
bitcoin = { version = "0.29", features = [ "rand", "serde" ] }
|
||||||
bmrng = "0.5"
|
bmrng = "0.5"
|
||||||
comfy-table = "6.1"
|
comfy-table = "7.1"
|
||||||
config = { version = "0.13", default-features = false, features = [ "toml" ] }
|
config = { version = "0.14", default-features = false, features = [ "toml" ] }
|
||||||
conquer-once = "0.4"
|
conquer-once = "0.4"
|
||||||
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" }
|
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" }
|
||||||
data-encoding = "2.4"
|
data-encoding = "2.6"
|
||||||
dialoguer = "0.10"
|
dialoguer = "0.11"
|
||||||
|
digest = "0.10.7"
|
||||||
directories-next = "2"
|
directories-next = "2"
|
||||||
ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] }
|
ecdsa_fun = { version = "0.10", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] }
|
||||||
ed25519-dalek = "1"
|
ed25519-dalek = "1"
|
||||||
futures = { version = "0.3", default-features = false }
|
futures = { version = "0.3", default-features = false }
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
itertools = "0.10"
|
itertools = "0.13"
|
||||||
|
jsonrpsee = { version = "0.16.2", features = [ "server" ] }
|
||||||
|
jsonrpsee-core = "0.16.2"
|
||||||
libp2p = { version = "0.42.2", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous", "identify" ] }
|
libp2p = { version = "0.42.2", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous", "identify" ] }
|
||||||
monero = { version = "0.12", features = [ "serde_support" ] }
|
monero = { version = "0.12", features = [ "serde_support" ] }
|
||||||
monero-rpc = { path = "../monero-rpc" }
|
monero-rpc = { path = "../monero-rpc" }
|
||||||
pem = "1.1"
|
once_cell = "1.20"
|
||||||
|
pem = "3.0"
|
||||||
proptest = "1"
|
proptest = "1"
|
||||||
qrcode = "0.12"
|
qrcode = "0.14"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rand_chacha = "0.3"
|
rand_chacha = "0.3"
|
||||||
reqwest = { version = "0.11", features = [ "rustls-tls", "stream", "socks" ], default-features = false }
|
regex = "1.11"
|
||||||
|
reqwest = { version = "0.12", features = [ "http2", "rustls-tls", "stream", "socks" ], default-features = false }
|
||||||
rust_decimal = { version = "1", features = [ "serde-float" ] }
|
rust_decimal = { version = "1", features = [ "serde-float" ] }
|
||||||
rust_decimal_macros = "1"
|
rust_decimal_macros = "1"
|
||||||
serde = { version = "1", features = [ "derive" ] }
|
serde = { version = "1", features = [ "derive" ] }
|
||||||
@ -47,24 +52,24 @@ serde_cbor = "0.11"
|
|||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
serde_with = { version = "1", features = [ "macros" ] }
|
serde_with = { version = "1", features = [ "macros" ] }
|
||||||
sha2 = "0.10"
|
sha2 = "0.10"
|
||||||
sigma_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "ed25519", "serde", "secp256k1", "alloc" ] }
|
sigma_fun = { version = "0.7", default-features = false, features = [ "ed25519", "serde", "secp256k1", "alloc" ] }
|
||||||
sqlx = { version = "0.6", features = [ "sqlite", "runtime-tokio-rustls", "offline" ] }
|
sqlx = { version = "0.6.3", features = [ "sqlite", "runtime-tokio-rustls", "offline" ] }
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
strum = { version = "0.24", features = [ "derive" ] }
|
strum = { version = "0.26", features = [ "derive" ] }
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] }
|
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net", "parking_lot" ] }
|
||||||
tokio-socks = "0.5"
|
tokio-socks = "0.5"
|
||||||
tokio-tungstenite = { version = "0.15", features = [ "rustls-tls" ] }
|
tokio-tungstenite = { version = "0.15", features = [ "rustls-tls" ] }
|
||||||
tokio-util = { version = "0.7", features = [ "io", "codec" ] }
|
tokio-util = { version = "0.7", features = [ "io", "codec" ] }
|
||||||
toml = "0.5"
|
toml = "0.8"
|
||||||
torut = { version = "0.2", default-features = false, features = [ "v3", "control" ] }
|
torut = { version = "0.2", default-features = false, features = [ "v3", "control" ] }
|
||||||
tracing = { version = "0.1", features = [ "attributes" ] }
|
tracing = { version = "0.1", features = [ "attributes" ] }
|
||||||
tracing-appender = "0.2"
|
tracing-appender = "0.2"
|
||||||
tracing-futures = { version = "0.2", features = [ "std-future", "futures-03" ] }
|
tracing-futures = { version = "0.2", features = [ "std-future", "futures-03" ] }
|
||||||
tracing-subscriber = { version = "0.3", default-features = false, features = [ "fmt", "ansi", "env-filter", "time", "tracing-log", "json" ] }
|
tracing-subscriber = { version = "0.3", default-features = false, features = [ "fmt", "ansi", "env-filter", "time", "tracing-log", "json" ] }
|
||||||
url = { version = "2", features = [ "serde" ] }
|
url = { version = "2", features = [ "serde" ] }
|
||||||
uuid = { version = "1.4", features = [ "serde", "v4" ] }
|
uuid = { version = "1.11", features = [ "serde", "v4" ] }
|
||||||
void = "1"
|
void = "1"
|
||||||
|
|
||||||
[target.'cfg(not(windows))'.dependencies]
|
[target.'cfg(not(windows))'.dependencies]
|
||||||
@ -74,18 +79,20 @@ tokio-tar = "0.3"
|
|||||||
zip = "0.5"
|
zip = "0.5"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bitcoin-harness = "0.2.2"
|
bitcoin-harness = { git = "https://github.com/delta1/bitcoin-harness-rs.git", rev = "80cc8d05db2610d8531011be505b7bee2b5cdf9f" }
|
||||||
get-port = "3"
|
get-port = "3"
|
||||||
hyper = "0.14"
|
hyper = "1.5"
|
||||||
|
jsonrpsee = { version = "0.16.2", features = [ "ws-client" ] }
|
||||||
|
mockito = "1.5"
|
||||||
monero-harness = { path = "../monero-harness" }
|
monero-harness = { path = "../monero-harness" }
|
||||||
port_check = "0.1"
|
port_check = "0.2"
|
||||||
proptest = "1"
|
proptest = "1"
|
||||||
|
sequential-test = "0.2.4"
|
||||||
serde_cbor = "0.11"
|
serde_cbor = "0.11"
|
||||||
serial_test = "2.0"
|
serial_test = "3.1"
|
||||||
spectral = "0.6"
|
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
testcontainers = "0.12"
|
testcontainers = "0.15"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
vergen = { version = "7.5", default-features = false, features = [ "git", "build" ] }
|
vergen = { version = "8.3", default-features = false, features = [ "build", "git", "git2" ] }
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use vergen::{vergen, Config, SemverKind};
|
use vergen::EmitBuilder;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut config = Config::default();
|
EmitBuilder::builder()
|
||||||
*config.git_mut().semver_kind_mut() = SemverKind::Lightweight;
|
.git_describe(true, true, None)
|
||||||
|
.emit()?;
|
||||||
vergen(config)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
135
swap/migrations/20240615140942_btcpunished_update.sql
Normal file
135
swap/migrations/20240615140942_btcpunished_update.sql
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
-- This migration script modifies swap states to be compatible with the new state structure introduced in PR #1676.
|
||||||
|
-- The following changes are made:
|
||||||
|
-- 1. Bob: BtcPunished state now has a new attribute 'state' (type: State6), 'tx_lock_id' (type: string) remains the same
|
||||||
|
-- 2. Bob: State6 has two new attributes: 'v' (monero viewkey) and 'monero_wallet_restore_blockheight' (type: BlockHeight)
|
||||||
|
-- State6 is used in BtcPunished, CancelTimelockExpired, BtcCancelled, BtcRefunded states
|
||||||
|
-- 3. Alice: BtcPunished state now has a new attribute 'state3' (type: State3)
|
||||||
|
|
||||||
|
-- Alice: Add new attribute 'state3' (type: State3) to the BtcPunished state by copying it from the BtcLocked state
|
||||||
|
UPDATE swap_states SET
|
||||||
|
state = json_replace( -- Replaces "{"Alice":{"Done":"BtcPunished"}}" with "{"Alice": {"Done": "BtcPunished": {"state": <state3 object from BtcLocked>} }}"
|
||||||
|
state,
|
||||||
|
'$.Alice.Done',
|
||||||
|
json_object(
|
||||||
|
'BtcPunished',
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Alice.BtcLocked') -- Read state3 object from BtcLocked
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id -- swap_states.swap_id is id of the BtcPunished row
|
||||||
|
AND json_extract(states.state, '$.Alice.BtcLocked') IS NOT NULL -- Filters out only the BtcLocked state
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WHERE json_extract(state, '$.Alice.Done') = 'BtcPunished'; -- Apply update only to BtcPunished state rows
|
||||||
|
|
||||||
|
-- Bob: Add new attribute 'state6' (type: State6) to the BtcPunished state by copying it from the BtcCancelled state
|
||||||
|
-- and add new State6 attributes 'v' and 'monero_wallet_restore_blockheight' from the BtcLocked state
|
||||||
|
UPDATE swap_states SET
|
||||||
|
state = json_replace(
|
||||||
|
state,
|
||||||
|
'$.Bob', -- Replace '{"Bob":{"Done": {"BtcPunished": {"tx_lock_id":"..."} }}}' with {"Bob":{"BtcPunished":{"state":{<state6 object>}, "tx_lock_id": "..."}}}
|
||||||
|
json_object(
|
||||||
|
'BtcPunished', -- {"Bob":{"BtcPunished":{}}
|
||||||
|
json_object(
|
||||||
|
'state', -- {"Bob":{"BtcPunished":{"state": {}}}
|
||||||
|
json_insert(
|
||||||
|
( -- object that we insert properties into (original state6 from BtcCancelled state)
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcCancelled') -- Get state6 from BtcCancelled state
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcCancelled') IS NOT NULL -- Filters out only the BtcCancelled state
|
||||||
|
),
|
||||||
|
'$.v', -- {"Bob":{"BtcPunished":{"state": {..., "v": "..."}, "tx_lock_id": "..."}}}
|
||||||
|
( -- Get v property from BtcLocked state
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.state3.v')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id -- swap_states.swap_id is id of the BtcPunished row
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL -- Filters out only the BtcLocked state
|
||||||
|
),
|
||||||
|
'$.monero_wallet_restore_blockheight', -- { "Bob": { "BtcPunished":{"state": {..., "monero_wallet_restore_blockheight": {"height":...}} }, "tx_lock_id": "..."} } }
|
||||||
|
( -- Get monero_wallet_restore_blockheight property from BtcLocked state
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.monero_wallet_restore_blockheight')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id -- swap_states.swap_id is id of the BtcPunished row, states.swap_id is id of the row that we are looking for
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL -- Filters out only the BtcLocked state
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'tx_lock_id', -- Insert tx_lock_id BtcPunished -> {"Bob": {"Done": {"BtcPunished": {"state":{<state object>}, "tx_lock_id": "..."} } }
|
||||||
|
json_extract(state, '$.Bob.Done.BtcPunished.tx_lock_id') -- Gets tx_lock_id from original state row
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WHERE json_extract(state, '$.Bob.Done.BtcPunished') IS NOT NULL; -- Apply update only to BtcPunished state rows
|
||||||
|
|
||||||
|
-- Bob: Add new State6 attributes 'v' and 'monero_wallet_restore_blockheight' to the BtcRefunded state
|
||||||
|
UPDATE swap_states SET
|
||||||
|
state = json_insert(
|
||||||
|
state, -- Object that we insert properties into (original state from the row)
|
||||||
|
'$.Bob.Done.BtcRefunded.v', -- {"Bob":{"BtcRefunded":{..., "v": "..."}}}
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.state3.v')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id -- swap_states.swap_id is id of the BtcRefunded row, states.swap_id is id of the row that we are looking for
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL
|
||||||
|
),
|
||||||
|
'$.Bob.Done.BtcRefunded.monero_wallet_restore_blockheight', -- {"Bob":{"BtcRefunded":{..., "monero_wallet_restore_blockheight": {"height":...}} }}
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.monero_wallet_restore_blockheight')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WHERE json_extract(state, '$.Bob.Done.BtcRefunded') IS NOT NULL; -- Apply update only to BtcRefunded state rows
|
||||||
|
|
||||||
|
-- Bob: Add new State6 attributes 'v' and 'monero_wallet_restore_blockheight' to the BtcCancelled state
|
||||||
|
UPDATE swap_states SET
|
||||||
|
state = json_insert(
|
||||||
|
state,
|
||||||
|
'$.Bob.BtcCancelled.v',
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.state3.v')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL
|
||||||
|
),
|
||||||
|
'$.Bob.BtcCancelled.monero_wallet_restore_blockheight',
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.monero_wallet_restore_blockheight')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WHERE json_extract(state, '$.Bob.BtcCancelled') IS NOT NULL; -- Apply update only to BtcCancelled state rows
|
||||||
|
|
||||||
|
-- Bob: Add new State6 attributes 'v' and 'monero_wallet_restore_blockheight' to the CancelTimelockExpired state
|
||||||
|
UPDATE swap_states SET
|
||||||
|
state = json_insert(
|
||||||
|
state,
|
||||||
|
'$.Bob.CancelTimelockExpired.v',
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.state3.v')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL
|
||||||
|
),
|
||||||
|
'$.Bob.CancelTimelockExpired.monero_wallet_restore_blockheight',
|
||||||
|
(
|
||||||
|
SELECT json_extract(states.state, '$.Bob.BtcLocked.monero_wallet_restore_blockheight')
|
||||||
|
FROM swap_states AS states
|
||||||
|
WHERE
|
||||||
|
states.swap_id = swap_states.swap_id
|
||||||
|
AND json_extract(states.state, '$.Bob.BtcLocked') IS NOT NULL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WHERE json_extract(state, '$.Bob.CancelTimelockExpired') IS NOT NULL; -- Apply update only to CancelTimelockExpired state rows
|
@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE if NOT EXISTS buffered_transfer_proofs
|
||||||
|
(
|
||||||
|
swap_id TEXT PRIMARY KEY NOT NULL,
|
||||||
|
proof TEXT NOT NULL
|
||||||
|
);
|
@ -1,7 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# run this script from the swap dir
|
# run this script from the swap dir
|
||||||
# make sure you have sqlx-cli installed: cargo install sqlx-cli
|
# make sure you have sqlx-cli installed: cargo install --version 0.6.3 sqlx-cli
|
||||||
|
# it's advised for the sqlx-cli to be the same version as specified in cargo.toml
|
||||||
|
|
||||||
# this script creates a temporary sqlite database
|
# this script creates a temporary sqlite database
|
||||||
# then runs the migration scripts to create the tables (migrations folder)
|
# then runs the migration scripts to create the tables (migrations folder)
|
||||||
|
@ -28,6 +28,24 @@
|
|||||||
},
|
},
|
||||||
"query": "\n insert into peer_addresses (\n peer_id,\n address\n ) values (?, ?);\n "
|
"query": "\n insert into peer_addresses (\n peer_id,\n address\n ) values (?, ?);\n "
|
||||||
},
|
},
|
||||||
|
"0d465a17ebbb5761421def759c73cad023c30705d5b41a1399ef79d8d2571d7c": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "start_date",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
true
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT min(entered_at) as start_date\n FROM swap_states\n WHERE swap_id = ?\n "
|
||||||
|
},
|
||||||
"1ec38c85e7679b2eb42b3df75d9098772ce44fdb8db3012d3c2410d828b74157": {
|
"1ec38c85e7679b2eb42b3df75d9098772ce44fdb8db3012d3c2410d828b74157": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
@ -62,6 +80,30 @@
|
|||||||
},
|
},
|
||||||
"query": "\n insert into peers (\n swap_id,\n peer_id\n ) values (?, ?);\n "
|
"query": "\n insert into peers (\n swap_id,\n peer_id\n ) values (?, ?);\n "
|
||||||
},
|
},
|
||||||
|
"3f2bfdd2d134586ccad22171cd85a465800fc5c4fdaf191d206974e530240c87": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "swap_id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT swap_id, state\n FROM swap_states\n "
|
||||||
|
},
|
||||||
"50a5764546f69c118fa0b64120da50f51073d36257d49768de99ff863e3511e0": {
|
"50a5764546f69c118fa0b64120da50f51073d36257d49768de99ff863e3511e0": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
@ -90,24 +132,6 @@
|
|||||||
},
|
},
|
||||||
"query": "\n SELECT state\n FROM swap_states\n WHERE swap_id = ?\n ORDER BY id desc\n LIMIT 1;\n\n "
|
"query": "\n SELECT state\n FROM swap_states\n WHERE swap_id = ?\n ORDER BY id desc\n LIMIT 1;\n\n "
|
||||||
},
|
},
|
||||||
"a0eb85d04ee3842c52291dad4d225941d1141af735922fcbc665868997fce304": {
|
|
||||||
"describe": {
|
|
||||||
"columns": [
|
|
||||||
{
|
|
||||||
"name": "address",
|
|
||||||
"ordinal": 0,
|
|
||||||
"type_info": "Text"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"nullable": [
|
|
||||||
false
|
|
||||||
],
|
|
||||||
"parameters": {
|
|
||||||
"Right": 1
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"query": "\n SELECT address\n FROM peer_addresses\n WHERE peer_id = ?\n "
|
|
||||||
},
|
|
||||||
"b703032b4ddc627a1124817477e7a8e5014bdc694c36a14053ef3bb2fc0c69b0": {
|
"b703032b4ddc627a1124817477e7a8e5014bdc694c36a14053ef3bb2fc0c69b0": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
@ -135,5 +159,69 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"query": "\n SELECT address\n FROM monero_addresses\n WHERE swap_id = ?\n "
|
"query": "\n SELECT address\n FROM monero_addresses\n WHERE swap_id = ?\n "
|
||||||
|
},
|
||||||
|
"d78acba5eb8563826dd190e0886aa665aae3c6f1e312ee444e65df1c95afe8b2": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "address",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT DISTINCT address\n FROM peer_addresses\n WHERE peer_id = ?\n "
|
||||||
|
},
|
||||||
|
"e05620f420f8c1022971eeb66a803323a8cf258cbebb2834e3f7cf8f812fa646": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT state\n FROM swap_states\n WHERE swap_id = ?\n "
|
||||||
|
},
|
||||||
|
"e36c287aa98ae80ad4b6bb6f7e4b59cced041406a9db71da827b09f0d3bacfd6": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"nullable": [],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n INSERT INTO buffered_transfer_proofs (\n swap_id,\n proof\n ) VALUES (?, ?);\n "
|
||||||
|
},
|
||||||
|
"e9d422daf774d099fcbde6c4cda35821da948bd86cc57798b4d8375baf0b51ae": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "proof",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT proof\n FROM buffered_transfer_proofs\n WHERE swap_id = ?\n "
|
||||||
}
|
}
|
||||||
}
|
}
|
478
swap/src/api.rs
Normal file
478
swap/src/api.rs
Normal file
@ -0,0 +1,478 @@
|
|||||||
|
pub mod request;
|
||||||
|
use crate::cli::command::{Bitcoin, Monero, Tor};
|
||||||
|
use crate::common::tracing_util::Format;
|
||||||
|
use crate::database::{open_db, AccessMode};
|
||||||
|
use crate::env::{Config as EnvConfig, GetConfig, Mainnet, Testnet};
|
||||||
|
use crate::fs::system_data_dir;
|
||||||
|
use crate::network::rendezvous::XmrBtcNamespace;
|
||||||
|
use crate::protocol::Database;
|
||||||
|
use crate::seed::Seed;
|
||||||
|
use crate::{bitcoin, common, monero};
|
||||||
|
use anyhow::{bail, Context as AnyContext, Error, Result};
|
||||||
|
use futures::future::try_join_all;
|
||||||
|
use std::fmt;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::{Arc, Once};
|
||||||
|
use tokio::sync::{broadcast, broadcast::Sender, Mutex, RwLock};
|
||||||
|
use tokio::task::JoinHandle;
|
||||||
|
use tracing::level_filters::LevelFilter;
|
||||||
|
use tracing::Level;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
static START: Once = Once::new();
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
|
pub struct Config {
|
||||||
|
tor_socks5_port: u16,
|
||||||
|
namespace: XmrBtcNamespace,
|
||||||
|
server_address: Option<SocketAddr>,
|
||||||
|
pub env_config: EnvConfig,
|
||||||
|
seed: Option<Seed>,
|
||||||
|
debug: bool,
|
||||||
|
json: bool,
|
||||||
|
data_dir: PathBuf,
|
||||||
|
is_testnet: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct PendingTaskList(Mutex<Vec<JoinHandle<()>>>);
|
||||||
|
|
||||||
|
impl PendingTaskList {
|
||||||
|
pub async fn spawn<F, T>(&self, future: F)
|
||||||
|
where
|
||||||
|
F: Future<Output = T> + Send + 'static,
|
||||||
|
T: Send + 'static,
|
||||||
|
{
|
||||||
|
let handle = tokio::spawn(async move {
|
||||||
|
let _ = future.await;
|
||||||
|
});
|
||||||
|
|
||||||
|
self.0.lock().await.push(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn wait_for_tasks(&self) -> Result<()> {
|
||||||
|
let tasks = {
|
||||||
|
// Scope for the lock, to avoid holding it for the entire duration of the async block
|
||||||
|
let mut guard = self.0.lock().await;
|
||||||
|
guard.drain(..).collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
|
||||||
|
try_join_all(tasks).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SwapLock {
|
||||||
|
current_swap: RwLock<Option<Uuid>>,
|
||||||
|
suspension_trigger: Sender<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SwapLock {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let (suspension_trigger, _) = broadcast::channel(10);
|
||||||
|
SwapLock {
|
||||||
|
current_swap: RwLock::new(None),
|
||||||
|
suspension_trigger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn listen_for_swap_force_suspension(&self) -> Result<(), Error> {
|
||||||
|
let mut listener = self.suspension_trigger.subscribe();
|
||||||
|
let event = listener.recv().await;
|
||||||
|
match event {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Error receiving swap suspension signal: {}", e);
|
||||||
|
bail!(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn acquire_swap_lock(&self, swap_id: Uuid) -> Result<(), Error> {
|
||||||
|
let mut current_swap = self.current_swap.write().await;
|
||||||
|
if current_swap.is_some() {
|
||||||
|
bail!("There already exists an active swap lock");
|
||||||
|
}
|
||||||
|
|
||||||
|
tracing::debug!(swap_id = %swap_id, "Acquiring swap lock");
|
||||||
|
*current_swap = Some(swap_id);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_current_swap_id(&self) -> Option<Uuid> {
|
||||||
|
*self.current_swap.read().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends a signal to suspend all ongoing swap processes.
|
||||||
|
///
|
||||||
|
/// This function performs the following steps:
|
||||||
|
/// 1. Triggers the suspension by sending a unit `()` signal to all listeners via `self.suspension_trigger`.
|
||||||
|
/// 2. Polls the `current_swap` state every 50 milliseconds to check if it has been set to `None`, indicating that the swap processes have been suspended and the lock released.
|
||||||
|
/// 3. If the lock is not released within 10 seconds, the function returns an error.
|
||||||
|
///
|
||||||
|
/// If we send a suspend signal while no swap is in progress, the function will not fail, but will return immediately.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// - `Ok(())` if the swap lock is successfully released.
|
||||||
|
/// - `Err(Error)` if the function times out waiting for the swap lock to be released.
|
||||||
|
///
|
||||||
|
/// # Notes
|
||||||
|
/// The 50ms polling interval is considered negligible overhead compared to the typical time required to suspend ongoing swap processes.
|
||||||
|
pub async fn send_suspend_signal(&self) -> Result<(), Error> {
|
||||||
|
const TIMEOUT: u64 = 10_000;
|
||||||
|
const INTERVAL: u64 = 50;
|
||||||
|
|
||||||
|
let _ = self.suspension_trigger.send(())?;
|
||||||
|
|
||||||
|
for _ in 0..(TIMEOUT / INTERVAL) {
|
||||||
|
if self.get_current_swap_id().await.is_none() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
tokio::time::sleep(tokio::time::Duration::from_millis(INTERVAL)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("Timed out waiting for swap lock to be released");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn release_swap_lock(&self) -> Result<Uuid, Error> {
|
||||||
|
let mut current_swap = self.current_swap.write().await;
|
||||||
|
if let Some(swap_id) = current_swap.as_ref() {
|
||||||
|
tracing::debug!(swap_id = %swap_id, "Releasing swap lock");
|
||||||
|
|
||||||
|
let prev_swap_id = *swap_id;
|
||||||
|
*current_swap = None;
|
||||||
|
drop(current_swap);
|
||||||
|
Ok(prev_swap_id)
|
||||||
|
} else {
|
||||||
|
bail!("There is no current swap lock to release");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for SwapLock {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// workaround for warning over monero_rpc_process which we must own but not read
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct Context {
|
||||||
|
pub db: Arc<dyn Database + Send + Sync>,
|
||||||
|
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
|
||||||
|
monero_wallet: Option<Arc<monero::Wallet>>,
|
||||||
|
monero_rpc_process: Option<monero::WalletRpcProcess>,
|
||||||
|
pub swap_lock: Arc<SwapLock>,
|
||||||
|
pub config: Config,
|
||||||
|
pub tasks: Arc<PendingTaskList>,
|
||||||
|
pub is_daemon: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
impl Context {
|
||||||
|
pub async fn build(
|
||||||
|
bitcoin: Option<Bitcoin>,
|
||||||
|
monero: Option<Monero>,
|
||||||
|
tor: Option<Tor>,
|
||||||
|
data: Option<PathBuf>,
|
||||||
|
is_testnet: bool,
|
||||||
|
debug: bool,
|
||||||
|
json: bool,
|
||||||
|
server_address: Option<SocketAddr>,
|
||||||
|
is_daemon: bool,
|
||||||
|
) -> Result<Context> {
|
||||||
|
let data_dir = data::data_dir_from(data, is_testnet)?;
|
||||||
|
let env_config = env_config_from(is_testnet);
|
||||||
|
|
||||||
|
let format = if json { Format::Json } else { Format::Raw };
|
||||||
|
let level_filter = if debug {
|
||||||
|
LevelFilter::from_level(Level::DEBUG)
|
||||||
|
} else {
|
||||||
|
LevelFilter::from_level(Level::INFO)
|
||||||
|
};
|
||||||
|
|
||||||
|
START.call_once(|| {
|
||||||
|
let _ = common::tracing_util::init(level_filter, format, data_dir.join("logs"));
|
||||||
|
});
|
||||||
|
|
||||||
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
||||||
|
.context("Failed to read seed in file")?;
|
||||||
|
|
||||||
|
let bitcoin_wallet = {
|
||||||
|
if let Some(bitcoin) = bitcoin {
|
||||||
|
let (bitcoin_electrum_rpc_url, bitcoin_target_block) =
|
||||||
|
bitcoin.apply_defaults(is_testnet)?;
|
||||||
|
Some(Arc::new(
|
||||||
|
init_bitcoin_wallet(
|
||||||
|
bitcoin_electrum_rpc_url,
|
||||||
|
&seed,
|
||||||
|
data_dir.clone(),
|
||||||
|
env_config,
|
||||||
|
bitcoin_target_block,
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (monero_wallet, monero_rpc_process) = {
|
||||||
|
if let Some(monero) = monero {
|
||||||
|
let monero_daemon_address = monero.apply_defaults(is_testnet);
|
||||||
|
let (wlt, prc) =
|
||||||
|
init_monero_wallet(data_dir.clone(), monero_daemon_address, env_config).await?;
|
||||||
|
(Some(Arc::new(wlt)), Some(prc))
|
||||||
|
} else {
|
||||||
|
(None, None)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tor_socks5_port = tor.map_or(9050, |tor| tor.tor_socks5_port);
|
||||||
|
|
||||||
|
let context = Context {
|
||||||
|
db: open_db(data_dir.join("sqlite"), AccessMode::ReadWrite).await?,
|
||||||
|
bitcoin_wallet,
|
||||||
|
monero_wallet,
|
||||||
|
monero_rpc_process,
|
||||||
|
config: Config {
|
||||||
|
tor_socks5_port,
|
||||||
|
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
|
||||||
|
env_config,
|
||||||
|
seed: Some(seed),
|
||||||
|
server_address,
|
||||||
|
debug,
|
||||||
|
json,
|
||||||
|
is_testnet,
|
||||||
|
data_dir,
|
||||||
|
},
|
||||||
|
swap_lock: Arc::new(SwapLock::new()),
|
||||||
|
tasks: Arc::new(PendingTaskList::default()),
|
||||||
|
is_daemon,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn for_harness(
|
||||||
|
seed: Seed,
|
||||||
|
env_config: EnvConfig,
|
||||||
|
db_path: PathBuf,
|
||||||
|
bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
bob_monero_wallet: Arc<monero::Wallet>,
|
||||||
|
) -> Self {
|
||||||
|
let config = Config::for_harness(seed, env_config);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
bitcoin_wallet: Some(bob_bitcoin_wallet),
|
||||||
|
monero_wallet: Some(bob_monero_wallet),
|
||||||
|
config,
|
||||||
|
db: open_db(db_path, AccessMode::ReadWrite)
|
||||||
|
.await
|
||||||
|
.expect("Could not open sqlite database"),
|
||||||
|
monero_rpc_process: None,
|
||||||
|
swap_lock: Arc::new(SwapLock::new()),
|
||||||
|
tasks: Arc::new(PendingTaskList::default()),
|
||||||
|
is_daemon: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bitcoin_wallet(&self) -> Option<Arc<bitcoin::Wallet>> {
|
||||||
|
self.bitcoin_wallet.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Context {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn init_bitcoin_wallet(
|
||||||
|
electrum_rpc_url: Url,
|
||||||
|
seed: &Seed,
|
||||||
|
data_dir: PathBuf,
|
||||||
|
env_config: EnvConfig,
|
||||||
|
bitcoin_target_block: usize,
|
||||||
|
) -> Result<bitcoin::Wallet> {
|
||||||
|
let wallet_dir = data_dir.join("wallet");
|
||||||
|
|
||||||
|
let wallet = bitcoin::Wallet::new(
|
||||||
|
electrum_rpc_url.clone(),
|
||||||
|
&wallet_dir,
|
||||||
|
seed.derive_extended_private_key(env_config.bitcoin_network)?,
|
||||||
|
env_config,
|
||||||
|
bitcoin_target_block,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.context("Failed to initialize Bitcoin wallet")?;
|
||||||
|
|
||||||
|
wallet.sync().await?;
|
||||||
|
|
||||||
|
Ok(wallet)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn init_monero_wallet(
|
||||||
|
data_dir: PathBuf,
|
||||||
|
monero_daemon_address: String,
|
||||||
|
env_config: EnvConfig,
|
||||||
|
) -> Result<(monero::Wallet, monero::WalletRpcProcess)> {
|
||||||
|
let network = env_config.monero_network;
|
||||||
|
|
||||||
|
const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-monitoring-wallet";
|
||||||
|
|
||||||
|
let monero_wallet_rpc = monero::WalletRpc::new(data_dir.join("monero")).await?;
|
||||||
|
|
||||||
|
let monero_wallet_rpc_process = monero_wallet_rpc
|
||||||
|
.run(network, Some(monero_daemon_address))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let monero_wallet = monero::Wallet::open_or_create(
|
||||||
|
monero_wallet_rpc_process.endpoint(),
|
||||||
|
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
|
||||||
|
env_config,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok((monero_wallet, monero_wallet_rpc_process))
|
||||||
|
}
|
||||||
|
|
||||||
|
mod data {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn data_dir_from(arg_dir: Option<PathBuf>, testnet: bool) -> Result<PathBuf> {
|
||||||
|
let base_dir = match arg_dir {
|
||||||
|
Some(custom_base_dir) => custom_base_dir,
|
||||||
|
None => os_default()?,
|
||||||
|
};
|
||||||
|
|
||||||
|
let sub_directory = if testnet { "testnet" } else { "mainnet" };
|
||||||
|
|
||||||
|
Ok(base_dir.join(sub_directory))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn os_default() -> Result<PathBuf> {
|
||||||
|
Ok(system_data_dir()?.join("cli"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn env_config_from(testnet: bool) -> EnvConfig {
|
||||||
|
if testnet {
|
||||||
|
Testnet::get_config()
|
||||||
|
} else {
|
||||||
|
Mainnet::get_config()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn for_harness(seed: Seed, env_config: EnvConfig) -> Self {
|
||||||
|
let data_dir = data::data_dir_from(None, false).expect("Could not find data directory");
|
||||||
|
|
||||||
|
Self {
|
||||||
|
tor_socks5_port: 9050,
|
||||||
|
namespace: XmrBtcNamespace::from_is_testnet(false),
|
||||||
|
server_address: None,
|
||||||
|
env_config,
|
||||||
|
seed: Some(seed),
|
||||||
|
debug: false,
|
||||||
|
json: false,
|
||||||
|
is_testnet: false,
|
||||||
|
data_dir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod api_test {
|
||||||
|
use super::*;
|
||||||
|
use crate::api::request::{Method, Request};
|
||||||
|
|
||||||
|
use libp2p::Multiaddr;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub const MULTI_ADDRESS: &str =
|
||||||
|
"/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
|
||||||
|
pub const MONERO_STAGENET_ADDRESS: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
|
||||||
|
pub const BITCOIN_TESTNET_ADDRESS: &str = "tb1qr3em6k3gfnyl8r7q0v7t4tlnyxzgxma3lressv";
|
||||||
|
pub const MONERO_MAINNET_ADDRESS: &str = "44Ato7HveWidJYUAVw5QffEcEtSH1DwzSP3FPPkHxNAS4LX9CqgucphTisH978FLHE34YNEx7FcbBfQLQUU8m3NUC4VqsRa";
|
||||||
|
pub const BITCOIN_MAINNET_ADDRESS: &str = "bc1qe4epnfklcaa0mun26yz5g8k24em5u9f92hy325";
|
||||||
|
pub const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn default(
|
||||||
|
is_testnet: bool,
|
||||||
|
data_dir: Option<PathBuf>,
|
||||||
|
debug: bool,
|
||||||
|
json: bool,
|
||||||
|
) -> Self {
|
||||||
|
let data_dir = data::data_dir_from(data_dir, is_testnet).unwrap();
|
||||||
|
|
||||||
|
let seed = Seed::from_file_or_generate(data_dir.as_path()).unwrap();
|
||||||
|
|
||||||
|
let env_config = env_config_from(is_testnet);
|
||||||
|
Self {
|
||||||
|
tor_socks5_port: 9050,
|
||||||
|
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
|
||||||
|
server_address: None,
|
||||||
|
env_config,
|
||||||
|
seed: Some(seed),
|
||||||
|
debug,
|
||||||
|
json,
|
||||||
|
is_testnet,
|
||||||
|
data_dir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Request {
|
||||||
|
pub fn buy_xmr(is_testnet: bool) -> Request {
|
||||||
|
let seller = Multiaddr::from_str(MULTI_ADDRESS).unwrap();
|
||||||
|
let bitcoin_change_address = {
|
||||||
|
if is_testnet {
|
||||||
|
bitcoin::Address::from_str(BITCOIN_TESTNET_ADDRESS).unwrap()
|
||||||
|
} else {
|
||||||
|
bitcoin::Address::from_str(BITCOIN_MAINNET_ADDRESS).unwrap()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let monero_receive_address = {
|
||||||
|
if is_testnet {
|
||||||
|
monero::Address::from_str(MONERO_STAGENET_ADDRESS).unwrap()
|
||||||
|
} else {
|
||||||
|
monero::Address::from_str(MONERO_MAINNET_ADDRESS).unwrap()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Request::new(Method::BuyXmr {
|
||||||
|
seller,
|
||||||
|
bitcoin_change_address: Some(bitcoin_change_address),
|
||||||
|
monero_receive_address,
|
||||||
|
swap_id: Uuid::new_v4(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resume() -> Request {
|
||||||
|
Request::new(Method::Resume {
|
||||||
|
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cancel() -> Request {
|
||||||
|
Request::new(Method::CancelAndRefund {
|
||||||
|
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refund() -> Request {
|
||||||
|
Request::new(Method::CancelAndRefund {
|
||||||
|
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1069
swap/src/api/request.rs
Normal file
1069
swap/src/api/request.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,6 @@ mod event_loop;
|
|||||||
mod network;
|
mod network;
|
||||||
mod rate;
|
mod rate;
|
||||||
mod recovery;
|
mod recovery;
|
||||||
pub mod tracing;
|
|
||||||
|
|
||||||
pub use event_loop::{EventLoop, EventLoopHandle, FixedRate, KrakenRate, LatestRate};
|
pub use event_loop::{EventLoop, EventLoopHandle, FixedRate, KrakenRate, LatestRate};
|
||||||
pub use network::behaviour::{Behaviour, OutEvent};
|
pub use network::behaviour::{Behaviour, OutEvent};
|
||||||
|
@ -19,7 +19,6 @@ where
|
|||||||
let args = RawArguments::from_clap(&matches);
|
let args = RawArguments::from_clap(&matches);
|
||||||
|
|
||||||
let json = args.json;
|
let json = args.json;
|
||||||
let disable_timestamp = args.disable_timestamp;
|
|
||||||
let testnet = args.testnet;
|
let testnet = args.testnet;
|
||||||
let config = args.config;
|
let config = args.config;
|
||||||
let command: RawCommand = args.cmd;
|
let command: RawCommand = args.cmd;
|
||||||
@ -28,23 +27,35 @@ where
|
|||||||
RawCommand::Start { resume_only } => Arguments {
|
RawCommand::Start { resume_only } => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Start { resume_only },
|
cmd: Command::Start { resume_only },
|
||||||
},
|
},
|
||||||
RawCommand::History => Arguments {
|
RawCommand::History { only_unfinished } => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::History,
|
cmd: Command::History { only_unfinished },
|
||||||
|
},
|
||||||
|
RawCommand::Logs {
|
||||||
|
logs_dir: dir_path,
|
||||||
|
swap_id,
|
||||||
|
redact,
|
||||||
|
} => Arguments {
|
||||||
|
testnet,
|
||||||
|
json,
|
||||||
|
config_path: config_path(config, testnet)?,
|
||||||
|
env_config: env_config(testnet),
|
||||||
|
cmd: Command::Logs {
|
||||||
|
logs_dir: dir_path,
|
||||||
|
swap_id,
|
||||||
|
redact,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
RawCommand::WithdrawBtc { amount, address } => Arguments {
|
RawCommand::WithdrawBtc { amount, address } => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::WithdrawBtc {
|
cmd: Command::WithdrawBtc {
|
||||||
@ -55,7 +66,6 @@ where
|
|||||||
RawCommand::Balance => Arguments {
|
RawCommand::Balance => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Balance,
|
cmd: Command::Balance,
|
||||||
@ -63,7 +73,6 @@ where
|
|||||||
RawCommand::Config => Arguments {
|
RawCommand::Config => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Config,
|
cmd: Command::Config,
|
||||||
@ -71,7 +80,6 @@ where
|
|||||||
RawCommand::ExportBitcoinWallet => Arguments {
|
RawCommand::ExportBitcoinWallet => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::ExportBitcoinWallet,
|
cmd: Command::ExportBitcoinWallet,
|
||||||
@ -82,7 +90,6 @@ where
|
|||||||
}) => Arguments {
|
}) => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Redeem {
|
cmd: Command::Redeem {
|
||||||
@ -96,7 +103,6 @@ where
|
|||||||
}) => Arguments {
|
}) => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Cancel { swap_id },
|
cmd: Command::Cancel { swap_id },
|
||||||
@ -106,7 +112,6 @@ where
|
|||||||
}) => Arguments {
|
}) => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Refund { swap_id },
|
cmd: Command::Refund { swap_id },
|
||||||
@ -116,7 +121,6 @@ where
|
|||||||
}) => Arguments {
|
}) => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::Punish { swap_id },
|
cmd: Command::Punish { swap_id },
|
||||||
@ -124,7 +128,6 @@ where
|
|||||||
RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments {
|
RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path: config_path(config, testnet)?,
|
config_path: config_path(config, testnet)?,
|
||||||
env_config: env_config(testnet),
|
env_config: env_config(testnet),
|
||||||
cmd: Command::SafelyAbort { swap_id },
|
cmd: Command::SafelyAbort { swap_id },
|
||||||
@ -184,7 +187,6 @@ pub struct BitcoinAddressNetworkMismatch {
|
|||||||
pub struct Arguments {
|
pub struct Arguments {
|
||||||
pub testnet: bool,
|
pub testnet: bool,
|
||||||
pub json: bool,
|
pub json: bool,
|
||||||
pub disable_timestamp: bool,
|
|
||||||
pub config_path: PathBuf,
|
pub config_path: PathBuf,
|
||||||
pub env_config: env::Config,
|
pub env_config: env::Config,
|
||||||
pub cmd: Command,
|
pub cmd: Command,
|
||||||
@ -195,8 +197,15 @@ pub enum Command {
|
|||||||
Start {
|
Start {
|
||||||
resume_only: bool,
|
resume_only: bool,
|
||||||
},
|
},
|
||||||
History,
|
History {
|
||||||
|
only_unfinished: bool,
|
||||||
|
},
|
||||||
Config,
|
Config,
|
||||||
|
Logs {
|
||||||
|
logs_dir: Option<PathBuf>,
|
||||||
|
swap_id: Option<Uuid>,
|
||||||
|
redact: bool,
|
||||||
|
},
|
||||||
WithdrawBtc {
|
WithdrawBtc {
|
||||||
amount: Option<Amount>,
|
amount: Option<Amount>,
|
||||||
address: Address,
|
address: Address,
|
||||||
@ -226,7 +235,7 @@ pub enum Command {
|
|||||||
name = "asb",
|
name = "asb",
|
||||||
about = "Automated Swap Backend for swapping XMR for BTC",
|
about = "Automated Swap Backend for swapping XMR for BTC",
|
||||||
author,
|
author,
|
||||||
version = env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT")
|
version = env!("VERGEN_GIT_DESCRIBE")
|
||||||
)]
|
)]
|
||||||
pub struct RawArguments {
|
pub struct RawArguments {
|
||||||
#[structopt(long, help = "Swap on testnet")]
|
#[structopt(long, help = "Swap on testnet")]
|
||||||
@ -268,8 +277,33 @@ pub enum RawCommand {
|
|||||||
)]
|
)]
|
||||||
resume_only: bool,
|
resume_only: bool,
|
||||||
},
|
},
|
||||||
|
#[structopt(about = "Prints all logging messages issued in the past.")]
|
||||||
|
Logs {
|
||||||
|
#[structopt(
|
||||||
|
short = "d",
|
||||||
|
help = "Print the logs from this directory instead of the default one."
|
||||||
|
)]
|
||||||
|
logs_dir: Option<PathBuf>,
|
||||||
|
#[structopt(
|
||||||
|
help = "Redact swap-ids, Bitcoin and Monero addresses.",
|
||||||
|
long = "redact"
|
||||||
|
)]
|
||||||
|
redact: bool,
|
||||||
|
#[structopt(
|
||||||
|
long = "swap-id",
|
||||||
|
help = "Filter for logs concerning this swap.",
|
||||||
|
long_help = "This checks whether each logging message contains the swap id. Some messages might be skipped when they don't contain the swap id even though they're relevant."
|
||||||
|
)]
|
||||||
|
swap_id: Option<Uuid>,
|
||||||
|
},
|
||||||
#[structopt(about = "Prints swap-id and the state of each swap ever made.")]
|
#[structopt(about = "Prints swap-id and the state of each swap ever made.")]
|
||||||
History,
|
History {
|
||||||
|
#[structopt(
|
||||||
|
long = "only-unfinished",
|
||||||
|
help = "If set, only unfinished swaps will be printed."
|
||||||
|
)]
|
||||||
|
only_unfinished: bool,
|
||||||
|
},
|
||||||
#[structopt(about = "Prints the current config")]
|
#[structopt(about = "Prints the current config")]
|
||||||
Config,
|
Config,
|
||||||
#[structopt(about = "Allows withdrawing BTC from the internal Bitcoin wallet.")]
|
#[structopt(about = "Allows withdrawing BTC from the internal Bitcoin wallet.")]
|
||||||
@ -366,7 +400,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::Start { resume_only: false },
|
cmd: Command::Start { resume_only: false },
|
||||||
@ -384,10 +417,11 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::History,
|
cmd: Command::History {
|
||||||
|
only_unfinished: false,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
let args = parse_args(raw_ars).unwrap();
|
let args = parse_args(raw_ars).unwrap();
|
||||||
assert_eq!(expected_args, args);
|
assert_eq!(expected_args, args);
|
||||||
@ -402,7 +436,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::Balance,
|
cmd: Command::Balance,
|
||||||
@ -424,7 +457,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::WithdrawBtc {
|
cmd: Command::WithdrawBtc {
|
||||||
@ -451,7 +483,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::Cancel {
|
cmd: Command::Cancel {
|
||||||
@ -477,7 +508,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::Refund {
|
cmd: Command::Refund {
|
||||||
@ -503,7 +533,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::Punish {
|
cmd: Command::Punish {
|
||||||
@ -529,7 +558,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::SafelyAbort {
|
cmd: Command::SafelyAbort {
|
||||||
@ -549,7 +577,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::Start { resume_only: false },
|
cmd: Command::Start { resume_only: false },
|
||||||
@ -567,10 +594,11 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::History,
|
cmd: Command::History {
|
||||||
|
only_unfinished: false,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
let args = parse_args(raw_ars).unwrap();
|
let args = parse_args(raw_ars).unwrap();
|
||||||
assert_eq!(expected_args, args);
|
assert_eq!(expected_args, args);
|
||||||
@ -585,7 +613,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::Balance,
|
cmd: Command::Balance,
|
||||||
@ -609,7 +636,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::WithdrawBtc {
|
cmd: Command::WithdrawBtc {
|
||||||
@ -636,7 +662,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::Cancel {
|
cmd: Command::Cancel {
|
||||||
@ -663,7 +688,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::Refund {
|
cmd: Command::Refund {
|
||||||
@ -690,7 +714,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::Punish {
|
cmd: Command::Punish {
|
||||||
@ -717,7 +740,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: true,
|
testnet: true,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: false,
|
|
||||||
config_path: default_testnet_conf_path,
|
config_path: default_testnet_conf_path,
|
||||||
env_config: testnet_env_config,
|
env_config: testnet_env_config,
|
||||||
cmd: Command::SafelyAbort {
|
cmd: Command::SafelyAbort {
|
||||||
@ -737,7 +759,6 @@ mod tests {
|
|||||||
let expected_args = Arguments {
|
let expected_args = Arguments {
|
||||||
testnet: false,
|
testnet: false,
|
||||||
json: false,
|
json: false,
|
||||||
disable_timestamp: true,
|
|
||||||
config_path: default_mainnet_conf_path,
|
config_path: default_mainnet_conf_path,
|
||||||
env_config: mainnet_env_config,
|
env_config: mainnet_env_config,
|
||||||
cmd: Command::Start { resume_only: false },
|
cmd: Command::Start { resume_only: false },
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use crate::asb::{Behaviour, OutEvent, Rate};
|
use crate::asb::{Behaviour, OutEvent, Rate};
|
||||||
use crate::monero::Amount;
|
use crate::monero::Amount;
|
||||||
|
use crate::network::cooperative_xmr_redeem_after_punish::CooperativeXmrRedeemRejectReason;
|
||||||
|
use crate::network::cooperative_xmr_redeem_after_punish::Response::{Fullfilled, Rejected};
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::network::swap_setup::alice::WalletSnapshot;
|
use crate::network::swap_setup::alice::WalletSnapshot;
|
||||||
use crate::network::transfer_proof;
|
use crate::network::transfer_proof;
|
||||||
@ -253,6 +255,59 @@ where
|
|||||||
channel
|
channel
|
||||||
}.boxed());
|
}.boxed());
|
||||||
}
|
}
|
||||||
|
SwarmEvent::Behaviour(OutEvent::CooperativeXmrRedeemRequested { swap_id, channel, peer }) => {
|
||||||
|
let swap_peer = self.db.get_peer_id(swap_id).await;
|
||||||
|
let swap_state = self.db.get_state(swap_id).await;
|
||||||
|
|
||||||
|
let (swap_peer, swap_state) = match (swap_peer, swap_state) {
|
||||||
|
(Ok(peer), Ok(state)) => (peer, state),
|
||||||
|
_ => {
|
||||||
|
tracing::warn!(
|
||||||
|
swap_id = %swap_id,
|
||||||
|
received_from = %peer,
|
||||||
|
reason = "swap not found",
|
||||||
|
"Rejecting cooperative XMR redeem request"
|
||||||
|
);
|
||||||
|
if self.swarm.behaviour_mut().cooperative_xmr_redeem.send_response(channel, Rejected { swap_id, reason: CooperativeXmrRedeemRejectReason::UnknownSwap }).is_err() {
|
||||||
|
tracing::error!(swap_id = %swap_id, "Failed to reject cooperative XMR redeem request");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if swap_peer != peer {
|
||||||
|
tracing::warn!(
|
||||||
|
swap_id = %swap_id,
|
||||||
|
received_from = %peer,
|
||||||
|
expected_from = %swap_peer,
|
||||||
|
reason = "unexpected peer",
|
||||||
|
"Rejecting cooperative XMR redeem request"
|
||||||
|
);
|
||||||
|
if self.swarm.behaviour_mut().cooperative_xmr_redeem.send_response(channel, Rejected { swap_id, reason: CooperativeXmrRedeemRejectReason::MaliciousRequest }).is_err() {
|
||||||
|
tracing::error!(swap_id = %swap_id, "Failed to reject cooperative XMR redeem request");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let State::Alice (AliceState::BtcPunished { state3 }) = swap_state else {
|
||||||
|
tracing::warn!(
|
||||||
|
swap_id = %swap_id,
|
||||||
|
reason = "swap is in invalid state",
|
||||||
|
"Rejecting cooperative XMR redeem request"
|
||||||
|
);
|
||||||
|
if self.swarm.behaviour_mut().cooperative_xmr_redeem.send_response(channel, Rejected { swap_id, reason: CooperativeXmrRedeemRejectReason::SwapInvalidState }).is_err() {
|
||||||
|
tracing::error!(swap_id = %swap_id, "Failed to reject cooperative XMR redeem request");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.swarm.behaviour_mut().cooperative_xmr_redeem.send_response(channel, Fullfilled { swap_id, s_a: state3.s_a }).is_err() {
|
||||||
|
tracing::error!(peer = %peer, "Failed to respond to cooperative XMR redeem request");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tracing::info!(swap_id = %swap_id, peer = %peer, "Fullfilled cooperative XMR redeem request");
|
||||||
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::Rendezvous(libp2p::rendezvous::client::Event::Registered { rendezvous_node, ttl, namespace })) => {
|
SwarmEvent::Behaviour(OutEvent::Rendezvous(libp2p::rendezvous::client::Event::Registered { rendezvous_node, ttl, namespace })) => {
|
||||||
tracing::info!("Successfully registered with rendezvous node: {} with namespace: {} and TTL: {:?}", rendezvous_node, namespace, ttl);
|
tracing::info!("Successfully registered with rendezvous node: {} with namespace: {} and TTL: {:?}", rendezvous_node, namespace, ttl);
|
||||||
}
|
}
|
||||||
@ -279,10 +334,10 @@ where
|
|||||||
SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => {
|
SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => {
|
||||||
tracing::warn!(%address, "Failed to set up connection with peer: {:#}", error);
|
tracing::warn!(%address, "Failed to set up connection with peer: {:#}", error);
|
||||||
}
|
}
|
||||||
SwarmEvent::ConnectionClosed { peer_id: peer, num_established, endpoint, cause: Some(error) } if num_established == 0 => {
|
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: Some(error) } => {
|
||||||
tracing::debug!(%peer, address = %endpoint.get_remote_address(), "Lost connection to peer: {:#}", error);
|
tracing::debug!(%peer, address = %endpoint.get_remote_address(), "Lost connection to peer: {:#}", error);
|
||||||
}
|
}
|
||||||
SwarmEvent::ConnectionClosed { peer_id: peer, num_established, endpoint, cause: None } if num_established == 0 => {
|
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: None } => {
|
||||||
tracing::info!(%peer, address = %endpoint.get_remote_address(), "Successfully closed connection");
|
tracing::info!(%peer, address = %endpoint.get_remote_address(), "Successfully closed connection");
|
||||||
}
|
}
|
||||||
SwarmEvent::NewListenAddr{address, ..} => {
|
SwarmEvent::NewListenAddr{address, ..} => {
|
||||||
@ -296,7 +351,7 @@ where
|
|||||||
Some(Ok((peer, transfer_proof, responder))) => {
|
Some(Ok((peer, transfer_proof, responder))) => {
|
||||||
if !self.swarm.behaviour_mut().transfer_proof.is_connected(&peer) {
|
if !self.swarm.behaviour_mut().transfer_proof.is_connected(&peer) {
|
||||||
tracing::warn!(%peer, "No active connection to peer, buffering transfer proof");
|
tracing::warn!(%peer, "No active connection to peer, buffering transfer proof");
|
||||||
self.buffered_transfer_proofs.entry(peer).or_insert_with(Vec::new).push((transfer_proof, responder));
|
self.buffered_transfer_proofs.entry(peer).or_default().push((transfer_proof, responder));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,9 @@ use crate::network::rendezvous::XmrBtcNamespace;
|
|||||||
use crate::network::swap_setup::alice;
|
use crate::network::swap_setup::alice;
|
||||||
use crate::network::swap_setup::alice::WalletSnapshot;
|
use crate::network::swap_setup::alice::WalletSnapshot;
|
||||||
use crate::network::transport::authenticate_and_multiplex;
|
use crate::network::transport::authenticate_and_multiplex;
|
||||||
use crate::network::{encrypted_signature, quote, transfer_proof};
|
use crate::network::{
|
||||||
|
cooperative_xmr_redeem_after_punish, encrypted_signature, quote, transfer_proof,
|
||||||
|
};
|
||||||
use crate::protocol::alice::State3;
|
use crate::protocol::alice::State3;
|
||||||
use anyhow::{anyhow, Error, Result};
|
use anyhow::{anyhow, Error, Result};
|
||||||
use futures::FutureExt;
|
use futures::FutureExt;
|
||||||
@ -76,6 +78,11 @@ pub mod behaviour {
|
|||||||
channel: ResponseChannel<()>,
|
channel: ResponseChannel<()>,
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
},
|
},
|
||||||
|
CooperativeXmrRedeemRequested {
|
||||||
|
channel: ResponseChannel<cooperative_xmr_redeem_after_punish::Response>,
|
||||||
|
swap_id: Uuid,
|
||||||
|
peer: PeerId,
|
||||||
|
},
|
||||||
Rendezvous(libp2p::rendezvous::client::Event),
|
Rendezvous(libp2p::rendezvous::client::Event),
|
||||||
Failure {
|
Failure {
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
@ -114,6 +121,7 @@ pub mod behaviour {
|
|||||||
pub quote: quote::Behaviour,
|
pub quote: quote::Behaviour,
|
||||||
pub swap_setup: alice::Behaviour<LR>,
|
pub swap_setup: alice::Behaviour<LR>,
|
||||||
pub transfer_proof: transfer_proof::Behaviour,
|
pub transfer_proof: transfer_proof::Behaviour,
|
||||||
|
pub cooperative_xmr_redeem: cooperative_xmr_redeem_after_punish::Behaviour,
|
||||||
pub encrypted_signature: encrypted_signature::Behaviour,
|
pub encrypted_signature: encrypted_signature::Behaviour,
|
||||||
pub identify: Identify,
|
pub identify: Identify,
|
||||||
|
|
||||||
@ -160,6 +168,7 @@ pub mod behaviour {
|
|||||||
),
|
),
|
||||||
transfer_proof: transfer_proof::alice(),
|
transfer_proof: transfer_proof::alice(),
|
||||||
encrypted_signature: encrypted_signature::alice(),
|
encrypted_signature: encrypted_signature::alice(),
|
||||||
|
cooperative_xmr_redeem: cooperative_xmr_redeem_after_punish::alice(),
|
||||||
ping: Ping::new(PingConfig::new().with_keep_alive(true)),
|
ping: Ping::new(PingConfig::new().with_keep_alive(true)),
|
||||||
identify: Identify::new(identifyConfig),
|
identify: Identify::new(identifyConfig),
|
||||||
}
|
}
|
||||||
@ -302,7 +311,7 @@ pub mod rendezvous {
|
|||||||
|
|
||||||
fn inject_disconnected(&mut self, peer_id: &PeerId) {
|
fn inject_disconnected(&mut self, peer_id: &PeerId) {
|
||||||
for i in 0..self.rendezvous_nodes.len() {
|
for i in 0..self.rendezvous_nodes.len() {
|
||||||
let mut node = &mut self.rendezvous_nodes[i];
|
let node = &mut self.rendezvous_nodes[i];
|
||||||
if peer_id == &node.peer_id {
|
if peer_id == &node.peer_id {
|
||||||
node.connection_status = ConnectionStatus::Disconnected;
|
node.connection_status = ConnectionStatus::Disconnected;
|
||||||
}
|
}
|
||||||
@ -325,7 +334,7 @@ pub mod rendezvous {
|
|||||||
_error: &DialError,
|
_error: &DialError,
|
||||||
) {
|
) {
|
||||||
for i in 0..self.rendezvous_nodes.len() {
|
for i in 0..self.rendezvous_nodes.len() {
|
||||||
let mut node = &mut self.rendezvous_nodes[i];
|
let node = &mut self.rendezvous_nodes[i];
|
||||||
if let Some(id) = peer_id {
|
if let Some(id) = peer_id {
|
||||||
if id == node.peer_id {
|
if id == node.peer_id {
|
||||||
node.connection_status = ConnectionStatus::Disconnected;
|
node.connection_status = ConnectionStatus::Disconnected;
|
||||||
|
@ -38,8 +38,8 @@ pub async fn cancel(
|
|||||||
// Alice already in final state
|
// Alice already in final state
|
||||||
| AliceState::BtcRedeemed
|
| AliceState::BtcRedeemed
|
||||||
| AliceState::XmrRefunded
|
| AliceState::XmrRefunded
|
||||||
| AliceState::BtcPunished
|
| AliceState::BtcPunished { .. }
|
||||||
| AliceState::SafelyAborted => bail!("Swap is is in state {} which is not cancelable", state),
|
| AliceState::SafelyAborted => bail!("Swap is in state {} which is not cancelable", state),
|
||||||
};
|
};
|
||||||
|
|
||||||
let txid = match state3.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
let txid = match state3.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
||||||
|
@ -38,7 +38,7 @@ pub async fn punish(
|
|||||||
// Alice already in final state
|
// Alice already in final state
|
||||||
| AliceState::BtcRedeemed
|
| AliceState::BtcRedeemed
|
||||||
| AliceState::XmrRefunded
|
| AliceState::XmrRefunded
|
||||||
| AliceState::BtcPunished
|
| AliceState::BtcPunished { .. }
|
||||||
| AliceState::SafelyAborted => bail!(Error::SwapNotPunishable(state)),
|
| AliceState::SafelyAborted => bail!(Error::SwapNotPunishable(state)),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -46,7 +46,9 @@ pub async fn punish(
|
|||||||
|
|
||||||
let txid = state3.punish_btc(&bitcoin_wallet).await?;
|
let txid = state3.punish_btc(&bitcoin_wallet).await?;
|
||||||
|
|
||||||
let state = AliceState::BtcPunished;
|
let state = AliceState::BtcPunished {
|
||||||
|
state3: state3.clone(),
|
||||||
|
};
|
||||||
db.insert_latest_state(swap_id, state.clone().into())
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ pub async fn redeem(
|
|||||||
| AliceState::BtcPunishable { .. }
|
| AliceState::BtcPunishable { .. }
|
||||||
| AliceState::BtcRedeemed
|
| AliceState::BtcRedeemed
|
||||||
| AliceState::XmrRefunded
|
| AliceState::XmrRefunded
|
||||||
| AliceState::BtcPunished
|
| AliceState::BtcPunished { .. }
|
||||||
| AliceState::SafelyAborted => bail!(
|
| AliceState::SafelyAborted => bail!(
|
||||||
"Cannot redeem swap {} because it is in state {} which cannot be manually redeemed",
|
"Cannot redeem swap {} because it is in state {} which cannot be manually redeemed",
|
||||||
swap_id,
|
swap_id,
|
||||||
|
@ -55,7 +55,7 @@ pub async fn refund(
|
|||||||
AliceState::BtcRedeemTransactionPublished { .. }
|
AliceState::BtcRedeemTransactionPublished { .. }
|
||||||
| AliceState::BtcRedeemed
|
| AliceState::BtcRedeemed
|
||||||
| AliceState::XmrRefunded
|
| AliceState::XmrRefunded
|
||||||
| AliceState::BtcPunished
|
| AliceState::BtcPunished { .. }
|
||||||
| AliceState::SafelyAborted => bail!(Error::SwapNotRefundable(state)),
|
| AliceState::SafelyAborted => bail!(Error::SwapNotRefundable(state)),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ pub async fn safely_abort(swap_id: Uuid, db: Arc<dyn Database>) -> Result<AliceS
|
|||||||
| AliceState::BtcPunishable { .. }
|
| AliceState::BtcPunishable { .. }
|
||||||
| AliceState::BtcRedeemed
|
| AliceState::BtcRedeemed
|
||||||
| AliceState::XmrRefunded
|
| AliceState::XmrRefunded
|
||||||
| AliceState::BtcPunished
|
| AliceState::BtcPunished { .. }
|
||||||
| AliceState::SafelyAborted => bail!(
|
| AliceState::SafelyAborted => bail!(
|
||||||
"Cannot safely abort swap {} because it is in state {} which cannot be safely aborted",
|
"Cannot safely abort swap {} because it is in state {} which cannot be safely aborted",
|
||||||
swap_id,
|
swap_id,
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
use anyhow::Result;
|
|
||||||
use tracing_subscriber::filter::LevelFilter;
|
|
||||||
use tracing_subscriber::fmt::time::UtcTime;
|
|
||||||
use tracing_subscriber::FmtSubscriber;
|
|
||||||
|
|
||||||
pub fn init(level: LevelFilter, json_format: bool, timestamp: bool) -> Result<()> {
|
|
||||||
if level == LevelFilter::OFF {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
|
||||||
|
|
||||||
let builder = FmtSubscriber::builder()
|
|
||||||
.with_env_filter(format!("asb={},swap={}", level, level))
|
|
||||||
.with_writer(std::io::stderr)
|
|
||||||
.with_ansi(is_terminal)
|
|
||||||
.with_timer(UtcTime::rfc_3339())
|
|
||||||
.with_target(false);
|
|
||||||
|
|
||||||
match (json_format, timestamp) {
|
|
||||||
(true, true) => builder.json().init(),
|
|
||||||
(true, false) => builder.json().without_time().init(),
|
|
||||||
(false, true) => builder.init(),
|
|
||||||
(false, false) => builder.without_time().init(),
|
|
||||||
}
|
|
||||||
|
|
||||||
tracing::info!(%level, "Initialized tracing");
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -18,6 +18,8 @@ use libp2p::core::multiaddr::Protocol;
|
|||||||
use libp2p::core::Multiaddr;
|
use libp2p::core::Multiaddr;
|
||||||
use libp2p::swarm::AddressScore;
|
use libp2p::swarm::AddressScore;
|
||||||
use libp2p::Swarm;
|
use libp2p::Swarm;
|
||||||
|
use rust_decimal::prelude::FromPrimitive;
|
||||||
|
use rust_decimal::Decimal;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||||
@ -29,51 +31,50 @@ use swap::asb::config::{
|
|||||||
initial_setup, query_user_for_initial_config, read_config, Config, ConfigNotInitialized,
|
initial_setup, query_user_for_initial_config, read_config, Config, ConfigNotInitialized,
|
||||||
};
|
};
|
||||||
use swap::asb::{cancel, punish, redeem, refund, safely_abort, EventLoop, Finality, KrakenRate};
|
use swap::asb::{cancel, punish, redeem, refund, safely_abort, EventLoop, Finality, KrakenRate};
|
||||||
use swap::common::check_latest_version;
|
use swap::common::tracing_util::Format;
|
||||||
use swap::database::open_db;
|
use swap::common::{self, check_latest_version, get_logs};
|
||||||
|
use swap::database::{open_db, AccessMode};
|
||||||
use swap::network::rendezvous::XmrBtcNamespace;
|
use swap::network::rendezvous::XmrBtcNamespace;
|
||||||
use swap::network::swarm;
|
use swap::network::swarm;
|
||||||
|
use swap::protocol::alice::swap::is_complete;
|
||||||
use swap::protocol::alice::{run, AliceState};
|
use swap::protocol::alice::{run, AliceState};
|
||||||
|
use swap::protocol::State;
|
||||||
use swap::seed::Seed;
|
use swap::seed::Seed;
|
||||||
use swap::tor::AuthenticatedClient;
|
use swap::tor::AuthenticatedClient;
|
||||||
use swap::{asb, bitcoin, kraken, monero, tor};
|
use swap::{bitcoin, kraken, monero, tor};
|
||||||
use tracing_subscriber::filter::LevelFilter;
|
use tracing_subscriber::filter::LevelFilter;
|
||||||
|
|
||||||
const DEFAULT_WALLET_NAME: &str = "asb-wallet";
|
const DEFAULT_WALLET_NAME: &str = "asb-wallet";
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
|
// parse cli arguments
|
||||||
let Arguments {
|
let Arguments {
|
||||||
testnet,
|
testnet,
|
||||||
json,
|
json,
|
||||||
disable_timestamp,
|
|
||||||
config_path,
|
config_path,
|
||||||
env_config,
|
env_config,
|
||||||
cmd,
|
cmd,
|
||||||
} = match parse_args(env::args_os()) {
|
} = match parse_args(env::args_os()) {
|
||||||
Ok(args) => args,
|
Ok(args) => args,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
// make sure to display the clap error message it exists
|
||||||
if let Some(clap_err) = e.downcast_ref::<clap::Error>() {
|
if let Some(clap_err) = e.downcast_ref::<clap::Error>() {
|
||||||
match clap_err.kind {
|
if let ErrorKind::HelpDisplayed | ErrorKind::VersionDisplayed = clap_err.kind {
|
||||||
ErrorKind::HelpDisplayed | ErrorKind::VersionDisplayed => {
|
println!("{}", clap_err.message);
|
||||||
println!("{}", clap_err.message);
|
std::process::exit(0);
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
bail!(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bail!(e);
|
bail!(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// warn if we're not on the latest version
|
||||||
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
|
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
asb::tracing::init(LevelFilter::DEBUG, json, !disable_timestamp).expect("initialize tracing");
|
// read config from the specified path
|
||||||
|
|
||||||
let config = match read_config(config_path.clone())? {
|
let config = match read_config(config_path.clone())? {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(ConfigNotInitialized {}) => {
|
Err(ConfigNotInitialized {}) => {
|
||||||
@ -82,6 +83,12 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// initialize tracing
|
||||||
|
let format = if json { Format::Json } else { Format::Raw };
|
||||||
|
let log_dir = config.data.dir.join("logs");
|
||||||
|
common::tracing_util::init(LevelFilter::DEBUG, format, log_dir).expect("initialize tracing");
|
||||||
|
|
||||||
|
// check for conflicting env / config values
|
||||||
if config.monero.network != env_config.monero_network {
|
if config.monero.network != env_config.monero_network {
|
||||||
bail!(format!(
|
bail!(format!(
|
||||||
"Expected monero network in config file to be {:?} but was {:?}",
|
"Expected monero network in config file to be {:?} but was {:?}",
|
||||||
@ -95,19 +102,20 @@ async fn main() -> Result<()> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let db = open_db(config.data.dir.join("sqlite")).await?;
|
|
||||||
|
|
||||||
let seed =
|
let seed =
|
||||||
Seed::from_file_or_generate(&config.data.dir).expect("Could not retrieve/initialize seed");
|
Seed::from_file_or_generate(&config.data.dir).expect("Could not retrieve/initialize seed");
|
||||||
|
|
||||||
match cmd {
|
match cmd {
|
||||||
Command::Start { resume_only } => {
|
Command::Start { resume_only } => {
|
||||||
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
// check and warn for duplicate rendezvous points
|
// check and warn for duplicate rendezvous points
|
||||||
let mut rendezvous_addrs = config.network.rendezvous_point.clone();
|
let mut rendezvous_addrs = config.network.rendezvous_point.clone();
|
||||||
let prev_len = rendezvous_addrs.len();
|
let prev_len = rendezvous_addrs.len();
|
||||||
rendezvous_addrs.sort();
|
rendezvous_addrs.sort();
|
||||||
rendezvous_addrs.dedup();
|
rendezvous_addrs.dedup();
|
||||||
let new_len = rendezvous_addrs.len();
|
let new_len = rendezvous_addrs.len();
|
||||||
|
|
||||||
if new_len < prev_len {
|
if new_len < prev_len {
|
||||||
tracing::warn!(
|
tracing::warn!(
|
||||||
"`rendezvous_point` config has {} duplicate entries, they are being ignored.",
|
"`rendezvous_point` config has {} duplicate entries, they are being ignored.",
|
||||||
@ -115,9 +123,12 @@ async fn main() -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initialize monero wallet
|
||||||
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
||||||
let monero_address = monero_wallet.get_main_address();
|
let monero_address = monero_wallet.get_main_address();
|
||||||
tracing::info!(%monero_address, "Monero wallet address");
|
tracing::info!(%monero_address, "Monero wallet address");
|
||||||
|
|
||||||
|
// check monero balance
|
||||||
let monero = monero_wallet.get_balance().await?;
|
let monero = monero_wallet.get_balance().await?;
|
||||||
match (monero.balance, monero.unlocked_balance) {
|
match (monero.balance, monero.unlocked_balance) {
|
||||||
(0, _) => {
|
(0, _) => {
|
||||||
@ -140,6 +151,7 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// init bitcoin wallet
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
||||||
tracing::info!(%bitcoin_balance, "Bitcoin wallet balance");
|
tracing::info!(%bitcoin_balance, "Bitcoin wallet balance");
|
||||||
@ -224,22 +236,105 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
event_loop.run().await;
|
event_loop.run().await;
|
||||||
}
|
}
|
||||||
Command::History => {
|
Command::History { only_unfinished } => {
|
||||||
let mut table = Table::new();
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadOnly).await?;
|
||||||
|
let mut table: Table = Table::new();
|
||||||
|
|
||||||
table.set_header(vec!["SWAP ID", "STATE"]);
|
table.set_header(vec![
|
||||||
|
"Swap ID",
|
||||||
|
"Start Date",
|
||||||
|
"State",
|
||||||
|
"BTC Amount",
|
||||||
|
"XMR Amount",
|
||||||
|
"Exchange Rate",
|
||||||
|
"Trading Partner Peer ID",
|
||||||
|
"Completed",
|
||||||
|
]);
|
||||||
|
|
||||||
for (swap_id, state) in db.all().await? {
|
let all_swaps = db.all().await?;
|
||||||
let state: AliceState = state.try_into()?;
|
for (swap_id, state) in all_swaps {
|
||||||
table.add_row(vec![swap_id.to_string(), state.to_string()]);
|
if let Err(e) = async {
|
||||||
|
let latest_state: AliceState = state.try_into()?;
|
||||||
|
let is_completed = is_complete(&latest_state);
|
||||||
|
|
||||||
|
if only_unfinished && is_completed {
|
||||||
|
return Ok::<_, anyhow::Error>(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let all_states = db.get_states(swap_id).await?;
|
||||||
|
let state3 = all_states
|
||||||
|
.iter()
|
||||||
|
.find_map(|s| match s {
|
||||||
|
State::Alice(AliceState::BtcLockTransactionSeen { state3 }) => {
|
||||||
|
Some(state3)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.context("Failed to get \"BtcLockTransactionSeen\" state")?;
|
||||||
|
|
||||||
|
let swap_start_date = db.get_swap_start_date(swap_id).await?;
|
||||||
|
let peer_id = db.get_peer_id(swap_id).await?;
|
||||||
|
|
||||||
|
let exchange_rate = Decimal::from_f64(state3.btc.to_btc())
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Failed to convert BTC amount to Decimal"))?
|
||||||
|
.checked_div(state3.xmr.as_xmr())
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Division by zero or overflow"))?;
|
||||||
|
let exchange_rate = format!("{} XMR/BTC", exchange_rate.round_dp(8));
|
||||||
|
|
||||||
|
if json {
|
||||||
|
tracing::info!(
|
||||||
|
swap_id = %swap_id,
|
||||||
|
swap_start_date = %swap_start_date,
|
||||||
|
latest_state = %latest_state,
|
||||||
|
btc_amount = %state3.btc,
|
||||||
|
xmr_amount = %state3.xmr,
|
||||||
|
exchange_rate = %exchange_rate,
|
||||||
|
trading_partner_peer_id = %peer_id,
|
||||||
|
completed = is_completed,
|
||||||
|
"Found swap in database"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
table.add_row(vec![
|
||||||
|
swap_id.to_string(),
|
||||||
|
swap_start_date.to_string(),
|
||||||
|
latest_state.to_string(),
|
||||||
|
state3.btc.to_string(),
|
||||||
|
state3.xmr.to_string(),
|
||||||
|
exchange_rate,
|
||||||
|
peer_id.to_string(),
|
||||||
|
is_completed.to_string(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok::<_, anyhow::Error>(())
|
||||||
|
}
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
tracing::error!(swap_id = %swap_id, error = %e, "Failed to get swap details");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{}", table);
|
if !json {
|
||||||
|
println!("{}", table);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Command::Config => {
|
Command::Config => {
|
||||||
let config_json = serde_json::to_string_pretty(&config)?;
|
let config_json = serde_json::to_string_pretty(&config)?;
|
||||||
println!("{}", config_json);
|
println!("{}", config_json);
|
||||||
}
|
}
|
||||||
|
Command::Logs {
|
||||||
|
logs_dir,
|
||||||
|
swap_id,
|
||||||
|
redact,
|
||||||
|
} => {
|
||||||
|
let dir = logs_dir.unwrap_or(config.data.dir.join("logs"));
|
||||||
|
|
||||||
|
let log_messages = get_logs(dir, swap_id, redact).await?;
|
||||||
|
|
||||||
|
for msg in log_messages {
|
||||||
|
println!("{msg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
Command::WithdrawBtc { amount, address } => {
|
Command::WithdrawBtc { amount, address } => {
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
|
|
||||||
@ -270,6 +365,8 @@ async fn main() -> Result<()> {
|
|||||||
tracing::info!(%bitcoin_balance, %monero_balance, "Current balance");
|
tracing::info!(%bitcoin_balance, %monero_balance, "Current balance");
|
||||||
}
|
}
|
||||||
Command::Cancel { swap_id } => {
|
Command::Cancel { swap_id } => {
|
||||||
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
|
|
||||||
let (txid, _) = cancel(swap_id, Arc::new(bitcoin_wallet), db).await?;
|
let (txid, _) = cancel(swap_id, Arc::new(bitcoin_wallet), db).await?;
|
||||||
@ -277,6 +374,8 @@ async fn main() -> Result<()> {
|
|||||||
tracing::info!("Cancel transaction successfully published with id {}", txid);
|
tracing::info!("Cancel transaction successfully published with id {}", txid);
|
||||||
}
|
}
|
||||||
Command::Refund { swap_id } => {
|
Command::Refund { swap_id } => {
|
||||||
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
||||||
|
|
||||||
@ -291,6 +390,8 @@ async fn main() -> Result<()> {
|
|||||||
tracing::info!("Monero successfully refunded");
|
tracing::info!("Monero successfully refunded");
|
||||||
}
|
}
|
||||||
Command::Punish { swap_id } => {
|
Command::Punish { swap_id } => {
|
||||||
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
|
|
||||||
let (txid, _) = punish(swap_id, Arc::new(bitcoin_wallet), db).await?;
|
let (txid, _) = punish(swap_id, Arc::new(bitcoin_wallet), db).await?;
|
||||||
@ -298,6 +399,8 @@ async fn main() -> Result<()> {
|
|||||||
tracing::info!("Punish transaction successfully published with id {}", txid);
|
tracing::info!("Punish transaction successfully published with id {}", txid);
|
||||||
}
|
}
|
||||||
Command::SafelyAbort { swap_id } => {
|
Command::SafelyAbort { swap_id } => {
|
||||||
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
safely_abort(swap_id, db).await?;
|
safely_abort(swap_id, db).await?;
|
||||||
|
|
||||||
tracing::info!("Swap safely aborted");
|
tracing::info!("Swap safely aborted");
|
||||||
@ -306,6 +409,8 @@ async fn main() -> Result<()> {
|
|||||||
swap_id,
|
swap_id,
|
||||||
do_not_await_finality,
|
do_not_await_finality,
|
||||||
} => {
|
} => {
|
||||||
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
|
|
||||||
let (txid, _) = redeem(
|
let (txid, _) = redeem(
|
||||||
|
@ -12,43 +12,15 @@
|
|||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::Result;
|
||||||
use comfy_table::Table;
|
|
||||||
use qrcode::render::unicode;
|
|
||||||
use qrcode::QrCode;
|
|
||||||
use std::cmp::min;
|
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::future::Future;
|
use swap::cli::command::{parse_args_and_apply_defaults, ParseResult};
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
|
||||||
use swap::bitcoin::TxLock;
|
|
||||||
use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command, ParseResult};
|
|
||||||
use swap::cli::{list_sellers, EventLoop, SellerStatus};
|
|
||||||
use swap::common::check_latest_version;
|
use swap::common::check_latest_version;
|
||||||
use swap::database::open_db;
|
|
||||||
use swap::env::Config;
|
|
||||||
use swap::libp2p_ext::MultiAddrExt;
|
|
||||||
use swap::network::quote::{BidQuote, ZeroQuoteReceived};
|
|
||||||
use swap::network::swarm;
|
|
||||||
use swap::protocol::bob;
|
|
||||||
use swap::protocol::bob::{BobState, Swap};
|
|
||||||
use swap::seed::Seed;
|
|
||||||
use swap::{bitcoin, cli, monero};
|
|
||||||
use url::Url;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let Arguments {
|
let (context, request) = match parse_args_and_apply_defaults(env::args_os()).await? {
|
||||||
env_config,
|
ParseResult::Context(context, request) => (context, request),
|
||||||
data_dir,
|
|
||||||
debug,
|
|
||||||
json,
|
|
||||||
cmd,
|
|
||||||
} = match parse_args_and_apply_defaults(env::args_os())? {
|
|
||||||
ParseResult::Arguments(args) => *args,
|
|
||||||
ParseResult::PrintAndExitZero { message } => {
|
ParseResult::PrintAndExitZero { message } => {
|
||||||
println!("{}", message);
|
println!("{}", message);
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
@ -58,601 +30,19 @@ async fn main() -> Result<()> {
|
|||||||
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
|
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
}
|
}
|
||||||
|
request.call(context.clone()).await?;
|
||||||
match cmd {
|
context.tasks.wait_for_tasks().await?;
|
||||||
Command::BuyXmr {
|
|
||||||
seller,
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
bitcoin_target_block,
|
|
||||||
bitcoin_change_address,
|
|
||||||
monero_receive_address,
|
|
||||||
monero_daemon_address,
|
|
||||||
tor_socks5_port,
|
|
||||||
namespace,
|
|
||||||
} => {
|
|
||||||
let swap_id = Uuid::new_v4();
|
|
||||||
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
|
|
||||||
|
|
||||||
let db = open_db(data_dir.join("sqlite")).await?;
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
&seed,
|
|
||||||
data_dir.clone(),
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let (monero_wallet, _process) =
|
|
||||||
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
|
|
||||||
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
|
||||||
let seller_peer_id = seller
|
|
||||||
.extract_peer_id()
|
|
||||||
.context("Seller address must contain peer ID")?;
|
|
||||||
db.insert_address(seller_peer_id, seller.clone()).await?;
|
|
||||||
|
|
||||||
let behaviour = cli::Behaviour::new(
|
|
||||||
seller_peer_id,
|
|
||||||
env_config,
|
|
||||||
bitcoin_wallet.clone(),
|
|
||||||
(seed.derive_libp2p_identity(), namespace),
|
|
||||||
);
|
|
||||||
let mut swarm =
|
|
||||||
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
|
|
||||||
swarm.behaviour_mut().add_address(seller_peer_id, seller);
|
|
||||||
|
|
||||||
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
|
|
||||||
|
|
||||||
let (event_loop, mut event_loop_handle) =
|
|
||||||
EventLoop::new(swap_id, swarm, seller_peer_id)?;
|
|
||||||
let event_loop = tokio::spawn(event_loop.run());
|
|
||||||
|
|
||||||
let max_givable = || bitcoin_wallet.max_giveable(TxLock::script_size());
|
|
||||||
let estimate_fee = |amount| bitcoin_wallet.estimate_fee(TxLock::weight(), amount);
|
|
||||||
|
|
||||||
let (amount, fees) = match determine_btc_to_swap(
|
|
||||||
json,
|
|
||||||
event_loop_handle.request_quote(),
|
|
||||||
bitcoin_wallet.new_address(),
|
|
||||||
|| bitcoin_wallet.balance(),
|
|
||||||
max_givable,
|
|
||||||
|| bitcoin_wallet.sync(),
|
|
||||||
estimate_fee,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(val) => val,
|
|
||||||
Err(error) => match error.downcast::<ZeroQuoteReceived>() {
|
|
||||||
Ok(_) => {
|
|
||||||
bail!("Seller's XMR balance is currently too low to initiate a swap, please try again later")
|
|
||||||
}
|
|
||||||
Err(other) => bail!(other),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
tracing::info!(%amount, %fees, "Determined swap amount");
|
|
||||||
|
|
||||||
db.insert_peer_id(swap_id, seller_peer_id).await?;
|
|
||||||
db.insert_monero_address(swap_id, monero_receive_address)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let swap = Swap::new(
|
|
||||||
db,
|
|
||||||
swap_id,
|
|
||||||
bitcoin_wallet,
|
|
||||||
Arc::new(monero_wallet),
|
|
||||||
env_config,
|
|
||||||
event_loop_handle,
|
|
||||||
monero_receive_address,
|
|
||||||
bitcoin_change_address,
|
|
||||||
amount,
|
|
||||||
);
|
|
||||||
|
|
||||||
tokio::select! {
|
|
||||||
result = event_loop => {
|
|
||||||
result
|
|
||||||
.context("EventLoop panicked")?;
|
|
||||||
},
|
|
||||||
result = bob::run(swap) => {
|
|
||||||
result.context("Failed to complete swap")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Command::History => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
||||||
|
|
||||||
let db = open_db(data_dir.join("sqlite")).await?;
|
|
||||||
let swaps = db.all().await?;
|
|
||||||
|
|
||||||
if json {
|
|
||||||
for (swap_id, state) in swaps {
|
|
||||||
let state: BobState = state.try_into()?;
|
|
||||||
tracing::info!(swap_id=%swap_id.to_string(), state=%state.to_string(), "Read swap state from database");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let mut table = Table::new();
|
|
||||||
|
|
||||||
table.set_header(vec!["SWAP ID", "STATE"]);
|
|
||||||
|
|
||||||
for (swap_id, state) in swaps {
|
|
||||||
let state: BobState = state.try_into()?;
|
|
||||||
table.add_row(vec![swap_id.to_string(), state.to_string()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{}", table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Command::Config => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
||||||
|
|
||||||
tracing::info!(path=%data_dir.display(), "Data directory");
|
|
||||||
tracing::info!(path=%format!("{}/logs", data_dir.display()), "Log files directory");
|
|
||||||
tracing::info!(path=%format!("{}/sqlite", data_dir.display()), "Sqlite file location");
|
|
||||||
tracing::info!(path=%format!("{}/seed.pem", data_dir.display()), "Seed file location");
|
|
||||||
tracing::info!(path=%format!("{}/monero", data_dir.display()), "Monero-wallet-rpc directory");
|
|
||||||
tracing::info!(path=%format!("{}/wallet", data_dir.display()), "Internal bitcoin wallet directory");
|
|
||||||
}
|
|
||||||
Command::WithdrawBtc {
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
bitcoin_target_block,
|
|
||||||
amount,
|
|
||||||
address,
|
|
||||||
} => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
||||||
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
&seed,
|
|
||||||
data_dir.clone(),
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let amount = match amount {
|
|
||||||
Some(amount) => amount,
|
|
||||||
None => {
|
|
||||||
bitcoin_wallet
|
|
||||||
.max_giveable(address.script_pubkey().len())
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let psbt = bitcoin_wallet
|
|
||||||
.send_to_address(address, amount, None)
|
|
||||||
.await?;
|
|
||||||
let signed_tx = bitcoin_wallet.sign_and_finalize(psbt).await?;
|
|
||||||
|
|
||||||
bitcoin_wallet.broadcast(signed_tx, "withdraw").await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Command::Balance {
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
bitcoin_target_block,
|
|
||||||
} => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
||||||
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
&seed,
|
|
||||||
data_dir.clone(),
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
|
||||||
tracing::info!(
|
|
||||||
balance = %bitcoin_balance,
|
|
||||||
"Checked Bitcoin balance",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Command::Resume {
|
|
||||||
swap_id,
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
bitcoin_target_block,
|
|
||||||
monero_daemon_address,
|
|
||||||
tor_socks5_port,
|
|
||||||
namespace,
|
|
||||||
} => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
|
|
||||||
|
|
||||||
let db = open_db(data_dir.join("sqlite")).await?;
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
&seed,
|
|
||||||
data_dir.clone(),
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let (monero_wallet, _process) =
|
|
||||||
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
|
|
||||||
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
|
||||||
|
|
||||||
let seller_peer_id = db.get_peer_id(swap_id).await?;
|
|
||||||
let seller_addresses = db.get_addresses(seller_peer_id).await?;
|
|
||||||
|
|
||||||
let behaviour = cli::Behaviour::new(
|
|
||||||
seller_peer_id,
|
|
||||||
env_config,
|
|
||||||
bitcoin_wallet.clone(),
|
|
||||||
(seed.derive_libp2p_identity(), namespace),
|
|
||||||
);
|
|
||||||
let mut swarm =
|
|
||||||
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
|
|
||||||
let our_peer_id = swarm.local_peer_id();
|
|
||||||
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
|
|
||||||
|
|
||||||
for seller_address in seller_addresses {
|
|
||||||
swarm
|
|
||||||
.behaviour_mut()
|
|
||||||
.add_address(seller_peer_id, seller_address);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) = EventLoop::new(swap_id, swarm, seller_peer_id)?;
|
|
||||||
let handle = tokio::spawn(event_loop.run());
|
|
||||||
|
|
||||||
let monero_receive_address = db.get_monero_address(swap_id).await?;
|
|
||||||
let swap = Swap::from_db(
|
|
||||||
db,
|
|
||||||
swap_id,
|
|
||||||
bitcoin_wallet,
|
|
||||||
Arc::new(monero_wallet),
|
|
||||||
env_config,
|
|
||||||
event_loop_handle,
|
|
||||||
monero_receive_address,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
tokio::select! {
|
|
||||||
event_loop_result = handle => {
|
|
||||||
event_loop_result?;
|
|
||||||
},
|
|
||||||
swap_result = bob::run(swap) => {
|
|
||||||
swap_result?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Command::CancelAndRefund {
|
|
||||||
swap_id,
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
bitcoin_target_block,
|
|
||||||
} => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
|
|
||||||
|
|
||||||
let db = open_db(data_dir.join("sqlite")).await?;
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
&seed,
|
|
||||||
data_dir,
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
cli::cancel_and_refund(swap_id, Arc::new(bitcoin_wallet), db).await?;
|
|
||||||
}
|
|
||||||
Command::ListSellers {
|
|
||||||
rendezvous_point,
|
|
||||||
namespace,
|
|
||||||
tor_socks5_port,
|
|
||||||
} => {
|
|
||||||
let rendezvous_node_peer_id = rendezvous_point
|
|
||||||
.extract_peer_id()
|
|
||||||
.context("Rendezvous node address must contain peer ID")?;
|
|
||||||
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
||||||
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
let identity = seed.derive_libp2p_identity();
|
|
||||||
|
|
||||||
let sellers = list_sellers(
|
|
||||||
rendezvous_node_peer_id,
|
|
||||||
rendezvous_point,
|
|
||||||
namespace,
|
|
||||||
tor_socks5_port,
|
|
||||||
identity,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if json {
|
|
||||||
for seller in sellers {
|
|
||||||
match seller.status {
|
|
||||||
SellerStatus::Online(quote) => {
|
|
||||||
tracing::info!(
|
|
||||||
price = %quote.price.to_string(),
|
|
||||||
min_quantity = %quote.min_quantity.to_string(),
|
|
||||||
max_quantity = %quote.max_quantity.to_string(),
|
|
||||||
status = "Online",
|
|
||||||
address = %seller.multiaddr.to_string(),
|
|
||||||
"Fetched peer status"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
SellerStatus::Unreachable => {
|
|
||||||
tracing::info!(
|
|
||||||
status = "Unreachable",
|
|
||||||
address = %seller.multiaddr.to_string(),
|
|
||||||
"Fetched peer status"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let mut table = Table::new();
|
|
||||||
|
|
||||||
table.set_header(vec![
|
|
||||||
"PRICE",
|
|
||||||
"MIN_QUANTITY",
|
|
||||||
"MAX_QUANTITY",
|
|
||||||
"STATUS",
|
|
||||||
"ADDRESS",
|
|
||||||
]);
|
|
||||||
|
|
||||||
for seller in sellers {
|
|
||||||
let row = match seller.status {
|
|
||||||
SellerStatus::Online(quote) => {
|
|
||||||
vec![
|
|
||||||
quote.price.to_string(),
|
|
||||||
quote.min_quantity.to_string(),
|
|
||||||
quote.max_quantity.to_string(),
|
|
||||||
"Online".to_owned(),
|
|
||||||
seller.multiaddr.to_string(),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
SellerStatus::Unreachable => {
|
|
||||||
vec![
|
|
||||||
"???".to_owned(),
|
|
||||||
"???".to_owned(),
|
|
||||||
"???".to_owned(),
|
|
||||||
"Unreachable".to_owned(),
|
|
||||||
seller.multiaddr.to_string(),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
table.add_row(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{}", table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Command::ExportBitcoinWallet {
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
bitcoin_target_block,
|
|
||||||
} => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
||||||
|
|
||||||
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
||||||
.context("Failed to read in seed file")?;
|
|
||||||
let bitcoin_wallet = init_bitcoin_wallet(
|
|
||||||
bitcoin_electrum_rpc_url,
|
|
||||||
&seed,
|
|
||||||
data_dir.clone(),
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let wallet_export = bitcoin_wallet.wallet_export("cli").await?;
|
|
||||||
tracing::info!(descriptor=%wallet_export.to_string(), "Exported bitcoin wallet");
|
|
||||||
}
|
|
||||||
Command::MoneroRecovery { swap_id } => {
|
|
||||||
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
|
|
||||||
|
|
||||||
let db = open_db(data_dir.join("sqlite")).await?;
|
|
||||||
|
|
||||||
let swap_state: BobState = db.get_state(swap_id).await?.try_into()?;
|
|
||||||
|
|
||||||
match swap_state {
|
|
||||||
BobState::Started { .. }
|
|
||||||
| BobState::SwapSetupCompleted(_)
|
|
||||||
| BobState::BtcLocked { .. }
|
|
||||||
| BobState::XmrLockProofReceived { .. }
|
|
||||||
| BobState::XmrLocked(_)
|
|
||||||
| BobState::EncSigSent(_)
|
|
||||||
| BobState::CancelTimelockExpired(_)
|
|
||||||
| BobState::BtcCancelled(_)
|
|
||||||
| BobState::BtcRefunded(_)
|
|
||||||
| BobState::BtcPunished { .. }
|
|
||||||
| BobState::SafelyAborted
|
|
||||||
| BobState::XmrRedeemed { .. } => {
|
|
||||||
bail!("Cannot print monero recovery information in state {}, only possible for BtcRedeemed", swap_state)
|
|
||||||
}
|
|
||||||
BobState::BtcRedeemed(state5) => {
|
|
||||||
let (spend_key, view_key) = state5.xmr_keys();
|
|
||||||
|
|
||||||
let address = monero::Address::standard(
|
|
||||||
env_config.monero_network,
|
|
||||||
monero::PublicKey::from_private_key(&spend_key),
|
|
||||||
monero::PublicKey::from(view_key.public()),
|
|
||||||
);
|
|
||||||
tracing::info!("Wallet address: {}", address.to_string());
|
|
||||||
|
|
||||||
let view_key = serde_json::to_string(&view_key)?;
|
|
||||||
println!("View key: {}", view_key);
|
|
||||||
|
|
||||||
println!("Spend key: {}", spend_key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_bitcoin_wallet(
|
|
||||||
electrum_rpc_url: Url,
|
|
||||||
seed: &Seed,
|
|
||||||
data_dir: PathBuf,
|
|
||||||
env_config: Config,
|
|
||||||
bitcoin_target_block: usize,
|
|
||||||
) -> Result<bitcoin::Wallet> {
|
|
||||||
tracing::debug!("Initializing bitcoin wallet");
|
|
||||||
let xprivkey = seed.derive_extended_private_key(env_config.bitcoin_network)?;
|
|
||||||
|
|
||||||
let wallet = bitcoin::Wallet::new(
|
|
||||||
electrum_rpc_url.clone(),
|
|
||||||
data_dir,
|
|
||||||
xprivkey,
|
|
||||||
env_config,
|
|
||||||
bitcoin_target_block,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.context("Failed to initialize Bitcoin wallet")?;
|
|
||||||
|
|
||||||
tracing::debug!("Syncing bitcoin wallet");
|
|
||||||
wallet.sync().await?;
|
|
||||||
|
|
||||||
Ok(wallet)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn init_monero_wallet(
|
|
||||||
data_dir: PathBuf,
|
|
||||||
monero_daemon_address: String,
|
|
||||||
env_config: Config,
|
|
||||||
) -> Result<(monero::Wallet, monero::WalletRpcProcess)> {
|
|
||||||
let network = env_config.monero_network;
|
|
||||||
|
|
||||||
const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-monitoring-wallet";
|
|
||||||
|
|
||||||
let monero_wallet_rpc = monero::WalletRpc::new(data_dir.join("monero")).await?;
|
|
||||||
|
|
||||||
let monero_wallet_rpc_process = monero_wallet_rpc
|
|
||||||
.run(network, monero_daemon_address.as_str())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let monero_wallet = monero::Wallet::open_or_create(
|
|
||||||
monero_wallet_rpc_process.endpoint(),
|
|
||||||
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
|
|
||||||
env_config,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok((monero_wallet, monero_wallet_rpc_process))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn qr_code(value: &impl ToString) -> Result<String> {
|
|
||||||
let code = QrCode::new(value.to_string())?;
|
|
||||||
let qr_code = code
|
|
||||||
.render::<unicode::Dense1x2>()
|
|
||||||
.dark_color(unicode::Dense1x2::Light)
|
|
||||||
.light_color(unicode::Dense1x2::Dark)
|
|
||||||
.build();
|
|
||||||
Ok(qr_code)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
|
|
||||||
json: bool,
|
|
||||||
bid_quote: impl Future<Output = Result<BidQuote>>,
|
|
||||||
get_new_address: impl Future<Output = Result<bitcoin::Address>>,
|
|
||||||
balance: FB,
|
|
||||||
max_giveable_fn: FMG,
|
|
||||||
sync: FS,
|
|
||||||
estimate_fee: FFE,
|
|
||||||
) -> Result<(bitcoin::Amount, bitcoin::Amount)>
|
|
||||||
where
|
|
||||||
TB: Future<Output = Result<bitcoin::Amount>>,
|
|
||||||
FB: Fn() -> TB,
|
|
||||||
TMG: Future<Output = Result<bitcoin::Amount>>,
|
|
||||||
FMG: Fn() -> TMG,
|
|
||||||
TS: Future<Output = Result<()>>,
|
|
||||||
FS: Fn() -> TS,
|
|
||||||
FFE: Fn(bitcoin::Amount) -> TFE,
|
|
||||||
TFE: Future<Output = Result<bitcoin::Amount>>,
|
|
||||||
{
|
|
||||||
tracing::debug!("Requesting quote");
|
|
||||||
let bid_quote = bid_quote.await?;
|
|
||||||
|
|
||||||
if bid_quote.max_quantity == bitcoin::Amount::ZERO {
|
|
||||||
bail!(ZeroQuoteReceived)
|
|
||||||
}
|
|
||||||
|
|
||||||
tracing::info!(
|
|
||||||
price = %bid_quote.price,
|
|
||||||
minimum_amount = %bid_quote.min_quantity,
|
|
||||||
maximum_amount = %bid_quote.max_quantity,
|
|
||||||
"Received quote",
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut max_giveable = max_giveable_fn().await?;
|
|
||||||
|
|
||||||
if max_giveable == bitcoin::Amount::ZERO || max_giveable < bid_quote.min_quantity {
|
|
||||||
let deposit_address = get_new_address.await?;
|
|
||||||
let minimum_amount = bid_quote.min_quantity;
|
|
||||||
let maximum_amount = bid_quote.max_quantity;
|
|
||||||
|
|
||||||
if !json {
|
|
||||||
eprintln!("{}", qr_code(&deposit_address)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let min_outstanding = bid_quote.min_quantity - max_giveable;
|
|
||||||
let min_fee = estimate_fee(min_outstanding).await?;
|
|
||||||
let min_deposit = min_outstanding + min_fee;
|
|
||||||
|
|
||||||
tracing::info!(
|
|
||||||
"Deposit at least {} to cover the min quantity with fee!",
|
|
||||||
min_deposit
|
|
||||||
);
|
|
||||||
tracing::info!(
|
|
||||||
%deposit_address,
|
|
||||||
%min_deposit,
|
|
||||||
%max_giveable,
|
|
||||||
%minimum_amount,
|
|
||||||
%maximum_amount,
|
|
||||||
"Waiting for Bitcoin deposit",
|
|
||||||
);
|
|
||||||
|
|
||||||
max_giveable = loop {
|
|
||||||
sync().await?;
|
|
||||||
let new_max_givable = max_giveable_fn().await?;
|
|
||||||
|
|
||||||
if new_max_givable > max_giveable {
|
|
||||||
break new_max_givable;
|
|
||||||
}
|
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
||||||
};
|
|
||||||
|
|
||||||
let new_balance = balance().await?;
|
|
||||||
tracing::info!(%new_balance, %max_giveable, "Received Bitcoin");
|
|
||||||
|
|
||||||
if max_giveable < bid_quote.min_quantity {
|
|
||||||
tracing::info!("Deposited amount is less than `min_quantity`");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let balance = balance().await?;
|
|
||||||
let fees = balance - max_giveable;
|
|
||||||
let max_accepted = bid_quote.max_quantity;
|
|
||||||
let btc_swap_amount = min(max_giveable, max_accepted);
|
|
||||||
|
|
||||||
Ok((btc_swap_amount, fees))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::determine_btc_to_swap;
|
|
||||||
use ::bitcoin::Amount;
|
use ::bitcoin::Amount;
|
||||||
use std::sync::Mutex;
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
use swap::api::request::determine_btc_to_swap;
|
||||||
|
use swap::network::quote::BidQuote;
|
||||||
use swap::tracing_ext::capture_logs;
|
use swap::tracing_ext::capture_logs;
|
||||||
use tracing::level_filters::LevelFilter;
|
use tracing::level_filters::LevelFilter;
|
||||||
|
|
||||||
@ -666,7 +56,7 @@ mod tests {
|
|||||||
|
|
||||||
let (amount, fees) = determine_btc_to_swap(
|
let (amount, fees) = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_max(0.01)) },
|
quote_with_max(0.01),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.001)?) },
|
|| async { Ok(Amount::from_btc(0.001)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -685,10 +75,10 @@ mod tests {
|
|||||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
|
||||||
INFO swap: Deposit at least 0.00001000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.00001 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001 BTC max_giveable=0 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
|
||||||
INFO swap: Received Bitcoin new_balance=0.00100000 BTC max_giveable=0.00090000 BTC
|
INFO swap::api::request: Received Bitcoin new_balance=0.001 BTC max_giveable=0.0009 BTC
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -703,7 +93,7 @@ mod tests {
|
|||||||
|
|
||||||
let (amount, fees) = determine_btc_to_swap(
|
let (amount, fees) = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_max(0.01)) },
|
quote_with_max(0.01),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.1001)?) },
|
|| async { Ok(Amount::from_btc(0.1001)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -722,10 +112,10 @@ mod tests {
|
|||||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
|
||||||
INFO swap: Deposit at least 0.00001000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.00001 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001 BTC max_giveable=0 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
|
||||||
INFO swap: Received Bitcoin new_balance=0.10010000 BTC max_giveable=0.10000000 BTC
|
INFO swap::api::request: Received Bitcoin new_balance=0.1001 BTC max_giveable=0.1 BTC
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -740,7 +130,7 @@ mod tests {
|
|||||||
|
|
||||||
let (amount, fees) = determine_btc_to_swap(
|
let (amount, fees) = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_max(0.01)) },
|
quote_with_max(0.01),
|
||||||
async { panic!("should not request new address when initial balance is > 0") },
|
async { panic!("should not request new address when initial balance is > 0") },
|
||||||
|| async { Ok(Amount::from_btc(0.005)?) },
|
|| async { Ok(Amount::from_btc(0.005)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -759,7 +149,7 @@ mod tests {
|
|||||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC\n"
|
" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -773,7 +163,7 @@ mod tests {
|
|||||||
|
|
||||||
let (amount, fees) = determine_btc_to_swap(
|
let (amount, fees) = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_max(0.01)) },
|
quote_with_max(0.01),
|
||||||
async { panic!("should not request new address when initial balance is > 0") },
|
async { panic!("should not request new address when initial balance is > 0") },
|
||||||
|| async { Ok(Amount::from_btc(0.1001)?) },
|
|| async { Ok(Amount::from_btc(0.1001)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -792,7 +182,7 @@ mod tests {
|
|||||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC\n"
|
" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -806,7 +196,7 @@ mod tests {
|
|||||||
|
|
||||||
let (amount, fees) = determine_btc_to_swap(
|
let (amount, fees) = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_min(0.01)) },
|
quote_with_min(0.01),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -825,10 +215,10 @@ mod tests {
|
|||||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Deposit at least 0.01001000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.01001 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.01001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.01001 BTC max_giveable=0 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
INFO swap::api::request: Received Bitcoin new_balance=0.0101 BTC max_giveable=0.01 BTC
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -843,7 +233,7 @@ mod tests {
|
|||||||
|
|
||||||
let (amount, fees) = determine_btc_to_swap(
|
let (amount, fees) = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_min(0.01)) },
|
quote_with_min(0.01),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -862,10 +252,10 @@ mod tests {
|
|||||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Deposit at least 0.00991000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.00991 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00991000 BTC max_giveable=0.00010000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00991 BTC max_giveable=0.0001 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
INFO swap::api::request: Received Bitcoin new_balance=0.0101 BTC max_giveable=0.01 BTC
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -885,7 +275,7 @@ mod tests {
|
|||||||
Duration::from_secs(1),
|
Duration::from_secs(1),
|
||||||
determine_btc_to_swap(
|
determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_min(0.1)) },
|
quote_with_min(0.1),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -902,13 +292,13 @@ mod tests {
|
|||||||
assert!(matches!(error, tokio::time::error::Elapsed { .. }));
|
assert!(matches!(error, tokio::time::error::Elapsed { .. }));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Deposit at least 0.10001000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.10001 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001 BTC max_giveable=0 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
INFO swap::api::request: Received Bitcoin new_balance=0.0101 BTC max_giveable=0.01 BTC
|
||||||
INFO swap: Deposited amount is less than `min_quantity`
|
INFO swap::api::request: Deposited amount is less than `min_quantity`
|
||||||
INFO swap: Deposit at least 0.09001000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.09001 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.09001000 BTC max_giveable=0.01000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.09001 BTC max_giveable=0.01 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -933,7 +323,7 @@ mod tests {
|
|||||||
Duration::from_secs(10),
|
Duration::from_secs(10),
|
||||||
determine_btc_to_swap(
|
determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_min(0.1)) },
|
quote_with_min(0.1),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.21)?) },
|
|| async { Ok(Amount::from_btc(0.21)?) },
|
||||||
|| async {
|
|| async {
|
||||||
@ -951,10 +341,10 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
writer.captured(),
|
writer.captured(),
|
||||||
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Deposit at least 0.10001000 BTC to cover the min quantity with fee!
|
INFO swap::api::request: Deposit at least 0.10001 BTC to cover the min quantity with fee!
|
||||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001 BTC max_giveable=0 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
|
||||||
INFO swap: Received Bitcoin new_balance=0.21000000 BTC max_giveable=0.20000000 BTC
|
INFO swap::api::request: Received Bitcoin new_balance=0.21 BTC max_giveable=0.2 BTC
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -968,7 +358,7 @@ mod tests {
|
|||||||
|
|
||||||
let determination_error = determine_btc_to_swap(
|
let determination_error = determine_btc_to_swap(
|
||||||
true,
|
true,
|
||||||
async { Ok(quote_with_max(0.00)) },
|
quote_with_max(0.00),
|
||||||
get_dummy_address(),
|
get_dummy_address(),
|
||||||
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
||||||
|| async {
|
|| async {
|
||||||
|
@ -15,7 +15,8 @@ pub use crate::bitcoin::refund::TxRefund;
|
|||||||
pub use crate::bitcoin::timelocks::{BlockHeight, ExpiredTimelocks};
|
pub use crate::bitcoin::timelocks::{BlockHeight, ExpiredTimelocks};
|
||||||
pub use ::bitcoin::util::amount::Amount;
|
pub use ::bitcoin::util::amount::Amount;
|
||||||
pub use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
pub use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
||||||
pub use ::bitcoin::{Address, Network, Transaction, Txid};
|
pub use ::bitcoin::{Address, AddressType, Network, Transaction, Txid};
|
||||||
|
use bitcoin::secp256k1::ecdsa;
|
||||||
pub use ecdsa_fun::adaptor::EncryptedSignature;
|
pub use ecdsa_fun::adaptor::EncryptedSignature;
|
||||||
pub use ecdsa_fun::fun::Scalar;
|
pub use ecdsa_fun::fun::Scalar;
|
||||||
pub use ecdsa_fun::Signature;
|
pub use ecdsa_fun::Signature;
|
||||||
@ -25,9 +26,8 @@ pub use wallet::Wallet;
|
|||||||
pub use wallet::WalletBuilder;
|
pub use wallet::WalletBuilder;
|
||||||
|
|
||||||
use crate::bitcoin::wallet::ScriptStatus;
|
use crate::bitcoin::wallet::ScriptStatus;
|
||||||
use ::bitcoin::hashes::hex::ToHex;
|
|
||||||
use ::bitcoin::hashes::Hash;
|
use ::bitcoin::hashes::Hash;
|
||||||
use ::bitcoin::{secp256k1, Sighash};
|
use ::bitcoin::Sighash;
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use bdk::miniscript::descriptor::Wsh;
|
use bdk::miniscript::descriptor::Wsh;
|
||||||
use bdk::miniscript::{Descriptor, Segwitv0};
|
use bdk::miniscript::{Descriptor, Segwitv0};
|
||||||
@ -206,20 +206,21 @@ pub fn verify_encsig(
|
|||||||
#[error("encrypted signature is invalid")]
|
#[error("encrypted signature is invalid")]
|
||||||
pub struct InvalidEncryptedSignature;
|
pub struct InvalidEncryptedSignature;
|
||||||
|
|
||||||
pub fn build_shared_output_descriptor(A: Point, B: Point) -> Descriptor<bitcoin::PublicKey> {
|
pub fn build_shared_output_descriptor(
|
||||||
|
A: Point,
|
||||||
|
B: Point,
|
||||||
|
) -> Result<Descriptor<bitcoin::PublicKey>> {
|
||||||
const MINISCRIPT_TEMPLATE: &str = "c:and_v(v:pk(A),pk_k(B))";
|
const MINISCRIPT_TEMPLATE: &str = "c:and_v(v:pk(A),pk_k(B))";
|
||||||
|
|
||||||
// NOTE: This shouldn't be a source of error, but maybe it is
|
let miniscript = MINISCRIPT_TEMPLATE
|
||||||
let A = ToHex::to_hex(&secp256k1::PublicKey::from(A));
|
.replace('A', &A.to_string())
|
||||||
let B = ToHex::to_hex(&secp256k1::PublicKey::from(B));
|
.replace('B', &B.to_string());
|
||||||
|
|
||||||
let miniscript = MINISCRIPT_TEMPLATE.replace('A', &A).replace('B', &B);
|
|
||||||
|
|
||||||
let miniscript =
|
let miniscript =
|
||||||
bdk::miniscript::Miniscript::<bitcoin::PublicKey, Segwitv0>::from_str(&miniscript)
|
bdk::miniscript::Miniscript::<bitcoin::PublicKey, Segwitv0>::from_str(&miniscript)
|
||||||
.expect("a valid miniscript");
|
.expect("a valid miniscript");
|
||||||
|
|
||||||
Descriptor::Wsh(Wsh::new(miniscript).expect("a valid descriptor"))
|
Ok(Descriptor::Wsh(Wsh::new(miniscript)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result<SecretKey> {
|
pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result<SecretKey> {
|
||||||
@ -244,10 +245,72 @@ pub fn current_epoch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if tx_lock_status.is_confirmed_with(cancel_timelock) {
|
if tx_lock_status.is_confirmed_with(cancel_timelock) {
|
||||||
return ExpiredTimelocks::Cancel;
|
return ExpiredTimelocks::Cancel {
|
||||||
|
blocks_left: tx_cancel_status.blocks_left_until(punish_timelock),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ExpiredTimelocks::None
|
ExpiredTimelocks::None {
|
||||||
|
blocks_left: tx_lock_status.blocks_left_until(cancel_timelock),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod bitcoin_address {
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use serde::Serialize;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Serialize)]
|
||||||
|
#[error("Invalid Bitcoin address provided, expected address on network {expected:?} but address provided is on {actual:?}")]
|
||||||
|
pub struct BitcoinAddressNetworkMismatch {
|
||||||
|
#[serde(with = "crate::bitcoin::network")]
|
||||||
|
expected: bitcoin::Network,
|
||||||
|
#[serde(with = "crate::bitcoin::network")]
|
||||||
|
actual: bitcoin::Network,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(addr_str: &str) -> Result<bitcoin::Address> {
|
||||||
|
let address = bitcoin::Address::from_str(addr_str)?;
|
||||||
|
|
||||||
|
if address.address_type() != Some(bitcoin::AddressType::P2wpkh) {
|
||||||
|
anyhow::bail!("Invalid Bitcoin address provided, only bech32 format is supported!")
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(address)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate(
|
||||||
|
address: bitcoin::Address,
|
||||||
|
expected_network: bitcoin::Network,
|
||||||
|
) -> Result<bitcoin::Address> {
|
||||||
|
if address.network != expected_network {
|
||||||
|
bail!(BitcoinAddressNetworkMismatch {
|
||||||
|
expected: expected_network,
|
||||||
|
actual: address.network
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(address)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_is_testnet(
|
||||||
|
address: bitcoin::Address,
|
||||||
|
is_testnet: bool,
|
||||||
|
) -> Result<bitcoin::Address> {
|
||||||
|
let expected_network = if is_testnet {
|
||||||
|
bitcoin::Network::Testnet
|
||||||
|
} else {
|
||||||
|
bitcoin::Network::Bitcoin
|
||||||
|
};
|
||||||
|
validate(address, expected_network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform the ecdsa der signature bytes into a secp256kfun ecdsa signature type.
|
||||||
|
pub fn extract_ecdsa_sig(sig: &[u8]) -> Result<Signature> {
|
||||||
|
let data = &sig[..sig.len() - 1];
|
||||||
|
let sig = ecdsa::Signature::from_der(data)?.serialize_compact();
|
||||||
|
Signature::from_bytes(sig).ok_or(anyhow::anyhow!("invalid signature"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bitcoin error codes: https://github.com/bitcoin/bitcoin/blob/97d3500601c1d28642347d014a6de1e38f53ae4e/src/rpc/protocol.h#L23
|
/// Bitcoin error codes: https://github.com/bitcoin/bitcoin/blob/97d3500601c1d28642347d014a6de1e38f53ae4e/src/rpc/protocol.h#L23
|
||||||
@ -323,7 +386,10 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::env::{GetConfig, Regtest};
|
use crate::env::{GetConfig, Regtest};
|
||||||
use crate::protocol::{alice, bob};
|
use crate::protocol::{alice, bob};
|
||||||
|
use bitcoin::secp256k1;
|
||||||
|
use ecdsa_fun::fun::marker::{NonZero, Public};
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
|
use std::matches;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -338,7 +404,7 @@ mod tests {
|
|||||||
tx_cancel_status,
|
tx_cancel_status,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(expired_timelock, ExpiredTimelocks::None)
|
assert!(matches!(expired_timelock, ExpiredTimelocks::None { .. }));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -353,7 +419,7 @@ mod tests {
|
|||||||
tx_cancel_status,
|
tx_cancel_status,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(expired_timelock, ExpiredTimelocks::Cancel)
|
assert!(matches!(expired_timelock, ExpiredTimelocks::Cancel { .. }));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -468,4 +534,16 @@ mod tests {
|
|||||||
transaction
|
transaction
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compare_point_hex() {
|
||||||
|
// secp256kfun Point and secp256k1 PublicKey should have the same bytes and hex representation
|
||||||
|
let secp = secp256k1::Secp256k1::default();
|
||||||
|
let keypair = secp256k1::KeyPair::new(&secp, &mut OsRng);
|
||||||
|
|
||||||
|
let pubkey = keypair.public_key();
|
||||||
|
let point: Point<_, Public, NonZero> = Point::from_bytes(pubkey.serialize()).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(pubkey.to_string(), point.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ use crate::bitcoin::{
|
|||||||
};
|
};
|
||||||
use ::bitcoin::util::sighash::SighashCache;
|
use ::bitcoin::util::sighash::SighashCache;
|
||||||
use ::bitcoin::{
|
use ::bitcoin::{
|
||||||
EcdsaSighashType, OutPoint, PackedLockTime, Script, Sequence, Sighash, TxIn, TxOut, Txid,
|
secp256k1, EcdsaSighashType, OutPoint, PackedLockTime, Script, Sequence, Sighash, TxIn, TxOut,
|
||||||
|
Txid,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use bdk::miniscript::Descriptor;
|
use bdk::miniscript::Descriptor;
|
||||||
@ -24,6 +25,12 @@ use std::ops::Add;
|
|||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct CancelTimelock(u32);
|
pub struct CancelTimelock(u32);
|
||||||
|
|
||||||
|
impl From<CancelTimelock> for u32 {
|
||||||
|
fn from(cancel_timelock: CancelTimelock) -> Self {
|
||||||
|
cancel_timelock.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CancelTimelock {
|
impl CancelTimelock {
|
||||||
pub const fn new(number_of_blocks: u32) -> Self {
|
pub const fn new(number_of_blocks: u32) -> Self {
|
||||||
Self(number_of_blocks)
|
Self(number_of_blocks)
|
||||||
@ -64,6 +71,12 @@ impl fmt::Display for CancelTimelock {
|
|||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct PunishTimelock(u32);
|
pub struct PunishTimelock(u32);
|
||||||
|
|
||||||
|
impl From<PunishTimelock> for u32 {
|
||||||
|
fn from(punish_timelock: PunishTimelock) -> Self {
|
||||||
|
punish_timelock.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PunishTimelock {
|
impl PunishTimelock {
|
||||||
pub const fn new(number_of_blocks: u32) -> Self {
|
pub const fn new(number_of_blocks: u32) -> Self {
|
||||||
Self(number_of_blocks)
|
Self(number_of_blocks)
|
||||||
@ -105,8 +118,8 @@ impl TxCancel {
|
|||||||
A: PublicKey,
|
A: PublicKey,
|
||||||
B: PublicKey,
|
B: PublicKey,
|
||||||
spending_fee: Amount,
|
spending_fee: Amount,
|
||||||
) -> Self {
|
) -> Result<Self> {
|
||||||
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0);
|
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0)?;
|
||||||
|
|
||||||
let tx_in = TxIn {
|
let tx_in = TxIn {
|
||||||
previous_output: tx_lock.as_outpoint(),
|
previous_output: tx_lock.as_outpoint(),
|
||||||
@ -136,12 +149,12 @@ impl TxCancel {
|
|||||||
)
|
)
|
||||||
.expect("sighash");
|
.expect("sighash");
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
inner: transaction,
|
inner: transaction,
|
||||||
digest,
|
digest,
|
||||||
output_descriptor: cancel_output_descriptor,
|
output_descriptor: cancel_output_descriptor,
|
||||||
lock_output_descriptor: tx_lock.output_descriptor.clone(),
|
lock_output_descriptor: tx_lock.output_descriptor.clone(),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn txid(&self) -> Txid {
|
pub fn txid(&self) -> Txid {
|
||||||
@ -202,25 +215,27 @@ impl TxCancel {
|
|||||||
|
|
||||||
let A = ::bitcoin::PublicKey {
|
let A = ::bitcoin::PublicKey {
|
||||||
compressed: true,
|
compressed: true,
|
||||||
inner: A.0.into(),
|
inner: secp256k1::PublicKey::from_slice(&A.0.to_bytes())?,
|
||||||
};
|
};
|
||||||
let B = ::bitcoin::PublicKey {
|
let B = ::bitcoin::PublicKey {
|
||||||
compressed: true,
|
compressed: true,
|
||||||
inner: B.0.into(),
|
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
|
||||||
};
|
};
|
||||||
|
|
||||||
// The order in which these are inserted doesn't matter
|
// The order in which these are inserted doesn't matter
|
||||||
|
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
|
||||||
|
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
A,
|
A,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_a.into(),
|
sig: sig_a,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
B,
|
B,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_b.into(),
|
sig: sig_b,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -4,9 +4,10 @@ use crate::bitcoin::{
|
|||||||
};
|
};
|
||||||
use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
||||||
use ::bitcoin::{OutPoint, TxIn, TxOut, Txid};
|
use ::bitcoin::{OutPoint, TxIn, TxOut, Txid};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use bdk::database::BatchDatabase;
|
use bdk::database::BatchDatabase;
|
||||||
use bdk::miniscript::Descriptor;
|
use bdk::miniscript::Descriptor;
|
||||||
|
use bdk::psbt::PsbtUtils;
|
||||||
use bitcoin::{PackedLockTime, Script, Sequence};
|
use bitcoin::{PackedLockTime, Script, Sequence};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ impl TxLock {
|
|||||||
C: EstimateFeeRate,
|
C: EstimateFeeRate,
|
||||||
D: BatchDatabase,
|
D: BatchDatabase,
|
||||||
{
|
{
|
||||||
let lock_output_descriptor = build_shared_output_descriptor(A.0, B.0);
|
let lock_output_descriptor = build_shared_output_descriptor(A.0, B.0)?;
|
||||||
let address = lock_output_descriptor
|
let address = lock_output_descriptor
|
||||||
.address(wallet.get_network())
|
.address(wallet.get_network())
|
||||||
.expect("can derive address from descriptor");
|
.expect("can derive address from descriptor");
|
||||||
@ -83,7 +84,7 @@ impl TxLock {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let descriptor = build_shared_output_descriptor(A.0, B.0);
|
let descriptor = build_shared_output_descriptor(A.0, B.0)?;
|
||||||
let legit_shared_output_script = descriptor.script_pubkey();
|
let legit_shared_output_script = descriptor.script_pubkey();
|
||||||
|
|
||||||
if shared_output_candidate.script_pubkey != legit_shared_output_script {
|
if shared_output_candidate.script_pubkey != legit_shared_output_script {
|
||||||
@ -100,6 +101,15 @@ impl TxLock {
|
|||||||
Amount::from_sat(self.inner.clone().extract_tx().output[self.lock_output_vout()].value)
|
Amount::from_sat(self.inner.clone().extract_tx().output[self.lock_output_vout()].value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn fee(&self) -> Result<Amount> {
|
||||||
|
Ok(Amount::from_sat(
|
||||||
|
self.inner
|
||||||
|
.clone()
|
||||||
|
.fee_amount()
|
||||||
|
.context("The PSBT is missing a TxOut for an input")?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn txid(&self) -> Txid {
|
pub fn txid(&self) -> Txid {
|
||||||
self.inner.clone().extract_tx().txid()
|
self.inner.clone().extract_tx().txid()
|
||||||
}
|
}
|
||||||
@ -253,7 +263,7 @@ mod tests {
|
|||||||
fn estimated_tx_lock_script_size_never_changes(a in crate::proptest::ecdsa_fun::point(), b in crate::proptest::ecdsa_fun::point()) {
|
fn estimated_tx_lock_script_size_never_changes(a in crate::proptest::ecdsa_fun::point(), b in crate::proptest::ecdsa_fun::point()) {
|
||||||
proptest::prop_assume!(a != b);
|
proptest::prop_assume!(a != b);
|
||||||
|
|
||||||
let computed_size = build_shared_output_descriptor(a, b).script_pubkey().len();
|
let computed_size = build_shared_output_descriptor(a, b).unwrap().script_pubkey().len();
|
||||||
|
|
||||||
assert_eq!(computed_size, SCRIPT_SIZE);
|
assert_eq!(computed_size, SCRIPT_SIZE);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::bitcoin::wallet::Watchable;
|
use crate::bitcoin::wallet::Watchable;
|
||||||
use crate::bitcoin::{self, Address, Amount, PunishTimelock, Transaction, TxCancel, Txid};
|
use crate::bitcoin::{self, Address, Amount, PunishTimelock, Transaction, TxCancel, Txid};
|
||||||
use ::bitcoin::util::sighash::SighashCache;
|
use ::bitcoin::util::sighash::SighashCache;
|
||||||
use ::bitcoin::{EcdsaSighashType, Sighash};
|
use ::bitcoin::{secp256k1, EcdsaSighashType, Sighash};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use bdk::bitcoin::Script;
|
use bdk::bitcoin::Script;
|
||||||
use bdk::miniscript::Descriptor;
|
use bdk::miniscript::Descriptor;
|
||||||
@ -64,18 +64,20 @@ impl TxPunish {
|
|||||||
let A = a.public().try_into()?;
|
let A = a.public().try_into()?;
|
||||||
let B = B.try_into()?;
|
let B = B.try_into()?;
|
||||||
|
|
||||||
|
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
|
||||||
|
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
|
||||||
// The order in which these are inserted doesn't matter
|
// The order in which these are inserted doesn't matter
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
A,
|
A,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_a.into(),
|
sig: sig_a,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
B,
|
B,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_b.into(),
|
sig: sig_b,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -6,7 +6,7 @@ use crate::bitcoin::{
|
|||||||
use ::bitcoin::{Sighash, Txid};
|
use ::bitcoin::{Sighash, Txid};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use bdk::miniscript::Descriptor;
|
use bdk::miniscript::Descriptor;
|
||||||
use bitcoin::secp256k1::ecdsa;
|
use bitcoin::secp256k1;
|
||||||
use bitcoin::util::sighash::SighashCache;
|
use bitcoin::util::sighash::SighashCache;
|
||||||
use bitcoin::{EcdsaSighashType, Script};
|
use bitcoin::{EcdsaSighashType, Script};
|
||||||
use ecdsa_fun::adaptor::{Adaptor, HashTranscript};
|
use ecdsa_fun::adaptor::{Adaptor, HashTranscript};
|
||||||
@ -16,6 +16,8 @@ use ecdsa_fun::Signature;
|
|||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use super::extract_ecdsa_sig;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TxRedeem {
|
pub struct TxRedeem {
|
||||||
inner: Transaction,
|
inner: Transaction,
|
||||||
@ -64,7 +66,7 @@ impl TxRedeem {
|
|||||||
) -> Result<Transaction> {
|
) -> Result<Transaction> {
|
||||||
verify_encsig(
|
verify_encsig(
|
||||||
B,
|
B,
|
||||||
PublicKey::from(s_a.clone()),
|
PublicKey::from(s_a),
|
||||||
&self.digest(),
|
&self.digest(),
|
||||||
&encrypted_signature,
|
&encrypted_signature,
|
||||||
)
|
)
|
||||||
@ -79,25 +81,27 @@ impl TxRedeem {
|
|||||||
|
|
||||||
let A = ::bitcoin::PublicKey {
|
let A = ::bitcoin::PublicKey {
|
||||||
compressed: true,
|
compressed: true,
|
||||||
inner: a.public.into(),
|
inner: secp256k1::PublicKey::from_slice(&a.public.to_bytes())?,
|
||||||
};
|
};
|
||||||
let B = ::bitcoin::PublicKey {
|
let B = ::bitcoin::PublicKey {
|
||||||
compressed: true,
|
compressed: true,
|
||||||
inner: B.0.into(),
|
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
|
||||||
|
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
|
||||||
// The order in which these are inserted doesn't matter
|
// The order in which these are inserted doesn't matter
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
A,
|
A,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_a.into(),
|
sig: sig_a,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
B,
|
B,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_b.into(),
|
sig: sig_b,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -120,16 +124,16 @@ impl TxRedeem {
|
|||||||
let input = match candidate_transaction.input.as_slice() {
|
let input = match candidate_transaction.input.as_slice() {
|
||||||
[input] => input,
|
[input] => input,
|
||||||
[] => bail!(NoInputs),
|
[] => bail!(NoInputs),
|
||||||
[inputs @ ..] => bail!(TooManyInputs(inputs.len())),
|
inputs => bail!(TooManyInputs(inputs.len())),
|
||||||
};
|
};
|
||||||
|
|
||||||
let sigs = match input.witness.iter().collect::<Vec<_>>().as_slice() {
|
let sigs = match input.witness.to_vec().as_slice() {
|
||||||
[sig_1, sig_2, _script] => [sig_1, sig_2]
|
[sig_1, sig_2, _script] => [sig_1, sig_2]
|
||||||
.iter()
|
.into_iter()
|
||||||
.map(|sig| ecdsa::Signature::from_der(&sig[..sig.len() - 1]).map(Signature::from))
|
.map(|sig| extract_ecdsa_sig(sig))
|
||||||
.collect::<std::result::Result<Vec<_>, _>>(),
|
.collect::<Result<Vec<_>, _>>(),
|
||||||
[] => bail!(EmptyWitnessStack),
|
[] => bail!(EmptyWitnessStack),
|
||||||
[witnesses @ ..] => bail!(NotThreeWitnesses(witnesses.len())),
|
witnesses => bail!(NotThreeWitnesses(witnesses.len())),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let sig = sigs
|
let sig = sigs
|
||||||
|
@ -4,7 +4,7 @@ use crate::bitcoin::{
|
|||||||
TooManyInputs, Transaction, TxCancel,
|
TooManyInputs, Transaction, TxCancel,
|
||||||
};
|
};
|
||||||
use crate::{bitcoin, monero};
|
use crate::{bitcoin, monero};
|
||||||
use ::bitcoin::secp256k1::ecdsa;
|
use ::bitcoin::secp256k1;
|
||||||
use ::bitcoin::util::sighash::SighashCache;
|
use ::bitcoin::util::sighash::SighashCache;
|
||||||
use ::bitcoin::{EcdsaSighashType, Script, Sighash, Txid};
|
use ::bitcoin::{EcdsaSighashType, Script, Sighash, Txid};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
@ -12,6 +12,8 @@ use bdk::miniscript::Descriptor;
|
|||||||
use ecdsa_fun::Signature;
|
use ecdsa_fun::Signature;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use super::extract_ecdsa_sig;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TxRefund {
|
pub struct TxRefund {
|
||||||
inner: Transaction,
|
inner: Transaction,
|
||||||
@ -62,25 +64,27 @@ impl TxRefund {
|
|||||||
|
|
||||||
let A = ::bitcoin::PublicKey {
|
let A = ::bitcoin::PublicKey {
|
||||||
compressed: true,
|
compressed: true,
|
||||||
inner: A.0.into(),
|
inner: secp256k1::PublicKey::from_slice(&A.0.to_bytes())?,
|
||||||
};
|
};
|
||||||
let B = ::bitcoin::PublicKey {
|
let B = ::bitcoin::PublicKey {
|
||||||
compressed: true,
|
compressed: true,
|
||||||
inner: B.0.into(),
|
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
|
||||||
|
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
|
||||||
// The order in which these are inserted doesn't matter
|
// The order in which these are inserted doesn't matter
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
A,
|
A,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_a.into(),
|
sig: sig_a,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
satisfier.insert(
|
satisfier.insert(
|
||||||
B,
|
B,
|
||||||
::bitcoin::EcdsaSig {
|
::bitcoin::EcdsaSig {
|
||||||
sig: sig_b.into(),
|
sig: sig_b,
|
||||||
hash_ty: EcdsaSighashType::All,
|
hash_ty: EcdsaSighashType::All,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -127,16 +131,16 @@ impl TxRefund {
|
|||||||
let input = match candidate_transaction.input.as_slice() {
|
let input = match candidate_transaction.input.as_slice() {
|
||||||
[input] => input,
|
[input] => input,
|
||||||
[] => bail!(NoInputs),
|
[] => bail!(NoInputs),
|
||||||
[inputs @ ..] => bail!(TooManyInputs(inputs.len())),
|
inputs => bail!(TooManyInputs(inputs.len())),
|
||||||
};
|
};
|
||||||
|
|
||||||
let sigs = match input.witness.iter().collect::<Vec<_>>().as_slice() {
|
let sigs = match input.witness.to_vec().as_slice() {
|
||||||
[sig_1, sig_2, _script] => [sig_1, sig_2]
|
[sig_1, sig_2, _script] => [sig_1, sig_2]
|
||||||
.iter()
|
.into_iter()
|
||||||
.map(|sig| ecdsa::Signature::from_der(&sig[..sig.len() - 1]).map(Signature::from))
|
.map(|sig| extract_ecdsa_sig(sig))
|
||||||
.collect::<std::result::Result<Vec<_>, _>>(),
|
.collect::<Result<Vec<_>, _>>(),
|
||||||
[] => bail!(EmptyWitnessStack),
|
[] => bail!(EmptyWitnessStack),
|
||||||
[witnesses @ ..] => bail!(NotThreeWitnesses(witnesses.len())),
|
witnesses => bail!(NotThreeWitnesses(witnesses.len())),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let sig = sigs
|
let sig = sigs
|
||||||
|
@ -37,9 +37,9 @@ impl Add<u32> for BlockHeight {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Serialize, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum ExpiredTimelocks {
|
pub enum ExpiredTimelocks {
|
||||||
None,
|
None { blocks_left: u32 },
|
||||||
Cancel,
|
Cancel { blocks_left: u32 },
|
||||||
Punish,
|
Punish,
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ use std::path::Path;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::sync::{watch, Mutex};
|
use tokio::sync::{watch, Mutex};
|
||||||
|
use tracing::{debug_span, Instrument};
|
||||||
|
|
||||||
const SLED_TREE_NAME: &str = "default_tree";
|
const SLED_TREE_NAME: &str = "default_tree";
|
||||||
|
|
||||||
@ -64,9 +65,7 @@ impl Wallet {
|
|||||||
database,
|
database,
|
||||||
) {
|
) {
|
||||||
Ok(w) => w,
|
Ok(w) => w,
|
||||||
Err(e) if matches!(e, bdk::Error::ChecksumMismatch) => {
|
Err(bdk::Error::ChecksumMismatch) => Self::migrate(data_dir, xprivkey, network)?,
|
||||||
Self::migrate(data_dir, xprivkey, network)?
|
|
||||||
}
|
|
||||||
err => err?,
|
err => err?,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -192,7 +191,7 @@ impl Wallet {
|
|||||||
|
|
||||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||||
}
|
}
|
||||||
});
|
}.instrument(debug_span!("BitcoinWalletSubscription")));
|
||||||
|
|
||||||
Subscription {
|
Subscription {
|
||||||
receiver,
|
receiver,
|
||||||
@ -274,7 +273,7 @@ impl Subscription {
|
|||||||
|
|
||||||
pub async fn wait_until_confirmed_with<T>(&self, target: T) -> Result<()>
|
pub async fn wait_until_confirmed_with<T>(&self, target: T) -> Result<()>
|
||||||
where
|
where
|
||||||
u32: PartialOrd<T>,
|
T: Into<u32>,
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
self.wait_until(|status| status.is_confirmed_with(target))
|
self.wait_until(|status| status.is_confirmed_with(target))
|
||||||
@ -469,6 +468,7 @@ where
|
|||||||
) -> Result<bitcoin::Amount> {
|
) -> Result<bitcoin::Amount> {
|
||||||
let client = self.client.lock().await;
|
let client = self.client.lock().await;
|
||||||
let fee_rate = client.estimate_feerate(self.target_block)?;
|
let fee_rate = client.estimate_feerate(self.target_block)?;
|
||||||
|
|
||||||
let min_relay_fee = client.min_relay_fee()?;
|
let min_relay_fee = client.min_relay_fee()?;
|
||||||
|
|
||||||
estimate_fee(weight, transfer_amount, fee_rate, min_relay_fee)
|
estimate_fee(weight, transfer_amount, fee_rate, min_relay_fee)
|
||||||
@ -761,9 +761,10 @@ impl Client {
|
|||||||
self.blockchain.get_tx(txid)
|
self.blockchain.get_tx(txid)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_state(&mut self) -> Result<()> {
|
fn update_state(&mut self, force_sync: bool) -> Result<()> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
if now < self.last_sync + self.sync_interval {
|
|
||||||
|
if !force_sync && now < self.last_sync + self.sync_interval {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -783,9 +784,14 @@ impl Client {
|
|||||||
|
|
||||||
if !self.script_history.contains_key(&script) {
|
if !self.script_history.contains_key(&script) {
|
||||||
self.script_history.insert(script.clone(), vec![]);
|
self.script_history.insert(script.clone(), vec![]);
|
||||||
}
|
|
||||||
|
|
||||||
self.update_state()?;
|
// When we first subscribe to a script we want to immediately fetch its status
|
||||||
|
// Otherwise we would have to wait for the next sync interval, which can take a minute
|
||||||
|
// This would result in potentially inaccurate status updates until that next sync interval is hit
|
||||||
|
self.update_state(true)?;
|
||||||
|
} else {
|
||||||
|
self.update_state(false)?;
|
||||||
|
}
|
||||||
|
|
||||||
let history = self.script_history.entry(script).or_default();
|
let history = self.script_history.entry(script).or_default();
|
||||||
|
|
||||||
@ -866,6 +872,11 @@ impl EstimateFeeRate for Client {
|
|||||||
// https://github.com/romanz/electrs/blob/f9cf5386d1b5de6769ee271df5eef324aa9491bc/src/rpc.rs#L213
|
// https://github.com/romanz/electrs/blob/f9cf5386d1b5de6769ee271df5eef324aa9491bc/src/rpc.rs#L213
|
||||||
// Returned estimated fees are per BTC/kb.
|
// Returned estimated fees are per BTC/kb.
|
||||||
let fee_per_byte = self.electrum.estimate_fee(target_block)?;
|
let fee_per_byte = self.electrum.estimate_fee(target_block)?;
|
||||||
|
|
||||||
|
if fee_per_byte < 0.0 {
|
||||||
|
bail!("Fee per byte returned by electrum server is negative: {}. This may indicate that fee estimation is not supported by this server", fee_per_byte);
|
||||||
|
}
|
||||||
|
|
||||||
// we do not expect fees being that high.
|
// we do not expect fees being that high.
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
Ok(FeeRate::from_btc_per_kvb(fee_per_byte as f32))
|
Ok(FeeRate::from_btc_per_kvb(fee_per_byte as f32))
|
||||||
@ -927,9 +938,20 @@ impl Confirmed {
|
|||||||
|
|
||||||
pub fn meets_target<T>(&self, target: T) -> bool
|
pub fn meets_target<T>(&self, target: T) -> bool
|
||||||
where
|
where
|
||||||
u32: PartialOrd<T>,
|
T: Into<u32>,
|
||||||
{
|
{
|
||||||
self.confirmations() >= target
|
self.confirmations() >= target.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn blocks_left_until<T>(&self, target: T) -> u32
|
||||||
|
where
|
||||||
|
T: Into<u32> + Copy,
|
||||||
|
{
|
||||||
|
if self.meets_target(target) {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
target.into() - self.confirmations()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -942,7 +964,7 @@ impl ScriptStatus {
|
|||||||
/// Check if the script has met the given confirmation target.
|
/// Check if the script has met the given confirmation target.
|
||||||
pub fn is_confirmed_with<T>(&self, target: T) -> bool
|
pub fn is_confirmed_with<T>(&self, target: T) -> bool
|
||||||
where
|
where
|
||||||
u32: PartialOrd<T>,
|
T: Into<u32>,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
ScriptStatus::Confirmed(inner) => inner.meets_target(target),
|
ScriptStatus::Confirmed(inner) => inner.meets_target(target),
|
||||||
@ -950,6 +972,17 @@ impl ScriptStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate the number of blocks left until the target is met.
|
||||||
|
pub fn blocks_left_until<T>(&self, target: T) -> u32
|
||||||
|
where
|
||||||
|
T: Into<u32> + Copy,
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
ScriptStatus::Confirmed(inner) => inner.blocks_left_until(target),
|
||||||
|
_ => target.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn has_been_seen(&self) -> bool {
|
pub fn has_been_seen(&self) -> bool {
|
||||||
matches!(self, ScriptStatus::InMempool | ScriptStatus::Confirmed(_))
|
matches!(self, ScriptStatus::InMempool | ScriptStatus::Confirmed(_))
|
||||||
}
|
}
|
||||||
@ -981,7 +1014,7 @@ mod tests {
|
|||||||
fn given_depth_0_should_meet_confirmation_target_one() {
|
fn given_depth_0_should_meet_confirmation_target_one() {
|
||||||
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
|
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
|
||||||
|
|
||||||
let confirmed = script.is_confirmed_with(1);
|
let confirmed = script.is_confirmed_with(1_u32);
|
||||||
|
|
||||||
assert!(confirmed)
|
assert!(confirmed)
|
||||||
}
|
}
|
||||||
@ -990,7 +1023,7 @@ mod tests {
|
|||||||
fn given_confirmations_1_should_meet_confirmation_target_one() {
|
fn given_confirmations_1_should_meet_confirmation_target_one() {
|
||||||
let script = ScriptStatus::from_confirmations(1);
|
let script = ScriptStatus::from_confirmations(1);
|
||||||
|
|
||||||
let confirmed = script.is_confirmed_with(1);
|
let confirmed = script.is_confirmed_with(1_u32);
|
||||||
|
|
||||||
assert!(confirmed)
|
assert!(confirmed)
|
||||||
}
|
}
|
||||||
@ -1005,6 +1038,33 @@ mod tests {
|
|||||||
assert_eq!(confirmed.depth, 0)
|
assert_eq!(confirmed.depth, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn given_depth_0_should_return_0_blocks_left_until_1() {
|
||||||
|
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
|
||||||
|
|
||||||
|
let blocks_left = script.blocks_left_until(1_u32);
|
||||||
|
|
||||||
|
assert_eq!(blocks_left, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn given_depth_1_should_return_0_blocks_left_until_1() {
|
||||||
|
let script = ScriptStatus::Confirmed(Confirmed { depth: 1 });
|
||||||
|
|
||||||
|
let blocks_left = script.blocks_left_until(1_u32);
|
||||||
|
|
||||||
|
assert_eq!(blocks_left, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn given_depth_0_should_return_1_blocks_left_until_2() {
|
||||||
|
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
|
||||||
|
|
||||||
|
let blocks_left = script.blocks_left_until(2_u32);
|
||||||
|
|
||||||
|
assert_eq!(blocks_left, 1)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn given_one_BTC_and_100k_sats_per_vb_fees_should_not_hit_max() {
|
fn given_one_BTC_and_100k_sats_per_vb_fees_should_not_hit_max() {
|
||||||
// 400 weight = 100 vbyte
|
// 400 weight = 100 vbyte
|
||||||
|
@ -3,7 +3,6 @@ pub mod cancel_and_refund;
|
|||||||
pub mod command;
|
pub mod command;
|
||||||
mod event_loop;
|
mod event_loop;
|
||||||
mod list_sellers;
|
mod list_sellers;
|
||||||
pub mod tracing;
|
|
||||||
pub mod transport;
|
pub mod transport;
|
||||||
|
|
||||||
pub use behaviour::{Behaviour, OutEvent};
|
pub use behaviour::{Behaviour, OutEvent};
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
|
use crate::monero::Scalar;
|
||||||
|
use crate::network::cooperative_xmr_redeem_after_punish::CooperativeXmrRedeemRejectReason;
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::network::rendezvous::XmrBtcNamespace;
|
use crate::network::rendezvous::XmrBtcNamespace;
|
||||||
use crate::network::swap_setup::bob;
|
use crate::network::swap_setup::bob;
|
||||||
use crate::network::{encrypted_signature, quote, redial, transfer_proof};
|
use crate::network::{
|
||||||
|
cooperative_xmr_redeem_after_punish, encrypted_signature, quote, redial, transfer_proof,
|
||||||
|
};
|
||||||
use crate::protocol::bob::State2;
|
use crate::protocol::bob::State2;
|
||||||
use crate::{bitcoin, env};
|
use crate::{bitcoin, env};
|
||||||
use anyhow::{anyhow, Error, Result};
|
use anyhow::{anyhow, Error, Result};
|
||||||
@ -28,6 +32,16 @@ pub enum OutEvent {
|
|||||||
EncryptedSignatureAcknowledged {
|
EncryptedSignatureAcknowledged {
|
||||||
id: RequestId,
|
id: RequestId,
|
||||||
},
|
},
|
||||||
|
CooperativeXmrRedeemFulfilled {
|
||||||
|
id: RequestId,
|
||||||
|
s_a: Scalar,
|
||||||
|
swap_id: uuid::Uuid,
|
||||||
|
},
|
||||||
|
CooperativeXmrRedeemRejected {
|
||||||
|
id: RequestId,
|
||||||
|
reason: CooperativeXmrRedeemRejectReason,
|
||||||
|
swap_id: uuid::Uuid,
|
||||||
|
},
|
||||||
AllRedialAttemptsExhausted {
|
AllRedialAttemptsExhausted {
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
},
|
},
|
||||||
@ -64,6 +78,7 @@ pub struct Behaviour {
|
|||||||
pub quote: quote::Behaviour,
|
pub quote: quote::Behaviour,
|
||||||
pub swap_setup: bob::Behaviour,
|
pub swap_setup: bob::Behaviour,
|
||||||
pub transfer_proof: transfer_proof::Behaviour,
|
pub transfer_proof: transfer_proof::Behaviour,
|
||||||
|
pub cooperative_xmr_redeem: cooperative_xmr_redeem_after_punish::Behaviour,
|
||||||
pub encrypted_signature: encrypted_signature::Behaviour,
|
pub encrypted_signature: encrypted_signature::Behaviour,
|
||||||
pub redial: redial::Behaviour,
|
pub redial: redial::Behaviour,
|
||||||
pub identify: Identify,
|
pub identify: Identify,
|
||||||
@ -91,6 +106,7 @@ impl Behaviour {
|
|||||||
swap_setup: bob::Behaviour::new(env_config, bitcoin_wallet),
|
swap_setup: bob::Behaviour::new(env_config, bitcoin_wallet),
|
||||||
transfer_proof: transfer_proof::bob(),
|
transfer_proof: transfer_proof::bob(),
|
||||||
encrypted_signature: encrypted_signature::bob(),
|
encrypted_signature: encrypted_signature::bob(),
|
||||||
|
cooperative_xmr_redeem: cooperative_xmr_redeem_after_punish::bob(),
|
||||||
redial: redial::Behaviour::new(alice, Duration::from_secs(2)),
|
redial: redial::Behaviour::new(alice, Duration::from_secs(2)),
|
||||||
ping: Ping::new(PingConfig::new().with_keep_alive(true)),
|
ping: Ping::new(PingConfig::new().with_keep_alive(true)),
|
||||||
identify: Identify::new(identifyConfig),
|
identify: Identify::new(identifyConfig),
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::bitcoin::wallet::Subscription;
|
use crate::bitcoin::{ExpiredTimelocks, Wallet};
|
||||||
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Wallet};
|
|
||||||
use crate::protocol::bob::BobState;
|
use crate::protocol::bob::BobState;
|
||||||
use crate::protocol::Database;
|
use crate::protocol::Database;
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
@ -10,10 +9,10 @@ use uuid::Uuid;
|
|||||||
pub async fn cancel_and_refund(
|
pub async fn cancel_and_refund(
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Arc<dyn Database>,
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<BobState> {
|
) -> Result<BobState> {
|
||||||
if let Err(err) = cancel(swap_id, bitcoin_wallet.clone(), db.clone()).await {
|
if let Err(err) = cancel(swap_id, bitcoin_wallet.clone(), db.clone()).await {
|
||||||
tracing::info!(%err, "Could not submit cancel transaction");
|
tracing::warn!(%err, "Could not cancel swap. Attempting to refund anyway");
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = match refund(swap_id, bitcoin_wallet, db).await {
|
let state = match refund(swap_id, bitcoin_wallet, db).await {
|
||||||
@ -21,20 +20,27 @@ pub async fn cancel_and_refund(
|
|||||||
Err(e) => bail!(e),
|
Err(e) => bail!(e),
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing::info!("Refund transaction submitted");
|
|
||||||
Ok(state)
|
Ok(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cancel(
|
pub async fn cancel(
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Arc<dyn Database>,
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<(Txid, Subscription, BobState)> {
|
) -> Result<(Txid, BobState)> {
|
||||||
let state = db.get_state(swap_id).await?.try_into()?;
|
let state = db.get_state(swap_id).await?.try_into()?;
|
||||||
|
|
||||||
let state6 = match state {
|
let state6 = match state {
|
||||||
BobState::BtcLocked { state3, .. } => state3.cancel(),
|
BobState::BtcLocked {
|
||||||
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
|
state3,
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
|
..
|
||||||
|
} => state3.cancel(monero_wallet_restore_blockheight),
|
||||||
|
BobState::XmrLockProofReceived {
|
||||||
|
state,
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
|
..
|
||||||
|
} => state.cancel(monero_wallet_restore_blockheight),
|
||||||
BobState::XmrLocked(state4) => state4.cancel(),
|
BobState::XmrLocked(state4) => state4.cancel(),
|
||||||
BobState::EncSigSent(state4) => state4.cancel(),
|
BobState::EncSigSent(state4) => state4.cancel(),
|
||||||
BobState::CancelTimelockExpired(state6) => state6,
|
BobState::CancelTimelockExpired(state6) => state6,
|
||||||
@ -47,46 +53,89 @@ pub async fn cancel(
|
|||||||
| BobState::XmrRedeemed { .. }
|
| BobState::XmrRedeemed { .. }
|
||||||
| BobState::BtcPunished { .. }
|
| BobState::BtcPunished { .. }
|
||||||
| BobState::SafelyAborted => bail!(
|
| BobState::SafelyAborted => bail!(
|
||||||
"Cannot cancel swap {} because it is in state {} which is not refundable.",
|
"Cannot cancel swap {} because it is in state {} which is not cancellable.",
|
||||||
swap_id,
|
swap_id,
|
||||||
state
|
state
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing::info!(%swap_id, "Manually cancelling swap");
|
tracing::info!(%swap_id, "Attempting to manually cancel swap");
|
||||||
|
|
||||||
let (txid, subscription) = match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
// Attempt to just publish the cancel transaction
|
||||||
Ok(txid) => txid,
|
match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
||||||
|
Ok((txid, _)) => {
|
||||||
|
let state = BobState::BtcCancelled(state6);
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
Ok((txid, state))
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we fail to submit the cancel transaction it can have one of two reasons:
|
||||||
|
// 1. The cancel timelock hasn't expired yet
|
||||||
|
// 2. The cancel transaction has already been published by Alice
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if let Ok(error_code) = parse_rpc_error_code(&err) {
|
// Check if Alice has already published the cancel transaction while we were absent
|
||||||
tracing::debug!(%error_code, "parse rpc error");
|
if let Ok(tx) = state6.check_for_tx_cancel(bitcoin_wallet.as_ref()).await {
|
||||||
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
|
let state = BobState::BtcCancelled(state6);
|
||||||
tracing::info!("Cancel transaction has already been confirmed on chain");
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
} else if error_code == i64::from(RpcErrorCode::RpcVerifyError) {
|
.await?;
|
||||||
tracing::info!("General error trying to submit cancel transaction");
|
tracing::info!("Alice has already cancelled the swap");
|
||||||
|
return Ok((tx.txid(), state));
|
||||||
|
}
|
||||||
|
|
||||||
|
// The cancel transaction has not been published yet and we failed to publish it ourselves
|
||||||
|
// Here we try to figure out why
|
||||||
|
match state6.expired_timelock(bitcoin_wallet.as_ref()).await {
|
||||||
|
// We cannot cancel because Alice has already cancelled and punished afterwards
|
||||||
|
Ok(ExpiredTimelocks::Punish { .. }) => {
|
||||||
|
let state = BobState::BtcPunished {
|
||||||
|
state: state6.clone(),
|
||||||
|
tx_lock_id: state6.tx_lock_id(),
|
||||||
|
};
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
tracing::info!("You have been punished for not refunding in time");
|
||||||
|
bail!(err.context("Cannot cancel swap because we have already been punished"));
|
||||||
|
}
|
||||||
|
// We cannot cancel because the cancel timelock has not expired yet
|
||||||
|
Ok(ExpiredTimelocks::None { blocks_left }) => {
|
||||||
|
bail!(err.context(
|
||||||
|
format!(
|
||||||
|
"Cannot cancel swap because the cancel timelock has not expired yet. Blocks left: {}",
|
||||||
|
blocks_left
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(ExpiredTimelocks::Cancel { .. }) => {
|
||||||
|
bail!(err.context("Failed to cancel swap even though cancel timelock has expired. This is unexpected."));
|
||||||
|
}
|
||||||
|
Err(timelock_err) => {
|
||||||
|
bail!(err
|
||||||
|
.context(timelock_err)
|
||||||
|
.context("Failed to cancel swap and could not check timelock status"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bail!(err);
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
let state = BobState::BtcCancelled(state6);
|
|
||||||
db.insert_latest_state(swap_id, state.clone().into())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok((txid, subscription, state))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refund(
|
pub async fn refund(
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Arc<dyn Database>,
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<BobState> {
|
) -> Result<BobState> {
|
||||||
let state = db.get_state(swap_id).await?.try_into()?;
|
let state = db.get_state(swap_id).await?.try_into()?;
|
||||||
|
|
||||||
let state6 = match state {
|
let state6 = match state {
|
||||||
BobState::BtcLocked { state3, .. } => state3.cancel(),
|
BobState::BtcLocked {
|
||||||
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
|
state3,
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
|
} => state3.cancel(monero_wallet_restore_blockheight),
|
||||||
|
BobState::XmrLockProofReceived {
|
||||||
|
state,
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
|
..
|
||||||
|
} => state.cancel(monero_wallet_restore_blockheight),
|
||||||
BobState::XmrLocked(state4) => state4.cancel(),
|
BobState::XmrLocked(state4) => state4.cancel(),
|
||||||
BobState::EncSigSent(state4) => state4.cancel(),
|
BobState::EncSigSent(state4) => state4.cancel(),
|
||||||
BobState::CancelTimelockExpired(state6) => state6,
|
BobState::CancelTimelockExpired(state6) => state6,
|
||||||
@ -104,12 +153,52 @@ pub async fn refund(
|
|||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing::info!(%swap_id, "Manually refunding swap");
|
tracing::info!(%swap_id, "Attempting to manually refund swap");
|
||||||
state6.publish_refund_btc(bitcoin_wallet.as_ref()).await?;
|
|
||||||
|
|
||||||
let state = BobState::BtcRefunded(state6);
|
// Attempt to just publish the refund transaction
|
||||||
db.insert_latest_state(swap_id, state.clone().into())
|
match state6.publish_refund_btc(bitcoin_wallet.as_ref()).await {
|
||||||
.await?;
|
Ok(_) => {
|
||||||
|
let state = BobState::BtcRefunded(state6);
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(state)
|
Ok(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we fail to submit the refund transaction it can have one of two reasons:
|
||||||
|
// 1. The cancel transaction has not been published yet
|
||||||
|
// 2. The refund timelock has already expired and we have been punished
|
||||||
|
Err(bitcoin_publication_err) => {
|
||||||
|
match state6.expired_timelock(bitcoin_wallet.as_ref()).await {
|
||||||
|
// We have been punished
|
||||||
|
Ok(ExpiredTimelocks::Punish { .. }) => {
|
||||||
|
let state = BobState::BtcPunished {
|
||||||
|
state: state6.clone(),
|
||||||
|
tx_lock_id: state6.tx_lock_id(),
|
||||||
|
};
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
tracing::info!("You have been punished for not refunding in time");
|
||||||
|
bail!(bitcoin_publication_err
|
||||||
|
.context("Cannot refund swap because we have already been punished"));
|
||||||
|
}
|
||||||
|
Ok(ExpiredTimelocks::None { blocks_left }) => {
|
||||||
|
bail!(
|
||||||
|
bitcoin_publication_err.context(format!(
|
||||||
|
"Cannot refund swap because the cancel timelock has not expired yet. Blocks left: {}",
|
||||||
|
blocks_left
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(ExpiredTimelocks::Cancel { .. }) => {
|
||||||
|
bail!(bitcoin_publication_err.context("Failed to refund swap even though cancel timelock has expired. This is unexpected."));
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
bail!(bitcoin_publication_err
|
||||||
|
.context(e)
|
||||||
|
.context("Failed to refund swap and could not check timelock status"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,12 @@
|
|||||||
use crate::bitcoin::EncryptedSignature;
|
use crate::bitcoin::EncryptedSignature;
|
||||||
use crate::cli::behaviour::{Behaviour, OutEvent};
|
use crate::cli::behaviour::{Behaviour, OutEvent};
|
||||||
use crate::monero;
|
use crate::monero;
|
||||||
|
use crate::network::cooperative_xmr_redeem_after_punish::{Request, Response};
|
||||||
use crate::network::encrypted_signature;
|
use crate::network::encrypted_signature;
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::network::swap_setup::bob::NewSwap;
|
use crate::network::swap_setup::bob::NewSwap;
|
||||||
use crate::protocol::bob::State2;
|
use crate::protocol::bob::State2;
|
||||||
|
use crate::protocol::Database;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use futures::future::{BoxFuture, OptionFuture};
|
use futures::future::{BoxFuture, OptionFuture};
|
||||||
use futures::{FutureExt, StreamExt};
|
use futures::{FutureExt, StreamExt};
|
||||||
@ -13,6 +15,7 @@ use libp2p::swarm::dial_opts::DialOpts;
|
|||||||
use libp2p::swarm::SwarmEvent;
|
use libp2p::swarm::SwarmEvent;
|
||||||
use libp2p::{PeerId, Swarm};
|
use libp2p::{PeerId, Swarm};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -21,9 +24,11 @@ pub struct EventLoop {
|
|||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
swarm: libp2p::Swarm<Behaviour>,
|
swarm: libp2p::Swarm<Behaviour>,
|
||||||
alice_peer_id: PeerId,
|
alice_peer_id: PeerId,
|
||||||
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
|
|
||||||
// these streams represents outgoing requests that we have to make
|
// these streams represents outgoing requests that we have to make
|
||||||
quote_requests: bmrng::RequestReceiverStream<(), BidQuote>,
|
quote_requests: bmrng::RequestReceiverStream<(), BidQuote>,
|
||||||
|
cooperative_xmr_redeem_requests: bmrng::RequestReceiverStream<Uuid, Response>,
|
||||||
encrypted_signatures: bmrng::RequestReceiverStream<EncryptedSignature, ()>,
|
encrypted_signatures: bmrng::RequestReceiverStream<EncryptedSignature, ()>,
|
||||||
swap_setup_requests: bmrng::RequestReceiverStream<NewSwap, Result<State2>>,
|
swap_setup_requests: bmrng::RequestReceiverStream<NewSwap, Result<State2>>,
|
||||||
|
|
||||||
@ -33,7 +38,7 @@ pub struct EventLoop {
|
|||||||
inflight_quote_requests: HashMap<RequestId, bmrng::Responder<BidQuote>>,
|
inflight_quote_requests: HashMap<RequestId, bmrng::Responder<BidQuote>>,
|
||||||
inflight_encrypted_signature_requests: HashMap<RequestId, bmrng::Responder<()>>,
|
inflight_encrypted_signature_requests: HashMap<RequestId, bmrng::Responder<()>>,
|
||||||
inflight_swap_setup: Option<bmrng::Responder<Result<State2>>>,
|
inflight_swap_setup: Option<bmrng::Responder<Result<State2>>>,
|
||||||
|
inflight_cooperative_xmr_redeem_requests: HashMap<RequestId, bmrng::Responder<Response>>,
|
||||||
/// The sender we will use to relay incoming transfer proofs.
|
/// The sender we will use to relay incoming transfer proofs.
|
||||||
transfer_proof: bmrng::RequestSender<monero::TransferProof, ()>,
|
transfer_proof: bmrng::RequestSender<monero::TransferProof, ()>,
|
||||||
/// The future representing the successful handling of an incoming transfer
|
/// The future representing the successful handling of an incoming transfer
|
||||||
@ -51,12 +56,13 @@ impl EventLoop {
|
|||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
swarm: Swarm<Behaviour>,
|
swarm: Swarm<Behaviour>,
|
||||||
alice_peer_id: PeerId,
|
alice_peer_id: PeerId,
|
||||||
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<(Self, EventLoopHandle)> {
|
) -> Result<(Self, EventLoopHandle)> {
|
||||||
let execution_setup = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
let execution_setup = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
||||||
let transfer_proof = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
let transfer_proof = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
||||||
let encrypted_signature = bmrng::channel(1);
|
let encrypted_signature = bmrng::channel(1);
|
||||||
let quote = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
let quote = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
||||||
|
let cooperative_xmr_redeem = bmrng::channel_with_timeout(1, Duration::from_secs(60));
|
||||||
let event_loop = EventLoop {
|
let event_loop = EventLoop {
|
||||||
swap_id,
|
swap_id,
|
||||||
swarm,
|
swarm,
|
||||||
@ -64,17 +70,21 @@ impl EventLoop {
|
|||||||
swap_setup_requests: execution_setup.1.into(),
|
swap_setup_requests: execution_setup.1.into(),
|
||||||
transfer_proof: transfer_proof.0,
|
transfer_proof: transfer_proof.0,
|
||||||
encrypted_signatures: encrypted_signature.1.into(),
|
encrypted_signatures: encrypted_signature.1.into(),
|
||||||
|
cooperative_xmr_redeem_requests: cooperative_xmr_redeem.1.into(),
|
||||||
quote_requests: quote.1.into(),
|
quote_requests: quote.1.into(),
|
||||||
inflight_quote_requests: HashMap::default(),
|
inflight_quote_requests: HashMap::default(),
|
||||||
inflight_swap_setup: None,
|
inflight_swap_setup: None,
|
||||||
inflight_encrypted_signature_requests: HashMap::default(),
|
inflight_encrypted_signature_requests: HashMap::default(),
|
||||||
|
inflight_cooperative_xmr_redeem_requests: HashMap::default(),
|
||||||
pending_transfer_proof: OptionFuture::from(None),
|
pending_transfer_proof: OptionFuture::from(None),
|
||||||
|
db,
|
||||||
};
|
};
|
||||||
|
|
||||||
let handle = EventLoopHandle {
|
let handle = EventLoopHandle {
|
||||||
swap_setup: execution_setup.0,
|
swap_setup: execution_setup.0,
|
||||||
transfer_proof: transfer_proof.1,
|
transfer_proof: transfer_proof.1,
|
||||||
encrypted_signature: encrypted_signature.0,
|
encrypted_signature: encrypted_signature.0,
|
||||||
|
cooperative_xmr_redeem: cooperative_xmr_redeem.0,
|
||||||
quote: quote.0,
|
quote: quote.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -108,71 +118,106 @@ impl EventLoop {
|
|||||||
SwarmEvent::Behaviour(OutEvent::TransferProofReceived { msg, channel, peer }) => {
|
SwarmEvent::Behaviour(OutEvent::TransferProofReceived { msg, channel, peer }) => {
|
||||||
let swap_id = msg.swap_id;
|
let swap_id = msg.swap_id;
|
||||||
|
|
||||||
if peer != self.alice_peer_id {
|
if swap_id == self.swap_id {
|
||||||
tracing::warn!(
|
if peer != self.alice_peer_id {
|
||||||
%swap_id,
|
tracing::warn!(
|
||||||
"Ignoring malicious transfer proof from {}, expected to receive it from {}",
|
%swap_id,
|
||||||
peer,
|
"Ignoring malicious transfer proof from {}, expected to receive it from {}",
|
||||||
self.alice_peer_id);
|
peer,
|
||||||
continue;
|
self.alice_peer_id);
|
||||||
}
|
continue;
|
||||||
|
|
||||||
if swap_id != self.swap_id {
|
|
||||||
|
|
||||||
// TODO: Save unexpected transfer proofs in the database and check for messages in the database when handling swaps
|
|
||||||
tracing::warn!("Received unexpected transfer proof for swap {} while running swap {}. This transfer proof will be ignored", swap_id, self.swap_id);
|
|
||||||
|
|
||||||
// When receiving a transfer proof that is unexpected we still have to acknowledge that it was received
|
|
||||||
let _ = self.swarm.behaviour_mut().transfer_proof.send_response(channel, ());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut responder = match self.transfer_proof.send(msg.tx_lock_proof).await {
|
|
||||||
Ok(responder) => responder,
|
|
||||||
Err(e) => {
|
|
||||||
tracing::warn!("Failed to pass on transfer proof: {:#}", e);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
self.pending_transfer_proof = OptionFuture::from(Some(async move {
|
let mut responder = match self.transfer_proof.send(msg.tx_lock_proof).await {
|
||||||
let _ = responder.recv().await;
|
Ok(responder) => responder,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to pass on transfer proof: {:#}", e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
channel
|
self.pending_transfer_proof = OptionFuture::from(Some(async move {
|
||||||
}.boxed()));
|
let _ = responder.recv().await;
|
||||||
|
|
||||||
|
channel
|
||||||
|
}.boxed()));
|
||||||
|
}else {
|
||||||
|
// Check if the transfer proof is sent from the correct peer and if we have a record of the swap
|
||||||
|
match self.db.get_peer_id(swap_id).await {
|
||||||
|
// We have a record of the swap
|
||||||
|
Ok(buffer_swap_alice_peer_id) => {
|
||||||
|
if buffer_swap_alice_peer_id == self.alice_peer_id {
|
||||||
|
// Save transfer proof in the database such that we can process it later when we resume the swap
|
||||||
|
match self.db.insert_buffered_transfer_proof(swap_id, msg.tx_lock_proof).await {
|
||||||
|
Ok(_) => {
|
||||||
|
tracing::info!("Received transfer proof for swap {} while running swap {}. Buffering this transfer proof in the database for later retrieval", swap_id, self.swap_id);
|
||||||
|
let _ = self.swarm.behaviour_mut().transfer_proof.send_response(channel, ());
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to buffer transfer proof for swap {}: {:#}", swap_id, e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}else {
|
||||||
|
tracing::warn!(
|
||||||
|
%swap_id,
|
||||||
|
"Ignoring malicious transfer proof from {}, expected to receive it from {}",
|
||||||
|
self.swap_id,
|
||||||
|
buffer_swap_alice_peer_id);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// We do not have a record of the swap or an error occurred while retrieving the peer id of Alice
|
||||||
|
Err(e) => {
|
||||||
|
if let Some(sqlx::Error::RowNotFound) = e.downcast_ref::<sqlx::Error>() {
|
||||||
|
tracing::warn!("Ignoring transfer proof for swap {} while running swap {}. We do not have a record of this swap", swap_id, self.swap_id);
|
||||||
|
} else {
|
||||||
|
tracing::error!("Ignoring transfer proof for swap {} while running swap {}. Failed to retrieve the peer id of Alice for the corresponding swap: {:#}", swap_id, self.swap_id, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::EncryptedSignatureAcknowledged { id }) => {
|
SwarmEvent::Behaviour(OutEvent::EncryptedSignatureAcknowledged { id }) => {
|
||||||
if let Some(responder) = self.inflight_encrypted_signature_requests.remove(&id) {
|
if let Some(responder) = self.inflight_encrypted_signature_requests.remove(&id) {
|
||||||
let _ = responder.respond(());
|
let _ = responder.respond(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SwarmEvent::Behaviour(OutEvent::CooperativeXmrRedeemFulfilled { id, swap_id, s_a }) => {
|
||||||
|
if let Some(responder) = self.inflight_cooperative_xmr_redeem_requests.remove(&id) {
|
||||||
|
let _ = responder.respond(Response::Fullfilled { s_a, swap_id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SwarmEvent::Behaviour(OutEvent::CooperativeXmrRedeemRejected { id, swap_id, reason }) => {
|
||||||
|
if let Some(responder) = self.inflight_cooperative_xmr_redeem_requests.remove(&id) {
|
||||||
|
let _ = responder.respond(Response::Rejected { reason, swap_id });
|
||||||
|
}
|
||||||
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::AllRedialAttemptsExhausted { peer }) if peer == self.alice_peer_id => {
|
SwarmEvent::Behaviour(OutEvent::AllRedialAttemptsExhausted { peer }) if peer == self.alice_peer_id => {
|
||||||
tracing::error!("Exhausted all re-dial attempts to Alice");
|
tracing::error!("Exhausted all re-dial attempts to Alice");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::Failure { peer, error }) => {
|
SwarmEvent::Behaviour(OutEvent::Failure { peer, error }) => {
|
||||||
tracing::warn!(%peer, "Communication error: {:#}", error);
|
tracing::warn!(%peer, err = %error, "Communication error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } if peer_id == self.alice_peer_id => {
|
SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } if peer_id == self.alice_peer_id => {
|
||||||
tracing::info!("Connected to Alice at {}", endpoint.get_remote_address());
|
tracing::info!(peer_id = %endpoint.get_remote_address(), "Connected to Alice");
|
||||||
}
|
}
|
||||||
SwarmEvent::Dialing(peer_id) if peer_id == self.alice_peer_id => {
|
SwarmEvent::Dialing(peer_id) if peer_id == self.alice_peer_id => {
|
||||||
tracing::debug!("Dialling Alice at {}", peer_id);
|
tracing::debug!(%peer_id, "Dialling Alice");
|
||||||
}
|
}
|
||||||
SwarmEvent::ConnectionClosed { peer_id, endpoint, num_established, cause: Some(error) } if peer_id == self.alice_peer_id && num_established == 0 => {
|
SwarmEvent::ConnectionClosed { peer_id, endpoint, num_established, cause: Some(error) } if peer_id == self.alice_peer_id && num_established == 0 => {
|
||||||
tracing::warn!("Lost connection to Alice at {}, cause: {}", endpoint.get_remote_address(), error);
|
tracing::warn!(peer_id = %endpoint.get_remote_address(), cause = %error, "Lost connection to Alice");
|
||||||
}
|
}
|
||||||
SwarmEvent::ConnectionClosed { peer_id, num_established, cause: None, .. } if peer_id == self.alice_peer_id && num_established == 0 => {
|
SwarmEvent::ConnectionClosed { peer_id, num_established, cause: None, .. } if peer_id == self.alice_peer_id && num_established == 0 => {
|
||||||
// no error means the disconnection was requested
|
// no error means the disconnection was requested
|
||||||
tracing::info!("Successfully closed connection to Alice");
|
tracing::info!("Successfully closed connection to Alice");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SwarmEvent::OutgoingConnectionError { peer_id, error } if matches!(peer_id, Some(alice_peer_id) if alice_peer_id == self.alice_peer_id) => {
|
SwarmEvent::OutgoingConnectionError { peer_id: Some(alice_peer_id), error } if alice_peer_id == self.alice_peer_id => {
|
||||||
tracing::warn!( "Failed to dial Alice: {}", error);
|
tracing::warn!(%error, "Failed to dial Alice");
|
||||||
|
|
||||||
if let Some(duration) = self.swarm.behaviour_mut().redial.until_next_redial() {
|
if let Some(duration) = self.swarm.behaviour_mut().redial.until_next_redial() {
|
||||||
tracing::info!("Next redial attempt in {}s", duration.as_secs());
|
tracing::info!(seconds_until_next_redial = %duration.as_secs(), "Waiting for next redial attempt");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -204,7 +249,14 @@ impl EventLoop {
|
|||||||
let _ = self.swarm.behaviour_mut().transfer_proof.send_response(response_channel, ());
|
let _ = self.swarm.behaviour_mut().transfer_proof.send_response(response_channel, ());
|
||||||
|
|
||||||
self.pending_transfer_proof = OptionFuture::from(None);
|
self.pending_transfer_proof = OptionFuture::from(None);
|
||||||
}
|
},
|
||||||
|
|
||||||
|
Some((swap_id, responder)) = self.cooperative_xmr_redeem_requests.next().fuse(), if self.is_connected_to_alice() => {
|
||||||
|
let id = self.swarm.behaviour_mut().cooperative_xmr_redeem.send_request(&self.alice_peer_id, Request {
|
||||||
|
swap_id
|
||||||
|
});
|
||||||
|
self.inflight_cooperative_xmr_redeem_requests.insert(id, responder);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -220,6 +272,7 @@ pub struct EventLoopHandle {
|
|||||||
transfer_proof: bmrng::RequestReceiver<monero::TransferProof, ()>,
|
transfer_proof: bmrng::RequestReceiver<monero::TransferProof, ()>,
|
||||||
encrypted_signature: bmrng::RequestSender<EncryptedSignature, ()>,
|
encrypted_signature: bmrng::RequestSender<EncryptedSignature, ()>,
|
||||||
quote: bmrng::RequestSender<(), BidQuote>,
|
quote: bmrng::RequestSender<(), BidQuote>,
|
||||||
|
cooperative_xmr_redeem: bmrng::RequestSender<Uuid, Response>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventLoopHandle {
|
impl EventLoopHandle {
|
||||||
@ -241,8 +294,12 @@ impl EventLoopHandle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn request_quote(&mut self) -> Result<BidQuote> {
|
pub async fn request_quote(&mut self) -> Result<BidQuote> {
|
||||||
|
tracing::debug!("Requesting quote");
|
||||||
Ok(self.quote.send_receive(()).await?)
|
Ok(self.quote.send_receive(()).await?)
|
||||||
}
|
}
|
||||||
|
pub async fn request_cooperative_xmr_redeem(&mut self, swap_id: Uuid) -> Result<Response> {
|
||||||
|
Ok(self.cooperative_xmr_redeem.send_receive(swap_id).await?)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn send_encrypted_signature(
|
pub async fn send_encrypted_signature(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
@ -1,145 +0,0 @@
|
|||||||
use anyhow::Result;
|
|
||||||
use std::option::Option::Some;
|
|
||||||
use std::path::Path;
|
|
||||||
use time::format_description::well_known::Rfc3339;
|
|
||||||
use tracing::subscriber::set_global_default;
|
|
||||||
use tracing::{Event, Level, Subscriber};
|
|
||||||
use tracing_subscriber::fmt::format::{DefaultFields, Format, JsonFields};
|
|
||||||
use tracing_subscriber::fmt::time::UtcTime;
|
|
||||||
use tracing_subscriber::layer::{Context, SubscriberExt};
|
|
||||||
use tracing_subscriber::{fmt, EnvFilter, FmtSubscriber, Layer, Registry};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>, swap_id: Option<Uuid>) -> Result<()> {
|
|
||||||
if let Some(swap_id) = swap_id {
|
|
||||||
let level_filter = EnvFilter::try_new("swap=debug")?;
|
|
||||||
|
|
||||||
let registry = Registry::default().with(level_filter);
|
|
||||||
|
|
||||||
let appender =
|
|
||||||
tracing_appender::rolling::never(dir.as_ref(), format!("swap-{}.log", swap_id));
|
|
||||||
let (appender, guard) = tracing_appender::non_blocking(appender);
|
|
||||||
|
|
||||||
std::mem::forget(guard);
|
|
||||||
|
|
||||||
let file_logger = registry.with(
|
|
||||||
fmt::layer()
|
|
||||||
.with_ansi(false)
|
|
||||||
.with_target(false)
|
|
||||||
.json()
|
|
||||||
.with_writer(appender),
|
|
||||||
);
|
|
||||||
|
|
||||||
if json && debug {
|
|
||||||
set_global_default(file_logger.with(debug_json_terminal_printer()))?;
|
|
||||||
} else if json && !debug {
|
|
||||||
set_global_default(file_logger.with(info_json_terminal_printer()))?;
|
|
||||||
} else if !json && debug {
|
|
||||||
set_global_default(file_logger.with(debug_terminal_printer()))?;
|
|
||||||
} else {
|
|
||||||
set_global_default(file_logger.with(info_terminal_printer()))?;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let level = if debug { Level::DEBUG } else { Level::INFO };
|
|
||||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
|
||||||
|
|
||||||
let builder = FmtSubscriber::builder()
|
|
||||||
.with_env_filter(format!("swap={}", level))
|
|
||||||
.with_writer(std::io::stderr)
|
|
||||||
.with_ansi(is_terminal)
|
|
||||||
.with_timer(UtcTime::rfc_3339())
|
|
||||||
.with_target(false);
|
|
||||||
|
|
||||||
if json {
|
|
||||||
builder.json().init();
|
|
||||||
} else {
|
|
||||||
builder.init();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
tracing::info!("Logging initialized to {}", dir.as_ref().display());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct StdErrPrinter<L> {
|
|
||||||
inner: L,
|
|
||||||
level: Level,
|
|
||||||
}
|
|
||||||
|
|
||||||
type StdErrLayer<S, T> = tracing_subscriber::fmt::Layer<
|
|
||||||
S,
|
|
||||||
DefaultFields,
|
|
||||||
Format<tracing_subscriber::fmt::format::Full, T>,
|
|
||||||
fn() -> std::io::Stderr,
|
|
||||||
>;
|
|
||||||
|
|
||||||
type StdErrJsonLayer<S, T> = tracing_subscriber::fmt::Layer<
|
|
||||||
S,
|
|
||||||
JsonFields,
|
|
||||||
Format<tracing_subscriber::fmt::format::Json, T>,
|
|
||||||
fn() -> std::io::Stderr,
|
|
||||||
>;
|
|
||||||
|
|
||||||
fn debug_terminal_printer<S>() -> StdErrPrinter<StdErrLayer<S, UtcTime<Rfc3339>>> {
|
|
||||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
|
||||||
StdErrPrinter {
|
|
||||||
inner: fmt::layer()
|
|
||||||
.with_ansi(is_terminal)
|
|
||||||
.with_target(false)
|
|
||||||
.with_timer(UtcTime::rfc_3339())
|
|
||||||
.with_writer(std::io::stderr),
|
|
||||||
level: Level::DEBUG,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn debug_json_terminal_printer<S>() -> StdErrPrinter<StdErrJsonLayer<S, UtcTime<Rfc3339>>> {
|
|
||||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
|
||||||
StdErrPrinter {
|
|
||||||
inner: fmt::layer()
|
|
||||||
.with_ansi(is_terminal)
|
|
||||||
.with_target(false)
|
|
||||||
.with_timer(UtcTime::rfc_3339())
|
|
||||||
.json()
|
|
||||||
.with_writer(std::io::stderr),
|
|
||||||
level: Level::DEBUG,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn info_terminal_printer<S>() -> StdErrPrinter<StdErrLayer<S, ()>> {
|
|
||||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
|
||||||
StdErrPrinter {
|
|
||||||
inner: fmt::layer()
|
|
||||||
.with_ansi(is_terminal)
|
|
||||||
.with_target(false)
|
|
||||||
.with_level(false)
|
|
||||||
.without_time()
|
|
||||||
.with_writer(std::io::stderr),
|
|
||||||
level: Level::INFO,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn info_json_terminal_printer<S>() -> StdErrPrinter<StdErrJsonLayer<S, ()>> {
|
|
||||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
|
||||||
StdErrPrinter {
|
|
||||||
inner: fmt::layer()
|
|
||||||
.with_ansi(is_terminal)
|
|
||||||
.with_target(false)
|
|
||||||
.with_level(false)
|
|
||||||
.without_time()
|
|
||||||
.json()
|
|
||||||
.with_writer(std::io::stderr),
|
|
||||||
level: Level::INFO,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<L, S> Layer<S> for StdErrPrinter<L>
|
|
||||||
where
|
|
||||||
L: 'static + Layer<S>,
|
|
||||||
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
|
||||||
{
|
|
||||||
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
|
|
||||||
if self.level.ge(event.metadata().level()) {
|
|
||||||
self.inner.on_event(event, ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
use anyhow::anyhow;
|
|
||||||
|
|
||||||
const LATEST_RELEASE_URL: &str = "https://github.com/comit-network/xmr-btc-swap/releases/latest";
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub enum Version {
|
|
||||||
Current,
|
|
||||||
Available,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check the latest release from GitHub API.
|
|
||||||
pub async fn check_latest_version(current_version: &str) -> anyhow::Result<Version> {
|
|
||||||
let response = reqwest::get(LATEST_RELEASE_URL).await?;
|
|
||||||
let e = "Failed to get latest release.";
|
|
||||||
let download_url = response.url();
|
|
||||||
let segments = download_url.path_segments().ok_or_else(|| anyhow!(e))?;
|
|
||||||
let latest_version = segments.last().ok_or_else(|| anyhow!(e))?;
|
|
||||||
|
|
||||||
let result = if is_latest_version(current_version, latest_version) {
|
|
||||||
Version::Current
|
|
||||||
} else {
|
|
||||||
tracing::warn!(%current_version, %latest_version, %download_url,
|
|
||||||
"You are not on the latest version",
|
|
||||||
);
|
|
||||||
Version::Available
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: naive implementation can be improved using semver
|
|
||||||
fn is_latest_version(current: &str, latest: &str) -> bool {
|
|
||||||
current == latest
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_compares_versions() {
|
|
||||||
assert!(is_latest_version("0.10.2", "0.10.2"));
|
|
||||||
assert!(!is_latest_version("0.10.2", "0.10.3"));
|
|
||||||
assert!(!is_latest_version("0.10.2", "0.11.0"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
#[ignore = "For local testing, makes http requests to github."]
|
|
||||||
async fn it_compares_with_github() {
|
|
||||||
let result = check_latest_version("0.11.0").await.unwrap();
|
|
||||||
assert_eq!(result, Version::Available);
|
|
||||||
|
|
||||||
let result = check_latest_version("0.11.1").await.unwrap();
|
|
||||||
assert_eq!(result, Version::Current);
|
|
||||||
}
|
|
||||||
}
|
|
221
swap/src/common/mod.rs
Normal file
221
swap/src/common/mod.rs
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
pub mod tracing_util;
|
||||||
|
|
||||||
|
use std::{collections::HashMap, path::PathBuf};
|
||||||
|
|
||||||
|
use anyhow::anyhow;
|
||||||
|
use tokio::{
|
||||||
|
fs::{read_dir, File},
|
||||||
|
io::{AsyncBufReadExt, BufReader},
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const LATEST_RELEASE_URL: &str = "https://github.com/comit-network/xmr-btc-swap/releases/latest";
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub enum Version {
|
||||||
|
Current,
|
||||||
|
Available,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check the latest release from GitHub API.
|
||||||
|
pub async fn check_latest_version(current_version: &str) -> anyhow::Result<Version> {
|
||||||
|
let response = reqwest::get(LATEST_RELEASE_URL).await?;
|
||||||
|
let e = "Failed to get latest release.";
|
||||||
|
let download_url = response.url();
|
||||||
|
let segments = download_url.path_segments().ok_or_else(|| anyhow!(e))?;
|
||||||
|
let latest_version = segments.last().ok_or_else(|| anyhow!(e))?;
|
||||||
|
|
||||||
|
let result = if is_latest_version(current_version, latest_version) {
|
||||||
|
Version::Current
|
||||||
|
} else {
|
||||||
|
tracing::warn!(%current_version, %latest_version, %download_url,
|
||||||
|
"You are not on the latest version",
|
||||||
|
);
|
||||||
|
Version::Available
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo: naive implementation can be improved using semver
|
||||||
|
fn is_latest_version(current: &str, latest: &str) -> bool {
|
||||||
|
current == latest
|
||||||
|
}
|
||||||
|
|
||||||
|
/// helper macro for [`redact`]... eldrich sorcery
|
||||||
|
/// the macro does in essence the following:
|
||||||
|
/// 1. create a static regex automaton for the pattern
|
||||||
|
/// 2. find all matching patterns using regex
|
||||||
|
/// 3. create a placeholder for each distinct matching pattern
|
||||||
|
/// 4. add the placeholder to the hashmap
|
||||||
|
macro_rules! regex_find_placeholders {
|
||||||
|
($pattern:expr, $create_placeholder:expr, $replacements:expr, $input:expr) => {{
|
||||||
|
// compile the regex pattern
|
||||||
|
static REGEX: once_cell::sync::Lazy<regex::Regex> = once_cell::sync::Lazy::new(|| {
|
||||||
|
tracing::debug!("initializing regex");
|
||||||
|
regex::Regex::new($pattern).expect("invalid regex pattern")
|
||||||
|
});
|
||||||
|
|
||||||
|
// keep count of count patterns to generate distinct placeholders
|
||||||
|
let mut counter: usize = 0;
|
||||||
|
|
||||||
|
// for every matched address check whether we already found it
|
||||||
|
// and if we didn't, generate a placeholder for it
|
||||||
|
for address in REGEX.find_iter($input) {
|
||||||
|
if !$replacements.contains_key(address.as_str()) {
|
||||||
|
#[allow(clippy::redundant_closure_call)]
|
||||||
|
$replacements.insert(address.as_str().to_owned(), $create_placeholder(counter));
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print the logs from the specified logs or from the default location
|
||||||
|
/// to the specified path or the terminal.
|
||||||
|
///
|
||||||
|
/// If specified, filter by swap id or redact addresses.
|
||||||
|
pub async fn get_logs(
|
||||||
|
logs_dir: PathBuf,
|
||||||
|
swap_id: Option<Uuid>,
|
||||||
|
redact_addresses: bool,
|
||||||
|
) -> anyhow::Result<Vec<String>> {
|
||||||
|
tracing::debug!("reading logfiles from {}", logs_dir.display());
|
||||||
|
|
||||||
|
// get all files in the directory
|
||||||
|
let mut log_files = read_dir(&logs_dir).await?;
|
||||||
|
|
||||||
|
let mut log_messages = Vec::new();
|
||||||
|
// when we redact we need to store the placeholder
|
||||||
|
let mut placeholders = HashMap::new();
|
||||||
|
|
||||||
|
// print all lines from every log file. TODO: sort files by date?
|
||||||
|
while let Some(entry) = log_files.next_entry().await? {
|
||||||
|
// get the file path
|
||||||
|
let file_path = entry.path();
|
||||||
|
|
||||||
|
// filter for .log files
|
||||||
|
let file_name = file_path
|
||||||
|
.file_name()
|
||||||
|
.and_then(|name| name.to_str())
|
||||||
|
.unwrap_or("");
|
||||||
|
|
||||||
|
if !file_name.ends_with(".log") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use BufReader to stay easy on memory and then read line by line
|
||||||
|
let buf_reader = BufReader::new(File::open(&file_path).await?);
|
||||||
|
let mut lines = buf_reader.lines();
|
||||||
|
|
||||||
|
// print each line, redacted if the flag is set
|
||||||
|
while let Some(line) = lines.next_line().await? {
|
||||||
|
// if we should filter by swap id, check if the line contains it
|
||||||
|
if let Some(swap_id) = swap_id {
|
||||||
|
// we only want lines which contain the swap id
|
||||||
|
if !line.contains(&swap_id.to_string()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// redact if necessary
|
||||||
|
let line = if redact_addresses {
|
||||||
|
redact_with(&line, &mut placeholders)
|
||||||
|
} else {
|
||||||
|
line
|
||||||
|
};
|
||||||
|
// save redacted message
|
||||||
|
log_messages.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(log_messages)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Redact logs, etc. by replacing Bitcoin and Monero addresses
|
||||||
|
/// with generic placeholders.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```rust
|
||||||
|
/// use swap::common::redact;
|
||||||
|
///
|
||||||
|
/// let redacted = redact("a9165a1e-d26d-4b56-bf6d-ca9658825c44");
|
||||||
|
/// assert_eq!(redacted, "<swap_id_0>");
|
||||||
|
/// ```
|
||||||
|
pub fn redact(input: &str) -> String {
|
||||||
|
let mut replacements = HashMap::new();
|
||||||
|
redact_with(input, &mut replacements)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same as [`redact`] but retrieves palceholders from and stores them
|
||||||
|
/// in a specified hashmap.
|
||||||
|
pub fn redact_with(input: &str, replacements: &mut HashMap<String, String>) -> String {
|
||||||
|
// TODO: verify regex patterns
|
||||||
|
const MONERO_ADDR_REGEX: &str = r#"[48][1-9A-HJ-NP-Za-km-z]{94}"#;
|
||||||
|
const BITCOIN_ADDR_REGEX: &str = r#"\b[13][a-km-zA-HJ-NP-Z1-9]{25,34}\b"#;
|
||||||
|
// Both XMR and BTC transactions have
|
||||||
|
// a 64 bit hex id so they aren't distinguishible
|
||||||
|
const TX_ID_REGEX: &str = r#"\b[a-fA-F0-9]{64}\b"#;
|
||||||
|
const SWAP_ID_REGEX: &str =
|
||||||
|
r#"\b[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}\b"#;
|
||||||
|
|
||||||
|
// use the macro to find all addresses and generate placeholders
|
||||||
|
// has to be a macro in order to create the regex automata only once.
|
||||||
|
regex_find_placeholders!(
|
||||||
|
MONERO_ADDR_REGEX,
|
||||||
|
|count| format!("<monero_address_{count}>"),
|
||||||
|
replacements,
|
||||||
|
input
|
||||||
|
);
|
||||||
|
regex_find_placeholders!(
|
||||||
|
BITCOIN_ADDR_REGEX,
|
||||||
|
|count| format!("<bitcoin_address_{count}>"),
|
||||||
|
replacements,
|
||||||
|
input
|
||||||
|
);
|
||||||
|
regex_find_placeholders!(
|
||||||
|
TX_ID_REGEX,
|
||||||
|
|count| format!("<tx_id_{count}>"),
|
||||||
|
replacements,
|
||||||
|
input
|
||||||
|
);
|
||||||
|
regex_find_placeholders!(
|
||||||
|
SWAP_ID_REGEX,
|
||||||
|
|count| format!("<swap_id_{count}>"),
|
||||||
|
replacements,
|
||||||
|
input
|
||||||
|
);
|
||||||
|
|
||||||
|
// allocate string variable to operate on
|
||||||
|
let mut redacted = input.to_owned();
|
||||||
|
|
||||||
|
// Finally we go through the input string and replace each occurance of an
|
||||||
|
// address we want to redact with the corresponding placeholder
|
||||||
|
for (address, placeholder) in replacements.iter() {
|
||||||
|
redacted = redacted.replace(address, placeholder);
|
||||||
|
}
|
||||||
|
|
||||||
|
redacted
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_compares_versions() {
|
||||||
|
assert!(is_latest_version("0.10.2", "0.10.2"));
|
||||||
|
assert!(!is_latest_version("0.10.2", "0.10.3"));
|
||||||
|
assert!(!is_latest_version("0.10.2", "0.11.0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "For local testing, makes http requests to github."]
|
||||||
|
async fn it_compares_with_github() {
|
||||||
|
let result = check_latest_version("0.11.0").await.unwrap();
|
||||||
|
assert_eq!(result, Version::Available);
|
||||||
|
|
||||||
|
let result = check_latest_version("0.11.1").await.unwrap();
|
||||||
|
assert_eq!(result, Version::Current);
|
||||||
|
}
|
||||||
|
}
|
64
swap/src/common/tracing_util.rs
Normal file
64
swap/src/common/tracing_util.rs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use tracing_subscriber::filter::{Directive, LevelFilter};
|
||||||
|
use tracing_subscriber::fmt::time::UtcTime;
|
||||||
|
use tracing_subscriber::layer::SubscriberExt;
|
||||||
|
use tracing_subscriber::util::SubscriberInitExt;
|
||||||
|
use tracing_subscriber::{fmt, EnvFilter, Layer};
|
||||||
|
|
||||||
|
/// Output formats for logging messages.
|
||||||
|
pub enum Format {
|
||||||
|
/// Standard, human readable format.
|
||||||
|
Raw,
|
||||||
|
/// JSON, machine readable format.
|
||||||
|
Json,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize tracing and enable logging messages according to these options.
|
||||||
|
/// Besides printing to `stdout`, this will append to a log file.
|
||||||
|
/// Said file will contain JSON-formatted logs of all levels,
|
||||||
|
/// disregarding the arguments to this function.
|
||||||
|
pub fn init(level_filter: LevelFilter, format: Format, dir: impl AsRef<Path>) -> Result<()> {
|
||||||
|
let env_filter = EnvFilter::from_default_env()
|
||||||
|
.add_directive(Directive::from_str(&format!("asb={}", &level_filter))?)
|
||||||
|
.add_directive(Directive::from_str(&format!("swap={}", &level_filter))?);
|
||||||
|
|
||||||
|
// file logger will always write in JSON format and with timestamps
|
||||||
|
let file_appender = tracing_appender::rolling::never(&dir, "swap-all.log");
|
||||||
|
|
||||||
|
let file_layer = fmt::layer()
|
||||||
|
.with_writer(file_appender)
|
||||||
|
.with_ansi(false)
|
||||||
|
.with_timer(UtcTime::rfc_3339())
|
||||||
|
.with_target(false)
|
||||||
|
.json()
|
||||||
|
.with_filter(env_filter);
|
||||||
|
|
||||||
|
// terminal logger
|
||||||
|
let is_terminal = atty::is(atty::Stream::Stderr);
|
||||||
|
let terminal_layer = fmt::layer()
|
||||||
|
.with_writer(std::io::stdout)
|
||||||
|
.with_ansi(is_terminal)
|
||||||
|
.with_timer(UtcTime::rfc_3339())
|
||||||
|
.with_target(false);
|
||||||
|
|
||||||
|
// combine the layers and start logging, format with json if specified
|
||||||
|
if let Format::Json = format {
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(file_layer)
|
||||||
|
.with(terminal_layer.json().with_filter(level_filter))
|
||||||
|
.init();
|
||||||
|
} else {
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(file_layer)
|
||||||
|
.with(terminal_layer.with_filter(level_filter))
|
||||||
|
.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
// now we can use the tracing macros to log messages
|
||||||
|
tracing::info!(%level_filter, logs_dir=%dir.as_ref().display(), "Initialized tracing");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -83,16 +83,25 @@ impl Swap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn open_db(sqlite_path: impl AsRef<Path>) -> Result<Arc<dyn Database + Send + Sync>> {
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, PartialEq)]
|
||||||
|
pub enum AccessMode {
|
||||||
|
ReadWrite,
|
||||||
|
ReadOnly,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn open_db(
|
||||||
|
sqlite_path: impl AsRef<Path>,
|
||||||
|
access_mode: AccessMode,
|
||||||
|
) -> Result<Arc<dyn Database + Send + Sync>> {
|
||||||
if sqlite_path.as_ref().exists() {
|
if sqlite_path.as_ref().exists() {
|
||||||
tracing::debug!("Using existing sqlite database.");
|
tracing::debug!("Using existing sqlite database.");
|
||||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
let sqlite = SqliteDatabase::open(sqlite_path, access_mode).await?;
|
||||||
Ok(Arc::new(sqlite))
|
Ok(Arc::new(sqlite))
|
||||||
} else {
|
} else {
|
||||||
tracing::debug!("Creating and using new sqlite database.");
|
tracing::debug!("Creating and using new sqlite database.");
|
||||||
ensure_directory_exists(sqlite_path.as_ref())?;
|
ensure_directory_exists(sqlite_path.as_ref())?;
|
||||||
tokio::fs::File::create(&sqlite_path).await?;
|
tokio::fs::File::create(&sqlite_path).await?;
|
||||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
let sqlite = SqliteDatabase::open(sqlite_path, access_mode).await?;
|
||||||
Ok(Arc::new(sqlite))
|
Ok(Arc::new(sqlite))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,12 +70,12 @@ pub enum Alice {
|
|||||||
Done(AliceEndState),
|
Done(AliceEndState),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
#[derive(Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
pub enum AliceEndState {
|
pub enum AliceEndState {
|
||||||
SafelyAborted,
|
SafelyAborted,
|
||||||
BtcRedeemed,
|
BtcRedeemed,
|
||||||
XmrRefunded,
|
XmrRefunded,
|
||||||
BtcPunished,
|
BtcPunished { state3: alice::State3 },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<AliceState> for Alice {
|
impl From<AliceState> for Alice {
|
||||||
@ -173,7 +173,9 @@ impl From<AliceState> for Alice {
|
|||||||
transfer_proof,
|
transfer_proof,
|
||||||
state3: state3.as_ref().clone(),
|
state3: state3.as_ref().clone(),
|
||||||
},
|
},
|
||||||
AliceState::BtcPunished => Alice::Done(AliceEndState::BtcPunished),
|
AliceState::BtcPunished { state3 } => Alice::Done(AliceEndState::BtcPunished {
|
||||||
|
state3: state3.as_ref().clone(),
|
||||||
|
}),
|
||||||
AliceState::SafelyAborted => Alice::Done(AliceEndState::SafelyAborted),
|
AliceState::SafelyAborted => Alice::Done(AliceEndState::SafelyAborted),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -277,7 +279,9 @@ impl From<Alice> for AliceState {
|
|||||||
AliceEndState::SafelyAborted => AliceState::SafelyAborted,
|
AliceEndState::SafelyAborted => AliceState::SafelyAborted,
|
||||||
AliceEndState::BtcRedeemed => AliceState::BtcRedeemed,
|
AliceEndState::BtcRedeemed => AliceState::BtcRedeemed,
|
||||||
AliceEndState::XmrRefunded => AliceState::XmrRefunded,
|
AliceEndState::XmrRefunded => AliceState::XmrRefunded,
|
||||||
AliceEndState::BtcPunished => AliceState::BtcPunished,
|
AliceEndState::BtcPunished { state3 } => AliceState::BtcPunished {
|
||||||
|
state3: Box::new(state3),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,10 @@ pub enum Bob {
|
|||||||
EncSigSent {
|
EncSigSent {
|
||||||
state4: bob::State4,
|
state4: bob::State4,
|
||||||
},
|
},
|
||||||
|
BtcPunished {
|
||||||
|
state: bob::State6,
|
||||||
|
tx_lock_id: bitcoin::Txid,
|
||||||
|
},
|
||||||
BtcRedeemed(bob::State5),
|
BtcRedeemed(bob::State5),
|
||||||
CancelTimelockExpired(bob::State6),
|
CancelTimelockExpired(bob::State6),
|
||||||
BtcCancelled(bob::State6),
|
BtcCancelled(bob::State6),
|
||||||
@ -44,7 +48,6 @@ pub enum BobEndState {
|
|||||||
SafelyAborted,
|
SafelyAborted,
|
||||||
XmrRedeemed { tx_lock_id: bitcoin::Txid },
|
XmrRedeemed { tx_lock_id: bitcoin::Txid },
|
||||||
BtcRefunded(Box<bob::State6>),
|
BtcRefunded(Box<bob::State6>),
|
||||||
BtcPunished { tx_lock_id: bitcoin::Txid },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BobState> for Bob {
|
impl From<BobState> for Bob {
|
||||||
@ -79,13 +82,11 @@ impl From<BobState> for Bob {
|
|||||||
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
|
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
|
||||||
BobState::CancelTimelockExpired(state6) => Bob::CancelTimelockExpired(state6),
|
BobState::CancelTimelockExpired(state6) => Bob::CancelTimelockExpired(state6),
|
||||||
BobState::BtcCancelled(state6) => Bob::BtcCancelled(state6),
|
BobState::BtcCancelled(state6) => Bob::BtcCancelled(state6),
|
||||||
|
BobState::BtcPunished { state, tx_lock_id } => Bob::BtcPunished { state, tx_lock_id },
|
||||||
BobState::BtcRefunded(state6) => Bob::Done(BobEndState::BtcRefunded(Box::new(state6))),
|
BobState::BtcRefunded(state6) => Bob::Done(BobEndState::BtcRefunded(Box::new(state6))),
|
||||||
BobState::XmrRedeemed { tx_lock_id } => {
|
BobState::XmrRedeemed { tx_lock_id } => {
|
||||||
Bob::Done(BobEndState::XmrRedeemed { tx_lock_id })
|
Bob::Done(BobEndState::XmrRedeemed { tx_lock_id })
|
||||||
}
|
}
|
||||||
BobState::BtcPunished { tx_lock_id } => {
|
|
||||||
Bob::Done(BobEndState::BtcPunished { tx_lock_id })
|
|
||||||
}
|
|
||||||
BobState::SafelyAborted => Bob::Done(BobEndState::SafelyAborted),
|
BobState::SafelyAborted => Bob::Done(BobEndState::SafelyAborted),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,11 +124,11 @@ impl From<Bob> for BobState {
|
|||||||
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
|
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
|
||||||
Bob::CancelTimelockExpired(state6) => BobState::CancelTimelockExpired(state6),
|
Bob::CancelTimelockExpired(state6) => BobState::CancelTimelockExpired(state6),
|
||||||
Bob::BtcCancelled(state6) => BobState::BtcCancelled(state6),
|
Bob::BtcCancelled(state6) => BobState::BtcCancelled(state6),
|
||||||
|
Bob::BtcPunished { state, tx_lock_id } => BobState::BtcPunished { state, tx_lock_id },
|
||||||
Bob::Done(end_state) => match end_state {
|
Bob::Done(end_state) => match end_state {
|
||||||
BobEndState::SafelyAborted => BobState::SafelyAborted,
|
BobEndState::SafelyAborted => BobState::SafelyAborted,
|
||||||
BobEndState::XmrRedeemed { tx_lock_id } => BobState::XmrRedeemed { tx_lock_id },
|
BobEndState::XmrRedeemed { tx_lock_id } => BobState::XmrRedeemed { tx_lock_id },
|
||||||
BobEndState::BtcRefunded(state6) => BobState::BtcRefunded(*state6),
|
BobEndState::BtcRefunded(state6) => BobState::BtcRefunded(*state6),
|
||||||
BobEndState::BtcPunished { tx_lock_id } => BobState::BtcPunished { tx_lock_id },
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,6 +149,7 @@ impl fmt::Display for Bob {
|
|||||||
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
||||||
Bob::Done(end_state) => write!(f, "Done: {}", end_state),
|
Bob::Done(end_state) => write!(f, "Done: {}", end_state),
|
||||||
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
||||||
|
Bob::BtcPunished { .. } => f.write_str("Bitcoin punished"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,41 @@
|
|||||||
use crate::database::Swap;
|
use crate::database::Swap;
|
||||||
use crate::monero::Address;
|
use crate::monero::{Address, TransferProof};
|
||||||
use crate::protocol::{Database, State};
|
use crate::protocol::{Database, State};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use libp2p::{Multiaddr, PeerId};
|
use libp2p::{Multiaddr, PeerId};
|
||||||
use sqlx::sqlite::Sqlite;
|
use sqlx::sqlite::{Sqlite, SqliteConnectOptions};
|
||||||
use sqlx::{Pool, SqlitePool};
|
use sqlx::{ConnectOptions, Pool, SqlitePool};
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::AccessMode;
|
||||||
|
|
||||||
pub struct SqliteDatabase {
|
pub struct SqliteDatabase {
|
||||||
pool: Pool<Sqlite>,
|
pool: Pool<Sqlite>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqliteDatabase {
|
impl SqliteDatabase {
|
||||||
pub async fn open(path: impl AsRef<Path>) -> Result<Self>
|
pub async fn open(path: impl AsRef<Path>, access_mode: AccessMode) -> Result<Self>
|
||||||
where
|
where
|
||||||
Self: std::marker::Sized,
|
Self: std::marker::Sized,
|
||||||
{
|
{
|
||||||
|
let read_only = matches!(access_mode, AccessMode::ReadOnly);
|
||||||
|
|
||||||
let path_str = format!("sqlite:{}", path.as_ref().display());
|
let path_str = format!("sqlite:{}", path.as_ref().display());
|
||||||
let pool = SqlitePool::connect(&path_str).await?;
|
let mut options = SqliteConnectOptions::from_str(&path_str)?.read_only(read_only);
|
||||||
|
options.disable_statement_logging();
|
||||||
|
|
||||||
|
let pool = SqlitePool::connect_with(options).await?;
|
||||||
let mut sqlite = Self { pool };
|
let mut sqlite = Self { pool };
|
||||||
sqlite.run_migrations().await?;
|
|
||||||
|
if !read_only {
|
||||||
|
sqlite.run_migrations().await?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(sqlite)
|
Ok(sqlite)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +161,7 @@ impl Database for SqliteDatabase {
|
|||||||
|
|
||||||
let rows = sqlx::query!(
|
let rows = sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
SELECT address
|
SELECT DISTINCT address
|
||||||
FROM peer_addresses
|
FROM peer_addresses
|
||||||
WHERE peer_id = ?
|
WHERE peer_id = ?
|
||||||
"#,
|
"#,
|
||||||
@ -169,6 +181,25 @@ impl Database for SqliteDatabase {
|
|||||||
addresses
|
addresses
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_swap_start_date(&self, swap_id: Uuid) -> Result<String> {
|
||||||
|
let mut conn = self.pool.acquire().await?;
|
||||||
|
let swap_id = swap_id.to_string();
|
||||||
|
|
||||||
|
let row = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT min(entered_at) as start_date
|
||||||
|
FROM swap_states
|
||||||
|
WHERE swap_id = ?
|
||||||
|
"#,
|
||||||
|
swap_id
|
||||||
|
)
|
||||||
|
.fetch_one(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
row.start_date
|
||||||
|
.ok_or_else(|| anyhow!("Could not get swap start date"))
|
||||||
|
}
|
||||||
|
|
||||||
async fn insert_latest_state(&self, swap_id: Uuid, state: State) -> Result<()> {
|
async fn insert_latest_state(&self, swap_id: Uuid, state: State) -> Result<()> {
|
||||||
let mut conn = self.pool.acquire().await?;
|
let mut conn = self.pool.acquire().await?;
|
||||||
let entered_at = OffsetDateTime::now_utc();
|
let entered_at = OffsetDateTime::now_utc();
|
||||||
@ -249,6 +280,119 @@ impl Database for SqliteDatabase {
|
|||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_states(&self, swap_id: Uuid) -> Result<Vec<State>> {
|
||||||
|
let mut conn = self.pool.acquire().await?;
|
||||||
|
let swap_id = swap_id.to_string();
|
||||||
|
|
||||||
|
// TODO: We should use query! instead of query here to allow for at-compile-time validation
|
||||||
|
// I didn't manage to generate the mappings for the query! macro because of problems with sqlx-cli
|
||||||
|
let rows = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT state
|
||||||
|
FROM swap_states
|
||||||
|
WHERE swap_id = ?
|
||||||
|
"#,
|
||||||
|
swap_id
|
||||||
|
)
|
||||||
|
.fetch_all(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let result = rows
|
||||||
|
.iter()
|
||||||
|
.map(|row| {
|
||||||
|
let state_str: &str = &row.state;
|
||||||
|
|
||||||
|
let state = match serde_json::from_str::<Swap>(state_str) {
|
||||||
|
Ok(a) => Ok(State::from(a)),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}?;
|
||||||
|
Ok(state)
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<State>>>();
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn insert_buffered_transfer_proof(
|
||||||
|
&self,
|
||||||
|
swap_id: Uuid,
|
||||||
|
proof: TransferProof,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut conn = self.pool.acquire().await?;
|
||||||
|
let swap_id = swap_id.to_string();
|
||||||
|
let proof = serde_json::to_string(&proof)?;
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO buffered_transfer_proofs (
|
||||||
|
swap_id,
|
||||||
|
proof
|
||||||
|
) VALUES (?, ?);
|
||||||
|
"#,
|
||||||
|
swap_id,
|
||||||
|
proof
|
||||||
|
)
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_buffered_transfer_proof(&self, swap_id: Uuid) -> Result<Option<TransferProof>> {
|
||||||
|
let mut conn = self.pool.acquire().await?;
|
||||||
|
let swap_id = swap_id.to_string();
|
||||||
|
|
||||||
|
let row = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT proof
|
||||||
|
FROM buffered_transfer_proofs
|
||||||
|
WHERE swap_id = ?
|
||||||
|
"#,
|
||||||
|
swap_id
|
||||||
|
)
|
||||||
|
.fetch_all(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if row.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let proof_str = &row[0].proof;
|
||||||
|
let proof = serde_json::from_str(proof_str)?;
|
||||||
|
|
||||||
|
Ok(Some(proof))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn raw_all(&self) -> Result<HashMap<Uuid, Vec<serde_json::Value>>> {
|
||||||
|
let mut conn = self.pool.acquire().await?;
|
||||||
|
let rows = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT swap_id, state
|
||||||
|
FROM swap_states
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.fetch_all(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut swaps: HashMap<Uuid, Vec<serde_json::Value>> = HashMap::new();
|
||||||
|
|
||||||
|
for row in &rows {
|
||||||
|
let swap_id = Uuid::from_str(&row.swap_id)?;
|
||||||
|
let state = serde_json::from_str(&row.state)?;
|
||||||
|
|
||||||
|
if let std::collections::hash_map::Entry::Vacant(e) = swaps.entry(swap_id) {
|
||||||
|
e.insert(vec![state]);
|
||||||
|
} else {
|
||||||
|
swaps
|
||||||
|
.get_mut(&swap_id)
|
||||||
|
.ok_or_else(|| anyhow!("Error while retrieving the swap"))?
|
||||||
|
.push(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(swaps)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -284,9 +428,8 @@ mod tests {
|
|||||||
let db = setup_test_db().await.unwrap();
|
let db = setup_test_db().await.unwrap();
|
||||||
|
|
||||||
let state_1 = State::Alice(AliceState::BtcRedeemed);
|
let state_1 = State::Alice(AliceState::BtcRedeemed);
|
||||||
let state_2 = State::Alice(AliceState::BtcPunished);
|
let state_2 = State::Alice(AliceState::SafelyAborted);
|
||||||
let state_3 = State::Alice(AliceState::SafelyAborted);
|
let state_3 = State::Bob(BobState::SafelyAborted);
|
||||||
let state_4 = State::Bob(BobState::SafelyAborted);
|
|
||||||
let swap_id_1 = Uuid::new_v4();
|
let swap_id_1 = Uuid::new_v4();
|
||||||
let swap_id_2 = Uuid::new_v4();
|
let swap_id_2 = Uuid::new_v4();
|
||||||
|
|
||||||
@ -296,10 +439,7 @@ mod tests {
|
|||||||
db.insert_latest_state(swap_id_1, state_2.clone())
|
db.insert_latest_state(swap_id_1, state_2.clone())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
db.insert_latest_state(swap_id_1, state_3.clone())
|
db.insert_latest_state(swap_id_2, state_3.clone())
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
db.insert_latest_state(swap_id_2, state_4.clone())
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -307,11 +447,10 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(latest_loaded.len(), 2);
|
assert_eq!(latest_loaded.len(), 2);
|
||||||
|
|
||||||
assert!(latest_loaded.contains(&(swap_id_1, state_3)));
|
assert!(latest_loaded.contains(&(swap_id_1, state_2)));
|
||||||
assert!(latest_loaded.contains(&(swap_id_2, state_4)));
|
assert!(latest_loaded.contains(&(swap_id_2, state_3)));
|
||||||
|
|
||||||
assert!(!latest_loaded.contains(&(swap_id_1, state_1)));
|
assert!(!latest_loaded.contains(&(swap_id_1, state_1)));
|
||||||
assert!(!latest_loaded.contains(&(swap_id_1, state_2)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@ -376,7 +515,7 @@ mod tests {
|
|||||||
// file has to exist in order to connect with sqlite
|
// file has to exist in order to connect with sqlite
|
||||||
File::create(temp_db.clone()).unwrap();
|
File::create(temp_db.clone()).unwrap();
|
||||||
|
|
||||||
let db = SqliteDatabase::open(temp_db).await?;
|
let db = SqliteDatabase::open(temp_db, AccessMode::ReadWrite).await?;
|
||||||
|
|
||||||
Ok(db)
|
Ok(db)
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ pub struct Regtest;
|
|||||||
impl GetConfig for Mainnet {
|
impl GetConfig for Mainnet {
|
||||||
fn get_config() -> Config {
|
fn get_config() -> Config {
|
||||||
Config {
|
Config {
|
||||||
bitcoin_lock_mempool_timeout: 3.std_minutes(),
|
bitcoin_lock_mempool_timeout: 10.std_minutes(),
|
||||||
bitcoin_lock_confirmed_timeout: 2.std_hours(),
|
bitcoin_lock_confirmed_timeout: 2.std_hours(),
|
||||||
bitcoin_finality_confirmations: 1,
|
bitcoin_finality_confirmations: 1,
|
||||||
bitcoin_avg_block_time: 10.std_minutes(),
|
bitcoin_avg_block_time: 10.std_minutes(),
|
||||||
@ -63,7 +63,7 @@ impl GetConfig for Mainnet {
|
|||||||
impl GetConfig for Testnet {
|
impl GetConfig for Testnet {
|
||||||
fn get_config() -> Config {
|
fn get_config() -> Config {
|
||||||
Config {
|
Config {
|
||||||
bitcoin_lock_mempool_timeout: 3.std_minutes(),
|
bitcoin_lock_mempool_timeout: 10.std_minutes(),
|
||||||
bitcoin_lock_confirmed_timeout: 1.std_hours(),
|
bitcoin_lock_confirmed_timeout: 1.std_hours(),
|
||||||
bitcoin_finality_confirmations: 1,
|
bitcoin_finality_confirmations: 1,
|
||||||
bitcoin_avg_block_time: 10.std_minutes(),
|
bitcoin_avg_block_time: 10.std_minutes(),
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
missing_copy_implementations
|
missing_copy_implementations
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
pub mod api;
|
||||||
pub mod asb;
|
pub mod asb;
|
||||||
pub mod bitcoin;
|
pub mod bitcoin;
|
||||||
pub mod cli;
|
pub mod cli;
|
||||||
@ -28,6 +29,7 @@ pub mod libp2p_ext;
|
|||||||
pub mod monero;
|
pub mod monero;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
|
pub mod rpc;
|
||||||
pub mod seed;
|
pub mod seed;
|
||||||
pub mod tor;
|
pub mod tor;
|
||||||
pub mod tracing_ext;
|
pub mod tracing_ext;
|
||||||
|
@ -42,6 +42,13 @@ pub fn private_key_from_secp256k1_scalar(scalar: bitcoin::Scalar) -> PrivateKey
|
|||||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
pub struct PrivateViewKey(#[serde(with = "monero_private_key")] PrivateKey);
|
pub struct PrivateViewKey(#[serde(with = "monero_private_key")] PrivateKey);
|
||||||
|
|
||||||
|
impl fmt::Display for PrivateViewKey {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
// Delegate to the Display implementation of PrivateKey
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PrivateViewKey {
|
impl PrivateViewKey {
|
||||||
pub fn new_random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
|
pub fn new_random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
|
||||||
let scalar = Scalar::random(rng);
|
let scalar = Scalar::random(rng);
|
||||||
@ -135,6 +142,14 @@ impl Amount {
|
|||||||
Decimal::from(self.as_piconero())
|
Decimal::from(self.as_piconero())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn as_xmr(&self) -> Decimal {
|
||||||
|
let mut decimal = Decimal::from(self.0);
|
||||||
|
decimal
|
||||||
|
.set_scale(12)
|
||||||
|
.expect("12 is smaller than max precision of 28");
|
||||||
|
decimal
|
||||||
|
}
|
||||||
|
|
||||||
fn from_decimal(amount: Decimal) -> Result<Self> {
|
fn from_decimal(amount: Decimal) -> Result<Self> {
|
||||||
let piconeros_dec =
|
let piconeros_dec =
|
||||||
amount.mul(Decimal::from_u64(PICONERO_OFFSET).expect("constant to fit into u64"));
|
amount.mul(Decimal::from_u64(PICONERO_OFFSET).expect("constant to fit into u64"));
|
||||||
@ -177,11 +192,8 @@ impl From<Amount> for u64 {
|
|||||||
|
|
||||||
impl fmt::Display for Amount {
|
impl fmt::Display for Amount {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut decimal = Decimal::from(self.0);
|
let xmr_value = self.as_xmr();
|
||||||
decimal
|
write!(f, "{} XMR", xmr_value)
|
||||||
.set_scale(12)
|
|
||||||
.expect("12 is smaller than max precision of 28");
|
|
||||||
write!(f, "{} XMR", decimal)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,6 +332,52 @@ pub mod monero_amount {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod monero_address {
|
||||||
|
use anyhow::{bail, Context, Result};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq)]
|
||||||
|
#[error("Invalid monero address provided, expected address on network {expected:?} but address provided is on {actual:?}")]
|
||||||
|
pub struct MoneroAddressNetworkMismatch {
|
||||||
|
pub expected: monero::Network,
|
||||||
|
pub actual: monero::Network,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(s: &str) -> Result<monero::Address> {
|
||||||
|
monero::Address::from_str(s).with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Failed to parse {} as a monero address, please make sure it is a valid address",
|
||||||
|
s
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate(
|
||||||
|
address: monero::Address,
|
||||||
|
expected_network: monero::Network,
|
||||||
|
) -> Result<monero::Address> {
|
||||||
|
if address.network != expected_network {
|
||||||
|
bail!(MoneroAddressNetworkMismatch {
|
||||||
|
expected: expected_network,
|
||||||
|
actual: address.network,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Ok(address)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_is_testnet(
|
||||||
|
address: monero::Address,
|
||||||
|
is_testnet: bool,
|
||||||
|
) -> Result<monero::Address> {
|
||||||
|
let expected_network = if is_testnet {
|
||||||
|
monero::Network::Stagenet
|
||||||
|
} else {
|
||||||
|
monero::Network::Mainnet
|
||||||
|
};
|
||||||
|
validate(address, expected_network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -6,6 +6,7 @@ use ::monero::{Address, Network, PrivateKey, PublicKey};
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use monero_rpc::wallet::{BlockHeight, MoneroWalletRpc as _, Refreshed};
|
use monero_rpc::wallet::{BlockHeight, MoneroWalletRpc as _, Refreshed};
|
||||||
use monero_rpc::{jsonrpc, wallet};
|
use monero_rpc::{jsonrpc, wallet};
|
||||||
|
use std::ops::Div;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
@ -45,6 +46,7 @@ impl Wallet {
|
|||||||
pub async fn connect(client: wallet::Client, name: String, env_config: Config) -> Result<Self> {
|
pub async fn connect(client: wallet::Client, name: String, env_config: Config) -> Result<Self> {
|
||||||
let main_address =
|
let main_address =
|
||||||
monero::Address::from_str(client.get_address(0).await?.address.as_str())?;
|
monero::Address::from_str(client.get_address(0).await?.address.as_str())?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
inner: Mutex::new(client),
|
inner: Mutex::new(client),
|
||||||
network: env_config.monero_network,
|
network: env_config.monero_network,
|
||||||
@ -125,13 +127,14 @@ impl Wallet {
|
|||||||
let temp_wallet_address =
|
let temp_wallet_address =
|
||||||
Address::standard(self.network, public_spend_key, public_view_key);
|
Address::standard(self.network, public_spend_key, public_view_key);
|
||||||
|
|
||||||
let wallet = self.inner.lock().await;
|
|
||||||
|
|
||||||
// Close the default wallet before generating the other wallet to ensure that
|
// Close the default wallet before generating the other wallet to ensure that
|
||||||
// it saves its state correctly
|
// it saves its state correctly
|
||||||
let _ = wallet.close_wallet().await?;
|
let _ = self.inner.lock().await.close_wallet().await?;
|
||||||
|
|
||||||
let _ = wallet
|
let _ = self
|
||||||
|
.inner
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
.generate_from_keys(
|
.generate_from_keys(
|
||||||
file_name,
|
file_name,
|
||||||
temp_wallet_address.to_string(),
|
temp_wallet_address.to_string(),
|
||||||
@ -144,8 +147,14 @@ impl Wallet {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Try to send all the funds from the generated wallet to the default wallet
|
// Try to send all the funds from the generated wallet to the default wallet
|
||||||
match wallet.refresh().await {
|
match self.refresh(3).await {
|
||||||
Ok(_) => match wallet.sweep_all(self.main_address.to_string()).await {
|
Ok(_) => match self
|
||||||
|
.inner
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.sweep_all(self.main_address.to_string())
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(sweep_all) => {
|
Ok(sweep_all) => {
|
||||||
for tx in sweep_all.tx_hash_list {
|
for tx in sweep_all.tx_hash_list {
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
@ -166,7 +175,12 @@ impl Wallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = wallet.open_wallet(self.name.clone()).await?;
|
let _ = self
|
||||||
|
.inner
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.open_wallet(self.name.clone())
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -174,11 +188,6 @@ impl Wallet {
|
|||||||
pub async fn transfer(&self, request: TransferRequest) -> Result<TransferProof> {
|
pub async fn transfer(&self, request: TransferRequest) -> Result<TransferProof> {
|
||||||
let inner = self.inner.lock().await;
|
let inner = self.inner.lock().await;
|
||||||
|
|
||||||
inner
|
|
||||||
.open_wallet(self.name.clone())
|
|
||||||
.await
|
|
||||||
.with_context(|| format!("Failed to open wallet {}", self.name))?;
|
|
||||||
|
|
||||||
let TransferRequest {
|
let TransferRequest {
|
||||||
public_spend_key,
|
public_spend_key,
|
||||||
public_view_key,
|
public_view_key,
|
||||||
@ -225,7 +234,7 @@ impl Wallet {
|
|||||||
|
|
||||||
let address = Address::standard(self.network, public_spend_key, public_view_key.into());
|
let address = Address::standard(self.network, public_spend_key, public_view_key.into());
|
||||||
|
|
||||||
let check_interval = tokio::time::interval(self.sync_interval);
|
let check_interval = tokio::time::interval(self.sync_interval.div(10));
|
||||||
|
|
||||||
wait_for_confirmations(
|
wait_for_confirmations(
|
||||||
&self.inner,
|
&self.inner,
|
||||||
@ -266,8 +275,44 @@ impl Wallet {
|
|||||||
self.main_address
|
self.main_address
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refresh(&self) -> Result<Refreshed> {
|
pub async fn refresh(&self, max_attempts: usize) -> Result<Refreshed> {
|
||||||
Ok(self.inner.lock().await.refresh().await?)
|
const RETRY_INTERVAL: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
|
for i in 1..=max_attempts {
|
||||||
|
tracing::info!(name = %self.name, attempt=i, "Syncing Monero wallet");
|
||||||
|
|
||||||
|
let result = self.inner.lock().await.refresh().await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(refreshed) => {
|
||||||
|
tracing::info!(name = %self.name, "Monero wallet synced");
|
||||||
|
return Ok(refreshed);
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
let attempts_left = max_attempts - i;
|
||||||
|
|
||||||
|
// We would not want to fail here if the height is not available
|
||||||
|
// as it is not critical for the operation of the wallet.
|
||||||
|
// We can just log a warning and continue.
|
||||||
|
let height = match self.inner.lock().await.get_height().await {
|
||||||
|
Ok(height) => height.to_string(),
|
||||||
|
Err(_) => {
|
||||||
|
tracing::warn!(name = %self.name, "Failed to fetch Monero wallet height during sync");
|
||||||
|
"unknown".to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::warn!(attempt=i, %height, %attempts_left, name = %self.name, %error, "Failed to sync Monero wallet");
|
||||||
|
|
||||||
|
if attempts_left == 0 {
|
||||||
|
return Err(error.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(RETRY_INTERVAL).await;
|
||||||
|
}
|
||||||
|
unreachable!("Loop should have returned by now");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,37 +1,75 @@
|
|||||||
use ::monero::Network;
|
use ::monero::Network;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{bail, Context, Error, Result};
|
||||||
use big_bytes::BigByte;
|
use big_bytes::BigByte;
|
||||||
|
use data_encoding::HEXLOWER;
|
||||||
use futures::{StreamExt, TryStreamExt};
|
use futures::{StreamExt, TryStreamExt};
|
||||||
use monero_rpc::wallet::{Client, MoneroWalletRpc as _};
|
use monero_rpc::wallet::{Client, MoneroWalletRpc as _};
|
||||||
use reqwest::header::CONTENT_LENGTH;
|
use reqwest::header::CONTENT_LENGTH;
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio::fs::{remove_file, OpenOptions};
|
use tokio::fs::{remove_file, OpenOptions};
|
||||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||||
use tokio::process::{Child, Command};
|
use tokio::process::{Child, Command};
|
||||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||||
use tokio_util::io::StreamReader;
|
use tokio_util::io::StreamReader;
|
||||||
|
|
||||||
|
// See: https://www.moneroworld.com/#nodes, https://monero.fail
|
||||||
|
// We don't need any testnet nodes because we don't support testnet at all
|
||||||
|
const MONERO_DAEMONS: [MoneroDaemon; 17] = [
|
||||||
|
MoneroDaemon::new("xmr-node.cakewallet.com", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("nodex.monerujo.io", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("node.moneroworld.com", 18089, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("nodes.hashvault.pro", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("p2pmd.xmrvsbeast.com", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("node.monerodevs.org", 18089, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("xmr-node-usa-east.cakewallet.com", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("xmr-node-uk.cakewallet.com", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("node.community.rino.io", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("testingjohnross.com", 20031, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("xmr.litepay.ch", 18081, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("node.trocador.app", 18089, Network::Mainnet),
|
||||||
|
MoneroDaemon::new("stagenet.xmr-tw.org", 38081, Network::Stagenet),
|
||||||
|
MoneroDaemon::new("node.monerodevs.org", 38089, Network::Stagenet),
|
||||||
|
MoneroDaemon::new("singapore.node.xmr.pm", 38081, Network::Stagenet),
|
||||||
|
MoneroDaemon::new("xmr-lux.boldsuck.org", 38081, Network::Stagenet),
|
||||||
|
MoneroDaemon::new("stagenet.community.rino.io", 38081, Network::Stagenet),
|
||||||
|
];
|
||||||
|
|
||||||
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
|
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
|
||||||
compile_error!("unsupported operating system");
|
compile_error!("unsupported operating system");
|
||||||
|
|
||||||
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
|
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
|
||||||
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.1.2.tar.bz2";
|
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.3.1.tar.bz2";
|
||||||
|
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
|
||||||
|
const DOWNLOAD_HASH: &str = "7f8bd9364ef16482b418aa802a65be0e4cc660c794bb5d77b2d17bc84427883a";
|
||||||
|
|
||||||
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
|
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
|
||||||
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.0.0.tar.bz2";
|
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.3.1.tar.bz2";
|
||||||
|
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
|
||||||
|
const DOWNLOAD_HASH: &str = "915288b023cb5811e626e10052adc6ac5323dd283c5a25b91059b0fb86a21fb6";
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
|
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
|
||||||
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.1.2.tar.bz2";
|
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.3.1.tar.bz2";
|
||||||
|
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
|
||||||
|
const DOWNLOAD_HASH: &str = "23af572fdfe3459b9ab97e2e9aa7e3c11021c955d6064b801a27d7e8c21ae09d";
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", target_arch = "arm"))]
|
#[cfg(all(target_os = "linux", target_arch = "arm"))]
|
||||||
const DOWNLOAD_URL: &str =
|
const DOWNLOAD_URL: &str =
|
||||||
"https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.1.2.tar.bz2";
|
"https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2";
|
||||||
|
#[cfg(all(target_os = "linux", target_arch = "arm"))]
|
||||||
|
const DOWNLOAD_HASH: &str = "2ea2c8898cbab88f49423f4f6c15f2a94046cb4bbe827493dd061edc0fd5f1ca";
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.1.2.zip";
|
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.3.1.zip";
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
const DOWNLOAD_HASH: &str = "35dcc4bee4caad3442659d37837e0119e4649a77f2e3b5e80dd6d9b8fc4fb6ad";
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
||||||
const PACKED_FILE: &str = "monero-wallet-rpc";
|
const PACKED_FILE: &str = "monero-wallet-rpc";
|
||||||
@ -39,7 +77,7 @@ const PACKED_FILE: &str = "monero-wallet-rpc";
|
|||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
const PACKED_FILE: &str = "monero-wallet-rpc.exe";
|
const PACKED_FILE: &str = "monero-wallet-rpc.exe";
|
||||||
|
|
||||||
const WALLET_RPC_VERSION: &str = "v0.18.1.2";
|
const WALLET_RPC_VERSION: &str = "v0.18.3.1";
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, thiserror::Error)]
|
#[derive(Debug, Clone, Copy, thiserror::Error)]
|
||||||
#[error("monero wallet rpc executable not found in downloaded archive")]
|
#[error("monero wallet rpc executable not found in downloaded archive")]
|
||||||
@ -50,6 +88,91 @@ pub struct WalletRpcProcess {
|
|||||||
port: u16,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct MoneroDaemon {
|
||||||
|
address: &'static str,
|
||||||
|
port: u16,
|
||||||
|
network: Network,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MoneroDaemon {
|
||||||
|
const fn new(address: &'static str, port: u16, network: Network) -> Self {
|
||||||
|
Self {
|
||||||
|
address,
|
||||||
|
port,
|
||||||
|
network,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the Monero daemon is available by sending a request to its `get_info` endpoint.
|
||||||
|
async fn is_available(&self, client: &reqwest::Client) -> Result<bool, Error> {
|
||||||
|
let url = format!("http://{}:{}/get_info", self.address, self.port);
|
||||||
|
let res = client
|
||||||
|
.get(url)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("Failed to send request to get_info endpoint")?;
|
||||||
|
|
||||||
|
let json: MoneroDaemonGetInfoResponse = res
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.context("Failed to deserialize daemon get_info response")?;
|
||||||
|
|
||||||
|
let is_status_ok = json.status == "OK";
|
||||||
|
let is_synchronized = json.synchronized;
|
||||||
|
let is_correct_network = match self.network {
|
||||||
|
Network::Mainnet => json.mainnet,
|
||||||
|
Network::Stagenet => json.stagenet,
|
||||||
|
Network::Testnet => json.testnet,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(is_status_ok && is_synchronized && is_correct_network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for MoneroDaemon {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}:{}", self.address, self.port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct MoneroDaemonGetInfoResponse {
|
||||||
|
status: String,
|
||||||
|
synchronized: bool,
|
||||||
|
mainnet: bool,
|
||||||
|
stagenet: bool,
|
||||||
|
testnet: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Chooses an available Monero daemon based on the specified network.
|
||||||
|
async fn choose_monero_daemon(network: Network) -> Result<&'static MoneroDaemon, Error> {
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.timeout(Duration::from_secs(30))
|
||||||
|
.https_only(false)
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
// We only want to check for daemons that match the specified network
|
||||||
|
let network_matching_daemons = MONERO_DAEMONS
|
||||||
|
.iter()
|
||||||
|
.filter(|daemon| daemon.network == network);
|
||||||
|
|
||||||
|
for daemon in network_matching_daemons {
|
||||||
|
match daemon.is_available(&client).await {
|
||||||
|
Ok(true) => {
|
||||||
|
tracing::debug!(%daemon, "Found available Monero daemon");
|
||||||
|
return Ok(daemon);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
tracing::debug!(%err, %daemon, "Failed to connect to Monero daemon");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Ok(false) => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("No Monero daemon could be found. Please specify one manually or try again later.")
|
||||||
|
}
|
||||||
|
|
||||||
impl WalletRpcProcess {
|
impl WalletRpcProcess {
|
||||||
pub fn endpoint(&self) -> Url {
|
pub fn endpoint(&self) -> Url {
|
||||||
Url::parse(&format!("http://127.0.0.1:{}/json_rpc", self.port))
|
Url::parse(&format!("http://127.0.0.1:{}/json_rpc", self.port))
|
||||||
@ -110,13 +233,20 @@ impl WalletRpc {
|
|||||||
.parse::<u64>()?;
|
.parse::<u64>()?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
"Downloading monero-wallet-rpc ({}) from {}",
|
progress="0%",
|
||||||
content_length.big_byte(2),
|
size=%content_length.big_byte(2),
|
||||||
DOWNLOAD_URL
|
download_url=DOWNLOAD_URL,
|
||||||
|
"Downloading monero-wallet-rpc",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
|
||||||
let byte_stream = response
|
let byte_stream = response
|
||||||
.bytes_stream()
|
.bytes_stream()
|
||||||
|
.map_ok(|bytes| {
|
||||||
|
hasher.update(&bytes);
|
||||||
|
bytes
|
||||||
|
})
|
||||||
.map_err(|err| std::io::Error::new(ErrorKind::Other, err));
|
.map_err(|err| std::io::Error::new(ErrorKind::Other, err));
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
@ -139,12 +269,36 @@ impl WalletRpc {
|
|||||||
let total = 3 * content_length;
|
let total = 3 * content_length;
|
||||||
let percent = 100 * received as u64 / total;
|
let percent = 100 * received as u64 / total;
|
||||||
if percent != notified && percent % 10 == 0 {
|
if percent != notified && percent % 10 == 0 {
|
||||||
tracing::debug!("{}%", percent);
|
tracing::info!(
|
||||||
|
progress=format!("{}%", percent),
|
||||||
|
size=%content_length.big_byte(2),
|
||||||
|
download_url=DOWNLOAD_URL,
|
||||||
|
"Downloading monero-wallet-rpc",
|
||||||
|
);
|
||||||
notified = percent;
|
notified = percent;
|
||||||
}
|
}
|
||||||
file.write_all(&bytes).await?;
|
file.write_all(&bytes).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::info!(
|
||||||
|
progress="100%",
|
||||||
|
size=%content_length.big_byte(2),
|
||||||
|
download_url=DOWNLOAD_URL,
|
||||||
|
"Downloading monero-wallet-rpc",
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = hasher.finalize();
|
||||||
|
let result_hash = HEXLOWER.encode(result.as_ref());
|
||||||
|
if result_hash != DOWNLOAD_HASH {
|
||||||
|
bail!(
|
||||||
|
"SHA256 of download ({}) does not match expected ({})!",
|
||||||
|
result_hash,
|
||||||
|
DOWNLOAD_HASH
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
tracing::debug!("Hashes match");
|
||||||
|
}
|
||||||
|
|
||||||
file.flush().await?;
|
file.flush().await?;
|
||||||
|
|
||||||
tracing::debug!("Extracting archive");
|
tracing::debug!("Extracting archive");
|
||||||
@ -153,13 +307,23 @@ impl WalletRpc {
|
|||||||
Ok(monero_wallet_rpc)
|
Ok(monero_wallet_rpc)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(&self, network: Network, daemon_address: &str) -> Result<WalletRpcProcess> {
|
pub async fn run(
|
||||||
|
&self,
|
||||||
|
network: Network,
|
||||||
|
daemon_address: Option<String>,
|
||||||
|
) -> Result<WalletRpcProcess> {
|
||||||
let port = tokio::net::TcpListener::bind("127.0.0.1:0")
|
let port = tokio::net::TcpListener::bind("127.0.0.1:0")
|
||||||
.await?
|
.await?
|
||||||
.local_addr()?
|
.local_addr()?
|
||||||
.port();
|
.port();
|
||||||
|
|
||||||
|
let daemon_address = match daemon_address {
|
||||||
|
Some(daemon_address) => daemon_address,
|
||||||
|
None => choose_monero_daemon(network).await?.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
|
%daemon_address,
|
||||||
%port,
|
%port,
|
||||||
"Starting monero-wallet-rpc"
|
"Starting monero-wallet-rpc"
|
||||||
);
|
);
|
||||||
@ -188,6 +352,7 @@ impl WalletRpc {
|
|||||||
.arg("--disable-rpc-login")
|
.arg("--disable-rpc-login")
|
||||||
.arg("--wallet-dir")
|
.arg("--wallet-dir")
|
||||||
.arg(self.working_dir.join("monero-data"))
|
.arg(self.working_dir.join("monero-data"))
|
||||||
|
.arg("--no-initial-sync")
|
||||||
.spawn()?;
|
.spawn()?;
|
||||||
|
|
||||||
let stdout = child
|
let stdout = child
|
||||||
@ -205,7 +370,7 @@ impl WalletRpc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we do not hear from the monero_wallet_rpc process for 3 seconds we assume
|
// If we do not hear from the monero_wallet_rpc process for 3 seconds we assume
|
||||||
// it is is ready
|
// it is ready
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
while let Ok(line) =
|
while let Ok(line) =
|
||||||
tokio::time::timeout(std::time::Duration::from_secs(3), reader.next_line()).await
|
tokio::time::timeout(std::time::Duration::from_secs(3), reader.next_line()).await
|
||||||
@ -232,7 +397,6 @@ impl WalletRpc {
|
|||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
async fn extract_archive(monero_wallet_rpc: &Self) -> Result<()> {
|
async fn extract_archive(monero_wallet_rpc: &Self) -> Result<()> {
|
||||||
use anyhow::bail;
|
|
||||||
use tokio_tar::Archive;
|
use tokio_tar::Archive;
|
||||||
|
|
||||||
let mut options = OpenOptions::new();
|
let mut options = OpenOptions::new();
|
||||||
@ -297,3 +461,123 @@ impl WalletRpc {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn extract_host_and_port(address: String) -> (&'static str, u16) {
|
||||||
|
let parts: Vec<&str> = address.split(':').collect();
|
||||||
|
|
||||||
|
if parts.len() == 2 {
|
||||||
|
let host = parts[0].to_string();
|
||||||
|
let port = parts[1].parse::<u16>().unwrap();
|
||||||
|
let static_str_host: &'static str = Box::leak(host.into_boxed_str());
|
||||||
|
return (static_str_host, port);
|
||||||
|
}
|
||||||
|
panic!("Could not extract host and port from address: {}", address)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_is_daemon_available_success() {
|
||||||
|
let mut server = mockito::Server::new_async().await;
|
||||||
|
|
||||||
|
let _ = server
|
||||||
|
.mock("GET", "/get_info")
|
||||||
|
.with_status(200)
|
||||||
|
.with_body(
|
||||||
|
r#"
|
||||||
|
{
|
||||||
|
"status": "OK",
|
||||||
|
"synchronized": true,
|
||||||
|
"mainnet": true,
|
||||||
|
"stagenet": false,
|
||||||
|
"testnet": false
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
let (host, port) = extract_host_and_port(server.host_with_port());
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let result = MoneroDaemon::new(host, port, Network::Mainnet)
|
||||||
|
.is_available(&client)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert!(result.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_is_daemon_available_wrong_network_failure() {
|
||||||
|
let mut server = mockito::Server::new_async().await;
|
||||||
|
|
||||||
|
let _ = server
|
||||||
|
.mock("GET", "/get_info")
|
||||||
|
.with_status(200)
|
||||||
|
.with_body(
|
||||||
|
r#"
|
||||||
|
{
|
||||||
|
"status": "OK",
|
||||||
|
"synchronized": true,
|
||||||
|
"mainnet": true,
|
||||||
|
"stagenet": false,
|
||||||
|
"testnet": false
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
let (host, port) = extract_host_and_port(server.host_with_port());
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let result = MoneroDaemon::new(host, port, Network::Stagenet)
|
||||||
|
.is_available(&client)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert!(!result.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_is_daemon_available_not_synced_failure() {
|
||||||
|
let mut server = mockito::Server::new_async().await;
|
||||||
|
|
||||||
|
let _ = server
|
||||||
|
.mock("GET", "/get_info")
|
||||||
|
.with_status(200)
|
||||||
|
.with_body(
|
||||||
|
r#"
|
||||||
|
{
|
||||||
|
"status": "OK",
|
||||||
|
"synchronized": false,
|
||||||
|
"mainnet": true,
|
||||||
|
"stagenet": false,
|
||||||
|
"testnet": false
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
let (host, port) = extract_host_and_port(server.host_with_port());
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let result = MoneroDaemon::new(host, port, Network::Mainnet)
|
||||||
|
.is_available(&client)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert!(!result.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_is_daemon_available_network_error_failure() {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let result = MoneroDaemon::new("does.not.exist.com", 18081, Network::Mainnet)
|
||||||
|
.is_available(&client)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
mod impl_from_rr_event;
|
mod impl_from_rr_event;
|
||||||
|
|
||||||
pub mod cbor_request_response;
|
pub mod cbor_request_response;
|
||||||
|
pub mod cooperative_xmr_redeem_after_punish;
|
||||||
pub mod encrypted_signature;
|
pub mod encrypted_signature;
|
||||||
pub mod json_pull_codec;
|
pub mod json_pull_codec;
|
||||||
pub mod quote;
|
pub mod quote;
|
||||||
|
@ -19,7 +19,7 @@ pub struct CborCodec<P, Req, Res> {
|
|||||||
impl<P, Req, Res> Default for CborCodec<P, Req, Res> {
|
impl<P, Req, Res> Default for CborCodec<P, Req, Res> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
phantom: PhantomData::default(),
|
phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
113
swap/src/network/cooperative_xmr_redeem_after_punish.rs
Normal file
113
swap/src/network/cooperative_xmr_redeem_after_punish.rs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
use crate::monero::Scalar;
|
||||||
|
use crate::network::cbor_request_response::CborCodec;
|
||||||
|
use crate::{asb, cli};
|
||||||
|
use libp2p::core::ProtocolName;
|
||||||
|
use libp2p::request_response::{
|
||||||
|
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
|
||||||
|
RequestResponseMessage,
|
||||||
|
};
|
||||||
|
use libp2p::PeerId;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const PROTOCOL: &str = "/comit/xmr/btc/cooperative_xmr_redeem_after_punish/1.0.0";
|
||||||
|
type OutEvent = RequestResponseEvent<Request, Response>;
|
||||||
|
type Message = RequestResponseMessage<Request, Response>;
|
||||||
|
|
||||||
|
pub type Behaviour = RequestResponse<CborCodec<CooperativeXmrRedeemProtocol, Request, Response>>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
|
pub struct CooperativeXmrRedeemProtocol;
|
||||||
|
|
||||||
|
impl ProtocolName for CooperativeXmrRedeemProtocol {
|
||||||
|
fn protocol_name(&self) -> &[u8] {
|
||||||
|
PROTOCOL.as_bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error, Clone, Serialize, Deserialize)]
|
||||||
|
pub enum CooperativeXmrRedeemRejectReason {
|
||||||
|
#[error("Alice does not have a record of the swap")]
|
||||||
|
UnknownSwap,
|
||||||
|
#[error("Alice rejected the request because it deemed it malicious")]
|
||||||
|
MaliciousRequest,
|
||||||
|
#[error("Alice is in a state where a cooperative redeem is not possible")]
|
||||||
|
SwapInvalidState,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Request {
|
||||||
|
pub swap_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub enum Response {
|
||||||
|
Fullfilled {
|
||||||
|
swap_id: Uuid,
|
||||||
|
s_a: Scalar,
|
||||||
|
},
|
||||||
|
Rejected {
|
||||||
|
swap_id: Uuid,
|
||||||
|
reason: CooperativeXmrRedeemRejectReason,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pub fn alice() -> Behaviour {
|
||||||
|
Behaviour::new(
|
||||||
|
CborCodec::default(),
|
||||||
|
vec![(CooperativeXmrRedeemProtocol, ProtocolSupport::Inbound)],
|
||||||
|
RequestResponseConfig::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bob() -> Behaviour {
|
||||||
|
Behaviour::new(
|
||||||
|
CborCodec::default(),
|
||||||
|
vec![(CooperativeXmrRedeemProtocol, ProtocolSupport::Outbound)],
|
||||||
|
RequestResponseConfig::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<(PeerId, Message)> for asb::OutEvent {
|
||||||
|
fn from((peer, message): (PeerId, Message)) -> Self {
|
||||||
|
match message {
|
||||||
|
Message::Request {
|
||||||
|
request, channel, ..
|
||||||
|
} => Self::CooperativeXmrRedeemRequested {
|
||||||
|
swap_id: request.swap_id,
|
||||||
|
channel,
|
||||||
|
peer,
|
||||||
|
},
|
||||||
|
Message::Response { .. } => Self::unexpected_response(peer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::impl_from_rr_event!(OutEvent, asb::OutEvent, PROTOCOL);
|
||||||
|
|
||||||
|
impl From<(PeerId, Message)> for cli::OutEvent {
|
||||||
|
fn from((peer, message): (PeerId, Message)) -> Self {
|
||||||
|
match message {
|
||||||
|
Message::Request { .. } => Self::unexpected_request(peer),
|
||||||
|
Message::Response {
|
||||||
|
response,
|
||||||
|
request_id,
|
||||||
|
} => match response {
|
||||||
|
Response::Fullfilled { swap_id, s_a } => Self::CooperativeXmrRedeemFulfilled {
|
||||||
|
id: request_id,
|
||||||
|
swap_id,
|
||||||
|
s_a,
|
||||||
|
},
|
||||||
|
Response::Rejected {
|
||||||
|
swap_id,
|
||||||
|
reason: error,
|
||||||
|
} => Self::CooperativeXmrRedeemRejected {
|
||||||
|
id: request_id,
|
||||||
|
swap_id,
|
||||||
|
reason: error,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::impl_from_rr_event!(OutEvent, cli::OutEvent, PROTOCOL);
|
@ -25,7 +25,7 @@ pub struct JsonPullCodec<P, Res> {
|
|||||||
impl<P, Res> Default for JsonPullCodec<P, Res> {
|
impl<P, Res> Default for JsonPullCodec<P, Res> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
phantom: PhantomData::default(),
|
phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures::stream::FusedStream;
|
use futures::stream::FusedStream;
|
||||||
use futures::{future, Future, Stream, StreamExt};
|
use futures::{future, Future, StreamExt};
|
||||||
use libp2p::core::muxing::StreamMuxerBox;
|
use libp2p::core::muxing::StreamMuxerBox;
|
||||||
use libp2p::core::transport::upgrade::Version;
|
use libp2p::core::transport::upgrade::Version;
|
||||||
use libp2p::core::transport::MemoryTransport;
|
use libp2p::core::transport::MemoryTransport;
|
||||||
@ -40,7 +40,7 @@ where
|
|||||||
.expect("failed to create dh_keys");
|
.expect("failed to create dh_keys");
|
||||||
let noise = NoiseConfig::xx(dh_keys).into_authenticated();
|
let noise = NoiseConfig::xx(dh_keys).into_authenticated();
|
||||||
|
|
||||||
let transport = MemoryTransport::default()
|
let transport = MemoryTransport
|
||||||
.or_transport(TokioTcpConfig::new())
|
.or_transport(TokioTcpConfig::new())
|
||||||
.upgrade(Version::V1)
|
.upgrade(Version::V1)
|
||||||
.authenticate(noise)
|
.authenticate(noise)
|
||||||
@ -75,8 +75,8 @@ async fn get_local_tcp_address() -> Multiaddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn await_events_or_timeout<A, B, E1, E2>(
|
pub async fn await_events_or_timeout<A, B, E1, E2>(
|
||||||
swarm_1: &mut (impl Stream<Item = SwarmEvent<A, E1>> + FusedStream + Unpin),
|
swarm_1: &mut (impl FusedStream<Item = SwarmEvent<A, E1>> + FusedStream + Unpin),
|
||||||
swarm_2: &mut (impl Stream<Item = SwarmEvent<B, E2>> + FusedStream + Unpin),
|
swarm_2: &mut (impl FusedStream<Item = SwarmEvent<B, E2>> + FusedStream + Unpin),
|
||||||
) -> (SwarmEvent<A, E1>, SwarmEvent<B, E2>)
|
) -> (SwarmEvent<A, E1>, SwarmEvent<B, E2>)
|
||||||
where
|
where
|
||||||
SwarmEvent<A, E1>: Debug,
|
SwarmEvent<A, E1>: Debug,
|
||||||
|
@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use sigma_fun::ext::dl_secp256k1_ed25519_eq::{CrossCurveDLEQ, CrossCurveDLEQProof};
|
use sigma_fun::ext::dl_secp256k1_ed25519_eq::{CrossCurveDLEQ, CrossCurveDLEQProof};
|
||||||
use sigma_fun::HashTranscript;
|
use sigma_fun::HashTranscript;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -139,7 +140,19 @@ pub trait Database {
|
|||||||
async fn get_monero_address(&self, swap_id: Uuid) -> Result<monero::Address>;
|
async fn get_monero_address(&self, swap_id: Uuid) -> Result<monero::Address>;
|
||||||
async fn insert_address(&self, peer_id: PeerId, address: Multiaddr) -> Result<()>;
|
async fn insert_address(&self, peer_id: PeerId, address: Multiaddr) -> Result<()>;
|
||||||
async fn get_addresses(&self, peer_id: PeerId) -> Result<Vec<Multiaddr>>;
|
async fn get_addresses(&self, peer_id: PeerId) -> Result<Vec<Multiaddr>>;
|
||||||
|
async fn get_swap_start_date(&self, swap_id: Uuid) -> Result<String>;
|
||||||
async fn insert_latest_state(&self, swap_id: Uuid, state: State) -> Result<()>;
|
async fn insert_latest_state(&self, swap_id: Uuid, state: State) -> Result<()>;
|
||||||
async fn get_state(&self, swap_id: Uuid) -> Result<State>;
|
async fn get_state(&self, swap_id: Uuid) -> Result<State>;
|
||||||
|
async fn get_states(&self, swap_id: Uuid) -> Result<Vec<State>>;
|
||||||
async fn all(&self) -> Result<Vec<(Uuid, State)>>;
|
async fn all(&self) -> Result<Vec<(Uuid, State)>>;
|
||||||
|
async fn raw_all(&self) -> Result<HashMap<Uuid, Vec<serde_json::Value>>>;
|
||||||
|
async fn insert_buffered_transfer_proof(
|
||||||
|
&self,
|
||||||
|
swap_id: Uuid,
|
||||||
|
proof: monero::TransferProof,
|
||||||
|
) -> Result<()>;
|
||||||
|
async fn get_buffered_transfer_proof(
|
||||||
|
&self,
|
||||||
|
swap_id: Uuid,
|
||||||
|
) -> Result<Option<monero::TransferProof>>;
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,9 @@ pub enum AliceState {
|
|||||||
transfer_proof: TransferProof,
|
transfer_proof: TransferProof,
|
||||||
state3: Box<State3>,
|
state3: Box<State3>,
|
||||||
},
|
},
|
||||||
BtcPunished,
|
BtcPunished {
|
||||||
|
state3: Box<State3>,
|
||||||
|
},
|
||||||
SafelyAborted,
|
SafelyAborted,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +100,7 @@ impl fmt::Display for AliceState {
|
|||||||
AliceState::BtcRedeemed => write!(f, "btc is redeemed"),
|
AliceState::BtcRedeemed => write!(f, "btc is redeemed"),
|
||||||
AliceState::BtcCancelled { .. } => write!(f, "btc is cancelled"),
|
AliceState::BtcCancelled { .. } => write!(f, "btc is cancelled"),
|
||||||
AliceState::BtcRefunded { .. } => write!(f, "btc is refunded"),
|
AliceState::BtcRefunded { .. } => write!(f, "btc is refunded"),
|
||||||
AliceState::BtcPunished => write!(f, "btc is punished"),
|
AliceState::BtcPunished { .. } => write!(f, "btc is punished"),
|
||||||
AliceState::SafelyAborted => write!(f, "safely aborted"),
|
AliceState::SafelyAborted => write!(f, "safely aborted"),
|
||||||
AliceState::BtcPunishable { .. } => write!(f, "btc is punishable"),
|
AliceState::BtcPunishable { .. } => write!(f, "btc is punishable"),
|
||||||
AliceState::XmrRefunded => write!(f, "xmr is refunded"),
|
AliceState::XmrRefunded => write!(f, "xmr is refunded"),
|
||||||
@ -310,7 +312,8 @@ impl State2 {
|
|||||||
self.a.public(),
|
self.a.public(),
|
||||||
self.B,
|
self.B,
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)
|
||||||
|
.expect("valid cancel tx");
|
||||||
|
|
||||||
let tx_refund =
|
let tx_refund =
|
||||||
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
|
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
|
||||||
@ -335,7 +338,7 @@ impl State2 {
|
|||||||
self.a.public(),
|
self.a.public(),
|
||||||
self.B,
|
self.B,
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)?;
|
||||||
bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig)
|
bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig)
|
||||||
.context("Failed to verify cancel transaction")?;
|
.context("Failed to verify cancel transaction")?;
|
||||||
let tx_punish = bitcoin::TxPunish::new(
|
let tx_punish = bitcoin::TxPunish::new(
|
||||||
@ -376,13 +379,13 @@ impl State2 {
|
|||||||
pub struct State3 {
|
pub struct State3 {
|
||||||
a: bitcoin::SecretKey,
|
a: bitcoin::SecretKey,
|
||||||
B: bitcoin::PublicKey,
|
B: bitcoin::PublicKey,
|
||||||
s_a: monero::Scalar,
|
pub s_a: monero::Scalar,
|
||||||
S_b_monero: monero::PublicKey,
|
S_b_monero: monero::PublicKey,
|
||||||
S_b_bitcoin: bitcoin::PublicKey,
|
S_b_bitcoin: bitcoin::PublicKey,
|
||||||
pub v: monero::PrivateViewKey,
|
pub v: monero::PrivateViewKey,
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
btc: bitcoin::Amount,
|
pub btc: bitcoin::Amount,
|
||||||
xmr: monero::Amount,
|
pub xmr: monero::Amount,
|
||||||
pub cancel_timelock: CancelTimelock,
|
pub cancel_timelock: CancelTimelock,
|
||||||
pub punish_timelock: PunishTimelock,
|
pub punish_timelock: PunishTimelock,
|
||||||
refund_address: bitcoin::Address,
|
refund_address: bitcoin::Address,
|
||||||
@ -458,6 +461,7 @@ impl State3 {
|
|||||||
self.B,
|
self.B,
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
)
|
)
|
||||||
|
.expect("valid cancel tx")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tx_refund(&self) -> TxRefund {
|
pub fn tx_refund(&self) -> TxRefund {
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
//! Run an XMR/BTC swap in the role of Alice.
|
//! Run an XMR/BTC swap in the role of Alice.
|
||||||
//! Alice holds XMR and wishes receive BTC.
|
//! Alice holds XMR and wishes receive BTC.
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::asb::{EventLoopHandle, LatestRate};
|
use crate::asb::{EventLoopHandle, LatestRate};
|
||||||
use crate::bitcoin::ExpiredTimelocks;
|
use crate::bitcoin::ExpiredTimelocks;
|
||||||
use crate::env::Config;
|
use crate::env::Config;
|
||||||
use crate::protocol::alice::{AliceState, Swap};
|
use crate::protocol::alice::{AliceState, Swap};
|
||||||
use crate::{bitcoin, monero};
|
use crate::{bitcoin, monero};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
|
use backoff::ExponentialBackoffBuilder;
|
||||||
use tokio::select;
|
use tokio::select;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -111,23 +114,63 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
AliceState::BtcLocked { state3 } => {
|
AliceState::BtcLocked { state3 } => {
|
||||||
match state3.expired_timelocks(bitcoin_wallet).await? {
|
// We retry to lock the Monero wallet until we succeed or until the cancel timelock expires.
|
||||||
ExpiredTimelocks::None => {
|
//
|
||||||
// Record the current monero wallet block height so we don't have to scan from
|
// This is necessary because the monero-wallet-rpc can sometimes error out due to various reasons, such as
|
||||||
// block 0 for scenarios where we create a refund wallet.
|
// - no connection to the daemon
|
||||||
let monero_wallet_restore_blockheight = monero_wallet.block_height().await?;
|
// - "failed to get output distribution"
|
||||||
|
// See https://github.com/comit-network/xmr-btc-swap/issues/1726
|
||||||
|
let backoff = ExponentialBackoffBuilder::new()
|
||||||
|
.with_initial_interval(Duration::from_secs(5))
|
||||||
|
.with_max_interval(Duration::from_secs(60 * 3))
|
||||||
|
.with_max_elapsed_time(None)
|
||||||
|
.build();
|
||||||
|
|
||||||
let transfer_proof = monero_wallet
|
let result = backoff::future::retry_notify(
|
||||||
.transfer(state3.lock_xmr_transfer_request())
|
backoff,
|
||||||
.await?;
|
|| async {
|
||||||
|
match state3.expired_timelocks(bitcoin_wallet).await {
|
||||||
|
Ok(ExpiredTimelocks::None { .. }) => {
|
||||||
|
// Record the current monero wallet block height so we don't have to scan from
|
||||||
|
// block 0 for scenarios where we create a refund wallet.
|
||||||
|
let monero_wallet_restore_blockheight = monero_wallet
|
||||||
|
.block_height()
|
||||||
|
.await
|
||||||
|
.map_err(backoff::Error::transient)?;
|
||||||
|
|
||||||
|
let transfer_proof = monero_wallet
|
||||||
|
.transfer(state3.lock_xmr_transfer_request())
|
||||||
|
.await
|
||||||
|
.map_err(backoff::Error::transient)?;
|
||||||
|
|
||||||
|
Ok(Some((monero_wallet_restore_blockheight, transfer_proof)))
|
||||||
|
}
|
||||||
|
Ok(_) => Ok(None),
|
||||||
|
Err(e) => Err(backoff::Error::transient(e)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|err, delay: Duration| {
|
||||||
|
tracing::warn!(
|
||||||
|
%err,
|
||||||
|
delay_secs = delay.as_secs(),
|
||||||
|
"Failed to lock XMR. We will retry after a delay"
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(Some((monero_wallet_restore_blockheight, transfer_proof))) => {
|
||||||
AliceState::XmrLockTransactionSent {
|
AliceState::XmrLockTransactionSent {
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
transfer_proof,
|
transfer_proof,
|
||||||
state3,
|
state3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => AliceState::SafelyAborted,
|
Ok(None) => AliceState::SafelyAborted,
|
||||||
|
Err(e) => {
|
||||||
|
unreachable!("We should retry forever until the cancel timelock expires. But we got an error: {:#}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AliceState::XmrLockTransactionSent {
|
AliceState::XmrLockTransactionSent {
|
||||||
@ -135,7 +178,7 @@ where
|
|||||||
transfer_proof,
|
transfer_proof,
|
||||||
state3,
|
state3,
|
||||||
} => match state3.expired_timelocks(bitcoin_wallet).await? {
|
} => match state3.expired_timelocks(bitcoin_wallet).await? {
|
||||||
ExpiredTimelocks::None => {
|
ExpiredTimelocks::None { .. } => {
|
||||||
monero_wallet
|
monero_wallet
|
||||||
.watch_for_transfer(state3.lock_xmr_watch_request(transfer_proof.clone(), 1))
|
.watch_for_transfer(state3.lock_xmr_watch_request(transfer_proof.clone(), 1))
|
||||||
.await
|
.await
|
||||||
@ -221,7 +264,7 @@ where
|
|||||||
encrypted_signature,
|
encrypted_signature,
|
||||||
state3,
|
state3,
|
||||||
} => match state3.expired_timelocks(bitcoin_wallet).await? {
|
} => match state3.expired_timelocks(bitcoin_wallet).await? {
|
||||||
ExpiredTimelocks::None => {
|
ExpiredTimelocks::None { .. } => {
|
||||||
let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await;
|
let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await;
|
||||||
match state3.signed_redeem_transaction(*encrypted_signature) {
|
match state3.signed_redeem_transaction(*encrypted_signature) {
|
||||||
Ok(tx) => match bitcoin_wallet.broadcast(tx, "redeem").await {
|
Ok(tx) => match bitcoin_wallet.broadcast(tx, "redeem").await {
|
||||||
@ -362,7 +405,7 @@ where
|
|||||||
let punish = state3.punish_btc(bitcoin_wallet).await;
|
let punish = state3.punish_btc(bitcoin_wallet).await;
|
||||||
|
|
||||||
match punish {
|
match punish {
|
||||||
Ok(_) => AliceState::BtcPunished,
|
Ok(_) => AliceState::BtcPunished { state3 },
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
tracing::warn!("Failed to publish punish transaction: {:#}", error);
|
tracing::warn!("Failed to publish punish transaction: {:#}", error);
|
||||||
|
|
||||||
@ -392,17 +435,17 @@ where
|
|||||||
}
|
}
|
||||||
AliceState::XmrRefunded => AliceState::XmrRefunded,
|
AliceState::XmrRefunded => AliceState::XmrRefunded,
|
||||||
AliceState::BtcRedeemed => AliceState::BtcRedeemed,
|
AliceState::BtcRedeemed => AliceState::BtcRedeemed,
|
||||||
AliceState::BtcPunished => AliceState::BtcPunished,
|
AliceState::BtcPunished { state3 } => AliceState::BtcPunished { state3 },
|
||||||
AliceState::SafelyAborted => AliceState::SafelyAborted,
|
AliceState::SafelyAborted => AliceState::SafelyAborted,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_complete(state: &AliceState) -> bool {
|
pub fn is_complete(state: &AliceState) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
state,
|
state,
|
||||||
AliceState::XmrRefunded
|
AliceState::XmrRefunded
|
||||||
| AliceState::BtcRedeemed
|
| AliceState::BtcRedeemed
|
||||||
| AliceState::BtcPunished
|
| AliceState::BtcPunished { .. }
|
||||||
| AliceState::SafelyAborted
|
| AliceState::SafelyAborted
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,10 @@ use sigma_fun::ext::dl_secp256k1_ed25519_eq::CrossCurveDLEQProof;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||||
pub enum BobState {
|
pub enum BobState {
|
||||||
Started {
|
Started {
|
||||||
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
btc_amount: bitcoin::Amount,
|
btc_amount: bitcoin::Amount,
|
||||||
change_address: bitcoin::Address,
|
change_address: bitcoin::Address,
|
||||||
},
|
},
|
||||||
@ -47,6 +48,7 @@ pub enum BobState {
|
|||||||
tx_lock_id: bitcoin::Txid,
|
tx_lock_id: bitcoin::Txid,
|
||||||
},
|
},
|
||||||
BtcPunished {
|
BtcPunished {
|
||||||
|
state: State6,
|
||||||
tx_lock_id: bitcoin::Txid,
|
tx_lock_id: bitcoin::Txid,
|
||||||
},
|
},
|
||||||
SafelyAborted,
|
SafelyAborted,
|
||||||
@ -242,7 +244,7 @@ impl State1 {
|
|||||||
self.A,
|
self.A,
|
||||||
self.b.public(),
|
self.b.public(),
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)?;
|
||||||
let tx_refund =
|
let tx_refund =
|
||||||
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
|
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
|
||||||
|
|
||||||
@ -287,13 +289,13 @@ pub struct State2 {
|
|||||||
S_a_monero: monero::PublicKey,
|
S_a_monero: monero::PublicKey,
|
||||||
S_a_bitcoin: bitcoin::PublicKey,
|
S_a_bitcoin: bitcoin::PublicKey,
|
||||||
v: monero::PrivateViewKey,
|
v: monero::PrivateViewKey,
|
||||||
xmr: monero::Amount,
|
pub xmr: monero::Amount,
|
||||||
cancel_timelock: CancelTimelock,
|
pub cancel_timelock: CancelTimelock,
|
||||||
punish_timelock: PunishTimelock,
|
pub punish_timelock: PunishTimelock,
|
||||||
refund_address: bitcoin::Address,
|
pub refund_address: bitcoin::Address,
|
||||||
redeem_address: bitcoin::Address,
|
redeem_address: bitcoin::Address,
|
||||||
punish_address: bitcoin::Address,
|
punish_address: bitcoin::Address,
|
||||||
tx_lock: bitcoin::TxLock,
|
pub tx_lock: bitcoin::TxLock,
|
||||||
tx_cancel_sig_a: Signature,
|
tx_cancel_sig_a: Signature,
|
||||||
tx_refund_encsig: bitcoin::EncryptedSignature,
|
tx_refund_encsig: bitcoin::EncryptedSignature,
|
||||||
min_monero_confirmations: u64,
|
min_monero_confirmations: u64,
|
||||||
@ -302,9 +304,9 @@ pub struct State2 {
|
|||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
tx_punish_fee: bitcoin::Amount,
|
tx_punish_fee: bitcoin::Amount,
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
tx_refund_fee: bitcoin::Amount,
|
pub tx_refund_fee: bitcoin::Amount,
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
tx_cancel_fee: bitcoin::Amount,
|
pub tx_cancel_fee: bitcoin::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State2 {
|
impl State2 {
|
||||||
@ -315,7 +317,8 @@ impl State2 {
|
|||||||
self.A,
|
self.A,
|
||||||
self.b.public(),
|
self.b.public(),
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)
|
||||||
|
.expect("valid cancel tx");
|
||||||
let tx_cancel_sig = self.b.sign(tx_cancel.digest());
|
let tx_cancel_sig = self.b.sign(tx_cancel.digest());
|
||||||
let tx_punish = bitcoin::TxPunish::new(
|
let tx_punish = bitcoin::TxPunish::new(
|
||||||
&tx_cancel,
|
&tx_cancel,
|
||||||
@ -366,7 +369,7 @@ pub struct State3 {
|
|||||||
S_a_monero: monero::PublicKey,
|
S_a_monero: monero::PublicKey,
|
||||||
S_a_bitcoin: bitcoin::PublicKey,
|
S_a_bitcoin: bitcoin::PublicKey,
|
||||||
v: monero::PrivateViewKey,
|
v: monero::PrivateViewKey,
|
||||||
xmr: monero::Amount,
|
pub xmr: monero::Amount,
|
||||||
pub cancel_timelock: CancelTimelock,
|
pub cancel_timelock: CancelTimelock,
|
||||||
punish_timelock: PunishTimelock,
|
punish_timelock: PunishTimelock,
|
||||||
refund_address: bitcoin::Address,
|
refund_address: bitcoin::Address,
|
||||||
@ -419,11 +422,13 @@ impl State3 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cancel(&self) -> State6 {
|
pub fn cancel(&self, monero_wallet_restore_blockheight: BlockHeight) -> State6 {
|
||||||
State6 {
|
State6 {
|
||||||
A: self.A,
|
A: self.A,
|
||||||
b: self.b.clone(),
|
b: self.b.clone(),
|
||||||
s_b: self.s_b,
|
s_b: self.s_b,
|
||||||
|
v: self.v,
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
cancel_timelock: self.cancel_timelock,
|
cancel_timelock: self.cancel_timelock,
|
||||||
punish_timelock: self.punish_timelock,
|
punish_timelock: self.punish_timelock,
|
||||||
refund_address: self.refund_address.clone(),
|
refund_address: self.refund_address.clone(),
|
||||||
@ -439,7 +444,7 @@ impl State3 {
|
|||||||
self.tx_lock.txid()
|
self.tx_lock.txid()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn current_epoch(
|
pub async fn expired_timelock(
|
||||||
&self,
|
&self,
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
) -> Result<ExpiredTimelocks> {
|
) -> Result<ExpiredTimelocks> {
|
||||||
@ -449,7 +454,7 @@ impl State3 {
|
|||||||
self.A,
|
self.A,
|
||||||
self.b.public(),
|
self.b.public(),
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
|
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
|
||||||
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
|
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
|
||||||
@ -461,6 +466,19 @@ impl State3 {
|
|||||||
tx_cancel_status,
|
tx_cancel_status,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
pub fn attempt_cooperative_redeem(
|
||||||
|
&self,
|
||||||
|
s_a: monero::PrivateKey,
|
||||||
|
monero_wallet_restore_blockheight: BlockHeight,
|
||||||
|
) -> State5 {
|
||||||
|
State5 {
|
||||||
|
s_a,
|
||||||
|
s_b: self.s_b,
|
||||||
|
v: self.v,
|
||||||
|
tx_lock: self.tx_lock.clone(),
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||||
@ -487,6 +505,27 @@ pub struct State4 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl State4 {
|
impl State4 {
|
||||||
|
pub async fn check_for_tx_redeem(&self, bitcoin_wallet: &bitcoin::Wallet) -> Result<State5> {
|
||||||
|
let tx_redeem =
|
||||||
|
bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address, self.tx_redeem_fee);
|
||||||
|
let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin, tx_redeem.digest());
|
||||||
|
|
||||||
|
let tx_redeem_candidate = bitcoin_wallet.get_raw_transaction(tx_redeem.txid()).await?;
|
||||||
|
|
||||||
|
let tx_redeem_sig =
|
||||||
|
tx_redeem.extract_signature_by_key(tx_redeem_candidate, self.b.public())?;
|
||||||
|
let s_a = bitcoin::recover(self.S_a_bitcoin, tx_redeem_sig, tx_redeem_encsig)?;
|
||||||
|
let s_a = monero::private_key_from_secp256k1_scalar(s_a.into());
|
||||||
|
|
||||||
|
Ok(State5 {
|
||||||
|
s_a,
|
||||||
|
s_b: self.s_b,
|
||||||
|
v: self.v,
|
||||||
|
tx_lock: self.tx_lock.clone(),
|
||||||
|
monero_wallet_restore_blockheight: self.monero_wallet_restore_blockheight,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn tx_redeem_encsig(&self) -> bitcoin::EncryptedSignature {
|
pub fn tx_redeem_encsig(&self) -> bitcoin::EncryptedSignature {
|
||||||
let tx_redeem =
|
let tx_redeem =
|
||||||
bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address, self.tx_redeem_fee);
|
bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address, self.tx_redeem_fee);
|
||||||
@ -530,7 +569,7 @@ impl State4 {
|
|||||||
self.A,
|
self.A,
|
||||||
self.b.public(),
|
self.b.public(),
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
|
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
|
||||||
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
|
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
|
||||||
@ -548,6 +587,8 @@ impl State4 {
|
|||||||
A: self.A,
|
A: self.A,
|
||||||
b: self.b,
|
b: self.b,
|
||||||
s_b: self.s_b,
|
s_b: self.s_b,
|
||||||
|
v: self.v,
|
||||||
|
monero_wallet_restore_blockheight: self.monero_wallet_restore_blockheight,
|
||||||
cancel_timelock: self.cancel_timelock,
|
cancel_timelock: self.cancel_timelock,
|
||||||
punish_timelock: self.punish_timelock,
|
punish_timelock: self.punish_timelock,
|
||||||
refund_address: self.refund_address,
|
refund_address: self.refund_address,
|
||||||
@ -581,6 +622,43 @@ impl State5 {
|
|||||||
pub fn tx_lock_id(&self) -> bitcoin::Txid {
|
pub fn tx_lock_id(&self) -> bitcoin::Txid {
|
||||||
self.tx_lock.txid()
|
self.tx_lock.txid()
|
||||||
}
|
}
|
||||||
|
pub async fn redeem_xmr(
|
||||||
|
&self,
|
||||||
|
monero_wallet: &monero::Wallet,
|
||||||
|
wallet_file_name: std::string::String,
|
||||||
|
monero_receive_address: monero::Address,
|
||||||
|
) -> Result<()> {
|
||||||
|
let (spend_key, view_key) = self.xmr_keys();
|
||||||
|
|
||||||
|
tracing::info!(%wallet_file_name, "Generating and opening Monero wallet from the extracted keys to redeem the Monero");
|
||||||
|
if let Err(e) = monero_wallet
|
||||||
|
.create_from_and_load(
|
||||||
|
wallet_file_name.clone(),
|
||||||
|
spend_key,
|
||||||
|
view_key,
|
||||||
|
self.monero_wallet_restore_blockheight,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
// In case we failed to refresh/sweep, when resuming the wallet might already
|
||||||
|
// exist! This is a very unlikely scenario, but if we don't take care of it we
|
||||||
|
// might not be able to ever transfer the Monero.
|
||||||
|
tracing::warn!("Failed to generate monero wallet from keys: {:#}", e);
|
||||||
|
tracing::info!(%wallet_file_name,
|
||||||
|
"Falling back to trying to open the wallet if it already exists",
|
||||||
|
);
|
||||||
|
monero_wallet.open(wallet_file_name).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the generated wallet is synced so we have a proper balance
|
||||||
|
monero_wallet.refresh(20).await?;
|
||||||
|
// Sweep (transfer all funds) to the given address
|
||||||
|
let tx_hashes = monero_wallet.sweep_all(monero_receive_address).await?;
|
||||||
|
for tx_hash in tx_hashes {
|
||||||
|
tracing::info!(%monero_receive_address, txid=%tx_hash.0, "Successfully transferred XMR to wallet");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||||
@ -588,6 +666,8 @@ pub struct State6 {
|
|||||||
A: bitcoin::PublicKey,
|
A: bitcoin::PublicKey,
|
||||||
b: bitcoin::SecretKey,
|
b: bitcoin::SecretKey,
|
||||||
s_b: monero::Scalar,
|
s_b: monero::Scalar,
|
||||||
|
v: monero::PrivateViewKey,
|
||||||
|
pub monero_wallet_restore_blockheight: BlockHeight,
|
||||||
cancel_timelock: CancelTimelock,
|
cancel_timelock: CancelTimelock,
|
||||||
punish_timelock: PunishTimelock,
|
punish_timelock: PunishTimelock,
|
||||||
refund_address: bitcoin::Address,
|
refund_address: bitcoin::Address,
|
||||||
@ -611,7 +691,7 @@ impl State6 {
|
|||||||
self.A,
|
self.A,
|
||||||
self.b.public(),
|
self.b.public(),
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
|
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
|
||||||
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
|
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
|
||||||
@ -623,18 +703,20 @@ impl State6 {
|
|||||||
tx_cancel_status,
|
tx_cancel_status,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
pub fn construct_tx_cancel(&self) -> Result<bitcoin::TxCancel> {
|
||||||
pub async fn check_for_tx_cancel(
|
bitcoin::TxCancel::new(
|
||||||
&self,
|
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
|
||||||
) -> Result<Transaction> {
|
|
||||||
let tx_cancel = bitcoin::TxCancel::new(
|
|
||||||
&self.tx_lock,
|
&self.tx_lock,
|
||||||
self.cancel_timelock,
|
self.cancel_timelock,
|
||||||
self.A,
|
self.A,
|
||||||
self.b.public(),
|
self.b.public(),
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
);
|
)
|
||||||
|
}
|
||||||
|
pub async fn check_for_tx_cancel(
|
||||||
|
&self,
|
||||||
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
|
) -> Result<Transaction> {
|
||||||
|
let tx_cancel = self.construct_tx_cancel()?;
|
||||||
|
|
||||||
let tx = bitcoin_wallet.get_raw_transaction(tx_cancel.txid()).await?;
|
let tx = bitcoin_wallet.get_raw_transaction(tx_cancel.txid()).await?;
|
||||||
|
|
||||||
@ -645,15 +727,10 @@ impl State6 {
|
|||||||
&self,
|
&self,
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
) -> Result<(Txid, Subscription)> {
|
) -> Result<(Txid, Subscription)> {
|
||||||
let transaction = bitcoin::TxCancel::new(
|
let transaction = self
|
||||||
&self.tx_lock,
|
.construct_tx_cancel()?
|
||||||
self.cancel_timelock,
|
.complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone())
|
||||||
self.A,
|
.context("Failed to complete Bitcoin cancel transaction")?;
|
||||||
self.b.public(),
|
|
||||||
self.tx_cancel_fee,
|
|
||||||
)
|
|
||||||
.complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone())
|
|
||||||
.context("Failed to complete Bitcoin cancel transaction")?;
|
|
||||||
|
|
||||||
let (tx_id, subscription) = bitcoin_wallet.broadcast(transaction, "cancel").await?;
|
let (tx_id, subscription) = bitcoin_wallet.broadcast(transaction, "cancel").await?;
|
||||||
|
|
||||||
@ -668,13 +745,7 @@ impl State6 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn signed_refund_transaction(&self) -> Result<Transaction> {
|
pub fn signed_refund_transaction(&self) -> Result<Transaction> {
|
||||||
let tx_cancel = bitcoin::TxCancel::new(
|
let tx_cancel = self.construct_tx_cancel()?;
|
||||||
&self.tx_lock,
|
|
||||||
self.cancel_timelock,
|
|
||||||
self.A,
|
|
||||||
self.b.public(),
|
|
||||||
self.tx_cancel_fee,
|
|
||||||
);
|
|
||||||
let tx_refund =
|
let tx_refund =
|
||||||
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
|
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
|
||||||
|
|
||||||
@ -692,4 +763,13 @@ impl State6 {
|
|||||||
pub fn tx_lock_id(&self) -> bitcoin::Txid {
|
pub fn tx_lock_id(&self) -> bitcoin::Txid {
|
||||||
self.tx_lock.txid()
|
self.tx_lock.txid()
|
||||||
}
|
}
|
||||||
|
pub fn attempt_cooperative_redeem(&self, s_a: monero::PrivateKey) -> State5 {
|
||||||
|
State5 {
|
||||||
|
s_a,
|
||||||
|
s_b: self.s_b,
|
||||||
|
v: self.v,
|
||||||
|
tx_lock: self.tx_lock.clone(),
|
||||||
|
monero_wallet_restore_blockheight: self.monero_wallet_restore_blockheight,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,33 @@
|
|||||||
use crate::bitcoin::{ExpiredTimelocks, TxCancel, TxRefund};
|
use crate::bitcoin::{ExpiredTimelocks, TxCancel, TxRefund};
|
||||||
use crate::cli::EventLoopHandle;
|
use crate::cli::EventLoopHandle;
|
||||||
|
use crate::network::cooperative_xmr_redeem_after_punish::Response::{Fullfilled, Rejected};
|
||||||
use crate::network::swap_setup::bob::NewSwap;
|
use crate::network::swap_setup::bob::NewSwap;
|
||||||
use crate::protocol::bob;
|
|
||||||
use crate::protocol::bob::state::*;
|
use crate::protocol::bob::state::*;
|
||||||
|
use crate::protocol::{bob, Database};
|
||||||
use crate::{bitcoin, monero};
|
use crate::{bitcoin, monero};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
|
use std::sync::Arc;
|
||||||
use tokio::select;
|
use tokio::select;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn is_complete(state: &BobState) -> bool {
|
pub fn is_complete(state: &BobState) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
state,
|
state,
|
||||||
BobState::BtcRefunded(..)
|
BobState::BtcRefunded(..) | BobState::XmrRedeemed { .. } | BobState::SafelyAborted
|
||||||
| BobState::XmrRedeemed { .. }
|
|
||||||
| BobState::BtcPunished { .. }
|
|
||||||
| BobState::SafelyAborted
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Identifies states that should be run at most once before exiting.
|
||||||
|
// This is used to prevent infinite retry loops while still allowing manual resumption.
|
||||||
|
//
|
||||||
|
// Currently, this applies to the BtcPunished state:
|
||||||
|
// - We want to attempt recovery via cooperative XMR redeem once.
|
||||||
|
// - If unsuccessful, we exit to avoid an infinite retry loop.
|
||||||
|
// - The swap can still be manually resumed later and retried if desired.
|
||||||
|
pub fn is_run_at_most_once(state: &BobState) -> bool {
|
||||||
|
matches!(state, BobState::BtcPunished { .. })
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub async fn run(swap: bob::Swap) -> Result<BobState> {
|
pub async fn run(swap: bob::Swap) -> Result<BobState> {
|
||||||
run_until(swap, is_complete).await
|
run_until(swap, is_complete).await
|
||||||
@ -27,13 +37,14 @@ pub async fn run_until(
|
|||||||
mut swap: bob::Swap,
|
mut swap: bob::Swap,
|
||||||
is_target_state: fn(&BobState) -> bool,
|
is_target_state: fn(&BobState) -> bool,
|
||||||
) -> Result<BobState> {
|
) -> Result<BobState> {
|
||||||
let mut current_state = swap.state;
|
let mut current_state = swap.state.clone();
|
||||||
|
|
||||||
while !is_target_state(¤t_state) {
|
while !is_target_state(¤t_state) {
|
||||||
current_state = next_state(
|
let next_state = next_state(
|
||||||
swap.id,
|
swap.id,
|
||||||
current_state.clone(),
|
current_state.clone(),
|
||||||
&mut swap.event_loop_handle,
|
&mut swap.event_loop_handle,
|
||||||
|
swap.db.clone(),
|
||||||
swap.bitcoin_wallet.as_ref(),
|
swap.bitcoin_wallet.as_ref(),
|
||||||
swap.monero_wallet.as_ref(),
|
swap.monero_wallet.as_ref(),
|
||||||
swap.monero_receive_address,
|
swap.monero_receive_address,
|
||||||
@ -41,8 +52,14 @@ pub async fn run_until(
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
swap.db
|
swap.db
|
||||||
.insert_latest_state(swap.id, current_state.clone().into())
|
.insert_latest_state(swap.id, next_state.clone().into())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
if is_run_at_most_once(¤t_state) && next_state == current_state {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_state = next_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(current_state)
|
Ok(current_state)
|
||||||
@ -52,6 +69,7 @@ async fn next_state(
|
|||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
state: BobState,
|
state: BobState,
|
||||||
event_loop_handle: &mut EventLoopHandle,
|
event_loop_handle: &mut EventLoopHandle,
|
||||||
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
monero_wallet: &monero::Wallet,
|
monero_wallet: &monero::Wallet,
|
||||||
monero_receive_address: monero::Address,
|
monero_receive_address: monero::Address,
|
||||||
@ -117,13 +135,29 @@ async fn next_state(
|
|||||||
} => {
|
} => {
|
||||||
let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await;
|
let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await;
|
||||||
|
|
||||||
if let ExpiredTimelocks::None = state3.current_epoch(bitcoin_wallet).await? {
|
if let ExpiredTimelocks::None { .. } = state3.expired_timelock(bitcoin_wallet).await? {
|
||||||
|
tracing::info!("Waiting for Alice to lock Monero");
|
||||||
|
|
||||||
|
let buffered_transfer_proof = db
|
||||||
|
.get_buffered_transfer_proof(swap_id)
|
||||||
|
.await
|
||||||
|
.context("Failed to get buffered transfer proof")?;
|
||||||
|
|
||||||
|
if let Some(transfer_proof) = buffered_transfer_proof {
|
||||||
|
tracing::debug!(txid = %transfer_proof.tx_hash(), "Found buffered transfer proof");
|
||||||
|
tracing::info!(txid = %transfer_proof.tx_hash(), "Alice locked Monero");
|
||||||
|
|
||||||
|
return Ok(BobState::XmrLockProofReceived {
|
||||||
|
state: state3,
|
||||||
|
lock_transfer_proof: transfer_proof,
|
||||||
|
monero_wallet_restore_blockheight,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let transfer_proof_watcher = event_loop_handle.recv_transfer_proof();
|
let transfer_proof_watcher = event_loop_handle.recv_transfer_proof();
|
||||||
let cancel_timelock_expires =
|
let cancel_timelock_expires =
|
||||||
tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock);
|
tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock);
|
||||||
|
|
||||||
tracing::info!("Waiting for Alice to lock Monero");
|
|
||||||
|
|
||||||
select! {
|
select! {
|
||||||
transfer_proof = transfer_proof_watcher => {
|
transfer_proof = transfer_proof_watcher => {
|
||||||
let transfer_proof = transfer_proof?;
|
let transfer_proof = transfer_proof?;
|
||||||
@ -140,12 +174,12 @@ async fn next_state(
|
|||||||
result?;
|
result?;
|
||||||
tracing::info!("Alice took too long to lock Monero, cancelling the swap");
|
tracing::info!("Alice took too long to lock Monero, cancelling the swap");
|
||||||
|
|
||||||
let state4 = state3.cancel();
|
let state4 = state3.cancel(monero_wallet_restore_blockheight);
|
||||||
BobState::CancelTimelockExpired(state4)
|
BobState::CancelTimelockExpired(state4)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let state4 = state3.cancel();
|
let state4 = state3.cancel(monero_wallet_restore_blockheight);
|
||||||
BobState::CancelTimelockExpired(state4)
|
BobState::CancelTimelockExpired(state4)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,7 +190,7 @@ async fn next_state(
|
|||||||
} => {
|
} => {
|
||||||
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
|
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
|
||||||
|
|
||||||
if let ExpiredTimelocks::None = state.current_epoch(bitcoin_wallet).await? {
|
if let ExpiredTimelocks::None { .. } = state.expired_timelock(bitcoin_wallet).await? {
|
||||||
let watch_request = state.lock_xmr_watch_request(lock_transfer_proof);
|
let watch_request = state.lock_xmr_watch_request(lock_transfer_proof);
|
||||||
|
|
||||||
select! {
|
select! {
|
||||||
@ -169,23 +203,30 @@ async fn next_state(
|
|||||||
|
|
||||||
tx_lock_status.wait_until_confirmed_with(state.cancel_timelock).await?;
|
tx_lock_status.wait_until_confirmed_with(state.cancel_timelock).await?;
|
||||||
|
|
||||||
BobState::CancelTimelockExpired(state.cancel())
|
BobState::CancelTimelockExpired(state.cancel(monero_wallet_restore_blockheight))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = tx_lock_status.wait_until_confirmed_with(state.cancel_timelock) => {
|
result = tx_lock_status.wait_until_confirmed_with(state.cancel_timelock) => {
|
||||||
result?;
|
result?;
|
||||||
BobState::CancelTimelockExpired(state.cancel())
|
BobState::CancelTimelockExpired(state.cancel(monero_wallet_restore_blockheight))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
BobState::CancelTimelockExpired(state.cancel())
|
BobState::CancelTimelockExpired(state.cancel(monero_wallet_restore_blockheight))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BobState::XmrLocked(state) => {
|
BobState::XmrLocked(state) => {
|
||||||
|
// In case we send the encrypted signature to Alice, but she doesn't give us a confirmation
|
||||||
|
// We need to check if she still published the Bitcoin redeem transaction
|
||||||
|
// Otherwise we risk staying stuck in "XmrLocked"
|
||||||
|
if let Ok(state5) = state.check_for_tx_redeem(bitcoin_wallet).await {
|
||||||
|
return Ok(BobState::BtcRedeemed(state5));
|
||||||
|
}
|
||||||
|
|
||||||
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
|
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
|
||||||
|
|
||||||
if let ExpiredTimelocks::None = state.expired_timelock(bitcoin_wallet).await? {
|
if let ExpiredTimelocks::None { .. } = state.expired_timelock(bitcoin_wallet).await? {
|
||||||
// Alice has locked Xmr
|
// Alice has locked Xmr
|
||||||
// Bob sends Alice his key
|
// Bob sends Alice his key
|
||||||
|
|
||||||
@ -207,9 +248,16 @@ async fn next_state(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
BobState::EncSigSent(state) => {
|
BobState::EncSigSent(state) => {
|
||||||
|
// We need to make sure that Alice did not publish the redeem transaction while we were offline
|
||||||
|
// Even if the cancel timelock expired, if Alice published the redeem transaction while we were away we cannot miss it
|
||||||
|
// If we do we cannot refund and will never be able to leave the "CancelTimelockExpired" state
|
||||||
|
if let Ok(state5) = state.check_for_tx_redeem(bitcoin_wallet).await {
|
||||||
|
return Ok(BobState::BtcRedeemed(state5));
|
||||||
|
}
|
||||||
|
|
||||||
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
|
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
|
||||||
|
|
||||||
if let ExpiredTimelocks::None = state.expired_timelock(bitcoin_wallet).await? {
|
if let ExpiredTimelocks::None { .. } = state.expired_timelock(bitcoin_wallet).await? {
|
||||||
select! {
|
select! {
|
||||||
state5 = state.watch_for_redeem_btc(bitcoin_wallet) => {
|
state5 = state.watch_for_redeem_btc(bitcoin_wallet) => {
|
||||||
BobState::BtcRedeemed(state5?)
|
BobState::BtcRedeemed(state5?)
|
||||||
@ -224,36 +272,9 @@ async fn next_state(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
BobState::BtcRedeemed(state) => {
|
BobState::BtcRedeemed(state) => {
|
||||||
let (spend_key, view_key) = state.xmr_keys();
|
state
|
||||||
|
.redeem_xmr(monero_wallet, swap_id.to_string(), monero_receive_address)
|
||||||
let wallet_file_name = swap_id.to_string();
|
.await?;
|
||||||
if let Err(e) = monero_wallet
|
|
||||||
.create_from_and_load(
|
|
||||||
wallet_file_name.clone(),
|
|
||||||
spend_key,
|
|
||||||
view_key,
|
|
||||||
state.monero_wallet_restore_blockheight,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
// In case we failed to refresh/sweep, when resuming the wallet might already
|
|
||||||
// exist! This is a very unlikely scenario, but if we don't take care of it we
|
|
||||||
// might not be able to ever transfer the Monero.
|
|
||||||
tracing::warn!("Failed to generate monero wallet from keys: {:#}", e);
|
|
||||||
tracing::info!(%wallet_file_name,
|
|
||||||
"Falling back to trying to open the the wallet if it already exists",
|
|
||||||
);
|
|
||||||
monero_wallet.open(wallet_file_name).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that the generated wallet is synced so we have a proper balance
|
|
||||||
monero_wallet.refresh().await?;
|
|
||||||
// Sweep (transfer all funds) to the given address
|
|
||||||
let tx_hashes = monero_wallet.sweep_all(monero_receive_address).await?;
|
|
||||||
|
|
||||||
for tx_hash in tx_hashes {
|
|
||||||
tracing::info!(%monero_receive_address, txid=%tx_hash.0, "Successfully transferred XMR to wallet");
|
|
||||||
}
|
|
||||||
|
|
||||||
BobState::XmrRedeemed {
|
BobState::XmrRedeemed {
|
||||||
tx_lock_id: state.tx_lock_id(),
|
tx_lock_id: state.tx_lock_id(),
|
||||||
@ -269,12 +290,12 @@ async fn next_state(
|
|||||||
BobState::BtcCancelled(state) => {
|
BobState::BtcCancelled(state) => {
|
||||||
// Bob has cancelled the swap
|
// Bob has cancelled the swap
|
||||||
match state.expired_timelock(bitcoin_wallet).await? {
|
match state.expired_timelock(bitcoin_wallet).await? {
|
||||||
ExpiredTimelocks::None => {
|
ExpiredTimelocks::None { .. } => {
|
||||||
bail!(
|
bail!(
|
||||||
"Internal error: canceled state reached before cancel timelock was expired"
|
"Internal error: canceled state reached before cancel timelock was expired"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
ExpiredTimelocks::Cancel => {
|
ExpiredTimelocks::Cancel { .. } => {
|
||||||
state.publish_refund_btc(bitcoin_wallet).await?;
|
state.publish_refund_btc(bitcoin_wallet).await?;
|
||||||
BobState::BtcRefunded(state)
|
BobState::BtcRefunded(state)
|
||||||
}
|
}
|
||||||
@ -282,12 +303,58 @@ async fn next_state(
|
|||||||
tracing::info!("You have been punished for not refunding in time");
|
tracing::info!("You have been punished for not refunding in time");
|
||||||
BobState::BtcPunished {
|
BobState::BtcPunished {
|
||||||
tx_lock_id: state.tx_lock_id(),
|
tx_lock_id: state.tx_lock_id(),
|
||||||
|
state,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BobState::BtcRefunded(state4) => BobState::BtcRefunded(state4),
|
BobState::BtcRefunded(state4) => BobState::BtcRefunded(state4),
|
||||||
BobState::BtcPunished { tx_lock_id } => BobState::BtcPunished { tx_lock_id },
|
BobState::BtcPunished { state, tx_lock_id } => {
|
||||||
|
tracing::info!("Attempting to cooperatively redeem XMR after being punished");
|
||||||
|
let response = event_loop_handle
|
||||||
|
.request_cooperative_xmr_redeem(swap_id)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match response {
|
||||||
|
Ok(Fullfilled { s_a, .. }) => {
|
||||||
|
tracing::info!(
|
||||||
|
"Alice has accepted our request to cooperatively redeem the XMR"
|
||||||
|
);
|
||||||
|
|
||||||
|
let s_a = monero::PrivateKey { scalar: s_a };
|
||||||
|
let state5 = state.attempt_cooperative_redeem(s_a);
|
||||||
|
|
||||||
|
match state5
|
||||||
|
.redeem_xmr(monero_wallet, swap_id.to_string(), monero_receive_address)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
return Ok(BobState::XmrRedeemed { tx_lock_id });
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
return Err(error)
|
||||||
|
.context("Failed to redeem XMR with revealed XMR key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Rejected { reason, .. }) => {
|
||||||
|
tracing::error!(
|
||||||
|
%reason,
|
||||||
|
"Alice rejected our request for cooperative XMR redeem"
|
||||||
|
);
|
||||||
|
return Err(reason)
|
||||||
|
.context("Alice rejected our request for cooperative XMR redeem");
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
tracing::error!(
|
||||||
|
%error,
|
||||||
|
"Failed to request cooperative XMR redeem from Alice"
|
||||||
|
);
|
||||||
|
return Err(error)
|
||||||
|
.context("Failed to request cooperative XMR redeem from Alice");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
BobState::SafelyAborted => BobState::SafelyAborted,
|
BobState::SafelyAborted => BobState::SafelyAborted,
|
||||||
BobState::XmrRedeemed { tx_lock_id } => BobState::XmrRedeemed { tx_lock_id },
|
BobState::XmrRedeemed { tx_lock_id } => BobState::XmrRedeemed { tx_lock_id },
|
||||||
})
|
})
|
||||||
|
31
swap/src/rpc.rs
Normal file
31
swap/src/rpc.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use crate::api::Context;
|
||||||
|
use jsonrpsee::server::{RpcModule, ServerBuilder, ServerHandle};
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
pub mod methods;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Could not parse key value from params")]
|
||||||
|
ParseError,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run_server(
|
||||||
|
server_address: SocketAddr,
|
||||||
|
context: Arc<Context>,
|
||||||
|
) -> anyhow::Result<(SocketAddr, ServerHandle)> {
|
||||||
|
let server = ServerBuilder::default().build(server_address).await?;
|
||||||
|
let mut modules = RpcModule::new(());
|
||||||
|
{
|
||||||
|
modules
|
||||||
|
.merge(methods::register_modules(Arc::clone(&context))?)
|
||||||
|
.expect("Could not register RPC modules")
|
||||||
|
}
|
||||||
|
|
||||||
|
let addr = server.local_addr()?;
|
||||||
|
let server_handle = server.start(modules)?;
|
||||||
|
|
||||||
|
Ok((addr, server_handle))
|
||||||
|
}
|
269
swap/src/rpc/methods.rs
Normal file
269
swap/src/rpc/methods.rs
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
use crate::api::request::{Method, Request};
|
||||||
|
use crate::api::Context;
|
||||||
|
use crate::bitcoin::bitcoin_address;
|
||||||
|
use crate::monero::monero_address;
|
||||||
|
use crate::{bitcoin, monero};
|
||||||
|
use anyhow::Result;
|
||||||
|
use jsonrpsee::server::RpcModule;
|
||||||
|
use jsonrpsee::types::Params;
|
||||||
|
use libp2p::core::Multiaddr;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>> {
|
||||||
|
let mut module = RpcModule::new(context);
|
||||||
|
|
||||||
|
module.register_async_method("suspend_current_swap", |params, context| async move {
|
||||||
|
execute_request(params, Method::SuspendCurrentSwap, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("get_swap_info", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let swap_id = params
|
||||||
|
.get("swap_id")
|
||||||
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?;
|
||||||
|
|
||||||
|
let swap_id = as_uuid(swap_id)
|
||||||
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||||
|
|
||||||
|
execute_request(params_raw, Method::GetSwapInfo { swap_id }, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("get_bitcoin_balance", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let force_refresh = params
|
||||||
|
.get("force_refresh")
|
||||||
|
.ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Does not contain force_refresh".to_string())
|
||||||
|
})?
|
||||||
|
.as_bool()
|
||||||
|
.ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
execute_request(params_raw, Method::Balance { force_refresh }, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("get_history", |params_raw, context| async move {
|
||||||
|
execute_request(params_raw, Method::History, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("get_logs", |params_raw, context| async move {
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
struct Params {
|
||||||
|
swap_id: Option<Uuid>,
|
||||||
|
logs_dir: Option<PathBuf>,
|
||||||
|
redact: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
let params: Params = params_raw.parse()?;
|
||||||
|
|
||||||
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::Logs {
|
||||||
|
swap_id: params.swap_id,
|
||||||
|
logs_dir: params.logs_dir,
|
||||||
|
redact: params.redact,
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("get_raw_states", |params, context| async move {
|
||||||
|
execute_request(params, Method::GetRawStates, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("resume_swap", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let swap_id = params
|
||||||
|
.get("swap_id")
|
||||||
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?;
|
||||||
|
|
||||||
|
let swap_id = as_uuid(swap_id)
|
||||||
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||||
|
|
||||||
|
execute_request(params_raw, Method::Resume { swap_id }, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("cancel_refund_swap", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let swap_id = params
|
||||||
|
.get("swap_id")
|
||||||
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?;
|
||||||
|
|
||||||
|
let swap_id = as_uuid(swap_id)
|
||||||
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||||
|
|
||||||
|
execute_request(params_raw, Method::CancelAndRefund { swap_id }, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method(
|
||||||
|
"get_monero_recovery_info",
|
||||||
|
|params_raw, context| async move {
|
||||||
|
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let swap_id = as_uuid(swap_id).ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
execute_request(params_raw, Method::MoneroRecovery { swap_id }, &context).await
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
module.register_async_method("withdraw_btc", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, String> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let amount = if let Some(amount_str) = params.get("amount") {
|
||||||
|
Some(
|
||||||
|
::bitcoin::Amount::from_str_in(amount_str, ::bitcoin::Denomination::Bitcoin)
|
||||||
|
.map_err(|_| {
|
||||||
|
jsonrpsee_core::Error::Custom("Unable to parse amount".to_string())
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let withdraw_address =
|
||||||
|
bitcoin::Address::from_str(params.get("address").ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Does not contain address".to_string())
|
||||||
|
})?)
|
||||||
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||||
|
let withdraw_address =
|
||||||
|
bitcoin_address::validate(withdraw_address, context.config.env_config.bitcoin_network)?;
|
||||||
|
|
||||||
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::WithdrawBtc {
|
||||||
|
amount,
|
||||||
|
address: withdraw_address,
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("buy_xmr", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, String> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let bitcoin_change_address = params
|
||||||
|
.get("bitcoin_change_address")
|
||||||
|
.map(|addr_str| {
|
||||||
|
bitcoin::Address::from_str(addr_str)
|
||||||
|
.map_err(|err| {
|
||||||
|
jsonrpsee_core::Error::Custom(format!(
|
||||||
|
"Could not parse bitcoin address: {}",
|
||||||
|
err
|
||||||
|
))
|
||||||
|
})
|
||||||
|
.and_then(|address| {
|
||||||
|
bitcoin_address::validate(
|
||||||
|
address,
|
||||||
|
context.config.env_config.bitcoin_network,
|
||||||
|
)
|
||||||
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
let monero_receive_address =
|
||||||
|
monero::Address::from_str(params.get("monero_receive_address").ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Does not contain monero_receiveaddress".to_string())
|
||||||
|
})?)
|
||||||
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||||
|
|
||||||
|
let monero_receive_address = monero_address::validate(
|
||||||
|
monero_receive_address,
|
||||||
|
context.config.env_config.monero_network,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let seller =
|
||||||
|
Multiaddr::from_str(params.get("seller").ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Does not contain seller".to_string())
|
||||||
|
})?)
|
||||||
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||||
|
|
||||||
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::BuyXmr {
|
||||||
|
bitcoin_change_address,
|
||||||
|
monero_receive_address,
|
||||||
|
seller,
|
||||||
|
swap_id: Uuid::new_v4(),
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("list_sellers", |params_raw, context| async move {
|
||||||
|
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
|
||||||
|
|
||||||
|
let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let rendezvous_point = rendezvous_point
|
||||||
|
.as_str()
|
||||||
|
.and_then(|addr_str| Multiaddr::from_str(addr_str).ok())
|
||||||
|
.ok_or_else(|| {
|
||||||
|
jsonrpsee_core::Error::Custom("Could not parse valid multiaddr".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::ListSellers {
|
||||||
|
rendezvous_point: rendezvous_point.clone(),
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
module.register_async_method("get_current_swap", |params, context| async move {
|
||||||
|
execute_request(params, Method::GetCurrentSwap, &context).await
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(module)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_uuid(json_value: &serde_json::Value) -> Option<Uuid> {
|
||||||
|
if let Some(uuid_str) = json_value.as_str() {
|
||||||
|
Uuid::parse_str(uuid_str).ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute_request(
|
||||||
|
params: Params<'static>,
|
||||||
|
cmd: Method,
|
||||||
|
context: &Arc<Context>,
|
||||||
|
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||||
|
// If we fail to parse the params as a String HashMap, it's most likely because its an empty object
|
||||||
|
// In that case, we want to make sure not to fail the request, so we set the log_reference_id to None
|
||||||
|
// and swallow the error
|
||||||
|
let reference_id = params
|
||||||
|
.parse::<HashMap<String, serde_json::Value>>()
|
||||||
|
.ok()
|
||||||
|
.and_then(|params_parsed| params_parsed.get("log_reference_id").cloned());
|
||||||
|
|
||||||
|
let request = Request::with_id(cmd, reference_id.map(|log_ref| log_ref.to_string()));
|
||||||
|
request
|
||||||
|
.call(Arc::clone(context))
|
||||||
|
.await
|
||||||
|
.map_err(|err| jsonrpsee_core::Error::Custom(format!("{:#}", err)))
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
use crate::fs::ensure_directory_exists;
|
use crate::fs::ensure_directory_exists;
|
||||||
use ::bitcoin::secp256k1::constants::SECRET_KEY_SIZE;
|
|
||||||
use ::bitcoin::secp256k1::{self, SecretKey};
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use bdk::bitcoin::util::bip32::ExtendedPrivKey;
|
use bdk::bitcoin::util::bip32::ExtendedPrivKey;
|
||||||
use bitcoin::hashes::{sha256, Hash, HashEngine};
|
use bitcoin::hashes::{sha256, Hash, HashEngine};
|
||||||
|
use bitcoin::secp256k1::constants::SECRET_KEY_SIZE;
|
||||||
|
use bitcoin::secp256k1::{self, SecretKey};
|
||||||
use libp2p::identity;
|
use libp2p::identity;
|
||||||
use pem::{encode, Pem};
|
use pem::{encode, Pem};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
@ -16,7 +16,7 @@ use torut::onion::TorSecretKeyV3;
|
|||||||
|
|
||||||
pub const SEED_LENGTH: usize = 32;
|
pub const SEED_LENGTH: usize = 32;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq)]
|
#[derive(Clone, Eq, PartialEq)]
|
||||||
pub struct Seed([u8; SEED_LENGTH]);
|
pub struct Seed([u8; SEED_LENGTH]);
|
||||||
|
|
||||||
impl Seed {
|
impl Seed {
|
||||||
@ -106,11 +106,12 @@ impl Seed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn from_pem(pem: pem::Pem) -> Result<Self, Error> {
|
fn from_pem(pem: pem::Pem) -> Result<Self, Error> {
|
||||||
if pem.contents.len() != SEED_LENGTH {
|
let contents = pem.contents();
|
||||||
Err(Error::IncorrectLength(pem.contents.len()))
|
if contents.len() != SEED_LENGTH {
|
||||||
|
Err(Error::IncorrectLength(contents.len()))
|
||||||
} else {
|
} else {
|
||||||
let mut array = [0; SEED_LENGTH];
|
let mut array = [0; SEED_LENGTH];
|
||||||
for (i, b) in pem.contents.iter().enumerate() {
|
for (i, b) in contents.iter().enumerate() {
|
||||||
array[i] = *b;
|
array[i] = *b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,10 +123,7 @@ impl Seed {
|
|||||||
ensure_directory_exists(&seed_file)?;
|
ensure_directory_exists(&seed_file)?;
|
||||||
|
|
||||||
let data = self.bytes();
|
let data = self.bytes();
|
||||||
let pem = Pem {
|
let pem = Pem::new("SEED", data);
|
||||||
tag: String::from("SEED"),
|
|
||||||
contents: data.to_vec(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let pem_string = encode(&pem);
|
let pem_string = encode(&pem);
|
||||||
|
|
||||||
@ -224,19 +222,20 @@ VnZUNFZ4dlY=
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn seed_from_pem_fails_for_long_seed() {
|
fn seed_from_pem_fails_for_long_seed() {
|
||||||
let long = "-----BEGIN SEED-----
|
let long = "-----BEGIN SEED-----
|
||||||
mbKANv2qKGmNVg1qtquj6Hx1pFPelpqOfE2JaJJAMEg1FlFhNRNlFlE=
|
MIIBPQIBAAJBAOsfi5AGYhdRs/x6q5H7kScxA0Kzzqe6WI6gf6+tc6IvKQJo5rQc
|
||||||
mbKANv2qKGmNVg1qtquj6Hx1pFPelpqOfE2JaJJAMEg1FlFhNRNlFlE=
|
dWWSQ0nRGt2hOPDO+35NKhQEjBQxPh/v7n0CAwEAAQJBAOGaBAyuw0ICyENy5NsO
|
||||||
-----END SEED-----
|
-----END SEED-----
|
||||||
";
|
";
|
||||||
let pem = pem::parse(long).unwrap();
|
let pem = pem::parse(long).unwrap();
|
||||||
|
assert_eq!(pem.contents().len(), 96);
|
||||||
|
|
||||||
match Seed::from_pem(pem) {
|
match Seed::from_pem(pem) {
|
||||||
Ok(_) => panic!("should fail for long payload"),
|
Ok(_) => panic!("should fail for long payload"),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
match e {
|
match e {
|
||||||
Error::IncorrectLength(_) => {} // pass
|
Error::IncorrectLength(len) => assert_eq!(len, 96), // pass
|
||||||
_ => panic!("should fail with IncorrectLength error"),
|
_ => panic!("should fail with IncorrectLength error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
|
|||||||
|
|
||||||
// Bob manually cancels
|
// Bob manually cancels
|
||||||
bob_join_handle.abort();
|
bob_join_handle.abort();
|
||||||
let (_, _, state) = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
|
let (_, state) = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
|
||||||
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
||||||
|
|
||||||
let (bob_swap, bob_join_handle) = ctx
|
let (bob_swap, bob_join_handle) = ctx
|
||||||
|
@ -42,10 +42,10 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
|
|||||||
let error = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db)
|
let error = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db)
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
assert_eq!(
|
|
||||||
parse_rpc_error_code(&error).unwrap(),
|
assert!(error
|
||||||
i64::from(RpcErrorCode::RpcVerifyRejected)
|
.to_string()
|
||||||
);
|
.contains("Cannot cancel swap because the cancel timelock has not expired yet"));
|
||||||
|
|
||||||
ctx.restart_alice().await;
|
ctx.restart_alice().await;
|
||||||
let alice_swap = ctx.alice_next_swap().await;
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
@ -72,10 +72,9 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
|
|||||||
let error = cli::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db)
|
let error = cli::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db)
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
assert_eq!(
|
assert!(error
|
||||||
parse_rpc_error_code(&error).unwrap(),
|
.to_string()
|
||||||
i64::from(RpcErrorCode::RpcVerifyError)
|
.contains("Cannot refund swap because the cancel timelock has not expired yet"));
|
||||||
);
|
|
||||||
|
|
||||||
let (bob_swap, _) = ctx
|
let (bob_swap, _) = ctx
|
||||||
.stop_and_resume_bob_from_db(bob_join_handle, swap_id)
|
.stop_and_resume_bob_from_db(bob_join_handle, swap_id)
|
||||||
|
@ -11,7 +11,7 @@ use swap::protocol::{alice, bob};
|
|||||||
|
|
||||||
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
||||||
/// the encsig and fail to refund or redeem. Alice punishes using the cancel and
|
/// the encsig and fail to refund or redeem. Alice punishes using the cancel and
|
||||||
/// punish command.
|
/// punish command. Bob then cooperates with Alice and redeems XMR with her key.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_manually_punishes_after_bob_dead() {
|
async fn alice_manually_punishes_after_bob_dead() {
|
||||||
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
||||||
@ -78,9 +78,7 @@ async fn alice_manually_punishes_after_bob_dead() {
|
|||||||
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
let bob_state = bob::run(bob_swap).await?;
|
let bob_state = bob::run(bob_swap).await?;
|
||||||
|
ctx.assert_bob_redeemed(bob_state).await;
|
||||||
ctx.assert_bob_punished(bob_state).await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -0,0 +1,98 @@
|
|||||||
|
pub mod harness;
|
||||||
|
|
||||||
|
use harness::alice_run_until::is_xmr_lock_transaction_sent;
|
||||||
|
use harness::bob_run_until::is_btc_locked;
|
||||||
|
use harness::FastPunishConfig;
|
||||||
|
use swap::asb;
|
||||||
|
use swap::asb::FixedRate;
|
||||||
|
use swap::cli;
|
||||||
|
use swap::protocol::alice::AliceState;
|
||||||
|
use swap::protocol::bob::BobState;
|
||||||
|
use swap::protocol::{alice, bob};
|
||||||
|
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
||||||
|
/// the encsig and fail to refund or redeem. Alice punishes using the cancel and
|
||||||
|
/// punish command. Then Bob tries to refund.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn alice_manually_punishes_after_bob_dead_and_bob_cancels() {
|
||||||
|
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.bob_swap().await;
|
||||||
|
let bob_swap_id = bob_swap.id;
|
||||||
|
let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
|
||||||
|
|
||||||
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
|
let alice_bitcoin_wallet = alice_swap.bitcoin_wallet.clone();
|
||||||
|
|
||||||
|
let alice_swap = tokio::spawn(alice::run_until(
|
||||||
|
alice_swap,
|
||||||
|
is_xmr_lock_transaction_sent,
|
||||||
|
FixedRate::default(),
|
||||||
|
));
|
||||||
|
|
||||||
|
let bob_state = bob_swap.await??;
|
||||||
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
let alice_state = alice_swap.await??;
|
||||||
|
|
||||||
|
// Ensure cancel timelock is expired
|
||||||
|
if let AliceState::XmrLockTransactionSent { state3, .. } = alice_state {
|
||||||
|
alice_bitcoin_wallet
|
||||||
|
.subscribe_to(state3.tx_lock)
|
||||||
|
.await
|
||||||
|
.wait_until_confirmed_with(state3.cancel_timelock)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
panic!("Alice in unexpected state {}", alice_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
// manual cancel (required to be able to punish)
|
||||||
|
|
||||||
|
ctx.restart_alice().await;
|
||||||
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
|
let (_, alice_state) =
|
||||||
|
asb::cancel(alice_swap.swap_id, alice_swap.bitcoin_wallet, alice_swap.db).await?;
|
||||||
|
|
||||||
|
// Ensure punish timelock is expired
|
||||||
|
if let AliceState::BtcCancelled { state3, .. } = alice_state {
|
||||||
|
alice_bitcoin_wallet
|
||||||
|
.subscribe_to(state3.tx_cancel())
|
||||||
|
.await
|
||||||
|
.wait_until_confirmed_with(state3.punish_timelock)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
panic!("Alice in unexpected state {}", alice_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
// manual punish
|
||||||
|
|
||||||
|
ctx.restart_alice().await;
|
||||||
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
|
let (_, alice_state) =
|
||||||
|
asb::punish(alice_swap.swap_id, alice_swap.bitcoin_wallet, alice_swap.db).await?;
|
||||||
|
ctx.assert_alice_punished(alice_state).await;
|
||||||
|
// Bob is in wrong state.
|
||||||
|
let (bob_swap, bob_join_handle) = ctx
|
||||||
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
||||||
|
.await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
bob_join_handle.abort();
|
||||||
|
|
||||||
|
let (_, state) = cli::cancel(bob_swap_id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
|
||||||
|
// Bob should be in BtcCancelled state now.
|
||||||
|
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
||||||
|
|
||||||
|
let (bob_swap, _) = ctx
|
||||||
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
||||||
|
.await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcCancelled { .. }));
|
||||||
|
// Alice punished Bob, so he should be in the BtcPunished state.
|
||||||
|
let error = cli::refund(bob_swap_id, bob_swap.bitcoin_wallet, bob_swap.db)
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(
|
||||||
|
error.to_string(),
|
||||||
|
"Cannot refund swap because we have already been punished"
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
@ -9,7 +9,7 @@ use swap::protocol::bob::BobState;
|
|||||||
use swap::protocol::{alice, bob};
|
use swap::protocol::{alice, bob};
|
||||||
|
|
||||||
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
||||||
/// the encsig and fail to refund or redeem. Alice cancels and punishes.
|
/// the encsig and fail to refund or redeem. Alice cancels and punishes. Bob then cooperates with Alice and redeems XMR with her key.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_punishes_after_restart_if_bob_dead() {
|
async fn alice_punishes_after_restart_if_bob_dead() {
|
||||||
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
||||||
@ -58,9 +58,7 @@ async fn alice_punishes_after_restart_if_bob_dead() {
|
|||||||
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
let bob_state = bob::run(bob_swap).await?;
|
let bob_state = bob::run(bob_swap).await?;
|
||||||
|
ctx.assert_bob_redeemed(bob_state).await;
|
||||||
ctx.assert_bob_punished(bob_state).await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -32,8 +32,7 @@ async fn concurrent_bobs_before_xmr_lock_proof_sent() {
|
|||||||
let alice_swap_2 = tokio::spawn(alice::run(alice_swap_2, FixedRate::default()));
|
let alice_swap_2 = tokio::spawn(alice::run(alice_swap_2, FixedRate::default()));
|
||||||
|
|
||||||
// The 2nd swap ALWAYS finish successfully in this
|
// The 2nd swap ALWAYS finish successfully in this
|
||||||
// scenario, but will receive an "unwanted" transfer proof that is ignored in
|
// scenario, but will receive an "unwanted" transfer proof that is buffered until the 1st swap is resumed
|
||||||
// the event loop.
|
|
||||||
|
|
||||||
let bob_state_2 = bob_swap_2.await??;
|
let bob_state_2 = bob_swap_2.await??;
|
||||||
assert!(matches!(bob_state_2, BobState::XmrRedeemed { .. }));
|
assert!(matches!(bob_state_2, BobState::XmrRedeemed { .. }));
|
||||||
@ -46,15 +45,13 @@ async fn concurrent_bobs_before_xmr_lock_proof_sent() {
|
|||||||
.await;
|
.await;
|
||||||
assert!(matches!(bob_state_1, BobState::BtcLocked { .. }));
|
assert!(matches!(bob_state_1, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
// The 1st (paused) swap is expected to refund, because the transfer
|
// The 1st (paused) swap is expected to finish successfully because the transfer proof is buffered when it is receives while another swap is running.
|
||||||
// proof is delivered to the wrong swap, and we currently don't store it in the
|
|
||||||
// database for the other swap.
|
|
||||||
|
|
||||||
let bob_state_1 = bob::run(bob_swap_1).await?;
|
let bob_state_1 = bob::run(bob_swap_1).await?;
|
||||||
assert!(matches!(bob_state_1, BobState::BtcRefunded { .. }));
|
assert!(matches!(bob_state_1, BobState::XmrRedeemed { .. }));
|
||||||
|
|
||||||
let alice_state_1 = alice_swap_1.await??;
|
let alice_state_1 = alice_swap_1.await??;
|
||||||
assert!(matches!(alice_state_1, AliceState::XmrRefunded { .. }));
|
assert!(matches!(alice_state_1, AliceState::BtcRedeemed { .. }));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
44
swap/tests/happy_path_bob_offline_while_alice_redeems_btc.rs
Normal file
44
swap/tests/happy_path_bob_offline_while_alice_redeems_btc.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
pub mod harness;
|
||||||
|
|
||||||
|
use crate::harness::bob_run_until::is_encsig_sent;
|
||||||
|
use swap::asb::FixedRate;
|
||||||
|
use swap::protocol::bob::BobState;
|
||||||
|
use swap::protocol::{alice, bob};
|
||||||
|
use tokio::join;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn given_bob_restarts_while_alice_redeems_btc() {
|
||||||
|
harness::setup_test(harness::SlowCancelConfig, |mut ctx| async move {
|
||||||
|
let (bob_swap, bob_handle) = ctx.bob_swap().await;
|
||||||
|
let swap_id = bob_swap.id;
|
||||||
|
|
||||||
|
let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_encsig_sent));
|
||||||
|
|
||||||
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
|
let alice_swap = tokio::spawn(alice::run(alice_swap, FixedRate::default()));
|
||||||
|
|
||||||
|
let (bob_state, alice_state) = join!(bob_swap, alice_swap);
|
||||||
|
ctx.assert_alice_redeemed(alice_state??).await;
|
||||||
|
assert!(matches!(bob_state??, BobState::EncSigSent { .. }));
|
||||||
|
|
||||||
|
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_handle, swap_id).await;
|
||||||
|
|
||||||
|
if let BobState::EncSigSent(state4) = bob_swap.state.clone() {
|
||||||
|
bob_swap
|
||||||
|
.bitcoin_wallet
|
||||||
|
.subscribe_to(state4.tx_lock)
|
||||||
|
.await
|
||||||
|
.wait_until_confirmed_with(state4.cancel_timelock)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
panic!("Bob in unexpected state {}", bob_swap.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restart Bob
|
||||||
|
let bob_state = bob::run(bob_swap).await?;
|
||||||
|
ctx.assert_bob_redeemed(bob_state).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::BTreeMap;
|
||||||
use testcontainers::core::{Container, Docker, WaitForMessage};
|
use testcontainers::{core::WaitFor, Image, ImageArgs};
|
||||||
use testcontainers::Image;
|
|
||||||
|
|
||||||
pub const RPC_USER: &str = "admin";
|
pub const RPC_USER: &str = "admin";
|
||||||
pub const RPC_PASSWORD: &str = "123";
|
pub const RPC_PASSWORD: &str = "123";
|
||||||
@ -10,57 +9,27 @@ pub const DATADIR: &str = "/home/bdk";
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Bitcoind {
|
pub struct Bitcoind {
|
||||||
args: BitcoindArgs,
|
|
||||||
entrypoint: Option<String>,
|
entrypoint: Option<String>,
|
||||||
volume: Option<String>,
|
volumes: BTreeMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Image for Bitcoind {
|
impl Image for Bitcoind {
|
||||||
type Args = BitcoindArgs;
|
type Args = BitcoindArgs;
|
||||||
type EnvVars = HashMap<String, String>;
|
|
||||||
type Volumes = HashMap<String, String>;
|
|
||||||
type EntryPoint = str;
|
|
||||||
|
|
||||||
fn descriptor(&self) -> String {
|
fn name(&self) -> String {
|
||||||
"coblox/bitcoin-core:0.21.0".to_string()
|
"coblox/bitcoin-core".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
|
fn tag(&self) -> String {
|
||||||
container
|
"0.21.0".into()
|
||||||
.logs()
|
|
||||||
.stdout
|
|
||||||
.wait_for_message("init message: Done loading")
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args(&self) -> <Self as Image>::Args {
|
fn ready_conditions(&self) -> Vec<WaitFor> {
|
||||||
self.args.clone()
|
vec![WaitFor::message_on_stdout("init message: Done loading")]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn volumes(&self) -> Self::Volumes {
|
fn volumes(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
|
||||||
let mut volumes = HashMap::new();
|
Box::new(self.volumes.iter())
|
||||||
match self.volume.clone() {
|
|
||||||
None => {}
|
|
||||||
Some(volume) => {
|
|
||||||
volumes.insert(volume, DATADIR.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
volumes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn env_vars(&self) -> Self::EnvVars {
|
|
||||||
HashMap::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_args(self, args: <Self as Image>::Args) -> Self {
|
|
||||||
Bitcoind { args, ..self }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_entrypoint(self, entrypoint: &Self::EntryPoint) -> Self {
|
|
||||||
Self {
|
|
||||||
entrypoint: Some(entrypoint.to_string()),
|
|
||||||
..self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entrypoint(&self) -> Option<String> {
|
fn entrypoint(&self) -> Option<String> {
|
||||||
@ -71,16 +40,15 @@ impl Image for Bitcoind {
|
|||||||
impl Default for Bitcoind {
|
impl Default for Bitcoind {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Bitcoind {
|
Bitcoind {
|
||||||
args: BitcoindArgs::default(),
|
|
||||||
entrypoint: Some("/usr/bin/bitcoind".into()),
|
entrypoint: Some("/usr/bin/bitcoind".into()),
|
||||||
volume: None,
|
volumes: BTreeMap::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bitcoind {
|
impl Bitcoind {
|
||||||
pub fn with_volume(mut self, volume: String) -> Self {
|
pub fn with_volume(mut self, volume: String) -> Self {
|
||||||
self.volume = Some(volume);
|
self.volumes.insert(volume, DATADIR.to_string());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +77,6 @@ impl IntoIterator for BitcoindArgs {
|
|||||||
format!("-rpcuser={}", RPC_USER),
|
format!("-rpcuser={}", RPC_USER),
|
||||||
format!("-rpcpassword={}", RPC_PASSWORD),
|
format!("-rpcpassword={}", RPC_PASSWORD),
|
||||||
"-printtoconsole".to_string(),
|
"-printtoconsole".to_string(),
|
||||||
"-rest".to_string(),
|
|
||||||
"-fallbackfee=0.0002".to_string(),
|
"-fallbackfee=0.0002".to_string(),
|
||||||
format!("-datadir={}", DATADIR),
|
format!("-datadir={}", DATADIR),
|
||||||
format!("-rpcport={}", RPC_PORT),
|
format!("-rpcport={}", RPC_PORT),
|
||||||
@ -120,3 +87,9 @@ impl IntoIterator for BitcoindArgs {
|
|||||||
args.into_iter()
|
args.into_iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ImageArgs for BitcoindArgs {
|
||||||
|
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
|
||||||
|
Box::new(self.into_iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use crate::harness::bitcoind;
|
use crate::harness::bitcoind;
|
||||||
use bitcoin::Network;
|
use bitcoin::Network;
|
||||||
use std::collections::HashMap;
|
use testcontainers::{core::WaitFor, Image, ImageArgs};
|
||||||
use testcontainers::core::{Container, Docker, WaitForMessage};
|
|
||||||
use testcontainers::Image;
|
|
||||||
|
|
||||||
pub const HTTP_PORT: u16 = 60401;
|
pub const HTTP_PORT: u16 = 60401;
|
||||||
pub const RPC_PORT: u16 = 3002;
|
pub const RPC_PORT: u16 = 3002;
|
||||||
@ -13,50 +13,25 @@ pub struct Electrs {
|
|||||||
args: ElectrsArgs,
|
args: ElectrsArgs,
|
||||||
entrypoint: Option<String>,
|
entrypoint: Option<String>,
|
||||||
wait_for_message: String,
|
wait_for_message: String,
|
||||||
volume: String,
|
volumes: BTreeMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Image for Electrs {
|
impl Image for Electrs {
|
||||||
type Args = ElectrsArgs;
|
type Args = ElectrsArgs;
|
||||||
type EnvVars = HashMap<String, String>;
|
fn name(&self) -> String {
|
||||||
type Volumes = HashMap<String, String>;
|
"vulpemventures/electrs".into()
|
||||||
type EntryPoint = str;
|
|
||||||
|
|
||||||
fn descriptor(&self) -> String {
|
|
||||||
format!("vulpemventures/electrs:{}", self.tag)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
|
fn tag(&self) -> String {
|
||||||
container
|
self.tag.clone()
|
||||||
.logs()
|
|
||||||
.stderr
|
|
||||||
.wait_for_message(&self.wait_for_message)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args(&self) -> <Self as Image>::Args {
|
fn ready_conditions(&self) -> Vec<WaitFor> {
|
||||||
self.args.clone()
|
vec![WaitFor::message_on_stderr(self.wait_for_message.clone())]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn volumes(&self) -> Self::Volumes {
|
fn volumes(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
|
||||||
let mut volumes = HashMap::new();
|
Box::new(self.volumes.iter())
|
||||||
volumes.insert(self.volume.clone(), bitcoind::DATADIR.to_string());
|
|
||||||
volumes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn env_vars(&self) -> Self::EnvVars {
|
|
||||||
HashMap::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_args(self, args: <Self as Image>::Args) -> Self {
|
|
||||||
Electrs { args, ..self }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_entrypoint(self, entrypoint: &Self::EntryPoint) -> Self {
|
|
||||||
Self {
|
|
||||||
entrypoint: Some(entrypoint.to_string()),
|
|
||||||
..self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entrypoint(&self) -> Option<String> {
|
fn entrypoint(&self) -> Option<String> {
|
||||||
@ -71,7 +46,7 @@ impl Default for Electrs {
|
|||||||
args: ElectrsArgs::default(),
|
args: ElectrsArgs::default(),
|
||||||
entrypoint: Some("/build/electrs".into()),
|
entrypoint: Some("/build/electrs".into()),
|
||||||
wait_for_message: "Running accept thread".to_string(),
|
wait_for_message: "Running accept thread".to_string(),
|
||||||
volume: uuid::Uuid::new_v4().to_string(),
|
volumes: BTreeMap::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,7 +60,7 @@ impl Electrs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_volume(mut self, volume: String) -> Self {
|
pub fn with_volume(mut self, volume: String) -> Self {
|
||||||
self.volume = volume;
|
self.volumes.insert(volume, bitcoind::DATADIR.to_string());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +68,11 @@ impl Electrs {
|
|||||||
self.args.daemon_rpc_addr = name;
|
self.args.daemon_rpc_addr = name;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn self_and_args(self) -> (Self, ElectrsArgs) {
|
||||||
|
let args = self.args.clone();
|
||||||
|
(self, args)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -137,7 +117,7 @@ impl IntoIterator for ElectrsArgs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
args.push("-vvvvv".to_string());
|
args.push("-vvvvv".to_string());
|
||||||
args.push(format!("--daemon-dir=={}", self.daemon_dir.as_str()));
|
args.push(format!("--daemon-dir={}", self.daemon_dir.as_str()));
|
||||||
args.push(format!("--daemon-rpc-addr={}", self.daemon_rpc_addr));
|
args.push(format!("--daemon-rpc-addr={}", self.daemon_rpc_addr));
|
||||||
args.push(format!("--cookie={}", self.cookie));
|
args.push(format!("--cookie={}", self.cookie));
|
||||||
args.push(format!("--http-addr={}", self.http_addr));
|
args.push(format!("--http-addr={}", self.http_addr));
|
||||||
@ -147,3 +127,9 @@ impl IntoIterator for ElectrsArgs {
|
|||||||
args.into_iter()
|
args.into_iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ImageArgs for ElectrsArgs {
|
||||||
|
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
|
||||||
|
Box::new(self.into_iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,19 +16,19 @@ use std::sync::Arc;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use swap::asb::FixedRate;
|
use swap::asb::FixedRate;
|
||||||
use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund};
|
use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund};
|
||||||
use swap::database::SqliteDatabase;
|
use swap::database::{AccessMode, SqliteDatabase};
|
||||||
use swap::env::{Config, GetConfig};
|
use swap::env::{Config, GetConfig};
|
||||||
use swap::fs::ensure_directory_exists;
|
use swap::fs::ensure_directory_exists;
|
||||||
use swap::network::rendezvous::XmrBtcNamespace;
|
use swap::network::rendezvous::XmrBtcNamespace;
|
||||||
use swap::network::swarm;
|
use swap::network::swarm;
|
||||||
use swap::protocol::alice::{AliceState, Swap};
|
use swap::protocol::alice::{AliceState, Swap};
|
||||||
use swap::protocol::bob::BobState;
|
use swap::protocol::bob::BobState;
|
||||||
use swap::protocol::{alice, bob};
|
use swap::protocol::{alice, bob, Database};
|
||||||
use swap::seed::Seed;
|
use swap::seed::Seed;
|
||||||
use swap::{asb, bitcoin, cli, env, monero};
|
use swap::{api, asb, bitcoin, cli, env, monero};
|
||||||
use tempfile::{tempdir, NamedTempFile};
|
use tempfile::{tempdir, NamedTempFile};
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
use testcontainers::{Container, Docker, RunArgs};
|
use testcontainers::{Container, RunnableImage};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
@ -61,10 +61,7 @@ where
|
|||||||
let alice_starting_balances =
|
let alice_starting_balances =
|
||||||
StartingBalances::new(bitcoin::Amount::ZERO, xmr_amount, Some(10));
|
StartingBalances::new(bitcoin::Amount::ZERO, xmr_amount, Some(10));
|
||||||
|
|
||||||
let electrs_rpc_port = containers
|
let electrs_rpc_port = containers.electrs.get_host_port_ipv4(electrs::RPC_PORT);
|
||||||
.electrs
|
|
||||||
.get_host_port(electrs::RPC_PORT)
|
|
||||||
.expect("Could not map electrs rpc port");
|
|
||||||
|
|
||||||
let alice_seed = Seed::random().unwrap();
|
let alice_seed = Seed::random().unwrap();
|
||||||
let (alice_bitcoin_wallet, alice_monero_wallet) = init_test_wallets(
|
let (alice_bitcoin_wallet, alice_monero_wallet) = init_test_wallets(
|
||||||
@ -146,14 +143,14 @@ where
|
|||||||
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
||||||
let prefix = random_prefix();
|
let prefix = random_prefix();
|
||||||
let bitcoind_name = format!("{}_{}", prefix, "bitcoind");
|
let bitcoind_name = format!("{}_{}", prefix, "bitcoind");
|
||||||
let (bitcoind, bitcoind_url) =
|
let (_bitcoind, bitcoind_url, mapped_port) =
|
||||||
init_bitcoind_container(cli, prefix.clone(), bitcoind_name.clone(), prefix.clone())
|
init_bitcoind_container(cli, prefix.clone(), bitcoind_name.clone(), prefix.clone())
|
||||||
.await
|
.await
|
||||||
.expect("could not init bitcoind");
|
.expect("could not init bitcoind");
|
||||||
let electrs = init_electrs_container(cli, prefix.clone(), bitcoind_name, prefix)
|
let electrs = init_electrs_container(cli, prefix.clone(), bitcoind_name, prefix, mapped_port)
|
||||||
.await
|
.await
|
||||||
.expect("could not init electrs");
|
.expect("could not init electrs");
|
||||||
let (monero, monerod_container, monero_wallet_rpc_containers) =
|
let (monero, _monerod_container, _monero_wallet_rpc_containers) =
|
||||||
Monero::new(cli, vec![MONERO_WALLET_NAME_ALICE, MONERO_WALLET_NAME_BOB])
|
Monero::new(cli, vec![MONERO_WALLET_NAME_ALICE, MONERO_WALLET_NAME_BOB])
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -162,9 +159,9 @@ async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
|||||||
monero,
|
monero,
|
||||||
Containers {
|
Containers {
|
||||||
bitcoind_url,
|
bitcoind_url,
|
||||||
bitcoind,
|
_bitcoind,
|
||||||
monerod_container,
|
_monerod_container,
|
||||||
monero_wallet_rpc_containers,
|
_monero_wallet_rpc_containers,
|
||||||
electrs,
|
electrs,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -175,29 +172,28 @@ async fn init_bitcoind_container(
|
|||||||
volume: String,
|
volume: String,
|
||||||
name: String,
|
name: String,
|
||||||
network: String,
|
network: String,
|
||||||
) -> Result<(Container<'_, Cli, bitcoind::Bitcoind>, Url)> {
|
) -> Result<(Container<'_, bitcoind::Bitcoind>, Url, u16)> {
|
||||||
let image = bitcoind::Bitcoind::default().with_volume(volume);
|
let image = bitcoind::Bitcoind::default().with_volume(volume);
|
||||||
|
let image = RunnableImage::from(image)
|
||||||
|
.with_container_name(name)
|
||||||
|
.with_network(network);
|
||||||
|
|
||||||
let run_args = RunArgs::default().with_name(name).with_network(network);
|
let docker = cli.run(image);
|
||||||
|
let port = docker.get_host_port_ipv4(bitcoind::RPC_PORT);
|
||||||
let docker = cli.run_with_args(image, run_args);
|
|
||||||
let a = docker
|
|
||||||
.get_host_port(bitcoind::RPC_PORT)
|
|
||||||
.context("Could not map bitcoind rpc port")?;
|
|
||||||
|
|
||||||
let bitcoind_url = {
|
let bitcoind_url = {
|
||||||
let input = format!(
|
let input = format!(
|
||||||
"http://{}:{}@localhost:{}",
|
"http://{}:{}@localhost:{}",
|
||||||
bitcoind::RPC_USER,
|
bitcoind::RPC_USER,
|
||||||
bitcoind::RPC_PASSWORD,
|
bitcoind::RPC_PASSWORD,
|
||||||
a
|
port
|
||||||
);
|
);
|
||||||
Url::parse(&input).unwrap()
|
Url::parse(&input).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
init_bitcoind(bitcoind_url.clone(), 5).await?;
|
init_bitcoind(bitcoind_url.clone(), 5).await?;
|
||||||
|
|
||||||
Ok((docker, bitcoind_url.clone()))
|
Ok((docker, bitcoind_url.clone(), bitcoind::RPC_PORT))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init_electrs_container(
|
pub async fn init_electrs_container(
|
||||||
@ -205,16 +201,18 @@ pub async fn init_electrs_container(
|
|||||||
volume: String,
|
volume: String,
|
||||||
bitcoind_container_name: String,
|
bitcoind_container_name: String,
|
||||||
network: String,
|
network: String,
|
||||||
) -> Result<Container<'_, Cli, electrs::Electrs>> {
|
port: u16,
|
||||||
let bitcoind_rpc_addr = format!("{}:{}", bitcoind_container_name, bitcoind::RPC_PORT);
|
) -> Result<Container<'_, electrs::Electrs>> {
|
||||||
|
let bitcoind_rpc_addr = format!("{}:{}", bitcoind_container_name, port);
|
||||||
let image = electrs::Electrs::default()
|
let image = electrs::Electrs::default()
|
||||||
.with_volume(volume)
|
.with_volume(volume)
|
||||||
.with_daemon_rpc_addr(bitcoind_rpc_addr)
|
.with_daemon_rpc_addr(bitcoind_rpc_addr)
|
||||||
.with_tag("latest");
|
.with_tag("latest");
|
||||||
|
let image = RunnableImage::from(image.self_and_args())
|
||||||
|
.with_network(network.clone())
|
||||||
|
.with_container_name(format!("{}_electrs", network));
|
||||||
|
|
||||||
let run_args = RunArgs::default().with_network(network);
|
let docker = cli.run(image);
|
||||||
|
|
||||||
let docker = cli.run_with_args(image, run_args);
|
|
||||||
|
|
||||||
Ok(docker)
|
Ok(docker)
|
||||||
}
|
}
|
||||||
@ -233,7 +231,11 @@ async fn start_alice(
|
|||||||
if !&db_path.exists() {
|
if !&db_path.exists() {
|
||||||
tokio::fs::File::create(&db_path).await.unwrap();
|
tokio::fs::File::create(&db_path).await.unwrap();
|
||||||
}
|
}
|
||||||
let db = Arc::new(SqliteDatabase::open(db_path.as_path()).await.unwrap());
|
let db = Arc::new(
|
||||||
|
SqliteDatabase::open(db_path.as_path(), AccessMode::ReadWrite)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
let min_buy = bitcoin::Amount::from_sat(u64::MIN);
|
let min_buy = bitcoin::Amount::from_sat(u64::MIN);
|
||||||
let max_buy = bitcoin::Amount::from_sat(u64::MAX);
|
let max_buy = bitcoin::Amount::from_sat(u64::MAX);
|
||||||
@ -402,7 +404,7 @@ impl StartingBalances {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BobParams {
|
pub struct BobParams {
|
||||||
seed: Seed,
|
seed: Seed,
|
||||||
db_path: PathBuf,
|
db_path: PathBuf,
|
||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
@ -413,19 +415,34 @@ struct BobParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BobParams {
|
impl BobParams {
|
||||||
pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, cli::EventLoop)> {
|
pub fn get_concentenated_alice_address(&self) -> String {
|
||||||
let (event_loop, handle) = self.new_eventloop(swap_id).await?;
|
format!(
|
||||||
|
"{}/p2p/{}",
|
||||||
|
self.alice_address.clone(),
|
||||||
|
self.alice_peer_id.clone().to_base58()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_change_receive_addresses(&self) -> (bitcoin::Address, monero::Address) {
|
||||||
|
(
|
||||||
|
self.bitcoin_wallet.new_address().await.unwrap(),
|
||||||
|
self.monero_wallet.get_main_address(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, cli::EventLoop)> {
|
||||||
if let Some(parent_dir) = self.db_path.parent() {
|
if let Some(parent_dir) = self.db_path.parent() {
|
||||||
ensure_directory_exists(parent_dir)?;
|
ensure_directory_exists(parent_dir)?;
|
||||||
}
|
}
|
||||||
if !self.db_path.exists() {
|
if !self.db_path.exists() {
|
||||||
tokio::fs::File::create(&self.db_path).await?;
|
tokio::fs::File::create(&self.db_path).await?;
|
||||||
}
|
}
|
||||||
let db = Arc::new(SqliteDatabase::open(&self.db_path).await?);
|
let db = Arc::new(SqliteDatabase::open(&self.db_path, AccessMode::ReadWrite).await?);
|
||||||
|
|
||||||
|
let (event_loop, handle) = self.new_eventloop(swap_id, db.clone()).await?;
|
||||||
|
|
||||||
let swap = bob::Swap::from_db(
|
let swap = bob::Swap::from_db(
|
||||||
db,
|
db.clone(),
|
||||||
swap_id,
|
swap_id,
|
||||||
self.bitcoin_wallet.clone(),
|
self.bitcoin_wallet.clone(),
|
||||||
self.monero_wallet.clone(),
|
self.monero_wallet.clone(),
|
||||||
@ -444,15 +461,17 @@ impl BobParams {
|
|||||||
) -> Result<(bob::Swap, cli::EventLoop)> {
|
) -> Result<(bob::Swap, cli::EventLoop)> {
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
let (event_loop, handle) = self.new_eventloop(swap_id).await?;
|
|
||||||
|
|
||||||
if let Some(parent_dir) = self.db_path.parent() {
|
if let Some(parent_dir) = self.db_path.parent() {
|
||||||
ensure_directory_exists(parent_dir)?;
|
ensure_directory_exists(parent_dir)?;
|
||||||
}
|
}
|
||||||
if !self.db_path.exists() {
|
if !self.db_path.exists() {
|
||||||
tokio::fs::File::create(&self.db_path).await?;
|
tokio::fs::File::create(&self.db_path).await?;
|
||||||
}
|
}
|
||||||
let db = Arc::new(SqliteDatabase::open(&self.db_path).await?);
|
let db = Arc::new(SqliteDatabase::open(&self.db_path, AccessMode::ReadWrite).await?);
|
||||||
|
|
||||||
|
let (event_loop, handle) = self.new_eventloop(swap_id, db.clone()).await?;
|
||||||
|
|
||||||
|
db.insert_peer_id(swap_id, self.alice_peer_id).await?;
|
||||||
|
|
||||||
let swap = bob::Swap::new(
|
let swap = bob::Swap::new(
|
||||||
db,
|
db,
|
||||||
@ -472,6 +491,7 @@ impl BobParams {
|
|||||||
pub async fn new_eventloop(
|
pub async fn new_eventloop(
|
||||||
&self,
|
&self,
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<(cli::EventLoop, cli::EventLoopHandle)> {
|
) -> Result<(cli::EventLoop, cli::EventLoopHandle)> {
|
||||||
let tor_socks5_port = get_port()
|
let tor_socks5_port = get_port()
|
||||||
.expect("We don't care about Tor in the tests so we get a free port to disable it.");
|
.expect("We don't care about Tor in the tests so we get a free port to disable it.");
|
||||||
@ -488,7 +508,7 @@ impl BobParams {
|
|||||||
.behaviour_mut()
|
.behaviour_mut()
|
||||||
.add_address(self.alice_peer_id, self.alice_address.clone());
|
.add_address(self.alice_peer_id, self.alice_address.clone());
|
||||||
|
|
||||||
cli::EventLoop::new(swap_id, swarm, self.alice_peer_id)
|
cli::EventLoop::new(swap_id, swarm, self.alice_peer_id, db.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -527,13 +547,24 @@ pub struct TestContext {
|
|||||||
alice_swap_handle: mpsc::Receiver<Swap>,
|
alice_swap_handle: mpsc::Receiver<Swap>,
|
||||||
alice_handle: AliceApplicationHandle,
|
alice_handle: AliceApplicationHandle,
|
||||||
|
|
||||||
bob_params: BobParams,
|
pub bob_params: BobParams,
|
||||||
bob_starting_balances: StartingBalances,
|
bob_starting_balances: StartingBalances,
|
||||||
bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
|
bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
bob_monero_wallet: Arc<monero::Wallet>,
|
bob_monero_wallet: Arc<monero::Wallet>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestContext {
|
impl TestContext {
|
||||||
|
pub async fn get_bob_context(self) -> api::Context {
|
||||||
|
api::Context::for_harness(
|
||||||
|
self.bob_params.seed,
|
||||||
|
self.env_config,
|
||||||
|
self.bob_params.db_path,
|
||||||
|
self.bob_bitcoin_wallet,
|
||||||
|
self.bob_monero_wallet,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn restart_alice(&mut self) {
|
pub async fn restart_alice(&mut self) {
|
||||||
self.alice_handle.abort();
|
self.alice_handle.abort();
|
||||||
|
|
||||||
@ -625,7 +656,7 @@ impl TestContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn assert_alice_punished(&self, state: AliceState) {
|
pub async fn assert_alice_punished(&self, state: AliceState) {
|
||||||
assert!(matches!(state, AliceState::BtcPunished));
|
assert!(matches!(state, AliceState::BtcPunished { .. }));
|
||||||
|
|
||||||
assert_eventual_balance(
|
assert_eventual_balance(
|
||||||
self.alice_bitcoin_wallet.as_ref(),
|
self.alice_bitcoin_wallet.as_ref(),
|
||||||
@ -671,7 +702,7 @@ impl TestContext {
|
|||||||
let lock_tx_id = if let BobState::BtcRefunded(state4) = state {
|
let lock_tx_id = if let BobState::BtcRefunded(state4) = state {
|
||||||
state4.tx_lock_id()
|
state4.tx_lock_id()
|
||||||
} else {
|
} else {
|
||||||
panic!("Bob in not in btc refunded state: {:?}", state);
|
panic!("Bob is not in btc refunded state: {:?}", state);
|
||||||
};
|
};
|
||||||
let lock_tx_bitcoin_fee = self
|
let lock_tx_bitcoin_fee = self
|
||||||
.bob_bitcoin_wallet
|
.bob_bitcoin_wallet
|
||||||
@ -792,7 +823,7 @@ impl TestContext {
|
|||||||
async fn bob_punished_btc_balance(&self, state: BobState) -> Result<bitcoin::Amount> {
|
async fn bob_punished_btc_balance(&self, state: BobState) -> Result<bitcoin::Amount> {
|
||||||
self.bob_bitcoin_wallet.sync().await?;
|
self.bob_bitcoin_wallet.sync().await?;
|
||||||
|
|
||||||
let lock_tx_id = if let BobState::BtcPunished { tx_lock_id } = state {
|
let lock_tx_id = if let BobState::BtcPunished { tx_lock_id, .. } = state {
|
||||||
tx_lock_id
|
tx_lock_id
|
||||||
} else {
|
} else {
|
||||||
bail!("Bob in not in btc punished state: {:?}", state);
|
bail!("Bob in not in btc punished state: {:?}", state);
|
||||||
@ -865,7 +896,7 @@ impl Wallet for monero::Wallet {
|
|||||||
type Amount = monero::Amount;
|
type Amount = monero::Amount;
|
||||||
|
|
||||||
async fn refresh(&self) -> Result<()> {
|
async fn refresh(&self) -> Result<()> {
|
||||||
self.refresh().await?;
|
self.refresh(1).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -952,13 +983,12 @@ pub async fn mint(node_url: Url, address: bitcoin::Address, amount: bitcoin::Amo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This is just to keep the containers alive
|
// This is just to keep the containers alive
|
||||||
#[allow(dead_code)]
|
|
||||||
struct Containers<'a> {
|
struct Containers<'a> {
|
||||||
bitcoind_url: Url,
|
bitcoind_url: Url,
|
||||||
bitcoind: Container<'a, Cli, bitcoind::Bitcoind>,
|
_bitcoind: Container<'a, bitcoind::Bitcoind>,
|
||||||
monerod_container: Container<'a, Cli, image::Monerod>,
|
_monerod_container: Container<'a, image::Monerod>,
|
||||||
monero_wallet_rpc_containers: Vec<Container<'a, Cli, image::MoneroWalletRpc>>,
|
_monero_wallet_rpc_containers: Vec<Container<'a, image::MoneroWalletRpc>>,
|
||||||
electrs: Container<'a, Cli, electrs::Electrs>,
|
electrs: Container<'a, electrs::Electrs>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod alice_run_until {
|
pub mod alice_run_until {
|
||||||
@ -971,6 +1001,10 @@ pub mod alice_run_until {
|
|||||||
pub fn is_encsig_learned(state: &AliceState) -> bool {
|
pub fn is_encsig_learned(state: &AliceState) -> bool {
|
||||||
matches!(state, AliceState::EncSigLearned { .. })
|
matches!(state, AliceState::EncSigLearned { .. })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_btc_redeemed(state: &AliceState) -> bool {
|
||||||
|
matches!(state, AliceState::BtcRedeemed { .. })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod bob_run_until {
|
pub mod bob_run_until {
|
||||||
|
@ -7,7 +7,7 @@ use swap::protocol::bob::BobState;
|
|||||||
use swap::protocol::{alice, bob};
|
use swap::protocol::{alice, bob};
|
||||||
|
|
||||||
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
|
||||||
/// the encsig and fail to refund or redeem. Alice punishes.
|
/// the encsig and fail to refund or redeem. Alice punishes. Bob then cooperates with Alice and redeems XMR with her key.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
||||||
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
harness::setup_test(FastPunishConfig, |mut ctx| async move {
|
||||||
@ -32,9 +32,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
|
|||||||
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
let bob_state = bob::run(bob_swap).await?;
|
let bob_state = bob::run(bob_swap).await?;
|
||||||
|
ctx.assert_bob_redeemed(bob_state).await;
|
||||||
ctx.assert_bob_punished(bob_state).await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
449
swap/tests/rpc.rs
Normal file
449
swap/tests/rpc.rs
Normal file
@ -0,0 +1,449 @@
|
|||||||
|
pub mod harness;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use jsonrpsee::ws_client::WsClientBuilder;
|
||||||
|
use jsonrpsee_core::client::{Client, ClientT};
|
||||||
|
use jsonrpsee_core::params::ObjectParams;
|
||||||
|
|
||||||
|
use serial_test::serial;
|
||||||
|
|
||||||
|
use serde_json::Value;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
use swap::api::request::{Method, Request};
|
||||||
|
use swap::api::Context;
|
||||||
|
|
||||||
|
use crate::harness::alice_run_until::is_xmr_lock_transaction_sent;
|
||||||
|
use crate::harness::bob_run_until::is_btc_locked;
|
||||||
|
use crate::harness::{setup_test, SlowCancelConfig, TestContext};
|
||||||
|
use swap::asb::FixedRate;
|
||||||
|
use swap::protocol::{alice, bob};
|
||||||
|
use swap::tracing_ext::{capture_logs, MakeCapturingWriter};
|
||||||
|
use tracing_subscriber::filter::LevelFilter;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const SERVER_ADDRESS: &str = "127.0.0.1:1234";
|
||||||
|
const SERVER_START_TIMEOUT_SECS: u64 = 50;
|
||||||
|
const BITCOIN_ADDR: &str = "bcrt1qahvhjfc7vx5857zf8knxs8yp5lkm26jgyt0k76";
|
||||||
|
const MONERO_ADDR: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
|
||||||
|
const SELLER: &str =
|
||||||
|
"/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
|
||||||
|
const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
|
||||||
|
|
||||||
|
pub async fn setup_daemon(
|
||||||
|
harness_ctx: TestContext,
|
||||||
|
) -> (Client, MakeCapturingWriter, Arc<Context>) {
|
||||||
|
let writer = capture_logs(LevelFilter::DEBUG);
|
||||||
|
let server_address: SocketAddr = SERVER_ADDRESS.parse().unwrap();
|
||||||
|
|
||||||
|
let request = Request::new(Method::StartDaemon {
|
||||||
|
server_address: Some(server_address),
|
||||||
|
});
|
||||||
|
|
||||||
|
let context = Arc::new(harness_ctx.get_bob_context().await);
|
||||||
|
|
||||||
|
let context_clone = context.clone();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Err(err) = request.call(context_clone).await {
|
||||||
|
println!("Failed to initialize daemon for testing: {}", err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for _ in 0..SERVER_START_TIMEOUT_SECS {
|
||||||
|
if writer.captured().contains("Started RPC server") {
|
||||||
|
let url = format!("ws://{}", SERVER_ADDRESS);
|
||||||
|
let client = WsClientBuilder::default().build(&url).await.unwrap();
|
||||||
|
|
||||||
|
return (client, writer, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
panic!(
|
||||||
|
"Failed to start RPC server after {} seconds",
|
||||||
|
SERVER_START_TIMEOUT_SECS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_has_keys_serde(map: &serde_json::Map<String, Value>, keys: &[&str]) {
|
||||||
|
for &key in keys {
|
||||||
|
assert!(map.contains_key(key), "Key {} is missing", key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function for HashMap
|
||||||
|
fn assert_has_keys_hashmap<T>(map: &HashMap<String, T>, keys: &[&str]) {
|
||||||
|
for &key in keys {
|
||||||
|
assert!(map.contains_key(key), "Key {} is missing", key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[serial]
|
||||||
|
pub async fn get_swap_info() {
|
||||||
|
setup_test(SlowCancelConfig, |mut harness_ctx| async move {
|
||||||
|
// Start a swap and wait for xmr lock transaction to be published (XmrLockTransactionSent)
|
||||||
|
let (bob_swap, _) = harness_ctx.bob_swap().await;
|
||||||
|
let bob_swap_id = bob_swap.id;
|
||||||
|
tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
|
||||||
|
let alice_swap = harness_ctx.alice_next_swap().await;
|
||||||
|
alice::run_until(
|
||||||
|
alice_swap,
|
||||||
|
is_xmr_lock_transaction_sent,
|
||||||
|
FixedRate::default(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let (client, _, _) = setup_daemon(harness_ctx).await;
|
||||||
|
|
||||||
|
let response: HashMap<String, Vec<Value>> = client
|
||||||
|
.request("get_history", ObjectParams::new())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let swaps = response.get("swaps").unwrap();
|
||||||
|
assert_eq!(swaps.len(), 1);
|
||||||
|
|
||||||
|
assert_has_keys_serde(
|
||||||
|
swaps[0].as_object().unwrap(),
|
||||||
|
&[
|
||||||
|
"swapId",
|
||||||
|
"startDate",
|
||||||
|
"state",
|
||||||
|
"btcAmount",
|
||||||
|
"xmrAmount",
|
||||||
|
"exchangeRate",
|
||||||
|
"tradingPartnerPeerId",
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let response: HashMap<String, HashMap<Uuid, Vec<Value>>> = client
|
||||||
|
.request("get_raw_states", ObjectParams::new())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let response_raw_states = response.get("raw_states").unwrap();
|
||||||
|
|
||||||
|
assert!(response_raw_states.contains_key(&bob_swap_id));
|
||||||
|
assert_eq!(response_raw_states.get(&bob_swap_id).unwrap().len(), 2);
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params.insert("swap_id", bob_swap_id).unwrap();
|
||||||
|
let response: HashMap<String, Value> =
|
||||||
|
client.request("get_swap_info", params).await.unwrap();
|
||||||
|
|
||||||
|
// Check primary keys in response
|
||||||
|
assert_has_keys_hashmap(
|
||||||
|
&response,
|
||||||
|
&[
|
||||||
|
"txRefundFee",
|
||||||
|
"swapId",
|
||||||
|
"cancelTimelock",
|
||||||
|
"timelock",
|
||||||
|
"punishTimelock",
|
||||||
|
"stateName",
|
||||||
|
"btcAmount",
|
||||||
|
"startDate",
|
||||||
|
"btcRefundAddress",
|
||||||
|
"txCancelFee",
|
||||||
|
"xmrAmount",
|
||||||
|
"completed",
|
||||||
|
"txLockId",
|
||||||
|
"seller",
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert specific fields
|
||||||
|
assert_eq!(response.get("swapId").unwrap(), &bob_swap_id.to_string());
|
||||||
|
assert_eq!(
|
||||||
|
response.get("stateName").unwrap(),
|
||||||
|
&"btc is locked".to_string()
|
||||||
|
);
|
||||||
|
assert_eq!(response.get("completed").unwrap(), &Value::Bool(false));
|
||||||
|
|
||||||
|
// Check seller object and its keys
|
||||||
|
let seller = response
|
||||||
|
.get("seller")
|
||||||
|
.expect("Field 'seller' is missing from response")
|
||||||
|
.as_object()
|
||||||
|
.expect("'seller' is not an object");
|
||||||
|
assert_has_keys_serde(seller, &["peerId"]);
|
||||||
|
|
||||||
|
// Check timelock object, nested 'None' object, and blocks_left
|
||||||
|
let timelock = response
|
||||||
|
.get("timelock")
|
||||||
|
.expect("Field 'timelock' is missing from response")
|
||||||
|
.as_object()
|
||||||
|
.expect("'timelock' is not an object");
|
||||||
|
let none_obj = timelock
|
||||||
|
.get("None")
|
||||||
|
.expect("Field 'None' is missing from 'timelock'")
|
||||||
|
.as_object()
|
||||||
|
.expect("'None' is not an object in 'timelock'");
|
||||||
|
let blocks_left = none_obj
|
||||||
|
.get("blocks_left")
|
||||||
|
.expect("Field 'blocks_left' is missing from 'None'")
|
||||||
|
.as_i64()
|
||||||
|
.expect("'blocks_left' is not an integer");
|
||||||
|
|
||||||
|
// Validate blocks_left
|
||||||
|
assert!(
|
||||||
|
blocks_left > 0 && blocks_left <= 180,
|
||||||
|
"Field 'blocks_left' should be > 0 and <= 180 but got {}",
|
||||||
|
blocks_left
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[serial]
|
||||||
|
pub async fn test_rpc_calls() {
|
||||||
|
setup_test(SlowCancelConfig, |harness_ctx| async move {
|
||||||
|
let alice_addr = harness_ctx.bob_params.get_concentenated_alice_address();
|
||||||
|
let (change_address, receive_address) =
|
||||||
|
harness_ctx.bob_params.get_change_receive_addresses().await;
|
||||||
|
|
||||||
|
let (client, writer, _) = setup_daemon(harness_ctx).await;
|
||||||
|
assert!(client.is_connected());
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
|
||||||
|
params.insert("force_refresh", false).unwrap();
|
||||||
|
let response: HashMap<String, i32> = client
|
||||||
|
.request("get_bitcoin_balance", params)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response, HashMap::from([("balance".to_string(), 10000000)]));
|
||||||
|
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params.insert("log_reference_id", "test_ref_id").unwrap();
|
||||||
|
params.insert("force_refresh", false).unwrap();
|
||||||
|
|
||||||
|
let _: HashMap<String, i32> = client.request("get_bitcoin_balance", params).await.unwrap();
|
||||||
|
|
||||||
|
assert!(writer.captured().contains(
|
||||||
|
r#"method{method_name="Balance" log_reference_id="\"test_ref_id\""}: swap::api::request: Current Bitcoin balance as of last sync balance=0.1 BTC"#
|
||||||
|
));
|
||||||
|
|
||||||
|
for method in ["get_swap_info", "resume_swap", "cancel_refund_swap"].iter() {
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params.insert("swap_id", "invalid_swap").unwrap();
|
||||||
|
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request(method, params).await;
|
||||||
|
response.expect_err(&format!(
|
||||||
|
"Expected an error when swap_id is invalid for method {}",
|
||||||
|
method
|
||||||
|
));
|
||||||
|
|
||||||
|
let params = ObjectParams::new();
|
||||||
|
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request(method, params).await;
|
||||||
|
response.expect_err(&format!(
|
||||||
|
"Expected an error when swap_id is missing for method {}",
|
||||||
|
method
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let params = ObjectParams::new();
|
||||||
|
let result: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("list_sellers", params).await;
|
||||||
|
|
||||||
|
result.expect_err("Expected an error when rendezvous_point is missing");
|
||||||
|
|
||||||
|
let params = ObjectParams::new();
|
||||||
|
let result: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("list_sellers", params).await;
|
||||||
|
|
||||||
|
result.expect_err("Expected an error when rendezvous_point is missing");
|
||||||
|
|
||||||
|
let params = ObjectParams::new();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("withdraw_btc", params).await;
|
||||||
|
response.expect_err("Expected an error when withdraw_address is missing");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params.insert("address", "invalid_address").unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("withdraw_btc", params).await;
|
||||||
|
response.expect_err("Expected an error when withdraw_address is malformed");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params.insert("address", BITCOIN_ADDR).unwrap();
|
||||||
|
params.insert("amount", "0").unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("withdraw_btc", params).await;
|
||||||
|
response.expect_err("Expected an error when amount is 0");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("address", BITCOIN_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params.insert("amount", "0.01").unwrap();
|
||||||
|
let response: HashMap<String, Value> = client
|
||||||
|
.request("withdraw_btc", params)
|
||||||
|
.await
|
||||||
|
.expect("Expected a valid response");
|
||||||
|
|
||||||
|
assert_has_keys_hashmap(&response, &["signed_tx", "amount", "txid"]);
|
||||||
|
assert_eq!(
|
||||||
|
response.get("amount").unwrap().as_u64().unwrap(),
|
||||||
|
1_000_000
|
||||||
|
);
|
||||||
|
|
||||||
|
let params = ObjectParams::new();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when no params are given");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("bitcoin_change_address", BITCOIN_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params
|
||||||
|
.insert("monero_receive_address", MONERO_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when seller is missing");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("bitcoin_change_address", BITCOIN_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params.insert("seller", SELLER).unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when monero_receive_address is missing");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("monero_receive_address", MONERO_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params.insert("seller", SELLER).unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when bitcoin_change_address is missing");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("bitcoin_change_address", "invalid_address")
|
||||||
|
.unwrap();
|
||||||
|
params
|
||||||
|
.insert("monero_receive_address", MONERO_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params.insert("seller", SELLER).unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when bitcoin_change_address is malformed");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("bitcoin_change_address", BITCOIN_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params
|
||||||
|
.insert("monero_receive_address", "invalid_address")
|
||||||
|
.unwrap();
|
||||||
|
params.insert("seller", SELLER).unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when monero_receive_address is malformed");
|
||||||
|
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("bitcoin_change_address", BITCOIN_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params
|
||||||
|
.insert("monero_receive_address", MONERO_ADDR)
|
||||||
|
.unwrap();
|
||||||
|
params.insert("seller", "invalid_seller").unwrap();
|
||||||
|
let response: Result<HashMap<String, String>, _> =
|
||||||
|
client.request("buy_xmr", params).await;
|
||||||
|
response.expect_err("Expected an error when seller is malformed");
|
||||||
|
|
||||||
|
let response: Result<HashMap<String, String>, _> = client
|
||||||
|
.request("suspend_current_swap", ObjectParams::new())
|
||||||
|
.await;
|
||||||
|
response.expect_err("Expected an error when no swap is running");
|
||||||
|
|
||||||
|
let mut params = ObjectParams::new();
|
||||||
|
params
|
||||||
|
.insert("bitcoin_change_address", change_address)
|
||||||
|
.unwrap();
|
||||||
|
params
|
||||||
|
.insert("monero_receive_address", receive_address)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
params.insert("seller", alice_addr).unwrap();
|
||||||
|
let response: HashMap<String, Value> = client
|
||||||
|
.request("buy_xmr", params)
|
||||||
|
.await
|
||||||
|
.expect("Expected a HashMap, got an error");
|
||||||
|
|
||||||
|
assert_has_keys_hashmap(&response, &["swapId"]);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[serial]
|
||||||
|
pub async fn suspend_current_swap_swap_running() {
|
||||||
|
setup_test(SlowCancelConfig, |harness_ctx| async move {
|
||||||
|
let (client, _, ctx) = setup_daemon(harness_ctx).await;
|
||||||
|
|
||||||
|
ctx.swap_lock
|
||||||
|
.acquire_swap_lock(Uuid::parse_str(SWAP_ID).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let cloned_ctx = ctx.clone();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
// Immediately release lock when suspend signal is received. Mocks a running swap that is then cancelled.
|
||||||
|
ctx.swap_lock
|
||||||
|
.listen_for_swap_force_suspension()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
ctx.swap_lock.release_swap_lock().await.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
let response: HashMap<String, String> = client
|
||||||
|
.request("suspend_current_swap", ObjectParams::new())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
response,
|
||||||
|
HashMap::from([("swapId".to_string(), SWAP_ID.to_string())])
|
||||||
|
);
|
||||||
|
|
||||||
|
cloned_ctx
|
||||||
|
.swap_lock
|
||||||
|
.acquire_swap_lock(Uuid::parse_str(SWAP_ID).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let response: Result<HashMap<String, String>, _> = client
|
||||||
|
.request("suspend_current_swap", ObjectParams::new())
|
||||||
|
.await;
|
||||||
|
response.expect_err("Expected an error when suspend signal times out");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
13
utils/gpg_keys/binarybaron.asc
Normal file
13
utils/gpg_keys/binarybaron.asc
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||||
|
|
||||||
|
mDMEZp+avRYJKwYBBAHaRw8BAQdAD99LhR+cHlXDsYPjRJr0Ag7BXsjGZKfdWCtx
|
||||||
|
CPA0fwG0LWJpbmFyeWJhcm9uIDxiaW5hcnliYXJvbkB1bnN0b3BwYWJsZXN3YXAu
|
||||||
|
bmV0PoiTBBMWCgA7FiEENahE1/S1W8ROGA/xmbddPhR2om4FAmafmr0CGwMFCwkI
|
||||||
|
BwICIgIGFQoJCAsCBBYCAwECHgcCF4AACgkQmbddPhR2om5IQQD/d/EmD/yKMKRl
|
||||||
|
Hw9RSP4bhcALmrZPri8sYkPteus8OhIA+wWTaIxXZJgydpXv95yECTfUXZ0UhuJq
|
||||||
|
6UH0FQL8mosJuDgEZp+avRIKKwYBBAGXVQEFAQEHQOd1tQ46YVKxyUKluPAvGJLY
|
||||||
|
LQ+3UWFWQJavLblkrYE2AwEIB4h4BBgWCgAgFiEENahE1/S1W8ROGA/xmbddPhR2
|
||||||
|
om4FAmafmr0CGwwACgkQmbddPhR2om6mmQEAn7vufrOp/HSYgn9l5tmJxMkyxJ3W
|
||||||
|
2WNo9u+JdnSik1IBAMsNcc4zm5ewfFr/qAnTHzHRId7dWR2+hs1oH7JOlf8L
|
||||||
|
=Rxij
|
||||||
|
-----END PGP PUBLIC KEY BLOCK-----
|
Loading…
x
Reference in New Issue
Block a user