Commit 82f96927 authored by Ben Wilson's avatar Ben Wilson

Added context to GetTotalElements

parent a756c1f3
......@@ -12,12 +12,11 @@ import (
// CTC interacts with the OVM CTC contract
type CTC struct {
Ctx context.Context
Address common.Address
Client *ethclient.Client
}
func (ctc *CTC) GetTotalElements() (*big.Int, error) {
func (ctc *CTC) GetTotalElements(ctx context.Context) (*big.Int, error) {
contract, err := bindings.NewCanonicalTransactionChainCaller(ctc.Address, ctc.Client)
if err != nil {
......@@ -25,7 +24,7 @@ func (ctc *CTC) GetTotalElements() (*big.Int, error) {
}
totalElements, err := contract.GetTotalElements(&bind.CallOpts{
Context: ctc.Ctx,
Context: ctx,
})
if err != nil {
return nil, err
......
package main
import (
"context"
"math/big"
"net/http"
"os"
......@@ -13,6 +14,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
l1TimeoutSeconds = 5
)
func main() {
listenAddress := os.Getenv("LISTEN_ADDRESS")
if listenAddress == "" {
......@@ -64,17 +69,20 @@ func getCTCTotalElements(address string, client *ethclient.Client) {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
totalElements, err := ctc.GetTotalElements()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(l1TimeoutSeconds))
totalElements, err := ctc.GetTotalElements(ctx)
if err != nil {
ctcTotalElementsCallSuccess.Set(0)
log.Error("Error calling GetTotalElements", "error", err)
cancel()
continue
}
ctcTotalElementsCallSuccess.Set(1)
totalElementsFloat, _ := new(big.Float).SetInt(totalElements).Float64()
ctcTotalElements.WithLabelValues(
"latest").Set(totalElementsFloat)
log.Info("ctc updated", "ctcTotalElements", totalElementsFloat)
ctx.Done()
<-ticker.C
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment