From 62676d8fb785a8fc279878cbe8e03b878f005910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Thu, 23 May 2019 18:02:22 +0100 Subject: [PATCH] Consolidate abstractions and core types into go-libp2p-core (#48) --- deprecated.go | 65 ++++++++++++ go.mod | 6 +- go.sum | 42 +++++--- peer.go | 181 ------------------------------- peer_serde.go | 75 ------------- peer_serde_test.go | 83 --------------- peer_test.go | 242 ------------------------------------------ peerset/deprecated.go | 17 +++ peerset/peerset.go | 72 ------------- test/deprecated.go | 24 +++++ test/utils.go | 39 ------- 11 files changed, 134 insertions(+), 712 deletions(-) create mode 100644 deprecated.go delete mode 100644 peer.go delete mode 100644 peer_serde.go delete mode 100644 peer_serde_test.go delete mode 100644 peer_test.go create mode 100644 peerset/deprecated.go delete mode 100644 peerset/peerset.go create mode 100644 test/deprecated.go delete mode 100644 test/utils.go diff --git a/deprecated.go b/deprecated.go new file mode 100644 index 0000000..600b5ec --- /dev/null +++ b/deprecated.go @@ -0,0 +1,65 @@ +// Deprecated: use github.com/libp2p/go-libp2p-core/peer instead. +package peer + +import ( + core "github.com/libp2p/go-libp2p-core/peer" + ic "github.com/libp2p/go-libp2p-crypto" +) + +var ( + // Deprecated: use github.com/libp2p/go-libp2p-core/peer.ErrEmptyPeerID instead. + ErrEmptyPeerID = core.ErrEmptyPeerID + // Deprecated: use github.com/libp2p/go-libp2p-core/peer.ErrNoPublicKey instead. + ErrNoPublicKey = core.ErrNoPublicKey +) + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.AdvanceEnableInlining instead. +// Warning: this variable's type makes it impossible to alias by reference. +// Reads and writes from/to this variable may be inaccurate or not have the intended effect. +var AdvancedEnableInlining = core.AdvancedEnableInlining + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.ID instead. +type ID = core.ID + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDSlice instead. +type IDSlice = core.IDSlice + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDFromString instead. +func IDFromString(s string) (core.ID, error) { + return core.IDFromString(s) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDFromBytes instead. +func IDFromBytes(b []byte) (core.ID, error) { + return core.IDFromBytes(b) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDB58Decode instead. +func IDB58Decode(s string) (core.ID, error) { + return core.IDB58Decode(s) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDB58Encode instead. +func IDB58Encode(id ID) string { + return core.IDB58Encode(id) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDHexDecode instead. +func IDHexDecode(s string) (core.ID, error) { + return core.IDHexDecode(s) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDHexEncode instead. +func IDHexEncode(id ID) string { + return core.IDHexEncode(id) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDFromPublicKey instead. +func IDFromPublicKey(pk ic.PubKey) (core.ID, error) { + return core.IDFromPublicKey(pk) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.IDFromPrivateKey instead. +func IDFromPrivateKey(sk ic.PrivKey) (core.ID, error) { + return core.IDFromPrivateKey(sk) +} diff --git a/go.mod b/go.mod index 9ec59dd..c2e26e7 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/libp2p/go-libp2p-peer require ( - github.com/libp2p/go-libp2p-crypto v0.0.2 - github.com/mr-tron/base58 v1.1.2 - github.com/multiformats/go-multihash v0.0.5 + github.com/libp2p/go-libp2p-core v0.0.1 + github.com/libp2p/go-libp2p-crypto v0.1.0 + golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 // indirect ) diff --git a/go.sum b/go.sum index 8566f0b..e5bf65d 100644 --- a/go.sum +++ b/go.sum @@ -8,30 +8,43 @@ github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVa github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495 h1:6IyqGr3fnd0tM3YxipK27TUskaOVUjU2nG45yzwcQKY= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyFSs7UnsU= +github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= +github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJu4ZbSc= +github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= +github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= -github.com/libp2p/go-libp2p-crypto v0.0.2 h1:TTdJ4y6Uoa6NxQcuEaVkQfFRcQeCE2ReDk8Ok4I0Fyw= -github.com/libp2p/go-libp2p-crypto v0.0.2/go.mod h1:eETI5OUfBnvARGOHrJz2eWNyTUxEGZnBxMcbUjfIj4I= +github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= +github.com/libp2p/go-libp2p-core v0.0.1 h1:HSTZtFIq/W5Ue43Zw+uWZyy2Vl5WtF0zDjKN8/DT/1I= +github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= +github.com/libp2p/go-libp2p-crypto v0.1.0 h1:k9MFy+o2zGDNGsaoZl0MA3iZ75qXxr9OOoAZF+sD5OQ= +github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= 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 h1:5W7KhL8HVF3XCFOweFD3BNESdnO8ewyYTFT2R+/b8FQ= 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 h1:l16XLUUJ34wIz+RIvLhSwGvLvKyy+W598b135bJN6mg= -github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= -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/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/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= +github.com/mr-tron/base58 v1.1.1 h1:OJIdWOWYe2l5PQNgimGtuwHY8nDskvJ5vvs//YnzRLs= +github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= +github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= +github.com/multiformats/go-multiaddr v0.0.2 h1:RBysRCv5rv3FWlhKWKoXv8tnsCUpEpIZpCmqAGZos2s= +github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= +github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= +github.com/multiformats/go-multihash v0.0.1 h1:HHwN1K12I+XllBCrqKnhX949Orn4oawPkegHMu2vDqQ= +github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= 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/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= @@ -39,21 +52,16 @@ github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a h1:/eS3yfGjQ 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= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b h1:+/WWzjwW6gidDJnMKWLKLX1gxn7irUTF1fLpQovfQ5M= golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo= -golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/peer.go b/peer.go deleted file mode 100644 index bd1fbd3..0000000 --- a/peer.go +++ /dev/null @@ -1,181 +0,0 @@ -// Package peer implements an object used to represent peers in the ipfs network. -package peer - -import ( - "encoding/hex" - "errors" - "fmt" - - ic "github.com/libp2p/go-libp2p-crypto" - b58 "github.com/mr-tron/base58/base58" - mh "github.com/multiformats/go-multihash" -) - -var ( - // ErrEmptyPeerID is an error for empty peer ID. - ErrEmptyPeerID = errors.New("empty peer ID") - // ErrNoPublicKey is an error for peer IDs that don't embed public keys - ErrNoPublicKey = errors.New("public key is not embedded in peer ID") -) - -// AdvancedEnableInlining enables automatically inlining keys shorter than -// 42 bytes into the peer ID (using the "identity" multihash function). -// -// WARNING: This flag will likely be set to false in the future and eventually -// be removed in favor of using a hash function specified by the key itself. -// See: https://github.com/libp2p/specs/issues/138 -// -// DO NOT change this flag unless you know what you're doing. -// -// This currently defaults to true for backwards compatibility but will likely -// be set to false by default when an upgrade path is determined. -var AdvancedEnableInlining = true - -const maxInlineKeyLength = 42 - -// ID is a libp2p peer identity. -type ID string - -// Pretty returns a b58-encoded string of the ID -func (id ID) Pretty() string { - return IDB58Encode(id) -} - -// Loggable returns a pretty peerID string in loggable JSON format -func (id ID) Loggable() map[string]interface{} { - return map[string]interface{}{ - "peerID": id.Pretty(), - } -} - -func (id ID) String() string { - return id.Pretty() -} - -// String prints out the peer. -// -// TODO(brian): ensure correctness at ID generation and -// enforce this by only exposing functions that generate -// IDs safely. Then any peer.ID type found in the -// codebase is known to be correct. -func (id ID) ShortString() string { - pid := id.Pretty() - if len(pid) <= 10 { - return fmt.Sprintf("", pid) - } - return fmt.Sprintf("", pid[:2], pid[len(pid)-6:]) -} - -// MatchesPrivateKey tests whether this ID was derived from sk -func (id ID) MatchesPrivateKey(sk ic.PrivKey) bool { - return id.MatchesPublicKey(sk.GetPublic()) -} - -// MatchesPublicKey tests whether this ID was derived from pk -func (id ID) MatchesPublicKey(pk ic.PubKey) bool { - oid, err := IDFromPublicKey(pk) - if err != nil { - return false - } - return oid == id -} - -// ExtractPublicKey attempts to extract the public key from an ID -// -// This method returns ErrNoPublicKey if the peer ID looks valid but it can't extract -// the public key. -func (id ID) ExtractPublicKey() (ic.PubKey, error) { - decoded, err := mh.Decode([]byte(id)) - if err != nil { - return nil, err - } - if decoded.Code != mh.ID { - return nil, ErrNoPublicKey - } - pk, err := ic.UnmarshalPublicKey(decoded.Digest) - if err != nil { - return nil, err - } - return pk, nil -} - -// Validate check if ID is empty or not -func (id ID) Validate() error { - if id == ID("") { - return ErrEmptyPeerID - } - - return nil -} - -// IDFromString cast a string to ID type, and validate -// the id to make sure it is a multihash. -func IDFromString(s string) (ID, error) { - if _, err := mh.Cast([]byte(s)); err != nil { - return ID(""), err - } - return ID(s), nil -} - -// IDFromBytes cast a string to ID type, and validate -// the id to make sure it is a multihash. -func IDFromBytes(b []byte) (ID, error) { - if _, err := mh.Cast(b); err != nil { - return ID(""), err - } - return ID(b), nil -} - -// IDB58Decode returns a b58-decoded Peer -func IDB58Decode(s string) (ID, error) { - m, err := mh.FromB58String(s) - if err != nil { - return "", err - } - return ID(m), err -} - -// IDB58Encode returns b58-encoded string -func IDB58Encode(id ID) string { - return b58.Encode([]byte(id)) -} - -// IDHexDecode returns a hex-decoded Peer -func IDHexDecode(s string) (ID, error) { - m, err := mh.FromHexString(s) - if err != nil { - return "", err - } - return ID(m), err -} - -// IDHexEncode returns hex-encoded string -func IDHexEncode(id ID) string { - return hex.EncodeToString([]byte(id)) -} - -// IDFromPublicKey returns the Peer ID corresponding to pk -func IDFromPublicKey(pk ic.PubKey) (ID, error) { - b, err := pk.Bytes() - if err != nil { - return "", err - } - var alg uint64 = mh.SHA2_256 - if AdvancedEnableInlining && len(b) <= maxInlineKeyLength { - alg = mh.ID - } - hash, _ := mh.Sum(b, alg, -1) - return ID(hash), nil -} - -// IDFromPrivateKey returns the Peer ID corresponding to sk -func IDFromPrivateKey(sk ic.PrivKey) (ID, error) { - return IDFromPublicKey(sk.GetPublic()) -} - -// IDSlice for sorting peers -type IDSlice []ID - -func (es IDSlice) Len() int { return len(es) } -func (es IDSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] } -func (es IDSlice) Less(i, j int) bool { return string(es[i]) < string(es[j]) } diff --git a/peer_serde.go b/peer_serde.go deleted file mode 100644 index 7f1b3e6..0000000 --- a/peer_serde.go +++ /dev/null @@ -1,75 +0,0 @@ -// This file contains Protobuf and JSON serialization/deserialization methods for peer IDs. -package peer - -import ( - "encoding" - "encoding/json" -) - -// Interface assertions commented out to avoid introducing hard dependencies to protobuf. -// var _ proto.Marshaler = (*ID)(nil) -// var _ proto.Unmarshaler = (*ID)(nil) -var _ json.Marshaler = (*ID)(nil) -var _ json.Unmarshaler = (*ID)(nil) - -var _ encoding.BinaryMarshaler = (*ID)(nil) -var _ encoding.BinaryUnmarshaler = (*ID)(nil) -var _ encoding.TextMarshaler = (*ID)(nil) -var _ encoding.TextUnmarshaler = (*ID)(nil) - -func (id ID) Marshal() ([]byte, error) { - return []byte(id), nil -} - -// BinaryMarshal returns the byte representation of the peer ID. -func (id ID) MarshalBinary() ([]byte, error) { - return id.Marshal() -} - -func (id ID) MarshalTo(data []byte) (n int, err error) { - return copy(data, []byte(id)), nil -} - -func (id *ID) Unmarshal(data []byte) (err error) { - *id, err = IDFromBytes(data) - return err -} - -// BinaryUnmarshal sets the ID from its binary representation. -func (id *ID) UnmarshalBinary(data []byte) error { - return id.Unmarshal(data) -} - -// Implements Gogo's proto.Sizer, but we omit the compile-time assertion to avoid introducing a hard -// dependency on gogo. -func (id ID) Size() int { - return len([]byte(id)) -} - -func (id ID) MarshalJSON() ([]byte, error) { - return json.Marshal(IDB58Encode(id)) -} - -func (id *ID) UnmarshalJSON(data []byte) (err error) { - var v string - if err = json.Unmarshal(data, &v); err != nil { - return err - } - *id, err = IDB58Decode(v) - return err -} - -// TextMarshal returns the text encoding of the ID. -func (id ID) MarshalText() ([]byte, error) { - return []byte(IDB58Encode(id)), nil -} - -// TextUnmarshal restores the ID from its text encoding. -func (id *ID) UnmarshalText(data []byte) error { - pid, err := IDB58Decode(string(data)) - if err != nil { - return err - } - *id = pid - return nil -} diff --git a/peer_serde_test.go b/peer_serde_test.go deleted file mode 100644 index 5e66f0b..0000000 --- a/peer_serde_test.go +++ /dev/null @@ -1,83 +0,0 @@ -package peer_test - -import ( - "testing" - - "github.com/libp2p/go-libp2p-peer" - "github.com/libp2p/go-libp2p-peer/test" -) - -func TestPeerSerdePB(t *testing.T) { - id, err := testutil.RandPeerID() - if err != nil { - t.Fatal(err) - } - b, err := id.Marshal() - if err != nil { - t.Fatal(err) - } - - var id2 peer.ID - if err = id2.Unmarshal(b); err != nil { - t.Fatal(err) - } - if id != id2 { - t.Error("expected equal ids in circular serde test") - } -} - -func TestPeerSerdeJSON(t *testing.T) { - id, err := testutil.RandPeerID() - if err != nil { - t.Fatal(err) - } - b, err := id.MarshalJSON() - if err != nil { - t.Fatal(err) - } - var id2 peer.ID - if err = id2.UnmarshalJSON(b); err != nil { - t.Fatal(err) - } - if id != id2 { - t.Error("expected equal ids in circular serde test") - } -} - -func TestBinaryMarshaler(t *testing.T) { - id, err := testutil.RandPeerID() - if err != nil { - t.Fatal(err) - } - b, err := id.MarshalBinary() - if err != nil { - t.Fatal(err) - } - - var id2 peer.ID - if err = id2.UnmarshalBinary(b); err != nil { - t.Fatal(err) - } - if id != id2 { - t.Error("expected equal ids in circular serde test") - } -} - -func TestTextMarshaler(t *testing.T) { - id, err := testutil.RandPeerID() - if err != nil { - t.Fatal(err) - } - b, err := id.MarshalText() - if err != nil { - t.Fatal(err) - } - - var id2 peer.ID - if err = id2.UnmarshalText(b); err != nil { - t.Fatal(err) - } - if id != id2 { - t.Error("expected equal ids in circular serde test") - } -} diff --git a/peer_test.go b/peer_test.go deleted file mode 100644 index 2f0cb91..0000000 --- a/peer_test.go +++ /dev/null @@ -1,242 +0,0 @@ -package peer_test - -import ( - "crypto/rand" - "encoding/base64" - "fmt" - "strings" - "testing" - - ic "github.com/libp2p/go-libp2p-crypto" - . "github.com/libp2p/go-libp2p-peer" - tu "github.com/libp2p/go-libp2p-peer/test" - mh "github.com/multiformats/go-multihash" - - b58 "github.com/mr-tron/base58/base58" -) - -var gen1 keyset // generated -var gen2 keyset // generated -var man keyset // manual - -func hash(b []byte) []byte { - h, _ := mh.Sum(b, mh.SHA2_256, -1) - return []byte(h) -} - -func init() { - if err := gen1.generate(); err != nil { - panic(err) - } - if err := gen2.generate(); err != nil { - panic(err) - } - - skManBytes = strings.Replace(skManBytes, "\n", "", -1) - if err := man.load(hpkpMan, skManBytes); err != nil { - panic(err) - } -} - -type keyset struct { - sk ic.PrivKey - pk ic.PubKey - hpk string - hpkp string -} - -func (ks *keyset) generate() error { - var err error - ks.sk, ks.pk, err = tu.RandTestKeyPair(512) - if err != nil { - return err - } - - bpk, err := ks.pk.Bytes() - if err != nil { - return err - } - - ks.hpk = string(hash(bpk)) - ks.hpkp = b58.Encode([]byte(ks.hpk)) - return nil -} - -func (ks *keyset) load(hpkp, skBytesStr string) error { - skBytes, err := base64.StdEncoding.DecodeString(skBytesStr) - if err != nil { - return err - } - - ks.sk, err = ic.UnmarshalPrivateKey(skBytes) - if err != nil { - return err - } - - ks.pk = ks.sk.GetPublic() - bpk, err := ks.pk.Bytes() - if err != nil { - return err - } - - ks.hpk = string(hash(bpk)) - ks.hpkp = b58.Encode([]byte(ks.hpk)) - if ks.hpkp != hpkp { - return fmt.Errorf("hpkp doesn't match key. %s", hpkp) - } - return nil -} - -func TestIDMatchesPublicKey(t *testing.T) { - - test := func(ks keyset) { - p1, err := IDB58Decode(ks.hpkp) - if err != nil { - t.Fatal(err) - } - - if ks.hpk != string(p1) { - t.Error("p1 and hpk differ") - } - - if !p1.MatchesPublicKey(ks.pk) { - t.Fatal("p1 does not match pk") - } - - p2, err := IDFromPublicKey(ks.pk) - if err != nil { - t.Fatal(err) - } - - if p1 != p2 { - t.Error("p1 and p2 differ", p1.Pretty(), p2.Pretty()) - } - - if p2.Pretty() != ks.hpkp { - t.Error("hpkp and p2.Pretty differ", ks.hpkp, p2.Pretty()) - } - } - - test(gen1) - test(gen2) - test(man) -} - -func TestIDMatchesPrivateKey(t *testing.T) { - - test := func(ks keyset) { - p1, err := IDB58Decode(ks.hpkp) - if err != nil { - t.Fatal(err) - } - - if ks.hpk != string(p1) { - t.Error("p1 and hpk differ") - } - - if !p1.MatchesPrivateKey(ks.sk) { - t.Fatal("p1 does not match sk") - } - - p2, err := IDFromPrivateKey(ks.sk) - if err != nil { - t.Fatal(err) - } - - if p1 != p2 { - t.Error("p1 and p2 differ", p1.Pretty(), p2.Pretty()) - } - } - - test(gen1) - test(gen2) - test(man) -} - -func TestPublicKeyExtraction(t *testing.T) { - t.Skip("disabled until libp2p/go-libp2p-crypto#51 is fixed") - // Happy path - _, originalPub, err := ic.GenerateEd25519Key(rand.Reader) - if err != nil { - t.Fatal(err) - } - - id, err := IDFromPublicKey(originalPub) - if err != nil { - t.Fatal(err) - } - - extractedPub, err := id.ExtractPublicKey() - if err != nil { - t.Fatal(err) - } - if extractedPub == nil { - t.Fatal("failed to extract public key") - } - if !originalPub.Equals(extractedPub) { - t.Fatal("extracted public key doesn't match") - } - - // Test invalid multihash (invariant of the type of public key) - pk, err := ID("").ExtractPublicKey() - if err == nil { - t.Fatal("expected an error") - } - if pk != nil { - t.Fatal("expected a nil public key") - } - - // Shouldn't work for, e.g. RSA keys (too large) - - _, rsaPub, err := ic.GenerateKeyPair(ic.RSA, 2048) - if err != nil { - t.Fatal(err) - } - rsaId, err := IDFromPublicKey(rsaPub) - if err != nil { - t.Fatal(err) - } - extractedRsaPub, err := rsaId.ExtractPublicKey() - if err != ErrNoPublicKey { - t.Fatal(err) - } - if extractedRsaPub != nil { - t.Fatal("expected to fail to extract public key from rsa ID") - } -} - -func TestValidate(t *testing.T) { - // Empty peer ID invalidates - err := ID("").Validate() - if err == nil { - t.Error("expected error") - } else if err != ErrEmptyPeerID { - t.Error("expected error message: " + ErrEmptyPeerID.Error()) - } - - // Non-empty peer ID validates - p, err := tu.RandPeerID() - if err != nil { - t.Fatal(err) - } - - err = p.Validate() - if err != nil { - t.Error("expected nil, but found " + err.Error()) - } -} - -var hpkpMan = `QmRK3JgmVEGiewxWbhpXLJyjWuGuLeSTMTndA1coMHEy5o` -var skManBytes = ` -CAAS4AQwggJcAgEAAoGBAL7w+Wc4VhZhCdM/+Hccg5Nrf4q9NXWwJylbSrXz/unFS24wyk6pEk0zi3W -7li+vSNVO+NtJQw9qGNAMtQKjVTP+3Vt/jfQRnQM3s6awojtjueEWuLYVt62z7mofOhCtj+VwIdZNBo -/EkLZ0ETfcvN5LVtLYa8JkXybnOPsLvK+PAgMBAAECgYBdk09HDM7zzL657uHfzfOVrdslrTCj6p5mo -DzvCxLkkjIzYGnlPuqfNyGjozkpSWgSUc+X+EGLLl3WqEOVdWJtbM61fewEHlRTM5JzScvwrJ39t7o6 -CCAjKA0cBWBd6UWgbN/t53RoWvh9HrA2AW5YrT0ZiAgKe9y7EMUaENVJ8QJBAPhpdmb4ZL4Fkm4OKia -NEcjzn6mGTlZtef7K/0oRC9+2JkQnCuf6HBpaRhJoCJYg7DW8ZY+AV6xClKrgjBOfERMCQQDExhnzu2 -dsQ9k8QChBlpHO0TRbZBiQfC70oU31kM1AeLseZRmrxv9Yxzdl8D693NNWS2JbKOXl0kMHHcuGQLMVA -kBZ7WvkmPV3aPL6jnwp2pXepntdVnaTiSxJ1dkXShZ/VSSDNZMYKY306EtHrIu3NZHtXhdyHKcggDXr -qkBrdgErAkAlpGPojUwemOggr4FD8sLX1ot2hDJyyV7OK2FXfajWEYJyMRL1Gm9Uk1+Un53RAkJneqp -JGAzKpyttXBTIDO51AkEA98KTiROMnnU8Y6Mgcvr68/SMIsvCYMt9/mtwSBGgl80VaTQ5Hpaktl6Xbh -VUt5Wv0tRxlXZiViCGCD1EtrrwTw== -` diff --git a/peerset/deprecated.go b/peerset/deprecated.go new file mode 100644 index 0000000..9a083c5 --- /dev/null +++ b/peerset/deprecated.go @@ -0,0 +1,17 @@ +// Deprecated: use github.com/libp2p/go-libp2p-core/peer/set instead. +package peerset + +import core "github.com/libp2p/go-libp2p-core/peer" + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer.Set instead. +type PeerSet = core.Set + +// Deprecated: use github.com/libp2p/go-libp2p-core/set.NewSet instead. +func New() *PeerSet { + return core.NewSet() +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer/set.NewLimitedSet instead. +func NewLimited(size int) *PeerSet { + return core.NewLimitedSet(size) +} diff --git a/peerset/peerset.go b/peerset/peerset.go deleted file mode 100644 index 0cc87dd..0000000 --- a/peerset/peerset.go +++ /dev/null @@ -1,72 +0,0 @@ -package peerset - -import ( - "sync" - - peer "github.com/libp2p/go-libp2p-peer" -) - -// PeerSet is a threadsafe set of peers -type PeerSet struct { - ps map[peer.ID]struct{} - lk sync.RWMutex - size int -} - -func New() *PeerSet { - ps := new(PeerSet) - ps.ps = make(map[peer.ID]struct{}) - ps.size = -1 - return ps -} - -func NewLimited(size int) *PeerSet { - ps := new(PeerSet) - ps.ps = make(map[peer.ID]struct{}) - ps.size = size - return ps -} - -func (ps *PeerSet) Add(p peer.ID) { - ps.lk.Lock() - ps.ps[p] = struct{}{} - ps.lk.Unlock() -} - -func (ps *PeerSet) Contains(p peer.ID) bool { - ps.lk.RLock() - _, ok := ps.ps[p] - ps.lk.RUnlock() - return ok -} - -func (ps *PeerSet) Size() int { - ps.lk.RLock() - defer ps.lk.RUnlock() - return len(ps.ps) -} - -// TryAdd Attempts to add the given peer into the set. -// This operation can fail for one of two reasons: -// 1) The given peer is already in the set -// 2) The number of peers in the set is equal to size -func (ps *PeerSet) TryAdd(p peer.ID) bool { - var success bool - ps.lk.Lock() - if _, ok := ps.ps[p]; !ok && (len(ps.ps) < ps.size || ps.size == -1) { - success = true - ps.ps[p] = struct{}{} - } - ps.lk.Unlock() - return success -} - -func (ps *PeerSet) Peers() []peer.ID { - ps.lk.Lock() - out := make([]peer.ID, 0, len(ps.ps)) - for p, _ := range ps.ps { - out = append(out, p) - } - ps.lk.Unlock() - return out -} diff --git a/test/deprecated.go b/test/deprecated.go new file mode 100644 index 0000000..5fbfd43 --- /dev/null +++ b/test/deprecated.go @@ -0,0 +1,24 @@ +package testutil + +import ( + ci "github.com/libp2p/go-libp2p-core/crypto" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p-core/test" +) + +// Deprecated: use github.com/libp2p/go-libp2p-core/peer/test.RandPeerID instead. +func RandPeerID() (peer.ID, error) { + return test.RandPeerID() +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/crypto/test.RandTestKeyPair instead. +// Supply RSA as a key type to get an equivalent result. +func RandTestKeyPair(bits int) (ci.PrivKey, ci.PubKey, error) { + return test.RandTestKeyPair(ci.RSA, bits) +} + +// Deprecated: use github.com/libp2p/go-libp2p-core/crypto/test.SeededTestKeyPair instead. +// Supply RSA as a key type, with 512 bits, to get an equivalent result. +func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) { + return test.SeededTestKeyPair(ci.RSA, 512, seed) +} diff --git a/test/utils.go b/test/utils.go deleted file mode 100644 index fd23d1d..0000000 --- a/test/utils.go +++ /dev/null @@ -1,39 +0,0 @@ -package testutil - -import ( - "io" - "math/rand" - "sync/atomic" - "time" - - ci "github.com/libp2p/go-libp2p-crypto" - peer "github.com/libp2p/go-libp2p-peer" - mh "github.com/multiformats/go-multihash" -) - -var generatedPairs int64 = 0 - -func RandPeerID() (peer.ID, error) { - r := rand.New(rand.NewSource(time.Now().UnixNano())) - buf := make([]byte, 16) - if _, err := io.ReadFull(r, buf); err != nil { - return "", err - } - h, _ := mh.Sum(buf, mh.SHA2_256, -1) - return peer.ID(h), nil -} - -func RandTestKeyPair(bits int) (ci.PrivKey, ci.PubKey, error) { - seed := time.Now().UnixNano() - - // workaround for low time resolution - seed += atomic.AddInt64(&generatedPairs, 1) << 32 - - r := rand.New(rand.NewSource(seed)) - return ci.GenerateKeyPairWithReader(ci.RSA, bits, r) -} - -func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) { - r := rand.New(rand.NewSource(seed)) - return ci.GenerateKeyPairWithReader(ci.RSA, 512, r) -}