【p2p、分布式,区块链笔记 Torrent】: WebTorrent & GitTorrent & bittorrent-dht

tanner2019安装教程 tanner2019安装教程 tanner安装包 软件由老师提供,但由于老师给的方法不知怎么回事无法使用就自己找了一些方法,本教程仅提供参考 链接:https://pan.baidu.com/s/1A2O6JuEF8YFbe8bPnD3J6w 提取码:ysif 复制这段内容后打开百度网盘手机App,操作更方便哦 安装过程 1.选择TannerTools_v2019.2-Win64.exe,进行安装。 2.选择安装产品 3.选择目标路径 4.确认安装 5.安装成功,退出。 获取许可过程 1.打开key压缩 阅读详情

bittorrent-dht模块

1. 导入依赖

var DHT = require('bittorrent-dht')

2. 创建实例

var dht = new DHT({
  bootstrap: config.dht.bootstrap
})
dht.listen(config.dht.listen)
  • new DHT({...}):创建一个新的 DHT 实例,bootstrap 参数用于指定引导节点的地址,允许客户端加入 DHT 网络。

  • dht.listen(...):在指定的端口上开始监听来自其他 DHT 节点的请求。

  • 处理 DHT 准备就绪事件

dht.on('ready', function () {
  // 处理
})
  • 示例代码
var DHT = require("bittorrent-dht");
var dht = new DHT();

var dht = new DHT();
var value = new Buffer(200).fill("abc");

dht.on("ready", function () {
  dht.put({ v: value }, function (errors, hash) {
    console.error("errors=", errors);
    console.log("hash=", hash);
  });
});

setTimeout(function () {
  var arr = dht.toArray(); // 获取DHT节点数组
  console.log("DHT节点数组:", arr); // 打印数组到控制台
  dht.destroy();
}, 100000);
  • 输出:
➜  workspace git:(master) ✗ node index.js
(node:17667) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
errors= [
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '95.72.172.196:6881'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '5.49.12.204:40333'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '95.29.240.208:6881'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '109.175.109.86:8893'
  },
  Error: 202 Server Error
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '146.19.24.245:23544'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '81.101.23.157:56418'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '159.146.86.15:42164'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '195.8.44.196:20515'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '5.79.173.86:26358'
  },
  Error: 202 Server Error
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '13.58.27.33:6881'
  },
  Error: 202 Server Error
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '18.223.137.220:6881'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '103.15.254.90:18384'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '189.147.128.165:6898'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '49.187.241.178:6881'
  },
  Error: 203 missing 'token' key
      at DHT._onResponseOrError (/project/workspace/node_modules/bittorrent-dht/client.js:1077:11)
      at DHT._onData (/project/workspace/node_modules/bittorrent-dht/client.js:1037:10)
      at Socket.emit (node:events:518:28)
      at UDP.onMessage [as onmessage] (node:dgram:942:8) {
    address: '195.211.192.65:43468'
  },
  Error: query timed out
      at Timeout.onTimeout [as _onTimeout] (/project/workspace/node_modules/bittorrent-dht/client.js:1445:8)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    address: '146.59.3.81:10240'
  },
  Error: query timed out
      at Timeout.onTimeout [as _onTimeout] (/project/workspace/node_modules/bittorrent-dht/client.js:1445:8)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    address: '195.8.44.196:41850'
  },
  Error: query timed out
      at Timeout.onTimeout [as _onTimeout] (/project/workspace/node_modules/bittorrent-dht/client.js:1445:8)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    address: '90.154.72.124:16183'
  },
  Error: query timed out
      at Timeout.onTimeout [as _onTimeout] (/project/workspace/node_modules/bittorrent-dht/client.js:1445:8)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    address: '193.233.120.141:16199'
  }
]
hash= <Buffer ba 16 0d 84 85 59 c6 05 52 b7 83 f2 3c d3 55 5b 2a 32 c7 b8>
DHT节点数组: [
  {
    id: '6b5611b3bfb7b8c8372c069a1e22e540609bda5a',
    addr: '87.98.162.88:6881'
  },
  {
    id: 'a811480f80e15cc2da5de2efb637b55db52124a1',
    addr: '178.32.217.211:6881'
  },
  {
    id: 'af15df9751f12d69492050218fbad8ec1245bce3',
    addr: '37.48.111.3:28010'
  },
  {
    id: '89f562f9f211a457acfe33b20f629ccac543ab72',
    addr: '51.38.34.56:51413'
  },
  {
    id: 'a995297613a597c36b910494795510f888a39263',
    addr: '89.149.202.214:21147'
  },
  {
    id: '9b334e8f333ef9fa3f2a52934ec1fd771535e68e',
    addr: '178.162.174.223:28009'
  },
  {
    id: 'a555d8b451608de54ccad3db9a7349ec5096e407',
    addr: '178.162.173.132:28015'
  },
  {
    id: 'a5d1090886585bfbec069777aed63730feb71483',
    addr: '188.34.190.92:6881'
  },
  {
    id: '85f1caa50b72c07a86a9501968002b7d59501a95',
    addr: '46.242.12.117:13066'
  },
  {
    id: '997e40df66b6fb64577807c6c30802851843520b',
    addr: '178.162.174.237:28001'
  },
  {
    id: 'ae5a71d29d9b373b84ff5d397db31135caea6af8',
    addr: '146.71.50.196:4000'
  },
  {
    id: 'a3a090845890ffa9883b2f65e4cdd319c536b0af',
    addr: '47.54.23.223:51413'
  },
  {
    id: '9aa0c85e94e179e4692e3d86e707d5899062bed8',
    addr: '178.162.173.166:28015'
  },
  {
    id: '9f53fe555940af320f9c7540445589cc84369884',
    addr: '23.158.56.119:10095'
  },
  {
    id: '84fe47cc3c76ba793bc0f7f1ae6996431161ba60',
    addr: '88.201.206.119:3334'
  },
  {
    id: 'a3573c0b6206eaa41f2ce9d29dc9de5fef2865d1',
    addr: '81.207.238.6:1024'
  },
  {
    id: '9191c3ea0cf55e663d18fc552df2a580395f7a80',
    addr: '82.65.220.127:6885'
  },
  {
    id: 'b2498f2a643a78207bcb3dce4307f8e0a615297e',
    addr: '24.20.153.108:6969'
  },
  {
    id: 'b5a304f2843ba084416908fe7e7002fe9c5478ad',
    addr: '14.48.222.76:7801'
  },
  {
    id: 'a449d2b1d34c39c9ee4d917b24ce22af0db22248',
    addr: '5.129.239.125:49001'
  },
  {
    id: '9900258a640be2f0cd0c35a961a386c1cc9cf589',
    addr: '103.140.3.17:15195'
  },
  {
    id: 'e3d5f4b4aed69802dbb34b24f6fd358063dd1cf4',
    addr: '14.104.200.224:10920'
  },
  {
    id: 'e0f0da00d1bee565e0a21088787bdcbb4a2633e7',
    addr: '125.77.176.152:37740'
  },
  {
    id: 'e0dd29c1e17a46839ab2e54c32675e5463f9e03b',
    addr: '113.0.38.69:32981'
  },
  {
    id: 'e36ff0fa563342fd2fc905576a784e4277b9ee59',
    addr: '124.115.94.28:64425'
  },
  {
    id: 'e13b76f9a31ebe5a6d29b765b293143d461ca426',
    addr: '122.192.133.176:6887'
  },
  {
    id: 'e29a50d52c1c9029a9d3a7f9fe152e5803ae3344',
    addr: '36.4.10.196:38999'
  },
  {
    id: 'e5c14f6d2ee91906c9265afaab45482594f7467e',
    addr: '221.157.193.11:6881'
  },
  {
    id: 'e46507c044a8265a16360dc481a3c631dc79aff1',
    addr: '61.147.218.204:56016'
  },
  {
    id: 'e66a2f0d5056c00ba4425bab7ca0cfab4df7ddcf',
    addr: '120.40.56.40:22840'
  },
  {
    id: 'e632ccac3c4bc229581ea4d2039626cb12881e1c',
    addr: '218.91.170.101:6891'
  },
  {
    id: 'e6b14eb0462c8ac5e7553ec6b6003a65075c363e',
    addr: '39.112.211.158:40954'
  },
  {
    id: 'e71f1918956034be0a634b9cdbbec5665d2ac654',
    addr: '183.134.38.41:6890'
  },
  {
    id: 'e73c15b0beffd4373c145e00f07d4a450408dd52',
    addr: '61.173.61.203:59951'
  },
  {
    id: 'e711e8dda372363d608afa0a050d3fbdec8425db',
    addr: '185.149.91.43:51079'
  },
  {
    id: 'e71361d74d4799186ac0d62bccecbfe66c63b2cd',
    addr: '169.150.223.225:6881'
  },
  {
    id: 'e706b08a6b939e8d5e37ea2e4f9418c97cc1d66d',
    addr: '94.75.194.118:28006'
  },
  {
    id: 'e703e8c3a3c766f1ed6a365998f05ba84d81dd83',
    addr: '77.82.189.152:49001'
  },
  {
    id: 'e72eb05590c079a0acb407fdbb5204bc9b95d53b',
    addr: '49.245.2.242:11706'
  },
  {
    id: 'e725c22b82a5adebf493bd66ac859562b4f2b3ee',
    addr: '14.103.76.31:60020'
  },
  {
    id: 'e71f2ba6bec45aff6d35b3356e1ed3df4d5f15bb',
    addr: '121.236.15.238:9888'
  },
  {
    id: 'e751aa9bd98fbfc9b3aaa026443ebd8c429f5fcc',
    addr: '195.154.171.138:5250'
  },
  {
    id: 'e74e28a755e0b680d4197753c31e9ad081d8b8b4',
    addr: '184.64.199.181:48670'
  },
  {
    id: 'e74ca9d20211352647212795b0c4e761c067d874',
    addr: '220.200.42.0:62559'
  },
  {
    id: 'e74bbc1b073c79c49051b88c502986c8c21dd36f',
    addr: '81.40.181.46:8621'
  },
  {
    id: 'e7507c39a1cb087aa7ccc8f3adc3d04203d37f31',
    addr: '185.203.56.10:60696'
  },
  {
    id: 'e743d8ec35a30db1bb12137f67eae6e39b013508',
    addr: '183.195.21.211:13284'
  },
  {
    id: 'e74f8c5f47304ac841de681ec4b4cfbc134cbd7f',
    addr: '112.24.228.151:27868'
  },
  {
    id: 'e7503d0d29de824e4caae6017a4e6419d1a43082',
    addr: '81.202.75.215:6889'
  },
  {
    id: 'e753d24bdf60c8128dd48d15431093c20c34693d',
    addr: '209.193.67.78:51413'
  },
  {
    id: 'e755e97111a41814b466e4b7a5526a06f2cc534c',
    addr: '186.57.159.109:6881'
  },
  {
    id: 'e761c3d8d84b09395c0a2affd38e24c0c9418b4c',
    addr: '42.193.175.166:18826'
  },
  {
    id: 'e760ac2c0424839b85bf02179b32866ab297689b',
    addr: '88.0.223.106:51413'
  },
  {
    id: 'e767d67550eb627914a8d20b78728c9d59d95fc5',
    addr: '195.230.109.6:43242'
  },
  {
    id: 'e760a6ddaf17ee5725d15c21d9ed8186085ff04c',
    addr: '59.5.76.86:41202'
  },
  {
    id: 'e76579760dc1ad7e3d992d2bef992d6ea156bd89',
    addr: '131.153.18.45:22574'
  },
  {
    id: 'e762cffedfef8b697f64e88452b548e4ca052d0f',
    addr: '217.225.89.57:6881'
  },
  {
    id: 'e763d907a08cf6fcb07eecc265d595632377624c',
    addr: '107.181.234.142:51413'
  },
  {
    id: 'e766eb1feb72f2cb87aa747086e3faef8764f06e',
    addr: '219.251.137.25:40972'
  },
  {
    id: 'e760b549f1f1bbe9ebb3a6db3c870c3e99245e52',
    addr: '59.6.5.153:64536'
  },
  {
    id: 'e760afea3b0f54c53a75070241592c5dc6fd0c59',
    addr: '27.125.248.23:15630'
  },
  {
    id: 'e767644b57a9dbe2029ead2d395633301bfff07e',
    addr: '59.3.119.55:40804'
  },
  {
    id: 'e763054ba30fa0e3e767721c6a911ce9e7ac50fe',
    addr: '60.163.61.30:17077'
  },
  {
    id: 'e76595f27f020daa5d106a3d4e25bd24b1564b6a',
    addr: '179.131.236.32:50321'
  },
  {
    id: 'e7672ee9801a6f85413c6314f6305f7b94f93e37',
    addr: '121.128.111.23:40811'
  },
  {
    id: 'e760245fb585019447ba47e497a94d662e6349c8',
    addr: '59.188.57.119:6889'
  },
  {
    id: 'e7620cc83f2240a3b3d69ef294eece88c729f902',
    addr: '72.21.17.91:61318'
  },
  {
    id: 'e761f830ff2f48422470dabafe53ce7d08fb3cd2',
    addr: '85.140.162.247:55414'
  },
  {
    id: 'e7672049fe578e109d980c8eb64057cdb3454c02',
    addr: '207.65.155.118:51413'
  },
  {
    id: 'e76793864960d52d457ef2ebe2fcb40d02f57ca9',
    addr: '157.157.193.103:5030'
  },
  {
    id: 'e769bdb7be648414ae3cafb158fb4c18fa94826a',
    addr: '72.21.17.3:12743'
  },
  {
    id: 'e76a5736ae4740254fc6c57c04afe7bcd093c5fc',
    addr: '222.113.206.3:32996'
  },
  {
    id: 'e769bd26374a11ebcd96ac0021ea2bbe5aec00f2',
    addr: '72.21.17.3:57563'
  },
  {
    id: 'e76b41a618e160c3a5c837d5ecc70648305e58a4',
    addr: '2.98.139.134:19856'
  },
  {
    id: 'e768b641d52f2dfe3bf304d3ee8a8ce4dded3bf8',
    addr: '119.197.185.173:60761'
  },
  {
    id: 'e76bb57181f76165df4289688ea4e6730f3cf7ec',
    addr: '188.21.206.210:29115'
  },
  {
    id: 'e76b7ad6ae529049f1f1bbe9ebb3a6db3c870ce1',
    addr: '84.9.37.4:35609'
  },
  {
    id: 'e76bc6b2e88753a773e28c5c73ce4dd13178d9c6',
    addr: '111.196.228.198:5774'
  },
  {
    id: 'e769bf4d3366684d43e979222cd64fdb976a0142',
    addr: '72.21.17.3:62436'
  },
  {
    id: 'e7683854beae9b503af99d95f65dc8626d274b94',
    addr: '178.162.174.237:28007'
  },
  {
    id: 'e7686b42c80418f1dbaac7420de4e7b865eed412',
    addr: '89.105.15.4:16345'
  },
  {
    id: 'e769beafcbe43b71166602d4f70a330402c7b07a',
    addr: '72.21.17.3:20057'
  },
  {
    id: 'e7680d81a626489edee91255d9a08264a78cb8f7',
    addr: '170.150.1.175:26340'
  },
  {
    id: 'e76c07b8216f99cb4eee48795bc504be5e85ad92',
    addr: '178.221.133.198:46873'
  },
  {
    id: 'e76c04cffe2e06e0aa57a0edec0062b8f42c5803',
    addr: '49.117.14.143:8392'
  },
  {
    id: 'e76c0d1dc74193c186b35361733df3bb591d16ef',
    addr: '46.197.49.135:26616'
  },
  {
    id: 'e76c00c84cc41bd997f5c942055eed780a74189e',
    addr: '185.251.151.248:51413'
  },
  {
    id: 'e76c047b5dc0aaad6529b9e9487159c2c74fed01',
    addr: '223.247.106.145:55291'
  },
  {
    id: 'e76c00169995fb98f8c90f59817b3c989b5dfb3d',
    addr: '82.132.246.194:54677'
  },
  {
    id: 'e76c074d30740c88fcdac1f93ba8faa46fa572a5',
    addr: '122.148.246.194:30754'
  },
  {
    id: 'e76c0f976890ad222b72782990615e613e03d7e7',
    addr: '212.35.183.80:3168'
  },
  {
    id: 'e76c06ba160fdc583ed1969d1db70fae30a78541',
    addr: '90.15.84.33:40622'
  },
  {
    id: 'e76c06ba160fdc583ed1969d1db70f8dc9547307',
    addr: '49.12.86.202:6883'
  },
  {
    id: 'e76c06ba160fdc583ed152fbdd8d2da45f042476',
    addr: '146.19.24.245:23544'
  },
  {
    id: 'e76c0993a20cef635b7fad5e4dc95a8bcae8e107',
    addr: '132.147.119.80:1041'
  },
  {
    id: 'e76c0df644d2255ca4a4c8c8ee3029ecb2fd2d1b',
    addr: '178.234.59.239:6881'
  },
  {
    id: 'e76c1edc0b17287dbee23dbdc40875faab321fff',
    addr: '93.156.208.226:48379'
  },
  {
    id: 'e76c1fd6f2a275af36e9955fbcce091188938046',
    addr: '200.121.141.155:31719'
  },
  {
    id: 'e76c108b98fc86ac2e0d75ed5ff5c03e163dd9eb',
    addr: '82.7.191.13:36957'
  },
  {
    id: 'e76c1bde86a78072e5aafa52b4d87b270f1d571e',
    addr: '70.175.203.76:6881'
  },
  ... 100 more items
]

