Skip to content

Commit

Permalink
Implemented Tests for consensus_rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
VeerChaurasia committed Sep 25, 2024
1 parent ece86b2 commit e12e28e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
84 changes: 84 additions & 0 deletions consensus/rpc/consensus_rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package rpc
import (
"testing"
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// MockConsensusRpc is a mock implementation of the ConsensusRpc interface
type MockConsensusRpc struct {
mock.Mock
}
func (m *MockConsensusRpc) GetBootstrap(block_root [32]byte) (consensus_core.Bootstrap, error) {
args := m.Called(block_root)
return args.Get(0).(consensus_core.Bootstrap), args.Error(1)
}
func (m *MockConsensusRpc) GetUpdates(period uint64, count uint8) ([]consensus_core.Update, error) {
args := m.Called(period, count)
return args.Get(0).([]consensus_core.Update), args.Error(1)
}
func (m *MockConsensusRpc) GetFinalityUpdate() (consensus_core.FinalityUpdate, error) {
args := m.Called()
return args.Get(0).(consensus_core.FinalityUpdate), args.Error(1)
}
func (m *MockConsensusRpc) GetOptimisticUpdate() (consensus_core.OptimisticUpdate, error) {
args := m.Called()
return args.Get(0).(consensus_core.OptimisticUpdate), args.Error(1)
}
func (m *MockConsensusRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock, error) {
args := m.Called(slot)
return args.Get(0).(consensus_core.BeaconBlock), args.Error(1)
}
func (m *MockConsensusRpc) ChainId() (uint64, error) {
args := m.Called()
return args.Get(0).(uint64), args.Error(1)
}
func TestNewConsensusRpc(t *testing.T) {
rpcURL := "http://example.com"
consensusRpc := NewConsensusRpc(rpcURL)
assert.Implements(t, (*ConsensusRpc)(nil), consensusRpc)
_, ok := consensusRpc.(*NimbusRpc)
assert.True(t, ok, "NewConsensusRpc should return a *NimbusRpc")
}

func TestConsensusRpcInterface(t *testing.T) {
mockRpc := new(MockConsensusRpc)
// Test GetBootstrap
mockBootstrap := consensus_core.Bootstrap{Header: consensus_core.Header{Slot: 1000}}
mockRpc.On("GetBootstrap", mock.Anything).Return(mockBootstrap, nil)
bootstrap, err := mockRpc.GetBootstrap([32]byte{})
assert.NoError(t, err)
assert.Equal(t, uint64(1000), bootstrap.Header.Slot)
// Test GetUpdates
mockUpdates := []consensus_core.Update{{AttestedHeader: consensus_core.Header{Slot: 2000}}}
mockRpc.On("GetUpdates", uint64(1), uint8(5)).Return(mockUpdates, nil)
updates, err := mockRpc.GetUpdates(1, 5)
assert.NoError(t, err)
assert.Equal(t, uint64(2000), updates[0].AttestedHeader.Slot)
// Test GetFinalityUpdate
mockFinalityUpdate := consensus_core.FinalityUpdate{FinalizedHeader: consensus_core.Header{Slot: 3000}}
mockRpc.On("GetFinalityUpdate").Return(mockFinalityUpdate, nil)
finalityUpdate, err := mockRpc.GetFinalityUpdate()
assert.NoError(t, err)
assert.Equal(t, uint64(3000), finalityUpdate.FinalizedHeader.Slot)
// Test GetOptimisticUpdate
mockOptimisticUpdate := consensus_core.OptimisticUpdate{SignatureSlot: 4000}
mockRpc.On("GetOptimisticUpdate").Return(mockOptimisticUpdate, nil)
optimisticUpdate, err := mockRpc.GetOptimisticUpdate()
assert.NoError(t, err)
assert.Equal(t, uint64(4000), optimisticUpdate.SignatureSlot)
// Test GetBlock
mockBlock := consensus_core.BeaconBlock{Slot: 5000}
mockRpc.On("GetBlock", uint64(5000)).Return(mockBlock, nil)
block, err := mockRpc.GetBlock(5000)
assert.NoError(t, err)
assert.Equal(t, uint64(5000), block.Slot)
// Test ChainId
mockChainId := uint64(1)
mockRpc.On("ChainId").Return(mockChainId, nil)
chainId, err := mockRpc.ChainId()
assert.NoError(t, err)
assert.Equal(t, uint64(1), chainId)
// Assert that all expected mock calls were made
mockRpc.AssertExpectations(t)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down

0 comments on commit e12e28e

Please sign in to comment.