|
| 1 | +// Copyright © 2020 AMIS Technologies |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "math/rand" |
| 20 | + |
| 21 | + "github.com/getamis/sirius/log" |
| 22 | + ggio "github.com/gogo/protobuf/io" |
| 23 | + "github.com/golang/protobuf/proto" |
| 24 | + "github.com/libp2p/go-libp2p" |
| 25 | + "github.com/libp2p/go-libp2p-core/crypto" |
| 26 | + "github.com/libp2p/go-libp2p-core/helpers" |
| 27 | + "github.com/libp2p/go-libp2p-core/host" |
| 28 | + "github.com/libp2p/go-libp2p-core/peer" |
| 29 | + "github.com/multiformats/go-multiaddr" |
| 30 | +) |
| 31 | + |
| 32 | +// makeBasicHost creates a LibP2P host. |
| 33 | +func makeBasicHost(port int64) (host.Host, error) { |
| 34 | + sourceMultiAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + priv, err := generateIdentity(port) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + opts := []libp2p.Option{ |
| 45 | + libp2p.ListenAddrs(sourceMultiAddr), |
| 46 | + libp2p.Identity(priv), |
| 47 | + } |
| 48 | + |
| 49 | + basicHost, err := libp2p.New(context.Background(), opts...) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + return basicHost, nil |
| 55 | +} |
| 56 | + |
| 57 | +// getPeerAddr gets peer full address from port. |
| 58 | +func getPeerAddr(port int64) (string, error) { |
| 59 | + priv, err := generateIdentity(port) |
| 60 | + if err != nil { |
| 61 | + return "", err |
| 62 | + } |
| 63 | + |
| 64 | + pid, err := peer.IDFromPrivateKey(priv) |
| 65 | + if err != nil { |
| 66 | + return "", err |
| 67 | + } |
| 68 | + return fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", port, pid), nil |
| 69 | +} |
| 70 | + |
| 71 | +// getPeerIDFromPort gets peer ID from port. |
| 72 | +func getPeerIDFromPort(port int64) string { |
| 73 | + // For convenience, we set peer ID as "id-" + port |
| 74 | + return fmt.Sprintf("id-%d", port) |
| 75 | +} |
| 76 | + |
| 77 | +// generateIdentity generates a fixed key pair by using port as random source. |
| 78 | +func generateIdentity(port int64) (crypto.PrivKey, error) { |
| 79 | + // Use the port as the randomness source in this example. |
| 80 | + r := rand.New(rand.NewSource(port)) |
| 81 | + |
| 82 | + // Generate a key pair for this host. |
| 83 | + priv, _, err := crypto.GenerateKeyPairWithReader(crypto.ECDSA, 2048, r) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + return priv, nil |
| 88 | +} |
| 89 | + |
| 90 | +// send sends the proto message to specified peer. |
| 91 | +func send(ctx context.Context, host host.Host, target string, data proto.Message) error { |
| 92 | + // Turn the destination into a multiaddr. |
| 93 | + maddr, err := multiaddr.NewMultiaddr(target) |
| 94 | + if err != nil { |
| 95 | + log.Warn("Cannot parse the target address", "target", target, "err", err) |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + // Extract the peer ID from the multiaddr. |
| 100 | + info, err := peer.AddrInfoFromP2pAddr(maddr) |
| 101 | + if err != nil { |
| 102 | + log.Warn("Cannot parse addr", "addr", maddr, "err", err) |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + s, err := host.NewStream(ctx, info.ID, dkgProtocol) |
| 107 | + if err != nil { |
| 108 | + log.Warn("Cannot create a new stream", "from", host.ID(), "to", target, "err", err) |
| 109 | + return err |
| 110 | + } |
| 111 | + writer := ggio.NewFullWriter(s) |
| 112 | + err = writer.WriteMsg(data) |
| 113 | + if err != nil { |
| 114 | + log.Warn("Cannot write message to IO", "err", err) |
| 115 | + return err |
| 116 | + } |
| 117 | + err = helpers.FullClose(s) |
| 118 | + if err != nil { |
| 119 | + log.Warn("Cannot close the stream", "err", err) |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + log.Info("Sent message", "peer", target) |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +// connect connects the host to the specified peer. |
| 128 | +func connect(ctx context.Context, host host.Host, target string) error { |
| 129 | + // Turn the destination into a multiaddr. |
| 130 | + maddr, err := multiaddr.NewMultiaddr(target) |
| 131 | + if err != nil { |
| 132 | + log.Warn("Cannot parse the target address", "target", target, "err", err) |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + // Extract the peer ID from the multiaddr. |
| 137 | + info, err := peer.AddrInfoFromP2pAddr(maddr) |
| 138 | + if err != nil { |
| 139 | + log.Error("Cannot parse addr", "addr", maddr, "err", err) |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + // Connect the host to the peer. |
| 144 | + err = host.Connect(ctx, *info) |
| 145 | + if err != nil { |
| 146 | + log.Warn("Failed to connect to peer", "err", err) |
| 147 | + return err |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
0 commit comments