3. 发布announce

dht.announce(sha, config.dht.announce, function (err) {
  if (err !== null) {
    console.log('Announced ' + sha)
  }
})
  • dht.announce(sha, ...):将特定的 SHA 值(GitTorrent项目即 Git 提交的哈希)及其相应的网络地址发布到 DHT 网络。其他节点可以通过这个信息来查找特定的 Git 数据。通过这个函数,节点通知 DHT 网络,它正在下载一个特定的种子。这使得其他节点能够发现这个下载中的节点,从而进行数据交换。具体实现如下:
/**
 * Announce that the peer, controlling the querying node, is downloading a torrent on a port.
 * 声明当前节点正在下载一个种子,告诉DHT网络此信息。
 * 
 * @param {string|Buffer} infoHash - 种子的 infoHash,唯一标识符。
 * @param {number} port - 当前节点下载种子的端口号。
 * @param {function=} cb - 可选的回调函数,宣布完成时调用。
 */
DHT.prototype.announce = function (infoHash, port, cb) {
  var self = this
  
  // 如果回调函数没有提供,设置一个空函数
  if (!cb) cb = function () {}

  // 如果DHT实例已经销毁,调用回调函数并传递错误信息
  if (self.destroyed) return cb(new Error('dht is destroyed'))

  // 将infoHash转换为Buffer格式
  infoHash = idToBuffer(infoHash)
  
  // 将infoHash转换为十六进制字符串,便于处理和输出
  var infoHashHex = idToHexString(infoHash)

  // 输出调试信息,表示正在宣布下载
  self._debug('announce %s %s', infoHashHex, port)

  // 遍历本地节点地址,为每个地址添加peer信息到DHT表中
  self.localAddresses.forEach(function (address) {
    self._addPeer(address + ':' + port, infoHashHex)
  })

  // 查找是否已经有对应 infoHash 的路由表存在
  var table = self.tables[infoHashHex]
  if (table) {
    // 如果有对应的路由表,直接使用表中的最近节点进行处理
    onClosest(null, table.closest({ id: infoHash }, K))
  } else {
    // 否则,发起一个lookup查询,查找最近的节点
    self.lookup(infoHash, onClosest)
  }

  /**
   * 当查找到最近的节点时调用的回调函数
   * @param {Error|null} err - 错误信息,如果存在错误
   * @param {Array} closest - 查找到的最近节点列表
   */
  function onClosest (err, closest) {
    if (err) return cb(err) // 如果有错误,调用回调并返回错误信息

    // 遍历每个最近的节点,向他们发送 "announce_peer" 消息
    closest.forEach(function (contact) {
      self._sendAnnouncePeer(contact.addr, infoHash, port, contact.token)
    })

    // 输出调试信息,表示宣布操作结束
    self._debug('announce end %s %s', infoHashHex, port)

    // 调用回调函数,表示宣布完成
    cb(null)
  }
}
  • 路由表的重用:
    • 如果 dht.announce 在调用 dht.lookup 之后的 5 分钟内被调用,并且路由表仍然有效,则可以重用最近的路由表。这意味着函数会更快地完成,因为不需要再次查询其他节点。
    • 如果调用时没有缓存的路由表,dht.announce 会首先执行 dht.lookup 来发现相关节点。这将耗费更多时间,因为需要重新获取有效的 “tokens”。下面是一个官方提供的路由表示例:
