-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP refactor loop to event based processing
- Loading branch information
Showing
7 changed files
with
258 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package relayer | ||
|
||
type ChainProcessor struct { | ||
chain *Chain | ||
unrelayedSequencesQueue chan uint64 | ||
} | ||
|
||
const unrelayedSequencesQueueCapacity = 1000 | ||
|
||
func NewChainProcessor(chain *Chain) *ChainProcessor { | ||
return &ChainProcessor{ | ||
chain: chain, | ||
unrelayedSequencesQueue: make(chan uint64, unrelayedSequencesQueueCapacity), | ||
} | ||
} | ||
|
||
func (p *ChainProcessor) Start() { | ||
go p.unrelayedSequencesWorker() | ||
} | ||
|
||
func (p *ChainProcessor) unrelayedSequencesWorker() { | ||
for unrelayedSequence := range p.unrelayedSequencesQueue { | ||
p.processUnrelayedSequence(unrelayedSequence) | ||
} | ||
} | ||
|
||
func (p *ChainProcessor) EnqueueJob(unrelayedSequences []uint64) { | ||
for _, unrelayedSequence := range unrelayedSequences { | ||
p.unrelayedSequencesQueue <- unrelayedSequence | ||
} | ||
} | ||
|
||
func (p *ChainProcessor) processUnrelayedSequence(unrelayedSequence uint64) { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package relayer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type ChainEventBus struct { | ||
chains map[string]*ChainEventBusChain | ||
} | ||
|
||
type ChainEventBusChain struct { | ||
chain *Chain | ||
subscribers []*ChainEventBusSubscriber | ||
latestHeight int64 | ||
lastHeightQueried int64 | ||
log *zap.Logger | ||
} | ||
|
||
type ChainEventBusSubscriber struct { | ||
chainID string | ||
onNewUnrelayedSequences func([]uint64) | ||
} | ||
|
||
const ( | ||
minQueryLoopDuration = 3 * time.Second | ||
heightQueryTimeout = 5 * time.Second | ||
) | ||
|
||
func NewChainEventBus(chains []*Chain, log *zap.Logger) ChainEventBus { | ||
chainEventBusChains := make(map[string]*ChainEventBusChain) | ||
for _, chain := range chains { | ||
chainEventBusChains[chain.ChainID()] = &ChainEventBusChain{ | ||
chain: chain, | ||
subscribers: []*ChainEventBusSubscriber{}, | ||
log: log, | ||
} | ||
} | ||
return ChainEventBus{ | ||
chains: chainEventBusChains, | ||
} | ||
} | ||
|
||
func (ceb *ChainEventBus) Subscribe(srcChainID string, dstChainID string, onNewUnrelayedSequences func([]uint64)) error { | ||
if _, ok := ceb.chains[srcChainID]; !ok { | ||
return fmt.Errorf("unable to subscribe, chain id does not exist: %s", srcChainID) | ||
} | ||
ceb.chains[srcChainID].subscribers = append(ceb.chains[srcChainID].subscribers, &ChainEventBusSubscriber{ | ||
chainID: dstChainID, | ||
onNewUnrelayedSequences: onNewUnrelayedSequences, | ||
}) | ||
return nil | ||
} | ||
|
||
func (ceb *ChainEventBus) Start(ctx context.Context) { | ||
wg := sync.WaitGroup{} | ||
for _, chain := range ceb.chains { | ||
wg.Add(1) | ||
go chain.QueryLoop(ctx, &wg) | ||
} | ||
// wait for initial height from all chains | ||
wg.Wait() | ||
} | ||
|
||
func (c *ChainEventBusChain) QueryLoop(ctx context.Context, wg *sync.WaitGroup) { | ||
haveInitialHeight := false | ||
for { | ||
cycleTimeStart := time.Now() | ||
doneWithThisCycle := func() { | ||
queryDuration := time.Since(cycleTimeStart) | ||
if queryDuration < minQueryLoopDuration { | ||
time.Sleep(minQueryLoopDuration - queryDuration) | ||
} | ||
} | ||
latestHeightQueryCtx, latestHeightQueryCtxCancel := context.WithTimeout(ctx, heightQueryTimeout) | ||
latestHeight, err := c.chain.ChainProvider.QueryLatestHeight(latestHeightQueryCtx) | ||
latestHeightQueryCtxCancel() | ||
if err != nil { | ||
c.log.Warn("failed to query latest height", zap.String("chainID", c.chain.ChainID()), zap.Error(err)) | ||
doneWithThisCycle() | ||
continue | ||
} | ||
|
||
c.latestHeight = latestHeight | ||
// have non-zero height now for this chain ID, so caller can unblock | ||
if !haveInitialHeight { | ||
haveInitialHeight = true | ||
wg.Done() | ||
} | ||
|
||
// TODO add packet commitment and ack queries | ||
// limit to blocks since lastHeightQueried, except initial query should be full query | ||
// call onNewUnrelayedSequences in goroutine for each c.Subscriber | ||
|
||
c.lastHeightQueried = latestHeight | ||
|
||
doneWithThisCycle() | ||
} | ||
} | ||
|
||
func (ceb *ChainEventBus) GetLatestHeight(chainID string) (int64, error) { | ||
chain, ok := ceb.chains[chainID] | ||
if !ok { | ||
return -1, fmt.Errorf("unable to get latest height, chain id does not exist in event bus: %s", chainID) | ||
} | ||
return chain.latestHeight, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.