RequestShardList fetches shard list from remote peer
(remotePeer peer.ID)
| 235 | |
| 236 | // RequestShardList fetches shard list from remote peer |
| 237 | func (n *NodeP2P) RequestShardList(remotePeer peer.ID) ([]*protocol.ContractShards, error) { |
| 238 | remoteShardList := make([]*protocol.ContractShards, 0) |
| 239 | ctx, cancel := context.WithTimeout(context.Background(), protocol.NewStreamTimeout) |
| 240 | defer cancel() |
| 241 | |
| 242 | s, err := n.Host().NewStream(ctx, remotePeer, protocol.RequestShardList) |
| 243 | if err != nil { |
| 244 | return remoteShardList, err |
| 245 | } |
| 246 | defer func() { |
| 247 | if s != nil { |
| 248 | s.Close() |
| 249 | } |
| 250 | }() |
| 251 | |
| 252 | code, err := protocol.SendRPC(s, make([]byte, 0), &remoteShardList) |
| 253 | if err != nil { |
| 254 | return remoteShardList, err |
| 255 | } |
| 256 | if code != 0 { |
| 257 | return remoteShardList, fmt.Errorf("request shard list fail, code %d", code) |
| 258 | } |
| 259 | |
| 260 | return remoteShardList, nil |
| 261 | } |
| 262 | |
| 263 | func (n *NodeP2P) Host() host.Host { |
| 264 | return n.host |