const dht1 = new DHT()

// some time passes ...

// destroy the dht
const nodes = dht1.toJSON().nodes
dht1.destroy()

// some time passes ...

// initialize a new dht with the same routing table as the first
const dht2 = new DHT()

nodes.forEach(function (node) {
  dht2.addNode(node)
})

4. 增dht.put(opts, callback)

import DHT from 'bittorrent-dht'
const dht = new DHT()
const value = Buffer.alloc(200).fill('abc')// 可放入一个小于 1000 字节的值

dht.put({ v: value }, function (err, hash) {
  console.error('error=', err)
  console.log('hash=', hash)
})
/**
 * Write arbitrary mutable and immutable data to the DHT.
 * Specified in BEP44: http://bittorrent.org/beps/bep_0044.html
 * @param {Object} opts
 * @param {function=} cb
 */
DHT.prototype.put = function (opts, cb) {
  var self = this
  var isMutable = opts.k || opts.sig
  if (opts.v === undefined) {
    throw new Error('opts.v not given')
  }
  if (opts.v.length >= 1000) {
    throw new Error('v must be less than 1000 bytes in put()')
  }

  if (isMutable && opts.cas && typeof opts.cas !== 'number') {
    throw new Error('opts.cas must be an integer if provided')
  }
  if (isMutable && !opts.k) {
    throw new Error('opts.k ed25519 public key required for mutable put')
  }
  if (isMutable && opts.k.length !== 32) {
    throw new Error('opts.k ed25519 public key must be 32 bytes')
  }
  if (isMutable && !opts.sig) {
    throw new Error('opts.sig signature required for mutable put')
  }
  if (isMutable && opts.sig.length !== 64) {
    throw new Error('opts.sig signature must be 64 bytes')
  }
  if (isMutable && opts.salt && opts.salt.length > 64) {
    throw new Error('opts.salt is > 64 bytes long')
  }
  if (isMutable && opts.seq === undefined) {
    throw new Error('opts.seq not provided for a mutable update')
  }
  if (isMutable && typeof opts.seq !== 'number') {
    throw new Error('opts.seq not an integer')
  }
  return self._put(opts, cb)
}

/**
 * put() without type checks for internal use
 * @param {Object} opts
 * @param {function=} cb
 */
DHT.prototype._put = function (opts, cb) {
  var self = this
  var pending = 0
  var errors = []
  var isMutable = opts.k || opts.sig
  var hash = isMutable
    ? sha1(opts.salt ? Buffer.concat([ opts.salt, opts.k ]) : opts.k)
    : sha1(opts.v)

  if (self.nodes.toArray().length === 0) {
    process.nextTick(function () {
      addLocal(null, [])
    })
  } else {
    self.lookup(hash, onLookup)
  }

  function onLookup (err, nodes) {
    if (err) return cb(err)
    nodes.forEach(function (node) {
      put(node)
    })
    addLocal()
  }

  function addLocal () {
    var localData = {
      id: self.nodeId,
      v: opts.v
    }
    var localAddr = '127.0.0.1:' + self._port
    if (isMutable) {
      if (opts.cas) localData.cas = opts.cas
      localData.sig = opts.sig
      localData.k = opts.k
      localData.seq = opts.seq
      localData.token = opts.token || self._generateToken(localAddr)
    }
    self.nodes.add({
      id: hash,
      addr: localAddr,
      data: localData
    })
    if (pending === 0) {
      process.nextTick(function () { cb(errors, hash) })
    }
  }
  return hash

  function put (node) {
    if (node.data) return // skip data nodes
    pending += 1
    var t = self._getTransactionId(node.addr, next(node))
    var data = {
      a: {
        id: opts.id || self.nodeId,
        v: opts.v
      },
      t: transactionIdToBuffer(t),
      y: 'q',
      q: 'put'
    }
    if (isMutable) {
      data.a.token = opts.token || self._generateToken(node.addr)
      data.a.seq = Math.round(opts.seq)
      data.a.sig = opts.sig
      data.a.k = opts.k
      if (opts.salt) data.a.salt = opts.salt
      if (opts.cas) data.a.cas = Math.round(opts.cas)
    }
    self._send(node.addr, data)
  }

  function next (node) {
    return function (err) {
      if (err) {
        err.address = node.addr
        errors.push(err)
      }
      if (--pending === 0) cb(errors, hash)
    }
  }
}

5. 查dht.get(hash, opts, callback)

    var val = new Buffer(key, 'hex')
    dht.get(val, function (err, res) {
      if (err) {
        return console.error(err)
      }
      var json = res.v.toString()
      var repos = JSON.parse(json)
    }

GitTorrent的gittorrentd命令实现

  • gittorrentd通过 BitTorrent DHT 和 GitTorrent 协议,将 Git 仓库数据通过 P2P 网络共享。
  • 通过遍历本地的 Git 仓库目录,列出所有引用,并将其 SHA 通过 DHT 网络广播。
  • 使用 net 模块创建 TCP 服务器,响应来自 P2P 节点的 Git 数据请求,并通过 WebTorrent 实现种子文件的共享。

创建 DHT 实例

#!/usr/bin/env node  // 文件开头的这行叫做“shebang”,它告诉操作系统使用node来运行这个脚本。因此,当你在命令行中运行./gittorrentd时,它实际上是在用Node.js环境执行该脚本。

// 引入所需的模块
var DHT = require('bittorrent-dht'); // BitTorrent 的 DHT 模块,用于发现对等节点
var EC = require('elliptic').ec; // 椭圆曲线加密库,支持多种加密算法
var ed25519 = new EC('ed25519'); // 使用 Ed25519 椭圆曲线进行加密操作
var exec = require('child_process').exec; // 允许执行 shell 命令
var glob = require('glob'); // 用于文件路径匹配
var fs = require('fs'); // 文件系统模块
var hat = require('hat'); // 随机 ID 生成库
var net = require('net'); // 提供网络套接字操作
var Protocol = require('bittorrent-protocol'); // BitTorrent 协议模块
var spawn = require('child_process').spawn; // 生成子进程以运行外部命令
var ut_gittorrent = require('ut_gittorrent'); // 用于 BitTorrent 上的 Git 传输扩展
var ut_metadata = require('ut_metadata'); // BitTorrent 元数据传输扩展
var WebTorrent = require('webtorrent'); // WebTorrent 客户端库,用于 BitTorrent 下载和种子
var zeroFill = require('zero-fill'); // 补零函数,格式化数字
var config = require('./config'); // 加载用户自定义配置
var git = require('./git'); // 加载 Git 操作相关的自定义模块

// 生成 BitTorrent 客户端的版本号字符串(用于 Peer ID),根据 package.json 的版本生成
var VERSION = require('./package.json').version
  .match(/([0-9]+)/g).slice(0, 2).map(zeroFill(2)).join('');

// 错误处理函数,打印错误并退出
function die(error) {
  console.error(error);
  process.exit(1);
}

// 创建 DHT 实例,使用配置文件中定义的引导节点
var dht = new DHT({
  bootstrap: config.dht.bootstrap
});
// DHT 监听来自 Peers 的请求
dht.listen(config.dht.listen);

var announcedRefs = {}; // 存储已经公布的 Git 引用 (refs)
var userProfile = {
  repositories: {} // 存储用户的 Git 仓库信息
};

// 生成或读取密钥文件
var key = create_or_read_keyfile();

function create_or_read_keyfile() {
  // 如果密钥文件不存在,创建一个新的 Ed25519 密钥对
  if (!fs.existsSync(config.key)) {
    var keypair = new EC('ed25519').genKeyPair();
    fs.writeFileSync(config.key, JSON.stringify({
      pub: keypair.getPublic('hex'), // 公钥
      priv: keypair.getPrivate('hex') // 私钥
    }));
  }

  // 读取已存在的密钥文件
  var key = JSON.parse(fs.readFileSync(config.key).toString());
  return ed25519.keyPair({
    priv: key.priv, // 私钥
    privEnc: 'hex',
    pub: key.pub, // 公钥
    pubEnc: 'hex'
  });
}

// 补零函数,确保缓冲区长度达到指定的字节数
function bpad(n, buf) {
  if (buf.length === n) return buf;
  if (buf.length < n) {
    var b = new Buffer(n);
    buf.copy(b, n - buf.length);
    for (var i = 0; i < n - buf.length; i++) b[i] = 0;
    return b;
  }
}

当 DHT 就绪时,执行以下操作


var head = ''; // 当前仓库的 HEAD 引用

// 当 DHT 就绪时,执行以下操作
dht.on('ready', function () {
  // 查找文件目录
  // 使用 glob 搜索所有带有 git-daemon-export-ok 文件的目录(Git 可公开的仓库)
  //,比如/projectA/git-daemon-export-ok和/projectB/.git/git-daemon-export-ok
  var repos = glob.sync('*/{,.git/}git-daemon-export-ok', { strict: false });
  var count = repos.length;

  repos.forEach(function (repo) {
    console.log('in repo ' + repo);
    repo = repo.replace(/git-daemon-export-ok$/, ''); // 文件名中移除 git-daemon-export-ok 
    console.log(repo);
    var reponame = repo.replace(/\/.git\/$/, ''); // 提取仓库名或路径部分
    
    userProfile.repositories[reponame] = {}; // 初始化用户的仓库信息

    // 使用自定义 Git 模块列出该仓库的所有引用 (refs)
    // 第二个参数是一个回调函数,当 git ls-remote 获取到每一个引用时,都会调用该回调函数,并传递该引用的 sha(40 位提交哈希)和 ref(引用名,通常是分支或标签的全名)
    var ls = git.ls(repo, function (sha, ref) {
      // 只处理 HEAD 和 heads(分支)引用,忽略其他引用
      if (ref !== 'HEAD' && !ref.match(/^refs\/heads\//)) {
        return;
      }
      if (ref === 'refs/heads/master') {
        head = sha; // 如果是 master 分支,将其 SHA 设为 HEAD
      }
      userProfile.repositories[reponame][ref] = sha; // 记录仓库引用
      if (!announcedRefs[sha]) {
        console.log('Announcing ' + sha + ' for ' + ref + ' on repo ' + repo);
        announcedRefs[sha] = repo; // 标记已公布的引用
        dht.announce(sha, config.dht.announce, function (err) {
          if (err !== null) {
            console.log('Announced ' + sha);
          }
        });
      }
    });

    // 当列出引用结束时,减少计数器,最终发布用户的可变密钥
    ls.stdout.on('end', function () {
      count--;
      if (count <= 0) {
        publish_mutable_key(); // 当所有仓库处理完毕,发布密钥
      }
    });

    ls.on('exit', function (err) {
      if (err) {
        die(err); // 如果 git.ls 失败,终止程序
      }
    });
  });

  // 定义publish_mutable_key函数发布用户的可变密钥到 DHT
  function publish_mutable_key() {
    var json = JSON.stringify(userProfile); // 将用户仓库信息序列化为 JSON
    if (json.length > 950) {
      console.error("Can't publish mutable key: doesn't fit in 950 bytes.");
      return false; // 如果数据过大,无法发布
    }
    var value = new Buffer(json.length);
    value.write(json);
    var sig = key.sign(value); // 使用用户密钥对数据进行签名
    var opts = {
      k: bpad(32, Buffer(key.getPublic().x.toArray())), // 公钥
      seq: 0,
      v: value, // 用户数据
      sig: Buffer.concat([
        bpad(32, Buffer(sig.r.toArray())), // 签名的一部分
        bpad(32, Buffer(sig.s.toArray()))  // 签名的另一部分
      ])
    };
    console.log(json);
    // 将可变密钥发布到 DHT
    dht.put(opts, function (errors, hash) {
      console.error('errors=', errors);
      console.log('hash=', hash.toString('hex')); // 打印发布的哈希值
    });
  }
  

  // 创建一个 TCP 服务器,传入一个回调函数,该函数在每次有新的连接时被调用
  net.createServer(function (socket) {
    var wire = new Protocol(); // 创建新的 BitTorrent 协议实例(bittorrent-protocol)
    wire.use(ut_gittorrent()); // 使用 GitTorrent 扩展
    wire.use(ut_metadata()); // 使用元数据传输扩展

    // 将 socket 和协议 wire 连接起来
    socket.pipe(wire).pipe(socket);

    // 当收到握手时,处理对等节点
    wire.on('handshake', function (infoHash, peerId) {
      console.log('Received handshake for ' + infoHash.toString('hex'));
      var myPeerId = new Buffer('-WW' + VERSION + '-' + hat(48), 'utf8');
      wire.handshake(new Buffer(infoHash), new Buffer(myPeerId)); // 发送握手响应
    });

    // 当对等节点请求 Git 包时,生成相应的数据
    wire.ut_gittorrent.on('generatePack', function (sha) {
      console.error('calling git pack-objects for ' + sha);
      if (!announcedRefs[sha]) {
        console.error('Asked for an unknown sha: ' + sha);
        return;
      }
      var directory = announcedRefs[sha];
      var have = null;
      if (sha !== head) {
        have = head;
      }

      // 使用 Git 打包对象
      var pack = git.upload_pack(directory, sha, have);
      pack.stderr.pipe(process.stderr);

      // 当打包完成时,写入到文件
      pack.on('ready', function () {
        var filename = sha + '.pack';
        var stream = fs.createWriteStream(filename);
        pack.stdout.pipe(stream);
        stream.on('close', function () {// 写入操作完成,流关闭时,执行匿名函数
          console.error('Finished writing ' + filename);
          
          // 使用 WebTorrent 将文件进行种子共享
          var webtorrent = new WebTorrent({
            dht: {bootstrap: config.dht.bootstrap},
            tracker: false
          });

          // 种子创建完成后发送给对等节点
          webtorrent.seed(filename, function onTorrent (torrent) {
            console.error(torrent.infoHash);
            wire.ut_gittorrent.sendTorrent(torrent.infoHash);
          });
        });
      });

      // 打包进程结束时,检查退出码
      pack.on('exit', function (code) {
        if (code !== 0) {
          console.error('git-upload-pack process exited with code ' + code);
        }
      });
    });
  }).listen(config.dht.announce); // 监听配置中定义的端口
});
骚戴笔记公告 阅读详情

相关推荐

Modbus4J vs EasyModbus4J:哪个Java库更适合你的工业项目?

本文深入对比了Java领域两大Modbus通信库Modbus4J与EasyModbus4J。Modbus4J设计稳健、类型安全,适合复杂业务集成;EasyModbus4J基于Netty,在高并发场景下性能更优。文章从架构、性能、API及生产环境多维度分析,为工业物联网项目选型提供决策依据。

weixin_28475533的博客 319

TortoiseGit 使用教程

第二步 下载TortoiseGit 以及 汉化包 (安装教程这里就不详细明解)第六步:提交代码:右键点击修改文件 (点击同步也是一样)第五步: 拉取代码 选择一个空文件夹点击git克隆。最后一步 :提交成功之后 点击推送 (很重要)第三步:Tortoisegit 汉化包配置。

Eliaukgit的博客 581

ArcGIS Pro样式导入全攻略:从.style到.stylx的完整转换流程(附常见问题解决)

本文详细解析了ArcGIS Pro中从旧版.style文件导入并转换为新版.stylx格式的完整流程。针对用户在迁移过程中常见的样式库为空、符号匹配失败、显示异常等问题,提供了清晰的排查思路与解决方案,帮助用户高效完成样式库的升级与迁移,确保制图工作的连续性。

raspberrypi5的博客 530

GitTorrent项目:开放式开源协作模式解析

GitTorrent采用了一种独特的开源协作模式,称为"OPEN Open Source"(开放式开源)。这种模式突破了传统开源项目中严格的权限控制机制,将项目视为一个开放的协作空间,类似于开放百科全书的编辑模式。在这种模式下,表现出持续贡献能力的开发者可以获得直接提交代码的权限。 ## 核心协作原则 ### 1. 版本控制规范 - 严格禁止使用强制推送(--force)操作 - 禁止任何修改...

gitblog_00897的博客 506

使用GEOquery包下载原始数据

使用GEOquery包下载原始数据

m0_75172651的博客 1908

DolphinScheduler任务状态异常排查:从僵尸任务到数据库修复全流程

本文详细介绍了DolphinScheduler中僵尸任务的识别、诊断与修复全流程,包括数据库层面的SQL操作指南、系统恢复验证方法以及预防措施。针对任务状态异常问题,提供了从基础排查到高级源码分析的完整解决方案,帮助运维人员有效管理系统资源和工作流调度。

weixin_27966471的博客 138

CANN/ops-blas Sdot算子实现

BLAS Sdot算子实现。 Sdot(Real Dot Product)算子实现了两个实数向量的点积运算,是BLAS基础线性代数库中的核心算子之一。 该算子广泛应用于信号处理、统计学和线性代数等领域。 ## 支持的产品 - Atlas A3 训练系列产品/Atlas A3 推理系列产品 - Atlas A2 训练系列产品/Atlas A2 推理系列产品 ## 目录

gitblog_00188的博客 485

别再瞎调参数了!用MATLAB fmincon优化傅里叶级数,为你的机器人设计“完美”激励轨迹

本文详细介绍了如何利用MATLAB的fmincon优化器设计机器人激励轨迹,通过优化傅里叶级数参数,解决传统试错法的低效问题。文章涵盖了目标函数设计、约束条件转化及优化算法选择,帮助工程师实现高精度的系统辨识和动力学参数优化。

weixin_42613501的博客 243

Git基础入门一:起源与安装

作者同系列博客参考链接: Git基础入门五:分支管理Git基础入门四:版本回退(撤销修改)Git基础入门三:创建版本库和添加修改文件基本命令Git基础入门二:工作区和暂存区Git基础入门一:起源与安装 1、起源 2005年,由于商业的版本控制系统公司BitMover要收回Linux社区的产品免费使用权,大师Linus仅花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!...

torrentliu的专栏 360

基于机器学习的电视剧类型点击量数据分析研究

1. 背景介绍 1.1 流媒体平台的兴起与电视剧市场的繁荣 近年来,随着互联网技术的飞速发展,流媒体平台如Netflix、Disney+、腾讯视频等迅速崛起,成为人们观看影视内容的主要渠道。电视剧作为流媒体平台上的重要内容类型,其点击量数据蕴含着丰富的用户偏好和市场趋势信息。

AI天才研究院 1054

YOLO11改进 - 卷积Conv | DEGConv(方向引导边缘门控卷积) : 空间分块、方向先验、轻量边缘增强

本文介绍了方向引导边缘门控卷积(DEGConv),一种专为增强边缘敏感度与方向感知能力设计的轻量级模块。该方法通过 Sobel 算子生成方向嵌入,结合条纹卷积提取水平与垂直边缘特征,并利用门控机制动态抑制背景噪声,从而精准捕捉细粒度结构。我们将 DEGConv 成功集成进 YOLO11,在 Backbone 的 SPPF 后及 Neck 的 P3、P4 分支中引入该模块以优化特征提取。实验证明,集成 DEGConv 的 YOLO11 在检测任务中表现优异,有效提升了模型对复杂边缘与方向特征的建模能力。

魔改工程师的博客 680

安装TortoiseGit下载地址

一、首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了 二、安装tortoise git(乌龟git) 同样先下载http://download.tortoisegit.org/tgit/选择最新版本下载就ok了 ...

Jack的博客 238

注册GitHub时出现Unable to verify your captcha response.Please visit https://docs.github.com/解决方案(持续更新)

本文主要介绍了注册GitHub时出现Unable to verify your captcha response. Please visit https://docs.github.com/articles/troubleshooting-connectivity-problems/#troubleshooting-the-captcha for troubleshooting information.解决方案,希望能对学习使用GitHub的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 8万+

【p2p、分布式,区块链笔记 Torrent】WebTorrent的上传和下载界面

【代码】【p2p、分布式,区块链笔记 Torrent】WebTorrent的上传和下载界面。

ResumeProject的博客 795

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

使用C++编写一个DHT爬虫,实现从DHT网络爬取BT种子

使用C++编写一个DHT爬虫,实现从DHT网络爬取BT种子1、相关术语1.1、P2P网络1.2、DHT网络1.3、Kademlia算法1.4、KRPC协议1.5、MagNet协议1.6、本段小结2、BT下载的一些概念梳理2.1、BT软件下载原理2.2、迅雷获取种子的速度为什么那么快2.3、资源时效性问题2.4、好用的BT软件2.5、有没有已经编写好的DHT爬虫3、使用C++编写DHT爬虫3.1、实现原理3.2、实现DHT协议3.2.1、创建UDP服务3.2.2、加入DHT网络3.2.3、报文解析3.2.4、

游戏行业资深搬砖人员 1万+

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

推荐开源项目:Bootstrap-DHT - 您的BitTorrent DHT网络引导者

推荐开源项目:Bootstrap-DHT - 您的BitTorrent DHT网络引导者 bootstrap-dhtDHT bootstrap server项目地址:https://gitcode.com/gh_mirrors/bo/bootstrap-dht 1、项目介绍 Bootstrap-DHT是一个强大的DHT(分布式哈希表)引导服务器,它为BitTorrent DHT网络提供了一个接入...

gitblog_00027的博客 1503

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

【TortoiseGit】TortoiseGit安装和配置详细说明

一、TortoiseGit 简介 TortoiseGit 是基于 TortoiseSVN 的 Git 版本的 Windows Shell 界面。它是开源的,可以完全使用免费软件构建。 TortoiseGit 支持你执行常规任务,例如 commit、显示日志、区分两个版本、创建分支和标签、创建补丁等。 网站:tortoisegit.org 下载:tortoisegit.org/Download 文件:tortoisegit.org/docs/ 支持:tortoisegit.org/Support

No8g攻城狮的博客 15万+

GitTorrent —— 去中心化的 Github 网络

GitTorrent 是一个点对点的 Git 仓库网络,基于 BitTorrent 进行分享。 GitTorrent 包含 5 个组件: 一个 git 传输助手,知道如何去下载和上传 git 对象,可用于 git 本身执行 fetch/clone/push 操作 一个分布式哈希表来告知节点提供的 git 服务 BitTorrent 协议扩展负责发送打包...

weixin_34356138的博客 650

多智能体架构详解:网状去中心化架构(Mesh对等P2P)

网状去中心化架构是一种完全对等、无中心的多智能体系统,支持点对点直接通信,通过协商与共识机制实现全局协调。相比黑板架构的间接共享,其鲁棒性更强,适合分布式仿真、区块链自治等高容错场景,但管控难度大、消息开销高。适用于LangGraph等框架的模拟实现,需设计共识规则与广播策略。核心优势在于抗故障、动态扩展,短板为可控性差,不推荐用于需稳定输出的业务系统。

谙弆悕博士的技术博客! 362

【轻量化复现大模型 / KIMI K3】训练模块梳理 & Muon P2P 优化器

【轻量化复现大模型 / KIMI K3】训练模块梳理 & Muon P2P 优化器。

罗西的思考 210

NCCL-AllReduce-P2P 与 NET(GDR)数据传输过程分析 (八)

四个 rank 执行 AllReduce 求和时,Ring 和 Tree 采用不同的归约顺序。在 Ring 中,每个 rank 从前驱接收数据,向后继发送数据。每个 chunk 沿环逐跳归约,再沿同一方向分发归约结果。在 Tree 中,叶节点向父节点发送本地输入数据。中间节点将子节点发来的数据与本地输入数据逐元素相加,再把部分和发给父节点。根节点完成归约后向下广播结果。两种算法都沿当前 Channel 的邻接关系收发。节点内走 P2P 时,GPU 通过映射地址,经 PCIe 或 NVLink 访问对端显存。

非专业业余程序员 303

阿里云 CacheFS 入选 ATC 2026 并开源:面向大模型权重分发的 P2P 缓存文件系统

阿里云自研的CacheFS系统被SIGOPS ATC 2026录用并开源,解决大模型推理中权重分发瓶颈。该系统基于去中心化P2P协同缓存,利用集群空闲内存与高带宽网络,实现高效读取。通过哈希感知协同淘汰机制保障全模型覆盖,结合去中心化分发与高性能数据面,使50个实例加载时间从28分钟降至30秒,每日节省超7700 GPU卡时。支持主动预热与零回源扩容,显著降低冷启动延迟,推动弹性扩缩容成本优化。项目已开源,欢迎共建。

AIGC 5979

分布式光伏“四可“合规:储能集成商的选型与落地指南

随着分布式光伏和储能系统规模的快速增长,电网对接入设备的管理要求越来越高。"可观、可测、可控、可调"的**"四可"合规**,已成为储能集成商和 EPC 工程师必须面对的硬性指标 — 这不仅关乎项目并网,更直接影响电站的长期稳定运行和运维效率。集成商普遍反馈:现有系统在满足复杂调度要求时力不从心。设备协议多样、数据采集效率低下、远程控制指令下发不稳定、缺乏统一解决方案,让项目交付和后期运维举步维艰。如何高效整合不同厂商的设备,并确保数据准确、实时上传至调度中心,是本文要回答的问题。

ZenovaEdgeOS的博客 452

电商订单系统核心设计、促销系统、分布式唯一ID的实现

数据正确是底线:代码无 Bug、正确使用事务(含分布式事务)是基础,但特殊情况仍需专门设计。重复下单靠幂等:预生成全局唯一订单号作为主键,利用主键唯一约束保证只插入一次;重复插入失败时直接返回成功响应。更新订单防 ABA:用 version 版本号,更新前校验、更新时自增,且整个过程在同一事务中保证原子性。读写分离提升并发:主库写、从库读,实施简单;但要接受主从延迟,通过业务设计(支付完成页)、同事务查询或强制走主库来规避不一致。分库分表解决海量与高并发:数据量大分表、并发高分库,遵循能少拆就少拆。

E_3LUE的博客 326

一个 IP 一台 Docker 起 的 MinIO? 一台服务器,一个硬盘,如果 Docker 分布式跑数据库跑多少个?一个?一个数据库存多少表?

(都是 Docker 跑 MySQL)

。 183

分布式 ID 与幂等性设计高频面试题全解:Snowflake 时钟回拨、K8s WorkerID 漂移与幂等四道防线(2026 招聘 JD 真题复盘)

先算容量再选型:UUID/号段/雪花五方案取舍与雪花 64 位分配;时钟回拨四档分级处理与「换号段根治」;K8s Pod 漂移撞 workerId 的三类成因与租约回收;19 位 ID 到前端丢精度如何反过来击穿幂等;幂等四道防线(唯一索引/令牌/状态机/乐观锁)、幂等键四性质、支付回调重复与乱序、日终跑批断点续跑,附三张速查表与三层追问链。题目来源为公开招聘 JD 与公开面经。

蒙眼过河 347
上一篇: 【VSCODE 调试 cmake_debug】解决 ERROR: Failed to prepare executable target with name ‘undefined‘
下一篇: 【p2p、分布式,区块链笔记 Blockchain】OP_RETURN 比特币数据存储操作
FakeOccupational
博客等级 码龄6年 2万+粉丝 1729原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值