Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/block_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestBlockRecordMarshalling(t *testing.T) {

bytes, err := json.Marshal(b)
assert.NoError(t, err)
assert.Equal(t, `{"number":42,"hash":"0x4242424242424242424242424242424242424242424242424242424242424242","Landed":true}`, string(bytes))
assert.Equal(t, `{"number":42,"hash":"0x4242424242424242424242424242424242424242424242424242424242424242","landed":true,"flashblocks_count":0}`, string(bytes))

b2 := &blockRecord{}
err = json.Unmarshal(bytes, b2)
Expand Down
103 changes: 17 additions & 86 deletions server/l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,42 @@ package server
import (
"context"
"fmt"
"math/big"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/flashbots/chain-monitor/config"
"github.com/flashbots/chain-monitor/logutils"
"github.com/flashbots/chain-monitor/metrics"
"github.com/flashbots/chain-monitor/rpc"
"github.com/flashbots/chain-monitor/utils"
"go.opentelemetry.io/otel/attribute"
"github.com/flashbots/chain-monitor/server/wallet"
otelapi "go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
)

type L1 struct {
cfg *config.L1

rpc *rpc.RPC

chainID *big.Int
wallets map[string]ethcommon.Address
walletObserver *wallet.Observer
}

func newL1(cfg *config.L1) (*L1, error) {
l := zap.L()

l1 := &L1{
cfg: cfg,
wallets: make(map[string]ethcommon.Address, len(cfg.MonitorWalletAddresses)),
}

for name, addrStr := range cfg.MonitorWalletAddresses {
var addr ethcommon.Address
addrBytes, err := ethcommon.ParseHexOrString(addrStr)
if err != nil {
return nil, err
}
if len(addrBytes) != 20 {
return nil, fmt.Errorf(
"invalid length for the l1 wallet address (want 20, got %d)",
len(addr),
)
}
copy(addr[:], addrBytes)
l1.wallets[name] = addr
}

{ // rpc
rpc, err := rpc.New(cfg.NetworkID, cfg.Rpc, cfg.RpcFallback...)
if err != nil {
return nil, err
}
l1.rpc = rpc
}

{ // chainID
chainID, err := l1.rpc.NetworkID(context.Background())
if err != nil {
l.Error("Failed to request network id",
zap.Error(err),
zap.String("kind", "l1"),
)
return nil, err
}

l1.chainID = chainID
walletObserver, err := wallet.NewObserver(cfg.NetworkID, cfg.Rpc, cfg.RpcFallback, cfg.MonitorWalletAddresses)
if err != nil {
return nil, fmt.Errorf(
"failed to initialise wallet observer: %w",
err,
)
}

return l1, nil
return &L1{
walletObserver: walletObserver,
}, nil
}

func (l1 *L1) run(_ context.Context) {
// no-op
}

func (l1 *L1) stop() {
// no-op
if l1 == nil {
return
}

l1.walletObserver.Stop()
}

func (l1 *L1) observeWallets(ctx context.Context, o otelapi.Observer) error {
l := logutils.LoggerFromContext(ctx)

errs := make([]error, 0)

for name, addr := range l1.wallets {
_balance, err := l1.rpc.BalanceAt(ctx, addr)
if err != nil {
l.Error("Failed to request balance",
zap.Error(err),
zap.String("at", addr.String()),
zap.String("kind", "l1"),
zap.String("rpc", l1.cfg.Rpc),
)
errs = append(errs, err)
continue
}

balance, _ := _balance.Float64()

o.ObserveFloat64(metrics.WalletBalance, balance, otelapi.WithAttributes(
attribute.KeyValue{Key: "kind", Value: attribute.StringValue("l1")},
attribute.KeyValue{Key: "network_id", Value: attribute.Int64Value(l1.chainID.Int64())},
attribute.KeyValue{Key: "wallet_address", Value: attribute.StringValue(addr.String())},
attribute.KeyValue{Key: "wallet_name", Value: attribute.StringValue(name)},
))
}

return utils.FlattenErrors(errs)
return l1.walletObserver.Observe(ctx, o)
}
Loading