Skip to content

Commit

Permalink
Merge pull request #63 from DarkLord017/RefactorRPC
Browse files Browse the repository at this point in the history
Refactor rpc
  • Loading branch information
star-gazer111 authored Nov 4, 2024
2 parents 4d58747 + 17ee63f commit d97fa10
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
9 changes: 8 additions & 1 deletion consensus/rpc/consensus_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/utils"
)

// return types not mention and oarameters as well
Expand All @@ -14,6 +15,12 @@ type ConsensusRpc interface {
ChainId() (uint64, error)
}


func NewConsensusRpc(rpc string) ConsensusRpc {
return NewNimbusRpc(rpc)
if utils.IsURL(rpc) {
return NewNimbusRpc(rpc)
} else {
return NewMockRpc(rpc)
}

}
36 changes: 19 additions & 17 deletions consensus/rpc/mock_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ package rpc
import (
"encoding/json"
"fmt"
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"os"
"path/filepath"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
)

type MockRpc struct {
testdata string
}

func NewMockRpc(path string) *MockRpc {
return &MockRpc{
testdata: path,
}
}
func (m *MockRpc) GetBootstrap(block_root []byte) (*consensus_core.Bootstrap, error) {
func (m *MockRpc) GetBootstrap(block_root [32]byte) (consensus_core.Bootstrap, error) {
path := filepath.Join(m.testdata, "bootstrap.json")
res, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
return consensus_core.Bootstrap{}, fmt.Errorf("failed to read file: %w", err)
}
var bootstrap BootstrapResponse
err = json.Unmarshal(res, &bootstrap)
if err != nil {
return &consensus_core.Bootstrap{}, fmt.Errorf("bootstrap error: %w", err)
return consensus_core.Bootstrap{}, fmt.Errorf("bootstrap error: %w", err)
}
return &bootstrap.Data, nil
return bootstrap.Data, nil
}
func (m *MockRpc) GetUpdates(period uint64, count uint8) ([]consensus_core.Update, error) {
path := filepath.Join(m.testdata, "updates.json")
Expand All @@ -48,44 +50,44 @@ func (m *MockRpc) GetUpdates(period uint64, count uint8) ([]consensus_core.Updat
}
return updates, nil
}
func (m *MockRpc) GetFinalityUpdate() (*consensus_core.FinalityUpdate, error) {
func (m *MockRpc) GetFinalityUpdate() (consensus_core.FinalityUpdate, error) {
path := filepath.Join(m.testdata, "finality.json")
res, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
return consensus_core.FinalityUpdate{}, fmt.Errorf("failed to read file: %w", err)
}
var finality FinalityUpdateResponse
err = json.Unmarshal(res, &finality)
if err != nil {
return &consensus_core.FinalityUpdate{}, fmt.Errorf("finality update error: %w", err)
return consensus_core.FinalityUpdate{}, fmt.Errorf("finality update error: %w", err)
}
return &finality.Data, nil
return finality.Data, nil
}
func (m *MockRpc) GetOptimisticUpdate() (*consensus_core.OptimisticUpdate, error) {
func (m *MockRpc) GetOptimisticUpdate() (consensus_core.OptimisticUpdate, error) {
path := filepath.Join(m.testdata, "optimistic.json")
res, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
return consensus_core.OptimisticUpdate{}, fmt.Errorf("failed to read file: %w", err)
}
var optimistic OptimisticUpdateResponse
err = json.Unmarshal(res, &optimistic)
if err != nil {
return &consensus_core.OptimisticUpdate{}, fmt.Errorf("optimistic update error: %w", err)
return consensus_core.OptimisticUpdate{}, fmt.Errorf("optimistic update error: %w", err)
}
return &optimistic.Data, nil
return optimistic.Data, nil
}
func (m *MockRpc) GetBlock(slot uint64) (*consensus_core.BeaconBlock, error) {
func (m *MockRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock, error) {
path := filepath.Join(m.testdata, fmt.Sprintf("blocks/%d.json", slot))
res, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
return consensus_core.BeaconBlock{}, fmt.Errorf("failed to read file: %w", err)
}
var block BeaconBlockResponse
err = json.Unmarshal(res, &block)
if err != nil {
return nil, err
return consensus_core.BeaconBlock{}, err
}
return &block.Data.Message, nil
return block.Data.Message, nil
}
func (m *MockRpc) ChainId() (uint64, error) {
return 0, fmt.Errorf("not implemented")
Expand Down
4 changes: 3 additions & 1 deletion consensus/rpc/mock_rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package rpc

import (
"encoding/json"
"os"
Expand All @@ -7,6 +8,7 @@ import (

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
)

func TestNewMockRpc(t *testing.T) {
path := "/tmp/testdata"
mockRpc := NewMockRpc(path)
Expand All @@ -33,7 +35,7 @@ func TestGetBootstrap(t *testing.T) {
t.Fatalf("Failed to write mock bootstrap file: %v", err)
}
mockRpc := NewMockRpc(tempDir)
bootstrap, err := mockRpc.GetBootstrap([]byte{})
bootstrap, err := mockRpc.GetBootstrap([32]byte{})
if err != nil {
t.Fatalf("GetBootstrap failed: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package utils

import (
"encoding/hex"

"net/url"
"strings"

"bytes"
Expand Down Expand Up @@ -279,3 +279,7 @@ func BranchToNodes(branch []consensus_core.Bytes32) ([][]byte, error) {
}
return nodes, nil
}
func IsURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}

0 comments on commit d97fa10

Please sign in to comment.