From 929a512b442d9667392bb462c7ccf5912564d7d4 Mon Sep 17 00:00:00 2001 From: gagliardetto Date: Wed, 15 Nov 2023 16:24:00 +0100 Subject: [PATCH] Handle getGenesisHash --- multiepoch-getGenesisHash.go | 41 ++++++++++++++++++++++++++++++++++++ multiepoch.go | 4 +++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 multiepoch-getGenesisHash.go diff --git a/multiepoch-getGenesisHash.go b/multiepoch-getGenesisHash.go new file mode 100644 index 00000000..381c519d --- /dev/null +++ b/multiepoch-getGenesisHash.go @@ -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 +} diff --git a/multiepoch.go b/multiepoch.go index 79628ae4..c5777dfe 100644 --- a/multiepoch.go +++ b/multiepoch.go @@ -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 @@ -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,