-
Notifications
You must be signed in to change notification settings - Fork 340
/
pricing.go
158 lines (131 loc) · 4.68 KB
/
pricing.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pricing
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/protobuf"
"github.com/ethersphere/bee/pkg/pricing/pb"
"github.com/ethersphere/bee/pkg/swarm"
)
// loggerName is the tree path name of the logger for this package.
const loggerName = "pricing"
const (
protocolName = "pricing"
protocolVersion = "1.0.0"
streamName = "pricing"
)
var (
// ErrThresholdTooLow says that the proposed payment threshold is too low for even a single reserve.
ErrThresholdTooLow = errors.New("threshold too low")
)
var _ Interface = (*Service)(nil)
// Interface is the main interface of the pricing protocol
type Interface interface {
AnnouncePaymentThreshold(ctx context.Context, peer swarm.Address, paymentThreshold *big.Int) error
}
// PaymentThresholdObserver is used for being notified of payment threshold updates
type PaymentThresholdObserver interface {
NotifyPaymentThreshold(peer swarm.Address, paymentThreshold *big.Int) error
}
type Service struct {
streamer p2p.Streamer
logger log.Logger
paymentThreshold *big.Int
lightPaymentThreshold *big.Int
minPaymentThreshold *big.Int
paymentThresholdObserver PaymentThresholdObserver
}
func New(streamer p2p.Streamer, logger log.Logger, paymentThreshold, lightPaymentThreshold, minThreshold *big.Int) *Service {
return &Service{
streamer: streamer,
logger: logger.WithName(loggerName).Register(),
paymentThreshold: paymentThreshold,
lightPaymentThreshold: lightPaymentThreshold,
minPaymentThreshold: minThreshold,
}
}
func (s *Service) Protocol() p2p.ProtocolSpec {
return p2p.ProtocolSpec{
Name: protocolName,
Version: protocolVersion,
StreamSpecs: []p2p.StreamSpec{
{
Name: streamName,
Handler: s.handler,
},
},
ConnectIn: s.init,
ConnectOut: s.init,
}
}
func (s *Service) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (err error) {
loggerV1 := s.logger.V(1).Register()
r := protobuf.NewReader(stream)
defer func() {
if err != nil {
_ = stream.Reset()
} else {
_ = stream.FullClose()
}
}()
var req pb.AnnouncePaymentThreshold
if err := r.ReadMsgWithContext(ctx, &req); err != nil {
s.logger.Debug("could not receive payment threshold and/or price table announcement from peer", "peer_address", p.Address)
return fmt.Errorf("read request from peer %v: %w", p.Address, err)
}
paymentThreshold := big.NewInt(0).SetBytes(req.PaymentThreshold)
loggerV1.Debug("received payment threshold announcement from peer", "peer_address", p.Address, "payment_threshold", paymentThreshold)
if paymentThreshold.Cmp(s.minPaymentThreshold) < 0 {
loggerV1.Debug("payment threshold from peer too small, need at least min payment threshold", "peer_address", p.Address, "payment_threshold", paymentThreshold, "min_payment_threshold", s.minPaymentThreshold)
return p2p.NewDisconnectError(ErrThresholdTooLow)
}
if paymentThreshold.Cmp(big.NewInt(0)) == 0 {
return err
}
return s.paymentThresholdObserver.NotifyPaymentThreshold(p.Address, paymentThreshold)
}
func (s *Service) init(ctx context.Context, p p2p.Peer) error {
threshold := s.paymentThreshold
if !p.FullNode {
threshold = s.lightPaymentThreshold
}
err := s.AnnouncePaymentThreshold(ctx, p.Address, threshold)
if err != nil {
s.logger.Warning("could not send payment threshold announcement to peer", "peer_address", p.Address)
}
return err
}
// AnnouncePaymentThreshold announces the payment threshold to per
func (s *Service) AnnouncePaymentThreshold(ctx context.Context, peer swarm.Address, paymentThreshold *big.Int) error {
loggerV1 := s.logger.V(1).Register()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
stream, err := s.streamer.NewStream(ctx, peer, nil, protocolName, protocolVersion, streamName)
if err != nil {
return err
}
defer func() {
if err != nil {
_ = stream.Reset()
} else {
stream.FullClose()
}
}()
loggerV1.Debug("sending payment threshold announcement to peer", "peer_address", peer, "payment_threshold", paymentThreshold)
w := protobuf.NewWriter(stream)
err = w.WriteMsgWithContext(ctx, &pb.AnnouncePaymentThreshold{
PaymentThreshold: paymentThreshold.Bytes(),
})
return err
}
// SetPaymentThresholdObserver sets the PaymentThresholdObserver to be used when receiving a new payment threshold
func (s *Service) SetPaymentThresholdObserver(observer PaymentThresholdObserver) {
s.paymentThresholdObserver = observer
}