版本:[email protected] [email protected]
本文运行两个节点,一个在ubuntu,另外一个在windows,下文用ipfs1代表ubuntu端的ipfs,用ipfs2代表windows端的ipfs。两个节点将彼此的地址添加到彼此的bootstrap中,形成由2个节点组成的测试网。
两者的peerID分别为:
ipfs1: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT
ipfs2: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
将bitswap/dht/eventlog的log等级设置为debug
$ ipfs log level bitswap debug & ipfs log level dht debug & ipfs log level eventlog debug & ipfs log level ipns-repub debug & ipfs log level reprovider debug
[1] 4617
[2] 4618
Changed log level of 'dht' to 'debug'
Changed log level of 'bitswap' to 'debug'
Changed log level of 'eventlog' to 'debug'
[1]- 已完成 ipfs log level bitswap debug
[2]+ 已完成 ipfs log level dht debug
$ ipfs bootstrap add /ip4/192.168.1.3/tcp/4001/ipfs/Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
added /ip4/192.168.1.3/tcp/4001/ipfs/Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
$ ipfs swarm connect /ip4/192.168.1.3/tcp/4001/ipfs/Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
connect Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy success
ipfs1 log:
18:45:23.745 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:45:23.745 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:45:23.745 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
18:45:23.745 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
18:45:23.748 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:45:23.748 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
18:45:23.749 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:45:23.749 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
18:45:51.874 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:45:51.874 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
18:45:51.876 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:45:51.876 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
18:45:51.876 DEBUG dht: putProvider(QmXgqKTbzdh83pQtKFb19SpMCpDDcKR2ujqk3pKph9aCNF, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
18:45:51.877 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:45:51.878 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
18:45:51.879 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:45:51.880 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
18:45:51.880 DEBUG dht: putProvider(QmXLfoiSpghstKhhQY1TbfHEZkyWgF1uw6kNjk9EeMACgd, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
18:45:51.880 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:45:51.881 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:162
18:45:51.883 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:45:51.883 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
18:45:51.883 DEBUG dht: putProvider(QmPhk6cJkRcFfZCdYam4c9MKYjFG9V29LswUnbrFNhtk2S, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
18:48:25.407 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT handlePutValue /F5YGWLYSEDLM25E26URSZVMYHJ3C62CXO7CPLCBMCOWV5VZJDQOYKJQ4L65HU handlers.go:203
18:48:25.418 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT handlePutValue /F5UXA3TTF4JCBVWNOSNPKIZM2WMDU5RPNBLXPRHVRAWBHLK624URYHMFEYOF7OT2 handlers.go:203
18:49:23.606 INFO dht: starting bootstrap query (0/1) to random ID (routing table size was 1) dht_bootstrap.go:151
18:49:23.606 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:49:23.608 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:49:23.608 INFO dht: finished bootstrap query (0/1) to random ID (routing table size is now 1) dht_bootstrap.go:154
18:49:23.608 INFO dht: starting bootstrap query (1/1) to self: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT (routing table size was 1) dht_bootstrap.go:151
18:49:23.608 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
18:49:23.610 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
18:49:23.610 INFO dht: finished bootstrap query (1/1) to self: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT (routing table size is now 1) dht_bootstrap.go:154
从上面的log可见,ipfs1在swarm connect上ipfs2后,首先会去寻找K个最接近ipfs2的节点,由于本文构造的是两个节点的测试网,所以ipfs1没能找到。
接着,ipfs1就会通过"ADD_PROVIDER",将自己本地provide的块的key添加到ipfs2的DHT中.
在这期间,ipfs1和ipfs2每5s会启动一次discovery,一个是随机游走,另外一个是向其它peer找自己(findpeer),以补充自己的路由表,同时将自己的信息发布到其它节点的K桶中。
同理,ipfs2也是一样。
ipfs2 log
10:44:40.083 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handlePutValue /F5UXA3TTF4JCBZ4FRVEG2KYKGOIRWI2UZOPNQ4H2KYFXXPTXPQQR3IVSNWBTLQJQ handlers.go:203
10:44:40.084 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handlePutValue /F5YGWLYSEDTYLDKINUVQUM4RDMRVJS463BYPUVQLPO7HO7BBDWRLE3MDGXATA handlers.go:203
10:45:08.210 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmXgqKTbzdh83pQtKFb19SpMCpDDcKR2ujqk3pKph9aCNF'
handlers.go:351
10:45:08.210 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmXgqKTbzdh83pQtKFb19SpMCpDDcKR2ujqk3pKph9aCNF (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
10:45:08.214 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmXLfoiSpghstKhhQY1TbfHEZkyWgF1uw6kNjk9EeMACgd'
handlers.go:351
10:45:08.214 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmXLfoiSpghstKhhQY1TbfHEZkyWgF1uw6kNjk9EeMACgd (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
10:45:08.217 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmPhk6cJkRcFfZCdYam4c9MKYjFG9V29LswUnbrFNhtk2S'
handlers.go:351
10:46:41.694 INFO dht: starting bootstrap query (0/1) to random ID (routing table size was 1) dht_bootstrap.go:151
10:46:41.694 DEBUG dht: dialling QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT took 0ms (as observed by the dht subsystem). dial_queue.go:331
10:46:41.694 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
10:46:41.697 DEBUG dht: QUERY worker for: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT - not found, and no closer peers. query.go:312
10:46:41.697 INFO dht: finished bootstrap query (0/1) to random ID (routing table size is now 1) dht_bootstrap.go:154
10:46:41.698 INFO dht: starting bootstrap query (1/1) to self: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy (routing table size was 1) dht_bootstrap.go:151
10:46:41.698 DEBUG dht: dialling QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT took 0ms (as observed by the dht subsystem). dial_queue.go:331
10:46:41.698 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
10:46:41.699 DEBUG dht: QUERY worker for: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT - not found, and no closer peers. query.go:312
10:46:41.700 INFO dht: finished bootstrap query (1/1) to self: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy (routing table size is now 1) dht_bootstrap.go:154
$ ipfs cat QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY
ipfs1 log
20:15:34.191 INFO bitswap: want blocks: [QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY] wantmanager.go:73
20:15:35.192 INFO bitswap: want blocks: [QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY] wantmanager.go:73
20:15:35.193 DEBUG bitswap: New Provider Query on cid: QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY providerquerymanager.go:323
20:15:35.193 DEBUG bitswap: Beginning Find Provider Request for cid: QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY providerquerymanager.go:230
20:15:35.195 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
20:15:35.195 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
20:15:35.201 DEBUG dht: 0 provider entries routing.go:519
20:15:35.201 DEBUG dht: 0 provider entries decoded routing.go:521
20:15:35.202 DEBUG dht: got closer peers: 0 [] routing.go:547
20:15:35.202 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
20:15:35.202 DEBUG dht: Query error: routing: not found routing.go:559
20:15:35.202 DEBUG bitswap: Finished Provider Query on cid: QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY providerquerymanager.go:323
20:15:36.193 INFO bitswap: want blocks: [QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY] wantmanager.go:73
20:15:38.194 INFO bitswap: want blocks: [QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY] wantmanager.go:73
20:15:41.196 INFO bitswap: want blocks: [QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY] wantmanager.go:73
20:15:45.197 INFO bitswap: want blocks: [QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY] wantmanager.go:73
ipfs2 log
12:14:51.830 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY): begin handlers.go:306
12:14:51.834 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY): have 0 closer peers: [] handlers.go:334
12:14:51.834 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY): end handlers.go:337
12:15:50.831 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY): begin handlers.go:306
12:15:50.831 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY): have 0 closer peers: [] handlers.go:334
12:15:50.831 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmdnqtJeDP6eq6cM8Vn7zp1fxmnUuFUzPoX3WyiaixcaeY): end handlers.go:337
由log可以得知,由于ipfs1没有那个block,它会通过bitswap向ipfs2发送wantlist,表明它需要该block。如果bitswap在1S内获取到block,则向ipfs2发送cancel,表明ipfs1不再需要这个块,最后向DHT网络provide这个块,具体是先计算ipfs1 peerID和block CID的距离d,从第[log d]个桶中选出alpha(现为3)和block CID最近的节点,向它们发送add provide消息。ipfs2收到add provide消息后,就会把该provider记录添加到自己的ht.providers中。
如果bitswap在1S内还没获取到block,ipfs1就会通过dht查询(FindProvidersAsync函数, 先计算ipfs1 peerID和block CID的距离d,从第[log d]个桶中选出alpha(现为3)和block CID最近的节点,询问他们是否有提供该block的provider记录),以得到provide这个块的peers。
ipfs2收到get provider请求后,首先从本地(dht.providers)中查找,由于不知道本地provider记录当前是否可用,或者其它peer是否有更好的选择(比如其它节点网络好),不管本地有没有这个记录,ipfs2都会从routingTable选出K(现为20)个距离记录sha256("/ipns/xxx")最近的节点(closerPeers),将节点信息(closerPeers)和provider记录(两者都可为空)发给ipfs1。
ipfs1收到ipfs2的回复后,如果resp中有provider记录就将其收集起来,如果resp中有closerPeers,则将它们添加到peerstore.AddrBook(值得注意的是,该AddrBook带有超时机制),以便接下来向它们发起get provider查询请求。
如果ipfs1一直没有得到这个块,则每分钟通过dht向ipfs2发get provider消息询问,bitswap每次请求失败(ipfs2没有ipfs1想要的block),等待(delay)时间就会增加1S后,超时后继续通过bitswap向ipfs2发wantlist请求
$ echo "I love IPFS" | ipfs add
added QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c
12 B / 12 B [========================================================================================================================] 100.00%
ipfs1 log
20:25:35.460 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
20:25:35.460 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
20:25:35.463 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
20:25:35.463 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
20:25:35.463 DEBUG dht: putProvider(QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
20:25:35.475 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
20:25:35.475 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
20:25:35.477 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
20:25:35.477 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
20:25:35.478 DEBUG dht: putProvider(QmYwdSBKFmavG8nocQ9EJzojzTEFoRke66F9sePvLfNrSW, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
20:25:35.515 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
20:25:35.515 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
20:25:35.516 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
20:25:35.517 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
20:25:35.517 DEBUG dht: putProvider(QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
值得注意的是,ipfs block put 命令也会触发putProvider,只不过provide的是data的CID,没有DAG node的CID。
估计通过ipfs block put添加的内容,没有触发ipfs构建DAG,所以也不能ipfs name publish
ipfs2 log
12:24:52.127 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c'
handlers.go:351
12:24:52.127 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
12:24:52.142 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmYwdSBKFmavG8nocQ9EJzojzTEFoRke66F9sePvLfNrSW'
handlers.go:351
12:24:52.142 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmYwdSBKFmavG8nocQ9EJzojzTEFoRke66F9sePvLfNrSW (addrs: [/ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001 /ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001]) handlers.go:368
12:24:52.179 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c'
handlers.go:351
12:24:52.180 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
$ ipfs cat QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c
I love IPFS
ipfs1 log
20:29:34.124 INFO bitswap: Sending block [Block QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c] to Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy workers.go:94
20:29:34.135 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT adding Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy as a provider for 'QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c'
handlers.go:351
20:29:34.135 DEBUG dht: received provider Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy for QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c (addrs: [/ip4/192.168.1.3/tcp/4001 /ip4/169.254.72.9/tcp/4001 /ip4/192.168.56.1/tcp/4001 /ip4/192.168.25.1/tcp/4001 /ip4/192.168.5.1/tcp/4001 /ip4/127.0.0.1/tcp/4001 /ip6/2409:8a55:435:8000:48b9:2f2b:e352:4f47/tcp/4001 /ip6/2409:8a55:435:8000:b4b1:84fe:321b:8183/tcp/4001 /ip6/::1/tcp/4001]) handlers.go:368
ipfs2 log
12:28:50.798 INFO bitswap: want blocks: [QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c] wantmanager.go:73
12:28:50.800 DEBUG bitswap: got block [Block QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c] from QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT bitswap.go:337
12:28:50.804 DEBUG dht: Provide QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c routing.go:407
12:28:50.805 DEBUG dht: dialling QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT took 0ms (as observed by the dht subsystem). dial_queue.go:331
12:28:50.805 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
12:28:50.806 DEBUG dht: QUERY worker for: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT - not found, and no closer peers. query.go:312
12:28:50.806 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
12:28:50.809 DEBUG dht: putProvider(QmTCw96CuSJ72BJysz67JLv9e3xwq6f8s2UrRwyF1sNK2c, QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT) routing.go:429
分析:
默认的时候,provide是enable的,ipfs1 add的时候,把记录add到ipfs2的dht中,向ipfs2表明自己有该block。
cat的时候,ipfs2发现自己的blockstore没有该block,就找bitswap要,bitswap首先向连接的peer发送wantlist。ipfs1收到这个wantlist请求后,将块发给ipfs2。
ipfs2在要到该block之后,自己也作为provider,把记录add到ipfs1的dht中,向dht网络提供内容。
ubuntu@ubuntu:~$ echo "Hello, Now is midnight" | ipfs add
added QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o
23 B / 23 B [========================================================================================================================] 100.00%ubuntu@ubuntu:~$
ubuntu@ubuntu:~$ ipfs name publish QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o
Published to QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT: /ipfs/QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o
ipfs1 log
07:59:35.451 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
07:59:35.452 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
07:59:35.453 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
07:59:35.453 DEBUG dht: putProvider(QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
07:59:35.464 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
07:59:35.464 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
07:59:35.466 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
07:59:35.466 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
07:59:35.466 DEBUG dht: putProvider(QmbtJV3KWk72L2K8nXt7PTZqDt1poUuWUeJZAFiRxZuyN8, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
07:59:35.510 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
07:59:35.512 DEBUG dht: delivering dialled peer to DHT; took 1ms. dial_queue.go:186
07:59:35.513 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
07:59:35.513 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
07:59:35.513 DEBUG dht: putProvider(QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
08:00:02.010 DEBUG dht: PutValue /pk/ 煍Hm+
3�#T˞�p�V
{�w|!��m�5�0 routing.go:47
08:00:02.010 DEBUG dht: getLocal /pk/ 煍Hm+
3�#T˞�p�V
{�w|!��m�5�0 dht.go:245
08:00:02.010 DEBUG dht: putLocal: /pk/ 煍Hm+
3�#T˞�p�V
{�w|!��m�5�0 key:"/pk/\022 \347\205\215Hm+\n3\221\033#T\313\236\330p\372V\013{\276w|!\035\242\262m\2035\3010" value:"\010\000\022\246\0020\202\001\"0\r\006\t*\206H\206\367\r\001\001\001\005\000\003\202\001\017\0000\202\001\n\002\202\001\001\000\301\025\221\271Y\257D\254\373\372\200\rc\215\n\327\240>\255s*U\255\312\203\t\n_\244m\335\035\241\375\033\345$\236\024o).K\032\332&c\023uk\311\222\333\226\005\224O\206L\224\371\316\270\245\0062\023\255\252\236\224\372\320-\033\216\317~\221\325\216\304\236\246v\223*?\177\255\240\326\031\n\340?\277\231\036\010C\033jz!S\376r\313\226\331\346\375*\237\234\242\236\014\r\247\214\022-/\267\236\230\304\361L\244\244`b\322\305\005H\031\262H\221S\207!\271\263y/+\275@\267\002j\270l6`\273\331\331\355n\013\257-\334+\000p[\243B\300\376\354\035\234[\323B\035\217\340\254\326\330-&9y\311\307\032\325\263\201\001\355\334\334\270\0073\204*\2342;b\304@]%TP@t\312p\222\224:f\324u\025\234*S/\323ji\025\333\303T\353\314\201\217\tU\370'\007\355\002\351\350\r\301\013\002\003\001\000\001" timeReceived:"2020-02-16T16:00:02.010464175Z" dht.go:263
add的时候,ipfs1在将block添加到blockstore后,向dht网络发布该块,通过provide函数将provide记录添加到ipfs2中。
name publish的时候,ipfs1将ipns记录,向dht网络发布该记录,通过putValue函数添加到ipfs2中。
上述的分析也可以通过ipfs2 log佐证。
ipfs2 log
3:58:54.334 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o'
handlers.go:351
23:58:54.334 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
23:58:54.347 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmbtJV3KWk72L2K8nXt7PTZqDt1poUuWUeJZAFiRxZuyN8'
handlers.go:351
23:58:54.347 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmbtJV3KWk72L2K8nXt7PTZqDt1poUuWUeJZAFiRxZuyN8 (addrs: [/ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001 /ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001]) handlers.go:368
23:58:54.394 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o'
handlers.go:351
23:58:54.395 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:a55a:5033:eea5:b209/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
23:59:20.894 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handlePutValue /F5YGWLYSEDTYLDKINUVQUM4RDMRVJS463BYPUVQLPO7HO7BBDWRLE3MDGXATA handlers.go:203
23:59:20.896 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handlePutValue /F5UXA3TTF4JCBZ4FRVEG2KYKGOIRWI2UZOPNQ4H2KYFXXPTXPQQR3IVSNWBTLQJQ handlers.go:203
$ ipfs cat /ipns/QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT
Hello, Now is midnight
ipfs1本地有,不需要通过dht查找,所以没有打印
ipfs2 cat /ipns/peerIDofIpfs1
$ ipfs cat /ipns/QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT
Hello, Now is midnight
ipfs1 log
08:02:58.587 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT handleGetValue for key: /ipns/ 煍Hm+
3�#T˞�p�V
{�w|!��m�5�0 handlers.go:51
08:02:58.587 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT handleGetValue looking into ds handlers.go:90
08:02:58.587 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT handleGetValue looking into ds GOT [10 40 47 105 112 110 115 47 18 32 231 133 141 72 109 43 10 51 145 27 35 84 203 158 216 112 250 86 11 123 190 119 124 33 29 162 178 109 131 53 193 48 18 139 5 10 52 47 105 112 102 115 47 81 109 83 116 80 78 100 110 52 76 110 103 67 77 65 98 119 49 110 82 83 53 89 84 115 89 68 72 114 52 72 67 98 100 121 49 50 121 56 117 104 115 120 83 56 111 18 128 2 111 51 121 114 140 69 130 232 189 19 234 222 79 13 25 90 20 239 252 200 98 249 140 229 109 91 146 195 5 207 29 1 190 62 167 12 248 144 8 224 191 74 0 137 73 165 165 193 203 250 120 219 30 193 173 87 66 30 221 233 203 79 184 227 82 21 184 160 241 152 25 129 137 17 162 182 3 191 180 163 14 50 30 227 242 187 234 36 49 253 56 67 152 83 190 166 253 115 20 118 135 213 155 154 150 126 100 195 250 197 97 82 34 212 164 62 144 244 111 51 23 255 71 93 185 15 89 67 154 64 145 68 33 147 126 194 62 46 180 212 193 109 4 142 229 20 72 103 98 211 130 17 237 25 183 194 160 27 27 0 80 196 203 5 202 36 8 215 7 244 178 199 124 249 73 33 231 125 63 139 52 187 155 81 192 142 173 12 1 62 132 95 135 168 83 219 75 13 13 232 138 90 135 142 92 201 88 100 97 200 135 219 215 47 76 133 221 220 104 212 159 115 39 153 226 152 15 176 157 250 160 188 42 225 67 21 180 40 44 74 205 51 162 85 200 38 249 214 153 7 180 250 208 104 161 151 24 0 34 30 50 48 50 48 45 48 50 45 49 55 84 49 54 58 48 48 58 48 50 46 48 48 55 50 57 54 48 53 53 90 40 2 58 171 2 8 0 18 166 2 48 130 1 34 48 13 6 9 42 134 72 134 247 13 1 1 1 5 0 3 130 1 15 0 48 130 1 10 2 130 1 1 0 193 21 145 185 89 175 68 172 251 250 128 13 99 141 10 215 160 62 173 115 42 85 173 202 131 9 10 95 164 109 221 29 161 253 27 229 36 158 20 111 41 46 75 26 218 38 99 19 117 107 201 146 219 150 5 148 79 134 76 148 249 206 184 165 6 50 19 173 170 158 148 250 208 45 27 142 207 126 145 213 142 196 158 166 118 147 42 63 127 173 160 214 25 10 224 63 191 153 30 8 67 27 106 122 33 83 254 114 203 150 217 230 253 42 159 156 162 158 12 13 167 140 18 45 47 183 158 152 196 241 76 164 164 96 98 210 197 5 72 25 178 72 145 83 135 33 185 179 121 47 43 189 64 183 2 106 184 108 54 96 187 217 217 237 110 11 175 45 220 43 0 112 91 163 66 192 254 236 29 156 91 211 66 29 143 224 172 214 216 45 38 57 121 201 199 26 213 179 129 1 237 220 220 184 7 51 132 42 156 50 59 98 196 64 93 37 84 80 64 116 202 112 146 148 58 102 212 117 21 156 42 83 47 211 106 105 21 219 195 84 235 204 129 143 9 85 248 39 7 237 2 233 232 13 193 11 2 3 1 0 1 42 30 50 48 50 48 45 48 50 45 49 54 84 49 54 58 48 48 58 48 50 46 48 49 48 56 55 54 49 56 51 90] handlers.go:93
08:02:58.587 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT handleGetValue success! handlers.go:105
08:02:58.590 INFO bitswap: Sending block [Block QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o] to Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy workers.go:94
08:02:58.601 DEBUG dht: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT adding Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy as a provider for 'QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o'
handlers.go:351
08:02:58.602 DEBUG dht: received provider Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy for QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o (addrs: [/ip4/192.168.1.3/tcp/4001 /ip4/169.254.72.9/tcp/4001 /ip4/192.168.56.1/tcp/4001 /ip4/192.168.25.1/tcp/4001 /ip4/192.168.5.1/tcp/4001 /ip4/127.0.0.1/tcp/4001 /ip6/2409:8a55:435:8000:48b9:2f2b:e352:4f47/tcp/4001 /ip6/2409:8a55:435:8000:b4b1:84fe:321b:8183/tcp/4001 /ip6/::1/tcp/4001]) handlers.go:368
ipfs2 log
00:02:17.476 DEBUG dht: getPublicKey for: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT records.go:27
00:02:17.477 DEBUG dht: getLocal /ipns/ 煍Hm+
3�#T˞�p�V{�w|!��m�5�0 dht.go:245
00:02:17.477 DEBUG dht: have it locally routing.go:290
00:02:17.477 DEBUG dht: peers in rt: 1 [QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT] routing.go:307
00:02:17.477 DEBUG dht: dialling QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT took 0ms (as observed by the dht subsystem). dial_queue.go:331
00:02:17.477 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
00:02:17.479 DEBUG dht: getValueOrPeers: got value dht.go:197
00:02:17.479 DEBUG dht: QUERY worker for: QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT - not found, and no closer peers. query.go:312
00:02:17.480 INFO bitswap: want blocks: [QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o] wantmanager.go:73
00:02:17.482 DEBUG bitswap: got block [Block QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o] from QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT bitswap.go:337
00:02:17.486 DEBUG dht: Provide QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o routing.go:407
ipfs2通过dht调用GetValues函数,发现了ipfs1之前添加的ipns记录,就调用getValueOrPeers函数找ipfs1要该ipns对应的块的CID,ipfs2紧接着通过bitswap找ipfs发送wantlist请求,在拿到块后,向DHT网络发布了这个块,表明自己提供这个块的下载。
在执行完上述操作后,执行ipfs dht cmd,通过dht获取value/nodeInfo/providers
获取peer的节点信息
$ ipfs dht findpeer Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
/ip4/192.168.5.1/tcp/4001
/ip4/127.0.0.1/tcp/4001
/ip6/2409:8a55:435:8000:e855:f9ee:b549:d0e0/tcp/4001
/ip6/::1/tcp/4001
/ip4/192.168.1.3/tcp/4001
/ip4/169.254.72.9/tcp/4001
/ip4/192.168.56.1/tcp/4001
/ip4/192.168.25.1/tcp/4001
/ip6/2409:8a55:435:8000:48b9:2f2b:e352:4f47/tcp/4001
获取提供内容的providers
$ ipfs dht findprovs QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o
Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT
ipfs1 log
23:11:40.139 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
23:11:40.139 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
23:11:40.141 DEBUG dht: 2 provider entries routing.go:519
23:11:40.141 DEBUG dht: 2 provider entries decoded routing.go:521
23:11:40.141 DEBUG dht: got provider: {Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy: []} routing.go:528
23:11:40.141 DEBUG dht: got provider: {QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT: [/ip4/127.0.0.1/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:41e:a01:82e4:89e1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]} routing.go:528
23:11:40.141 DEBUG dht: got closer peers: 0 [] routing.go:547
23:11:40.141 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
23:11:40.141 DEBUG dht: Query error: routing: not found routing.go:559
ipfs2 log
15:17:44.334 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o): begin handlers.go:306
15:17:44.334 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o): have 2 providers: [{Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy: []} {QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT: [/ip4/127.0.0.1/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:41e:a01:82e4:89e1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]}] handlers.go:326
15:17:44.334 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o): have 0 closer peers: [] handlers.go:334
15:17:44.334 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetProviders(QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT, QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o): end handlers.go:337
ipfs1通过FindProvidersAsync函数在自己的providers找provider的同时,还去问了ipfs2,以获取更新的数据
$ ipfs dht get /ipns/QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT
4/ipfs/QmNa1wxCtN2fJnLmS1J8raFDcfRjFWsqy6mAHAUH95zds1�
Q\�h����s�)-�\>�M}1����YT'�\Ed�WHj*�m����Y��"����M[����S��X�o�\���0ο�[����3��g�P[P,@�L
��Z›�,�!�ߊ�!���+�2o���@&X�U���{�B�uUJ��zt#��Akon�0�������N���j��W� ��`�n����5in�p��B
�W�b0 �2^�_xo�[<���n�ݵY6L}�����
�0��H��0-02-18T01:26:03.619649005Z(:��0�"0
c����Y�D����
נ>�s*U�ʃ
_�m����$�o).K�&cukɒۖ�O�L��θ�2������-��~�ՎĞ�v�*?���
�?��Cjz!S�r˖���*����
��-/�����L��`b��H�H�S�!��y/+�@�j�l6`����n
� �-�+p[�B����[�B�����-&9y��ճ���ܸ3�*�2;b�@]%TP@t�p��:f�u�*S/�ji��T�́� U�'���
ipfs1 log
23:28:26.860 DEBUG dht: getLocal /ipns/ 煍Hm+
3�#T˞�p�V
{�w|!��m�5�0 dht.go:245
23:28:26.860 DEBUG dht: have it locally routing.go:290
23:28:26.860 DEBUG dht: peers in rt: 1 [Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy] routing.go:307
23:28:26.860 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
23:28:26.862 DEBUG dht: getValueOrPeers: got value dht.go:197
23:28:26.862 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
23:28:26.862 DEBUG dht: GetValue /ipns/ 煍Hm+
3�#T˞�p�V
{�w|!��m�5�0 [10 52 47 105 112 102 115 47 81 109 78 97 49 119 120 67 116 78 50 102 74 110 76 109 83 49 74 56 114 97 70 68 99 102 82 106 70 87 115 113 121 54 109 65 72 65 85 72 57 53 122 100 115 49 18 128 2 23 161 134 22 199 191 107 65 35 69 206 120 244 85 235 72 189 192 182 253 151 29 118 152 94 177 200 98 99 233 245 102 1 204 160 188 238 219 72 96 63 163 86 66 17 136 122 212 235 19 124 145 160 190 251 213 174 193 36 86 163 65 145 70 200 218 198 135 97 158 170 165 37 147 146 153 167 142 118 248 199 106 161 206 63 114 235 185 90 73 124 176 181 17 111 135 165 71 246 236 238 220 4 163 147 23 116 227 191 249 34 237 140 141 3 254 249 4 52 248 122 26 82 149 111 120 165 175 226 49 170 72 228 169 175 9 201 72 149 63 111 153 37 156 126 202 147 159 118 170 179 72 140 228 104 102 233 246 29 42 128 229 78 111 223 247 122 189 46 8 224 205 80 62 63 39 33 66 177 98 49 220 19 141 118 134 9 25 132 142 238 220 92 118 85 87 53 80 100 45 145 81 225 149 196 87 7 211 243 214 12 246 197 116 171 125 98 167 22 201 20 210 244 158 159 163 0 216 26 233 122 188 133 76 212 4 168 53 175 197 58 226 252 225 74 39 114 206 30 94 8 32 199 3 71 249 24 0 34 30 50 48 50 48 45 48 50 45 49 56 84 48 54 58 48 57 58 48 56 46 56 48 52 56 54 49 53 52 53 90 40 6 58 171 2 8 0 18 166 2 48 130 1 34 48 13 6 9 42 134 72 134 247 13 1 1 1 5 0 3 130 1 15 0 48 130 1 10 2 130 1 1 0 193 21 145 185 89 175 68 172 251 250 128 13 99 141 10 215 160 62 173 115 42 85 173 202 131 9 10 95 164 109 221 29 161 253 27 229 36 158 20 111 41 46 75 26 218 38 99 19 117 107 201 146 219 150 5 148 79 134 76 148 249 206 184 165 6 50 19 173 170 158 148 250 208 45 27 142 207 126 145 213 142 196 158 166 118 147 42 63 127 173 160 214 25 10 224 63 191 153 30 8 67 27 106 122 33 83 254 114 203 150 217 230 253 42 159 156 162 158 12 13 167 140 18 45 47 183 158 152 196 241 76 164 164 96 98 210 197 5 72 25 178 72 145 83 135 33 185 179 121 47 43 189 64 183 2 106 184 108 54 96 187 217 217 237 110 11 175 45 220 43 0 112 91 163 66 192 254 236 29 156 91 211 66 29 143 224 172 214 216 45 38 57 121 201 199 26 213 179 129 1 237 220 220 184 7 51 132 42 156 50 59 98 196 64 93 37 84 80 64 116 202 112 146 148 58 102 212 117 21 156 42 83 47 211 106 105 21 219 195 84 235 204 129 143 9 85 248 39 7 237 2 233 232 13 193 11 2 3 1 0 1] routing.go:147
ipfs2 log
15:27:43.120 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetValue for key: /ipns/ 煍Hm+
3�#T˞�p�V{�w|!��m�5�0 handlers.go:51
15:27:43.120 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetValue looking into ds handlers.go:90
15:27:43.120 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetValue looking into ds GOT [10 40 47 105 112 110 115 47 18 32 231 133 141 72 109 43 10 51 145 27 35 84 203 158 216 112 250 86 11 123 190 119 124 33 29 162 178 109 131 53 193 48 18 139 5 10 52 47 105 112 102 115 47 81 109 78 97 49 119 120 67 116 78 50 102 74 110 76 109 83 49 74 56 114 97 70 68 99 102 82 106 70 87 115 113 121 54 109 65 72 65 85 72 57 53 122 100 115 49 18 128 2 23 161 134 22 199 191 107 65 35 69 206 120 244 85 235 72 189 192 182 253 151 29 118 152 94 177 200 98 99 233 245 102 1 204 160 188 238 219 72 96 63 163 86 66 17 136 122 212 235 19 124 145 160 190 251 213 174 193 36 86 163 65 145 70 200 218 198 135 97 158 170 165 37 147 146 153 167 142 118 248 199 106 161 206 63 114 235 185 90 73 124 176 181 17 111 135 165 71 246 236 238 220 4 163 147 23 116 227 191 249 34 237 140 141 3 254 249 4 52 248 122 26 82 149 111 120 165 175 226 49 170 72 228 169 175 9 201 72 149 63 111 153 37 156 126 202 147 159 118 170 179 72 140 228 104 102 233 246 29 42 128 229 78 111 223 247 122 189 46 8 224 205 80 62 63 39 33 66 177 98 49 220 19 141 118 134 9 25 132 142 238 220 92 118 85 87 53 80 100 45 145 81 225 149 196 87 7 211 243 214 12 246 197 116 171 125 98 167 22 201 20 210 244 158 159 163 0 216 26 233 122 188 133 76 212 4 168 53 175 197 58 226 252 225 74 39 114 206 30 94 8 32 199 3 71 249 24 0 34 30 50 48 50 48 45 48 50 45 49 56 84 48 54 58 48 57 58 48 56 46 56 48 52 56 54 49 53 52 53 90 40 6 58 171 2 8 0 18 166 2 48 130 1 34 48 13 6 9 42 134 72 134 247 13 1 1 1 5 0 3 130 1 15 0 48 130 1 10 2 130 1 1 0 193 21 145 185 89 175 68 172 251 250 128 13 99 141 10 215 160 62 173 115 42 85 173 202 131 9 10 95 164 109 221 29 161 253 27 229 36 158 20 111 41 46 75 26 218 38 99 19 117 107 201 146 219 150 5 148 79 134 76 148 249 206 184 165 6 50 19 173 170 158 148 250 208 45 27 142 207 126 145 213 142 196 158 166 118 147 42 63 127 173 160 214 25 10 224 63 191 153 30 8 67 27 106 122 33 83 254 114 203 150 217 230 253 42 159 156 162 158 12 13 167 140 18 45 47 183 158 152 196 241 76 164 164 96 98 210 197 5 72 25 178 72 145 83 135 33 185 179 121 47 43 189 64 183 2 106 184 108 54 96 187 217 217 237 110 11 175 45 220 43 0 112 91 163 66 192 254 236 29 156 91 211 66 29 143 224 172 214 216 45 38 57 121 201 199 26 213 179 129 1 237 220 220 184 7 51 132 42 156 50 59 98 196 64 93 37 84 80 64 116 202 112 146 148 58 102 212 117 21 156 42 83 47 211 106 105 21 219 195 84 235 204 129 143 9 85 248 39 7 237 2 233 232 13 193 11 2 3 1 0 1 42 27 50 48 50 48 45 48 50 45 49 55 84 48 54 58 48 56 58 52 55 46 52 55 57 55 56 51 90] handlers.go:93
15:27:43.120 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy handleGetValue success! handlers.go:105
ipfs1通过GetValue函数在自己的datastore找ipns记录的同时,还去问了ipfs2,以获取更新的数据
$ ipfs dht query Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy
log
23:32:37.791 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
23:32:37.791 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
23:32:37.792 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
23:32:37.792 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
由于只有2个节点,所以找不到最接近ipfs2的节点(此时最接近ipfs2的节点应该是ipfs1,可能是被过滤了?)
ipfs dht provide QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o
ipfs1 log
23:35:37.057 DEBUG dht: dialling Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy took 0ms (as observed by the dht subsystem). dial_queue.go:331
23:35:37.057 DEBUG dht: delivering dialled peer to DHT; took 0ms. dial_queue.go:186
23:35:37.059 DEBUG dht: QUERY worker for: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy - not found, and no closer peers. query.go:312
23:35:37.059 DEBUG dht: closestPeers query run error: routing: not found lookup.go:98
23:35:37.059 DEBUG dht: putProvider(QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o, Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy) routing.go:428
ipfs2 log
15:34:53.342 DEBUG dht: Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy adding QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT as a provider for 'QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o'
handlers.go:351
15:34:53.342 DEBUG dht: received provider QmdvR13vcQvRUy8vrdK7g2guwLVB4fQZGSbNifERCz5iNT for QmStPNdn4LngCMAbw1nRS5YTsYDHr4HCbdy12y8uhsxS8o (addrs: [/ip4/127.0.0.1/tcp/4001 /ip4/192.168.1.6/tcp/4001 /ip4/172.19.0.1/tcp/4001 /ip4/172.17.0.1/tcp/4001 /ip4/172.18.0.1/tcp/4001 /ip4/172.20.0.1/tcp/4001 /ip6/::1/tcp/4001 /ip6/2409:8a55:435:8000:41e:a01:82e4:89e1/tcp/4001 /ip6/2409:8a55:435:8000:9e2c:fb3b:7718:6a84/tcp/4001]) handlers.go:368
Provide的时候,ipfs1会调用Provide函数,Provide函数调用AddProvider函数,将provider记录添加到自己的providers,AddProvider函数会在add的时候,会保存当前的时间,如该provider记录超过24h都没更新,就会被清除。
ipfs2收到ipfs1的provide消息后,也会调用AddProvider函数,将provider记录添加到自己的providers。
下面的结构形似providers,使用形式为:providers[block_CID][peerID]。Created记录都是上次保存的时间。
providers map[string]map[peer.ID]providerRecord
type providerRecord struct {
Peer peer.AddrInfo
Created time.Time
}
值得注意的是,ipfs默认每12h reprovide一次,如ipfs1每12h就会reprovide一次,将本地以刷新存有该记录的时效性。
ipfs config文件
"Reprovider": {
"Interval": "12h",
"Strategy": "all"
}
刷新策略有3种:all/roots/pinned,默认是all。
其中,all代表reprovide blockstore中所有的block; roots代表reprovide pinned中标注着"direct"和"recursive"的block,不包括"indirect"; pinned代表reprovide除了"Internal"外被pinned的所有的block。
Recursive 状态
文件块树被递归添加到 pin 中,根文件块的状态是 Recursive,非根文件块的状态是 Indirect
Direct 状态
只有目标文件块添加到 pin 中, 子孙块不做处理,目标文件块的状态就是 Direct
Indirect 状态
文件块树被递归添加到 pin 中,根文件块的状态是 Recursive,非根文件块的状态是 Indirect
Internal 状态
ipfs 使用文件块来保存 pinner 状态,这些文件块的状态就是 Internal
NotPinned 状态
文件块没有被 pin,在 GC 时会被删除
打了个叉,现在回来。reprovide默认时间为12h,provider记录的有效期是24h,回收频率是1h
// go-ipfs/core/node/provide.go
const kReprovideFrequency = time.Hour * 12
// go-libp2p-kad-dht/providers/providers.go
var ProvideValidity = time.Hour * 24
var defaultCleanupInterval = time.Hour
如果关机休息上12h(及以上),再次开启ipfs2,就会有以下log。由于不能连上ipfs1,所以reprovider和repblulish都是失败的。
10:34:54.679 DEBUG dht: Provide QmYQTeucwbZDADZGBaJwHuEFvWtwiEd5G8ZwbmgCw5aDUz routing.go:407
10:34:54.679 DEBUG reprovider: Failed to provide key: failed to find any peer in table reprovide.go:105
10:35:28.755 INFO bitswap: want blocks: [] wantmanager.go:73
10:36:23.451 DEBUG dht: Provide QmYQTeucwbZDADZGBaJwHuEFvWtwiEd5G8ZwbmgCw5aDUz routing.go:407
10:36:23.451 DEBUG reprovider: Failed to provide key: failed to find any peer in table reprovide.go:105
10:37:12.472 INFO dht: starting bootstrap query (0/1) to random ID (routing table size was 0) dht_bootstrap.go:151
10:37:12.472 INFO dht: finished bootstrap query (0/1) to random ID (routing table size is now 0) dht_bootstrap.go:154
10:37:12.472 WARNI dht: error bootstrapping: failed to find any peer in table dht_bootstrap.go:86
10:37:34.538 DEBUG dht: Provide QmYQTeucwbZDADZGBaJwHuEFvWtwiEd5G8ZwbmgCw5aDUz routing.go:407
10:37:34.538 DEBUG reprovider: Failed to provide key: failed to find any peer in table reprovide.go:105
10:38:12.483 DEBUG ipns-repub: republishing ipns entry for Qmco9fPhEC9aYsFxY3ZekoUMZucmNa9soWDXh6xgr6FsJy repub.go:126
10:38:12.487 DEBUG dht: PutValue /pk/ ��t��#,:v/hWw���,�^�)�&_�z routing.go:47
10:38:12.487 DEBUG dht: PutValue /ipns/ ��t��#,:v/hWw���,�^�)�&_�z routing.go:47
10:38:12.487 DEBUG dht: getLocal /pk/ ��t��#,:v/hWw���,�^�)�&_�z dht.go:245
10:38:12.487 DEBUG dht: getLocal /ipns/ ��t��#,:v/hWw���,�^�)�&_�z dht.go:245
10:38:12.487 DEBUG dht: putLocal: /pk/ ��t��#,:v/hWw���,�^�)�&_�z key:"/pk/\022 \326\315t\232\365#,\325\230:v/hWw\304\365\210,\023\255^\327)\034\035\205&\034_\272z" value:"\010\000\022\246\0020\202\001\"0\r\006\t*\206H\206\367\r\001\001\001\005\000\003\202\001\017\0000\202\001\n\002\202\001\001\000\313-C\330%*\216\225\\z~\001\211xv\356\265\357-_&~VO\245B\212\217\017\351\023j&`g|\262o\345\355\034\212\023\356\241\030\0167\351}\264\205\003+\005\306\207Sx\361\224\257cJ\315\nc[R!\033i\352`\277\260\336r\261\233M\202\000=a\003\022\307\276U\204\032\376\244d)\224\315-\033\212\"'n\201*\033\004|\341x\361\241C\023\213D\224\037\354\323Z\215S:Vbv\037\267'\352\214\370\255\322B\355w\2755\265V\343YC\215\313\345W\305\250/\304\026\236\323\032\242\302\236\340{#M\222\022\231b\334\200\337\342\017=\262Ij\335\013O\351\021\021\223\374?\263\366\302UC~\267\254\221\006\361\330\317\013\307\033