1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
//! IPFS node implementation
//!
//! [Ipfs](https://ipfs.io) is a peer-to-peer system with content addressed functionality. The main
//! entry point for users of this crate is the [`Ipfs`] facade, which allows access to most of the
//! implemented functionality.
//!
//! This crate passes a lot of the [interface-ipfs-core] test suite; most of that functionality is
//! in `ipfs-http` crate. The crate has some interoperability with the [go-ipfs] and [js-ipfs]
//! implementations.
//!
//! `ipfs` is an early alpha level crate: APIs and their implementation are subject to change in
//! any upcoming release at least for now. The aim of the crate is to become a library-first
//! production ready implementation of an Ipfs node.
//!
//! [interface-ipfs-core]: https://www.npmjs.com/package/interface-ipfs-core
//! [go-ipfs]: https://github.com/ipfs/go-ipfs/
//! [js-ipfs]: https://github.com/ipfs/js-ipfs/
// We are not done yet, but uncommenting this makes it easier to hunt down for missing docs.
//#![deny(missing_docs)]
//
// This isn't recognized in stable yet, but we should disregard any nags on these to keep making
// the docs better.
//#![allow(private_intra_doc_links)]
pub mod config;
pub mod dag;
pub mod error;
pub mod ipns;
mod keystore;
pub mod p2p;
pub mod path;
pub mod refs;
pub mod repo;
mod task;
pub mod unixfs;
#[macro_use]
extern crate tracing;
use anyhow::{anyhow, format_err};
use dag::{DagGet, DagPut};
use either::Either;
use futures::{
channel::{
mpsc::{channel, Sender, UnboundedReceiver},
oneshot::{self, channel as oneshot_channel, Sender as OneshotSender},
},
future::BoxFuture,
sink::SinkExt,
stream::{BoxStream, Stream},
StreamExt, TryStreamExt,
};
use keystore::Keystore;
use p2p::{
BitswapConfig, IdentifyConfiguration, KadConfig, KadStoreConfig, PeerInfo, PubsubConfig,
RelayConfig,
};
use repo::{BlockStore, DataStore, GCConfig, GCTrigger, Lock};
use tokio::task::JoinHandle;
use tracing::Span;
use tracing_futures::Instrument;
use unixfs::{IpfsUnixfs, UnixfsAdd, UnixfsCat, UnixfsGet, UnixfsLs};
use std::{
collections::{BTreeSet, HashMap, HashSet},
fmt,
ops::{Deref, DerefMut, Range},
path::{Path, PathBuf},
sync::atomic::AtomicU64,
sync::Arc,
time::Duration,
};
use self::{
dag::IpldDag,
ipns::Ipns,
p2p::{create_swarm, TSwarm},
repo::Repo,
};
pub use self::p2p::gossipsub::SubscriptionStream;
pub use self::{
error::Error,
p2p::BehaviourEvent,
p2p::KadResult,
path::IpfsPath,
repo::{PinKind, PinMode},
};
pub type Block = libipld::Block<libipld::DefaultParams>;
use libipld::{Cid, Ipld, IpldCodec};
pub use libp2p::{
self,
core::transport::ListenerId,
gossipsub::{MessageId, PublishError},
identity::Keypair,
identity::PublicKey,
kad::{Quorum, RecordKey as Key},
multiaddr::multiaddr,
multiaddr::Protocol,
swarm::NetworkBehaviour,
Multiaddr, PeerId,
};
use libp2p::{
core::{muxing::StreamMuxerBox, transport::Boxed},
kad::{store::MemoryStoreConfig, Mode, Record},
ping::Config as PingConfig,
rendezvous::Namespace,
swarm::dial_opts::DialOpts,
StreamProtocol,
};
pub(crate) static BITSWAP_ID: AtomicU64 = AtomicU64::new(1);
#[derive(Default, Debug, Clone)]
pub enum StoragePath {
Disk(PathBuf),
#[default]
Memory,
Custom {
blockstore: Arc<dyn BlockStore>,
datastore: Arc<dyn DataStore>,
lock: Arc<dyn Lock>,
},
}
impl PartialEq for StoragePath {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(StoragePath::Disk(left_path), StoragePath::Disk(right_path)) => {
left_path.eq(right_path)
}
(StoragePath::Memory, StoragePath::Memory) => true,
(StoragePath::Custom { .. }, StoragePath::Custom { .. }) => {
//Do we really care if they equal?
//TODO: Possibly implement PartialEq/Eq for the traits so we could make sure
// that they do or dont eq each other. For now this will always be true
true
}
_ => false,
}
}
}
impl Eq for StoragePath {}
/// Ipfs node options used to configure the node to be created with [`UninitializedIpfs`].
// TODO: Refactor
#[derive(Clone)]
pub struct IpfsOptions {
/// The path of the ipfs repo (blockstore and datastore).
///
/// This is always required but can be any path with in-memory backends. The filesystem backend
/// creates a directory structure alike but not compatible to other ipfs implementations.
///
/// # Incompatiblity and interop warning
///
/// It is **not** recommended to set this to IPFS_PATH without first at least backing up your
/// existing repository.
pub ipfs_path: StoragePath,
/// Nodes used as bootstrap peers.
pub bootstrap: Vec<Multiaddr>,
/// Bitswap configuration
pub bitswap_config: Option<BitswapConfig>,
/// Relay server config
pub relay_server_config: Option<RelayConfig>,
/// Bound listening addresses; by default the node will not listen on any address.
pub listening_addrs: Vec<Multiaddr>,
/// Transport configuration
pub transport_configuration: Option<crate::p2p::TransportConfig>,
/// Swarm configuration
pub swarm_configuration: Option<crate::p2p::SwarmConfig>,
/// Identify configuration
pub identify_configuration: Option<crate::p2p::IdentifyConfiguration>,
/// Pubsub configuration
pub pubsub_config: Option<crate::p2p::PubsubConfig>,
/// Kad configuration
pub kad_configuration: Option<Either<KadConfig, libp2p::kad::Config>>,
/// Kad Store Config
/// Note: Only supports MemoryStoreConfig at this time
pub kad_store_config: KadStoreConfig,
/// Ping Configuration
pub ping_configuration: Option<PingConfig>,
/// Address book configuration
pub addr_config: Option<AddressBookConfig>,
pub keystore: Keystore,
/// Connection idle
pub connection_idle: Duration,
/// Repo Provider option
pub provider: RepoProvider,
/// The span for tracing purposes, `None` value is converted to `tracing::trace_span!("ipfs")`.
///
/// All futures returned by `Ipfs`, background task actions and swarm actions are instrumented
/// with this span or spans referring to this as their parent. Setting this other than `None`
/// default is useful when running multiple nodes.
pub span: Option<Span>,
pub(crate) protocols: Libp2pProtocol,
}
#[derive(Default, Clone, Copy)]
pub(crate) struct Libp2pProtocol {
pub(crate) pubsub: bool,
pub(crate) kad: bool,
pub(crate) bitswap: bool,
pub(crate) relay_client: bool,
pub(crate) relay_server: bool,
pub(crate) dcutr: bool,
pub(crate) mdns: bool,
pub(crate) identify: bool,
pub(crate) autonat: bool,
pub(crate) rendezvous_client: bool,
pub(crate) rendezvous_server: bool,
pub(crate) upnp: bool,
pub(crate) ping: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RepoProvider {
/// Dont provide any blocks automatically
#[default]
None,
/// Provide all blocks stored automatically
All,
/// Provide pinned blocks
Pinned,
/// Provide root blocks only
Roots,
}
impl Default for IpfsOptions {
fn default() -> Self {
Self {
ipfs_path: StoragePath::Memory,
bootstrap: Default::default(),
bitswap_config: Default::default(),
relay_server_config: Default::default(),
kad_configuration: Default::default(),
kad_store_config: Default::default(),
ping_configuration: Default::default(),
identify_configuration: Default::default(),
addr_config: Default::default(),
provider: Default::default(),
keystore: Keystore::in_memory(),
connection_idle: Duration::from_secs(30),
listening_addrs: vec![],
transport_configuration: None,
pubsub_config: None,
swarm_configuration: None,
span: None,
protocols: Default::default(),
}
}
}
impl fmt::Debug for IpfsOptions {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
// needed since libp2p::identity::Keypair does not have a Debug impl, and the IpfsOptions
// is a struct with all public fields, don't enforce users to use this wrapper.
fmt.debug_struct("IpfsOptions")
.field("ipfs_path", &self.ipfs_path)
.field("bootstrap", &self.bootstrap)
.field("listening_addrs", &self.listening_addrs)
.field("span", &self.span)
.finish()
}
}
/// The facade for the Ipfs node.
///
/// The facade has most of the functionality either directly as a method or the functionality can
/// be implemented using the provided methods. For more information, see examples or the HTTP
/// endpoint implementations in `ipfs-http`.
///
/// The facade is created through [`UninitializedIpfs`] which is configured with [`IpfsOptions`].
#[derive(Clone)]
#[allow(clippy::type_complexity)]
pub struct Ipfs {
span: Span,
repo: Repo,
key: Keypair,
keystore: Keystore,
identify_conf: IdentifyConfiguration,
to_task: Sender<IpfsEvent>,
record_key_validator: HashMap<String, Arc<dyn Fn(&str) -> anyhow::Result<Key> + Sync + Send>>,
}
impl std::fmt::Debug for Ipfs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ipfs").finish()
}
}
type Channel<T> = OneshotSender<Result<T, Error>>;
type ReceiverChannel<T> = oneshot::Receiver<Result<T, Error>>;
/// Events used internally to communicate with the swarm, which is executed in the the background
/// task.
#[derive(Debug)]
#[allow(clippy::type_complexity)]
enum IpfsEvent {
/// Connect
Connect(DialOpts, Channel<()>),
/// Node supported protocol
Protocol(OneshotSender<Vec<String>>),
/// Addresses
Addresses(Channel<Vec<(PeerId, Vec<Multiaddr>)>>),
/// Local addresses
Listeners(Channel<Vec<Multiaddr>>),
/// Local addresses
ExternalAddresses(Channel<Vec<Multiaddr>>),
/// Connected peers
Connected(Channel<Vec<PeerId>>),
/// Is Connected
IsConnected(PeerId, Channel<bool>),
/// Disconnect
Disconnect(PeerId, Channel<()>),
/// Ban Peer
Ban(PeerId, Channel<()>),
/// Unban peer
Unban(PeerId, Channel<()>),
PubsubSubscribe(String, Channel<Option<SubscriptionStream>>),
PubsubUnsubscribe(String, Channel<Result<bool, Error>>),
PubsubPublish(String, Vec<u8>, Channel<Result<MessageId, PublishError>>),
PubsubPeers(Option<String>, Channel<Vec<PeerId>>),
GetBitswapPeers(Channel<BoxFuture<'static, Vec<PeerId>>>),
WantList(Option<PeerId>, Channel<BoxFuture<'static, Vec<Cid>>>),
PubsubSubscribed(Channel<Vec<String>>),
AddListeningAddress(Multiaddr, Channel<Multiaddr>),
RemoveListeningAddress(Multiaddr, Channel<()>),
Bootstrap(Channel<ReceiverChannel<KadResult>>),
AddPeer(PeerId, Multiaddr, Channel<()>),
RemovePeer(PeerId, Option<Multiaddr>, Channel<bool>),
GetClosestPeers(PeerId, Channel<ReceiverChannel<KadResult>>),
FindPeerIdentity(PeerId, Channel<ReceiverChannel<libp2p::identify::Info>>),
FindPeer(
PeerId,
bool,
Channel<Either<Vec<Multiaddr>, ReceiverChannel<KadResult>>>,
),
GetProviders(Cid, Channel<Option<BoxStream<'static, PeerId>>>),
Provide(Cid, Channel<ReceiverChannel<KadResult>>),
DhtMode(DhtMode, Channel<()>),
DhtGet(Key, Channel<BoxStream<'static, Record>>),
DhtPut(Key, Vec<u8>, Quorum, Channel<ReceiverChannel<KadResult>>),
GetBootstrappers(OneshotSender<Vec<Multiaddr>>),
AddBootstrapper(Multiaddr, Channel<Multiaddr>),
RemoveBootstrapper(Multiaddr, Channel<Multiaddr>),
ClearBootstrappers(Channel<Vec<Multiaddr>>),
DefaultBootstrap(Channel<Vec<Multiaddr>>),
AddRelay(PeerId, Multiaddr, Channel<()>),
RemoveRelay(PeerId, Multiaddr, Channel<()>),
EnableRelay(Option<PeerId>, Channel<()>),
DisableRelay(PeerId, Channel<()>),
ListRelays(Channel<Vec<(PeerId, Vec<Multiaddr>)>>),
ListActiveRelays(Channel<Vec<(PeerId, Vec<Multiaddr>)>>),
//event streams
PubsubEventStream(OneshotSender<UnboundedReceiver<InnerPubsubEvent>>),
RegisterRendezvousNamespace(Namespace, PeerId, Option<u64>, Channel<()>),
UnregisterRendezvousNamespace(Namespace, PeerId, Channel<()>),
RendezvousNamespaceDiscovery(
Option<Namespace>,
bool,
Option<u64>,
PeerId,
Channel<HashMap<PeerId, Vec<Multiaddr>>>,
),
Exit,
}
#[derive(Debug, Copy, Clone)]
pub enum DhtMode {
Auto,
Client,
Server,
}
impl From<DhtMode> for Option<Mode> {
fn from(mode: DhtMode) -> Self {
match mode {
DhtMode::Auto => None,
DhtMode::Client => Some(Mode::Client),
DhtMode::Server => Some(Mode::Server),
}
}
}
#[derive(Debug, Clone)]
pub enum PubsubEvent {
/// Subscription event to a given topic
Subscribe { peer_id: PeerId },
/// Unsubscribing event to a given topic
Unsubscribe { peer_id: PeerId },
}
#[derive(Debug, Clone)]
pub(crate) enum InnerPubsubEvent {
/// Subscription event to a given topic
Subscribe { topic: String, peer_id: PeerId },
/// Unsubscribing event to a given topic
Unsubscribe { topic: String, peer_id: PeerId },
}
impl From<InnerPubsubEvent> for PubsubEvent {
fn from(event: InnerPubsubEvent) -> Self {
match event {
InnerPubsubEvent::Subscribe { peer_id, .. } => PubsubEvent::Subscribe { peer_id },
InnerPubsubEvent::Unsubscribe { peer_id, .. } => PubsubEvent::Unsubscribe { peer_id },
}
}
}
type TSwarmEvent<C> = <TSwarm<C> as Stream>::Item;
type TSwarmEventFn<C> = Arc<dyn Fn(&mut TSwarm<C>, &TSwarmEvent<C>) + Sync + Send>;
type TTransportFn = Box<
dyn Fn(
&Keypair,
Option<libp2p::relay::client::Transport>,
) -> std::io::Result<Boxed<(PeerId, StreamMuxerBox)>>
+ Sync
+ Send
+ 'static,
>;
#[derive(Debug, Copy, Clone)]
pub enum FDLimit {
Max,
Custom(u64),
}
/// Configured Ipfs which can only be started.
#[allow(clippy::type_complexity)]
pub struct UninitializedIpfs<C: NetworkBehaviour<ToSwarm = void::Void> + Send> {
keys: Option<Keypair>,
options: IpfsOptions,
fdlimit: Option<FDLimit>,
repo_handle: Option<Repo>,
local_external_addr: bool,
swarm_event: Option<TSwarmEventFn<C>>,
// record_validators: HashMap<String, Arc<dyn Fn(&str, &Record) -> bool + Sync + Send>>,
record_key_validator: HashMap<String, Arc<dyn Fn(&str) -> anyhow::Result<Key> + Sync + Send>>,
custom_behaviour: Option<C>,
custom_transport: Option<TTransportFn>,
gc_config: Option<GCConfig>,
gc_repo_duration: Option<Duration>,
}
pub type UninitializedIpfsNoop = UninitializedIpfs<libp2p::swarm::dummy::Behaviour>;
impl<C: NetworkBehaviour<ToSwarm = void::Void> + Send> Default for UninitializedIpfs<C> {
fn default() -> Self {
Self::new()
}
}
impl<C: NetworkBehaviour<ToSwarm = void::Void> + Send> UninitializedIpfs<C> {
/// New uninitualized instance
pub fn new() -> Self {
UninitializedIpfs {
keys: None,
options: Default::default(),
fdlimit: None,
repo_handle: None,
// record_validators: Default::default(),
record_key_validator: Default::default(),
local_external_addr: false,
swarm_event: None,
custom_behaviour: None,
custom_transport: None,
gc_config: None,
gc_repo_duration: None,
}
}
/// New uninitualized instance without any listener addresses
#[deprecated(
note = "UninitializedIpfs::empty will be removed in the future. Use UninitializedIpfs::new()"
)]
pub fn empty() -> Self {
Self::new()
}
/// Configures a new UninitializedIpfs with from the given options and optionally a span.
/// If the span is not given, it is defaulted to `tracing::trace_span!("ipfs")`.
///
/// The span is attached to all operations called on the later created `Ipfs` along with all
/// operations done in the background task as well as tasks spawned by the underlying
/// `libp2p::Swarm`.
#[deprecated(
note = "UninitializedIpfs::with_opt will be removed in the future. Use UninitializedIpfs::new()"
)]
pub fn with_opt(options: IpfsOptions) -> Self {
let mut opt = Self::new();
opt.options = options;
opt
}
/// Set default listening unspecified ipv4 and ipv6 addresseses for tcp and udp/quic
pub fn set_default_listener(self) -> Self {
self.add_listening_addrs(vec![
"/ip4/0.0.0.0/tcp/0".parse().unwrap(),
"/ip4/0.0.0.0/udp/0/quic-v1".parse().unwrap(),
])
}
/// Adds a listening address
pub fn add_listening_addr(mut self, addr: Multiaddr) -> Self {
if !self.options.listening_addrs.contains(&addr) {
self.options.listening_addrs.push(addr)
}
self
}
/// Adds a listening addresses
pub fn add_listening_addrs(mut self, addrs: Vec<Multiaddr>) -> Self {
self.options.listening_addrs.extend(addrs);
self
}
/// Set a list of listening addresses
pub fn set_listening_addrs(mut self, addrs: Vec<Multiaddr>) -> Self {
self.options.listening_addrs = addrs;
self
}
/// Adds a bootstrap node
pub fn add_bootstrap(mut self, addr: Multiaddr) -> Self {
if !self.options.bootstrap.contains(&addr) {
self.options.bootstrap.push(addr)
}
self
}
/// Load default behaviour for basic functionality
pub fn with_default(self) -> Self {
self.with_identify(None)
.with_autonat()
.with_bitswap(None)
.with_kademlia(None, Default::default())
.with_ping(None)
.with_pubsub(None)
}
/// Enables kademlia
pub fn with_kademlia(
mut self,
config: Option<Either<KadConfig, libp2p::kad::Config>>,
store: KadStoreConfig,
) -> Self {
self.options.protocols.kad = true;
self.options.kad_configuration = config;
self.options.kad_store_config = store;
self
}
/// Enables bitswap
pub fn with_bitswap(mut self, config: Option<BitswapConfig>) -> Self {
self.options.protocols.bitswap = true;
self.options.bitswap_config = config;
self
}
/// Enable mdns
pub fn with_mdns(mut self) -> Self {
self.options.protocols.mdns = true;
self
}
/// Enable relay client
pub fn with_relay(mut self, with_dcutr: bool) -> Self {
self.options.protocols.relay_client = true;
self.options.protocols.dcutr = with_dcutr;
self
}
/// Enable relay server
pub fn with_relay_server(mut self, config: Option<RelayConfig>) -> Self {
self.options.protocols.relay_server = true;
self.options.relay_server_config = config;
self
}
/// Enable port mapping (AKA UPnP)
pub fn with_upnp(mut self) -> Self {
self.options.protocols.upnp = true;
self
}
/// Enables rendezvous server
pub fn with_rendezvous_server(mut self) -> Self {
self.options.protocols.rendezvous_server = true;
self
}
/// Enables rendezvous client
pub fn with_rendezvous_client(mut self) -> Self {
self.options.protocols.rendezvous_client = true;
self
}
/// Enables identify
pub fn with_identify(mut self, config: Option<crate::p2p::IdentifyConfiguration>) -> Self {
self.options.protocols.identify = true;
self.options.identify_configuration = config;
self
}
/// Enables pubsub
pub fn with_pubsub(mut self, config: Option<PubsubConfig>) -> Self {
self.options.protocols.pubsub = true;
self.options.pubsub_config = config;
self
}
/// Enables autonat
pub fn with_autonat(mut self) -> Self {
self.options.protocols.autonat = true;
self
}
/// Enables ping
pub fn with_ping(mut self, config: Option<PingConfig>) -> Self {
self.options.protocols.ping = true;
self.options.ping_configuration = config;
self
}
/// Set a custom behaviour
pub fn with_custom_behaviour(mut self, behaviour: C) -> Self {
self.custom_behaviour = Some(behaviour);
self
}
/// Enables automatic garbage collection
pub fn with_gc(mut self, config: GCConfig) -> Self {
self.gc_config = Some(config);
self
}
/// Set a duration for which blocks are not removed due to the garbage collector
/// Defaults: 2 mins
pub fn set_temp_pin_duration(mut self, duration: Duration) -> Self {
self.gc_repo_duration = Some(duration);
self
}
/// Sets a path
pub fn set_path<P: AsRef<Path>>(mut self, path: P) -> Self {
let path = path.as_ref().to_path_buf();
self.options.ipfs_path = StoragePath::Disk(path);
self
}
/// Set transport configuration
pub fn set_transport_configuration(mut self, config: crate::p2p::TransportConfig) -> Self {
self.options.transport_configuration = Some(config);
self
}
/// Set timeout for idle connections
pub fn set_idle_connection_timeout(mut self, duration: u64) -> Self {
self.options.connection_idle = Duration::from_secs(duration);
self
}
/// Set swarm configuration
pub fn set_swarm_configuration(mut self, config: crate::p2p::SwarmConfig) -> Self {
self.options.swarm_configuration = Some(config);
self
}
/// Set default record validator for IPFS
/// Note: This will override any keys set for `ipns` prefix
pub fn default_record_key_validator(mut self) -> Self {
self.record_key_validator.insert(
"ipns".into(),
Arc::new(|key| to_dht_key(("ipns", |key| ipns_to_dht_key(key)), key)),
);
self
}
#[allow(clippy::type_complexity)]
pub fn set_record_prefix_validator(
mut self,
key: &str,
callback: Arc<dyn Fn(&str) -> anyhow::Result<Key> + Sync + Send>,
) -> Self {
self.record_key_validator.insert(key.to_string(), callback);
self
}
/// Set address book configuration
pub fn set_addrbook_configuration(mut self, config: AddressBookConfig) -> Self {
self.options.addr_config = Some(config);
self
}
/// Set RepoProvider option to provide blocks automatically
pub fn set_provider(mut self, opt: RepoProvider) -> Self {
self.options.provider = opt;
self
}
/// Set keypair
pub fn set_keypair(mut self, keypair: Keypair) -> Self {
self.keys = Some(keypair);
self
}
/// Set block and data repo
pub fn set_repo(mut self, repo: Repo) -> Self {
self.repo_handle = Some(repo);
self
}
/// Set a keystore
pub fn set_keystore(mut self, keystore: Keystore) -> Self {
self.options.keystore = keystore;
self
}
/// Automatically add any listened address as an external address
pub fn listen_as_external_addr(mut self) -> Self {
self.local_external_addr = true;
self
}
#[allow(clippy::type_complexity)]
/// Set a transport
pub fn with_custom_transport(mut self, transport: TTransportFn) -> Self {
self.custom_transport = Some(transport);
self
}
/// Set file desc limit
pub fn fd_limit(mut self, limit: FDLimit) -> Self {
self.fdlimit = Some(limit);
self
}
/// Set tracing span
pub fn set_span(mut self, span: Span) -> Self {
self.options.span = Some(span);
self
}
/// Handle libp2p swarm events
pub fn swarm_events<F>(mut self, func: F) -> Self
where
F: Fn(&mut TSwarm<C>, &TSwarmEvent<C>) + Sync + Send + 'static,
{
self.swarm_event = Some(Arc::new(func));
self
}
/// Initialize the ipfs node. The returned `Ipfs` value is cloneable, send and sync.
pub async fn start(self) -> Result<Ipfs, Error> {
let UninitializedIpfs {
keys,
fdlimit,
mut options,
swarm_event,
custom_behaviour,
custom_transport,
record_key_validator,
local_external_addr,
repo_handle,
gc_config,
gc_repo_duration,
..
} = self;
let keys = keys.unwrap_or(Keypair::generate_ed25519());
let root_span = Option::take(&mut options.span)
// not sure what would be the best practice with tracing and spans
.unwrap_or_else(|| tracing::trace_span!(parent: &Span::current(), "ipfs"));
// the "current" span which is not entered but the awaited futures are instrumented with it
let init_span = tracing::trace_span!(parent: &root_span, "init");
// stored in the Ipfs, instrumenting every method call
let facade_span = tracing::trace_span!("facade");
// stored in the executor given to libp2p, used to spawn at least the connections,
// instrumenting each of those.
let exec_span = tracing::trace_span!(parent: &root_span, "exec");
// instruments the IpfsFuture, the background task.
let swarm_span = tracing::trace_span!(parent: &root_span, "swarm");
let repo = match repo_handle {
Some(repo) => {
if repo.is_online() {
anyhow::bail!("Repo is already initialized");
}
repo
}
None => {
if let StoragePath::Disk(path) = &options.ipfs_path {
if !path.is_dir() {
tokio::fs::create_dir_all(path).await?;
}
}
Repo::new(options.ipfs_path.clone(), gc_repo_duration)
}
};
repo.init().instrument(init_span.clone()).await?;
let repo_events = repo.initialize_channel();
if let Some(limit) = fdlimit {
#[cfg(unix)]
{
let (_, hard) = rlimit::Resource::NOFILE.get()?;
let limit = match limit {
FDLimit::Max => hard,
FDLimit::Custom(limit) => limit,
};
let target = std::cmp::min(hard, limit);
rlimit::Resource::NOFILE.set(target, hard)?;
let (soft, _) = rlimit::Resource::NOFILE.get()?;
if soft < 2048 {
error!("Limit is too low: {soft}");
}
}
#[cfg(not(unix))]
{
warn!("Cannot set {limit:?}. Can only set a fd limit on unix systems. Ignoring...")
}
}
let (to_task, receiver) = channel::<IpfsEvent>(1);
let id_conf = options.identify_configuration.clone().unwrap_or_default();
let keystore = options.keystore.clone();
let ipfs = Ipfs {
span: facade_span,
repo: repo.clone(),
identify_conf: id_conf,
key: keys.clone(),
keystore,
to_task,
record_key_validator,
};
//Note: If `All` or `Pinned` are used, we would have to auto adjust the amount of
// provider records by adding the amount of blocks to the config.
//TODO: Add persistent layer for kad store
let blocks = match options.provider {
RepoProvider::None => vec![],
RepoProvider::All => repo.list_blocks().await.unwrap_or_default(),
RepoProvider::Pinned => {
repo.list_pins(None)
.await
.filter_map(|result| async move { result.map(|(cid, _)| cid).ok() })
.collect()
.await
}
RepoProvider::Roots => {
//TODO: Scan blockstore for root unixfs blocks
warn!("RepoProvider::Roots is not implemented... ignoring...");
vec![]
}
};
let count = blocks.len();
let store_config = &mut options.kad_store_config;
match store_config.memory.as_mut() {
Some(memory_config) => {
memory_config.max_provided_keys += count;
}
None => {
store_config.memory = Some(MemoryStoreConfig {
//Provide a buffer to the max amount of provided keys
max_provided_keys: (50 * 1024) + count,
..Default::default()
})
}
}
let swarm_config = options.swarm_configuration.clone().unwrap_or_default();
let transport_config = options.transport_configuration.unwrap_or_default();
let swarm = create_swarm(
&keys,
&options,
swarm_config,
transport_config,
repo.clone(),
exec_span,
(custom_behaviour, custom_transport),
)
.instrument(tracing::trace_span!(parent: &init_span, "swarm"))
.await?;
let IpfsOptions {
listening_addrs, ..
} = options;
if let Some(config) = gc_config {
tokio::spawn({
let repo = repo.clone();
async move {
let GCConfig { duration, trigger } = config;
let use_config_timer = duration != Duration::ZERO;
if trigger == GCTrigger::None && !use_config_timer {
tracing::warn!("GC does not have a set timer or a trigger. Disabling GC");
return;
}
let time = match use_config_timer {
true => duration,
false => Duration::from_secs(60 * 60),
};
loop {
tokio::time::sleep(time).await;
let pinned = repo
.list_pins(None)
.await
.try_filter_map(|(cid, _)| futures::future::ready(Ok(Some(cid))))
.try_collect::<BTreeSet<_>>()
.await
.unwrap_or_default();
let pinned = Vec::from_iter(pinned);
let total_size = repo.get_total_size().await.unwrap_or_default();
let pinned_size = repo
.get_blocks_size(&pinned)
.await
.ok()
.flatten()
.unwrap_or_default();
let cleanup = match trigger {
GCTrigger::At { size } => {
total_size > 0 && (total_size - pinned_size) >= size
}
GCTrigger::AtStorage => {
(total_size - pinned_size) > 0
&& (total_size - pinned_size) >= repo.max_storage_size()
}
GCTrigger::None => (total_size - pinned_size) > 0,
};
if cleanup {
let blocks = repo.cleanup().await.unwrap();
for block in blocks {
tracing::debug!(
block = block.to_string(),
"has been cleared from the block store"
);
}
}
}
}
});
}
let mut fut = task::IpfsTask::new(swarm, repo_events.fuse(), receiver.fuse(), repo);
fut.swarm_event = swarm_event;
fut.local_external_addr = local_external_addr;
for addr in listening_addrs.into_iter() {
match fut.swarm.listen_on(addr) {
Ok(id) => {
let (tx, _rx) = oneshot_channel();
fut.pending_add_listener.insert(id, tx);
}
_ => continue,
};
}
for block in blocks {
if let Some(kad) = fut.swarm.behaviour_mut().kademlia.as_mut() {
let key = Key::from(block.hash().to_bytes());
match kad.start_providing(key) {
Ok(id) => {
let (tx, _rx) = oneshot_channel();
fut.kad_subscriptions.insert(id, tx);
}
Err(e) => match e {
libp2p::kad::store::Error::MaxProvidedKeys => break,
_ => unreachable!(),
},
};
}
}
tokio::spawn({
async move {
//Note: For now this is not configurable as its meant for internal testing purposes but may change in the future
let as_fut = false;
if as_fut {
fut.instrument(swarm_span).await;
} else {
fut.run().instrument(swarm_span).await;
}
}
});
Ok(ipfs)
}
}
impl Ipfs {
/// Return an [`IpldDag`] for DAG operations
pub fn dag(&self) -> IpldDag {
IpldDag::new(self.clone())
}
/// Return an [`Repo`] to access the internal repo of the node
pub fn repo(&self) -> &Repo {
&self.repo
}
/// Returns an [`IpfsFiles`] for files operations
pub fn unixfs(&self) -> IpfsUnixfs {
IpfsUnixfs::new(self.clone())
}
pub fn ipns(&self) -> Ipns {
Ipns::new(self.clone())
}
/// Puts a block into the ipfs repo.
///
/// # Forget safety
///
/// Forgetting the returned future will not result in memory unsafety, but it can
/// deadlock other tasks.
pub async fn put_block(&self, block: Block) -> Result<Cid, Error> {
self.repo
.put_block(block)
.instrument(self.span.clone())
.await
.map(|(cid, _put_status)| cid)
}
/// Retrieves a block from the local blockstore, or starts fetching from the network or join an
/// already started fetch.
pub async fn get_block(&self, cid: &Cid) -> Result<Block, Error> {
self.repo
.get_block(cid, &[], false)
.instrument(self.span.clone())
.await
}
/// Remove block from the ipfs repo. A pinned block cannot be removed.
pub async fn remove_block(&self, cid: Cid, recursive: bool) -> Result<Vec<Cid>, Error> {
self.repo
.remove_block(&cid, recursive)
.instrument(self.span.clone())
.await
}
/// Cleans up of all unpinned blocks
/// Note: This is extremely basic and should not be relied on completely
/// until there is additional or extended implementation for a gc
pub async fn gc(&self) -> Result<Vec<Cid>, Error> {
self.repo.cleanup().instrument(self.span.clone()).await
}
/// Pins a given Cid recursively or directly (non-recursively).
///
/// Pins on a block are additive in sense that a previously directly (non-recursively) pinned
/// can be made recursive, but removing the recursive pin on the block removes also the direct
/// pin as well.
///
/// Pinning a Cid recursively (for supported dag-protobuf and dag-cbor) will walk its
/// references and pin the references indirectly. When a Cid is pinned indirectly it will keep
/// its previous direct or recursive pin and be indirect in addition.
///
/// Recursively pinned Cids cannot be re-pinned non-recursively but non-recursively pinned Cids
/// can be "upgraded to" being recursively pinned.
///
/// # Crash unsafety
///
/// If a recursive `insert_pin` operation is interrupted because of a crash or the crash
/// prevents from synchronizing the data store to disk, this will leave the system in an inconsistent
/// state. The remedy is to re-pin recursive pins.
pub async fn insert_pin(&self, cid: &Cid, recursive: bool) -> Result<(), Error> {
let span = debug_span!(parent: &self.span, "insert_pin", cid = %cid, recursive);
self.repo()
.insert_pin(cid, recursive, false)
.instrument(span)
.await
}
/// Unpins a given Cid recursively or only directly.
///
/// Recursively unpinning a previously only directly pinned Cid will remove the direct pin.
///
/// Unpinning an indirectly pinned Cid is not possible other than through its recursively
/// pinned tree roots.
pub async fn remove_pin(&self, cid: &Cid, recursive: bool) -> Result<(), Error> {
let span = debug_span!(parent: &self.span, "remove_pin", cid = %cid, recursive);
async move {
if !recursive {
self.repo.remove_direct_pin(cid).await
} else {
// start walking refs of the root after loading it
let block = match self.repo.get_block_now(cid).await? {
Some(b) => b,
None => {
return Err(anyhow::anyhow!("pinned root not found: {}", cid));
}
};
let ipld = block.decode::<IpldCodec, Ipld>()?;
let st = crate::refs::IpldRefs::default()
.with_only_unique()
.with_existing_blocks()
.refs_of_resolved(self.repo(), vec![(*cid, ipld.clone())].into_iter())
.map_ok(|crate::refs::Edge { destination, .. }| destination)
.into_stream()
.boxed();
self.repo.remove_recursive_pin(cid, st).await
}
}
.instrument(span)
.await
}
/// Checks whether a given block is pinned.
///
/// Returns true if the block is pinned, false if not. See Crash unsafety notes for the false
/// response.
///
/// # Crash unsafety
///
/// Cannot currently detect partially written recursive pins. Those can happen if
/// `Ipfs::insert_pin(cid, true)` is interrupted by a crash for example.
///
/// Works correctly only under no-crash situations. Workaround for hitting a crash is to re-pin
/// any existing recursive pins.
///
// TODO: This operation could be provided as a `Ipfs::fix_pins()`.
pub async fn is_pinned(&self, cid: &Cid) -> Result<bool, Error> {
let span = debug_span!(parent: &self.span, "is_pinned", cid = %cid);
self.repo.is_pinned(cid).instrument(span).await
}
/// Lists all pins, or the specific kind thereof.
///
/// # Crash unsafety
///
/// Does not currently recover from partial recursive pin insertions.
pub async fn list_pins(
&self,
filter: Option<PinMode>,
) -> futures::stream::BoxStream<'static, Result<(Cid, PinMode), Error>> {
let span = debug_span!(parent: &self.span, "list_pins", ?filter);
self.repo.list_pins(filter).instrument(span).await
}
/// Read specific pins. When `requirement` is `Some`, all pins are required to be of the given
/// [`PinMode`].
///
/// # Crash unsafety
///
/// Does not currently recover from partial recursive pin insertions.
pub async fn query_pins(
&self,
cids: Vec<Cid>,
requirement: Option<PinMode>,
) -> Result<Vec<(Cid, PinKind<Cid>)>, Error> {
let span = debug_span!(parent: &self.span, "query_pins", ids = cids.len(), ?requirement);
self.repo
.query_pins(cids, requirement)
.instrument(span)
.await
}
/// Puts an ipld node into the ipfs repo using `dag-cbor` codec and Sha2_256 hash.
///
/// Returns Cid version 1 for the document
pub fn put_dag(&self, ipld: Ipld) -> DagPut {
self.dag().put_dag(ipld).span(self.span.clone())
}
/// Gets an ipld node from the ipfs, fetching the block if necessary.
///
/// See [`IpldDag::get`] for more information.
pub fn get_dag<I: Into<IpfsPath>>(&self, path: I) -> DagGet {
self.dag().get_dag(path).span(self.span.clone())
}
/// Get an ipld path from the datastore.
/// Note: This will be replaced in the future and shouldnt be depended on completely
pub async fn get_ipns(&self, peer_id: &PeerId) -> Result<Option<IpfsPath>, Error> {
self.repo
.get_ipns(peer_id)
.instrument(self.span.clone())
.await
}
/// Put an ipld path into the datastore.
/// Note: This will be replaced in the future and shouldnt be depended on completely
pub async fn put_ipns(&self, peer_id: &PeerId, path: &IpfsPath) -> Result<(), Error> {
self.repo
.put_ipns(peer_id, path)
.instrument(self.span.clone())
.await
}
/// Remove an ipld path from the datastore.
/// Note: This will be replaced in the future and shouldnt be depended on completely
pub async fn remove_ipns(&self, peer_id: &PeerId) -> Result<(), Error> {
self.repo
.remove_ipns(peer_id)
.instrument(self.span.clone())
.await
}
/// Creates a stream which will yield the bytes of an UnixFS file from the root Cid, with the
/// optional file byte range. If the range is specified and is outside of the file, the stream
/// will end without producing any bytes.
///
/// To create an owned version of the stream, please use `ipfs::unixfs::cat` directly.
pub fn cat_unixfs(
&self,
starting_point: impl Into<unixfs::StartingPoint>,
range: Option<Range<u64>>,
) -> UnixfsCat<'_> {
self.unixfs()
.cat(starting_point, range, &[], false, None)
.span(self.span.clone())
}
/// Add a file from a path to the blockstore
///
/// To create an owned version of the stream, please use `ipfs::unixfs::add_file` directly.
pub fn add_file_unixfs<P: AsRef<std::path::Path>>(&self, path: P) -> UnixfsAdd<'_> {
let path = path.as_ref();
self.unixfs().add(path, None).span(self.span.clone())
}
/// Add a file through a stream of data to the blockstore
///
/// To create an owned version of the stream, please use `ipfs::unixfs::add` directly.
pub fn add_unixfs<'a>(&self, stream: BoxStream<'a, std::io::Result<Vec<u8>>>) -> UnixfsAdd<'a> {
self.unixfs().add(stream, None).span(self.span.clone())
}
/// Retreive a file and saving it to a path.
///
/// To create an owned version of the stream, please use `ipfs::unixfs::get` directly.
pub fn get_unixfs<P: AsRef<Path>>(&self, path: IpfsPath, dest: P) -> UnixfsGet<'_> {
self.unixfs()
.get(path, dest, &[], false, None)
.span(self.span.clone())
}
/// List directory contents
pub fn ls_unixfs(&self, path: IpfsPath) -> UnixfsLs<'_> {
self.unixfs()
.ls(path, &[], false, None)
.span(self.span.clone())
}
/// Resolves a ipns path to an ipld path; currently only supports dht and dnslink resolution.
pub async fn resolve_ipns(&self, path: &IpfsPath, recursive: bool) -> Result<IpfsPath, Error> {
async move {
let ipns = self.ipns();
let mut resolved = ipns.resolve(path).await;
if recursive {
let mut seen = HashSet::with_capacity(1);
while let Ok(ref res) = resolved {
if !seen.insert(res.clone()) {
break;
}
resolved = ipns.resolve(res).await;
}
}
resolved
}
.instrument(self.span.clone())
.await
}
/// Publish ipns record to DHT
pub async fn publish_ipns(&self, path: &IpfsPath) -> Result<IpfsPath, Error> {
async move {
let ipns = self.ipns();
ipns.publish(None, path, None).await
}
.instrument(self.span.clone())
.await
}
/// Connects to the peer
pub async fn connect(&self, target: impl Into<DialOpts>) -> Result<(), Error> {
async move {
let target = target.into();
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::Connect(target, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Returns known peer addresses
pub async fn addrs(&self) -> Result<Vec<(PeerId, Vec<Multiaddr>)>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task.clone().send(IpfsEvent::Addresses(tx)).await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Checks whether there is an established connection to a peer.
pub async fn is_connected(&self, peer_id: PeerId) -> Result<bool, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::IsConnected(peer_id, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Returns the connected peers
pub async fn connected(&self) -> Result<Vec<PeerId>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task.clone().send(IpfsEvent::Connected(tx)).await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Disconnects a given peer.
pub async fn disconnect(&self, target: PeerId) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::Disconnect(target, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Bans a peer.
pub async fn ban_peer(&self, target: PeerId) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::Ban(target, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Unbans a peer.
pub async fn unban_peer(&self, target: PeerId) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::Unban(target, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Returns the peer identity information. If no peer id is supplied the local node identity is used.
pub async fn identity(&self, peer_id: Option<PeerId>) -> Result<PeerInfo, Error> {
async move {
match peer_id {
Some(peer_id) => {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::FindPeerIdentity(peer_id, tx))
.await?;
rx.await??.await?.map(PeerInfo::from)
}
None => {
let mut addresses = HashSet::new();
let (local_result, external_result) =
futures::join!(self.listening_addresses(), self.external_addresses());
let external: HashSet<Multiaddr> =
HashSet::from_iter(external_result.unwrap_or_default());
let local: HashSet<Multiaddr> =
HashSet::from_iter(local_result.unwrap_or_default());
addresses.extend(external.iter().cloned());
addresses.extend(local.iter().cloned());
let mut addresses = Vec::from_iter(addresses);
let (tx, rx) = oneshot_channel();
self.to_task.clone().send(IpfsEvent::Protocol(tx)).await?;
let protocols = rx
.await?
.iter()
.filter_map(|s| StreamProtocol::try_from_owned(s.clone()).ok())
.collect();
let public_key = self.key.public();
let peer_id = public_key.to_peer_id();
for addr in &mut addresses {
if !matches!(addr.iter().last(), Some(Protocol::P2p(_))) {
addr.push(Protocol::P2p(peer_id))
}
}
let info = PeerInfo {
peer_id,
public_key,
protocol_version: self.identify_conf.protocol_version.clone(),
agent_version: self.identify_conf.agent_version.clone(),
listen_addrs: addresses,
protocols,
observed_addr: None,
};
Ok(info)
}
}
}
.instrument(self.span.clone())
.await
}
/// Subscribes to a given topic. Can be done at most once without unsubscribing in the between.
/// The subscription can be unsubscribed by dropping the stream or calling
/// [`Ipfs::pubsub_unsubscribe`].
pub async fn pubsub_subscribe(&self, topic: String) -> Result<SubscriptionStream, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::PubsubSubscribe(topic.clone(), tx))
.await?;
rx.await??
.ok_or_else(|| format_err!("already subscribed to {:?}", topic))
}
.instrument(self.span.clone())
.await
}
/// Stream that returns [`PubsubEvent`] for a given topic
pub async fn pubsub_events(
&self,
topic: &str,
) -> Result<BoxStream<'static, PubsubEvent>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::PubsubEventStream(tx))
.await?;
let mut receiver = rx
.await?;
let defined_topic = topic.to_string();
let stream = async_stream::stream! {
while let Some(event) = receiver.next().await {
match &event {
InnerPubsubEvent::Subscribe { topic, .. } | InnerPubsubEvent::Unsubscribe { topic, .. } if topic.eq(&defined_topic) => yield event.into(),
_ => {}
}
}
};
Ok(stream.boxed())
}
.instrument(self.span.clone())
.await
}
/// Publishes to the topic which may have been subscribed to earlier
pub async fn pubsub_publish(&self, topic: String, data: Vec<u8>) -> Result<MessageId, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::PubsubPublish(topic, data, tx))
.await?;
rx.await??.map_err(anyhow::Error::from)
}
.instrument(self.span.clone())
.await
}
/// Forcibly unsubscribes a previously made [`SubscriptionStream`], which could also be
/// unsubscribed by dropping the stream.
///
/// Returns true if unsubscription was successful
pub async fn pubsub_unsubscribe(&self, topic: &str) -> Result<bool, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::PubsubUnsubscribe(topic.into(), tx))
.await?;
rx.await??
}
.instrument(self.span.clone())
.await
}
/// Returns all known pubsub peers with the optional topic filter
pub async fn pubsub_peers(&self, topic: Option<String>) -> Result<Vec<PeerId>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::PubsubPeers(topic, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Returns all currently subscribed topics
pub async fn pubsub_subscribed(&self) -> Result<Vec<String>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::PubsubSubscribed(tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Returns the known wantlist for the local node when the `peer` is `None` or the wantlist of the given `peer`
pub async fn bitswap_wantlist(&self, peer: Option<PeerId>) -> Result<Vec<Cid>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::WantList(peer, tx))
.await?;
Ok(rx.await??.await)
}
.instrument(self.span.clone())
.await
}
/// Returns a list of local blocks
///
/// This implementation is subject to change into a stream, which might only include the pinned
/// blocks.
pub async fn refs_local(&self) -> Result<Vec<Cid>, Error> {
self.repo.list_blocks().instrument(self.span.clone()).await
}
/// Returns local listening addresses
pub async fn listening_addresses(&self) -> Result<Vec<Multiaddr>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task.clone().send(IpfsEvent::Listeners(tx)).await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Returns external addresses
pub async fn external_addresses(&self) -> Result<Vec<Multiaddr>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::ExternalAddresses(tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Add a given multiaddr as a listening address. Will fail if the address is unsupported, or
/// if it is already being listened on. Currently will invoke `Swarm::listen_on` internally,
/// keep the ListenerId for later `remove_listening_address` use in a HashMap.
///
/// The returned future will resolve on the first bound listening address when this is called
/// with `/ip4/0.0.0.0/...` or anything similar which will bound through multiple concrete
/// listening addresses.
///
/// Trying to add an unspecified listening address while any other listening address adding is
/// in progress will result in error.
///
/// Returns the bound multiaddress, which in the case of original containing an ephemeral port
/// has now been changed.
pub async fn add_listening_address(&self, addr: Multiaddr) -> Result<Multiaddr, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::AddListeningAddress(addr, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Stop listening on a previously added listening address. Fails if the address is not being
/// listened to.
///
/// The removal of all listening addresses added through unspecified addresses is not supported.
pub async fn remove_listening_address(&self, addr: Multiaddr) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::RemoveListeningAddress(addr, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Obtain the addresses associated with the given `PeerId`; they are first searched for locally
/// and the DHT is used as a fallback: a `Kademlia::get_closest_peers(peer_id)` query is run and
/// when it's finished, the newly added DHT records are checked for the existence of the desired
/// `peer_id` and if it's there, the list of its known addresses is returned.
pub async fn find_peer(&self, peer_id: PeerId) -> Result<Vec<Multiaddr>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::FindPeer(peer_id, false, tx))
.await?;
match rx.await?? {
Either::Left(addrs) if !addrs.is_empty() => Ok(addrs),
Either::Left(_) => unreachable!(),
Either::Right(future) => {
future.await??;
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::FindPeer(peer_id, true, tx))
.await?;
match rx.await?? {
Either::Left(addrs) if !addrs.is_empty() => Ok(addrs),
_ => Err(anyhow!("couldn't find peer {}", peer_id)),
}
}
}
}
.instrument(self.span.clone())
.await
}
/// Performs a DHT lookup for providers of a value to the given key.
///
/// Returns a list of peers found providing the Cid.
pub async fn get_providers(&self, cid: Cid) -> Result<BoxStream<'static, PeerId>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::GetProviders(cid, tx))
.await?;
rx.await??.ok_or_else(|| anyhow!("Provider already exist"))
}
.instrument(self.span.clone())
.await
}
/// Establishes the node as a provider of a block with the given Cid: it publishes a provider
/// record with the given key (Cid) and the node's PeerId to the peers closest to the key. The
/// publication of provider records is periodically repeated as per the interval specified in
/// `libp2p`'s `KademliaConfig`.
pub async fn provide(&self, cid: Cid) -> Result<(), Error> {
// don't provide things we don't actually have
if self.repo.get_block_now(&cid).await?.is_none() {
return Err(anyhow!(
"Error: block {} not found locally, cannot provide",
cid
));
}
let kad_result = async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::Provide(cid, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await?
.await;
match kad_result? {
Ok(KadResult::Complete) => Ok(()),
Ok(_) => unreachable!(),
Err(e) => Err(anyhow!(e)),
}
}
/// Returns a list of peers closest to the given `PeerId`, as suggested by the DHT. The
/// node must have at least one known peer in its routing table in order for the query
/// to return any values.
pub async fn get_closest_peers(&self, peer_id: PeerId) -> Result<Vec<PeerId>, Error> {
let kad_result = async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::GetClosestPeers(peer_id, tx))
.await?;
Ok(rx.await??).map_err(|e: String| anyhow!(e))
}
.instrument(self.span.clone())
.await?
.await;
match kad_result? {
Ok(KadResult::Peers(closest)) => Ok(closest),
Ok(_) => unreachable!(),
Err(e) => Err(anyhow!(e)),
}
}
/// Change the DHT mode
pub async fn dht_mode(&self, mode: DhtMode) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::DhtMode(mode, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Attempts to look a key up in the DHT and returns the values found in the records
/// containing that key.
pub async fn dht_get<T: AsRef<[u8]>>(
&self,
key: T,
) -> Result<BoxStream<'static, Record>, Error> {
async move {
let key = key.as_ref();
let key_str = String::from_utf8_lossy(key);
let key = if let Ok((prefix, _)) = split_dht_key(&key_str) {
if let Some(key_fn) = self.record_key_validator.get(prefix) {
key_fn(&key_str)?
} else {
Key::from(key.to_vec())
}
} else {
Key::from(key.to_vec())
};
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::DhtGet(key, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Stores the given key + value record locally and replicates it in the DHT. It doesn't
/// expire locally and is periodically replicated in the DHT, as per the `KademliaConfig`
/// setup.
pub async fn dht_put<T: AsRef<[u8]>>(
&self,
key: T,
value: Vec<u8>,
quorum: Quorum,
) -> Result<(), Error> {
let kad_result = async move {
let key = key.as_ref();
let key_str = String::from_utf8_lossy(key);
let key = if let Ok((prefix, _)) = split_dht_key(&key_str) {
if let Some(key_fn) = self.record_key_validator.get(prefix) {
key_fn(&key_str)?
} else {
Key::from(key.to_vec())
}
} else {
Key::from(key.to_vec())
};
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::DhtPut(key, value, quorum, tx))
.await?;
Ok(rx.await?).map_err(|e: String| anyhow!(e))
}
.instrument(self.span.clone())
.await??
.await;
match kad_result? {
Ok(KadResult::Complete) => Ok(()),
Ok(_) => unreachable!(),
Err(e) => Err(anyhow!(e)),
}
}
/// Add relay address
pub async fn add_relay(&self, peer_id: PeerId, addr: Multiaddr) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::AddRelay(peer_id, addr, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Remove relay address
pub async fn remove_relay(&self, peer_id: PeerId, addr: Multiaddr) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::RemoveRelay(peer_id, addr, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// List all relays. if `active` is true, it will list all active relays
pub async fn list_relays(&self, active: bool) -> Result<Vec<(PeerId, Vec<Multiaddr>)>, Error> {
async move {
let (tx, rx) = oneshot_channel();
match active {
true => {
self.to_task
.clone()
.send(IpfsEvent::ListActiveRelays(tx))
.await?
}
false => self.to_task.clone().send(IpfsEvent::ListRelays(tx)).await?,
};
rx.await?
}
.instrument(self.span.clone())
.await
}
pub async fn enable_autorelay(&self) -> Result<(), Error> {
Err(anyhow::anyhow!("Unimplemented"))
}
pub async fn disable_autorelay(&self) -> Result<(), Error> {
Err(anyhow::anyhow!("Unimplemented"))
}
/// Enable use of a relay. If `peer_id` is `None`, it will select a relay at random to use, if one have been added
pub async fn enable_relay(&self, peer_id: Option<PeerId>) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::EnableRelay(peer_id, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Disable the use of a selected relay.
pub async fn disable_relay(&self, peer_id: PeerId) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::DisableRelay(peer_id, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
pub async fn rendezvous_register_namespace<S: Into<String>>(
&self,
namespace: S,
ttl: Option<u64>,
peer_id: PeerId,
) -> Result<(), Error> {
async move {
let namespace = Namespace::new(namespace.into())?;
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::RegisterRendezvousNamespace(
namespace, peer_id, ttl, tx,
))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
pub async fn rendezvous_unregister_namespace<S: Into<String>>(
&self,
namespace: S,
peer_id: PeerId,
) -> Result<(), Error> {
async move {
let namespace = Namespace::new(namespace.into())?;
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::UnregisterRendezvousNamespace(
namespace, peer_id, tx,
))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
pub async fn rendezvous_namespace_discovery<S: Into<String>>(
&self,
namespace: S,
ttl: Option<u64>,
peer_id: PeerId,
) -> Result<HashMap<PeerId, Vec<Multiaddr>>, Error> {
async move {
let namespace = Namespace::new(namespace.into())?;
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::RendezvousNamespaceDiscovery(
Some(namespace),
false,
ttl,
peer_id,
tx,
))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Walk the given Iplds' links up to `max_depth` (or indefinitely for `None`). Will return
/// any duplicate trees unless `unique` is `true`.
///
/// More information and a `'static` lifetime version available at [`refs::iplds_refs`].
pub fn refs<'a, Iter>(
&'a self,
iplds: Iter,
max_depth: Option<u64>,
unique: bool,
) -> impl Stream<Item = Result<refs::Edge, libipld::error::Error>> + Send + 'a
where
Iter: IntoIterator<Item = (Cid, Ipld)> + Send + 'a,
{
refs::iplds_refs(self.repo(), iplds, max_depth, unique)
}
/// Obtain the list of addresses of bootstrapper nodes that are currently used.
pub async fn get_bootstraps(&self) -> Result<Vec<Multiaddr>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::GetBootstrappers(tx))
.await?;
Ok(rx.await?)
}
.instrument(self.span.clone())
.await
}
/// Extend the list of used bootstrapper nodes with an additional address.
/// Return value cannot be used to determine if the `addr` was a new bootstrapper, subject to
/// change.
pub async fn add_bootstrap(&self, addr: Multiaddr) -> Result<Multiaddr, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::AddBootstrapper(addr, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Remove an address from the currently used list of bootstrapper nodes.
/// Return value cannot be used to determine if the `addr` was an actual bootstrapper, subject to
/// change.
pub async fn remove_bootstrap(&self, addr: Multiaddr) -> Result<Multiaddr, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::RemoveBootstrapper(addr, tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Clear the currently used list of bootstrapper nodes, returning the removed addresses.
pub async fn clear_bootstrap(&self) -> Result<Vec<Multiaddr>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::ClearBootstrappers(tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Restore the originally configured bootstrapper node list by adding them to the list of the
/// currently used bootstrapper node address list; returns the restored addresses.
pub async fn default_bootstrap(&self) -> Result<Vec<Multiaddr>, Error> {
async move {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::DefaultBootstrap(tx))
.await?;
rx.await?
}
.instrument(self.span.clone())
.await
}
/// Bootstraps the local node to join the DHT: it looks up the node's own ID in the
/// DHT and introduces it to the other nodes in it; at least one other node must be
/// known in order for the process to succeed. Subsequently, additional queries are
/// ran with random keys so that the buckets farther from the closest neighbor also
/// get refreshed.
pub async fn bootstrap(&self) -> Result<JoinHandle<Result<KadResult, Error>>, Error> {
let (tx, rx) = oneshot_channel();
self.to_task.clone().send(IpfsEvent::Bootstrap(tx)).await?;
let fut = rx.await??;
let bootstrap_task =
tokio::spawn(async move { fut.await.map_err(|e| anyhow!(e)).and_then(|res| res) });
Ok(bootstrap_task)
}
/// Add address of a peer to the address book
pub async fn add_peer(&self, peer_id: PeerId, mut addr: Multiaddr) -> Result<(), Error> {
if matches!(addr.iter().last(), Some(Protocol::P2p(_))) {
addr.pop();
}
let (tx, rx) = oneshot::channel();
self.to_task
.clone()
.send(IpfsEvent::AddPeer(peer_id, addr, tx))
.await?;
rx.await??;
Ok(())
}
/// Remove peer from the address book
pub async fn remove_peer(&self, peer_id: PeerId) -> Result<bool, Error> {
let (tx, rx) = oneshot::channel();
self.to_task
.clone()
.send(IpfsEvent::RemovePeer(peer_id, None, tx))
.await?;
rx.await.map_err(anyhow::Error::from)?
}
/// Remove peer address from the address book
pub async fn remove_peer_address(
&self,
peer_id: PeerId,
addr: Multiaddr,
) -> Result<bool, Error> {
let (tx, rx) = oneshot::channel();
self.to_task
.clone()
.send(IpfsEvent::RemovePeer(peer_id, Some(addr), tx))
.await?;
rx.await.map_err(anyhow::Error::from)?
}
/// Returns the Bitswap peers for the a `Node`.
pub async fn get_bitswap_peers(&self) -> Result<Vec<PeerId>, Error> {
let (tx, rx) = oneshot_channel();
self.to_task
.clone()
.send(IpfsEvent::GetBitswapPeers(tx))
.await?;
Ok(rx.await??.await)
}
/// Returns the keypair to the node
pub fn keypair(&self) -> Result<&Keypair, Error> {
Ok(&self.key)
}
/// Returns the keystore
pub fn keystore(&self) -> &Keystore {
&self.keystore
}
/// Exit daemon.
pub async fn exit_daemon(mut self) {
// FIXME: this is a stopgap measure needed while repo is part of the struct Ipfs instead of
// the background task or stream. After that this could be handled by dropping.
self.repo.shutdown();
// ignoring the error because it'd mean that the background task had already been dropped
let _ = self.to_task.try_send(IpfsEvent::Exit);
}
}
#[inline]
pub(crate) fn split_dht_key(key: &str) -> anyhow::Result<(&str, &str)> {
anyhow::ensure!(!key.is_empty(), "Key cannot be empty");
let (key, val) = {
let data = key
.split('/')
.filter(|s| !s.trim().is_empty())
.collect::<Vec<_>>();
anyhow::ensure!(
!data.is_empty() && data.len() == 2,
"split dats cannot be empty"
);
(data[0], data[1])
};
Ok((key, val))
}
#[inline]
pub(crate) fn ipns_to_dht_key<B: AsRef<str>>(key: B) -> anyhow::Result<Key> {
use libipld::multibase;
let default_ipns_prefix = b"/ipns/";
let mut key = key.as_ref().trim().to_string();
anyhow::ensure!(!key.is_empty(), "Key cannot be empty");
if key.starts_with('1') || key.starts_with('Q') {
key.insert(0, 'z');
}
let mut data = multibase::decode(key).map(|(_, data)| data)?;
if data[0] != 0x01 && data[1] != 0x72 {
data = [vec![0x01, 0x72], data].concat();
}
data = [default_ipns_prefix.to_vec(), data[2..].to_vec()].concat();
Ok(data.into())
}
#[inline]
pub(crate) fn to_dht_key<B: AsRef<str>, F: Fn(&str) -> anyhow::Result<Key>>(
(prefix, func): (&str, F),
key: B,
) -> anyhow::Result<Key> {
let key = key.as_ref().trim();
let (key, val) = split_dht_key(key)?;
anyhow::ensure!(!key.is_empty(), "Key cannot be empty");
anyhow::ensure!(!val.is_empty(), "Value cannot be empty");
if key == prefix {
return func(val);
}
anyhow::bail!("Invalid prefix")
}
#[allow(dead_code)]
pub(crate) fn peerid_from_multiaddr(addr: &Multiaddr) -> anyhow::Result<PeerId> {
let mut addr = addr.clone();
let peer_id = match addr.pop() {
Some(Protocol::P2p(peer_id)) => peer_id,
_ => anyhow::bail!("Invalid PeerId"),
};
Ok(peer_id)
}
use crate::p2p::AddressBookConfig;
#[doc(hidden)]
pub use node::Node;
/// Node module provides an easy to use interface used in `tests/`.
mod node {
use futures::TryFutureExt;
use super::*;
/// Node encapsulates everything to setup a testing instance so that multi-node tests become
/// easier.
pub struct Node {
/// The Ipfs facade.
pub ipfs: Ipfs,
/// The peer identifier on the network.
pub id: PeerId,
/// The listened to and externally visible addresses. The addresses are suffixed with the
/// P2p protocol containing the node's PeerID.
pub addrs: Vec<Multiaddr>,
}
impl Node {
/// Initialises a new `Node` with an in-memory store backed configuration.
///
/// This will use the testing defaults for the `IpfsOptions`. If `IpfsOptions` has been
/// initialised manually, use `Node::with_options` instead.
pub async fn new<T: AsRef<str>>(name: T) -> Self {
Self::with_options(Some(trace_span!("ipfs", node = name.as_ref())), None).await
}
/// Connects to a peer at the given address.
pub async fn connect<D: Into<DialOpts>>(&self, opt: D) -> Result<(), Error> {
let opts = opt.into();
if let Some(peer_id) = opts.get_peer_id() {
if self.ipfs.is_connected(peer_id).await? {
return Ok(());
}
}
self.ipfs.connect(opts).await
}
/// Returns a new `Node` based on `IpfsOptions`.
pub async fn with_options(span: Option<Span>, addr: Option<Vec<Multiaddr>>) -> Self {
// for future: assume UninitializedIpfs handles instrumenting any futures with the
// given span
let mut uninit = UninitializedIpfsNoop::new().with_default();
if let Some(span) = span {
uninit = uninit.set_span(span);
}
let list = match addr {
Some(addr) => addr,
None => vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
};
let ipfs = uninit.start().await.unwrap();
let id = ipfs.keypair().map(|kp| kp.public().to_peer_id()).unwrap();
for addr in list {
ipfs.add_listening_address(addr).await.expect("To succeed");
}
let mut addrs = ipfs.listening_addresses().await.unwrap();
for addr in &mut addrs {
if let Some(proto) = addr.iter().last() {
if !matches!(proto, Protocol::P2p(_)) {
addr.push(Protocol::P2p(id));
}
}
}
Node { ipfs, id, addrs }
}
/// Returns the subscriptions for a `Node`.
#[allow(clippy::type_complexity)]
pub fn get_subscriptions(
&self,
) -> &parking_lot::Mutex<HashMap<Cid, Vec<oneshot::Sender<Result<Block, String>>>>>
{
&self.ipfs.repo.subscriptions
}
/// Bootstraps the local node to join the DHT: it looks up the node's own ID in the
/// DHT and introduces it to the other nodes in it; at least one other node must be
/// known in order for the process to succeed. Subsequently, additional queries are
/// ran with random keys so that the buckets farther from the closest neighbor also
/// get refreshed.
pub async fn bootstrap(&self) -> Result<KadResult, Error> {
self.ipfs
.bootstrap()
.and_then(|fut| async { fut.await.map_err(anyhow::Error::from) })
.await?
}
pub async fn add_node(&self, node: &Self) -> Result<(), Error> {
for addr in &node.addrs {
self.add_peer(node.id, addr.to_owned()).await?;
}
Ok(())
}
/// Shuts down the `Node`.
pub async fn shutdown(self) {
self.ipfs.exit_daemon().await;
}
}
impl Deref for Node {
type Target = Ipfs;
fn deref(&self) -> &Self::Target {
&self.ipfs
}
}
impl DerefMut for Node {
fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
&mut self.ipfs
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use libipld::{
ipld,
multihash::{Code, MultihashDigest},
IpldCodec,
};
#[tokio::test]
async fn test_put_and_get_block() {
let ipfs = Node::new("test_node").await;
let data = b"hello block\n".to_vec();
let cid = Cid::new_v1(IpldCodec::Raw.into(), Code::Sha2_256.digest(&data));
let block = Block::new(cid, data).unwrap();
let cid: Cid = ipfs.put_block(block.clone()).await.unwrap();
let new_block = ipfs.get_block(&cid).await.unwrap();
assert_eq!(block, new_block);
}
#[tokio::test]
async fn test_put_and_get_dag() {
let ipfs = Node::new("test_node").await;
let data = ipld!([-1, -2, -3]);
let cid = ipfs.put_dag(data.clone()).await.unwrap();
let new_data = ipfs.get_dag(cid).await.unwrap();
assert_eq!(data, new_data);
}
#[tokio::test]
async fn test_pin_and_unpin() {
let ipfs = Node::new("test_node").await;
let data = ipld!([-1, -2, -3]);
let cid = ipfs.put_dag(data.clone()).pin(false).await.unwrap();
assert!(ipfs.is_pinned(&cid).await.unwrap());
ipfs.remove_pin(&cid, false).await.unwrap();
assert!(!ipfs.is_pinned(&cid).await.unwrap());
}
}