Skip to content

Commit

Permalink
Fixed build error n refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkLord017 committed Oct 10, 2024
1 parent 7861383 commit 70d4a72
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 52 deletions.
8 changes: 4 additions & 4 deletions consensus/rpc/consensus_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type ConsensusRpc interface {
}

func NewConsensusRpc(rpc string) ConsensusRpc {
if rpc == "testdata/" {
return NewMockRpc(rpc)
if(rpc == "testdata/"){
return NewMockRpc(rpc)
}else {
return NewNimbusRpc(rpc)
}
return NewNimbusRpc(rpc)

}
104 changes: 80 additions & 24 deletions consensus/rpc/mock_rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,125 +1,180 @@
package rpc

import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
)
func TestNewMockRpc(t *testing.T) {
path := "/tmp/testdata"
mockRpc := NewMockRpc(path)
if mockRpc.testdata != path {
t.Errorf("Expected testdata path to be %s, got %s", path, mockRpc.testdata)
}
}

func TestGetBootstrap(t *testing.T) {
tempDir, err := os.MkdirTemp("", "mock_rpc_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
mockBootstrap := BootstrapResponse{
Data: consensus_core.Bootstrap{
Header: consensus_core.Header{
Slot: 1000,

mockBootstrap := map[string]interface{}{
"data": map[string]interface{}{
"header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "1000",
"proposer_index": "12345",
"parent_root": "0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
"state_root": "0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40",
"body_root": "0x4142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60",
},
},
},
}

bootstrapJSON, _ := json.Marshal(mockBootstrap)
err = os.WriteFile(filepath.Join(tempDir, "bootstrap.json"), bootstrapJSON, 0644)
if err != nil {
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)
}

if bootstrap.Header.Slot != 1000 {
t.Errorf("Expected bootstrap slot to be 1000, got %d", bootstrap.Header.Slot)
}
}

func TestGetUpdates(t *testing.T) {
tempDir, err := os.MkdirTemp("", "mock_rpc_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
mockUpdates := UpdateResponse{
{Data: consensus_core.Update{SignatureSlot: 1}},
{Data: consensus_core.Update{SignatureSlot: 2}},

mockUpdates := []map[string]interface{}{
{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "1",
"proposer_index": "12345",
"parent_root": "0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
"state_root": "0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40",
"body_root": "0x4142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60",
},
},
"signature_slot": "1",
},
},
{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "2",
},
},
"signature_slot": "2",
},
},
}

updatesJSON, _ := json.Marshal(mockUpdates)
err = os.WriteFile(filepath.Join(tempDir, "updates.json"), updatesJSON, 0644)
if err != nil {
t.Fatalf("Failed to write mock updates file: %v", err)
}

mockRpc := NewMockRpc(tempDir)
updates, err := mockRpc.GetUpdates(1, 2)
if err != nil {
t.Fatalf("GetUpdates failed: %v", err)
}

if len(updates) != 2 {
t.Errorf("Expected 2 updates, got %d", len(updates))
}
if updates[0].SignatureSlot != 1 || updates[1].SignatureSlot != 2 {
t.Errorf("Unexpected update signature slots")
}
}

func TestGetFinalityUpdate(t *testing.T) {
tempDir, err := os.MkdirTemp("", "mock_rpc_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
mockFinality := FinalityUpdateResponse{
Data: consensus_core.FinalityUpdate{
FinalizedHeader: consensus_core.Header{
Slot: 2000,

mockFinality := map[string]interface{}{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "1000",
},
},
"finalized_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "2000",
},
},
},
}

finalityJSON, _ := json.Marshal(mockFinality)
err = os.WriteFile(filepath.Join(tempDir, "finality.json"), finalityJSON, 0644)
if err != nil {
t.Fatalf("Failed to write mock finality file: %v", err)
}

mockRpc := NewMockRpc(tempDir)
finality, err := mockRpc.GetFinalityUpdate()
if err != nil {
t.Fatalf("GetFinalityUpdate failed: %v", err)
}

if finality.FinalizedHeader.Slot != 2000 {
t.Errorf("Expected finality slot to be 2000, got %d", finality.FinalizedHeader.Slot)
}
}

func TestGetOptimisticUpdate(t *testing.T) {
tempDir, err := os.MkdirTemp("", "mock_rpc_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
mockOptimistic := OptimisticUpdateResponse{
Data: consensus_core.OptimisticUpdate{
SignatureSlot: 3000,

mockOptimistic := map[string]interface{}{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "3000",
},
},
"signature_slot": "3000",
},
}

optimisticJSON, _ := json.Marshal(mockOptimistic)
err = os.WriteFile(filepath.Join(tempDir, "optimistic.json"), optimisticJSON, 0644)
if err != nil {
t.Fatalf("Failed to write mock optimistic file: %v", err)
}

mockRpc := NewMockRpc(tempDir)
optimistic, err := mockRpc.GetOptimisticUpdate()
if err != nil {
t.Fatalf("GetOptimisticUpdate failed: %v", err)
}

if optimistic.SignatureSlot != 3000 {
t.Errorf("Expected optimistic signature slot to be 3000, got %d", optimistic.SignatureSlot)
}
}

