Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor rpc #63

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
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
8 changes: 7 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 @@ -15,5 +16,10 @@ type ConsensusRpc interface {
}

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 != ""
}
Loading