Skip to content

Commit

Permalink
discovery: cache orchestrator stake for current round when DBOrchestr…
Browse files Browse the repository at this point in the history
…atorPoolcache is created
  • Loading branch information
Nico Vergauwen authored and Nico Vergauwen committed Nov 27, 2019
1 parent 2638ad5 commit 9b93f63
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions discovery/db_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func NewDBOrchestratorPoolCache(ctx context.Context, node *core.LivepeerNode, rm
return nil, err
}

if err := dbo.cacheOrchestratorStake(); err != nil {
return nil, err
}

if err := dbo.pollOrchestratorInfo(ctx); err != nil {
return nil, err
}
Expand Down Expand Up @@ -145,6 +149,53 @@ func (dbo *DBOrchestratorPoolCache) cacheTranscoderPool() error {
return nil
}

func (dbo *DBOrchestratorPoolCache) cacheOrchestratorStake() error {
orchs, err := dbo.store.SelectOrchs(
&common.DBOrchFilter{
CurrentRound: dbo.rm.LastInitializedRound(),
},
)
if err != nil {
return fmt.Errorf("could not retrieve orchestrators from DB: %v", err)
}

resc, errc := make(chan *common.DBOrch), make(chan error)
ctx, cancel := context.WithTimeout(context.Background(), getOrchestratorsTimeoutLoop)
defer cancel()

currentRound := dbo.rm.LastInitializedRound()

getStake := func(o *common.DBOrch) {
ep, err := dbo.lpEth.GetTranscoderEarningsPoolForRound(ethcommon.HexToAddress(o.EthereumAddr), currentRound)
if err != nil {
errc <- err
return
}
o.Stake = ep.TotalStake.String()
resc <- o
}

for _, o := range orchs {
go getStake(o)
}

for i := 0; i < len(orchs); i++ {
select {
case res := <-resc:
if err := dbo.store.UpdateOrch(res); err != nil {
glog.Error("Error updating Orchestrator in DB: ", err)
}
case err := <-errc:
glog.Errorln(err)
case <-ctx.Done():
glog.Info("Done fetching stake for orchestrators, context timeout")
break
}
}

return nil
}

func (dbo *DBOrchestratorPoolCache) pollOrchestratorInfo(ctx context.Context) error {
if err := dbo.cacheDBOrchs(); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func TestNewDBOrchestratorPoolCache_GivenListOfOrchs_CreatesPoolCacheCorrectly(t
Database: dbh,
Eth: &eth.StubClient{
Orchestrators: orchestrators,
TotalStake: big.NewInt(5000),
},
Sender: sender,
}
Expand All @@ -235,6 +236,7 @@ func TestNewDBOrchestratorPoolCache_GivenListOfOrchs_CreatesPoolCacheCorrectly(t
for _, o := range dbOrchs {
test := toOrchTest(o.EthereumAddr, o.ServiceURI, o.PricePerPixel)
assert.Contains(testOrchs, test)
assert.Equal(o.Stake, big.NewInt(5000).String())
}

urls := pool.GetURLs()
Expand Down

0 comments on commit 9b93f63

Please sign in to comment.