func TestGetBlock(t *testing.T) {
tempDir, err := os.MkdirTemp("", "mock_rpc_test")
if err != nil {
Expand Down Expand Up @@ -154,10 +209,11 @@ func TestGetBlock(t *testing.T) {
t.Errorf("Expected block slot to be 4000, got %d", block.Slot)
}
}

func TestChainId(t *testing.T) {
mockRpc := NewMockRpc("/tmp/testdata")
_, err := mockRpc.ChainId()
if err == nil || err.Error() != "not implemented" {
t.Errorf("Expected 'not implemented' error, got %v", err)
}
}
}
12 changes: 5 additions & 7 deletions consensus/rpc/nimbus_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (n *NimbusRpc) GetUpdates(period uint64, count uint8) ([]consensus_core.Upd
if err != nil {
return nil, fmt.Errorf("updates error: %w", err)
}

updates := make([]consensus_core.Update, len(res))
for i, update := range res {
updates[i] = update.Data
Expand Down Expand Up @@ -117,7 +116,7 @@ func (n *NimbusRpc) ChainId() (uint64, error) {
}

// BeaconBlock, Update,FinalityUpdate ,OptimisticUpdate,Bootstrap yet to be defined in consensus-core/src/types/mod.go
// For now defined in consensus/ go
// For now defined in consensus/consensus_core.go
type BeaconBlockResponse struct {
Data BeaconBlockData
}
Expand All @@ -126,14 +125,13 @@ type BeaconBlockData struct {
}
type UpdateResponse = []UpdateData
type UpdateData struct {
Data consensus_core.Update `json:"data"`
Data consensus_core.Update
}

type FinalityUpdateResponse struct {
Data consensus_core.FinalityUpdate `json:"data"`
Data consensus_core.FinalityUpdate
}
type OptimisticUpdateResponse struct {
Data consensus_core.OptimisticUpdate `json:"data"`
Data consensus_core.OptimisticUpdate
}
type SpecResponse struct {
Data Spec
Expand All @@ -142,5 +140,5 @@ type Spec struct {
ChainId uint64
}
type BootstrapResponse struct {
Data consensus_core.Bootstrap `json:"data"`
Data consensus_core.Bootstrap
}
74 changes: 57 additions & 17 deletions consensus/rpc/nimbus_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ func TestNimbusGetBootstrap(t *testing.T) {
expectedPath := fmt.Sprintf("/eth/v1/beacon/light_client/bootstrap/0x%x", blockRoot)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, expectedPath, r.URL.Path)
response := BootstrapResponse{
Data: consensus_core.Bootstrap{
Header: consensus_core.Header{
Slot: 1000,
response := map[string]interface{}{
"data": map[string]interface{}{
"header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "1000",
"proposer_index": "12345",
"parent_root": "0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
"state_root": "0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40",
"body_root": "0x4142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60",
},
},
},
}
Expand All @@ -39,9 +45,31 @@ func TestNimbusGetUpdates(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/eth/v1/beacon/light_client/updates", r.URL.Path)
assert.Equal(t, "start_period=1000&count=5", r.URL.RawQuery)
response := UpdateResponse{
{Data: consensus_core.Update{AttestedHeader: consensus_core.Header{Slot: 1000}}},
{Data: consensus_core.Update{AttestedHeader: consensus_core.Header{Slot: 1001}}},
response := []map[string]interface{}{
{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "1",
"proposer_index": "12345",
"parent_root": "0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
"state_root": "0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40",
"body_root": "0x4142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60",
},
},
"signature_slot": "1",
},
},
{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "2",
},
},
"signature_slot": "2",
},
},
}
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
Expand All @@ -51,16 +79,23 @@ func TestNimbusGetUpdates(t *testing.T) {
updates, err := nimbusRpc.GetUpdates(1000, 5)
assert.NoError(t, err)
assert.Len(t, updates, 2)
assert.Equal(t, uint64(1000), updates[0].AttestedHeader.Slot)
assert.Equal(t, uint64(1001), updates[1].AttestedHeader.Slot)
assert.Equal(t, uint64(1), updates[0].AttestedHeader.Slot)
assert.Equal(t, uint64(2), updates[1].AttestedHeader.Slot)
}
func TestNimbusGetFinalityUpdate(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/eth/v1/beacon/light_client/finality_update", r.URL.Path)
response := FinalityUpdateResponse{
Data: consensus_core.FinalityUpdate{
FinalizedHeader: consensus_core.Header{
Slot: 2000,
response := map[string]interface{}{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "2000",
},
},
"finalized_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "2000",
},
},
},
}
Expand All @@ -76,9 +111,14 @@ func TestNimbusGetFinalityUpdate(t *testing.T) {
func TestNimbusGetOptimisticUpdate(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/eth/v1/beacon/light_client/optimistic_update", r.URL.Path)
response := OptimisticUpdateResponse{
Data: consensus_core.OptimisticUpdate{
SignatureSlot: 3000,
response := map[string]interface{}{
"data": map[string]interface{}{
"attested_header": map[string]interface{}{
"beacon": map[string]interface{}{
"slot": "3000",
},
},
"signature_slot": "3000",
},
}
err := json.NewEncoder(w).Encode(response)
Expand Down Expand Up @@ -125,4 +165,4 @@ func TestNimbusChainId(t *testing.T) {
chainId, err := nimbusRpc.ChainId()
assert.NoError(t, err)
assert.Equal(t, uint64(5000), chainId)
}
}

0 comments on commit 70d4a72

Please sign in to comment.