-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathrewardservice.go
115 lines (93 loc) · 2.13 KB
/
rewardservice.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
package eth
import (
"context"
"fmt"
"sync"
"github.com/ethereum/go-ethereum/core/types"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/monitor"
)
var (
ErrRewardServiceStarted = fmt.Errorf("reward service already started")
ErrRewardServiceStopped = fmt.Errorf("reward service already stopped")
)
type RewardService struct {
client LivepeerEthClient
working bool
cancelWorker context.CancelFunc
tw timeWatcher
mu sync.Mutex
}
func NewRewardService(client LivepeerEthClient, tw timeWatcher) *RewardService {
return &RewardService{
client: client,
tw: tw,
}
}
func (s *RewardService) Start(ctx context.Context) error {
if s.working {
return ErrRewardServiceStarted
}
cancelCtx, cancel := context.WithCancel(ctx)
s.cancelWorker = cancel
roundSink := make(chan types.Log, 10)
sub := s.tw.SubscribeRounds(roundSink)
defer sub.Unsubscribe()
s.working = true
defer func() {
s.working = false
}()
for {
select {
case err := <-sub.Err():
if err != nil {
glog.Errorf("Round subscription error err=%q", err)
}
case <-roundSink:
go func() {
err := s.tryReward()
if err != nil {
glog.Errorf("Error trying to call reward for round %v err=%q", s.tw.LastInitializedRound(), err)
if monitor.Enabled {
monitor.RewardCallError(err.Error())
}
}
}()
case <-cancelCtx.Done():
glog.V(5).Infof("Reward service done")
return nil
}
}
}
func (s *RewardService) Stop() error {
if !s.working {
return ErrRewardServiceStopped
}
s.cancelWorker()
s.working = false
return nil
}
func (s *RewardService) IsWorking() bool {
return s.working
}
func (s *RewardService) tryReward() error {
s.mu.Lock()
defer s.mu.Unlock()
currentRound := s.tw.LastInitializedRound()
t, err := s.client.GetTranscoder(s.client.Account().Address)
if err != nil {
return err
}
if t.LastRewardRound.Cmp(currentRound) == -1 && t.Active {
tx, err := s.client.Reward()
if err != nil {
return err
}
if err := s.client.CheckTx(tx); err != nil {
return err
}
glog.Infof("Called reward for round %v", currentRound)
return nil
}
return nil
}