Skip to content

Commit

Permalink
Handle getGenesisHash
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Nov 15, 2023
1 parent 6cf9caf commit 929a512
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
41 changes: 41 additions & 0 deletions multiepoch-getGenesisHash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"

"github.com/sourcegraph/jsonrpc2"
)

func (multi *MultiEpoch) handleGetGenesisHash(ctx context.Context, conn *requestContext, req *jsonrpc2.Request) (*jsonrpc2.Error, error) {
// Epoch 0 contains the genesis config.
epochNumber := uint64(0)
epochHandler, err := multi.GetEpoch(epochNumber)
if err != nil {
// If epoch 0 is not available, then the genesis config is not available.
return &jsonrpc2.Error{
Code: CodeNotFound,
Message: fmt.Sprintf("Epoch %d is not available", epochNumber),
}, fmt.Errorf("failed to get epoch %d: %w", epochNumber, err)
}

genesis := epochHandler.GetGenesis()
if genesis == nil {
return &jsonrpc2.Error{
Code: CodeNotFound,
Message: "Genesis is not available",
}, fmt.Errorf("genesis is nil")
}

genesisHash := genesis.Hash

err = conn.ReplyRaw(
ctx,
req.ID,
genesisHash.String(),
)
if err != nil {
return nil, fmt.Errorf("failed to reply: %w", err)
}
return nil, nil
}
4 changes: 3 additions & 1 deletion multiepoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func sanitizeMethod(method string) string {

func isValidLocalMethod(method string) bool {
switch method {
case "getBlock", "getTransaction", "getSignaturesForAddress", "getBlockTime":
case "getBlock", "getTransaction", "getSignaturesForAddress", "getBlockTime", "getGenesisHash":
return true
default:
return false
Expand All @@ -419,6 +419,8 @@ func (ser *MultiEpoch) handleRequest(ctx context.Context, conn *requestContext,
return ser.handleGetSignaturesForAddress(ctx, conn, req)
case "getBlockTime":
return ser.handleGetBlockTime(ctx, conn, req)
case "getGenesisHash":
return ser.handleGetGenesisHash(ctx, conn, req)
default:
return &jsonrpc2.Error{
Code: jsonrpc2.CodeMethodNotFound,
Expand Down

0 comments on commit 929a512

Please sign in to comment.