From 94439e6b5f2e0efdcf6548015976b4dfda5fb02d Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 20 Dec 2019 16:25:31 -0500 Subject: [PATCH 01/43] wip - exchange signed routing records in identify --- go.mod | 9 +- go.sum | 91 ++----- p2p/protocol/identify/id.go | 53 +++- p2p/protocol/identify/pb/identify.pb.go | 330 +++++++++++++++--------- p2p/protocol/identify/pb/identify.proto | 7 + 5 files changed, 281 insertions(+), 209 deletions(-) diff --git a/go.mod b/go.mod index 7cb953c71b..b856c16b71 100644 --- a/go.mod +++ b/go.mod @@ -13,13 +13,13 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.0 + github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211501-15623e878c25 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 github.com/libp2p/go-libp2p-nat v0.0.5 github.com/libp2p/go-libp2p-netutil v0.1.0 - github.com/libp2p/go-libp2p-peerstore v0.1.4 + github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191122180640-0f9538856f85 github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-swarm v0.2.2 github.com/libp2p/go-libp2p-testing v0.1.1 @@ -29,13 +29,16 @@ require ( github.com/libp2p/go-stream-muxer-multistream v0.2.0 github.com/libp2p/go-tcp-transport v0.1.1 github.com/libp2p/go-ws-transport v0.2.0 + github.com/mailru/easyjson v0.7.0 // indirect github.com/miekg/dns v1.1.12 // indirect github.com/multiformats/go-multiaddr v0.2.0 github.com/multiformats/go-multiaddr-dns v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.2 github.com/multiformats/go-multistream v0.1.1 github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 - golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect + golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 // indirect + golang.org/x/sys v0.0.0-20191220142924-d4481acd189f // indirect + golang.org/x/tools v0.0.0-20191220195505-2f6d8bf0ad77 // indirect ) go 1.12 diff --git a/go.sum b/go.sum index 09d410c936..21ca279db5 100644 --- a/go.sum +++ b/go.sum @@ -6,9 +6,7 @@ github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETF github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8= -github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c h1:aEbSeNALREWXk0G7UdNhR3ayBV7tZ4M2PNmnrCAph6Q= github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI= -github.com/btcsuite/btcd v0.0.0-20190824003749-130ea5bddde3 h1:A/EVblehb75cUgXA5njHPn0kLAsykn6mJGz7rnmW5W0= github.com/btcsuite/btcd v0.0.0-20190824003749-130ea5bddde3/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI= github.com/btcsuite/btcd v0.20.1-beta h1:Ik4hyJqN8Jfyv3S4AGBOmyouMsYE3EdYODkMbQjwPGw= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= @@ -29,7 +27,6 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ= github.com/dgraph-io/badger v1.6.0-rc1/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= @@ -37,27 +34,20 @@ github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6ps github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= -github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= -github.com/gogo/protobuf v1.3.0 h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE= github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0= -github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -69,25 +59,20 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= -github.com/ipfs/go-cid v0.0.2 h1:tuuKaZPU1M6HcejsO3AcYWW8sZ8MTvyxfc4uqB4eFE8= github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= -github.com/ipfs/go-cid v0.0.3 h1:UIAh32wymBpStoe83YCzwVQQ5Oy/H0FdxvUS6DJDzms= github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= -github.com/ipfs/go-cid v0.0.4 h1:UlfXKrZx1DjZoBhQHmNHLC1fK1dUJDN20Y28A7s+gJ8= github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj6+M= github.com/ipfs/go-cid v0.0.5 h1:o0Ix8e/ql7Zb5UVUJEUfjsWCIY8t48++9lR8qi6oiJU= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= -github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= @@ -104,7 +89,6 @@ github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQ github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs= -github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2 h1:vhC1OXXiT9R2pczegwz6moDvuRpggaroAXhPIseh57A= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod h1:8GXXJV31xl8whumTzdZsTt3RnUIiPqzkyf7mxToRCMs= @@ -122,10 +106,8 @@ github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ= github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/libp2p/go-addr-util v0.0.1 h1:TpTQm9cXVRVSKsYbgQ7GKc3KbbHVTnbostgGaDEP+88= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= @@ -136,27 +118,24 @@ github.com/libp2p/go-conn-security-multistream v0.1.0 h1:aqGmto+ttL/uJgX0JtQI0tD github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-eventbus v0.1.0 h1:mlawomSAjjkk97QnYiEmHsLu7E136+2oCWSHRUvMfzQ= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= -github.com/libp2p/go-flow-metrics v0.0.1 h1:0gxuFd2GuK7IIP5pKljLwps6TvcuYgvG7Atqi3INF5s= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p-autonat v0.1.1 h1:WLBZcIRsjZlWdAZj9CiBSvU2wQXoUOiS1Zk1tM7DTJI= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= -github.com/libp2p/go-libp2p-blankhost v0.1.4 h1:I96SWjR4rK9irDHcHq3XHN6hawCRTPUADzkJacgZLvk= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= github.com/libp2p/go-libp2p-circuit v0.1.4 h1:Phzbmrg3BkVzbqd4ZZ149JxCuUWu2wZcXf/Kr6hZJj8= github.com/libp2p/go-libp2p-circuit v0.1.4/go.mod h1:CY67BrEjKNDhdTk8UgBX1Y/H5c3xkAcs3gnksxY7osU= github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= github.com/libp2p/go-libp2p-core v0.0.4/go.mod h1:jyuCQP356gzfCFtRKyvAbNkyeuxb7OlyhWZ3nls5d2I= -github.com/libp2p/go-libp2p-core v0.2.0 h1:ycFtuNwtZBAJSxzaHbyv6NjG3Yj5Nmra1csHaQ3zwaw= github.com/libp2p/go-libp2p-core v0.2.0/go.mod h1:X0eyB0Gy93v0DZtSYbEM7RnMChm9Uv3j7yRXjO77xSI= -github.com/libp2p/go-libp2p-core v0.2.2 h1:Sv1ggdoMx9c7v7FOFkR7agraHCnAgqYsXrU1ARSRUMs= github.com/libp2p/go-libp2p-core v0.2.2/go.mod h1:8fcwTbsG2B+lTgRJ1ICZtiM5GWCWZVoVrLaDRvIRng0= -github.com/libp2p/go-libp2p-core v0.2.4 h1:Et6ykkTwI6PU44tr8qUF9k43vP0aduMNniShAbUJJw8= github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g= -github.com/libp2p/go-libp2p-core v0.3.0 h1:F7PqduvrztDtFsAa/bcheQ3azmNo+Nq7m8hQY5GiUW8= +github.com/libp2p/go-libp2p-core v0.2.6-0.20191121175514-5fa975301271/go.mod h1:xDyprN8hkMpX27XQ1bBnYtuSuaCUFvKm+Q6gltHZCHE= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= +github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211501-15623e878c25 h1:Yzu98yHyavbHX1udrzM0xMN/06osM+6OKtKlzTuBMVQ= +github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211501-15623e878c25/go.mod h1:3PDCxmuTTy+gyLxQz7Tf7lOf36n7WUECOJ9CPWx0S6A= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -171,13 +150,10 @@ github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLK github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= -github.com/libp2p/go-libp2p-peerstore v0.1.3 h1:wMgajt1uM2tMiqf4M+4qWKVyyFc8SfA+84VV9glZq1M= github.com/libp2p/go-libp2p-peerstore v0.1.3/go.mod h1:BJ9sHlm59/80oSkpWgr1MyY1ciXAXV397W6h1GH/uKI= -github.com/libp2p/go-libp2p-peerstore v0.1.4 h1:d23fvq5oYMJ/lkkbO4oTwBp/JP+I/1m5gZJobNXCE/k= -github.com/libp2p/go-libp2p-peerstore v0.1.4/go.mod h1:+4BDbDiiKf4PzpANZDAT+knVdLxvqh7hXOujessqdzs= -github.com/libp2p/go-libp2p-secio v0.1.0 h1:NNP5KLxuP97sE5Bu3iuwOWyT/dKEGMN5zSLMWdB7GTQ= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191122180640-0f9538856f85 h1:VxnYkz4A0AbbdUF6pvjBdvYKKjSrybcSXnluxJlKziQ= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191122180640-0f9538856f85/go.mod h1:JZoEvuUQYNg+eiYQHXN954IMzdAd/bmkjiiMy7VE6TA= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= -github.com/libp2p/go-libp2p-secio v0.2.0 h1:ywzZBsWEEz2KNTn5RtzauEDq5RFEefPsttXYwAWqHng= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1 h1:eNWbJTdyPA7NxhP7J3c5lT97DC5d+u+IldkgCYFTPVA= github.com/libp2p/go-libp2p-secio v0.2.1/go.mod h1:cWtZpILJqkqrSkiYcDBh5lA3wbT2Q+hz3rJQq3iftD8= @@ -186,9 +162,7 @@ github.com/libp2p/go-libp2p-swarm v0.2.2 h1:T4hUpgEs2r371PweU3DuH7EOmBIdTBCwWs+F github.com/libp2p/go-libp2p-swarm v0.2.2/go.mod h1:fvmtQ0T1nErXym1/aa1uJEyN7JzaTNyBcHImCxRpPKU= github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= -github.com/libp2p/go-libp2p-testing v0.0.4 h1:Qev57UR47GcLPXWjrunv5aLIQGO4n9mhI/8/EIrEEFc= github.com/libp2p/go-libp2p-testing v0.0.4/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= -github.com/libp2p/go-libp2p-testing v0.1.0 h1:WaFRj/t3HdMZGNZqnU2pS7pDRBmMeoDx7/HDNpeyT9U= github.com/libp2p/go-libp2p-testing v0.1.0/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-testing v0.1.1 h1:U03z3HnGI7Ni8Xx6ONVZvUFOAzWYmolWf5W5jAOPNmU= github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= @@ -203,17 +177,13 @@ github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDR github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0= github.com/libp2p/go-mplex v0.1.0 h1:/nBTy5+1yRyY82YaO6HXQRnO5IAGsXTjEJaR3LdTPc0= github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU= -github.com/libp2p/go-msgio v0.0.2 h1:ivPvEKHxmVkTClHzg6RXTYHqaJQ0V9cDbq+6lKb3UV0= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4 h1:agEFehY3zWJFUHK6SEMR7UYmk2z6kC3oeCM7ybLhguA= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-nat v0.0.4 h1:KbizNnq8YIf7+Hn7+VFL/xE0eDrkPru2zIO9NMwL8UQ= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= -github.com/libp2p/go-openssl v0.0.2 h1:9pP2d3Ubaxkv7ZisLjx9BFwgOGnQdQYnfcH29HNY3ls= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= -github.com/libp2p/go-openssl v0.0.3 h1:wjlG7HvQkt4Fq4cfH33Ivpwp0omaElYEi9z26qaIkIk= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= -github.com/libp2p/go-openssl v0.0.4 h1:d27YZvLoTyMhIN4njrkr8zMDOM4lfpHIp6A+TK9fovg= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1 h1:7PhkfH73VXfPJYKQ6JwS5I/eVcoyYi9IMNGc6FWpFLw= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= @@ -222,7 +192,6 @@ github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA2 github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0 h1:714bRJ4Zy9mdhyTLJ+ZKiROmAFwUHpeRidG+q7LTQOg= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= -github.com/libp2p/go-tcp-transport v0.1.0 h1:IGhowvEqyMFknOar4FWCKSWE0zL36UFKQtiRQD60/8o= github.com/libp2p/go-tcp-transport v0.1.0/go.mod h1:oJ8I5VXryj493DEJ7OsBieu8fcg2nHGctwtInJVpipc= github.com/libp2p/go-tcp-transport v0.1.1 h1:yGlqURmqgNA2fvzjSgZNlHcsd/IulAnKM8Ncu+vlqnw= github.com/libp2p/go-tcp-transport v0.1.1/go.mod h1:3HzGvLbx6etZjnFlERyakbaYPdfjg2pWP97dFZworkY= @@ -232,8 +201,8 @@ github.com/libp2p/go-yamux v1.2.2/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZ github.com/libp2p/go-yamux v1.2.3 h1:xX8A36vpXb59frIzWFdEgptLMsOANMFq2K7fPRlunYI= github.com/libp2p/go-yamux v1.2.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= @@ -247,9 +216,7 @@ github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0 github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= -github.com/minio/sha256-simd v0.1.0 h1:U41/2erhAKcmSI14xh/ZTUdBPOzDOIfS93ibzUSl8KM= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= -github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= @@ -257,7 +224,6 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= -github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= @@ -265,70 +231,53 @@ github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= -github.com/multiformats/go-multiaddr v0.0.4 h1:WgMSI84/eRLdbptXMkMWDXPjPq7SPLIgGUVm2eroyU4= github.com/multiformats/go-multiaddr v0.0.4/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.1.0/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= -github.com/multiformats/go-multiaddr v0.1.1 h1:rVAztJYMhCQ7vEFr8FvxW3mS+HF2eY/oPbOMeS0ZDnE= github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo= github.com/multiformats/go-multiaddr v0.2.0 h1:lR52sFwcTCuQb6bTfnXF6zA2XfyYvyd+5a9qECv/J90= github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4= github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= -github.com/multiformats/go-multiaddr-dns v0.0.2 h1:/Bbsgsy3R6e3jf2qBahzNHzww6usYaZ0NhNH3sqdFS8= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.2.0 h1:YWJoIDwLePniH7OU5hBnDZV6SWuvJqJ0YtN6pLeH9zA= github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsVkduhQH2YqYSbxWJzYGdK0= -github.com/multiformats/go-multiaddr-fmt v0.0.1 h1:5YjeOIzbX8OTKVaN72aOzGIYW7PnrZrnkDyOfAWRSMA= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= -github.com/multiformats/go-multiaddr-net v0.0.1 h1:76O59E3FavvHqNg7jvzWzsPSW5JSi/ek0E4eiDVbg9g= github.com/multiformats/go-multiaddr-net v0.0.1/go.mod h1:nw6HSxNmCIQH27XPGBuX+d1tnvM7ihcFwHMSstNAVUU= -github.com/multiformats/go-multiaddr-net v0.1.0 h1:ZepO8Ezwovd+7b5XPPDhQhayk1yt0AJpzQBpq9fejx4= github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= -github.com/multiformats/go-multiaddr-net v0.1.1 h1:jFFKUuXTXv+3ARyHZi3XUqQO+YWMKgBdhEvuGRfnL6s= github.com/multiformats/go-multiaddr-net v0.1.1/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= github.com/multiformats/go-multiaddr-net v0.1.2 h1:P7zcBH9FRETdPkDrylcXVjQLQ2t1JQtNItZULWNWgeg= github.com/multiformats/go-multiaddr-net v0.1.2/go.mod h1:QsWt3XK/3hwvNxZJp92iMQKME1qHfpYmyIjFVsSOY6Y= github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA= github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= -github.com/multiformats/go-multihash v0.0.5 h1:1wxmCvTXAifAepIMyF39vZinRw5sbqjPs/UIi93+uik= github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= -github.com/multiformats/go-multihash v0.0.8 h1:wrYcW5yxSi3dU07n5jnuS5PrNwyHy0zRHGVoUugWvXg= github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= -github.com/multiformats/go-multihash v0.0.10 h1:lMoNbh2Ssd9PUF74Nz008KGzGPlfeV6wH3rit5IIGCM= github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= -github.com/multiformats/go-multistream v0.1.0 h1:UpO6jrsjqs46mqAK3n6wKRYFhugss9ArzbyUzU+4wkQ= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.1 h1:JlAdpIFhBhGRLxe9W6Om0w++Gd6KMWoFPZL/dEnm9nI= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= -github.com/multiformats/go-varint v0.0.1 h1:TR/0rdQtnNxuN2IhiB639xC3tWM4IUi7DkTBVTdGW/M= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/smola/gocompat v0.2.0 h1:6b1oIMlUXIpz//VKEDzPVBK8KG7beVwmHIUEBIs/Pns= github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY= -github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a h1:/eS3yfGjQKG+9kayBkj0ip1BGhq6zJ3eaVksphxAaek= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= -github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -342,14 +291,11 @@ github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= -github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/go-logging v0.0.1 h1:fwpzlmT0kRC/Fmd0MdmGgJG/CXIZ6gFq46FQZjprUcc= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= @@ -361,9 +307,7 @@ github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1: github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= -go.opencensus.io v0.22.1 h1:8dP3SGL7MPB94crU3bEPplMPe83FI4EouesJUeFHv50= go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2 h1:75k/FF0Q2YM8QYo07VPddOLBslDt1MZOdEslOHvmzAs= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -376,14 +320,15 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 h1:IcSOAf4PyMp3U3XbIEj1/xJ2BjNN2jWv7JoyOsMxXUU= golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 h1:aJ0ex187qoXrJHPo8ZasVTASQB7llQP6YeNzgDALPRk= +golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -397,8 +342,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -409,11 +354,9 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 h1:rOhMmluY6kLMhdnrivzec6lLgaVbMHMn2ISQXJeJ5EM= -golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/sys v0.0.0-20191220142924-d4481acd189f h1:68K/z8GLUxV76xGSqwTWw2gyk/jwn79LUL43rES2g8o= +golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -422,11 +365,11 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181130052023-1c3d964395ce/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c h1:vamGzbGri8IKo20MQncCuljcQ5uAO6kaCeawQPVblAI= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191220195505-2f6d8bf0ad77/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -436,15 +379,11 @@ google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRn google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 35e2e57ad8..a8cbbe7d66 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -3,6 +3,7 @@ package identify import ( "context" "fmt" + "github.com/libp2p/go-libp2p-core/routing" "runtime/debug" "sync" "time" @@ -316,6 +317,16 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { } log.Debugf("%s sent listen addrs to %s: %s", c.LocalPeer(), c.RemotePeer(), laddrs) + // Generate a signed routing record containing our listen addresses and + // send it along with the unsigned addrs + signedState, err := host.SignedRoutingStateFromHost(ids.Host) + if err != nil { + envelopeBytes, err := signedState.Marshal() + if err != nil { + mes.SignedRoutingState = envelopeBytes + } + } + // set our public key ownKey := ids.Host.Peerstore().PubKey(ids.Host.ID()) @@ -374,18 +385,35 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // that picks random source ports, this can cause DHT nodes to collect // many undialable addresses for other peers. + // add certified addresses for the peer, if they sent us a signed routing + // state record + routingState, err := signedRoutingStateFromMsg(mes) + if err != nil { + log.Warning("error getting routing state from Identify message: %v", err) + } + // Extend the TTLs on the known (probably) good addresses. // Taking the lock ensures that we don't concurrently process a disconnect. ids.addrMu.Lock() - switch ids.Host.Network().Connectedness(p) { - case network.Connected: - // invalidate previous addrs -- we use a transient ttl instead of 0 to ensure there - // is no period of having no good addrs whatsoever - ids.Host.Peerstore().UpdateAddrs(p, peerstore.ConnectedAddrTTL, transientTTL) - ids.Host.Peerstore().AddAddrs(p, lmaddrs, peerstore.ConnectedAddrTTL) - default: - ids.Host.Peerstore().UpdateAddrs(p, peerstore.ConnectedAddrTTL, transientTTL) - ids.Host.Peerstore().AddAddrs(p, lmaddrs, peerstore.RecentlyConnectedAddrTTL) + ttl := peerstore.RecentlyConnectedAddrTTL + if ids.Host.Network().Connectedness(p) == network.Connected { + ttl = peerstore.ConnectedAddrTTL + } + + // invalidate previous addrs -- we use a transient ttl instead of 0 to ensure there + // is no period of having no good addrs whatsoever + ids.Host.Peerstore().UpdateAddrs(p, peerstore.ConnectedAddrTTL, transientTTL) + + // add signed addrs if we have them + if routingState != nil { + addErr := ids.Host.Peerstore().AddCertifiedAddrs(routingState, ttl) + if addErr != nil { + log.Errorf("error adding signed addrs to peerstore: %v", addErr) + // fall back to adding unsigned addrs + ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl) + } + } else { + ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl) } ids.addrMu.Unlock() @@ -572,6 +600,13 @@ func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool { return false } +func signedRoutingStateFromMsg(msg *pb.Identify) (*routing.SignedRoutingState, error) { + if msg.SignedRoutingState == nil || len(msg.SignedRoutingState) == 0 { + return nil, nil + } + return routing.UnmarshalSignedRoutingState(msg.SignedRoutingState) +} + // netNotifiee defines methods to be used with the IpfsDHT type netNotifiee IDService diff --git a/p2p/protocol/identify/pb/identify.pb.go b/p2p/protocol/identify/pb/identify.pb.go index 5175425292..4af720789e 100644 --- a/p2p/protocol/identify/pb/identify.pb.go +++ b/p2p/protocol/identify/pb/identify.pb.go @@ -8,7 +8,6 @@ import ( proto "github.com/gogo/protobuf/proto" io "io" math "math" - math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. @@ -20,7 +19,7 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package type Delta struct { // new protocols now serviced by the peer. @@ -46,7 +45,7 @@ func (m *Delta) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { return xxx_messageInfo_Delta.Marshal(b, m, deterministic) } else { b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) + n, err := m.MarshalTo(b) if err != nil { return nil, err } @@ -98,7 +97,13 @@ type Identify struct { // protocols are the services this node is running Protocols []string `protobuf:"bytes,3,rep,name=protocols" json:"protocols,omitempty"` // a delta update is incompatible with everything else. If this field is included, none of the others can appear. - Delta *Delta `protobuf:"bytes,7,opt,name=delta" json:"delta,omitempty"` + Delta *Delta `protobuf:"bytes,7,opt,name=delta" json:"delta,omitempty"` + // signedRoutingState contains a serialized SignedEnvelope containing a RoutingStateRecord, + // signed by the sending node. It contains the same addresses as the listenAddrs field, but + // in a form that lets us share authenticated addrs with other peers. + // see github.com/libp2p/go-libp2p-core/crypto/pb/envelope.proto and + // github.com/libp2p/go-libp2p-core/routing/pb/routing_state.proto for message definitions. + SignedRoutingState []byte `protobuf:"bytes,8,opt,name=signedRoutingState" json:"signedRoutingState,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -118,7 +123,7 @@ func (m *Identify) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { return xxx_messageInfo_Identify.Marshal(b, m, deterministic) } else { b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) + n, err := m.MarshalTo(b) if err != nil { return nil, err } @@ -186,6 +191,13 @@ func (m *Identify) GetDelta() *Delta { return nil } +func (m *Identify) GetSignedRoutingState() []byte { + if m != nil { + return m.SignedRoutingState + } + return nil +} + func init() { proto.RegisterType((*Delta)(nil), "identify.pb.Delta") proto.RegisterType((*Identify)(nil), "identify.pb.Identify") @@ -194,29 +206,31 @@ func init() { func init() { proto.RegisterFile("identify.proto", fileDescriptor_83f1e7e6b485409f) } var fileDescriptor_83f1e7e6b485409f = []byte{ - // 251 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x90, 0xb1, 0x4e, 0xc3, 0x30, - 0x14, 0x45, 0xe5, 0x96, 0x02, 0x79, 0xb1, 0x5a, 0xe9, 0x4d, 0x1e, 0x50, 0x64, 0xb2, 0xe0, 0x29, - 0x03, 0x7f, 0x00, 0x62, 0x41, 0x2c, 0xc8, 0x48, 0xac, 0x28, 0xa9, 0x1f, 0xc8, 0x52, 0x1a, 0x57, - 0x8e, 0x41, 0xea, 0xce, 0xc7, 0x31, 0xf2, 0x09, 0x28, 0x5f, 0x82, 0xe2, 0x92, 0x26, 0x65, 0xf4, - 0xd1, 0x91, 0xef, 0xbb, 0x17, 0x96, 0xd6, 0x50, 0x13, 0xec, 0xeb, 0xae, 0xd8, 0x7a, 0x17, 0x1c, - 0xa6, 0xe3, 0xbb, 0xca, 0x9f, 0x60, 0x71, 0x47, 0x75, 0x28, 0xf1, 0x0a, 0x56, 0xa5, 0x31, 0x64, - 0x5e, 0xa2, 0xb4, 0x76, 0x75, 0x2b, 0x98, 0x9c, 0xab, 0x44, 0x2f, 0x23, 0x7e, 0x1c, 0x28, 0x5e, - 0x02, 0xf7, 0x9b, 0x89, 0x35, 0x8b, 0x56, 0xea, 0x37, 0x07, 0x25, 0xff, 0x9c, 0xc1, 0xf9, 0xfd, - 0x5f, 0x08, 0x2a, 0x58, 0x0d, 0xf2, 0x33, 0xf9, 0xd6, 0xba, 0x46, 0x2c, 0x24, 0x53, 0x89, 0xfe, - 0x8f, 0x31, 0x07, 0x5e, 0xbe, 0x51, 0x13, 0x06, 0xed, 0x34, 0x6a, 0x47, 0x0c, 0x2f, 0x20, 0xd9, - 0xbe, 0x57, 0xb5, 0x5d, 0x3f, 0xd0, 0x4e, 0x30, 0xc9, 0x14, 0xd7, 0x23, 0x40, 0x09, 0x69, 0x6d, - 0xdb, 0x40, 0xcd, 0x8d, 0x31, 0x7e, 0x7f, 0x1a, 0xd7, 0x53, 0xd4, 0x67, 0xb8, 0xaa, 0x25, 0xff, - 0x41, 0xa6, 0x07, 0xe2, 0x24, 0x7e, 0x71, 0xc4, 0x62, 0xc6, 0xa1, 0xde, 0x3c, 0xd6, 0x1b, 0x01, - 0x2a, 0x58, 0x98, 0x7e, 0x31, 0x71, 0x26, 0x99, 0x4a, 0xaf, 0xb1, 0x98, 0xcc, 0x59, 0xc4, 0x2d, - 0xf5, 0x5e, 0xb8, 0xe5, 0x5f, 0x5d, 0xc6, 0xbe, 0xbb, 0x8c, 0xfd, 0x74, 0x19, 0xfb, 0x0d, 0x00, - 0x00, 0xff, 0xff, 0xa1, 0x77, 0x03, 0x42, 0x87, 0x01, 0x00, 0x00, + // 275 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xb1, 0x4e, 0xf3, 0x30, + 0x14, 0x85, 0xe5, 0xf6, 0xef, 0x4f, 0x7b, 0x13, 0xb5, 0xd2, 0x9d, 0x3c, 0xa0, 0x28, 0x64, 0xc1, + 0x53, 0x06, 0xde, 0x00, 0xc4, 0x82, 0x58, 0x90, 0x2b, 0xb1, 0xa2, 0xa4, 0xbe, 0x44, 0x96, 0x52, + 0xbb, 0x72, 0x5c, 0xa4, 0xbe, 0x17, 0x0f, 0xc1, 0xc8, 0x23, 0xa0, 0x3c, 0x09, 0x8a, 0x4b, 0x9a, + 0x14, 0x31, 0xfa, 0xf3, 0x27, 0x1f, 0x9f, 0x03, 0x4b, 0xad, 0xc8, 0x78, 0xfd, 0x7a, 0xc8, 0x77, + 0xce, 0x7a, 0x8b, 0xd1, 0x70, 0x2e, 0xb3, 0x35, 0xcc, 0xee, 0xa9, 0xf6, 0x05, 0x5e, 0xc3, 0xaa, + 0x50, 0x8a, 0xd4, 0x4b, 0x90, 0x36, 0xb6, 0x6e, 0x38, 0x4b, 0xa7, 0x62, 0x21, 0x97, 0x01, 0x3f, + 0xf5, 0x14, 0xaf, 0x20, 0x76, 0xdb, 0x91, 0x35, 0x09, 0x56, 0xe4, 0xb6, 0x27, 0x25, 0x7b, 0x9f, + 0xc0, 0xfc, 0xe1, 0x27, 0x04, 0x05, 0xac, 0x7a, 0xf9, 0x99, 0x5c, 0xa3, 0xad, 0xe1, 0xb3, 0x94, + 0x89, 0x85, 0xfc, 0x8d, 0x31, 0x83, 0xb8, 0xa8, 0xc8, 0xf8, 0x5e, 0xfb, 0x1f, 0xb4, 0x33, 0x86, + 0x97, 0xb0, 0xd8, 0xed, 0xcb, 0x5a, 0x6f, 0x1e, 0xe9, 0xc0, 0x59, 0xca, 0x44, 0x2c, 0x07, 0x80, + 0x29, 0x44, 0xb5, 0x6e, 0x3c, 0x99, 0x5b, 0xa5, 0xdc, 0xf1, 0x6b, 0xb1, 0x1c, 0xa3, 0x2e, 0xc3, + 0x96, 0x0d, 0xb9, 0x37, 0x52, 0x1d, 0xe0, 0xff, 0xc2, 0x13, 0x67, 0x2c, 0x64, 0x9c, 0xea, 0x4d, + 0x43, 0xbd, 0x01, 0xa0, 0x80, 0x99, 0xea, 0x16, 0xe3, 0x17, 0x29, 0x13, 0xd1, 0x0d, 0xe6, 0xa3, + 0x39, 0xf3, 0xb0, 0xa5, 0x3c, 0x0a, 0x98, 0x03, 0x36, 0xba, 0x32, 0xa4, 0xa4, 0xdd, 0x7b, 0x6d, + 0xaa, 0xb5, 0x2f, 0x3c, 0xf1, 0x79, 0x48, 0xfc, 0xe3, 0xe6, 0x2e, 0xfe, 0x68, 0x13, 0xf6, 0xd9, + 0x26, 0xec, 0xab, 0x4d, 0xd8, 0x77, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7b, 0x1e, 0x66, 0x03, 0xb7, + 0x01, 0x00, 0x00, } func (m *Delta) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) + n, err := m.MarshalTo(dAtA) if err != nil { return nil, err } @@ -224,44 +238,50 @@ func (m *Delta) Marshal() (dAtA []byte, err error) { } func (m *Delta) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Delta) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) + var i int _ = i var l int _ = l - if m.XXX_unrecognized != nil { - i -= len(m.XXX_unrecognized) - copy(dAtA[i:], m.XXX_unrecognized) + if len(m.AddedProtocols) > 0 { + for _, s := range m.AddedProtocols { + dAtA[i] = 0xa + i++ + l = len(s) + for l >= 1<<7 { + dAtA[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + dAtA[i] = uint8(l) + i++ + i += copy(dAtA[i:], s) + } } if len(m.RmProtocols) > 0 { - for iNdEx := len(m.RmProtocols) - 1; iNdEx >= 0; iNdEx-- { - i -= len(m.RmProtocols[iNdEx]) - copy(dAtA[i:], m.RmProtocols[iNdEx]) - i = encodeVarintIdentify(dAtA, i, uint64(len(m.RmProtocols[iNdEx]))) - i-- + for _, s := range m.RmProtocols { dAtA[i] = 0x12 + i++ + l = len(s) + for l >= 1<<7 { + dAtA[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + dAtA[i] = uint8(l) + i++ + i += copy(dAtA[i:], s) } } - if len(m.AddedProtocols) > 0 { - for iNdEx := len(m.AddedProtocols) - 1; iNdEx >= 0; iNdEx-- { - i -= len(m.AddedProtocols[iNdEx]) - copy(dAtA[i:], m.AddedProtocols[iNdEx]) - i = encodeVarintIdentify(dAtA, i, uint64(len(m.AddedProtocols[iNdEx]))) - i-- - dAtA[i] = 0xa - } + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) } - return len(dAtA) - i, nil + return i, nil } func (m *Identify) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) + n, err := m.MarshalTo(dAtA) if err != nil { return nil, err } @@ -269,90 +289,87 @@ func (m *Identify) Marshal() (dAtA []byte, err error) { } func (m *Identify) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Identify) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) + var i int _ = i var l int _ = l - if m.XXX_unrecognized != nil { - i -= len(m.XXX_unrecognized) - copy(dAtA[i:], m.XXX_unrecognized) + if m.PublicKey != nil { + dAtA[i] = 0xa + i++ + i = encodeVarintIdentify(dAtA, i, uint64(len(m.PublicKey))) + i += copy(dAtA[i:], m.PublicKey) } - if m.Delta != nil { - { - size, err := m.Delta.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintIdentify(dAtA, i, uint64(size)) + if len(m.ListenAddrs) > 0 { + for _, b := range m.ListenAddrs { + dAtA[i] = 0x12 + i++ + i = encodeVarintIdentify(dAtA, i, uint64(len(b))) + i += copy(dAtA[i:], b) } - i-- - dAtA[i] = 0x3a } - if m.AgentVersion != nil { - i -= len(*m.AgentVersion) - copy(dAtA[i:], *m.AgentVersion) - i = encodeVarintIdentify(dAtA, i, uint64(len(*m.AgentVersion))) - i-- - dAtA[i] = 0x32 + if len(m.Protocols) > 0 { + for _, s := range m.Protocols { + dAtA[i] = 0x1a + i++ + l = len(s) + for l >= 1<<7 { + dAtA[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + dAtA[i] = uint8(l) + i++ + i += copy(dAtA[i:], s) + } + } + if m.ObservedAddr != nil { + dAtA[i] = 0x22 + i++ + i = encodeVarintIdentify(dAtA, i, uint64(len(m.ObservedAddr))) + i += copy(dAtA[i:], m.ObservedAddr) } if m.ProtocolVersion != nil { - i -= len(*m.ProtocolVersion) - copy(dAtA[i:], *m.ProtocolVersion) - i = encodeVarintIdentify(dAtA, i, uint64(len(*m.ProtocolVersion))) - i-- dAtA[i] = 0x2a + i++ + i = encodeVarintIdentify(dAtA, i, uint64(len(*m.ProtocolVersion))) + i += copy(dAtA[i:], *m.ProtocolVersion) } - if m.ObservedAddr != nil { - i -= len(m.ObservedAddr) - copy(dAtA[i:], m.ObservedAddr) - i = encodeVarintIdentify(dAtA, i, uint64(len(m.ObservedAddr))) - i-- - dAtA[i] = 0x22 + if m.AgentVersion != nil { + dAtA[i] = 0x32 + i++ + i = encodeVarintIdentify(dAtA, i, uint64(len(*m.AgentVersion))) + i += copy(dAtA[i:], *m.AgentVersion) } - if len(m.Protocols) > 0 { - for iNdEx := len(m.Protocols) - 1; iNdEx >= 0; iNdEx-- { - i -= len(m.Protocols[iNdEx]) - copy(dAtA[i:], m.Protocols[iNdEx]) - i = encodeVarintIdentify(dAtA, i, uint64(len(m.Protocols[iNdEx]))) - i-- - dAtA[i] = 0x1a + if m.Delta != nil { + dAtA[i] = 0x3a + i++ + i = encodeVarintIdentify(dAtA, i, uint64(m.Delta.Size())) + n1, err := m.Delta.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err } + i += n1 } - if len(m.ListenAddrs) > 0 { - for iNdEx := len(m.ListenAddrs) - 1; iNdEx >= 0; iNdEx-- { - i -= len(m.ListenAddrs[iNdEx]) - copy(dAtA[i:], m.ListenAddrs[iNdEx]) - i = encodeVarintIdentify(dAtA, i, uint64(len(m.ListenAddrs[iNdEx]))) - i-- - dAtA[i] = 0x12 - } + if m.SignedRoutingState != nil { + dAtA[i] = 0x42 + i++ + i = encodeVarintIdentify(dAtA, i, uint64(len(m.SignedRoutingState))) + i += copy(dAtA[i:], m.SignedRoutingState) } - if m.PublicKey != nil { - i -= len(m.PublicKey) - copy(dAtA[i:], m.PublicKey) - i = encodeVarintIdentify(dAtA, i, uint64(len(m.PublicKey))) - i-- - dAtA[i] = 0xa + if m.XXX_unrecognized != nil { + i += copy(dAtA[i:], m.XXX_unrecognized) } - return len(dAtA) - i, nil + return i, nil } func encodeVarintIdentify(dAtA []byte, offset int, v uint64) int { - offset -= sovIdentify(v) - base := offset for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) - return base + return offset + 1 } func (m *Delta) Size() (n int) { if m == nil { @@ -416,6 +433,10 @@ func (m *Identify) Size() (n int) { l = m.Delta.Size() n += 1 + l + sovIdentify(uint64(l)) } + if m.SignedRoutingState != nil { + l = len(m.SignedRoutingState) + n += 1 + l + sovIdentify(uint64(l)) + } if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } @@ -423,7 +444,14 @@ func (m *Identify) Size() (n int) { } func sovIdentify(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 + for { + n++ + x >>= 7 + if x == 0 { + break + } + } + return n } func sozIdentify(x uint64) (n int) { return sovIdentify(uint64((x << 1) ^ uint64((int64(x) >> 63)))) @@ -809,6 +837,40 @@ func (m *Identify) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 8: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SignedRoutingState", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowIdentify + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthIdentify + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthIdentify + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.SignedRoutingState = append(m.SignedRoutingState[:0], dAtA[iNdEx:postIndex]...) + if m.SignedRoutingState == nil { + m.SignedRoutingState = []byte{} + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipIdentify(dAtA[iNdEx:]) @@ -837,7 +899,6 @@ func (m *Identify) Unmarshal(dAtA []byte) error { func skipIdentify(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 - depth := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { @@ -869,8 +930,10 @@ func skipIdentify(dAtA []byte) (n int, err error) { break } } + return iNdEx, nil case 1: iNdEx += 8 + return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { @@ -894,27 +957,52 @@ func skipIdentify(dAtA []byte) (n int, err error) { if iNdEx < 0 { return 0, ErrInvalidLengthIdentify } + return iNdEx, nil case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupIdentify + for { + var innerWire uint64 + var start int = iNdEx + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowIdentify + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + innerWire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + innerWireType := int(innerWire & 0x7) + if innerWireType == 4 { + break + } + next, err := skipIdentify(dAtA[start:]) + if err != nil { + return 0, err + } + iNdEx = start + next + if iNdEx < 0 { + return 0, ErrInvalidLengthIdentify + } } - depth-- + return iNdEx, nil + case 4: + return iNdEx, nil case 5: iNdEx += 4 + return iNdEx, nil default: return 0, fmt.Errorf("proto: illegal wireType %d", wireType) } - if depth == 0 { - return iNdEx, nil - } } - return 0, io.ErrUnexpectedEOF + panic("unreachable") } var ( - ErrInvalidLengthIdentify = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowIdentify = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupIdentify = fmt.Errorf("proto: unexpected end of group") + ErrInvalidLengthIdentify = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowIdentify = fmt.Errorf("proto: integer overflow") ) diff --git a/p2p/protocol/identify/pb/identify.proto b/p2p/protocol/identify/pb/identify.proto index 44283870f1..620671d8ef 100644 --- a/p2p/protocol/identify/pb/identify.proto +++ b/p2p/protocol/identify/pb/identify.proto @@ -36,4 +36,11 @@ message Identify { // a delta update is incompatible with everything else. If this field is included, none of the others can appear. optional Delta delta = 7; + + // signedRoutingState contains a serialized SignedEnvelope containing a RoutingStateRecord, + // signed by the sending node. It contains the same addresses as the listenAddrs field, but + // in a form that lets us share authenticated addrs with other peers. + // see github.com/libp2p/go-libp2p-core/crypto/pb/envelope.proto and + // github.com/libp2p/go-libp2p-core/routing/pb/routing_state.proto for message definitions. + optional bytes signedRoutingState = 8; } From cfd71499f3ace20cbb3efed60bba80d8f530e2be Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 20 Dec 2019 16:26:09 -0500 Subject: [PATCH 02/43] point -peerstore dep at PR branch --- go.mod | 2 +- go.sum | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index b856c16b71..7bc50ce1c4 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/libp2p/go-libp2p-mplex v0.2.1 github.com/libp2p/go-libp2p-nat v0.0.5 github.com/libp2p/go-libp2p-netutil v0.1.0 - github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191122180640-0f9538856f85 + github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191121180145-50b0a8d4d5d1 github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-swarm v0.2.2 github.com/libp2p/go-libp2p-testing v0.1.1 diff --git a/go.sum b/go.sum index 21ca279db5..c70ba3689e 100644 --- a/go.sum +++ b/go.sum @@ -151,8 +151,8 @@ github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCTh github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= github.com/libp2p/go-libp2p-peerstore v0.1.3/go.mod h1:BJ9sHlm59/80oSkpWgr1MyY1ciXAXV397W6h1GH/uKI= -github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191122180640-0f9538856f85 h1:VxnYkz4A0AbbdUF6pvjBdvYKKjSrybcSXnluxJlKziQ= -github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191122180640-0f9538856f85/go.mod h1:JZoEvuUQYNg+eiYQHXN954IMzdAd/bmkjiiMy7VE6TA= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191121180145-50b0a8d4d5d1 h1:gVAbf4Q/MSlBPG5ufPxtiJglsoMtPjyICuASqah6tH4= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191121180145-50b0a8d4d5d1/go.mod h1:JZoEvuUQYNg+eiYQHXN954IMzdAd/bmkjiiMy7VE6TA= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1 h1:eNWbJTdyPA7NxhP7J3c5lT97DC5d+u+IldkgCYFTPVA= @@ -260,7 +260,6 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS github.com/multiformats/go-multistream v0.1.1 h1:JlAdpIFhBhGRLxe9W6Om0w++Gd6KMWoFPZL/dEnm9nI= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= -github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= From daaef634105f03f4d5f4c4909471c260fb504e14 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 22 Nov 2019 12:28:33 -0600 Subject: [PATCH 03/43] fix error handling --- p2p/protocol/identify/id.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index a8cbbe7d66..1246b491ef 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -321,8 +321,12 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { // send it along with the unsigned addrs signedState, err := host.SignedRoutingStateFromHost(ids.Host) if err != nil { + log.Warningf("error generating signed routing state: %v", err) + } else { envelopeBytes, err := signedState.Marshal() if err != nil { + log.Warningf("error marshaling signed routing state: %v", err) + } else { mes.SignedRoutingState = envelopeBytes } } @@ -389,7 +393,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // state record routingState, err := signedRoutingStateFromMsg(mes) if err != nil { - log.Warning("error getting routing state from Identify message: %v", err) + log.Warningf("error getting routing state from Identify message: %v", err) } // Extend the TTLs on the known (probably) good addresses. From 2db11a55dbceb93541a8f8e6024221a00f8b996f Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 25 Nov 2019 08:59:32 -0600 Subject: [PATCH 04/43] test certified addr exchange during identify --- p2p/protocol/identify/id_test.go | 36 +++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 5c422a9092..afde9eb7af 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -2,6 +2,7 @@ package identify_test import ( "context" + "fmt" "reflect" "sort" "testing" @@ -41,6 +42,9 @@ func subtestIDService(t *testing.T) { testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing + // the forgetMe addr represents an address for h1 that h2 has learned out of band + // (not via identify protocol). Shortly after the identify exchange, it will be + // forgotten and replaced by the addrs h1 sends during identify forgetMe, _ := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/1234") h2.Peerstore().AddAddr(h1p, forgetMe, peerstore.RecentlyConnectedAddrTTL) @@ -62,6 +66,7 @@ func subtestIDService(t *testing.T) { // what we should see now is that both peers know about each others listen addresses. t.Log("test peer1 has peer2 addrs correctly") testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them + testHasCertifiedAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // should have signed addrs also testHasProtocolVersions(t, h1, h2p) testHasPublicKey(t, h1, h2p, h2.Peerstore().PubKey(h2p)) // h1 should have h2's public key @@ -78,7 +83,11 @@ func subtestIDService(t *testing.T) { // and the protocol versions. t.Log("test peer2 has peer1 addrs correctly") + // h2 still has the forgetMe addr for h1, since it's not deleted immediately testKnowsAddrs(t, h2, h1p, addrs) // has them + // the forgetMe addr was not certified by h1, so h2's CertifiedAddrs for + // h1 will only include the ones sent during the exchange + testHasCertifiedAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) testHasProtocolVersions(t, h2, h1p) testHasPublicKey(t, h2, h1p, h1.Peerstore().PubKey(h1p)) // h1 should have h2's public key @@ -89,26 +98,43 @@ func subtestIDService(t *testing.T) { t.Fatal("should have no connections") } + // addresses don't immediately expire on disconnect, so we should still have them testKnowsAddrs(t, h2, h1p, addrs) testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) + testHasCertifiedAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) + testHasCertifiedAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) + // the forgetMe addr had its TTL reduced during the identify exchange, + // since it was not present in the exchanged addrs, + // so it will be forgotten first time.Sleep(500 * time.Millisecond) - - // Forget the first one. testKnowsAddrs(t, h2, h1p, addrs[:len(addrs)-1]) + // the remaining addrs had their TTLs reduced on disconnect, and + // will be forgotten soon after time.Sleep(1 * time.Second) - - // Forget the rest. testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) + testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) + testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) + } func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { t.Helper() actual := h.Peerstore().Addrs(p) + checkAddrs(t, expected, actual, fmt.Sprintf("%s did not have addr for %s", h.ID(), p)) +} +func testHasCertifiedAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { + t.Helper() + actual := h.Peerstore().CertifiedAddrs(p) + checkAddrs(t, expected, actual, fmt.Sprintf("%s did not have certified addr for %s", h.ID(), p)) +} + +func checkAddrs(t *testing.T, expected, actual []ma.Multiaddr, msg string) { + t.Helper() if len(actual) != len(expected) { t.Errorf("expected: %s", expected) t.Errorf("actual: %s", actual) @@ -121,7 +147,7 @@ func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiadd } for _, addr := range expected { if _, found := have[addr.String()]; !found { - t.Errorf("%s did not have addr for %s: %s", h.ID(), p, addr) + t.Errorf("%s: %s", msg, addr) } } } From c999bd059c2b1109c0f7fab15745e3045252b0ee Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 25 Nov 2019 09:16:24 -0600 Subject: [PATCH 05/43] intellij was supposed to run go fmt for me. i feel betrayed --- p2p/protocol/identify/id_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index afde9eb7af..6e881a397a 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -65,7 +65,7 @@ func subtestIDService(t *testing.T) { // the IDService should be opened automatically, by the network. // what we should see now is that both peers know about each others listen addresses. t.Log("test peer1 has peer2 addrs correctly") - testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them + testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them testHasCertifiedAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // should have signed addrs also testHasProtocolVersions(t, h1, h2p) testHasPublicKey(t, h1, h2p, h2.Peerstore().PubKey(h2p)) // h1 should have h2's public key @@ -115,8 +115,8 @@ func subtestIDService(t *testing.T) { time.Sleep(1 * time.Second) testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) - testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) - testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) + testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) + testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) } From b632ebae3be20b1a9a6fb8674e28c73b746a9fc6 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Wed, 27 Nov 2019 12:01:25 -0500 Subject: [PATCH 06/43] add option to disable signed addrs for testing --- p2p/protocol/identify/id.go | 35 ++++++++++++++++---------- p2p/protocol/identify/id_test.go | 43 ++++++++++++++++++++++++++++++++ p2p/protocol/identify/opts.go | 12 ++++++++- 3 files changed, 76 insertions(+), 14 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 1246b491ef..1fe26c7a1d 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -88,6 +88,8 @@ type IDService struct { emitters struct { evtPeerProtocolsUpdated event.Emitter } + + useSignedAddrs bool } // NewIDService constructs a new *IDService and activates it by @@ -107,9 +109,10 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { Host: h, UserAgent: userAgent, - ctx: ctx, - currid: make(map[network.Conn]chan struct{}), - observedAddrs: NewObservedAddrSet(ctx), + ctx: ctx, + currid: make(map[network.Conn]chan struct{}), + observedAddrs: NewObservedAddrSet(ctx), + useSignedAddrs: !cfg.disableSignedAddrSupport, } // handle local protocol handler updates, and push deltas to peers. @@ -319,15 +322,17 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { // Generate a signed routing record containing our listen addresses and // send it along with the unsigned addrs - signedState, err := host.SignedRoutingStateFromHost(ids.Host) - if err != nil { - log.Warningf("error generating signed routing state: %v", err) - } else { - envelopeBytes, err := signedState.Marshal() + if ids.useSignedAddrs { + signedState, err := host.SignedRoutingStateFromHost(ids.Host) if err != nil { - log.Warningf("error marshaling signed routing state: %v", err) + log.Warningf("error generating signed routing state: %v", err) } else { - mes.SignedRoutingState = envelopeBytes + envelopeBytes, err := signedState.Marshal() + if err != nil { + log.Warningf("error marshaling signed routing state: %v", err) + } else { + mes.SignedRoutingState = envelopeBytes + } } } @@ -391,9 +396,13 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // add certified addresses for the peer, if they sent us a signed routing // state record - routingState, err := signedRoutingStateFromMsg(mes) - if err != nil { - log.Warningf("error getting routing state from Identify message: %v", err) + var routingState *routing.SignedRoutingState + if ids.useSignedAddrs { + var err error + routingState, err = signedRoutingStateFromMsg(mes) + if err != nil { + log.Warningf("error getting routing state from Identify message: %v", err) + } } // Extend the TTLs on the known (probably) good addresses. diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 6e881a397a..dfc125409a 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -431,3 +431,46 @@ func TestUserAgent(t *testing.T) { t.Errorf("expected agent version %q, got %q", "bar", av) } } + +// make sure that we still use the unsigned listen addresses if the remote peer +// does not send us a signed address record +func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + h1 := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h2 := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + defer h2.Close() + defer h1.Close() + + h1p := h1.ID() + h2p := h2.ID() + ids1 := identify.NewIDService(ctx, h1) + ids2 := identify.NewIDService(ctx, h2, identify.DisableSignedAddrSupport()) + + h2pi := h2.Peerstore().PeerInfo(h2p) + if err := h1.Connect(ctx, h2pi); err != nil { + t.Fatal(err) + } + + h1t2c := h1.Network().ConnsToPeer(h2p) + if len(h1t2c) == 0 { + t.Fatal("should have a conn here") + } + + ids1.IdentifyConn(h1t2c[0]) + + // the IDService should be opened automatically, by the network. + // what we should see now is that both peers know about each others listen addresses. + t.Log("test peer1 has peer2 addrs correctly") + testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them + testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) // should not have signed addrs + + c := h2.Network().ConnsToPeer(h1.ID()) + if len(c) < 1 { + t.Fatal("should have connection by now at least.") + } + ids2.IdentifyConn(c[0]) + testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them + testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) // no signed addrs +} diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index 670b213b71..cddd404528 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -1,7 +1,8 @@ package identify type config struct { - userAgent string + userAgent string + disableSignedAddrSupport bool } // Option is an option function for identify. @@ -13,3 +14,12 @@ func UserAgent(ua string) Option { cfg.userAgent = ua } } + +// DisableSignedAddrSupport prevents the identify service from sending or parsing +// routing.SignedRoutingState messages during the exchange. Used for testing +// compatibility with older versions that do not support signed addresses. +func DisableSignedAddrSupport() Option { + return func(cfg *config) { + cfg.disableSignedAddrSupport = true + } +} From 894861076bd29fa0469dca62ef351472c04b07bb Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Wed, 27 Nov 2019 12:33:54 -0500 Subject: [PATCH 07/43] more explicit name for option to disable signed addrs --- p2p/protocol/identify/id_test.go | 2 +- p2p/protocol/identify/opts.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index dfc125409a..f31ab11783 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -446,7 +446,7 @@ func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { h1p := h1.ID() h2p := h2.ID() ids1 := identify.NewIDService(ctx, h1) - ids2 := identify.NewIDService(ctx, h2, identify.DisableSignedAddrSupport()) + ids2 := identify.NewIDService(ctx, h2, identify.DisableSignedAddrSupportForTesting()) h2pi := h2.Peerstore().PeerInfo(h2p) if err := h1.Connect(ctx, h2pi); err != nil { diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index cddd404528..3a1d199d83 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -15,10 +15,11 @@ func UserAgent(ua string) Option { } } -// DisableSignedAddrSupport prevents the identify service from sending or parsing +// DisableSignedAddrSupportForTesting prevents the identify service from sending or parsing // routing.SignedRoutingState messages during the exchange. Used for testing // compatibility with older versions that do not support signed addresses. -func DisableSignedAddrSupport() Option { +// Do not use in production! +func DisableSignedAddrSupportForTesting() Option { return func(cfg *config) { cfg.disableSignedAddrSupport = true } From c97dd744432d73754d65fed2935c20cb945b4d8f Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 20 Dec 2019 14:07:40 -0500 Subject: [PATCH 08/43] add routingStateManager --- p2p/protocol/identify/id.go | 34 ++++---- p2p/protocol/identify/id_test.go | 4 +- p2p/protocol/identify/opts.go | 9 +- p2p/protocol/identify/routingstate.go | 116 ++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 p2p/protocol/identify/routingstate.go diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 1fe26c7a1d..a16117ec8d 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -84,12 +84,15 @@ type IDService struct { // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet - subscription event.Subscription - emitters struct { + routingStateManager *routingStateManager + useSignedAddrs bool + + subscriptions struct { + localProtocolsUpdated event.Subscription + } + emitters struct { evtPeerProtocolsUpdated event.Emitter } - - useSignedAddrs bool } // NewIDService constructs a new *IDService and activates it by @@ -117,7 +120,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { // handle local protocol handler updates, and push deltas to peers. var err error - s.subscription, err = h.EventBus().Subscribe(&event.EvtLocalProtocolsUpdated{}, eventbus.BufSize(128)) + s.subscriptions.localProtocolsUpdated, err = h.EventBus().Subscribe(&event.EvtLocalProtocolsUpdated{}, eventbus.BufSize(128)) if err != nil { log.Warningf("identify service not subscribed to local protocol handlers updates; err: %s", err) } else { @@ -129,6 +132,11 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { log.Warningf("identify service not emitting peer protocol updates; err: %s", err) } + s.routingStateManager, err = NewRoutingStateManager(ctx, h, cfg.includeLocalAddrsInRoutingState) + if err != nil { + log.Warnf("identify service not tracking routing state changes; err: %s", err) + } + h.SetStreamHandler(ID, s.requestHandler) h.SetStreamHandler(IDPush, s.pushHandler) h.SetStreamHandler(IDDelta, s.deltaHandler) @@ -137,7 +145,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } func (ids *IDService) handleEvents() { - sub := ids.subscription + sub := ids.subscriptions.localProtocolsUpdated defer func() { _ = sub.Close() // drain the channel. @@ -322,17 +330,13 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { // Generate a signed routing record containing our listen addresses and // send it along with the unsigned addrs - if ids.useSignedAddrs { - signedState, err := host.SignedRoutingStateFromHost(ids.Host) + signedState := ids.routingStateManager.LatestState() + if ids.useSignedAddrs && signedState != nil { + envelopeBytes, err := signedState.Marshal() if err != nil { - log.Warningf("error generating signed routing state: %v", err) + log.Warningf("error marshaling signed routing state: %v", err) } else { - envelopeBytes, err := signedState.Marshal() - if err != nil { - log.Warningf("error marshaling signed routing state: %v", err) - } else { - mes.SignedRoutingState = envelopeBytes - } + mes.SignedRoutingState = envelopeBytes } } diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index f31ab11783..25792a134f 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -36,8 +36,8 @@ func subtestIDService(t *testing.T) { h1p := h1.ID() h2p := h2.ID() - ids1 := identify.NewIDService(ctx, h1) - ids2 := identify.NewIDService(ctx, h2) + ids1 := identify.NewIDService(ctx, h1, identify.IncludeLocalAddrsInRoutingState) + ids2 := identify.NewIDService(ctx, h2, identify.IncludeLocalAddrsInRoutingState) testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index 3a1d199d83..ccb813f250 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -1,8 +1,9 @@ package identify type config struct { - userAgent string - disableSignedAddrSupport bool + userAgent string + disableSignedAddrSupport bool + includeLocalAddrsInRoutingState bool } // Option is an option function for identify. @@ -24,3 +25,7 @@ func DisableSignedAddrSupportForTesting() Option { cfg.disableSignedAddrSupport = true } } + +func IncludeLocalAddrsInRoutingState(cfg *config) { + cfg.includeLocalAddrsInRoutingState = true +} diff --git a/p2p/protocol/identify/routingstate.go b/p2p/protocol/identify/routingstate.go new file mode 100644 index 0000000000..92be01fe88 --- /dev/null +++ b/p2p/protocol/identify/routingstate.go @@ -0,0 +1,116 @@ +package identify + +import ( + "context" + "github.com/libp2p/go-eventbus" + "github.com/libp2p/go-libp2p-core/event" + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/routing" + "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr-net" +) + +// routingStateManager creates new SignedRoutingState records that can +// be shared with other peers to inform them of our listen addresses in +// a secure and authenticated way. +// +// New signed records are created in response to EvtLocalAddressesUpdated events, +// and are emitted in EvtLocalRoutingStateUpdated events. +type routingStateManager struct { + latestState *routing.SignedRoutingState + + ctx context.Context + host host.Host + includeLocalAddrs bool + subscriptions struct { + localAddrsUpdated event.Subscription + } + emitters struct { + evtLocalRoutingStateUpdated event.Emitter + } +} + +func NewRoutingStateManager(ctx context.Context, host host.Host, includeLocalAddrs bool) (*routingStateManager, error) { + m := &routingStateManager{ + ctx: ctx, + host: host, + includeLocalAddrs: includeLocalAddrs, + } + bus := host.EventBus() + var err error + m.subscriptions.localAddrsUpdated, err = bus.Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) + if err != nil { + return nil, err + } + m.emitters.evtLocalRoutingStateUpdated, err = bus.Emitter(&event.EvtLocalPeerRoutingStateUpdated{}, eventbus.Stateful) + if err != nil { + return nil, err + } + + m.updateRoutingState() + go m.handleEvents() + return m, nil +} + +func (m *routingStateManager) LatestState() *routing.SignedRoutingState { + if m == nil { + return nil + } + return m.latestState +} + +func (m *routingStateManager) handleEvents() { + sub := m.subscriptions.localAddrsUpdated + defer func() { + _ = sub.Close() + // drain the channel. + for range sub.Out() { + } + }() + + for { + select { + case _, more := <-sub.Out(): + if !more { + return + } + m.updateRoutingState() + case <-m.ctx.Done(): + return + } + } +} + +func (m *routingStateManager) updateRoutingState() { + state, err := m.makeRoutingState() + if err != nil { + log.Warnf("error creating routing state record: %v", err) + return + } + m.latestState = state + stateEvt := event.EvtLocalPeerRoutingStateUpdated{State: m.latestState} + err = m.emitters.evtLocalRoutingStateUpdated.Emit(stateEvt) + if err != nil { + log.Warnf("error emitting routing state event: %v", err) + } +} + +func (m *routingStateManager) makeRoutingState() (*routing.SignedRoutingState, error) { + privKey := m.host.Peerstore().PrivKey(m.host.ID()) + if privKey == nil { + log.Warn("error making routing state: unable to find host's private key in peerstore") + } + + var addrs []multiaddr.Multiaddr + if m.includeLocalAddrs { + addrs = m.host.Addrs() + } else { + for _, a := range m.host.Addrs() { + if manet.IsPublicAddr(a) { + addrs = append(addrs, a) + } + } + } + + return routing.MakeSignedRoutingState(privKey, addrs) +} From de97dd1f0f4ce453b2943855e681887b1ef97cab Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 20 Dec 2019 16:26:50 -0500 Subject: [PATCH 09/43] use event bus to trigger identify/push This changes BasicHost to emit EvtLocalAddressesUpdated events when it detects changes to the local listen addresses instead of directly calling IDService.Push. These events are consumed by a new routingStateManager component, which build a new SignedRoutingState record and sends it on the event bus in an EvtLocalRoutingStateUpdated event. Finally, the identify service listens for the updated routing state events and triggers an identify/push. --- go.mod | 4 +-- go.sum | 19 ++++++++----- p2p/host/basic/basic_host.go | 54 ++++++++++++++++++++++-------------- p2p/host/relay/autorelay.go | 2 +- p2p/protocol/identify/id.go | 25 +++++++++++++---- 5 files changed, 68 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 7bc50ce1c4..c5080486df 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ require ( github.com/ipfs/go-cid v0.0.5 github.com/ipfs/go-detect-race v0.0.1 github.com/ipfs/go-ipfs-util v0.0.1 - github.com/ipfs/go-log v0.0.1 + github.com/ipfs/go-log v1.0.0 github.com/jbenet/go-cienv v0.1.0 github.com/jbenet/goprocess v0.1.3 github.com/libp2p/go-conn-security-multistream v0.1.0 @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211501-15623e878c25 + github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211544-b57c5023fbf9 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 diff --git a/go.sum b/go.sum index c70ba3689e..39f80b01c7 100644 --- a/go.sum +++ b/go.sum @@ -82,8 +82,9 @@ github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZ github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1 h1:Wz9bL2wB2YBJqggkA4dD7oSmqB4cAnpNbGrlHJulv50= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= -github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= +github.com/ipfs/go-log v1.0.0 h1:BW3LQIiZzpNyolt84yvKNCd3FU+AK4VDw1hnHR+1aiI= +github.com/ipfs/go-log v1.0.0/go.mod h1:JO7RzlMK6rA+CIxFMLOuB6Wf5b81GDiKElL7UPSIKjA= github.com/jackpal/gateway v1.0.5 h1:qzXWUJfuMdlLMtt0a3Dgt+xkWQiA5itDEITVJtuSwMc= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA= @@ -134,8 +135,8 @@ github.com/libp2p/go-libp2p-core v0.2.2/go.mod h1:8fcwTbsG2B+lTgRJ1ICZtiM5GWCWZV github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g= github.com/libp2p/go-libp2p-core v0.2.6-0.20191121175514-5fa975301271/go.mod h1:xDyprN8hkMpX27XQ1bBnYtuSuaCUFvKm+Q6gltHZCHE= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= -github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211501-15623e878c25 h1:Yzu98yHyavbHX1udrzM0xMN/06osM+6OKtKlzTuBMVQ= -github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211501-15623e878c25/go.mod h1:3PDCxmuTTy+gyLxQz7Tf7lOf36n7WUECOJ9CPWx0S6A= +github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211544-b57c5023fbf9 h1:Doq/tZH8j+OGxNwRmYJoLkyLE/WrhoU67vcV1YLO2WA= +github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211544-b57c5023fbf9/go.mod h1:KJbT3ekTPccG9QUSa5vc/0QVZXPE9+3TVlyqESRQic0= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -204,10 +205,8 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/miekg/dns v1.1.12 h1:WMhc1ik4LNkTg8U9l3hI1LvxKmIL+f1+WV/SZtCbDDA= @@ -267,8 +266,9 @@ github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -296,7 +296,6 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -github.com/whyrusleeping/go-logging v0.0.1 h1:fwpzlmT0kRC/Fmd0MdmGgJG/CXIZ6gFq46FQZjprUcc= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= github.com/whyrusleeping/mafmt v1.2.8 h1:TCghSl5kkwEE0j+sU/gudyhVMRlpBin8fMBBHg59EbA= github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA= @@ -310,6 +309,12 @@ go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2 h1:75k/FF0Q2YM8QYo07VPddOLBslDt1MZOdEslOHvmzAs= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index f8bf279cff..973631f083 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -88,6 +88,7 @@ type BasicHost struct { lastAddrs []ma.Multiaddr emitters struct { evtLocalProtocolsUpdated event.Emitter + evtLocalAddrsUpdated event.Emitter } } @@ -141,6 +142,9 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo if h.emitters.evtLocalProtocolsUpdated, err = h.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil { return nil, err } + if h.emitters.evtLocalAddrsUpdated, err = h.eventbus.Emitter(&event.EvtLocalAddressesUpdated{}); err != nil { + return nil, err + } h.proc = goprocessctx.WithContextAndTeardown(ctx, func() error { if h.natmgr != nil { @@ -150,6 +154,7 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo h.cmgr.Close() } _ = h.emitters.evtLocalProtocolsUpdated.Close() + _ = h.emitters.evtLocalAddrsUpdated.Close() return h.Network().Close() }) @@ -295,21 +300,29 @@ func (h *BasicHost) newStreamHandler(s network.Stream) { go handle(protoID, s) } -// PushIdentify pushes an identify update through the identify push protocol +// CheckForAddressChanges determines whether our listen addresses have recently +// changed and emits an EvtLocalAddressesUpdatedEvent if so. // Warning: this interface is unstable and may disappear in the future. -func (h *BasicHost) PushIdentify() { - push := false +func (h *BasicHost) CheckForAddressChanges() { + changed := false h.mx.Lock() addrs := h.Addrs() - if !sameAddrs(addrs, h.lastAddrs) { - push = true + added, removed := addrDiff(h.lastAddrs, addrs) + if len(added) != 0 || len(removed) != 0 { + changed = true h.lastAddrs = addrs } h.mx.Unlock() - if push { - h.ids.Push() + if changed { + err := h.emitters.evtLocalAddrsUpdated.Emit(event.EvtLocalAddressesUpdated{ + Added: added, + Removed: removed, + }) + if err != nil { + log.Warnf("error emitting event for updated addrs: %s", err) + } } } @@ -329,7 +342,7 @@ func (h *BasicHost) background(p goprocess.Process) { for { select { case <-ticker.C: - h.PushIdentify() + h.CheckForAddressChanges() case <-p.Closing(): return @@ -337,24 +350,23 @@ func (h *BasicHost) background(p goprocess.Process) { } } -func sameAddrs(a, b []ma.Multiaddr) bool { - if len(a) != len(b) { - return false - } +func addrDiff(old, new []ma.Multiaddr) (added, removed []ma.Multiaddr) { + oldmap := make(map[string]ma.Multiaddr, len(old)) - bmap := make(map[string]struct{}, len(b)) - for _, addr := range b { - bmap[string(addr.Bytes())] = struct{}{} + for _, addr := range old { + oldmap[string(addr.Bytes())] = addr } - - for _, addr := range a { - _, ok := bmap[string(addr.Bytes())] + for _, addr := range new { + _, ok := oldmap[string(addr.Bytes())] if !ok { - return false + added = append(added, addr) } + delete(oldmap, string(addr.Bytes())) } - - return true + for _, addr := range oldmap { + removed = append(removed, addr) + } + return added, removed } // ID returns the (local) peer.ID associated with this Host diff --git a/p2p/host/relay/autorelay.go b/p2p/host/relay/autorelay.go index 0c3f40b53f..484c7dbb9f 100644 --- a/p2p/host/relay/autorelay.go +++ b/p2p/host/relay/autorelay.go @@ -125,7 +125,7 @@ func (ar *AutoRelay) background(ctx context.Context) { ar.cachedAddrs = nil ar.mx.Unlock() push = false - ar.host.PushIdentify() + ar.host.CheckForAddressChanges() } select { diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index a16117ec8d..24a321c9b7 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -88,7 +88,8 @@ type IDService struct { useSignedAddrs bool subscriptions struct { - localProtocolsUpdated event.Subscription + localProtocolsUpdated event.Subscription + localRoutingStateUpdated event.Subscription } emitters struct { evtPeerProtocolsUpdated event.Emitter @@ -124,7 +125,13 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { if err != nil { log.Warningf("identify service not subscribed to local protocol handlers updates; err: %s", err) } else { - go s.handleEvents() + go s.handleEvents(s.subscriptions.localProtocolsUpdated, s.handleProtosChanged) + } + s.subscriptions.localRoutingStateUpdated, err = h.EventBus().Subscribe(&event.EvtLocalPeerRoutingStateUpdated{}, eventbus.BufSize(128)) + if err != nil { + log.Warnf("identify service not subscribed to local routing state changes; err: %s", err) + } else { + go s.handleEvents(s.subscriptions.localRoutingStateUpdated, s.handleRoutingStateUpdated) } s.emitters.evtPeerProtocolsUpdated, err = h.EventBus().Emitter(&event.EvtPeerProtocolsUpdated{}) @@ -144,8 +151,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { return s } -func (ids *IDService) handleEvents() { - sub := ids.subscriptions.localProtocolsUpdated +func (ids *IDService) handleEvents(sub event.Subscription, handler func(interface{})) { defer func() { _ = sub.Close() // drain the channel. @@ -159,13 +165,22 @@ func (ids *IDService) handleEvents() { if !more { return } - ids.fireProtocolDelta(evt.(event.EvtLocalProtocolsUpdated)) + handler(evt) case <-ids.ctx.Done(): return } } } +func (ids *IDService) handleProtosChanged(evt interface{}) { + ids.fireProtocolDelta(evt.(event.EvtLocalProtocolsUpdated)) +} + +func (ids *IDService) handleRoutingStateUpdated(evt interface{}) { + log.Debug("triggering push based on routing state updated event") + ids.Push() +} + // OwnObservedAddrs returns the addresses peers have reported we've dialed from func (ids *IDService) OwnObservedAddrs() []ma.Multiaddr { return ids.observedAddrs.Addrs() From 2b0a74e07a66d6af6bc287bcb5a1a1e517021e47 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 7 Jan 2020 12:53:42 -0500 Subject: [PATCH 10/43] update to reflect name changes in -core --- p2p/protocol/identify/id.go | 48 ++++++------- p2p/protocol/identify/id_test.go | 42 +++++++----- p2p/protocol/identify/pb/identify.pb.go | 67 +++++++++---------- p2p/protocol/identify/pb/identify.proto | 8 +-- .../{routingstate.go => peer_record.go} | 37 +++++----- 5 files changed, 106 insertions(+), 96 deletions(-) rename p2p/protocol/identify/{routingstate.go => peer_record.go} (66%) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 24a321c9b7..6e94884eaa 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -3,7 +3,7 @@ package identify import ( "context" "fmt" - "github.com/libp2p/go-libp2p-core/routing" + "github.com/libp2p/go-libp2p-core/record" "runtime/debug" "sync" "time" @@ -84,12 +84,12 @@ type IDService struct { // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet - routingStateManager *routingStateManager + routingStateManager *peerRecordManager useSignedAddrs bool subscriptions struct { - localProtocolsUpdated event.Subscription - localRoutingStateUpdated event.Subscription + localProtocolsUpdated event.Subscription + localPeerRecordUpdated event.Subscription } emitters struct { evtPeerProtocolsUpdated event.Emitter @@ -127,11 +127,11 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } else { go s.handleEvents(s.subscriptions.localProtocolsUpdated, s.handleProtosChanged) } - s.subscriptions.localRoutingStateUpdated, err = h.EventBus().Subscribe(&event.EvtLocalPeerRoutingStateUpdated{}, eventbus.BufSize(128)) + s.subscriptions.localPeerRecordUpdated, err = h.EventBus().Subscribe(&event.EvtLocalPeerRecordUpdated{}, eventbus.BufSize(128)) if err != nil { log.Warnf("identify service not subscribed to local routing state changes; err: %s", err) } else { - go s.handleEvents(s.subscriptions.localRoutingStateUpdated, s.handleRoutingStateUpdated) + go s.handleEvents(s.subscriptions.localPeerRecordUpdated, s.handleRoutingStateUpdated) } s.emitters.evtPeerProtocolsUpdated, err = h.EventBus().Emitter(&event.EvtPeerProtocolsUpdated{}) @@ -139,7 +139,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { log.Warningf("identify service not emitting peer protocol updates; err: %s", err) } - s.routingStateManager, err = NewRoutingStateManager(ctx, h, cfg.includeLocalAddrsInRoutingState) + s.routingStateManager, err = NewPeerRecordManager(ctx, h, cfg.includeLocalAddrsInRoutingState) if err != nil { log.Warnf("identify service not tracking routing state changes; err: %s", err) } @@ -343,15 +343,15 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { } log.Debugf("%s sent listen addrs to %s: %s", c.LocalPeer(), c.RemotePeer(), laddrs) - // Generate a signed routing record containing our listen addresses and + // Generate a signed peer record containing our listen addresses and // send it along with the unsigned addrs - signedState := ids.routingStateManager.LatestState() - if ids.useSignedAddrs && signedState != nil { - envelopeBytes, err := signedState.Marshal() + signedRecord := ids.routingStateManager.LatestRecord() + if ids.useSignedAddrs && signedRecord != nil { + envelopeBytes, err := signedRecord.Marshal() if err != nil { - log.Warningf("error marshaling signed routing state: %v", err) + log.Warnf("error marshaling signed routing state: %v", err) } else { - mes.SignedRoutingState = envelopeBytes + mes.SignedPeerRecord = envelopeBytes } } @@ -413,14 +413,13 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // that picks random source ports, this can cause DHT nodes to collect // many undialable addresses for other peers. - // add certified addresses for the peer, if they sent us a signed routing - // state record - var routingState *routing.SignedRoutingState + // add certified addresses for the peer, if they sent us a signed peer record + var signedPeerRecord *record.SignedEnvelope if ids.useSignedAddrs { var err error - routingState, err = signedRoutingStateFromMsg(mes) + signedPeerRecord, err = signedPeerRecordFromMessage(mes) if err != nil { - log.Warningf("error getting routing state from Identify message: %v", err) + log.Warnf("error getting routing state from Identify message: %v", err) } } @@ -436,9 +435,10 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // is no period of having no good addrs whatsoever ids.Host.Peerstore().UpdateAddrs(p, peerstore.ConnectedAddrTTL, transientTTL) - // add signed addrs if we have them - if routingState != nil { - addErr := ids.Host.Peerstore().AddCertifiedAddrs(routingState, ttl) + // add signed addrs if we have them and the peerstore supports them + cab, ok := peerstore.GetCertifiedAddrBook(ids.Host.Peerstore()) + if ok && signedPeerRecord != nil { + addErr := cab.AddCertifiedAddrs(signedPeerRecord, ttl) if addErr != nil { log.Errorf("error adding signed addrs to peerstore: %v", addErr) // fall back to adding unsigned addrs @@ -632,11 +632,11 @@ func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool { return false } -func signedRoutingStateFromMsg(msg *pb.Identify) (*routing.SignedRoutingState, error) { - if msg.SignedRoutingState == nil || len(msg.SignedRoutingState) == 0 { +func signedPeerRecordFromMessage(msg *pb.Identify) (*record.SignedEnvelope, error) { + if msg.SignedPeerRecord == nil || len(msg.SignedPeerRecord) == 0 { return nil, nil } - return routing.UnmarshalSignedRoutingState(msg.SignedRoutingState) + return record.ConsumeEnvelope(msg.SignedPeerRecord, peer.PeerRecordEnvelopeDomain) } // netNotifiee defines methods to be used with the IpfsDHT diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 25792a134f..ebcc9951d8 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -83,10 +83,10 @@ func subtestIDService(t *testing.T) { // and the protocol versions. t.Log("test peer2 has peer1 addrs correctly") - // h2 still has the forgetMe addr for h1, since it's not deleted immediately - testKnowsAddrs(t, h2, h1p, addrs) // has them - // the forgetMe addr was not certified by h1, so h2's CertifiedAddrs for - // h1 will only include the ones sent during the exchange + // the forgetMe addr (and any other uncertified addrs) is removed + // from h2's peerstore when it receives certified addrs for h1. + // h2 should only know the actual (certified) addrs + testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them testHasCertifiedAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) testHasProtocolVersions(t, h2, h1p) testHasPublicKey(t, h2, h1p, h1.Peerstore().PubKey(h1p)) // h1 should have h2's public key @@ -98,26 +98,21 @@ func subtestIDService(t *testing.T) { t.Fatal("should have no connections") } + t.Log("testing addrs just after disconnect") // addresses don't immediately expire on disconnect, so we should still have them - testKnowsAddrs(t, h2, h1p, addrs) + testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) testHasCertifiedAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) testHasCertifiedAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) - // the forgetMe addr had its TTL reduced during the identify exchange, - // since it was not present in the exchanged addrs, - // so it will be forgotten first - time.Sleep(500 * time.Millisecond) - testKnowsAddrs(t, h2, h1p, addrs[:len(addrs)-1]) - - // the remaining addrs had their TTLs reduced on disconnect, and + // the addrs had their TTLs reduced on disconnect, and // will be forgotten soon after - time.Sleep(1 * time.Second) + t.Log("testing addrs after TTL expiration") + time.Sleep(2 * time.Second) testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) - } func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { @@ -129,8 +124,23 @@ func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiadd func testHasCertifiedAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { t.Helper() - actual := h.Peerstore().CertifiedAddrs(p) - checkAddrs(t, expected, actual, fmt.Sprintf("%s did not have certified addr for %s", h.ID(), p)) + cab, ok := peerstore.GetCertifiedAddrBook(h.Peerstore()) + if !ok { + t.Error("expected peerstore to implement CertifiedAddrBook") + } + recordEnvelope := cab.SignedPeerRecord(p) + if recordEnvelope == nil { + if len(expected) == 0 { + return + } + t.Errorf("peerstore has no signed record for peer %s", p) + } + rec, err := peer.PeerRecordFromSignedEnvelope(recordEnvelope) + if err != nil { + t.Error("Error unwrapping signed PeerRecord from envelope", err) + } + + checkAddrs(t, expected, rec.Addrs, fmt.Sprintf("%s did not have certified addr for %s", h.ID(), p)) } func checkAddrs(t *testing.T, expected, actual []ma.Multiaddr, msg string) { diff --git a/p2p/protocol/identify/pb/identify.pb.go b/p2p/protocol/identify/pb/identify.pb.go index 4af720789e..0c45885944 100644 --- a/p2p/protocol/identify/pb/identify.pb.go +++ b/p2p/protocol/identify/pb/identify.pb.go @@ -98,12 +98,12 @@ type Identify struct { Protocols []string `protobuf:"bytes,3,rep,name=protocols" json:"protocols,omitempty"` // a delta update is incompatible with everything else. If this field is included, none of the others can appear. Delta *Delta `protobuf:"bytes,7,opt,name=delta" json:"delta,omitempty"` - // signedRoutingState contains a serialized SignedEnvelope containing a RoutingStateRecord, + // signedPeerRecord contains a serialized SignedEnvelope containing a PeerRecord, // signed by the sending node. It contains the same addresses as the listenAddrs field, but // in a form that lets us share authenticated addrs with other peers. - // see github.com/libp2p/go-libp2p-core/crypto/pb/envelope.proto and - // github.com/libp2p/go-libp2p-core/routing/pb/routing_state.proto for message definitions. - SignedRoutingState []byte `protobuf:"bytes,8,opt,name=signedRoutingState" json:"signedRoutingState,omitempty"` + // see github.com/libp2p/go-libp2p-core/record/pb/envelope.proto and + // github.com/libp2p/go-libp2p-core/peer/pb/peer_record.proto for message definitions. + SignedPeerRecord []byte `protobuf:"bytes,8,opt,name=signedPeerRecord" json:"signedPeerRecord,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -191,9 +191,9 @@ func (m *Identify) GetDelta() *Delta { return nil } -func (m *Identify) GetSignedRoutingState() []byte { +func (m *Identify) GetSignedPeerRecord() []byte { if m != nil { - return m.SignedRoutingState + return m.SignedPeerRecord } return nil } @@ -206,25 +206,24 @@ func init() { func init() { proto.RegisterFile("identify.proto", fileDescriptor_83f1e7e6b485409f) } var fileDescriptor_83f1e7e6b485409f = []byte{ - // 275 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xb1, 0x4e, 0xf3, 0x30, - 0x14, 0x85, 0xe5, 0xf6, 0xef, 0x4f, 0x7b, 0x13, 0xb5, 0xd2, 0x9d, 0x3c, 0xa0, 0x28, 0x64, 0xc1, - 0x53, 0x06, 0xde, 0x00, 0xc4, 0x82, 0x58, 0x90, 0x2b, 0xb1, 0xa2, 0xa4, 0xbe, 0x44, 0x96, 0x52, - 0xbb, 0x72, 0x5c, 0xa4, 0xbe, 0x17, 0x0f, 0xc1, 0xc8, 0x23, 0xa0, 0x3c, 0x09, 0x8a, 0x4b, 0x9a, - 0x14, 0x31, 0xfa, 0xf3, 0x27, 0x1f, 0x9f, 0x03, 0x4b, 0xad, 0xc8, 0x78, 0xfd, 0x7a, 0xc8, 0x77, - 0xce, 0x7a, 0x8b, 0xd1, 0x70, 0x2e, 0xb3, 0x35, 0xcc, 0xee, 0xa9, 0xf6, 0x05, 0x5e, 0xc3, 0xaa, - 0x50, 0x8a, 0xd4, 0x4b, 0x90, 0x36, 0xb6, 0x6e, 0x38, 0x4b, 0xa7, 0x62, 0x21, 0x97, 0x01, 0x3f, - 0xf5, 0x14, 0xaf, 0x20, 0x76, 0xdb, 0x91, 0x35, 0x09, 0x56, 0xe4, 0xb6, 0x27, 0x25, 0x7b, 0x9f, - 0xc0, 0xfc, 0xe1, 0x27, 0x04, 0x05, 0xac, 0x7a, 0xf9, 0x99, 0x5c, 0xa3, 0xad, 0xe1, 0xb3, 0x94, - 0x89, 0x85, 0xfc, 0x8d, 0x31, 0x83, 0xb8, 0xa8, 0xc8, 0xf8, 0x5e, 0xfb, 0x1f, 0xb4, 0x33, 0x86, - 0x97, 0xb0, 0xd8, 0xed, 0xcb, 0x5a, 0x6f, 0x1e, 0xe9, 0xc0, 0x59, 0xca, 0x44, 0x2c, 0x07, 0x80, - 0x29, 0x44, 0xb5, 0x6e, 0x3c, 0x99, 0x5b, 0xa5, 0xdc, 0xf1, 0x6b, 0xb1, 0x1c, 0xa3, 0x2e, 0xc3, - 0x96, 0x0d, 0xb9, 0x37, 0x52, 0x1d, 0xe0, 0xff, 0xc2, 0x13, 0x67, 0x2c, 0x64, 0x9c, 0xea, 0x4d, - 0x43, 0xbd, 0x01, 0xa0, 0x80, 0x99, 0xea, 0x16, 0xe3, 0x17, 0x29, 0x13, 0xd1, 0x0d, 0xe6, 0xa3, - 0x39, 0xf3, 0xb0, 0xa5, 0x3c, 0x0a, 0x98, 0x03, 0x36, 0xba, 0x32, 0xa4, 0xa4, 0xdd, 0x7b, 0x6d, - 0xaa, 0xb5, 0x2f, 0x3c, 0xf1, 0x79, 0x48, 0xfc, 0xe3, 0xe6, 0x2e, 0xfe, 0x68, 0x13, 0xf6, 0xd9, - 0x26, 0xec, 0xab, 0x4d, 0xd8, 0x77, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7b, 0x1e, 0x66, 0x03, 0xb7, - 0x01, 0x00, 0x00, + // 272 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0x90, 0x41, 0x4a, 0xc3, 0x40, + 0x14, 0x86, 0x99, 0xd6, 0x6a, 0xf3, 0x12, 0x5a, 0x99, 0xd5, 0x2c, 0x24, 0xc4, 0x6c, 0x1c, 0x5c, + 0x64, 0xe1, 0x0d, 0x14, 0x37, 0xe2, 0xa6, 0x8c, 0xe0, 0x56, 0x92, 0xbc, 0x67, 0x19, 0x48, 0x33, + 0x65, 0x32, 0x0a, 0xbd, 0x95, 0xc7, 0x70, 0xe9, 0x11, 0x24, 0x27, 0x91, 0x4c, 0x4d, 0x93, 0xea, + 0x72, 0x3e, 0x3e, 0xe6, 0x7f, 0xff, 0x0f, 0x0b, 0x8d, 0x54, 0x3b, 0xfd, 0xba, 0xcb, 0xb6, 0xd6, + 0x38, 0xc3, 0xc3, 0xe1, 0x5d, 0xa4, 0x4f, 0x30, 0xbb, 0xa7, 0xca, 0xe5, 0xfc, 0x0a, 0x96, 0x39, + 0x22, 0xe1, 0x8b, 0x97, 0x4a, 0x53, 0x35, 0x82, 0x25, 0x53, 0x19, 0xa8, 0x85, 0xc7, 0xab, 0x9e, + 0xf2, 0x4b, 0x88, 0xec, 0x66, 0x64, 0x4d, 0xbc, 0x15, 0xda, 0xcd, 0x41, 0x49, 0x3f, 0x26, 0x30, + 0x7f, 0xf8, 0x0d, 0xe1, 0x12, 0x96, 0xbd, 0xfc, 0x4c, 0xb6, 0xd1, 0xa6, 0x16, 0xb3, 0x84, 0xc9, + 0x40, 0xfd, 0xc5, 0x3c, 0x85, 0x28, 0x5f, 0x53, 0xed, 0x7a, 0xed, 0xd4, 0x6b, 0x47, 0x8c, 0x5f, + 0x40, 0xb0, 0x7d, 0x2b, 0x2a, 0x5d, 0x3e, 0xd2, 0x4e, 0xb0, 0x84, 0xc9, 0x48, 0x0d, 0x80, 0x27, + 0x10, 0x56, 0xba, 0x71, 0x54, 0xdf, 0x22, 0xda, 0xfd, 0x69, 0x91, 0x1a, 0xa3, 0x2e, 0xc3, 0x14, + 0x0d, 0xd9, 0x77, 0xc2, 0x0e, 0x88, 0x13, 0xff, 0xc5, 0x11, 0xf3, 0x19, 0x87, 0x7a, 0x53, 0x5f, + 0x6f, 0x00, 0x5c, 0xc2, 0x0c, 0xbb, 0xc5, 0xc4, 0x59, 0xc2, 0x64, 0x78, 0xc3, 0xb3, 0xd1, 0x9c, + 0x99, 0xdf, 0x52, 0xed, 0x05, 0x7e, 0x0d, 0xe7, 0x8d, 0x5e, 0xd7, 0x84, 0x2b, 0x22, 0xab, 0xa8, + 0x34, 0x16, 0xc5, 0xdc, 0xe7, 0xfd, 0xe3, 0x77, 0xd1, 0x67, 0x1b, 0xb3, 0xaf, 0x36, 0x66, 0xdf, + 0x6d, 0xcc, 0x7e, 0x02, 0x00, 0x00, 0xff, 0xff, 0xc0, 0x03, 0xc8, 0x41, 0xb3, 0x01, 0x00, 0x00, } func (m *Delta) Marshal() (dAtA []byte, err error) { @@ -350,11 +349,11 @@ func (m *Identify) MarshalTo(dAtA []byte) (int, error) { } i += n1 } - if m.SignedRoutingState != nil { + if m.SignedPeerRecord != nil { dAtA[i] = 0x42 i++ - i = encodeVarintIdentify(dAtA, i, uint64(len(m.SignedRoutingState))) - i += copy(dAtA[i:], m.SignedRoutingState) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.SignedPeerRecord))) + i += copy(dAtA[i:], m.SignedPeerRecord) } if m.XXX_unrecognized != nil { i += copy(dAtA[i:], m.XXX_unrecognized) @@ -433,8 +432,8 @@ func (m *Identify) Size() (n int) { l = m.Delta.Size() n += 1 + l + sovIdentify(uint64(l)) } - if m.SignedRoutingState != nil { - l = len(m.SignedRoutingState) + if m.SignedPeerRecord != nil { + l = len(m.SignedPeerRecord) n += 1 + l + sovIdentify(uint64(l)) } if m.XXX_unrecognized != nil { @@ -839,7 +838,7 @@ func (m *Identify) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 8: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SignedRoutingState", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SignedPeerRecord", wireType) } var byteLen int for shift := uint(0); ; shift += 7 { @@ -866,9 +865,9 @@ func (m *Identify) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.SignedRoutingState = append(m.SignedRoutingState[:0], dAtA[iNdEx:postIndex]...) - if m.SignedRoutingState == nil { - m.SignedRoutingState = []byte{} + m.SignedPeerRecord = append(m.SignedPeerRecord[:0], dAtA[iNdEx:postIndex]...) + if m.SignedPeerRecord == nil { + m.SignedPeerRecord = []byte{} } iNdEx = postIndex default: diff --git a/p2p/protocol/identify/pb/identify.proto b/p2p/protocol/identify/pb/identify.proto index 620671d8ef..afc85253f6 100644 --- a/p2p/protocol/identify/pb/identify.proto +++ b/p2p/protocol/identify/pb/identify.proto @@ -37,10 +37,10 @@ message Identify { // a delta update is incompatible with everything else. If this field is included, none of the others can appear. optional Delta delta = 7; - // signedRoutingState contains a serialized SignedEnvelope containing a RoutingStateRecord, + // signedPeerRecord contains a serialized SignedEnvelope containing a PeerRecord, // signed by the sending node. It contains the same addresses as the listenAddrs field, but // in a form that lets us share authenticated addrs with other peers. - // see github.com/libp2p/go-libp2p-core/crypto/pb/envelope.proto and - // github.com/libp2p/go-libp2p-core/routing/pb/routing_state.proto for message definitions. - optional bytes signedRoutingState = 8; + // see github.com/libp2p/go-libp2p-core/record/pb/envelope.proto and + // github.com/libp2p/go-libp2p-core/peer/pb/peer_record.proto for message definitions. + optional bytes signedPeerRecord = 8; } diff --git a/p2p/protocol/identify/routingstate.go b/p2p/protocol/identify/peer_record.go similarity index 66% rename from p2p/protocol/identify/routingstate.go rename to p2p/protocol/identify/peer_record.go index 92be01fe88..4219cf67e5 100644 --- a/p2p/protocol/identify/routingstate.go +++ b/p2p/protocol/identify/peer_record.go @@ -5,19 +5,20 @@ import ( "github.com/libp2p/go-eventbus" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/host" - "github.com/libp2p/go-libp2p-core/routing" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p-core/record" "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" ) -// routingStateManager creates new SignedRoutingState records that can +// peerRecordManager creates new SignedRoutingState records that can // be shared with other peers to inform them of our listen addresses in // a secure and authenticated way. // // New signed records are created in response to EvtLocalAddressesUpdated events, // and are emitted in EvtLocalRoutingStateUpdated events. -type routingStateManager struct { - latestState *routing.SignedRoutingState +type peerRecordManager struct { + latest *record.SignedEnvelope ctx context.Context host host.Host @@ -30,8 +31,8 @@ type routingStateManager struct { } } -func NewRoutingStateManager(ctx context.Context, host host.Host, includeLocalAddrs bool) (*routingStateManager, error) { - m := &routingStateManager{ +func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs bool) (*peerRecordManager, error) { + m := &peerRecordManager{ ctx: ctx, host: host, includeLocalAddrs: includeLocalAddrs, @@ -42,7 +43,7 @@ func NewRoutingStateManager(ctx context.Context, host host.Host, includeLocalAdd if err != nil { return nil, err } - m.emitters.evtLocalRoutingStateUpdated, err = bus.Emitter(&event.EvtLocalPeerRoutingStateUpdated{}, eventbus.Stateful) + m.emitters.evtLocalRoutingStateUpdated, err = bus.Emitter(&event.EvtLocalPeerRecordUpdated{}, eventbus.Stateful) if err != nil { return nil, err } @@ -52,14 +53,14 @@ func NewRoutingStateManager(ctx context.Context, host host.Host, includeLocalAdd return m, nil } -func (m *routingStateManager) LatestState() *routing.SignedRoutingState { +func (m *peerRecordManager) LatestRecord() *record.SignedEnvelope { if m == nil { return nil } - return m.latestState + return m.latest } -func (m *routingStateManager) handleEvents() { +func (m *peerRecordManager) handleEvents() { sub := m.subscriptions.localAddrsUpdated defer func() { _ = sub.Close() @@ -81,21 +82,21 @@ func (m *routingStateManager) handleEvents() { } } -func (m *routingStateManager) updateRoutingState() { - state, err := m.makeRoutingState() +func (m *peerRecordManager) updateRoutingState() { + envelope, err := m.makeSignedPeerRecord() if err != nil { - log.Warnf("error creating routing state record: %v", err) + log.Warnf("error creating signed peer record: %v", err) return } - m.latestState = state - stateEvt := event.EvtLocalPeerRoutingStateUpdated{State: m.latestState} + m.latest = envelope + stateEvt := event.EvtLocalPeerRecordUpdated{SignedRecord: m.latest} err = m.emitters.evtLocalRoutingStateUpdated.Emit(stateEvt) if err != nil { - log.Warnf("error emitting routing state event: %v", err) + log.Warnf("error emitting event for updated peer record: %v", err) } } -func (m *routingStateManager) makeRoutingState() (*routing.SignedRoutingState, error) { +func (m *peerRecordManager) makeSignedPeerRecord() (*record.SignedEnvelope, error) { privKey := m.host.Peerstore().PrivKey(m.host.ID()) if privKey == nil { log.Warn("error making routing state: unable to find host's private key in peerstore") @@ -112,5 +113,5 @@ func (m *routingStateManager) makeRoutingState() (*routing.SignedRoutingState, e } } - return routing.MakeSignedRoutingState(privKey, addrs) + return peer.NewPeerRecord(m.host.ID(), addrs).Sign(privKey) } From c2309aface33ed5afc274e04ac5f4669e5c483ab Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 21 Jan 2020 12:16:34 -0500 Subject: [PATCH 11/43] update to track changes from -core PR --- p2p/host/basic/basic_host.go | 51 ++++++++++------- p2p/protocol/identify/id.go | 23 ++++---- p2p/protocol/identify/id_test.go | 14 +++-- p2p/protocol/identify/opts.go | 2 +- p2p/protocol/identify/peer_record.go | 85 +++++++++++++++++++--------- 5 files changed, 108 insertions(+), 67 deletions(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 973631f083..08ea2e63be 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -304,22 +304,16 @@ func (h *BasicHost) newStreamHandler(s network.Stream) { // changed and emits an EvtLocalAddressesUpdatedEvent if so. // Warning: this interface is unstable and may disappear in the future. func (h *BasicHost) CheckForAddressChanges() { - changed := false - h.mx.Lock() addrs := h.Addrs() - added, removed := addrDiff(h.lastAddrs, addrs) - if len(added) != 0 || len(removed) != 0 { - changed = true + changeEvt := makeUpdatedAddrEvent(h.lastAddrs, addrs) + if changeEvt != nil { h.lastAddrs = addrs } h.mx.Unlock() - if changed { - err := h.emitters.evtLocalAddrsUpdated.Emit(event.EvtLocalAddressesUpdated{ - Added: added, - Removed: removed, - }) + if changeEvt != nil { + err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt) if err != nil { log.Warnf("error emitting event for updated addrs: %s", err) } @@ -350,23 +344,36 @@ func (h *BasicHost) background(p goprocess.Process) { } } -func addrDiff(old, new []ma.Multiaddr) (added, removed []ma.Multiaddr) { - oldmap := make(map[string]ma.Multiaddr, len(old)) +func makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.EvtLocalAddressesUpdated { + prevmap := make(map[string]ma.Multiaddr, len(prev)) + evt := event.EvtLocalAddressesUpdated{Diffs: true} + addrsAdded := false - for _, addr := range old { - oldmap[string(addr.Bytes())] = addr + for _, addr := range prev { + prevmap[string(addr.Bytes())] = addr } - for _, addr := range new { - _, ok := oldmap[string(addr.Bytes())] - if !ok { - added = append(added, addr) + for _, addr := range current { + _, ok := prevmap[string(addr.Bytes())] + updated := event.UpdatedAddress{Address: addr} + if ok { + updated.Action = event.Maintained + } else { + updated.Action = event.Added + addrsAdded = true } - delete(oldmap, string(addr.Bytes())) + evt.Current = append(evt.Current, updated) + delete(prevmap, string(addr.Bytes())) + } + for _, addr := range prevmap { + updated := event.UpdatedAddress{Action: event.Removed, Address: addr} + evt.Removed = append(evt.Removed, updated) } - for _, addr := range oldmap { - removed = append(removed, addr) + + if !addrsAdded && len(evt.Removed) == 0 { + return nil } - return added, removed + + return &evt } // ID returns the (local) peer.ID associated with this Host diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 6e94884eaa..08bc063198 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -84,8 +84,8 @@ type IDService struct { // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet - routingStateManager *peerRecordManager - useSignedAddrs bool + peerRecordManager *peerRecordManager + useSignedAddrs bool subscriptions struct { localProtocolsUpdated event.Subscription @@ -131,7 +131,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { if err != nil { log.Warnf("identify service not subscribed to local routing state changes; err: %s", err) } else { - go s.handleEvents(s.subscriptions.localPeerRecordUpdated, s.handleRoutingStateUpdated) + go s.handleEvents(s.subscriptions.localPeerRecordUpdated, s.handlePeerRecordUpdated) } s.emitters.evtPeerProtocolsUpdated, err = h.EventBus().Emitter(&event.EvtPeerProtocolsUpdated{}) @@ -139,7 +139,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { log.Warningf("identify service not emitting peer protocol updates; err: %s", err) } - s.routingStateManager, err = NewPeerRecordManager(ctx, h, cfg.includeLocalAddrsInRoutingState) + s.peerRecordManager, err = NewPeerRecordManager(ctx, h, cfg.includeLocalAddrsInRoutingState) if err != nil { log.Warnf("identify service not tracking routing state changes; err: %s", err) } @@ -176,8 +176,8 @@ func (ids *IDService) handleProtosChanged(evt interface{}) { ids.fireProtocolDelta(evt.(event.EvtLocalProtocolsUpdated)) } -func (ids *IDService) handleRoutingStateUpdated(evt interface{}) { - log.Debug("triggering push based on routing state updated event") +func (ids *IDService) handlePeerRecordUpdated(evt interface{}) { + log.Debug("triggering push based on updated local PeerRecord") ids.Push() } @@ -345,7 +345,7 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { // Generate a signed peer record containing our listen addresses and // send it along with the unsigned addrs - signedRecord := ids.routingStateManager.LatestRecord() + signedRecord := ids.peerRecordManager.LatestRecord() if ids.useSignedAddrs && signedRecord != nil { envelopeBytes, err := signedRecord.Marshal() if err != nil { @@ -414,7 +414,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // many undialable addresses for other peers. // add certified addresses for the peer, if they sent us a signed peer record - var signedPeerRecord *record.SignedEnvelope + var signedPeerRecord *record.Envelope if ids.useSignedAddrs { var err error signedPeerRecord, err = signedPeerRecordFromMessage(mes) @@ -438,7 +438,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // add signed addrs if we have them and the peerstore supports them cab, ok := peerstore.GetCertifiedAddrBook(ids.Host.Peerstore()) if ok && signedPeerRecord != nil { - addErr := cab.AddCertifiedAddrs(signedPeerRecord, ttl) + addErr := cab.ProcessPeerRecord(signedPeerRecord, ttl) if addErr != nil { log.Errorf("error adding signed addrs to peerstore: %v", addErr) // fall back to adding unsigned addrs @@ -632,11 +632,12 @@ func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool { return false } -func signedPeerRecordFromMessage(msg *pb.Identify) (*record.SignedEnvelope, error) { +func signedPeerRecordFromMessage(msg *pb.Identify) (*record.Envelope, error) { if msg.SignedPeerRecord == nil || len(msg.SignedPeerRecord) == 0 { return nil, nil } - return record.ConsumeEnvelope(msg.SignedPeerRecord, peer.PeerRecordEnvelopeDomain) + env, _, err := record.ConsumeEnvelope(msg.SignedPeerRecord, peer.PeerRecordEnvelopeDomain) + return env, err } // netNotifiee defines methods to be used with the IpfsDHT diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index ebcc9951d8..49455936b7 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -36,8 +36,8 @@ func subtestIDService(t *testing.T) { h1p := h1.ID() h2p := h2.ID() - ids1 := identify.NewIDService(ctx, h1, identify.IncludeLocalAddrsInRoutingState) - ids2 := identify.NewIDService(ctx, h2, identify.IncludeLocalAddrsInRoutingState) + ids1 := identify.NewIDService(ctx, h1, identify.IncludeLocalAddrsInPeerRecord) + ids2 := identify.NewIDService(ctx, h2, identify.IncludeLocalAddrsInPeerRecord) testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing @@ -128,17 +128,21 @@ func testHasCertifiedAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.M if !ok { t.Error("expected peerstore to implement CertifiedAddrBook") } - recordEnvelope := cab.SignedPeerRecord(p) + recordEnvelope := cab.GetPeerRecord(p) if recordEnvelope == nil { if len(expected) == 0 { return } - t.Errorf("peerstore has no signed record for peer %s", p) + t.Fatalf("peerstore has no signed record for peer %s", p) } - rec, err := peer.PeerRecordFromSignedEnvelope(recordEnvelope) + r, err := recordEnvelope.Record() if err != nil { t.Error("Error unwrapping signed PeerRecord from envelope", err) } + rec, ok := r.(*peer.PeerRecord) + if !ok { + t.Error("unexpected record type") + } checkAddrs(t, expected, rec.Addrs, fmt.Sprintf("%s did not have certified addr for %s", h.ID(), p)) } diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index ccb813f250..1a01dc0dcd 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -26,6 +26,6 @@ func DisableSignedAddrSupportForTesting() Option { } } -func IncludeLocalAddrsInRoutingState(cfg *config) { +func IncludeLocalAddrsInPeerRecord(cfg *config) { cfg.includeLocalAddrsInRoutingState = true } diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index 4219cf67e5..e67cf2da13 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -2,7 +2,10 @@ package identify import ( "context" + "errors" + "fmt" "github.com/libp2p/go-eventbus" + "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" @@ -11,17 +14,22 @@ import ( manet "github.com/multiformats/go-multiaddr-net" ) -// peerRecordManager creates new SignedRoutingState records that can +// peerRecordManager creates new signed peer.PeerRecords that can // be shared with other peers to inform them of our listen addresses in // a secure and authenticated way. // // New signed records are created in response to EvtLocalAddressesUpdated events, -// and are emitted in EvtLocalRoutingStateUpdated events. +// and are emitted in EvtLocalPeerRecordUpdated events. +// +// EvtLocalPeerRecordUpdated is emitted using a Stateful emitter, so new subscribers +// will immediately receive the current record when they subscribe, with future +// records delivered in future events. type peerRecordManager struct { - latest *record.SignedEnvelope + latest *record.Envelope + hostID peer.ID + signingKey crypto.PrivKey ctx context.Context - host host.Host includeLocalAddrs bool subscriptions struct { localAddrsUpdated event.Subscription @@ -32,13 +40,25 @@ type peerRecordManager struct { } func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs bool) (*peerRecordManager, error) { + hostKey := host.Peerstore().PrivKey(host.ID()) + if hostKey == nil { + return nil, errors.New("unable to get private key for host") + } + m := &peerRecordManager{ ctx: ctx, - host: host, + signingKey: hostKey, + hostID: host.ID(), includeLocalAddrs: includeLocalAddrs, } + + initialRec, err := m.makeSignedPeerRecord(host.Addrs()) + if err != nil { + return nil, fmt.Errorf("error constructing initial peer record: %w", err) + } + m.latest = initialRec + bus := host.EventBus() - var err error m.subscriptions.localAddrsUpdated, err = bus.Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) if err != nil { return nil, err @@ -47,13 +67,13 @@ func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs if err != nil { return nil, err } + m.emitLatest() - m.updateRoutingState() go m.handleEvents() return m, nil } -func (m *peerRecordManager) LatestRecord() *record.SignedEnvelope { +func (m *peerRecordManager) LatestRecord() *record.Envelope { if m == nil { return nil } @@ -71,47 +91,56 @@ func (m *peerRecordManager) handleEvents() { for { select { - case _, more := <-sub.Out(): + case evt, more := <-sub.Out(): if !more { return } - m.updateRoutingState() + m.update(evt.(event.EvtLocalAddressesUpdated)) case <-m.ctx.Done(): return } } } -func (m *peerRecordManager) updateRoutingState() { - envelope, err := m.makeSignedPeerRecord() +func (m *peerRecordManager) update(evt event.EvtLocalAddressesUpdated) { + envelope, err := m.makeSignedPeerRecord(addrsFromEvent(evt)) if err != nil { log.Warnf("error creating signed peer record: %v", err) return } m.latest = envelope + m.emitLatest() +} + +func (m *peerRecordManager) emitLatest() { stateEvt := event.EvtLocalPeerRecordUpdated{SignedRecord: m.latest} - err = m.emitters.evtLocalRoutingStateUpdated.Emit(stateEvt) + err := m.emitters.evtLocalRoutingStateUpdated.Emit(stateEvt) if err != nil { log.Warnf("error emitting event for updated peer record: %v", err) } } -func (m *peerRecordManager) makeSignedPeerRecord() (*record.SignedEnvelope, error) { - privKey := m.host.Peerstore().PrivKey(m.host.ID()) - if privKey == nil { - log.Warn("error making routing state: unable to find host's private key in peerstore") - } - - var addrs []multiaddr.Multiaddr - if m.includeLocalAddrs { - addrs = m.host.Addrs() - } else { - for _, a := range m.host.Addrs() { - if manet.IsPublicAddr(a) { - addrs = append(addrs, a) - } +func (m *peerRecordManager) makeSignedPeerRecord(current []multiaddr.Multiaddr) (*record.Envelope, error) { + addrs := make([]multiaddr.Multiaddr, 0, len(current)) + for _, a := range current { + if a == nil { + continue + } + if m.includeLocalAddrs || manet.IsPublicAddr(a) { + addrs = append(addrs, a) } } - return peer.NewPeerRecord(m.host.ID(), addrs).Sign(privKey) + rec := peer.NewPeerRecord() + rec.PeerID = m.hostID + rec.Addrs = addrs + return rec.Sign(m.signingKey) +} + +func addrsFromEvent(evt event.EvtLocalAddressesUpdated) []multiaddr.Multiaddr { + addrs := make([]multiaddr.Multiaddr, len(evt.Current)) + for _, a := range evt.Current { + addrs = append(addrs, a.Address) + } + return addrs } From 3f7951d824aed9d0af0ec717933d36a1ace68921 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 21 Jan 2020 14:02:55 -0500 Subject: [PATCH 12/43] only generate initial peer record if we have addrs --- p2p/protocol/identify/peer_record.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index e67cf2da13..f163d5cde2 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -52,12 +52,15 @@ func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs includeLocalAddrs: includeLocalAddrs, } - initialRec, err := m.makeSignedPeerRecord(host.Addrs()) - if err != nil { - return nil, fmt.Errorf("error constructing initial peer record: %w", err) + if len(host.Addrs()) != 0 { + initialRec, err := m.makeSignedPeerRecord(host.Addrs()) + if err != nil { + return nil, fmt.Errorf("error constructing initial peer record: %w", err) + } + m.latest = initialRec } - m.latest = initialRec + var err error bus := host.EventBus() m.subscriptions.localAddrsUpdated, err = bus.Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) if err != nil { @@ -67,7 +70,6 @@ func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs if err != nil { return nil, err } - m.emitLatest() go m.handleEvents() return m, nil From 71187c5a896636f423fcf11e91f0152f917bea65 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 21 Jan 2020 14:05:45 -0500 Subject: [PATCH 13/43] update PR branch dependencies --- go.mod | 8 ++------ go.sum | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index c5080486df..404aea8849 100644 --- a/go.mod +++ b/go.mod @@ -13,13 +13,13 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211544-b57c5023fbf9 + github.com/libp2p/go-libp2p-core v0.3.1-0.20200121152518-5a523a102629 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 github.com/libp2p/go-libp2p-nat v0.0.5 github.com/libp2p/go-libp2p-netutil v0.1.0 - github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191121180145-50b0a8d4d5d1 + github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200121162712-1f4670bb6c34 github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-swarm v0.2.2 github.com/libp2p/go-libp2p-testing v0.1.1 @@ -29,16 +29,12 @@ require ( github.com/libp2p/go-stream-muxer-multistream v0.2.0 github.com/libp2p/go-tcp-transport v0.1.1 github.com/libp2p/go-ws-transport v0.2.0 - github.com/mailru/easyjson v0.7.0 // indirect github.com/miekg/dns v1.1.12 // indirect github.com/multiformats/go-multiaddr v0.2.0 github.com/multiformats/go-multiaddr-dns v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.2 github.com/multiformats/go-multistream v0.1.1 github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 - golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 // indirect - golang.org/x/sys v0.0.0-20191220142924-d4481acd189f // indirect - golang.org/x/tools v0.0.0-20191220195505-2f6d8bf0ad77 // indirect ) go 1.12 diff --git a/go.sum b/go.sum index 39f80b01c7..e68c0c68cb 100644 --- a/go.sum +++ b/go.sum @@ -133,10 +133,9 @@ github.com/libp2p/go-libp2p-core v0.0.4/go.mod h1:jyuCQP356gzfCFtRKyvAbNkyeuxb7O github.com/libp2p/go-libp2p-core v0.2.0/go.mod h1:X0eyB0Gy93v0DZtSYbEM7RnMChm9Uv3j7yRXjO77xSI= github.com/libp2p/go-libp2p-core v0.2.2/go.mod h1:8fcwTbsG2B+lTgRJ1ICZtiM5GWCWZVoVrLaDRvIRng0= github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g= -github.com/libp2p/go-libp2p-core v0.2.6-0.20191121175514-5fa975301271/go.mod h1:xDyprN8hkMpX27XQ1bBnYtuSuaCUFvKm+Q6gltHZCHE= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= -github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211544-b57c5023fbf9 h1:Doq/tZH8j+OGxNwRmYJoLkyLE/WrhoU67vcV1YLO2WA= -github.com/libp2p/go-libp2p-core v0.3.1-0.20191220211544-b57c5023fbf9/go.mod h1:KJbT3ekTPccG9QUSa5vc/0QVZXPE9+3TVlyqESRQic0= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200121152518-5a523a102629 h1:U0mnCQcq57Y67jSgC4DtNpYcyFen91pWnxT4dsZ2M1Q= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200121152518-5a523a102629/go.mod h1:KJbT3ekTPccG9QUSa5vc/0QVZXPE9+3TVlyqESRQic0= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -152,8 +151,8 @@ github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCTh github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= github.com/libp2p/go-libp2p-peerstore v0.1.3/go.mod h1:BJ9sHlm59/80oSkpWgr1MyY1ciXAXV397W6h1GH/uKI= -github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191121180145-50b0a8d4d5d1 h1:gVAbf4Q/MSlBPG5ufPxtiJglsoMtPjyICuASqah6tH4= -github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20191121180145-50b0a8d4d5d1/go.mod h1:JZoEvuUQYNg+eiYQHXN954IMzdAd/bmkjiiMy7VE6TA= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200121162712-1f4670bb6c34 h1:JCxoYrBcaJGlhbrhzTtRMXel5+fhZ7+jUO/ikD6FYRc= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200121162712-1f4670bb6c34/go.mod h1:FF3tFfdIhpdFwOA0R/hwWwEzdmwXzd8K/O3eqBvlwsw= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1 h1:eNWbJTdyPA7NxhP7J3c5lT97DC5d+u+IldkgCYFTPVA= @@ -259,6 +258,7 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS github.com/multiformats/go-multistream v0.1.1 h1:JlAdpIFhBhGRLxe9W6Om0w++Gd6KMWoFPZL/dEnm9nI= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -326,8 +326,8 @@ golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 h1:aJ0ex187qoXrJHPo8ZasVTASQB7llQP6YeNzgDALPRk= -golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -359,8 +359,8 @@ golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191220142924-d4481acd189f h1:68K/z8GLUxV76xGSqwTWw2gyk/jwn79LUL43rES2g8o= -golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200121082415-34d275377bf9 h1:N19i1HjUnR7TF7rMt8O4p3dLvqvmYyzB6ifMFmrbY50= +golang.org/x/sys v0.0.0-20200121082415-34d275377bf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -371,7 +371,7 @@ golang.org/x/tools v0.0.0-20181130052023-1c3d964395ce/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20191220195505-2f6d8bf0ad77/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200121145236-13c74800b9d2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= From baf71df21a2a2009a5aaa7896b9556f3ea23ddee Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Thu, 23 Jan 2020 11:54:13 -0500 Subject: [PATCH 14/43] add test for addr change event production --- p2p/host/basic/basic_host_test.go | 131 ++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index ed0370b8a4..3ad38b6612 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -507,6 +507,106 @@ func TestAddrResolutionRecursive(t *testing.T) { } } +func TestHostAddrChangeDetection(t *testing.T) { + // This test uses the address factory to provide several + // sets of listen addresses for the host. It advances through + // the sets by changing the currentAddrSet index var below. + addrSets := [][]ma.Multiaddr{ + {}, + {ma.StringCast("/ip4/1.2.3.4/tcp/1234")}, + {ma.StringCast("/ip4/1.2.3.4/tcp/1234"), ma.StringCast("/ip4/2.3.4.5/tcp/1234")}, + {ma.StringCast("/ip4/2.3.4.5/tcp/1234"), ma.StringCast("/ip4/3.4.5.6/tcp/4321")}, + } + + // The events we expect the host to emit when CheckForAddressChanges is called + // and the changes between addr sets are detected + expectedEvents := []event.EvtLocalAddressesUpdated{ + { + Diffs: true, + Current: []event.UpdatedAddress{ + {Action: event.Added, Address: ma.StringCast("/ip4/1.2.3.4/tcp/1234")}, + }, + Removed: []event.UpdatedAddress{}, + }, + { + Diffs: true, + Current: []event.UpdatedAddress{ + {Action: event.Maintained, Address: ma.StringCast("/ip4/1.2.3.4/tcp/1234")}, + {Action: event.Added, Address: ma.StringCast("/ip4/2.3.4.5/tcp/1234")}, + }, + Removed: []event.UpdatedAddress{}, + }, + { + Diffs: true, + Current: []event.UpdatedAddress{ + {Action: event.Added, Address: ma.StringCast("/ip4/3.4.5.6/tcp/4321")}, + {Action: event.Maintained, Address: ma.StringCast("/ip4/2.3.4.5/tcp/1234")}, + }, + Removed: []event.UpdatedAddress{ + {Action: event.Removed, Address: ma.StringCast("/ip4/1.2.3.4/tcp/1234")}, + }, + }, + } + + currentAddrSet := 0 + addrsFactory := func(addrs []ma.Multiaddr) []ma.Multiaddr { + return addrSets[currentAddrSet] + } + + ctx := context.Background() + h := New(swarmt.GenSwarm(t, ctx), AddrsFactory(addrsFactory)) + defer h.Close() + + sub, err := h.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(10)) + if err != nil { + t.Error(err) + } + defer sub.Close() + + // host should start with no addrs (addrSet 0) + addrs := h.Addrs() + if len(addrs) != 0 { + t.Fatalf("expected 0 addrs, got %d", len(addrs)) + } + + // Drain EvtLocalAddressesUpdated from the bus in a goroutine until we have as many as we expect + var receivedEvents []event.EvtLocalAddressesUpdated + go func() { + for { + select { + case evt, more := <-sub.Out(): + if !more { + return + } + receivedEvents = append(receivedEvents, evt.(event.EvtLocalAddressesUpdated)) + if len(receivedEvents) == len(expectedEvents) { + return + } + case <-ctx.Done(): + return + } + } + }() + + // Advance between addrSets, with a little delay to let the event propagate over the bus + for i := 1; i < len(addrSets); i++ { + currentAddrSet = i + h.CheckForAddressChanges() // forces the host to check for changes now, instead of waiting for background update + time.Sleep(100 * time.Millisecond) + } + + // assert that we received the events we expected + if len(receivedEvents) != len(expectedEvents) { + t.Errorf("expected to receive %d addr change events, got %d", len(expectedEvents), len(receivedEvents)) + } + for i, expected := range expectedEvents { + actual := receivedEvents[i] + if !updatedAddrEventsEqual(expected, actual) { + t.Errorf("change events not equal: \n\texpected: %v \n\tactual: %v", expected, actual) + } + } +} + type sortedMultiaddrs []ma.Multiaddr func (sma sortedMultiaddrs) Len() int { return len(sma) } @@ -514,3 +614,34 @@ func (sma sortedMultiaddrs) Swap(i, j int) { sma[i], sma[j] = sma[j], sma[i] } func (sma sortedMultiaddrs) Less(i, j int) bool { return bytes.Compare(sma[i].Bytes(), sma[j].Bytes()) == 1 } + +// updatedAddrsEqual is a helper to check whether two lists of +// event.UpdatedAddress have the same contents, ignoring ordering. +func updatedAddrsEqual(a, b []event.UpdatedAddress) bool { + if len(a) != len(b) { + return false + } + // ignore ordering in addr lists + aSet := make(map[string]struct{}) + for _, addr := range a { + s := string(addr.Action) + "::" + string(addr.Address.Bytes()) + aSet[s] = struct{}{} + } + for _, addr := range b { + s := string(addr.Action) + "::" + string(addr.Address.Bytes()) + _, ok := aSet[s] + if !ok { + return false + } + } + return true +} + +// updatedAddrEventsEqual is a helper to check whether two +// event.EvtLocalAddressesUpdated are equal, ignoring the ordering of +// addresses in the inner lists. +func updatedAddrEventsEqual(a, b event.EvtLocalAddressesUpdated) bool { + return a.Diffs == b.Diffs && + updatedAddrsEqual(a.Current, b.Current) && + updatedAddrsEqual(a.Removed, b.Removed) +} From f86b996026821fc4c8994be4e12edabd0a504457 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 27 Jan 2020 10:44:53 -0500 Subject: [PATCH 15/43] rename option (RoutingState -> PeerRecord) --- p2p/protocol/identify/opts.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index 1a01dc0dcd..402030d5af 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -1,9 +1,9 @@ package identify type config struct { - userAgent string - disableSignedAddrSupport bool - includeLocalAddrsInRoutingState bool + userAgent string + disableSignedAddrSupport bool + includeLocalAddrsInPeerRecord bool } // Option is an option function for identify. @@ -26,6 +26,14 @@ func DisableSignedAddrSupportForTesting() Option { } } +// IncludeLocalAddrsInPeerRecord controls whether the signed PeerRecords that are exchanged +// with other peers during the identify exchange will include local addresses. +// +// Addresses are considered "local" if they are IP loopback addresses, or if they belong to +// a reserved private IP address range. +// +// By default, local addresses are not included in PeerRecords. Use this option to +// force them to be included. func IncludeLocalAddrsInPeerRecord(cfg *config) { - cfg.includeLocalAddrsInRoutingState = true + cfg.includeLocalAddrsInPeerRecord = true } From 5fc5ead4a960b552bb707714f4c34b62f4d5cece Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 27 Jan 2020 10:45:19 -0500 Subject: [PATCH 16/43] change NewPeerRecordManager to not accept Host - makes testing it in isolation easier --- p2p/protocol/identify/id.go | 3 ++- p2p/protocol/identify/peer_record.go | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 08bc063198..9b6f85066e 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -139,7 +139,8 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { log.Warningf("identify service not emitting peer protocol updates; err: %s", err) } - s.peerRecordManager, err = NewPeerRecordManager(ctx, h, cfg.includeLocalAddrsInRoutingState) + hostKey := h.Peerstore().PrivKey(h.ID()) + s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs(), cfg.includeLocalAddrsInPeerRecord) if err != nil { log.Warnf("identify service not tracking routing state changes; err: %s", err) } diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index f163d5cde2..3df0bc54a5 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -2,12 +2,10 @@ package identify import ( "context" - "errors" "fmt" "github.com/libp2p/go-eventbus" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/event" - "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/record" "github.com/multiformats/go-multiaddr" @@ -39,29 +37,27 @@ type peerRecordManager struct { } } -func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs bool) (*peerRecordManager, error) { - hostKey := host.Peerstore().PrivKey(host.ID()) - if hostKey == nil { - return nil, errors.New("unable to get private key for host") +func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr, includeLocalAddrs bool) (*peerRecordManager, error) { + hostID, err := peer.IDFromPrivateKey(hostKey) + if err != nil { + return nil, err } m := &peerRecordManager{ ctx: ctx, signingKey: hostKey, - hostID: host.ID(), + hostID: hostID, includeLocalAddrs: includeLocalAddrs, } - if len(host.Addrs()) != 0 { - initialRec, err := m.makeSignedPeerRecord(host.Addrs()) + if len(initialAddrs) != 0 { + initialRec, err := m.makeSignedPeerRecord(initialAddrs) if err != nil { return nil, fmt.Errorf("error constructing initial peer record: %w", err) } m.latest = initialRec } - var err error - bus := host.EventBus() m.subscriptions.localAddrsUpdated, err = bus.Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) if err != nil { return nil, err @@ -72,6 +68,11 @@ func NewPeerRecordManager(ctx context.Context, host host.Host, includeLocalAddrs } go m.handleEvents() + + if m.latest != nil { + m.emitLatest() + } + return m, nil } From c86f6e370b43e2965e222a70bdd01f11ba4d634c Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 27 Jan 2020 10:45:31 -0500 Subject: [PATCH 17/43] tests for peerRecordManager behavior --- p2p/protocol/identify/peer_record_test.go | 170 ++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 p2p/protocol/identify/peer_record_test.go diff --git a/p2p/protocol/identify/peer_record_test.go b/p2p/protocol/identify/peer_record_test.go new file mode 100644 index 0000000000..79dcbb7a29 --- /dev/null +++ b/p2p/protocol/identify/peer_record_test.go @@ -0,0 +1,170 @@ +package identify + +import ( + "context" + "testing" + "time" + + "github.com/libp2p/go-eventbus" + "github.com/libp2p/go-libp2p-core/crypto" + "github.com/libp2p/go-libp2p-core/event" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p-core/test" + "github.com/multiformats/go-multiaddr" +) + +func TestPeerRecordManagerEmitsPeerRecord(t *testing.T) { + bus, emitter, sub := setupEventBus(t) + priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) + if err != nil { + t.Fatalf("error generating test keypair: %v", err) + } + + ctx := context.Background() + var initialAddrs []multiaddr.Multiaddr + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) + if err != nil { + t.Fatalf("error creating peerRecordManager: %v", err) + } + + addrs := []multiaddr.Multiaddr{ + multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), + } + addrEvent := makeAddrEvent(addrs) + err = emitter.Emit(addrEvent) + test.AssertNilError(t, err) + + evt := getNextPeerRecordEvent(t, sub) + peerRec := peerRecordFromEvent(t, evt) + test.AssertAddressesEqual(t, addrs, peerRec.Addrs) +} + +func TestPeerRecordExcludesLocalAddrsIfFlagIsUnset(t *testing.T) { + bus, emitter, sub := setupEventBus(t) + priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) + if err != nil { + t.Fatalf("error generating test keypair: %v", err) + } + + ctx := context.Background() + var initialAddrs []multiaddr.Multiaddr + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) + if err != nil { + t.Fatalf("error creating peerRecordManager: %v", err) + } + + addrs := []multiaddr.Multiaddr{ + multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), + multiaddr.StringCast("/ip4/192.168.1.3/tcp/4321"), + multiaddr.StringCast("/ip4/127.0.0.1/tcp/1234"), + } + addrEvent := makeAddrEvent(addrs) + err = emitter.Emit(addrEvent) + test.AssertNilError(t, err) + + evt := getNextPeerRecordEvent(t, sub) + peerRec := peerRecordFromEvent(t, evt) + expected := []multiaddr.Multiaddr{ + multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), + } + test.AssertAddressesEqual(t, expected, peerRec.Addrs) +} + +func TestPeerRecordIncludesLocalAddrsIfFlagIsSet(t *testing.T) { + bus, emitter, sub := setupEventBus(t) + priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) + if err != nil { + t.Fatalf("error generating test keypair: %v", err) + } + + ctx := context.Background() + var initialAddrs []multiaddr.Multiaddr + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, true) + if err != nil { + t.Fatalf("error creating peerRecordManager: %v", err) + } + + addrs := []multiaddr.Multiaddr{ + multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), + multiaddr.StringCast("/ip4/192.168.1.3/tcp/4321"), + multiaddr.StringCast("/ip4/127.0.0.1/tcp/1234"), + } + addrEvent := makeAddrEvent(addrs) + err = emitter.Emit(addrEvent) + test.AssertNilError(t, err) + + evt := getNextPeerRecordEvent(t, sub) + peerRec := peerRecordFromEvent(t, evt) + test.AssertAddressesEqual(t, addrs, peerRec.Addrs) +} + +func TestPeerRecordManagerEmitsRecordImmediatelyIfInitialAddrsAreProvided(t *testing.T) { + bus, _, sub := setupEventBus(t) + priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) + if err != nil { + t.Fatalf("error generating test keypair: %v", err) + } + + ctx := context.Background() + initialAddrs := []multiaddr.Multiaddr{ + multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), + } + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, true) + if err != nil { + t.Fatalf("error creating peerRecordManager: %v", err) + } + + recEvent := getNextPeerRecordEvent(t, sub) + rec, err := recEvent.SignedRecord.Record() + test.AssertNilError(t, err) + test.AssertAddressesEqual(t, initialAddrs, rec.(*peer.PeerRecord).Addrs) +} + +func setupEventBus(t *testing.T) (bus event.Bus, emitter event.Emitter, sub event.Subscription) { + t.Helper() + bus = eventbus.NewBus() + emitter, err := bus.Emitter(&event.EvtLocalAddressesUpdated{}) + if err != nil { + t.Fatalf("error creating event emitter: %v", err) + } + + sub, err = bus.Subscribe(&event.EvtLocalPeerRecordUpdated{}) + if err != nil { + t.Fatalf("error creating event subscription: %v", err) + } + return bus, emitter, sub +} + +func getNextPeerRecordEvent(t *testing.T, sub event.Subscription) event.EvtLocalPeerRecordUpdated { + t.Helper() + select { + case e := <-sub.Out(): + return e.(event.EvtLocalPeerRecordUpdated) + case <-time.After(1 * time.Second): + t.Fatalf("timed out waiting for peer record") + return event.EvtLocalPeerRecordUpdated{} + } +} + +func peerRecordFromEvent(t *testing.T, evt event.EvtLocalPeerRecordUpdated) *peer.PeerRecord { + t.Helper() + rec, err := evt.SignedRecord.Record() + if err != nil { + t.Fatalf("error getting PeerRecord from event: %v", err) + return nil + } + peerRec, ok := rec.(*peer.PeerRecord) + if !ok { + t.Fatalf("wrong type for peer record") + return nil + } + return peerRec +} + +func makeAddrEvent(addrs []multiaddr.Multiaddr) event.EvtLocalAddressesUpdated { + current := make([]event.UpdatedAddress, len(addrs)) + for i, a := range addrs { + current[i] = event.UpdatedAddress{Address: a} + } + return event.EvtLocalAddressesUpdated{Current: current} +} From ad52da5305f64bde84a940dc3b2c1c0b7da6815e Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 27 Jan 2020 12:01:39 -0500 Subject: [PATCH 18/43] fix racy test --- p2p/host/basic/basic_host_test.go | 42 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 3ad38b6612..e849ee378d 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -569,30 +569,30 @@ func TestHostAddrChangeDetection(t *testing.T) { t.Fatalf("expected 0 addrs, got %d", len(addrs)) } - // Drain EvtLocalAddressesUpdated from the bus in a goroutine until we have as many as we expect - var receivedEvents []event.EvtLocalAddressesUpdated - go func() { - for { - select { - case evt, more := <-sub.Out(): - if !more { - return - } - receivedEvents = append(receivedEvents, evt.(event.EvtLocalAddressesUpdated)) - if len(receivedEvents) == len(expectedEvents) { - return - } - case <-ctx.Done(): - return - } - } - }() - - // Advance between addrSets, with a little delay to let the event propagate over the bus + // Advance between addrSets for i := 1; i < len(addrSets); i++ { currentAddrSet = i h.CheckForAddressChanges() // forces the host to check for changes now, instead of waiting for background update - time.Sleep(100 * time.Millisecond) + } + + // drain events from the subscription + var receivedEvents []event.EvtLocalAddressesUpdated +readEvents: + for { + select { + case evt, more := <-sub.Out(): + if !more { + break readEvents + } + receivedEvents = append(receivedEvents, evt.(event.EvtLocalAddressesUpdated)) + if len(receivedEvents) == len(expectedEvents) { + break readEvents + } + case <-ctx.Done(): + break readEvents + case <-time.After(1 * time.Second): + break readEvents + } } // assert that we received the events we expected From 63c99a2d5ccf0d11cc3122db00e821e70aac30e4 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 27 Jan 2020 13:06:16 -0500 Subject: [PATCH 19/43] include local addrs in peer records by defaul --- p2p/protocol/identify/id.go | 2 +- p2p/protocol/identify/id_test.go | 4 ++-- p2p/protocol/identify/opts.go | 18 ++++++++++-------- p2p/protocol/identify/peer_record.go | 8 ++++---- p2p/protocol/identify/peer_record_test.go | 10 +++++----- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 9b6f85066e..5111baad99 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -140,7 +140,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } hostKey := h.Peerstore().PrivKey(h.ID()) - s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs(), cfg.includeLocalAddrsInPeerRecord) + s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs(), cfg.excludeLocalAddrsFromPeerRecord) if err != nil { log.Warnf("identify service not tracking routing state changes; err: %s", err) } diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 49455936b7..366491271a 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -36,8 +36,8 @@ func subtestIDService(t *testing.T) { h1p := h1.ID() h2p := h2.ID() - ids1 := identify.NewIDService(ctx, h1, identify.IncludeLocalAddrsInPeerRecord) - ids2 := identify.NewIDService(ctx, h2, identify.IncludeLocalAddrsInPeerRecord) + ids1 := identify.NewIDService(ctx, h1) + ids2 := identify.NewIDService(ctx, h2) testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index 402030d5af..087a13a96f 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -1,9 +1,9 @@ package identify type config struct { - userAgent string - disableSignedAddrSupport bool - includeLocalAddrsInPeerRecord bool + userAgent string + disableSignedAddrSupport bool + excludeLocalAddrsFromPeerRecord bool } // Option is an option function for identify. @@ -26,14 +26,16 @@ func DisableSignedAddrSupportForTesting() Option { } } -// IncludeLocalAddrsInPeerRecord controls whether the signed PeerRecords that are exchanged +// ExcludeLocalAddrsFromPeerRecord controls whether the signed PeerRecords that are exchanged // with other peers during the identify exchange will include local addresses. // // Addresses are considered "local" if they are IP loopback addresses, or if they belong to // a reserved private IP address range. // -// By default, local addresses are not included in PeerRecords. Use this option to -// force them to be included. -func IncludeLocalAddrsInPeerRecord(cfg *config) { - cfg.includeLocalAddrsInPeerRecord = true +// By default, local addresses are included in PeerRecords. To exclude them, +// pass ExcludeLocalAddrsFromPeerRecord(true) into NewIDService. +func ExcludeLocalAddrsFromPeerRecord(exclude bool) Option { + return func(cfg *config) { + cfg.excludeLocalAddrsFromPeerRecord = exclude + } } diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index 3df0bc54a5..4e3b25fc7f 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -28,7 +28,7 @@ type peerRecordManager struct { signingKey crypto.PrivKey ctx context.Context - includeLocalAddrs bool + excludeLocalAddrs bool subscriptions struct { localAddrsUpdated event.Subscription } @@ -37,7 +37,7 @@ type peerRecordManager struct { } } -func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr, includeLocalAddrs bool) (*peerRecordManager, error) { +func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr, excludeLocalAddrs bool) (*peerRecordManager, error) { hostID, err := peer.IDFromPrivateKey(hostKey) if err != nil { return nil, err @@ -47,7 +47,7 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri ctx: ctx, signingKey: hostKey, hostID: hostID, - includeLocalAddrs: includeLocalAddrs, + excludeLocalAddrs: excludeLocalAddrs, } if len(initialAddrs) != 0 { @@ -129,7 +129,7 @@ func (m *peerRecordManager) makeSignedPeerRecord(current []multiaddr.Multiaddr) if a == nil { continue } - if m.includeLocalAddrs || manet.IsPublicAddr(a) { + if !m.excludeLocalAddrs || manet.IsPublicAddr(a) { addrs = append(addrs, a) } } diff --git a/p2p/protocol/identify/peer_record_test.go b/p2p/protocol/identify/peer_record_test.go index 79dcbb7a29..403dc94180 100644 --- a/p2p/protocol/identify/peer_record_test.go +++ b/p2p/protocol/identify/peer_record_test.go @@ -39,7 +39,7 @@ func TestPeerRecordManagerEmitsPeerRecord(t *testing.T) { test.AssertAddressesEqual(t, addrs, peerRec.Addrs) } -func TestPeerRecordExcludesLocalAddrsIfFlagIsUnset(t *testing.T) { +func TestPeerRecordExcludesLocalAddrsIfFlagIsSet(t *testing.T) { bus, emitter, sub := setupEventBus(t) priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) if err != nil { @@ -48,7 +48,7 @@ func TestPeerRecordExcludesLocalAddrsIfFlagIsUnset(t *testing.T) { ctx := context.Background() var initialAddrs []multiaddr.Multiaddr - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, true) if err != nil { t.Fatalf("error creating peerRecordManager: %v", err) } @@ -70,7 +70,7 @@ func TestPeerRecordExcludesLocalAddrsIfFlagIsUnset(t *testing.T) { test.AssertAddressesEqual(t, expected, peerRec.Addrs) } -func TestPeerRecordIncludesLocalAddrsIfFlagIsSet(t *testing.T) { +func TestPeerRecordIncludesLocalAddrsIfFlagIsUnset(t *testing.T) { bus, emitter, sub := setupEventBus(t) priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) if err != nil { @@ -79,7 +79,7 @@ func TestPeerRecordIncludesLocalAddrsIfFlagIsSet(t *testing.T) { ctx := context.Background() var initialAddrs []multiaddr.Multiaddr - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, true) + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) if err != nil { t.Fatalf("error creating peerRecordManager: %v", err) } @@ -109,7 +109,7 @@ func TestPeerRecordManagerEmitsRecordImmediatelyIfInitialAddrsAreProvided(t *tes initialAddrs := []multiaddr.Multiaddr{ multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), } - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, true) + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) if err != nil { t.Fatalf("error creating peerRecordManager: %v", err) } From ea966e205d2e73b8d2aa0a47e73b91688d6d851d Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 4 Feb 2020 16:07:21 -0500 Subject: [PATCH 20/43] track peer record API changes, update PR deps --- go.mod | 4 ++-- go.sum | 33 ++++++++++++++-------------- p2p/protocol/identify/id.go | 2 +- p2p/protocol/identify/peer_record.go | 2 +- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 404aea8849..e31d574440 100644 --- a/go.mod +++ b/go.mod @@ -13,13 +13,13 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.1-0.20200121152518-5a523a102629 + github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 github.com/libp2p/go-libp2p-nat v0.0.5 github.com/libp2p/go-libp2p-netutil v0.1.0 - github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200121162712-1f4670bb6c34 + github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277 github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-swarm v0.2.2 github.com/libp2p/go-libp2p-testing v0.1.1 diff --git a/go.sum b/go.sum index e68c0c68cb..d5054abe75 100644 --- a/go.sum +++ b/go.sum @@ -56,8 +56,9 @@ github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk= +github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo= @@ -71,14 +72,14 @@ github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj github.com/ipfs/go-cid v0.0.5 h1:o0Ix8e/ql7Zb5UVUJEUfjsWCIY8t48++9lR8qi6oiJU= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= -github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= -github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= +github.com/ipfs/go-datastore v0.3.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= +github.com/ipfs/go-datastore v0.3.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= -github.com/ipfs/go-ds-badger v0.0.7/go.mod h1:qt0/fWzZDoPW6jpQeqUjR5kBfhDNB65jd9YlmAvpQBk= +github.com/ipfs/go-ds-badger v0.2.0/go.mod h1:471n2X/Qtk8rRO1iuxcgdwmHJdWjDj9VRGhaP/tvoZw= github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= -github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZCg+cSZvYb8= +github.com/ipfs/go-ds-leveldb v0.4.0/go.mod h1:NX127692WYenEjmlzyZunFv+f8nrKbH1s9eIaRNnhzs= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1 h1:Wz9bL2wB2YBJqggkA4dD7oSmqB4cAnpNbGrlHJulv50= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= @@ -134,8 +135,8 @@ github.com/libp2p/go-libp2p-core v0.2.0/go.mod h1:X0eyB0Gy93v0DZtSYbEM7RnMChm9Uv github.com/libp2p/go-libp2p-core v0.2.2/go.mod h1:8fcwTbsG2B+lTgRJ1ICZtiM5GWCWZVoVrLaDRvIRng0= github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= -github.com/libp2p/go-libp2p-core v0.3.1-0.20200121152518-5a523a102629 h1:U0mnCQcq57Y67jSgC4DtNpYcyFen91pWnxT4dsZ2M1Q= -github.com/libp2p/go-libp2p-core v0.3.1-0.20200121152518-5a523a102629/go.mod h1:KJbT3ekTPccG9QUSa5vc/0QVZXPE9+3TVlyqESRQic0= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b h1:dStuLsQQG74FsC+frL1GDJ2J4KzFQPwySoMJ3Sihscc= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -151,8 +152,8 @@ github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCTh github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= github.com/libp2p/go-libp2p-peerstore v0.1.3/go.mod h1:BJ9sHlm59/80oSkpWgr1MyY1ciXAXV397W6h1GH/uKI= -github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200121162712-1f4670bb6c34 h1:JCxoYrBcaJGlhbrhzTtRMXel5+fhZ7+jUO/ikD6FYRc= -github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200121162712-1f4670bb6c34/go.mod h1:FF3tFfdIhpdFwOA0R/hwWwEzdmwXzd8K/O3eqBvlwsw= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277 h1:h+4z1eGlFBtNU3idlwfzCRuKUZwvk0wBDXg3zFBpztw= +github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277/go.mod h1:T5L262N+VtcnnPIUt5X/mNcdEC6fz+1a1PvRoQPTIOU= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1 h1:eNWbJTdyPA7NxhP7J3c5lT97DC5d+u+IldkgCYFTPVA= @@ -258,7 +259,6 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS github.com/multiformats/go-multistream v0.1.1 h1:JlAdpIFhBhGRLxe9W6Om0w++Gd6KMWoFPZL/dEnm9nI= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= -github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -270,8 +270,9 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -326,8 +327,8 @@ golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= -golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 h1:+ELyKg6m8UBf0nPFSqD0mi7zUfwPyXo23HNjMnXPz7w= +golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -359,8 +360,8 @@ golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200121082415-34d275377bf9 h1:N19i1HjUnR7TF7rMt8O4p3dLvqvmYyzB6ifMFmrbY50= -golang.org/x/sys v0.0.0-20200121082415-34d275377bf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= +golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -371,7 +372,7 @@ golang.org/x/tools v0.0.0-20181130052023-1c3d964395ce/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20200121145236-13c74800b9d2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200204192400-7124308813f3/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 5111baad99..0b711c25f9 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -439,7 +439,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // add signed addrs if we have them and the peerstore supports them cab, ok := peerstore.GetCertifiedAddrBook(ids.Host.Peerstore()) if ok && signedPeerRecord != nil { - addErr := cab.ProcessPeerRecord(signedPeerRecord, ttl) + _, addErr := cab.ConsumePeerRecord(signedPeerRecord, ttl) if addErr != nil { log.Errorf("error adding signed addrs to peerstore: %v", addErr) // fall back to adding unsigned addrs diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index 4e3b25fc7f..a3f5a3a0ef 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -137,7 +137,7 @@ func (m *peerRecordManager) makeSignedPeerRecord(current []multiaddr.Multiaddr) rec := peer.NewPeerRecord() rec.PeerID = m.hostID rec.Addrs = addrs - return rec.Sign(m.signingKey) + return record.Seal(rec, m.signingKey) } func addrsFromEvent(evt event.EvtLocalAddressesUpdated) []multiaddr.Multiaddr { From 64f13538b4290e905edb5e119c47baa6f3420c95 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 4 Feb 2020 16:24:55 -0500 Subject: [PATCH 21/43] name changes + mutex around latest peer record --- go.mod | 2 +- go.sum | 2 ++ p2p/protocol/identify/peer_record.go | 16 ++++++++++++---- p2p/protocol/identify/peer_record_test.go | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index e31d574440..ebe8ca34c4 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b + github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 diff --git a/go.sum b/go.sum index d5054abe75..2233fc35f7 100644 --- a/go.sum +++ b/go.sum @@ -137,6 +137,8 @@ github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b h1:dStuLsQQG74FsC+frL1GDJ2J4KzFQPwySoMJ3Sihscc= github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf h1:LzjCXLcsGn5e3tN0f8DHGsj+kajoLyyLoc9pzMtiqXU= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index a3f5a3a0ef..715fc6f1c5 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -10,6 +10,7 @@ import ( "github.com/libp2p/go-libp2p-core/record" "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" + "sync" ) // peerRecordManager creates new signed peer.PeerRecords that can @@ -23,6 +24,7 @@ import ( // will immediately receive the current record when they subscribe, with future // records delivered in future events. type peerRecordManager struct { + lock sync.RWMutex latest *record.Envelope hostID peer.ID signingKey crypto.PrivKey @@ -33,7 +35,7 @@ type peerRecordManager struct { localAddrsUpdated event.Subscription } emitters struct { - evtLocalRoutingStateUpdated event.Emitter + evtLocalPeerRecordUpdated event.Emitter } } @@ -62,7 +64,7 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri if err != nil { return nil, err } - m.emitters.evtLocalRoutingStateUpdated, err = bus.Emitter(&event.EvtLocalPeerRecordUpdated{}, eventbus.Stateful) + m.emitters.evtLocalPeerRecordUpdated, err = bus.Emitter(&event.EvtLocalPeerRecordUpdated{}, eventbus.Stateful) if err != nil { return nil, err } @@ -80,6 +82,8 @@ func (m *peerRecordManager) LatestRecord() *record.Envelope { if m == nil { return nil } + m.lock.RLock() + defer m.lock.RUnlock() return m.latest } @@ -111,13 +115,17 @@ func (m *peerRecordManager) update(evt event.EvtLocalAddressesUpdated) { log.Warnf("error creating signed peer record: %v", err) return } + m.lock.Lock() m.latest = envelope + m.lock.Unlock() m.emitLatest() } func (m *peerRecordManager) emitLatest() { - stateEvt := event.EvtLocalPeerRecordUpdated{SignedRecord: m.latest} - err := m.emitters.evtLocalRoutingStateUpdated.Emit(stateEvt) + m.lock.RLock() + stateEvt := event.EvtLocalPeerRecordUpdated{Record: m.latest} + m.lock.RUnlock() + err := m.emitters.evtLocalPeerRecordUpdated.Emit(stateEvt) if err != nil { log.Warnf("error emitting event for updated peer record: %v", err) } diff --git a/p2p/protocol/identify/peer_record_test.go b/p2p/protocol/identify/peer_record_test.go index 403dc94180..8ae00427d0 100644 --- a/p2p/protocol/identify/peer_record_test.go +++ b/p2p/protocol/identify/peer_record_test.go @@ -115,7 +115,7 @@ func TestPeerRecordManagerEmitsRecordImmediatelyIfInitialAddrsAreProvided(t *tes } recEvent := getNextPeerRecordEvent(t, sub) - rec, err := recEvent.SignedRecord.Record() + rec, err := recEvent.Record.Record() test.AssertNilError(t, err) test.AssertAddressesEqual(t, initialAddrs, rec.(*peer.PeerRecord).Addrs) } @@ -148,7 +148,7 @@ func getNextPeerRecordEvent(t *testing.T, sub event.Subscription) event.EvtLocal func peerRecordFromEvent(t *testing.T, evt event.EvtLocalPeerRecordUpdated) *peer.PeerRecord { t.Helper() - rec, err := evt.SignedRecord.Record() + rec, err := evt.Record.Record() if err != nil { t.Fatalf("error getting PeerRecord from event: %v", err) return nil From 6ecbfea79b42b28f144b801ab8bd37b859265566 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 4 Feb 2020 16:38:38 -0500 Subject: [PATCH 22/43] doc comments --- p2p/protocol/identify/peer_record.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index 715fc6f1c5..f5e9343785 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -40,6 +40,17 @@ type peerRecordManager struct { } func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr, excludeLocalAddrs bool) (*peerRecordManager, error) { +// NewPeerRecordManager creates a peerRecordManager that will subscribe to the given event.Bus +// and listen for changes in the local Host's addresses, emitting new signed peer.PeerRecords +// in response. The new records will be contained in event.EvtLocalPeerRecordUpdated events +// and emitted on the event bus. +// +// PeerRecords will be signed with the given private key, which must be the libp2p Host's +// identity key for the resulting records to be valid. +// +// If initialAddrs is non-empty, a PeerRecord will be created immediately and emitted on +// the bus, without waiting for an event.LocalPeerAddressesUpdated event to trigger an +// update. hostID, err := peer.IDFromPrivateKey(hostKey) if err != nil { return nil, err @@ -78,6 +89,10 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri return m, nil } +// LatestRecord returns the most recently constructed signed PeerRecord. +// If you have a direct reference to the peerRecordManager, this method +// gives you a simple way to access the latest record without pulling +// from the event bus. func (m *peerRecordManager) LatestRecord() *record.Envelope { if m == nil { return nil From 1f922f01563b4686a93138e9d5a569eb36aef266 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 4 Feb 2020 16:40:51 -0500 Subject: [PATCH 23/43] remove option to filter local addrs from peer rec --- p2p/protocol/identify/id.go | 2 +- p2p/protocol/identify/opts.go | 14 ----- p2p/protocol/identify/peer_record.go | 23 ++++----- p2p/protocol/identify/peer_record_test.go | 63 +---------------------- 4 files changed, 12 insertions(+), 90 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 0b711c25f9..d1f4919ef4 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -140,7 +140,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } hostKey := h.Peerstore().PrivKey(h.ID()) - s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs(), cfg.excludeLocalAddrsFromPeerRecord) + s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs()) if err != nil { log.Warnf("identify service not tracking routing state changes; err: %s", err) } diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index 087a13a96f..fd81614855 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -25,17 +25,3 @@ func DisableSignedAddrSupportForTesting() Option { cfg.disableSignedAddrSupport = true } } - -// ExcludeLocalAddrsFromPeerRecord controls whether the signed PeerRecords that are exchanged -// with other peers during the identify exchange will include local addresses. -// -// Addresses are considered "local" if they are IP loopback addresses, or if they belong to -// a reserved private IP address range. -// -// By default, local addresses are included in PeerRecords. To exclude them, -// pass ExcludeLocalAddrsFromPeerRecord(true) into NewIDService. -func ExcludeLocalAddrsFromPeerRecord(exclude bool) Option { - return func(cfg *config) { - cfg.excludeLocalAddrsFromPeerRecord = exclude - } -} diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index f5e9343785..3b18af0438 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -9,7 +9,6 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/record" "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr-net" "sync" ) @@ -29,9 +28,8 @@ type peerRecordManager struct { hostID peer.ID signingKey crypto.PrivKey - ctx context.Context - excludeLocalAddrs bool - subscriptions struct { + ctx context.Context + subscriptions struct { localAddrsUpdated event.Subscription } emitters struct { @@ -39,7 +37,6 @@ type peerRecordManager struct { } } -func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr, excludeLocalAddrs bool) (*peerRecordManager, error) { // NewPeerRecordManager creates a peerRecordManager that will subscribe to the given event.Bus // and listen for changes in the local Host's addresses, emitting new signed peer.PeerRecords // in response. The new records will be contained in event.EvtLocalPeerRecordUpdated events @@ -51,16 +48,16 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri // If initialAddrs is non-empty, a PeerRecord will be created immediately and emitted on // the bus, without waiting for an event.LocalPeerAddressesUpdated event to trigger an // update. +func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr) (*peerRecordManager, error) { hostID, err := peer.IDFromPrivateKey(hostKey) if err != nil { return nil, err } m := &peerRecordManager{ - ctx: ctx, - signingKey: hostKey, - hostID: hostID, - excludeLocalAddrs: excludeLocalAddrs, + ctx: ctx, + signingKey: hostKey, + hostID: hostID, } if len(initialAddrs) != 0 { @@ -80,12 +77,12 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri return nil, err } - go m.handleEvents() - if m.latest != nil { m.emitLatest() } + go m.handleEvents() + return m, nil } @@ -152,9 +149,7 @@ func (m *peerRecordManager) makeSignedPeerRecord(current []multiaddr.Multiaddr) if a == nil { continue } - if !m.excludeLocalAddrs || manet.IsPublicAddr(a) { - addrs = append(addrs, a) - } + addrs = append(addrs, a) } rec := peer.NewPeerRecord() diff --git a/p2p/protocol/identify/peer_record_test.go b/p2p/protocol/identify/peer_record_test.go index 8ae00427d0..702fdc3715 100644 --- a/p2p/protocol/identify/peer_record_test.go +++ b/p2p/protocol/identify/peer_record_test.go @@ -22,7 +22,7 @@ func TestPeerRecordManagerEmitsPeerRecord(t *testing.T) { ctx := context.Background() var initialAddrs []multiaddr.Multiaddr - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs) if err != nil { t.Fatalf("error creating peerRecordManager: %v", err) } @@ -39,65 +39,6 @@ func TestPeerRecordManagerEmitsPeerRecord(t *testing.T) { test.AssertAddressesEqual(t, addrs, peerRec.Addrs) } -func TestPeerRecordExcludesLocalAddrsIfFlagIsSet(t *testing.T) { - bus, emitter, sub := setupEventBus(t) - priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - t.Fatalf("error generating test keypair: %v", err) - } - - ctx := context.Background() - var initialAddrs []multiaddr.Multiaddr - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, true) - if err != nil { - t.Fatalf("error creating peerRecordManager: %v", err) - } - - addrs := []multiaddr.Multiaddr{ - multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), - multiaddr.StringCast("/ip4/192.168.1.3/tcp/4321"), - multiaddr.StringCast("/ip4/127.0.0.1/tcp/1234"), - } - addrEvent := makeAddrEvent(addrs) - err = emitter.Emit(addrEvent) - test.AssertNilError(t, err) - - evt := getNextPeerRecordEvent(t, sub) - peerRec := peerRecordFromEvent(t, evt) - expected := []multiaddr.Multiaddr{ - multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), - } - test.AssertAddressesEqual(t, expected, peerRec.Addrs) -} - -func TestPeerRecordIncludesLocalAddrsIfFlagIsUnset(t *testing.T) { - bus, emitter, sub := setupEventBus(t) - priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - t.Fatalf("error generating test keypair: %v", err) - } - - ctx := context.Background() - var initialAddrs []multiaddr.Multiaddr - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) - if err != nil { - t.Fatalf("error creating peerRecordManager: %v", err) - } - - addrs := []multiaddr.Multiaddr{ - multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), - multiaddr.StringCast("/ip4/192.168.1.3/tcp/4321"), - multiaddr.StringCast("/ip4/127.0.0.1/tcp/1234"), - } - addrEvent := makeAddrEvent(addrs) - err = emitter.Emit(addrEvent) - test.AssertNilError(t, err) - - evt := getNextPeerRecordEvent(t, sub) - peerRec := peerRecordFromEvent(t, evt) - test.AssertAddressesEqual(t, addrs, peerRec.Addrs) -} - func TestPeerRecordManagerEmitsRecordImmediatelyIfInitialAddrsAreProvided(t *testing.T) { bus, _, sub := setupEventBus(t) priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) @@ -109,7 +50,7 @@ func TestPeerRecordManagerEmitsRecordImmediatelyIfInitialAddrsAreProvided(t *tes initialAddrs := []multiaddr.Multiaddr{ multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), } - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs, false) + _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs) if err != nil { t.Fatalf("error creating peerRecordManager: %v", err) } From 58fe0620a1f6b93d5867a25372b5c24d4194db87 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 4 Feb 2020 16:43:03 -0500 Subject: [PATCH 24/43] naming --- p2p/protocol/identify/id.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index d1f4919ef4..dd2e4b56bb 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -346,13 +346,13 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { // Generate a signed peer record containing our listen addresses and // send it along with the unsigned addrs - signedRecord := ids.peerRecordManager.LatestRecord() - if ids.useSignedAddrs && signedRecord != nil { - envelopeBytes, err := signedRecord.Marshal() + rec := ids.peerRecordManager.LatestRecord() + if ids.useSignedAddrs && rec != nil { + recBytes, err := rec.Marshal() if err != nil { - log.Warnf("error marshaling signed routing state: %v", err) + log.Warnf("error marshaling signed peer record: %v", err) } else { - mes.SignedPeerRecord = envelopeBytes + mes.SignedPeerRecord = recBytes } } From c019ac1e8c2aafcda089d95f3b0053da5a2b23fd Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Tue, 4 Feb 2020 18:06:42 -0500 Subject: [PATCH 25/43] define new protocol versions for id & id/push --- p2p/protocol/identify/id.go | 87 +++++++++++++++++++------------ p2p/protocol/identify/id_delta.go | 2 +- p2p/protocol/identify/id_push.go | 15 ++++-- p2p/protocol/identify/id_test.go | 20 +++---- p2p/protocol/identify/opts.go | 14 +---- 5 files changed, 73 insertions(+), 65 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index dd2e4b56bb..45ed9668a1 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -30,7 +30,11 @@ import ( var log = logging.Logger("net/identify") // ID is the protocol.ID of the Identify Service. -const ID = "/ipfs/id/1.0.0" +const ID = "/p2p/id/1.1.0" + +// LegacyID is the protocol.ID of version 1.0.0 of the identify +// service, which does not support signed peer records. +const LegacyID = "/ipfs/id/1.0.0" // LibP2PVersion holds the current protocol version for a client running this code // TODO(jbenet): fix the versioning mess. @@ -85,7 +89,6 @@ type IDService struct { observedAddrs *ObservedAddrSet peerRecordManager *peerRecordManager - useSignedAddrs bool subscriptions struct { localProtocolsUpdated event.Subscription @@ -113,10 +116,9 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { Host: h, UserAgent: userAgent, - ctx: ctx, - currid: make(map[network.Conn]chan struct{}), - observedAddrs: NewObservedAddrSet(ctx), - useSignedAddrs: !cfg.disableSignedAddrSupport, + ctx: ctx, + currid: make(map[network.Conn]chan struct{}), + observedAddrs: NewObservedAddrSet(ctx), } // handle local protocol handler updates, and push deltas to peers. @@ -146,7 +148,9 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } h.SetStreamHandler(ID, s.requestHandler) + h.SetStreamHandler(LegacyID, s.requestHandler) h.SetStreamHandler(IDPush, s.pushHandler) + h.SetStreamHandler(LegacyIDPush, s.pushHandler) h.SetStreamHandler(IDDelta, s.deltaHandler) h.Network().Notify((*netNotifiee)(s)) return s @@ -219,9 +223,10 @@ func (ids *IDService) IdentifyConn(c network.Conn) { } s.SetProtocol(ID) + protocolIDs := []string{ID, LegacyID} // ok give the response to our handler. - if err := msmux.SelectProtoOrFail(ID, s); err != nil { + if _, err := msmux.SelectOneOf(protocolIDs, s); err != nil { log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer(), logging.Metadata{"error": err}) s.Reset() return @@ -230,13 +235,23 @@ func (ids *IDService) IdentifyConn(c network.Conn) { ids.responseHandler(s) } +func protoSupportsPeerRecords(proto protocol.ID) bool { + switch proto { + case ID: + return true + case IDPush: + return true + } + return false +} + func (ids *IDService) requestHandler(s network.Stream) { defer helpers.FullClose(s) c := s.Conn() w := ggio.NewDelimitedWriter(s) mes := pb.Identify{} - ids.populateMessage(&mes, s.Conn()) + ids.populateMessage(&mes, s.Conn(), protoSupportsPeerRecords(s.Protocol())) w.WriteMsg(&mes) log.Debugf("%s sent message to %s %s", ID, c.RemotePeer(), c.RemoteMultiaddr()) @@ -256,14 +271,15 @@ func (ids *IDService) responseHandler(s network.Stream) { defer func() { go helpers.FullClose(s) }() log.Debugf("%s received message from %s %s", s.Protocol(), c.RemotePeer(), c.RemoteMultiaddr()) - ids.consumeMessage(&mes, c) + ids.consumeMessage(&mes, c, protoSupportsPeerRecords(s.Protocol())) } -func (ids *IDService) broadcast(proto protocol.ID, payloadWriter func(s network.Stream)) { +func (ids *IDService) broadcast(protos []protocol.ID, payloadWriter func(s network.Stream)) { var wg sync.WaitGroup + protoStrs := protocol.ConvertToStrings(protos) ctx, cancel := context.WithTimeout(ids.ctx, 30*time.Second) - ctx = network.WithNoDial(ctx, string(proto)) + ctx = network.WithNoDial(ctx, protoStrs[0]) pstore := ids.Host.Peerstore() for _, p := range ids.Host.Network().Peers() { @@ -290,13 +306,13 @@ func (ids *IDService) broadcast(proto protocol.ID, payloadWriter func(s network. } // avoid the unnecessary stream if the peer does not support the protocol. - if sup, err := pstore.SupportsProtocols(p, string(proto)); err != nil && len(sup) == 0 { + if sup, err := pstore.SupportsProtocols(p, protoStrs...); err != nil && len(sup) == 0 { // the peer does not support the required protocol. return } // if the peerstore query errors, we go ahead anyway. - s, err := ids.Host.NewStream(ctx, p, proto) + s, err := ids.Host.NewStream(ctx, p, protos...) if err != nil { log.Debugf("error opening push stream to %s: %s", p, err.Error()) return @@ -324,7 +340,7 @@ func (ids *IDService) broadcast(proto protocol.ID, payloadWriter func(s network. }() } -func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { +func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn, usePeerRecords bool) { // set protocols this node is currently handling protos := ids.Host.Mux().Protocols() mes.Protocols = make([]string, len(protos)) @@ -336,24 +352,29 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { // "public" address, at least in relation to us. mes.ObservedAddr = c.RemoteMultiaddr().Bytes() - // set listen addrs, get our latest addrs from Host. - laddrs := ids.Host.Addrs() - mes.ListenAddrs = make([][]byte, len(laddrs)) - for i, addr := range laddrs { - mes.ListenAddrs[i] = addr.Bytes() - } - log.Debugf("%s sent listen addrs to %s: %s", c.LocalPeer(), c.RemotePeer(), laddrs) - - // Generate a signed peer record containing our listen addresses and - // send it along with the unsigned addrs - rec := ids.peerRecordManager.LatestRecord() - if ids.useSignedAddrs && rec != nil { - recBytes, err := rec.Marshal() - if err != nil { - log.Warnf("error marshaling signed peer record: %v", err) + if usePeerRecords { + // Generate a signed peer record containing our listen addresses and + // send it instead of unsigned addrs + rec := ids.peerRecordManager.LatestRecord() + if rec == nil { + log.Errorf("latest PeerRecord does not exist. identify message incomplete!") } else { - mes.SignedPeerRecord = recBytes + recBytes, err := rec.Marshal() + if err != nil { + log.Warnf("error marshaling peer record: %v", err) + } else { + mes.SignedPeerRecord = recBytes + log.Debugf("%s sent peer record to %s", c.LocalPeer(), c.RemotePeer()) + } + } + } else { + // set listen addrs, get our latest addrs from Host. + laddrs := ids.Host.Addrs() + mes.ListenAddrs = make([][]byte, len(laddrs)) + for i, addr := range laddrs { + mes.ListenAddrs[i] = addr.Bytes() } + log.Debugf("%s sent listen addrs to %s: %s", c.LocalPeer(), c.RemotePeer(), laddrs) } // set our public key @@ -384,7 +405,7 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { mes.AgentVersion = &av } -func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { +func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn, usePeerRecords bool) { p := c.RemotePeer() // mes.Protocols @@ -416,7 +437,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { // add certified addresses for the peer, if they sent us a signed peer record var signedPeerRecord *record.Envelope - if ids.useSignedAddrs { + if usePeerRecords { var err error signedPeerRecord, err = signedPeerRecordFromMessage(mes) if err != nil { @@ -442,8 +463,6 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) { _, addErr := cab.ConsumePeerRecord(signedPeerRecord, ttl) if addErr != nil { log.Errorf("error adding signed addrs to peerstore: %v", addErr) - // fall back to adding unsigned addrs - ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl) } } else { ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl) diff --git a/p2p/protocol/identify/id_delta.go b/p2p/protocol/identify/id_delta.go index 2cc95b7290..9ef9e06407 100644 --- a/p2p/protocol/identify/id_delta.go +++ b/p2p/protocol/identify/id_delta.go @@ -57,7 +57,7 @@ func (ids *IDService) fireProtocolDelta(evt event.EvtLocalProtocolsUpdated) { } log.Debugf("%s sent delta update to %s: %s", IDDelta, c.RemotePeer(), c.RemoteMultiaddr()) } - ids.broadcast(IDDelta, deltaWriter) + ids.broadcast([]protocol.ID{IDDelta}, deltaWriter) } // consumeDelta processes an incoming delta from a peer, updating the peerstore diff --git a/p2p/protocol/identify/id_push.go b/p2p/protocol/identify/id_push.go index 7f0104ce73..6b8dfee98e 100644 --- a/p2p/protocol/identify/id_push.go +++ b/p2p/protocol/identify/id_push.go @@ -1,17 +1,26 @@ package identify -import "github.com/libp2p/go-libp2p-core/network" +import ( + "github.com/libp2p/go-libp2p-core/network" + "github.com/libp2p/go-libp2p-core/protocol" +) // IDPush is the protocol.ID of the Identify push protocol. It sends full identify messages containing // the current state of the peer. // // It is in the process of being replaced by identify delta, which sends only diffs for better // resource utilisation. -const IDPush = "/ipfs/id/push/1.0.0" +const IDPush = "/p2p/id/push/1.1.0" + +// LegacyIDPush is the protocol.ID of the previous version of the Identify push protocol, +// which does not support exchanging signed addresses in PeerRecords. +// It is still supported for backwards compatibility if a remote peer does not support +// the current version. +const LegacyIDPush = "/ipfs/id/push/1.0.0" // Push pushes a full identify message to all peers containing the current state. func (ids *IDService) Push() { - ids.broadcast(IDPush, ids.requestHandler) + ids.broadcast([]protocol.ID{IDPush, LegacyIDPush}, ids.requestHandler) } // pushHandler handles incoming identify push streams. The behaviour is identical to the ordinary identify protocol. diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 366491271a..aae7b6aa92 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -446,8 +446,7 @@ func TestUserAgent(t *testing.T) { } } -// make sure that we still use the unsigned listen addresses if the remote peer -// does not send us a signed address record +// make sure that we still support older peers using "legacy" versions of identify func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -457,10 +456,11 @@ func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { defer h2.Close() defer h1.Close() - h1p := h1.ID() h2p := h2.ID() - ids1 := identify.NewIDService(ctx, h1) - ids2 := identify.NewIDService(ctx, h2, identify.DisableSignedAddrSupportForTesting()) + ids := identify.NewIDService(ctx, h1) + + // remove new protocol ID from h2, so it only responds to "legacy" protocol id + h2.RemoveStreamHandler(identify.ID) h2pi := h2.Peerstore().PeerInfo(h2p) if err := h1.Connect(ctx, h2pi); err != nil { @@ -472,19 +472,11 @@ func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { t.Fatal("should have a conn here") } - ids1.IdentifyConn(h1t2c[0]) + ids.IdentifyConn(h1t2c[0]) // the IDService should be opened automatically, by the network. // what we should see now is that both peers know about each others listen addresses. t.Log("test peer1 has peer2 addrs correctly") testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) // should not have signed addrs - - c := h2.Network().ConnsToPeer(h1.ID()) - if len(c) < 1 { - t.Fatal("should have connection by now at least.") - } - ids2.IdentifyConn(c[0]) - testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them - testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) // no signed addrs } diff --git a/p2p/protocol/identify/opts.go b/p2p/protocol/identify/opts.go index fd81614855..670b213b71 100644 --- a/p2p/protocol/identify/opts.go +++ b/p2p/protocol/identify/opts.go @@ -1,9 +1,7 @@ package identify type config struct { - userAgent string - disableSignedAddrSupport bool - excludeLocalAddrsFromPeerRecord bool + userAgent string } // Option is an option function for identify. @@ -15,13 +13,3 @@ func UserAgent(ua string) Option { cfg.userAgent = ua } } - -// DisableSignedAddrSupportForTesting prevents the identify service from sending or parsing -// routing.SignedRoutingState messages during the exchange. Used for testing -// compatibility with older versions that do not support signed addresses. -// Do not use in production! -func DisableSignedAddrSupportForTesting() Option { - return func(cfg *config) { - cfg.disableSignedAddrSupport = true - } -} From 1da3bd530f8e4550db9bd4d18f92881829064ba0 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Mon, 10 Feb 2020 15:21:12 -0500 Subject: [PATCH 26/43] update -core dependency --- go.mod | 4 +++- go.sum | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index ebe8ca34c4..4214d4f1d1 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf + github.com/libp2p/go-libp2p-core v0.3.1-0.20200210195324-7b2888dfdb65 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 @@ -35,6 +35,8 @@ require ( github.com/multiformats/go-multiaddr-net v0.1.2 github.com/multiformats/go-multistream v0.1.1 github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 + golang.org/x/crypto v0.0.0-20200210191831-6ca56c2f2e2b // indirect + golang.org/x/tools v0.0.0-20200210192313-1ace956b0e17 // indirect ) go 1.12 diff --git a/go.sum b/go.sum index 2233fc35f7..77b3b08932 100644 --- a/go.sum +++ b/go.sum @@ -139,6 +139,8 @@ github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b h1:dStuLsQ github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf h1:LzjCXLcsGn5e3tN0f8DHGsj+kajoLyyLoc9pzMtiqXU= github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200210195324-7b2888dfdb65 h1:1pzqPhclcQSUF9eGsrC99AI9DauqZsYjiZB5kY3nK98= +github.com/libp2p/go-libp2p-core v0.3.1-0.20200210195324-7b2888dfdb65/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -331,6 +333,8 @@ golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 h1:+ELyKg6m8UBf0nPFSqD0mi7zUfwPyXo23HNjMnXPz7w= golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200210191831-6ca56c2f2e2b h1:at/SwedEdvIO7WPO4V9Yn1a3MqdSqAo0p6trBocu5aA= +golang.org/x/crypto v0.0.0-20200210191831-6ca56c2f2e2b/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -375,6 +379,8 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20200204192400-7124308813f3/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200210192313-1ace956b0e17 h1:a/Fd23DJvg1CaeDH0dYHahE+hCI0v9rFgxSNIThoUcM= +golang.org/x/tools v0.0.0-20200210192313-1ace956b0e17/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= From 8d316c32664b6fc557a9cb0c475b82c1a15b2d32 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 14 Feb 2020 10:09:11 -0500 Subject: [PATCH 27/43] use struct as map key instead of stringifying --- p2p/host/basic/basic_host_test.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index e849ee378d..6d6e72ab32 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -621,15 +621,23 @@ func updatedAddrsEqual(a, b []event.UpdatedAddress) bool { if len(a) != len(b) { return false } - // ignore ordering in addr lists - aSet := make(map[string]struct{}) + + // We can't use an UpdatedAddress directly as a map key, since + // Multiaddr is an interface, and go won't know how to compare + // for equality. So we convert to this little struct, which + // stores the multiaddr as a string. + type ua struct { + action event.AddrAction + addrStr string + } + aSet := make(map[ua]struct{}) for _, addr := range a { - s := string(addr.Action) + "::" + string(addr.Address.Bytes()) - aSet[s] = struct{}{} + k := ua{action: addr.Action, addrStr: string(addr.Address.Bytes())} + aSet[k] = struct{}{} } for _, addr := range b { - s := string(addr.Action) + "::" + string(addr.Address.Bytes()) - _, ok := aSet[s] + k := ua{action: addr.Action, addrStr: string(addr.Address.Bytes())} + _, ok := aSet[k] if !ok { return false } From ba8f9606aa6e05cc32b16e05f6bb1182f38ccfaa Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 14 Feb 2020 10:10:54 -0500 Subject: [PATCH 28/43] simplify protoSupportsPeerRecords --- p2p/protocol/identify/id.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 45ed9668a1..6e8d498e8a 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -236,13 +236,7 @@ func (ids *IDService) IdentifyConn(c network.Conn) { } func protoSupportsPeerRecords(proto protocol.ID) bool { - switch proto { - case ID: - return true - case IDPush: - return true - } - return false + return proto == ID || proto == IDPush } func (ids *IDService) requestHandler(s network.Stream) { From db2625bfd532afb6360394d7b42ed1cef5025466 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 14 Feb 2020 10:13:59 -0500 Subject: [PATCH 29/43] add nil check before emitting peer record event --- p2p/protocol/identify/peer_record.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index 3b18af0438..81e51793cb 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -135,8 +135,13 @@ func (m *peerRecordManager) update(evt event.EvtLocalAddressesUpdated) { func (m *peerRecordManager) emitLatest() { m.lock.RLock() + defer m.lock.RUnlock() + if m.latest == nil { + log.Warnf("emitLatest called, but no latest record exists") + return + } + stateEvt := event.EvtLocalPeerRecordUpdated{Record: m.latest} - m.lock.RUnlock() err := m.emitters.evtLocalPeerRecordUpdated.Emit(stateEvt) if err != nil { log.Warnf("error emitting event for updated peer record: %v", err) From 839c8d2f20fa3e2214aa0c4e7285341a95d108f6 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 6 Mar 2020 10:01:56 -0500 Subject: [PATCH 30/43] regen with correct gogoproto package --- p2p/protocol/identify/pb/identify.pb.go | 252 +++++++++++------------- 1 file changed, 112 insertions(+), 140 deletions(-) diff --git a/p2p/protocol/identify/pb/identify.pb.go b/p2p/protocol/identify/pb/identify.pb.go index 0c45885944..a9f28d6e15 100644 --- a/p2p/protocol/identify/pb/identify.pb.go +++ b/p2p/protocol/identify/pb/identify.pb.go @@ -8,6 +8,7 @@ import ( proto "github.com/gogo/protobuf/proto" io "io" math "math" + math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. @@ -19,7 +20,7 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type Delta struct { // new protocols now serviced by the peer. @@ -45,7 +46,7 @@ func (m *Delta) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { return xxx_messageInfo_Delta.Marshal(b, m, deterministic) } else { b = b[:cap(b)] - n, err := m.MarshalTo(b) + n, err := m.MarshalToSizedBuffer(b) if err != nil { return nil, err } @@ -123,7 +124,7 @@ func (m *Identify) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { return xxx_messageInfo_Identify.Marshal(b, m, deterministic) } else { b = b[:cap(b)] - n, err := m.MarshalTo(b) + n, err := m.MarshalToSizedBuffer(b) if err != nil { return nil, err } @@ -229,7 +230,7 @@ var fileDescriptor_83f1e7e6b485409f = []byte{ func (m *Delta) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) - n, err := m.MarshalTo(dAtA) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) if err != nil { return nil, err } @@ -237,50 +238,44 @@ func (m *Delta) Marshal() (dAtA []byte, err error) { } func (m *Delta) MarshalTo(dAtA []byte) (int, error) { - var i int + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Delta) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) _ = i var l int _ = l - if len(m.AddedProtocols) > 0 { - for _, s := range m.AddedProtocols { - dAtA[i] = 0xa - i++ - l = len(s) - for l >= 1<<7 { - dAtA[i] = uint8(uint64(l)&0x7f | 0x80) - l >>= 7 - i++ - } - dAtA[i] = uint8(l) - i++ - i += copy(dAtA[i:], s) - } + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) } if len(m.RmProtocols) > 0 { - for _, s := range m.RmProtocols { + for iNdEx := len(m.RmProtocols) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.RmProtocols[iNdEx]) + copy(dAtA[i:], m.RmProtocols[iNdEx]) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.RmProtocols[iNdEx]))) + i-- dAtA[i] = 0x12 - i++ - l = len(s) - for l >= 1<<7 { - dAtA[i] = uint8(uint64(l)&0x7f | 0x80) - l >>= 7 - i++ - } - dAtA[i] = uint8(l) - i++ - i += copy(dAtA[i:], s) } } - if m.XXX_unrecognized != nil { - i += copy(dAtA[i:], m.XXX_unrecognized) + if len(m.AddedProtocols) > 0 { + for iNdEx := len(m.AddedProtocols) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.AddedProtocols[iNdEx]) + copy(dAtA[i:], m.AddedProtocols[iNdEx]) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.AddedProtocols[iNdEx]))) + i-- + dAtA[i] = 0xa + } } - return i, nil + return len(dAtA) - i, nil } func (m *Identify) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) - n, err := m.MarshalTo(dAtA) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) if err != nil { return nil, err } @@ -288,87 +283,97 @@ func (m *Identify) Marshal() (dAtA []byte, err error) { } func (m *Identify) MarshalTo(dAtA []byte) (int, error) { - var i int + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Identify) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) _ = i var l int _ = l - if m.PublicKey != nil { - dAtA[i] = 0xa - i++ - i = encodeVarintIdentify(dAtA, i, uint64(len(m.PublicKey))) - i += copy(dAtA[i:], m.PublicKey) + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) } - if len(m.ListenAddrs) > 0 { - for _, b := range m.ListenAddrs { - dAtA[i] = 0x12 - i++ - i = encodeVarintIdentify(dAtA, i, uint64(len(b))) - i += copy(dAtA[i:], b) - } + if m.SignedPeerRecord != nil { + i -= len(m.SignedPeerRecord) + copy(dAtA[i:], m.SignedPeerRecord) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.SignedPeerRecord))) + i-- + dAtA[i] = 0x42 } - if len(m.Protocols) > 0 { - for _, s := range m.Protocols { - dAtA[i] = 0x1a - i++ - l = len(s) - for l >= 1<<7 { - dAtA[i] = uint8(uint64(l)&0x7f | 0x80) - l >>= 7 - i++ - } - dAtA[i] = uint8(l) - i++ - i += copy(dAtA[i:], s) + if m.Delta != nil { + { + size, err := m.Delta.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintIdentify(dAtA, i, uint64(size)) } + i-- + dAtA[i] = 0x3a } - if m.ObservedAddr != nil { - dAtA[i] = 0x22 - i++ - i = encodeVarintIdentify(dAtA, i, uint64(len(m.ObservedAddr))) - i += copy(dAtA[i:], m.ObservedAddr) + if m.AgentVersion != nil { + i -= len(*m.AgentVersion) + copy(dAtA[i:], *m.AgentVersion) + i = encodeVarintIdentify(dAtA, i, uint64(len(*m.AgentVersion))) + i-- + dAtA[i] = 0x32 } if m.ProtocolVersion != nil { - dAtA[i] = 0x2a - i++ + i -= len(*m.ProtocolVersion) + copy(dAtA[i:], *m.ProtocolVersion) i = encodeVarintIdentify(dAtA, i, uint64(len(*m.ProtocolVersion))) - i += copy(dAtA[i:], *m.ProtocolVersion) + i-- + dAtA[i] = 0x2a } - if m.AgentVersion != nil { - dAtA[i] = 0x32 - i++ - i = encodeVarintIdentify(dAtA, i, uint64(len(*m.AgentVersion))) - i += copy(dAtA[i:], *m.AgentVersion) + if m.ObservedAddr != nil { + i -= len(m.ObservedAddr) + copy(dAtA[i:], m.ObservedAddr) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.ObservedAddr))) + i-- + dAtA[i] = 0x22 } - if m.Delta != nil { - dAtA[i] = 0x3a - i++ - i = encodeVarintIdentify(dAtA, i, uint64(m.Delta.Size())) - n1, err := m.Delta.MarshalTo(dAtA[i:]) - if err != nil { - return 0, err + if len(m.Protocols) > 0 { + for iNdEx := len(m.Protocols) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Protocols[iNdEx]) + copy(dAtA[i:], m.Protocols[iNdEx]) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.Protocols[iNdEx]))) + i-- + dAtA[i] = 0x1a } - i += n1 } - if m.SignedPeerRecord != nil { - dAtA[i] = 0x42 - i++ - i = encodeVarintIdentify(dAtA, i, uint64(len(m.SignedPeerRecord))) - i += copy(dAtA[i:], m.SignedPeerRecord) + if len(m.ListenAddrs) > 0 { + for iNdEx := len(m.ListenAddrs) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.ListenAddrs[iNdEx]) + copy(dAtA[i:], m.ListenAddrs[iNdEx]) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.ListenAddrs[iNdEx]))) + i-- + dAtA[i] = 0x12 + } } - if m.XXX_unrecognized != nil { - i += copy(dAtA[i:], m.XXX_unrecognized) + if m.PublicKey != nil { + i -= len(m.PublicKey) + copy(dAtA[i:], m.PublicKey) + i = encodeVarintIdentify(dAtA, i, uint64(len(m.PublicKey))) + i-- + dAtA[i] = 0xa } - return i, nil + return len(dAtA) - i, nil } func encodeVarintIdentify(dAtA []byte, offset int, v uint64) int { + offset -= sovIdentify(v) + base := offset for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) - return offset + 1 + return base } func (m *Delta) Size() (n int) { if m == nil { @@ -443,14 +448,7 @@ func (m *Identify) Size() (n int) { } func sovIdentify(x uint64) (n int) { - for { - n++ - x >>= 7 - if x == 0 { - break - } - } - return n + return (math_bits.Len64(x|1) + 6) / 7 } func sozIdentify(x uint64) (n int) { return sovIdentify(uint64((x << 1) ^ uint64((int64(x) >> 63)))) @@ -898,6 +896,7 @@ func (m *Identify) Unmarshal(dAtA []byte) error { func skipIdentify(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 + depth := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { @@ -929,10 +928,8 @@ func skipIdentify(dAtA []byte) (n int, err error) { break } } - return iNdEx, nil case 1: iNdEx += 8 - return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { @@ -953,55 +950,30 @@ func skipIdentify(dAtA []byte) (n int, err error) { return 0, ErrInvalidLengthIdentify } iNdEx += length - if iNdEx < 0 { - return 0, ErrInvalidLengthIdentify - } - return iNdEx, nil case 3: - for { - var innerWire uint64 - var start int = iNdEx - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowIdentify - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - innerWire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - innerWireType := int(innerWire & 0x7) - if innerWireType == 4 { - break - } - next, err := skipIdentify(dAtA[start:]) - if err != nil { - return 0, err - } - iNdEx = start + next - if iNdEx < 0 { - return 0, ErrInvalidLengthIdentify - } - } - return iNdEx, nil + depth++ case 4: - return iNdEx, nil + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupIdentify + } + depth-- case 5: iNdEx += 4 - return iNdEx, nil default: return 0, fmt.Errorf("proto: illegal wireType %d", wireType) } + if iNdEx < 0 { + return 0, ErrInvalidLengthIdentify + } + if depth == 0 { + return iNdEx, nil + } } - panic("unreachable") + return 0, io.ErrUnexpectedEOF } var ( - ErrInvalidLengthIdentify = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowIdentify = fmt.Errorf("proto: integer overflow") + ErrInvalidLengthIdentify = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowIdentify = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupIdentify = fmt.Errorf("proto: unexpected end of group") ) From aff832d21dc9ee811caf97c3adb11c0492463a88 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 6 Mar 2020 10:03:08 -0500 Subject: [PATCH 31/43] import grouping --- p2p/protocol/identify/id.go | 2 +- p2p/protocol/identify/peer_record.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 6e8d498e8a..f836ac3bb9 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -3,7 +3,6 @@ package identify import ( "context" "fmt" - "github.com/libp2p/go-libp2p-core/record" "runtime/debug" "sync" "time" @@ -17,6 +16,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/protocol" + "github.com/libp2p/go-libp2p-core/record" pb "github.com/libp2p/go-libp2p/p2p/protocol/identify/pb" diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index 81e51793cb..bcd1f07582 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -3,13 +3,14 @@ package identify import ( "context" "fmt" + "sync" + "github.com/libp2p/go-eventbus" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/record" "github.com/multiformats/go-multiaddr" - "sync" ) // peerRecordManager creates new signed peer.PeerRecords that can From 140feb8d1d399b7dd2852d911aaa1127bf1cac8c Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 6 Mar 2020 11:30:45 -0500 Subject: [PATCH 32/43] make peerRecordManager stateless - moves the responsibility for tracking the latest peer record to the IDService motivation is to decouple the peerRecordManager from the IDService as much as possible, so we can move the record manager elsewhere. --- go.mod | 10 +++-- go.sum | 28 ++++++++++++ p2p/protocol/identify/id.go | 25 ++++++++--- p2p/protocol/identify/peer_record.go | 67 +++++----------------------- 4 files changed, 65 insertions(+), 65 deletions(-) diff --git a/go.mod b/go.mod index 4214d4f1d1..b07a14cddc 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-libp2p-autonat v0.1.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.3.1-0.20200210195324-7b2888dfdb65 + github.com/libp2p/go-libp2p-core v0.4.0 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.1 @@ -29,14 +29,16 @@ require ( github.com/libp2p/go-stream-muxer-multistream v0.2.0 github.com/libp2p/go-tcp-transport v0.1.1 github.com/libp2p/go-ws-transport v0.2.0 + github.com/mailru/easyjson v0.7.1 // indirect github.com/miekg/dns v1.1.12 // indirect - github.com/multiformats/go-multiaddr v0.2.0 + github.com/multiformats/go-multiaddr v0.2.1 github.com/multiformats/go-multiaddr-dns v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.2 github.com/multiformats/go-multistream v0.1.1 github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 - golang.org/x/crypto v0.0.0-20200210191831-6ca56c2f2e2b // indirect - golang.org/x/tools v0.0.0-20200210192313-1ace956b0e17 // indirect + golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect + golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect + golang.org/x/tools v0.0.0-20200306143135-a0897bacddcb // indirect ) go 1.12 diff --git a/go.sum b/go.sum index 77b3b08932..834bf459d6 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,7 @@ github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67Fexh github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.3.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= github.com/ipfs/go-datastore v0.3.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= +github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= @@ -91,6 +92,7 @@ github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQ github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs= +github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2 h1:vhC1OXXiT9R2pczegwz6moDvuRpggaroAXhPIseh57A= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod h1:8GXXJV31xl8whumTzdZsTt3RnUIiPqzkyf7mxToRCMs= @@ -126,6 +128,7 @@ github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS github.com/libp2p/go-libp2p-autonat v0.1.1 h1:WLBZcIRsjZlWdAZj9CiBSvU2wQXoUOiS1Zk1tM7DTJI= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= +github.com/libp2p/go-libp2p-blankhost v0.1.4 h1:I96SWjR4rK9irDHcHq3XHN6hawCRTPUADzkJacgZLvk= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= github.com/libp2p/go-libp2p-circuit v0.1.4 h1:Phzbmrg3BkVzbqd4ZZ149JxCuUWu2wZcXf/Kr6hZJj8= github.com/libp2p/go-libp2p-circuit v0.1.4/go.mod h1:CY67BrEjKNDhdTk8UgBX1Y/H5c3xkAcs3gnksxY7osU= @@ -141,6 +144,8 @@ github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf h1:LzjCXLc github.com/libp2p/go-libp2p-core v0.3.1-0.20200204211556-97eee5a8dabf/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= github.com/libp2p/go-libp2p-core v0.3.1-0.20200210195324-7b2888dfdb65 h1:1pzqPhclcQSUF9eGsrC99AI9DauqZsYjiZB5kY3nK98= github.com/libp2p/go-libp2p-core v0.3.1-0.20200210195324-7b2888dfdb65/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= +github.com/libp2p/go-libp2p-core v0.4.0 h1:LjZJP/Yy4q8kc724izkYQ9v6YkAmkKCOaE5jLv/NZRo= +github.com/libp2p/go-libp2p-core v0.4.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -189,6 +194,7 @@ github.com/libp2p/go-nat v0.0.4 h1:KbizNnq8YIf7+Hn7+VFL/xE0eDrkPru2zIO9NMwL8UQ= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= +github.com/libp2p/go-openssl v0.0.4 h1:d27YZvLoTyMhIN4njrkr8zMDOM4lfpHIp6A+TK9fovg= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1 h1:7PhkfH73VXfPJYKQ6JwS5I/eVcoyYi9IMNGc6FWpFLw= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= @@ -207,7 +213,10 @@ github.com/libp2p/go-yamux v1.2.3 h1:xX8A36vpXb59frIzWFdEgptLMsOANMFq2K7fPRlunYI github.com/libp2p/go-yamux v1.2.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM= github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= +github.com/mailru/easyjson v0.7.1 h1:mdxE1MF9o53iCb2Ghj1VfWvh7ZOwHpnVG/xwXrV90U8= +github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= @@ -239,6 +248,8 @@ github.com/multiformats/go-multiaddr v0.1.0/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lg github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo= github.com/multiformats/go-multiaddr v0.2.0 h1:lR52sFwcTCuQb6bTfnXF6zA2XfyYvyd+5a9qECv/J90= github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4= +github.com/multiformats/go-multiaddr v0.2.1 h1:SgG/cw5vqyB5QQe5FPe2TqggU9WtrA9X4nZw7LlVqOI= +github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z7tZ04iMk5wP4QMGGE= github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.2.0 h1:YWJoIDwLePniH7OU5hBnDZV6SWuvJqJ0YtN6pLeH9zA= @@ -263,6 +274,7 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS github.com/multiformats/go-multistream v0.1.1 h1:JlAdpIFhBhGRLxe9W6Om0w++Gd6KMWoFPZL/dEnm9nI= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -280,8 +292,10 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smola/gocompat v0.2.0 h1:6b1oIMlUXIpz//VKEDzPVBK8KG7beVwmHIUEBIs/Pns= github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= +github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -314,6 +328,8 @@ go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2 h1:75k/FF0Q2YM8QYo07VPddOLBslDt1MZOdEslOHvmzAs= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= +go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= @@ -335,11 +351,14 @@ golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 h1:+ELyKg6m8UBf0nPFSqD0mi golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200210191831-6ca56c2f2e2b h1:at/SwedEdvIO7WPO4V9Yn1a3MqdSqAo0p6trBocu5aA= golang.org/x/crypto v0.0.0-20200210191831-6ca56c2f2e2b/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -350,11 +369,14 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -368,6 +390,8 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -378,11 +402,15 @@ golang.org/x/tools v0.0.0-20181130052023-1c3d964395ce/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200204192400-7124308813f3/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200210192313-1ace956b0e17 h1:a/Fd23DJvg1CaeDH0dYHahE+hCI0v9rFgxSNIThoUcM= golang.org/x/tools v0.0.0-20200210192313-1ace956b0e17/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200306143135-a0897bacddcb h1:EDG71NBLbXBaWJa/sfgBZwU13ZrO4Zq5j0gQzr2KlLg= +golang.org/x/tools v0.0.0-20200306143135-a0897bacddcb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index f836ac3bb9..6ada81e52b 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -84,6 +84,9 @@ type IDService struct { addrMu sync.Mutex + peerrec *record.Envelope + peerrecMu sync.RWMutex + // our own observed addresses. // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet @@ -142,9 +145,13 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } hostKey := h.Peerstore().PrivKey(h.ID()) - s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs()) - if err != nil { - log.Warnf("identify service not tracking routing state changes; err: %s", err) + if hostKey == nil { + log.Errorf("identify service does not have host key; cannot create peer records") + } else { + s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs()) + if err != nil { + log.Warnf("identify service not tracking routing state changes; err: %s", err) + } } h.SetStreamHandler(ID, s.requestHandler) @@ -182,6 +189,11 @@ func (ids *IDService) handleProtosChanged(evt interface{}) { } func (ids *IDService) handlePeerRecordUpdated(evt interface{}) { + ids.peerrecMu.Lock() + rec := (evt.(event.EvtLocalPeerRecordUpdated)).Record + ids.peerrec = &rec + ids.peerrecMu.Unlock() + log.Debug("triggering push based on updated local PeerRecord") ids.Push() } @@ -347,9 +359,10 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn, usePeerR mes.ObservedAddr = c.RemoteMultiaddr().Bytes() if usePeerRecords { - // Generate a signed peer record containing our listen addresses and - // send it instead of unsigned addrs - rec := ids.peerRecordManager.LatestRecord() + ids.peerrecMu.RLock() + rec := ids.peerrec + ids.peerrecMu.RUnlock() + if rec == nil { log.Errorf("latest PeerRecord does not exist. identify message incomplete!") } else { diff --git a/p2p/protocol/identify/peer_record.go b/p2p/protocol/identify/peer_record.go index bcd1f07582..7c296ba9ba 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/protocol/identify/peer_record.go @@ -2,8 +2,6 @@ package identify import ( "context" - "fmt" - "sync" "github.com/libp2p/go-eventbus" "github.com/libp2p/go-libp2p-core/crypto" @@ -24,8 +22,6 @@ import ( // will immediately receive the current record when they subscribe, with future // records delivered in future events. type peerRecordManager struct { - lock sync.RWMutex - latest *record.Envelope hostID peer.ID signingKey crypto.PrivKey @@ -61,14 +57,6 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri hostID: hostID, } - if len(initialAddrs) != 0 { - initialRec, err := m.makeSignedPeerRecord(initialAddrs) - if err != nil { - return nil, fmt.Errorf("error constructing initial peer record: %w", err) - } - m.latest = initialRec - } - m.subscriptions.localAddrsUpdated, err = bus.Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) if err != nil { return nil, err @@ -78,36 +66,17 @@ func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.Pri return nil, err } - if m.latest != nil { - m.emitLatest() - } - go m.handleEvents() - return m, nil -} - -// LatestRecord returns the most recently constructed signed PeerRecord. -// If you have a direct reference to the peerRecordManager, this method -// gives you a simple way to access the latest record without pulling -// from the event bus. -func (m *peerRecordManager) LatestRecord() *record.Envelope { - if m == nil { - return nil + if len(initialAddrs) != 0 { + m.update(initialAddrs) } - m.lock.RLock() - defer m.lock.RUnlock() - return m.latest + return m, nil } func (m *peerRecordManager) handleEvents() { sub := m.subscriptions.localAddrsUpdated - defer func() { - _ = sub.Close() - // drain the channel. - for range sub.Out() { - } - }() + defer sub.Close() for { select { @@ -115,37 +84,25 @@ func (m *peerRecordManager) handleEvents() { if !more { return } - m.update(evt.(event.EvtLocalAddressesUpdated)) + e := evt.(event.EvtLocalAddressesUpdated) + m.update(addrsFromEvent(e)) case <-m.ctx.Done(): return } } } -func (m *peerRecordManager) update(evt event.EvtLocalAddressesUpdated) { - envelope, err := m.makeSignedPeerRecord(addrsFromEvent(evt)) +func (m *peerRecordManager) update(addrs []multiaddr.Multiaddr) { + envelope, err := m.makeSignedPeerRecord(addrs) if err != nil { - log.Warnf("error creating signed peer record: %v", err) - return - } - m.lock.Lock() - m.latest = envelope - m.lock.Unlock() - m.emitLatest() -} - -func (m *peerRecordManager) emitLatest() { - m.lock.RLock() - defer m.lock.RUnlock() - if m.latest == nil { - log.Warnf("emitLatest called, but no latest record exists") + log.Errorf("error creating signed peer record: %v", err) return } - stateEvt := event.EvtLocalPeerRecordUpdated{Record: m.latest} - err := m.emitters.evtLocalPeerRecordUpdated.Emit(stateEvt) + stateEvt := event.EvtLocalPeerRecordUpdated{Record: *envelope} + err = m.emitters.evtLocalPeerRecordUpdated.Emit(stateEvt) if err != nil { - log.Warnf("error emitting event for updated peer record: %v", err) + log.Errorf("error emitting event for updated peer record: %v", err) } } From 0808676819910cbef96f37f74dd4158ad9f76385 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 6 Mar 2020 16:43:48 -0500 Subject: [PATCH 33/43] certified addrs don't replace existing anymore --- p2p/protocol/identify/id_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 3b2c6ca708..22e1a8f739 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -91,10 +91,7 @@ func subtestIDService(t *testing.T) { // and the protocol versions. t.Log("test peer2 has peer1 addrs correctly") - // the forgetMe addr (and any other uncertified addrs) is removed - // from h2's peerstore when it receives certified addrs for h1. - // h2 should only know the actual (certified) addrs - testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them + testKnowsAddrs(t, h2, h1p, addrs) // has them testHasCertifiedAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) testHasProtocolVersions(t, h2, h1p) testHasPublicKey(t, h2, h1p, h1.Peerstore().PubKey(h1p)) // h1 should have h2's public key @@ -108,7 +105,7 @@ func subtestIDService(t *testing.T) { t.Log("testing addrs just after disconnect") // addresses don't immediately expire on disconnect, so we should still have them - testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) + testKnowsAddrs(t, h2, h1p, addrs) testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) testHasCertifiedAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) testHasCertifiedAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) From 65c2cf192b69f666dccc3a6ff25c5b15c16d30b6 Mon Sep 17 00:00:00 2001 From: Yusef Napora Date: Fri, 6 Mar 2020 16:48:24 -0500 Subject: [PATCH 34/43] move peerRecordManager from IDService to BasicHost also changes how the new ID protocols are enabled. Now, we only enable the protocols that require peer records after the ID service gets the first peer record from the event bus and we're certain that we can generate them. --- go.mod | 4 +- go.sum | 16 ++++ p2p/host/basic/basic_host.go | 11 +++ p2p/host/basic/basic_host_test.go | 36 +++++++-- .../identify => host/basic}/peer_record.go | 2 +- .../basic}/peer_record_test.go | 2 +- p2p/protocol/identify/id.go | 75 +++++++++++-------- p2p/protocol/identify/id_test.go | 60 +++++++++++++-- 8 files changed, 154 insertions(+), 52 deletions(-) rename p2p/{protocol/identify => host/basic}/peer_record.go (99%) rename p2p/{protocol/identify => host/basic}/peer_record_test.go (99%) diff --git a/go.mod b/go.mod index b07a14cddc..a402bbecd2 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ require ( github.com/ipfs/go-cid v0.0.5 github.com/ipfs/go-detect-race v0.0.1 github.com/ipfs/go-ipfs-util v0.0.1 - github.com/ipfs/go-log v1.0.0 + github.com/ipfs/go-log v1.0.2 github.com/jbenet/go-cienv v0.1.0 github.com/jbenet/goprocess v0.1.3 github.com/libp2p/go-conn-security-multistream v0.1.0 @@ -19,7 +19,7 @@ require ( github.com/libp2p/go-libp2p-mplex v0.2.1 github.com/libp2p/go-libp2p-nat v0.0.5 github.com/libp2p/go-libp2p-netutil v0.1.0 - github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277 + github.com/libp2p/go-libp2p-peerstore v0.2.0 github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-swarm v0.2.2 github.com/libp2p/go-libp2p-testing v0.1.1 diff --git a/go.sum b/go.sum index 09c2ff374d..8c3d20183a 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk= github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo= @@ -74,19 +76,28 @@ github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67Fexh github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.3.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= github.com/ipfs/go-datastore v0.3.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= +github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= +github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= +github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= github.com/ipfs/go-ds-badger v0.2.0/go.mod h1:471n2X/Qtk8rRO1iuxcgdwmHJdWjDj9VRGhaP/tvoZw= +github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE= github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= github.com/ipfs/go-ds-leveldb v0.4.0/go.mod h1:NX127692WYenEjmlzyZunFv+f8nrKbH1s9eIaRNnhzs= +github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1 h1:Wz9bL2wB2YBJqggkA4dD7oSmqB4cAnpNbGrlHJulv50= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= github.com/ipfs/go-log v1.0.0 h1:BW3LQIiZzpNyolt84yvKNCd3FU+AK4VDw1hnHR+1aiI= github.com/ipfs/go-log v1.0.0/go.mod h1:JO7RzlMK6rA+CIxFMLOuB6Wf5b81GDiKElL7UPSIKjA= +github.com/ipfs/go-log v1.0.2 h1:s19ZwJxH8rPWzypjcDpqPLIyV7BnbLqvpli3iZoqYK0= +github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk= +github.com/ipfs/go-log/v2 v2.0.2 h1:xguurydRdfKMJjKyxNXNU8lYP0VZH1NUwJRwUorjuEw= +github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/jackpal/gateway v1.0.5 h1:qzXWUJfuMdlLMtt0a3Dgt+xkWQiA5itDEITVJtuSwMc= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA= @@ -158,6 +169,8 @@ github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HB github.com/libp2p/go-libp2p-peerstore v0.1.3/go.mod h1:BJ9sHlm59/80oSkpWgr1MyY1ciXAXV397W6h1GH/uKI= github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277 h1:h+4z1eGlFBtNU3idlwfzCRuKUZwvk0wBDXg3zFBpztw= github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277/go.mod h1:T5L262N+VtcnnPIUt5X/mNcdEC6fz+1a1PvRoQPTIOU= +github.com/libp2p/go-libp2p-peerstore v0.2.0 h1:XcgJhI8WyUOCbHyRLNEX5542YNj8hnLSJ2G1InRjDhk= +github.com/libp2p/go-libp2p-peerstore v0.2.0/go.mod h1:N2l3eVIeAitSg3Pi2ipSrJYnqhVnMNQZo9nkSCuAbnQ= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1 h1:eNWbJTdyPA7NxhP7J3c5lT97DC5d+u+IldkgCYFTPVA= @@ -189,6 +202,7 @@ github.com/libp2p/go-nat v0.0.4 h1:KbizNnq8YIf7+Hn7+VFL/xE0eDrkPru2zIO9NMwL8UQ= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= +github.com/libp2p/go-openssl v0.0.4 h1:d27YZvLoTyMhIN4njrkr8zMDOM4lfpHIp6A+TK9fovg= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1 h1:7PhkfH73VXfPJYKQ6JwS5I/eVcoyYi9IMNGc6FWpFLw= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= @@ -285,6 +299,7 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= +github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -401,6 +416,7 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 08ea2e63be..d2833a2702 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -77,6 +77,7 @@ type BasicHost struct { maResolver *madns.Resolver cmgr connmgr.ConnManager eventbus event.Bus + peerrecmgr *peerRecordManager AddrsFactory AddrsFactory @@ -146,6 +147,16 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo return nil, err } + hostKey := h.Peerstore().PrivKey(h.ID()) + if hostKey == nil { + log.Warn("unable to access host key. peer record support disabled.") + } else { + h.peerrecmgr, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs()) + if err != nil { + log.Errorf("error creating peer record manager. peer record support disabled. err: %s", err) + } + } + h.proc = goprocessctx.WithContextAndTeardown(ctx, func() error { if h.natmgr != nil { h.natmgr.Close() diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 6d6e72ab32..4eb18d5616 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -3,6 +3,7 @@ package basichost import ( "bytes" "context" + "github.com/libp2p/go-libp2p/p2p/protocol/identify" "io" "reflect" "sort" @@ -90,16 +91,35 @@ func TestProtocolHandlerEvents(t *testing.T) { } defer sub.Close() - assert := func(added, removed []protocol.ID) { - var next event.EvtLocalProtocolsUpdated - select { - case evt := <-sub.Out(): - next = evt.(event.EvtLocalProtocolsUpdated) - break - case <-time.After(5 * time.Second): - t.Fatal("event not received in 5 seconds") + // the identify service adds new protocol handlers shortly after the host + // starts. this helps us filter those events out, since they're unrelated + // to the test. + isIdentify := func(evt event.EvtLocalProtocolsUpdated) bool { + for _, p := range evt.Added { + if p == identify.ID || p == identify.IDPush { + return true + } + } + return false + } + + nextEvent := func() event.EvtLocalProtocolsUpdated { + for { + select { + case evt := <-sub.Out(): + next := evt.(event.EvtLocalProtocolsUpdated) + if isIdentify(next) { + continue + } + return next + case <-time.After(5 * time.Second): + t.Fatal("event not received in 5 seconds") + } } + } + assert := func(added, removed []protocol.ID) { + next := nextEvent() if !reflect.DeepEqual(added, next.Added) { t.Errorf("expected added: %v; received: %v", added, next.Added) } diff --git a/p2p/protocol/identify/peer_record.go b/p2p/host/basic/peer_record.go similarity index 99% rename from p2p/protocol/identify/peer_record.go rename to p2p/host/basic/peer_record.go index 7c296ba9ba..66bc233a68 100644 --- a/p2p/protocol/identify/peer_record.go +++ b/p2p/host/basic/peer_record.go @@ -1,4 +1,4 @@ -package identify +package basichost import ( "context" diff --git a/p2p/protocol/identify/peer_record_test.go b/p2p/host/basic/peer_record_test.go similarity index 99% rename from p2p/protocol/identify/peer_record_test.go rename to p2p/host/basic/peer_record_test.go index 702fdc3715..d883ce2b46 100644 --- a/p2p/protocol/identify/peer_record_test.go +++ b/p2p/host/basic/peer_record_test.go @@ -1,4 +1,4 @@ -package identify +package basichost import ( "context" diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 26bfd4932e..72c558b43b 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -85,17 +85,17 @@ type IDService struct { addrMu sync.Mutex - peerrec *record.Envelope - peerrecMu sync.RWMutex + enableNewProtos sync.Once + peerrec *record.Envelope + peerrecMu sync.RWMutex // our own observed addresses. // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet - peerRecordManager *peerRecordManager - subscriptions struct { localProtocolsUpdated event.Subscription + localAddrsUpdated event.Subscription localPeerRecordUpdated event.Subscription } emitters struct { @@ -137,10 +137,16 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } s.subscriptions.localPeerRecordUpdated, err = h.EventBus().Subscribe(&event.EvtLocalPeerRecordUpdated{}, eventbus.BufSize(128)) if err != nil { - log.Warnf("identify service not subscribed to local routing state changes; err: %s", err) + log.Warnf("identify service not subscribed to local peer record changes; err: %s", err) } else { go s.handleEvents(s.subscriptions.localPeerRecordUpdated, s.handlePeerRecordUpdated) } + s.subscriptions.localAddrsUpdated, err = h.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) + if err != nil { + log.Warnf("identify service not subscribed to address changes. err: %s", err) + } else { + go s.handleEvents(s.subscriptions.localAddrsUpdated, s.handleLocalAddrsUpdated) + } s.emitters.evtPeerProtocolsUpdated, err = h.EventBus().Emitter(&event.EvtPeerProtocolsUpdated{}) if err != nil { @@ -155,22 +161,10 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { log.Warnf("identify service not emitting identification failed events; err: %s", err) } - hostKey := h.Peerstore().PrivKey(h.ID()) - if hostKey == nil { - log.Errorf("identify service does not have host key; cannot create peer records") - } else { - s.peerRecordManager, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs()) - if err != nil { - log.Errorf("identify service not tracking peer record changes; err: %s", err) - } - } - - // without a peer record manager, we can only support the legacy protocols - if s.peerRecordManager != nil { - h.SetStreamHandler(IDPush, s.pushHandler) - h.SetStreamHandler(IDDelta, s.deltaHandler) - } - + // register protocols that depend on peer records immediately. + // those that do will be registered when we receive the first + // peer record on the event bus + h.SetStreamHandler(IDDelta, s.deltaHandler) h.SetStreamHandler(LegacyID, s.requestHandler) h.SetStreamHandler(LegacyIDPush, s.pushHandler) @@ -179,12 +173,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } func (ids *IDService) handleEvents(sub event.Subscription, handler func(interface{})) { - defer func() { - _ = sub.Close() - // drain the channel. - for range sub.Out() { - } - }() + defer sub.Close() for { select { @@ -203,12 +192,25 @@ func (ids *IDService) handleProtosChanged(evt interface{}) { ids.fireProtocolDelta(evt.(event.EvtLocalProtocolsUpdated)) } +func (ids *IDService) handleLocalAddrsUpdated(evt interface{}) { + log.Debug("triggering push based on updated local addresses") + ids.Push() +} + func (ids *IDService) handlePeerRecordUpdated(evt interface{}) { ids.peerrecMu.Lock() + existed := ids.peerrec != nil rec := (evt.(event.EvtLocalPeerRecordUpdated)).Record ids.peerrec = &rec ids.peerrecMu.Unlock() + if !existed { + ids.enableNewProtos.Do(func() { + ids.Host.SetStreamHandler(ID, ids.requestHandler) + ids.Host.SetStreamHandler(IDPush, ids.pushHandler) + }) + } + log.Debug("triggering push based on updated local PeerRecord") ids.Push() } @@ -261,16 +263,23 @@ func (ids *IDService) IdentifyConn(c network.Conn) { return } - s.SetProtocol(ID) + ids.peerrecMu.RLock() + havePeerRec := ids.peerrec != nil + ids.peerrecMu.RUnlock() + protocolIDs := []string{ID, LegacyID} + if !havePeerRec { + protocolIDs = []string{LegacyID} + } // ok give the response to our handler. - if _, err := msmux.SelectOneOf(protocolIDs, s); err != nil { + var selectedProto string + if selectedProto, err = msmux.SelectOneOf(protocolIDs, s); err != nil { log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer(), logging.Metadata{"error": err}) s.Reset() return } - + s.SetProtocol(protocol.ID(selectedProto)) ids.responseHandler(s) } @@ -391,11 +400,11 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn, usePeerR ids.peerrecMu.RUnlock() if rec == nil { - log.Errorf("latest PeerRecord does not exist. identify message incomplete!") + log.Errorf("latest peer record does not exist. identify message incomplete!") } else { recBytes, err := rec.Marshal() if err != nil { - log.Warnf("error marshaling peer record: %v", err) + log.Errorf("error marshaling peer record: %v", err) } else { mes.SignedPeerRecord = recBytes log.Debugf("%s sent peer record to %s", c.LocalPeer(), c.RemotePeer()) @@ -479,7 +488,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn, usePeerRe var err error signedPeerRecord, err = signedPeerRecordFromMessage(mes) if err != nil { - log.Warnf("error getting routing state from Identify message: %v", err) + log.Errorf("error getting peer record from Identify message: %v", err) } } diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 22e1a8f739..1ac5cfb419 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -3,6 +3,7 @@ package identify_test import ( "context" "fmt" + "github.com/libp2p/go-libp2p-core/record" "reflect" "sort" "testing" @@ -35,6 +36,8 @@ func subtestIDService(t *testing.T) { h1 := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) h2 := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + generatePeerRecord(t, h1) + generatePeerRecord(t, h2) h1p := h1.ID() h2p := h2.ID() @@ -212,6 +215,36 @@ func testHasPublicKey(t *testing.T, h host.Host, p peer.ID, shouldBe ic.PubKey) } } +// we're using BlankHost in our tests, which doesn't automatically generate peer records +// like BasicHost. This generates a record and puts it on the host's event bus, which +// will cause the identify service to start supporting new protocol versions that +// depend on peer records being available. +func generatePeerRecord(t *testing.T, h host.Host) { + t.Helper() + + key := h.Peerstore().PrivKey(h.ID()) + if key == nil { + t.Fatal("no private key for host") + } + + rec := peer.NewPeerRecord() + rec.PeerID = h.ID() + rec.Addrs = h.Addrs() + signed, err := record.Seal(rec, key) + if err != nil { + t.Fatalf("error generating peer record: %s", err) + } + evt := event.EvtLocalPeerRecordUpdated{Record: *signed} + emitter, err := h.EventBus().Emitter(&evt, eventbus.Stateful) + if err != nil { + t.Fatal(err) + } + err = emitter.Emit(evt) + if err != nil { + t.Fatal(err) + } +} + // TestIDServiceWait gives the ID service 1s to finish after dialing // this is because it used to be concurrent. Now, Dial wait till the // id service is done. @@ -447,11 +480,14 @@ func TestIdentifyDeltaWhileIdentifyingConn(t *testing.T) { // replace the original identify handler by one that blocks until we close the block channel. // this allows us to control how long identify runs. block := make(chan struct{}) - h1.RemoveStreamHandler(identify.ID) - h1.SetStreamHandler(identify.ID, func(s network.Stream) { + handler := func(s network.Stream) { <-block go helpers.FullClose(s) - }) + } + h1.RemoveStreamHandler(identify.ID) + h1.RemoveStreamHandler(identify.LegacyID) + h1.SetStreamHandler(identify.ID, handler) + h1.SetStreamHandler(identify.LegacyID, handler) // from h2 connect to h1. if err := h2.Connect(ctx, peer.AddrInfo{ID: h1.ID(), Addrs: h1.Addrs()}); err != nil { @@ -542,12 +578,14 @@ func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { defer h2.Close() defer h1.Close() - h2p := h2.ID() ids := identify.NewIDService(ctx, h1) + _ = identify.NewIDService(ctx, h2) - // remove new protocol ID from h2, so it only responds to "legacy" protocol id - h2.RemoveStreamHandler(identify.ID) + // generate initial peer record only for h1. this will cause h1 to enable + // the new protocols, but h2 will still use legacy protos + generatePeerRecord(t, h1) + h2p := h2.ID() h2pi := h2.Peerstore().PeerInfo(h2p) if err := h1.Connect(ctx, h2pi); err != nil { t.Fatal(err) @@ -559,10 +597,18 @@ func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) { } ids.IdentifyConn(h1t2c[0]) - // the IDService should be opened automatically, by the network. // what we should see now is that both peers know about each others listen addresses. t.Log("test peer1 has peer2 addrs correctly") testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) // should not have signed addrs + + // double check that it works when both peers support the new protos + // enable new protos for h2 by generating a peer record + generatePeerRecord(t, h2) + + // if we re-identify, h1 should now have certified addrs for h2 + ids.IdentifyConn(h1t2c[0]) + t.Log("test peer1 has peer2 certified addrs correctly") + testHasCertifiedAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) } From 4bfae9d4cb787b5e636612f4cc09da71bb2943b5 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 26 Mar 2020 11:06:26 +0530 Subject: [PATCH 35/43] order imports --- p2p/protocol/identify/id.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 72c558b43b..d83c065d8c 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -7,7 +7,6 @@ import ( "sync" "time" - "github.com/libp2p/go-eventbus" ic "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/helpers" @@ -18,11 +17,11 @@ import ( "github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-libp2p-core/record" + "github.com/libp2p/go-eventbus" pb "github.com/libp2p/go-libp2p/p2p/protocol/identify/pb" ggio "github.com/gogo/protobuf/io" logging "github.com/ipfs/go-log" - ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" msmux "github.com/multiformats/go-multistream" From a45bb3fe8a31bf07c1cac50cb65fe397db3969fd Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 26 Mar 2020 12:15:59 +0530 Subject: [PATCH 36/43] signal addr changes --- go.mod | 24 ++++------ go.sum | 50 ++++++++++++++++++++ p2p/host/basic/basic_host.go | 76 +++++++++++++++++-------------- p2p/host/basic/basic_host_test.go | 48 ++++++++++--------- p2p/host/relay/autorelay.go | 68 +++++++++++---------------- 5 files changed, 152 insertions(+), 114 deletions(-) diff --git a/go.mod b/go.mod index a402bbecd2..76b69f314f 100644 --- a/go.mod +++ b/go.mod @@ -7,38 +7,34 @@ require ( github.com/ipfs/go-ipfs-util v0.0.1 github.com/ipfs/go-log v1.0.2 github.com/jbenet/go-cienv v0.1.0 - github.com/jbenet/goprocess v0.1.3 + github.com/jbenet/goprocess v0.1.4 github.com/libp2p/go-conn-security-multistream v0.1.0 - github.com/libp2p/go-eventbus v0.1.0 - github.com/libp2p/go-libp2p-autonat v0.1.1 + github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4 + github.com/libp2p/go-libp2p-autonat v0.2.0 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.4.0 + github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 - github.com/libp2p/go-libp2p-mplex v0.2.1 + github.com/libp2p/go-libp2p-mplex v0.2.2 github.com/libp2p/go-libp2p-nat v0.0.5 github.com/libp2p/go-libp2p-netutil v0.1.0 - github.com/libp2p/go-libp2p-peerstore v0.2.0 + github.com/libp2p/go-libp2p-peerstore v0.2.1 github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-swarm v0.2.2 github.com/libp2p/go-libp2p-testing v0.1.1 - github.com/libp2p/go-libp2p-transport-upgrader v0.1.1 - github.com/libp2p/go-libp2p-yamux v0.2.1 + github.com/libp2p/go-libp2p-transport-upgrader v0.2.0 + github.com/libp2p/go-libp2p-yamux v0.2.5 github.com/libp2p/go-maddr-filter v0.0.5 github.com/libp2p/go-stream-muxer-multistream v0.2.0 github.com/libp2p/go-tcp-transport v0.1.1 github.com/libp2p/go-ws-transport v0.2.0 - github.com/mailru/easyjson v0.7.1 // indirect - github.com/miekg/dns v1.1.12 // indirect + github.com/miekg/dns v1.1.28 // indirect github.com/multiformats/go-multiaddr v0.2.1 github.com/multiformats/go-multiaddr-dns v0.2.0 - github.com/multiformats/go-multiaddr-net v0.1.2 + github.com/multiformats/go-multiaddr-net v0.1.3 github.com/multiformats/go-multistream v0.1.1 github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 - golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect - golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect - golang.org/x/tools v0.0.0-20200306143135-a0897bacddcb // indirect ) go 1.12 diff --git a/go.sum b/go.sum index 8c3d20183a..00257714d6 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018 h1:6xT9KW8zLC5IlbaIF5Q7JNieBoACT7iW0YTxQHR0in0= +github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018/go.mod h1:rQYf4tfk5sSwFsnDg3qYaBxSjsD9S8+59vW0dKUgme4= github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ= github.com/dgraph-io/badger v1.6.0-rc1/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= @@ -110,6 +112,8 @@ github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/jbenet/goprocess v0.1.3 h1:YKyIEECS/XvcfHtBzxtjBBbWK+MbvA6dG8ASiqwvr10= github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= +github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= @@ -133,11 +137,16 @@ github.com/libp2p/go-conn-security-multistream v0.1.0 h1:aqGmto+ttL/uJgX0JtQI0tD github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-eventbus v0.1.0 h1:mlawomSAjjkk97QnYiEmHsLu7E136+2oCWSHRUvMfzQ= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= +github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4 h1:HoGcifIkfX/eBrb19dzCBFfzUXL9g2aZl4OHAvgbDuE= +github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4/go.mod h1:cGzwn3/iN5WAOBJuqrwIuqwoUoDenNavOK+K0kVcbnM= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= +github.com/libp2p/go-libp2p v0.6.1/go.mod h1:CTFnWXogryAHjXAKEbOf1OWY+VeAP3lDMZkfEI5sT54= github.com/libp2p/go-libp2p-autonat v0.1.1 h1:WLBZcIRsjZlWdAZj9CiBSvU2wQXoUOiS1Zk1tM7DTJI= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= +github.com/libp2p/go-libp2p-autonat v0.2.0 h1:Kok+0M/4jiz6TTmxtBqAa5tLyHb/U+G/7o/JEeW7Wok= +github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQdNbfzE1C718tcViI= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4 h1:I96SWjR4rK9irDHcHq3XHN6hawCRTPUADzkJacgZLvk= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= @@ -150,8 +159,15 @@ github.com/libp2p/go-libp2p-core v0.2.2/go.mod h1:8fcwTbsG2B+lTgRJ1ICZtiM5GWCWZV github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= github.com/libp2p/go-libp2p-core v0.3.1-0.20200204201746-48fdd0fe436b/go.mod h1:xblDXGika41jyMQImcF5SzEmM3mRJlXtm7Xa/9QOjFg= +github.com/libp2p/go-libp2p-core v0.3.1/go.mod h1:thvWy0hvaSBhnVBaW37BvzgVV68OUhgJJLAa6almrII= github.com/libp2p/go-libp2p-core v0.4.0 h1:LjZJP/Yy4q8kc724izkYQ9v6YkAmkKCOaE5jLv/NZRo= github.com/libp2p/go-libp2p-core v0.4.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= +github.com/libp2p/go-libp2p-core v0.5.0 h1:FBQ1fpq2Fo/ClyjojVJ5AKXlKhvNc/B6U0O+7AN1ffE= +github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326052016-dc8b8fa8c504 h1:fFejZlp7mgufxOc+lKGGtJg6LNJARwceBuHuyTQzpU8= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326052016-dc8b8fa8c504/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f h1:b8OxzaV6EecoGBMum8ENSl07sbacu2EepywXgTOpRVM= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -160,6 +176,8 @@ github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3x github.com/libp2p/go-libp2p-mplex v0.2.0/go.mod h1:Ejl9IyjvXJ0T9iqUTE1jpYATQ9NM3g+OtR+EMMODbKo= github.com/libp2p/go-libp2p-mplex v0.2.1 h1:E1xaJBQnbSiTHGI1gaBKmKhu1TUKkErKJnE8iGvirYI= github.com/libp2p/go-libp2p-mplex v0.2.1/go.mod h1:SC99Rxs8Vuzrf/6WhmH41kNn13TiYdAWNYHrwImKLnE= +github.com/libp2p/go-libp2p-mplex v0.2.2 h1:+Ld7YDAfVERQ0E+qqjE7o6fHwKuM0SqTzYiwN1lVVSA= +github.com/libp2p/go-libp2p-mplex v0.2.2/go.mod h1:74S9eum0tVQdAfFiKxAyKzNdSuLqw5oadDq7+L/FELo= github.com/libp2p/go-libp2p-nat v0.0.5 h1:/mH8pXFVKleflDL1YwqMg27W9GD8kjEx7NY0P6eGc98= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLKcKF72EAMQ= @@ -171,6 +189,10 @@ github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277 h1:h+ github.com/libp2p/go-libp2p-peerstore v0.1.5-0.20200204202600-1ef508a3b277/go.mod h1:T5L262N+VtcnnPIUt5X/mNcdEC6fz+1a1PvRoQPTIOU= github.com/libp2p/go-libp2p-peerstore v0.2.0 h1:XcgJhI8WyUOCbHyRLNEX5542YNj8hnLSJ2G1InRjDhk= github.com/libp2p/go-libp2p-peerstore v0.2.0/go.mod h1:N2l3eVIeAitSg3Pi2ipSrJYnqhVnMNQZo9nkSCuAbnQ= +github.com/libp2p/go-libp2p-peerstore v0.2.1 h1:u+gOfsKgu73ZkGWhvckRm03z9C+iS9TrLqpANweELGs= +github.com/libp2p/go-libp2p-peerstore v0.2.1/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= +github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= +github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1 h1:eNWbJTdyPA7NxhP7J3c5lT97DC5d+u+IldkgCYFTPVA= @@ -186,15 +208,21 @@ github.com/libp2p/go-libp2p-testing v0.1.1 h1:U03z3HnGI7Ni8Xx6ONVZvUFOAzWYmolWf5 github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1 h1:PZMS9lhjK9VytzMCW3tWHAXtKXmlURSc3ZdvwEcKCzw= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= +github.com/libp2p/go-libp2p-transport-upgrader v0.2.0 h1:5EhPgQhXZNyfL22ERZTUoVp9UVVbNowWNVtELQaKCHk= +github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= github.com/libp2p/go-libp2p-yamux v0.2.0/go.mod h1:Db2gU+XfLpm6E4rG5uGCFX6uXA8MEXOxFcRoXUODaK8= github.com/libp2p/go-libp2p-yamux v0.2.1 h1:Q3XYNiKCC2vIxrvUJL+Jg1kiyeEaIDNKLjgEjo3VQdI= github.com/libp2p/go-libp2p-yamux v0.2.1/go.mod h1:1FBXiHDk1VyRM1C0aez2bCfHQ4vMZKkAQzZbkSQt5fI= +github.com/libp2p/go-libp2p-yamux v0.2.5 h1:MuyItOqz03oi8npvjgMJxgnhllJLZnO/dKVOpTZ9+XI= +github.com/libp2p/go-libp2p-yamux v0.2.5/go.mod h1:Zpgj6arbyQrmZ3wxSZxfBmbdnWtbZ48OpsfmQVTErwA= github.com/libp2p/go-maddr-filter v0.0.4/go.mod h1:6eT12kSQMA9x2pvFQa+xesMKUBlj9VImZbj3B9FBH/Q= github.com/libp2p/go-maddr-filter v0.0.5 h1:CW3AgbMO6vUvT4kf87y4N+0P8KUl2aqLYhrGyDUbLSg= github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDRp/w6ENFaC9bG+M= github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0= github.com/libp2p/go-mplex v0.1.0 h1:/nBTy5+1yRyY82YaO6HXQRnO5IAGsXTjEJaR3LdTPc0= github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU= +github.com/libp2p/go-mplex v0.1.1 h1:huPH/GGRJzmsHR9IZJJsrSwIM5YE2gL4ssgl1YWb/ps= +github.com/libp2p/go-mplex v0.1.1/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4 h1:agEFehY3zWJFUHK6SEMR7UYmk2z6kC3oeCM7ybLhguA= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= @@ -219,6 +247,8 @@ github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzl github.com/libp2p/go-yamux v1.2.2/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.2.3 h1:xX8A36vpXb59frIzWFdEgptLMsOANMFq2K7fPRlunYI= github.com/libp2p/go-yamux v1.2.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= +github.com/libp2p/go-yamux v1.3.3 h1:mWuzZRCAeTBFdynLlsYgA/EIeMOLr8XY04wa52NRhsE= +github.com/libp2p/go-yamux v1.3.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= @@ -230,6 +260,8 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/miekg/dns v1.1.12 h1:WMhc1ik4LNkTg8U9l3hI1LvxKmIL+f1+WV/SZtCbDDA= github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/miekg/dns v1.1.28 h1:gQhy5bsJa8zTlVI8lywCTZp1lguor+xevFoYlzeCTQY= +github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= @@ -267,6 +299,8 @@ github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJV github.com/multiformats/go-multiaddr-net v0.1.1/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= github.com/multiformats/go-multiaddr-net v0.1.2 h1:P7zcBH9FRETdPkDrylcXVjQLQ2t1JQtNItZULWNWgeg= github.com/multiformats/go-multiaddr-net v0.1.2/go.mod h1:QsWt3XK/3hwvNxZJp92iMQKME1qHfpYmyIjFVsSOY6Y= +github.com/multiformats/go-multiaddr-net v0.1.3 h1:q/IYAvoPKuRzGeERn3uacWgm0LIWkLZBAvO5DxSzq3g= +github.com/multiformats/go-multiaddr-net v0.1.3/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA= github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= @@ -285,8 +319,11 @@ github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXS github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= @@ -335,6 +372,7 @@ go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM= @@ -351,12 +389,15 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -368,6 +409,8 @@ golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -387,7 +430,11 @@ golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -400,7 +447,9 @@ golang.org/x/tools v0.0.0-20181130052023-1c3d964395ce/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200204192400-7124308813f3/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200306143135-a0897bacddcb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -423,4 +472,5 @@ gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFab gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index d2833a2702..d5bf4169e1 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -2,9 +2,9 @@ package basichost import ( "context" + "fmt" "io" "net" - "sync" "time" "github.com/libp2p/go-libp2p/p2p/protocol/identify" @@ -85,12 +85,12 @@ type BasicHost struct { proc goprocess.Process - mx sync.Mutex - lastAddrs []ma.Multiaddr - emitters struct { + emitters struct { evtLocalProtocolsUpdated event.Emitter evtLocalAddrsUpdated event.Emitter } + + addrChangeChan chan struct{} } var _ host.Host = (*BasicHost)(nil) @@ -131,12 +131,13 @@ type HostOpts struct { // NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network. func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHost, error) { h := &BasicHost{ - network: net, - mux: msmux.NewMultistreamMuxer(), - negtimeout: DefaultNegotiationTimeout, - AddrsFactory: DefaultAddrsFactory, - maResolver: madns.DefaultResolver, - eventbus: eventbus.NewBus(), + network: net, + mux: msmux.NewMultistreamMuxer(), + negtimeout: DefaultNegotiationTimeout, + AddrsFactory: DefaultAddrsFactory, + maResolver: madns.DefaultResolver, + eventbus: eventbus.NewBus(), + addrChangeChan: make(chan struct{}, 1), } var err error @@ -210,6 +211,14 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo net.SetConnHandler(h.newConnHandler) net.SetStreamHandler(h.newStreamHandler) + var hostInitializedEmitter event.Emitter + if hostInitializedEmitter, err = h.eventbus.Emitter(&event.EvtLocalHostInitialized{}); err != nil { + return nil, fmt.Errorf("failed to create emitter for EvtLocalHostInitialized, err=%s", err) + } + if err := hostInitializedEmitter.Emit(new(event.EvtLocalHostInitialized)); err != nil { + return nil, fmt.Errorf("failed to emit EvtLocalHostInitialized, err=%s", err) + } + return h, nil } @@ -246,6 +255,7 @@ func New(net network.Network, opts ...interface{}) *BasicHost { // plus we want to keep the (deprecated) legacy interface unchanged panic(err) } + h.Start() return h } @@ -311,23 +321,12 @@ func (h *BasicHost) newStreamHandler(s network.Stream) { go handle(protoID, s) } -// CheckForAddressChanges determines whether our listen addresses have recently -// changed and emits an EvtLocalAddressesUpdatedEvent if so. -// Warning: this interface is unstable and may disappear in the future. -func (h *BasicHost) CheckForAddressChanges() { - h.mx.Lock() - addrs := h.Addrs() - changeEvt := makeUpdatedAddrEvent(h.lastAddrs, addrs) - if changeEvt != nil { - h.lastAddrs = addrs - } - h.mx.Unlock() - - if changeEvt != nil { - err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt) - if err != nil { - log.Warnf("error emitting event for updated addrs: %s", err) - } +// SignalAddressChange signals to the host that it needs to determine whether our listen addresses have recently +// changed. +func (h *BasicHost) SignalAddressChange() { + select { + case h.addrChangeChan <- struct{}{}: + default: } } @@ -338,20 +337,29 @@ func (h *BasicHost) background(p goprocess.Process) { defer ticker.Stop() // initialize lastAddrs - h.mx.Lock() - if h.lastAddrs == nil { - h.lastAddrs = h.Addrs() - } - h.mx.Unlock() + lastAddrs := h.Addrs() for { select { case <-ticker.C: - h.CheckForAddressChanges() - + case <-h.addrChangeChan: case <-p.Closing(): return } + + // emit an EvtLocalAddressesUpdatedEvent & a Push Identify if our listen addresses have changed. + addrs := h.Addrs() + changeEvt := makeUpdatedAddrEvent(lastAddrs, addrs) + if changeEvt != nil { + lastAddrs = addrs + } + + if changeEvt != nil { + err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt) + if err != nil { + log.Warnf("error emitting event for updated addrs: %s", err) + } + } } } diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 4eb18d5616..361df9ad66 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -7,6 +7,7 @@ import ( "io" "reflect" "sort" + "sync" "testing" "time" @@ -538,7 +539,7 @@ func TestHostAddrChangeDetection(t *testing.T) { {ma.StringCast("/ip4/2.3.4.5/tcp/1234"), ma.StringCast("/ip4/3.4.5.6/tcp/4321")}, } - // The events we expect the host to emit when CheckForAddressChanges is called + // The events we expect the host to emit when SignalAddressChange is called // and the changes between addr sets are detected expectedEvents := []event.EvtLocalAddressesUpdated{ { @@ -568,8 +569,11 @@ func TestHostAddrChangeDetection(t *testing.T) { }, } + var lk sync.Mutex currentAddrSet := 0 addrsFactory := func(addrs []ma.Multiaddr) []ma.Multiaddr { + lk.Lock() + defer lk.Unlock() return addrSets[currentAddrSet] } @@ -583,46 +587,40 @@ func TestHostAddrChangeDetection(t *testing.T) { } defer sub.Close() + // wait for the host background thread to start + time.Sleep(1 * time.Second) // host should start with no addrs (addrSet 0) addrs := h.Addrs() if len(addrs) != 0 { t.Fatalf("expected 0 addrs, got %d", len(addrs)) } - // Advance between addrSets + // change addr, signal and assert event for i := 1; i < len(addrSets); i++ { + lk.Lock() currentAddrSet = i - h.CheckForAddressChanges() // forces the host to check for changes now, instead of waiting for background update + lk.Unlock() + h.SignalAddressChange() + evt := waitForAddrChangeEvent(ctx, sub, t) + if !updatedAddrEventsEqual(expectedEvents[i-1], evt) { + t.Errorf("change events not equal: \n\texpected: %v \n\tactual: %v", expectedEvents[i], evt) + } + } +} - // drain events from the subscription - var receivedEvents []event.EvtLocalAddressesUpdated -readEvents: +func waitForAddrChangeEvent(ctx context.Context, sub event.Subscription, t *testing.T) event.EvtLocalAddressesUpdated { for { select { case evt, more := <-sub.Out(): if !more { - break readEvents - } - receivedEvents = append(receivedEvents, evt.(event.EvtLocalAddressesUpdated)) - if len(receivedEvents) == len(expectedEvents) { - break readEvents + t.Fatal("channel should not be closed") } + return evt.(event.EvtLocalAddressesUpdated) case <-ctx.Done(): - break readEvents - case <-time.After(1 * time.Second): - break readEvents - } - } - - // assert that we received the events we expected - if len(receivedEvents) != len(expectedEvents) { - t.Errorf("expected to receive %d addr change events, got %d", len(expectedEvents), len(receivedEvents)) - } - for i, expected := range expectedEvents { - actual := receivedEvents[i] - if !updatedAddrEventsEqual(expected, actual) { - t.Errorf("change events not equal: \n\texpected: %v \n\tactual: %v", expected, actual) + t.Fatal("context should not have cancelled") + case <-time.After(2 * time.Second): + t.Fatal("timed out waiting for address change event") } } } diff --git a/p2p/host/relay/autorelay.go b/p2p/host/relay/autorelay.go index 484c7dbb9f..a1691efb26 100644 --- a/p2p/host/relay/autorelay.go +++ b/p2p/host/relay/autorelay.go @@ -7,11 +7,11 @@ import ( "sync" "time" + "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/routing" - autonat "github.com/libp2p/go-libp2p-autonat" circuit "github.com/libp2p/go-libp2p-circuit" discovery "github.com/libp2p/go-libp2p-discovery" basic "github.com/libp2p/go-libp2p/p2p/host/basic" @@ -42,7 +42,6 @@ type AutoRelay struct { host *basic.BasicHost discover discovery.Discoverer router routing.PeerRouting - autonat autonat.AutoNAT addrsF basic.AddrsFactory static []peer.AddrInfo @@ -51,7 +50,7 @@ type AutoRelay struct { mx sync.Mutex relays map[peer.ID]struct{} - status autonat.NATStatus + status network.Reachability cachedAddrs []ma.Multiaddr cachedAddrsExpiry time.Time @@ -66,58 +65,53 @@ func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discover static: static, relays: make(map[peer.ID]struct{}), disconnect: make(chan struct{}, 1), - status: autonat.NATStatusUnknown, + status: network.ReachabilityUnknown, } - ar.autonat = autonat.NewAutoNAT(ctx, bhost, ar.baseAddrs) bhost.AddrsFactory = ar.hostAddrs bhost.Network().Notify(ar) go ar.background(ctx) return ar } -func (ar *AutoRelay) baseAddrs() []ma.Multiaddr { - return ar.addrsF(ar.host.AllAddrs()) -} - func (ar *AutoRelay) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr { return ar.relayAddrs(ar.addrsF(addrs)) } func (ar *AutoRelay) background(ctx context.Context) { - select { - case <-time.After(autonat.AutoNATBootDelay + BootDelay): - case <-ctx.Done(): - return - } + subReachability, _ := ar.host.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged)) + defer subReachability.Close() // when true, we need to identify push push := false for { - wait := autonat.AutoNATRefreshInterval - switch ar.autonat.Status() { - case autonat.NATStatusUnknown: - ar.mx.Lock() - ar.status = autonat.NATStatusUnknown - ar.mx.Unlock() - wait = autonat.AutoNATRetryInterval + select { + case ev, ok := <-subReachability.Out(): + if !ok { + return + } + evt, ok := ev.(event.EvtLocalReachabilityChanged) + if !ok { + return + } - case autonat.NATStatusPublic: - ar.mx.Lock() - if ar.status != autonat.NATStatusPublic { - push = true + var update bool + if evt.Reachability == network.ReachabilityPrivate { + // TODO: this is a long-lived (2.5min task) that should get spun up in a separate thread + // and canceled if the relay learns the nat is now public. + update = ar.findRelays(ctx) } - ar.status = autonat.NATStatusPublic - ar.mx.Unlock() - case autonat.NATStatusPrivate: - update := ar.findRelays(ctx) ar.mx.Lock() - if update || ar.status != autonat.NATStatusPrivate { + if update || (ar.status != evt.Reachability && evt.Reachability != network.ReachabilityUnknown) { push = true } - ar.status = autonat.NATStatusPrivate + ar.status = evt.Reachability ar.mx.Unlock() + case <-ar.disconnect: + push = true + case <-ctx.Done(): + return } if push { @@ -125,15 +119,7 @@ func (ar *AutoRelay) background(ctx context.Context) { ar.cachedAddrs = nil ar.mx.Unlock() push = false - ar.host.CheckForAddressChanges() - } - - select { - case <-ar.disconnect: - push = true - case <-time.After(wait): - case <-ctx.Done(): - return + ar.host.SignalAddressChange() } } } @@ -283,7 +269,7 @@ func (ar *AutoRelay) relayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr { ar.mx.Lock() defer ar.mx.Unlock() - if ar.status != autonat.NATStatusPrivate { + if ar.status != network.ReachabilityPrivate { return addrs } From 4269886489e5edc620caa1792d72dad55ecaaee9 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 26 Mar 2020 12:49:46 +0530 Subject: [PATCH 37/43] get it working --- go.mod | 4 ++-- go.sum | 21 ++++++++++++++++++++- p2p/host/basic/basic_host.go | 7 +------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 4044698945..532d10a5af 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,11 @@ require ( github.com/jbenet/go-cienv v0.1.0 github.com/jbenet/goprocess v0.1.4 github.com/libp2p/go-conn-security-multistream v0.1.0 - github.com/libp2p/go-eventbus v0.1.0 + github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4 github.com/libp2p/go-libp2p-autonat v0.2.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.5.0 + github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.2 diff --git a/go.sum b/go.sum index c9480f43fe..70f1e34f36 100644 --- a/go.sum +++ b/go.sum @@ -59,7 +59,6 @@ github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfm github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= - github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= @@ -82,6 +81,8 @@ github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= +github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE= +github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1 h1:Wz9bL2wB2YBJqggkA4dD7oSmqB4cAnpNbGrlHJulv50= @@ -132,6 +133,8 @@ github.com/libp2p/go-conn-security-multistream v0.1.0 h1:aqGmto+ttL/uJgX0JtQI0tD github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-eventbus v0.1.0 h1:mlawomSAjjkk97QnYiEmHsLu7E136+2oCWSHRUvMfzQ= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= +github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4 h1:HoGcifIkfX/eBrb19dzCBFfzUXL9g2aZl4OHAvgbDuE= +github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4/go.mod h1:cGzwn3/iN5WAOBJuqrwIuqwoUoDenNavOK+K0kVcbnM= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= @@ -153,6 +156,13 @@ github.com/libp2p/go-libp2p-core v0.2.0/go.mod h1:X0eyB0Gy93v0DZtSYbEM7RnMChm9Uv github.com/libp2p/go-libp2p-core v0.2.2/go.mod h1:8fcwTbsG2B+lTgRJ1ICZtiM5GWCWZVoVrLaDRvIRng0= github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= +github.com/libp2p/go-libp2p-core v0.3.1/go.mod h1:thvWy0hvaSBhnVBaW37BvzgVV68OUhgJJLAa6almrII= +github.com/libp2p/go-libp2p-core v0.4.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= +github.com/libp2p/go-libp2p-core v0.5.0 h1:FBQ1fpq2Fo/ClyjojVJ5AKXlKhvNc/B6U0O+7AN1ffE= +github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326052016-dc8b8fa8c504/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f h1:b8OxzaV6EecoGBMum8ENSl07sbacu2EepywXgTOpRVM= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -362,10 +372,14 @@ golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -381,6 +395,7 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -408,6 +423,9 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -418,6 +436,7 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index ed486e0619..d0ff39615a 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -215,7 +215,7 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo if hostInitializedEmitter, err = h.eventbus.Emitter(&event.EvtLocalHostInitialized{}); err != nil { return nil, fmt.Errorf("failed to create emitter for EvtLocalHostInitialized, err=%s", err) } - if err := hostInitializedEmitter.Emit(new(event.EvtLocalHostInitialized)); err != nil { + if err := hostInitializedEmitter.Emit(event.EvtLocalHostInitialized{}); err != nil { return nil, fmt.Errorf("failed to emit EvtLocalHostInitialized, err=%s", err) } @@ -250,7 +250,6 @@ func New(net network.Network, opts ...interface{}) *BasicHost { } h, err := NewHost(context.Background(), net, hostopts) - h.Start() if err != nil { // this cannot happen with legacy options // plus we want to keep the (deprecated) legacy interface unchanged @@ -386,9 +385,6 @@ func (h *BasicHost) background(p goprocess.Process) { changeEvt := makeUpdatedAddrEvent(lastAddrs, addrs) if changeEvt != nil { lastAddrs = addrs - } - - if changeEvt != nil { err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt) if err != nil { log.Warnf("error emitting event for updated addrs: %s", err) @@ -397,7 +393,6 @@ func (h *BasicHost) background(p goprocess.Process) { } } - // ID returns the (local) peer.ID associated with this Host func (h *BasicHost) ID() peer.ID { return h.Network().LocalPeer() From ffef2df8d1a29a24f34b4a915b1dd839369c7e49 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 26 Mar 2020 13:07:33 +0530 Subject: [PATCH 38/43] skip test local addr filtering --- p2p/protocol/identify/id_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 1ac5cfb419..7118a0997e 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -277,6 +277,7 @@ func TestProtoMatching(t *testing.T) { } func TestLocalhostAddrFiltering(t *testing.T) { + t.Skip("need to fix this test") ctx, cancel := context.WithCancel(context.Background()) defer cancel() mn := mocknet.New(ctx) From c351c42dc7b8de13a947367391cd4f5f5387484a Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 26 Mar 2020 19:17:18 +0530 Subject: [PATCH 39/43] take signed records to completion --- go.mod | 3 +- go.sum | 6 ++ p2p/host/basic/basic_host.go | 111 ++++++++++++++++-------- p2p/host/basic/basic_host_test.go | 73 +++++++++++++++- p2p/host/basic/peer_record.go | 130 ----------------------------- p2p/host/basic/peer_record_test.go | 111 ------------------------ p2p/protocol/identify/id.go | 40 +++------ p2p/protocol/identify/id_test.go | 4 +- 8 files changed, 168 insertions(+), 310 deletions(-) delete mode 100644 p2p/host/basic/peer_record.go delete mode 100644 p2p/host/basic/peer_record_test.go diff --git a/go.mod b/go.mod index 532d10a5af..d189b19a77 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-libp2p-autonat v0.2.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 - github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f + github.com/libp2p/go-libp2p-core v0.5.1-0.20200326115709-8173af76d179 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-loggables v0.1.0 github.com/libp2p/go-libp2p-mplex v0.2.2 @@ -34,6 +34,7 @@ require ( github.com/multiformats/go-multiaddr-dns v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.3 github.com/multiformats/go-multistream v0.1.1 + github.com/stretchr/testify v1.4.0 github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 ) diff --git a/go.sum b/go.sum index 70f1e34f36..d0fe1ee081 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018 h1:6xT9KW8zLC5IlbaIF5Q7JNieBoACT7iW0YTxQHR0in0= github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018/go.mod h1:rQYf4tfk5sSwFsnDg3qYaBxSjsD9S8+59vW0dKUgme4= @@ -163,6 +164,8 @@ github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZas github.com/libp2p/go-libp2p-core v0.5.1-0.20200326052016-dc8b8fa8c504/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f h1:b8OxzaV6EecoGBMum8ENSl07sbacu2EepywXgTOpRVM= github.com/libp2p/go-libp2p-core v0.5.1-0.20200326060257-dc01664f700f/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326115709-8173af76d179 h1:Pu5KI52/+yjD3WoTg4UlxbwHvct96i3NQYFUqubv4jU= +github.com/libp2p/go-libp2p-core v0.5.1-0.20200326115709-8173af76d179/go.mod h1:2SngjXPThb990GE6oJMZqMV11LOXSADC7bCgf95w1qI= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -315,6 +318,7 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -335,6 +339,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= @@ -443,5 +448,6 @@ gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFab gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index d0ff39615a..c157f9de65 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -2,29 +2,31 @@ package basichost import ( "context" - "fmt" + "errors" "io" "net" "time" - "github.com/libp2p/go-libp2p/p2p/protocol/identify" - "github.com/libp2p/go-libp2p/p2p/protocol/ping" - "github.com/libp2p/go-libp2p-core/connmgr" + "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/protocol" + "github.com/libp2p/go-libp2p-core/record" "github.com/libp2p/go-eventbus" inat "github.com/libp2p/go-libp2p-nat" + "github.com/libp2p/go-libp2p/p2p/protocol/identify" + "github.com/libp2p/go-libp2p/p2p/protocol/ping" logging "github.com/ipfs/go-log" "github.com/jbenet/goprocess" goprocessctx "github.com/jbenet/goprocess/context" + "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr" madns "github.com/multiformats/go-multiaddr-dns" manet "github.com/multiformats/go-multiaddr-net" @@ -77,7 +79,6 @@ type BasicHost struct { maResolver *madns.Resolver cmgr connmgr.ConnManager eventbus event.Bus - peerrecmgr *peerRecordManager AddrsFactory AddrsFactory @@ -91,6 +92,9 @@ type BasicHost struct { } addrChangeChan chan struct{} + + signKey crypto.PrivKey + caBook peerstore.CertifiedAddrBook } var _ host.Host = (*BasicHost)(nil) @@ -144,18 +148,19 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo if h.emitters.evtLocalProtocolsUpdated, err = h.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil { return nil, err } - if h.emitters.evtLocalAddrsUpdated, err = h.eventbus.Emitter(&event.EvtLocalAddressesUpdated{}); err != nil { + if h.emitters.evtLocalAddrsUpdated, err = h.eventbus.Emitter(&event.EvtLocalAddressesUpdated{}, eventbus.Stateful); err != nil { return nil, err } - hostKey := h.Peerstore().PrivKey(h.ID()) - if hostKey == nil { - log.Warn("unable to access host key. peer record support disabled.") - } else { - h.peerrecmgr, err = NewPeerRecordManager(ctx, h.EventBus(), hostKey, h.Addrs()) - if err != nil { - log.Errorf("error creating peer record manager. peer record support disabled. err: %s", err) - } + cab, ok := peerstore.GetCertifiedAddrBook(net.Peerstore()) + if !ok { + return nil, errors.New("peerstore should also be a certified address book") + } + h.caBook = cab + + h.signKey = h.Peerstore().PrivKey(h.ID()) + if h.signKey == nil { + return nil, errors.New("unable to access host key") } h.proc = goprocessctx.WithContextAndTeardown(ctx, func() error { @@ -211,14 +216,6 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo net.SetConnHandler(h.newConnHandler) net.SetStreamHandler(h.newStreamHandler) - var hostInitializedEmitter event.Emitter - if hostInitializedEmitter, err = h.eventbus.Emitter(&event.EvtLocalHostInitialized{}); err != nil { - return nil, fmt.Errorf("failed to create emitter for EvtLocalHostInitialized, err=%s", err) - } - if err := hostInitializedEmitter.Emit(event.EvtLocalHostInitialized{}); err != nil { - return nil, fmt.Errorf("failed to emit EvtLocalHostInitialized, err=%s", err) - } - return h, nil } @@ -363,15 +360,65 @@ func makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.EvtLocalAddresses return &evt } +func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (*record.Envelope, error) { + current := make([]multiaddr.Multiaddr, len(evt.Current)) + for _, a := range evt.Current { + current = append(current, a.Address) + } + + addrs := make([]multiaddr.Multiaddr, 0, len(current)) + for _, a := range current { + if a == nil { + continue + } + addrs = append(addrs, a) + } + + rec := peer.NewPeerRecord() + rec.PeerID = h.network.LocalPeer() + rec.Addrs = addrs + return record.Seal(rec, h.signKey) +} + func (h *BasicHost) background(p goprocess.Process) { + var lastAddrs []ma.Multiaddr + + emitAddrChange := func(currentAddrs []ma.Multiaddr) { + changeEvt := makeUpdatedAddrEvent(lastAddrs, currentAddrs) + if changeEvt != nil { + // add signed peer record to the event + sr, err := h.makeSignedPeerRecord(changeEvt) + if err != nil { + log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err) + return + } + changeEvt.SignedPeerRecord = *sr + + // persist the signed record to the peerstore + if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil { + log.Errorf("failed to persist signed peer record in peer store, err=%s", err) + return + } + + // emit addr change event on the bus + if err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt); err != nil { + log.Warnf("error emitting event for updated addrs: %s", err) + } + } + } + + // immediately emit the first address change event if we have a listen address + if h.Addrs() != nil { + emitAddrChange(h.Addrs()) + } + // init lastAddrs + lastAddrs = h.Addrs() + // periodically schedules an IdentifyPush to update our peers for changes // in our address set (if needed) ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() - // initialize lastAddrs - lastAddrs := h.Addrs() - for { select { case <-ticker.C: @@ -380,16 +427,10 @@ func (h *BasicHost) background(p goprocess.Process) { return } - // emit an EvtLocalAddressesUpdatedEvent & a Push Identify if our listen addresses have changed. - addrs := h.Addrs() - changeEvt := makeUpdatedAddrEvent(lastAddrs, addrs) - if changeEvt != nil { - lastAddrs = addrs - err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt) - if err != nil { - log.Warnf("error emitting event for updated addrs: %s", err) - } - } + // emit an EvtLocalAddressesUpdatedEvent event if our listen addresses have changed. + emitAddrChange(h.Addrs()) + // update last seen addrs + lastAddrs = h.Addrs() } } diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 361df9ad66..7e81c26103 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -3,7 +3,6 @@ package basichost import ( "bytes" "context" - "github.com/libp2p/go-libp2p/p2p/protocol/identify" "io" "reflect" "sort" @@ -11,6 +10,9 @@ import ( "testing" "time" + "github.com/libp2p/go-libp2p-core/peerstore" + "github.com/libp2p/go-libp2p-core/record" + "github.com/libp2p/go-eventbus" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/helpers" @@ -19,10 +21,12 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-libp2p-core/test" + "github.com/libp2p/go-libp2p/p2p/protocol/identify" swarmt "github.com/libp2p/go-libp2p-swarm/testing" ma "github.com/multiformats/go-multiaddr" madns "github.com/multiformats/go-multiaddr-dns" + "github.com/stretchr/testify/require" ) func TestHostDoubleClose(t *testing.T) { @@ -528,6 +532,47 @@ func TestAddrResolutionRecursive(t *testing.T) { } } +func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) { + ctx := context.Background() + taddrs := []ma.Multiaddr{ma.StringCast("/ip4/1.2.3.4/tcp/1234")} + + h := New(swarmt.GenSwarm(t, ctx), AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr { + return taddrs + })) + defer h.Close() + + sub, err := h.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}) + if err != nil { + t.Error(err) + } + defer sub.Close() + // wait for the host background thread to start + time.Sleep(1 * time.Second) + + expected := event.EvtLocalAddressesUpdated{ + Diffs: true, + Current: []event.UpdatedAddress{ + {Action: event.Added, Address: ma.StringCast("/ip4/1.2.3.4/tcp/1234")}, + }, + Removed: []event.UpdatedAddress{}} + + // assert we get expected event + evt := waitForAddrChangeEvent(ctx, sub, t) + if !updatedAddrEventsEqual(expected, evt) { + t.Errorf("change events not equal: \n\texpected: %v \n\tactual: %v", expected, evt) + } + + // assert it's on the signed record + rc := peerRecordFromEnvelope(t, evt.SignedPeerRecord) + require.Equal(t, taddrs, rc.Addrs) + + // assert it's in the peerstore + ev := h.Peerstore().(peerstore.CertifiedAddrBook).GetPeerRecord(h.ID()) + require.NotNil(t, ev) + rc = peerRecordFromEnvelope(t, *ev) + require.Equal(t, taddrs, rc.Addrs) +} + func TestHostAddrChangeDetection(t *testing.T) { // This test uses the address factory to provide several // sets of listen addresses for the host. It advances through @@ -603,9 +648,18 @@ func TestHostAddrChangeDetection(t *testing.T) { h.SignalAddressChange() evt := waitForAddrChangeEvent(ctx, sub, t) if !updatedAddrEventsEqual(expectedEvents[i-1], evt) { - t.Errorf("change events not equal: \n\texpected: %v \n\tactual: %v", expectedEvents[i], evt) + t.Errorf("change events not equal: \n\texpected: %v \n\tactual: %v", expectedEvents[i-1], evt) } + // assert it's on the signed record + rc := peerRecordFromEnvelope(t, evt.SignedPeerRecord) + require.Equal(t, addrSets[i], rc.Addrs) + + // assert it's in the peerstore + ev := h.Peerstore().(peerstore.CertifiedAddrBook).GetPeerRecord(h.ID()) + require.NotNil(t, ev) + rc = peerRecordFromEnvelope(t, *ev) + require.Equal(t, addrSets[i], rc.Addrs) } } @@ -671,3 +725,18 @@ func updatedAddrEventsEqual(a, b event.EvtLocalAddressesUpdated) bool { updatedAddrsEqual(a.Current, b.Current) && updatedAddrsEqual(a.Removed, b.Removed) } + +func peerRecordFromEnvelope(t *testing.T, ev record.Envelope) *peer.PeerRecord { + t.Helper() + rec, err := ev.Record() + if err != nil { + t.Fatalf("error getting PeerRecord from event: %v", err) + return nil + } + peerRec, ok := rec.(*peer.PeerRecord) + if !ok { + t.Fatalf("wrong type for peer record") + return nil + } + return peerRec +} diff --git a/p2p/host/basic/peer_record.go b/p2p/host/basic/peer_record.go deleted file mode 100644 index 66bc233a68..0000000000 --- a/p2p/host/basic/peer_record.go +++ /dev/null @@ -1,130 +0,0 @@ -package basichost - -import ( - "context" - - "github.com/libp2p/go-eventbus" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/libp2p/go-libp2p-core/event" - "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-core/record" - "github.com/multiformats/go-multiaddr" -) - -// peerRecordManager creates new signed peer.PeerRecords that can -// be shared with other peers to inform them of our listen addresses in -// a secure and authenticated way. -// -// New signed records are created in response to EvtLocalAddressesUpdated events, -// and are emitted in EvtLocalPeerRecordUpdated events. -// -// EvtLocalPeerRecordUpdated is emitted using a Stateful emitter, so new subscribers -// will immediately receive the current record when they subscribe, with future -// records delivered in future events. -type peerRecordManager struct { - hostID peer.ID - signingKey crypto.PrivKey - - ctx context.Context - subscriptions struct { - localAddrsUpdated event.Subscription - } - emitters struct { - evtLocalPeerRecordUpdated event.Emitter - } -} - -// NewPeerRecordManager creates a peerRecordManager that will subscribe to the given event.Bus -// and listen for changes in the local Host's addresses, emitting new signed peer.PeerRecords -// in response. The new records will be contained in event.EvtLocalPeerRecordUpdated events -// and emitted on the event bus. -// -// PeerRecords will be signed with the given private key, which must be the libp2p Host's -// identity key for the resulting records to be valid. -// -// If initialAddrs is non-empty, a PeerRecord will be created immediately and emitted on -// the bus, without waiting for an event.LocalPeerAddressesUpdated event to trigger an -// update. -func NewPeerRecordManager(ctx context.Context, bus event.Bus, hostKey crypto.PrivKey, initialAddrs []multiaddr.Multiaddr) (*peerRecordManager, error) { - hostID, err := peer.IDFromPrivateKey(hostKey) - if err != nil { - return nil, err - } - - m := &peerRecordManager{ - ctx: ctx, - signingKey: hostKey, - hostID: hostID, - } - - m.subscriptions.localAddrsUpdated, err = bus.Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) - if err != nil { - return nil, err - } - m.emitters.evtLocalPeerRecordUpdated, err = bus.Emitter(&event.EvtLocalPeerRecordUpdated{}, eventbus.Stateful) - if err != nil { - return nil, err - } - - go m.handleEvents() - - if len(initialAddrs) != 0 { - m.update(initialAddrs) - } - return m, nil -} - -func (m *peerRecordManager) handleEvents() { - sub := m.subscriptions.localAddrsUpdated - defer sub.Close() - - for { - select { - case evt, more := <-sub.Out(): - if !more { - return - } - e := evt.(event.EvtLocalAddressesUpdated) - m.update(addrsFromEvent(e)) - case <-m.ctx.Done(): - return - } - } -} - -func (m *peerRecordManager) update(addrs []multiaddr.Multiaddr) { - envelope, err := m.makeSignedPeerRecord(addrs) - if err != nil { - log.Errorf("error creating signed peer record: %v", err) - return - } - - stateEvt := event.EvtLocalPeerRecordUpdated{Record: *envelope} - err = m.emitters.evtLocalPeerRecordUpdated.Emit(stateEvt) - if err != nil { - log.Errorf("error emitting event for updated peer record: %v", err) - } -} - -func (m *peerRecordManager) makeSignedPeerRecord(current []multiaddr.Multiaddr) (*record.Envelope, error) { - addrs := make([]multiaddr.Multiaddr, 0, len(current)) - for _, a := range current { - if a == nil { - continue - } - addrs = append(addrs, a) - } - - rec := peer.NewPeerRecord() - rec.PeerID = m.hostID - rec.Addrs = addrs - return record.Seal(rec, m.signingKey) -} - -func addrsFromEvent(evt event.EvtLocalAddressesUpdated) []multiaddr.Multiaddr { - addrs := make([]multiaddr.Multiaddr, len(evt.Current)) - for _, a := range evt.Current { - addrs = append(addrs, a.Address) - } - return addrs -} diff --git a/p2p/host/basic/peer_record_test.go b/p2p/host/basic/peer_record_test.go deleted file mode 100644 index d883ce2b46..0000000000 --- a/p2p/host/basic/peer_record_test.go +++ /dev/null @@ -1,111 +0,0 @@ -package basichost - -import ( - "context" - "testing" - "time" - - "github.com/libp2p/go-eventbus" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/libp2p/go-libp2p-core/event" - "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-core/test" - "github.com/multiformats/go-multiaddr" -) - -func TestPeerRecordManagerEmitsPeerRecord(t *testing.T) { - bus, emitter, sub := setupEventBus(t) - priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - t.Fatalf("error generating test keypair: %v", err) - } - - ctx := context.Background() - var initialAddrs []multiaddr.Multiaddr - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs) - if err != nil { - t.Fatalf("error creating peerRecordManager: %v", err) - } - - addrs := []multiaddr.Multiaddr{ - multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), - } - addrEvent := makeAddrEvent(addrs) - err = emitter.Emit(addrEvent) - test.AssertNilError(t, err) - - evt := getNextPeerRecordEvent(t, sub) - peerRec := peerRecordFromEvent(t, evt) - test.AssertAddressesEqual(t, addrs, peerRec.Addrs) -} - -func TestPeerRecordManagerEmitsRecordImmediatelyIfInitialAddrsAreProvided(t *testing.T) { - bus, _, sub := setupEventBus(t) - priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - t.Fatalf("error generating test keypair: %v", err) - } - - ctx := context.Background() - initialAddrs := []multiaddr.Multiaddr{ - multiaddr.StringCast("/ip4/1.2.3.4/tcp/42"), - } - _, err = NewPeerRecordManager(ctx, bus, priv, initialAddrs) - if err != nil { - t.Fatalf("error creating peerRecordManager: %v", err) - } - - recEvent := getNextPeerRecordEvent(t, sub) - rec, err := recEvent.Record.Record() - test.AssertNilError(t, err) - test.AssertAddressesEqual(t, initialAddrs, rec.(*peer.PeerRecord).Addrs) -} - -func setupEventBus(t *testing.T) (bus event.Bus, emitter event.Emitter, sub event.Subscription) { - t.Helper() - bus = eventbus.NewBus() - emitter, err := bus.Emitter(&event.EvtLocalAddressesUpdated{}) - if err != nil { - t.Fatalf("error creating event emitter: %v", err) - } - - sub, err = bus.Subscribe(&event.EvtLocalPeerRecordUpdated{}) - if err != nil { - t.Fatalf("error creating event subscription: %v", err) - } - return bus, emitter, sub -} - -func getNextPeerRecordEvent(t *testing.T, sub event.Subscription) event.EvtLocalPeerRecordUpdated { - t.Helper() - select { - case e := <-sub.Out(): - return e.(event.EvtLocalPeerRecordUpdated) - case <-time.After(1 * time.Second): - t.Fatalf("timed out waiting for peer record") - return event.EvtLocalPeerRecordUpdated{} - } -} - -func peerRecordFromEvent(t *testing.T, evt event.EvtLocalPeerRecordUpdated) *peer.PeerRecord { - t.Helper() - rec, err := evt.Record.Record() - if err != nil { - t.Fatalf("error getting PeerRecord from event: %v", err) - return nil - } - peerRec, ok := rec.(*peer.PeerRecord) - if !ok { - t.Fatalf("wrong type for peer record") - return nil - } - return peerRec -} - -func makeAddrEvent(addrs []multiaddr.Multiaddr) event.EvtLocalAddressesUpdated { - current := make([]event.UpdatedAddress, len(addrs)) - for i, a := range addrs { - current[i] = event.UpdatedAddress{Address: a} - } - return event.EvtLocalAddressesUpdated{Current: current} -} diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index d83c065d8c..8106da2b14 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -84,18 +84,16 @@ type IDService struct { addrMu sync.Mutex - enableNewProtos sync.Once - peerrec *record.Envelope - peerrecMu sync.RWMutex + peerrec *record.Envelope + peerrecMu sync.RWMutex // our own observed addresses. // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet subscriptions struct { - localProtocolsUpdated event.Subscription - localAddrsUpdated event.Subscription - localPeerRecordUpdated event.Subscription + localProtocolsUpdated event.Subscription + localAddrsUpdated event.Subscription } emitters struct { evtPeerProtocolsUpdated event.Emitter @@ -134,12 +132,7 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { } else { go s.handleEvents(s.subscriptions.localProtocolsUpdated, s.handleProtosChanged) } - s.subscriptions.localPeerRecordUpdated, err = h.EventBus().Subscribe(&event.EvtLocalPeerRecordUpdated{}, eventbus.BufSize(128)) - if err != nil { - log.Warnf("identify service not subscribed to local peer record changes; err: %s", err) - } else { - go s.handleEvents(s.subscriptions.localPeerRecordUpdated, s.handlePeerRecordUpdated) - } + s.subscriptions.localAddrsUpdated, err = h.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) if err != nil { log.Warnf("identify service not subscribed to address changes. err: %s", err) @@ -160,13 +153,15 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { log.Warnf("identify service not emitting identification failed events; err: %s", err) } - // register protocols that depend on peer records immediately. - // those that do will be registered when we receive the first - // peer record on the event bus + // register protocols that do not depend on peer records. h.SetStreamHandler(IDDelta, s.deltaHandler) h.SetStreamHandler(LegacyID, s.requestHandler) h.SetStreamHandler(LegacyIDPush, s.pushHandler) + // register protocols that depend on peer records. + h.SetStreamHandler(ID, s.requestHandler) + h.SetStreamHandler(IDPush, s.pushHandler) + h.Network().Notify((*netNotifiee)(s)) return s } @@ -192,24 +187,11 @@ func (ids *IDService) handleProtosChanged(evt interface{}) { } func (ids *IDService) handleLocalAddrsUpdated(evt interface{}) { - log.Debug("triggering push based on updated local addresses") - ids.Push() -} - -func (ids *IDService) handlePeerRecordUpdated(evt interface{}) { ids.peerrecMu.Lock() - existed := ids.peerrec != nil - rec := (evt.(event.EvtLocalPeerRecordUpdated)).Record + rec := (evt.(event.EvtLocalAddressesUpdated)).SignedPeerRecord ids.peerrec = &rec ids.peerrecMu.Unlock() - if !existed { - ids.enableNewProtos.Do(func() { - ids.Host.SetStreamHandler(ID, ids.requestHandler) - ids.Host.SetStreamHandler(IDPush, ids.pushHandler) - }) - } - log.Debug("triggering push based on updated local PeerRecord") ids.Push() } diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 7118a0997e..9bf8873c71 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -234,8 +234,8 @@ func generatePeerRecord(t *testing.T, h host.Host) { if err != nil { t.Fatalf("error generating peer record: %s", err) } - evt := event.EvtLocalPeerRecordUpdated{Record: *signed} - emitter, err := h.EventBus().Emitter(&evt, eventbus.Stateful) + evt := event.EvtLocalAddressesUpdated{SignedPeerRecord: *signed} + emitter, err := h.EventBus().Emitter(new(event.EvtLocalAddressesUpdated), eventbus.Stateful) if err != nil { t.Fatal(err) } From d7405745dda131d52d3cc59e8be9c79496621554 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Fri, 27 Mar 2020 16:02:17 +0530 Subject: [PATCH 40/43] change ticker time --- p2p/host/basic/basic_host.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index c157f9de65..344b678d7c 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -416,7 +416,7 @@ func (h *BasicHost) background(p goprocess.Process) { // periodically schedules an IdentifyPush to update our peers for changes // in our address set (if needed) - ticker := time.NewTicker(10 * time.Second) + ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() for { From 447464594b084c825d0411908ea5c71629504192 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Wed, 1 Apr 2020 11:21:45 +0530 Subject: [PATCH 41/43] changes as per first round of review --- go.mod | 2 +- p2p/host/basic/basic_host.go | 60 +++++++++++++++---------------- p2p/host/basic/basic_host_test.go | 41 +++++++-------------- 3 files changed, 44 insertions(+), 59 deletions(-) diff --git a/go.mod b/go.mod index d189b19a77..4c96a9d11d 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/jbenet/go-cienv v0.1.0 github.com/jbenet/goprocess v0.1.4 github.com/libp2p/go-conn-security-multistream v0.1.0 - github.com/libp2p/go-eventbus v0.1.1-0.20200326052355-c30c9409b9a4 + github.com/libp2p/go-eventbus v0.1.0 github.com/libp2p/go-libp2p-autonat v0.2.1 github.com/libp2p/go-libp2p-blankhost v0.1.4 github.com/libp2p/go-libp2p-circuit v0.1.4 diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 344b678d7c..21034d4091 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -375,7 +375,7 @@ func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (* } rec := peer.NewPeerRecord() - rec.PeerID = h.network.LocalPeer() + rec.PeerID = h.ID() rec.Addrs = addrs return record.Seal(rec, h.signKey) } @@ -383,36 +383,37 @@ func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (* func (h *BasicHost) background(p goprocess.Process) { var lastAddrs []ma.Multiaddr - emitAddrChange := func(currentAddrs []ma.Multiaddr) { + emitAddrChange := func(currentAddrs []ma.Multiaddr, lastAddrs []ma.Multiaddr) { + // nothing to do if both are nil..defensive check + if currentAddrs == nil && lastAddrs == nil { + return + } + changeEvt := makeUpdatedAddrEvent(lastAddrs, currentAddrs) - if changeEvt != nil { - // add signed peer record to the event - sr, err := h.makeSignedPeerRecord(changeEvt) - if err != nil { - log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err) - return - } - changeEvt.SignedPeerRecord = *sr - // persist the signed record to the peerstore - if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil { - log.Errorf("failed to persist signed peer record in peer store, err=%s", err) - return - } + if changeEvt == nil { + return + } - // emit addr change event on the bus - if err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt); err != nil { - log.Warnf("error emitting event for updated addrs: %s", err) - } + // add signed peer record to the event + sr, err := h.makeSignedPeerRecord(changeEvt) + if err != nil { + log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err) + return } - } + changeEvt.SignedPeerRecord = *sr - // immediately emit the first address change event if we have a listen address - if h.Addrs() != nil { - emitAddrChange(h.Addrs()) + // persist the signed record to the peerstore + if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil { + log.Errorf("failed to persist signed peer record in peer store, err=%s", err) + return + } + + // emit addr change event on the bus + if err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt); err != nil { + log.Warnf("error emitting event for updated addrs: %s", err) + } } - // init lastAddrs - lastAddrs = h.Addrs() // periodically schedules an IdentifyPush to update our peers for changes // in our address set (if needed) @@ -420,17 +421,16 @@ func (h *BasicHost) background(p goprocess.Process) { defer ticker.Stop() for { + curr := h.Addrs() + emitAddrChange(curr, lastAddrs) + lastAddrs = curr + select { case <-ticker.C: case <-h.addrChangeChan: case <-p.Closing(): return } - - // emit an EvtLocalAddressesUpdatedEvent event if our listen addresses have changed. - emitAddrChange(h.Addrs()) - // update last seen addrs - lastAddrs = h.Addrs() } } diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 7e81c26103..fe67e1a9ae 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -5,25 +5,24 @@ import ( "context" "io" "reflect" - "sort" "sync" "testing" "time" - "github.com/libp2p/go-libp2p-core/peerstore" - "github.com/libp2p/go-libp2p-core/record" - - "github.com/libp2p/go-eventbus" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/helpers" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/protocol" + "github.com/libp2p/go-libp2p-core/record" "github.com/libp2p/go-libp2p-core/test" - "github.com/libp2p/go-libp2p/p2p/protocol/identify" + "github.com/libp2p/go-eventbus" swarmt "github.com/libp2p/go-libp2p-swarm/testing" + "github.com/libp2p/go-libp2p/p2p/protocol/identify" + ma "github.com/multiformats/go-multiaddr" madns "github.com/multiformats/go-multiaddr-dns" "github.com/stretchr/testify/require" @@ -456,11 +455,10 @@ func TestAddrResolution(t *testing.T) { _ = h.Connect(tctx, *pi) addrs := h.Peerstore().Addrs(pi.ID) - sort.Sort(sortedMultiaddrs(addrs)) - if len(addrs) != 2 || !addrs[0].Equal(addr1) || !addrs[1].Equal(addr2) { - t.Fatalf("expected [%s %s], got %+v", addr1, addr2, addrs) - } + require.Len(t, addrs, 2) + require.Contains(t, addrs, addr1) + require.Contains(t, addrs, addr2) } func TestAddrResolutionRecursive(t *testing.T) { @@ -511,11 +509,9 @@ func TestAddrResolutionRecursive(t *testing.T) { _ = h.Connect(tctx, *pi1) addrs1 := h.Peerstore().Addrs(pi1.ID) - sort.Sort(sortedMultiaddrs(addrs1)) - - if len(addrs1) != 2 || !addrs1[0].Equal(addr1) || !addrs1[1].Equal(addr2) { - t.Fatalf("expected [%s %s], got %+v", addr1, addr2, addrs1) - } + require.Len(t, addrs1, 2) + require.Contains(t, addrs1, addr1) + require.Contains(t, addrs1, addr2) pi2, err := peer.AddrInfoFromP2pAddr(p2paddr2) if err != nil { @@ -525,11 +521,8 @@ func TestAddrResolutionRecursive(t *testing.T) { _ = h.Connect(tctx, *pi2) addrs2 := h.Peerstore().Addrs(pi2.ID) - sort.Sort(sortedMultiaddrs(addrs2)) - - if len(addrs2) != 1 || !addrs2[0].Equal(addr1) { - t.Fatalf("expected [%s], got %+v", addr1, addrs2) - } + require.Len(t, addrs2, 1) + require.Contains(t, addrs2, addr1) } func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) { @@ -679,14 +672,6 @@ func waitForAddrChangeEvent(ctx context.Context, sub event.Subscription, t *test } } -type sortedMultiaddrs []ma.Multiaddr - -func (sma sortedMultiaddrs) Len() int { return len(sma) } -func (sma sortedMultiaddrs) Swap(i, j int) { sma[i], sma[j] = sma[j], sma[i] } -func (sma sortedMultiaddrs) Less(i, j int) bool { - return bytes.Compare(sma[i].Bytes(), sma[j].Bytes()) == 1 -} - // updatedAddrsEqual is a helper to check whether two lists of // event.UpdatedAddress have the same contents, ignoring ordering. func updatedAddrsEqual(a, b []event.UpdatedAddress) bool { From 15310dcea4f20f8316a62cbbee08e8c8eca272bf Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 9 Apr 2020 12:26:36 +0530 Subject: [PATCH 42/43] changes as per review --- p2p/host/basic/basic_host.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 21034d4091..189f266bf4 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -361,22 +361,12 @@ func makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.EvtLocalAddresses } func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (*record.Envelope, error) { - current := make([]multiaddr.Multiaddr, len(evt.Current)) + current := make([]multiaddr.Multiaddr, 0, len(evt.Current)) for _, a := range evt.Current { current = append(current, a.Address) } - addrs := make([]multiaddr.Multiaddr, 0, len(current)) - for _, a := range current { - if a == nil { - continue - } - addrs = append(addrs, a) - } - - rec := peer.NewPeerRecord() - rec.PeerID = h.ID() - rec.Addrs = addrs + rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{h.ID(), current}) return record.Seal(rec, h.signKey) } From 8c60995a92fbb5ecfc832ef16a74fb6a65b4bd7b Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Fri, 24 Apr 2020 14:08:00 +0530 Subject: [PATCH 43/43] changes as per review --- p2p/protocol/identify/id.go | 76 ++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 91b8d4a854..a81b02bd35 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -96,10 +96,6 @@ type IDService struct { // TODO: instead of expiring, remove these when we disconnect observedAddrs *ObservedAddrSet - subscriptions struct { - localProtocolsUpdated event.Subscription - localAddrsUpdated event.Subscription - } emitters struct { evtPeerProtocolsUpdated event.Emitter evtPeerIdentificationCompleted event.Emitter @@ -133,21 +129,9 @@ func NewIDService(h host.Host, opts ...Option) *IDService { // handle local protocol handler updates, and push deltas to peers. var err error - s.subscriptions.localProtocolsUpdated, err = h.EventBus().Subscribe(&event.EvtLocalProtocolsUpdated{}, eventbus.BufSize(128)) - if err != nil { - log.Warnf("identify service not subscribed to local protocol handlers updates; err: %s", err) - } else { - s.refCount.Add(1) - go s.handleEvents(s.subscriptions.localProtocolsUpdated, s.handleProtosChanged) - } - s.subscriptions.localAddrsUpdated, err = h.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(128)) - if err != nil { - log.Warnf("identify service not subscribed to address changes. err: %s", err) - } else { - s.refCount.Add(1) - go s.handleEvents(s.subscriptions.localAddrsUpdated, s.handleLocalAddrsUpdated) - } + s.refCount.Add(1) + go s.handleEvents() s.emitters.evtPeerProtocolsUpdated, err = h.EventBus().Emitter(&event.EvtPeerProtocolsUpdated{}) if err != nil { @@ -175,39 +159,53 @@ func NewIDService(h host.Host, opts ...Option) *IDService { return s } -// Close shuts down the IDService -func (ids *IDService) Close() error { - ids.closeSync.Do(func() { - ids.ctxCancel() - ids.refCount.Wait() - }) - return nil -} - -func (ids *IDService) handleEvents(sub event.Subscription, handler func(interface{})) { +func (ids *IDService) handleEvents() { defer ids.refCount.Done() + + sub, err := ids.Host.EventBus().Subscribe([]interface{}{&event.EvtLocalProtocolsUpdated{}, + &event.EvtLocalAddressesUpdated{}}, eventbus.BufSize(256)) + if err != nil { + log.Errorf("failed to subscribe to events on the bus, err=%s", err) + return + } + defer sub.Close() for { select { - case evt, more := <-sub.Out(): + case e, more := <-sub.Out(): if !more { return } - handler(evt) + switch evt := e.(type) { + case event.EvtLocalAddressesUpdated: + ids.handleLocalAddrsUpdated(evt) + case event.EvtLocalProtocolsUpdated: + ids.handleProtosChanged(evt) + } + case <-ids.ctx.Done(): return } } } -func (ids *IDService) handleProtosChanged(evt interface{}) { - ids.fireProtocolDelta(evt.(event.EvtLocalProtocolsUpdated)) +// Close shuts down the IDService +func (ids *IDService) Close() error { + ids.closeSync.Do(func() { + ids.ctxCancel() + ids.refCount.Wait() + }) + return nil +} + +func (ids *IDService) handleProtosChanged(evt event.EvtLocalProtocolsUpdated) { + ids.fireProtocolDelta(evt) } -func (ids *IDService) handleLocalAddrsUpdated(evt interface{}) { +func (ids *IDService) handleLocalAddrsUpdated(evt event.EvtLocalAddressesUpdated) { ids.peerrecMu.Lock() - rec := (evt.(event.EvtLocalAddressesUpdated)).SignedPeerRecord + rec := evt.SignedPeerRecord ids.peerrec = &rec ids.peerrecMu.Unlock() @@ -263,15 +261,7 @@ func (ids *IDService) IdentifyConn(c network.Conn) { return } - ids.peerrecMu.RLock() - havePeerRec := ids.peerrec != nil - ids.peerrecMu.RUnlock() - protocolIDs := []string{ID, LegacyID} - if !havePeerRec { - protocolIDs = []string{LegacyID} - } - // ok give the response to our handler. var selectedProto string if selectedProto, err = msmux.SelectOneOf(protocolIDs, s); err != nil { @@ -509,7 +499,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn, usePeerRe if ok && signedPeerRecord != nil { _, addErr := cab.ConsumePeerRecord(signedPeerRecord, ttl) if addErr != nil { - log.Errorf("error adding signed addrs to peerstore: %v", addErr) + log.Debugf("error adding signed addrs to peerstore: %v", addErr) } } else { ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl)