forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requester.go
53 lines (46 loc) · 1.21 KB
/
requester.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package fraud
import (
"context"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/celestiaorg/go-libp2p-messenger/serde"
pb "github.com/celestiaorg/celestia-node/fraud/pb"
)
const (
// writeDeadline sets timeout for sending messages to the stream
writeDeadline = time.Second * 5
// readDeadline sets timeout for reading messages from the stream
readDeadline = time.Minute
)
func (f *ProofService) requestProofs(
ctx context.Context,
pid peer.ID,
proofTypes []string,
) ([]*pb.ProofResponse, error) {
msg := &pb.FraudMessageRequest{RequestedProofType: proofTypes}
stream, err := f.host.NewStream(ctx, pid, f.protocolID)
if err != nil {
return nil, err
}
if err = stream.SetWriteDeadline(time.Now().Add(writeDeadline)); err != nil {
log.Warn(err)
}
_, err = serde.Write(stream, msg)
if err != nil {
stream.Reset() //nolint:errcheck
return nil, err
}
if err = stream.CloseWrite(); err != nil {
log.Warn(err)
}
if err = stream.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
log.Warn(err)
}
resp := &pb.FraudMessageResponse{}
_, err = serde.Read(stream, resp)
if err != nil {
stream.Reset() //nolint:errcheck
return nil, err
}
return resp.Proofs, stream.Close()
}