2023-07-11 14:50:31 -07:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
2023-07-14 15:17:02 -07:00
|
|
|
|
|
|
|
"github.com/ethereum-optimism/optimism/op-ufm/pkg/config"
|
2023-07-11 14:50:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Provider struct {
|
2023-07-12 09:49:37 -07:00
|
|
|
name string
|
|
|
|
config *config.ProviderConfig
|
|
|
|
signerConfig *config.SignerServiceConfig
|
|
|
|
walletConfig *config.WalletConfig
|
2023-07-12 12:20:23 -07:00
|
|
|
txPool *NetworkTransactionPool
|
2023-07-11 14:50:31 -07:00
|
|
|
|
2023-07-14 14:08:02 -07:00
|
|
|
cancelFunc context.CancelFunc
|
2023-07-11 14:50:31 -07:00
|
|
|
}
|
|
|
|
|
2023-07-12 09:49:37 -07:00
|
|
|
func New(name string, cfg *config.ProviderConfig,
|
|
|
|
signerConfig *config.SignerServiceConfig,
|
2023-07-12 12:20:23 -07:00
|
|
|
walletConfig *config.WalletConfig,
|
|
|
|
txPool *NetworkTransactionPool) *Provider {
|
2023-07-11 14:50:31 -07:00
|
|
|
p := &Provider{
|
2023-07-12 09:49:37 -07:00
|
|
|
name: name,
|
|
|
|
config: cfg,
|
|
|
|
signerConfig: signerConfig,
|
|
|
|
walletConfig: walletConfig,
|
2023-07-12 12:20:23 -07:00
|
|
|
txPool: txPool,
|
2023-07-11 14:50:31 -07:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) Start(ctx context.Context) {
|
|
|
|
providerCtx, cancelFunc := context.WithCancel(ctx)
|
|
|
|
p.cancelFunc = cancelFunc
|
2023-07-14 14:08:02 -07:00
|
|
|
|
2023-07-11 14:50:31 -07:00
|
|
|
schedule(providerCtx, time.Duration(p.config.ReadInterval), p.Heartbeat)
|
|
|
|
if !p.config.ReadOnly {
|
2023-07-13 16:09:56 -07:00
|
|
|
schedule(providerCtx, time.Duration(p.config.SendInterval), p.RoundTrip)
|
2023-07-11 14:50:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) Shutdown() {
|
|
|
|
if p.cancelFunc != nil {
|
|
|
|
p.cancelFunc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 16:09:56 -07:00
|
|
|
func (p *Provider) Name() string {
|
|
|
|
return p.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) URL() string {
|
|
|
|
return p.config.URL
|
|
|
|
}
|
|
|
|
|
2023-07-11 14:50:31 -07:00
|
|
|
func schedule(ctx context.Context, interval time.Duration, handler func(ctx context.Context)) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
timer := time.NewTimer(interval)
|
|
|
|
handler(ctx)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
case <-ctx.Done():
|
|
|
|
timer.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|