Skip to content
codermaybePublic

About

Multi-source Ethereum JSON-RPC aggregation with standards-aware field normalization, cross-provider reliability evidence, and an ethclient-shaped Go API.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

EthGuard

English · 简体中文

EthGuard is an Ethereum execution RPC client for Go. It compares answers from the RPC endpoints you configure. For a read against a moving block tag such as latest, EthGuard first establishes a common reference block. It checks the responses it receives against the method’s rules before returning a value.

With three configured sources, if two agree on a value at the same block and the third times out, a method that requires majority agreement can still succeed. If only one source answers, the required majority stays at two. With a single configured source, the client applies the method’s checks and reports the successful read as Forwarded. A Corroborated result records agreement among configured providers. Its scope is provider evidence, independent of Ethereum consensus or protocol finality.

Choose a client

Client Calls Subscriptions
ethadapter.Client for go-ethereum and bind/v2 geth types and familiar (value, error) signatures geth channels that terminate with ErrReorgDetected on a path retraction
ethguard.Client for direct access to evidence ethapi models for structured values, with CallResult alongside the value and error Next(ctx) returns atomic path changes with Retracted and Appended

CallResult exposes the evidence formed during a call, including the reference block, which sources supported the result, and source faults when available. The adapter delegates execution to a native client and converts the arguments, results, and subscription delivery. Use ethadapter.New(core) to wrap an existing client or client.Core() to access the native client behind an adapter. Both share its connections and Close lifecycle.

Topology

ethadapter.Client
  └─ ethguard.Client (public entry points)
      └─ internal/client (lifecycle and result delivery)
          ├─ internal/runtime
          │   ├─ registered reads ─▶ internal/aggregator
          │   ├─ reference establishment ─▶ internal/reference
          │   └─ writes and raw sampling ─▶ internal/pool
          └─ internal/subscription
              ├─ path selection ─▶ internal/reference
              └─ source capabilities supplied by internal/runtime

internal/pool ─▶ internal/transport ─▶ HTTP(S) / WS(S) / IPC sources

The root package exposes constructors, RPC and subscription methods, and result types. internal/client owns client lifecycle, argument handling, and result capture. internal/runtime owns the source pool, read plans, and transaction submission. It assembles the method plans from internal/methods. The aggregator executes each read in one invocation and uses internal/rpcengine for adjudication. Subscriptions track a continuous block path and can deliver logs or receipts along it. Reads and subscriptions share the path selection algorithm in internal/reference. Subscriptions use WebSocket or IPC source capabilities. See the module topology for object validation, typed results, and execution stages.

Usage

Examples follow the current source API. The required Go version is in go.mod.

Dial and call

client, err := ethadapter.Dial("wss://rpc-a.example", "wss://rpc-b.example")
if err != nil { return err }
defer client.Close()

balance, err := client.BalanceAt(ctx, common.HexToAddress("0x000000000000000000000000000000000000dead"), nil)

Use github.com/codermaybe/ethguard/ethadapter with geth's common and core/types packages. Reuse the client across calls and goroutines; each call's context sets its total waiting deadline. Runnable example.

For BalanceAt, nil requests latest. If three sources report heights 103, 102, and 101 on the same verified path, the highest block supported by two sources is 102. The client reads the balance at that block. Explicit heights, hashes, safe, and finalized keep the meaning defined for each method. See the reference contract.

Configure once

client, err := ethadapter.Open(ethguard.Config{
    Endpoints: []string{"wss://rpc-a.example", "wss://rpc-b.example"},
    ChainID:   1,
    Logger:    slog.Default(),
})
if err != nil { return err }
defer client.Close()

Or load YAML, then attach runtime observers:

cfg, err := ethguard.LoadConfig("config.yaml")
if err != nil { return err }
cfg.Logger = slog.Default()
client, err := ethadapter.Open(cfg)
if err != nil { return err }
defer client.Close()

Use Endpoints for URL or IPC strings, or Upstreams for named sources. Start with the sample YAML. Set Logger and Recorder on the config in Go, and use ethadapter.WithAudit for an adapter audit callback. See configuration and observation.

Subscribe

For multi-source subscriptions, configure at least two WebSocket or IPC sources, as above.

headers := make(chan *types.Header, 16)
sub, err := client.SubscribeNewHead(ctx, headers)
if err != nil { return err }
defer sub.Unsubscribe()

for {
    select {
    case <-ctx.Done():
        return ctx.Err()
    case head := <-headers:
        fmt.Println(head.Number, head.Hash())
    case err := <-sub.Err():
        if errors.Is(err, ethguard.ErrReorgDetected) {
            // Roll back/rescan affected application state before resubscribing.
        }
        return err
    }
}

Subscribe* creates a handle; use Status() to check progress. Its context bounds setup only; Unsubscribe or client Close stops an established stream. Caller-owned channels stay open. Adapter subscriptions terminate with ErrReorgDetected on a path retraction. Indexers that need continuous atomic reorg events can use client.Core().Subscribe* and Next(ctx).

SubscribeTransactionReceipts(ctx, nil, ch) selects all live receipts and can carry substantial data. Subscriptions do not replay history; backfill through historical RPC. See the subscription example and subscription contract.

Check status

status := client.Status()
fmt.Printf("sources: %+v\n", status.Sources)
fmt.Printf("subscriptions: %+v\n", status.Subscriptions)

Status() reads memory only. It reports source health, subscription progress, pending delivery, and separate core/adapter terminal causes. An empty log filter result can still have a progressing subscription. See diagnostics.

Inspect a call

callCtx, capture := ethguard.WithResult(ctx)
balance, err := client.BalanceAt(callCtx, common.HexToAddress("0x000000000000000000000000000000000000dead"), nil)
result, published, captureErr := capture.Result()

err reports the business outcome, while captureErr reports a capture failure. Use a fresh capture for each call. To include raw source responses up to a byte budget, call WithSourceEvidence on the context before invoking the method. See the evidence example.

Native callers receive the result directly:

core, err := ethguard.Dial("wss://rpc-a.example", "wss://rpc-b.example")
if err != nil { return err }
defer core.Close()

balance, result, err := core.BalanceAt(ctx, ethapi.Address("0x000000000000000000000000000000000000dead"), nil)

Use ethguard.Open(cfg) to open a native client from the same configuration.

Further reading

About

Multi-source Ethereum JSON-RPC aggregation with standards-aware field normalization, cross-provider reliability evidence, and an ethclient-shaped Go API.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages