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

Test/test consensus module #108

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Updated version to deneb and tests passing
18aaddy committed Nov 5, 2024
commit 1311e76f03b6dd78ca459c37ab2bbb43a03a74f8
11 changes: 7 additions & 4 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
@@ -295,22 +295,25 @@ func (in *Inner) Check_rpc() error {
func (in *Inner) get_execution_payload(slot *uint64) (*consensus_core.ExecutionPayload, error) {
errorChan := make(chan error, 1)
blockChan := make(chan consensus_core.BeaconBlock, 1)
versionChan := make(chan string , 1)
go func() {
var err error
block, err := in.RPC.GetBlock(*slot)
block,version, err := in.RPC.GetBlock(*slot)
if err != nil {
errorChan <- err
}
errorChan <- nil
blockChan <- block
versionChan <- version
}()

if err := <-errorChan; err != nil {
return nil, err
}

block := <-blockChan
Gethblock, err := beacon.BlockFromJSON("deneb", block.Hash)
version := <-versionChan
Gethblock, err := beacon.BlockFromJSON(version, block.Hash)
if err != nil {
return nil, err
}
@@ -340,7 +343,7 @@ func (in *Inner) Get_payloads(startSlot, endSlot uint64) ([]interface{}, error)
var payloads []interface{}

// Fetch the block at endSlot to get the initial parent hash
endBlock, err := in.RPC.GetBlock(endSlot)
endBlock,_, err := in.RPC.GetBlock(endSlot)
if err != nil {
return nil, err
}
@@ -357,7 +360,7 @@ func (in *Inner) Get_payloads(startSlot, endSlot uint64) ([]interface{}, error)
wg.Add(1)
go func(slot uint64) {
defer wg.Done()
block, err := in.RPC.GetBlock(slot)
block,_, err := in.RPC.GetBlock(slot)
if err != nil {
errorChan <- err
return
2 changes: 1 addition & 1 deletion consensus/rpc/consensus_rpc.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ type ConsensusRpc interface {
GetUpdates(period uint64, count uint8) ([]consensus_core.Update, error)
GetFinalityUpdate() (consensus_core.FinalityUpdate, error)
GetOptimisticUpdate() (consensus_core.OptimisticUpdate, error)
GetBlock(slot uint64) (consensus_core.BeaconBlock, error)
GetBlock(slot uint64) (consensus_core.BeaconBlock,string, error)
ChainId() (uint64, error)
}

8 changes: 4 additions & 4 deletions consensus/rpc/mock_rpc.go
Original file line number Diff line number Diff line change
@@ -81,18 +81,18 @@ func (m *MockRpc) GetOptimisticUpdate() (consensus_core.OptimisticUpdate, error)
}
return optimistic.Data, nil
}
func (m *MockRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock, error) {
func (m *MockRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock,string, error) {
path := filepath.Join(m.testdata, fmt.Sprintf("blocks/%d.json", slot))
res, err := os.ReadFile(path)
if err != nil {
return consensus_core.BeaconBlock{}, 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 consensus_core.BeaconBlock{}, err
return consensus_core.BeaconBlock{},"", err
}
return block.Data.Message, nil
return block.Data.Message,block.Version, nil
}
func (m *MockRpc) ChainId() (uint64, error) {
return 0, fmt.Errorf("not implemented")
2 changes: 1 addition & 1 deletion consensus/rpc/mock_rpc_test.go
Original file line number Diff line number Diff line change
@@ -205,7 +205,7 @@ func TestGetBlock(t *testing.T) {
}

mockRpc := NewMockRpc(tempDir)
block, err := mockRpc.GetBlock(4000)
block,_, err := mockRpc.GetBlock(4000)
if err != nil {
t.Fatalf("GetBlock failed: %v", err)
}
7 changes: 4 additions & 3 deletions consensus/rpc/nimbus_rpc.go
Original file line number Diff line number Diff line change
@@ -96,14 +96,14 @@ func (n *NimbusRpc) GetOptimisticUpdate() (consensus_core.OptimisticUpdate, erro
}
return res.Data, nil
}
func (n *NimbusRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock, error) {
func (n *NimbusRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock,string, error) {
req := fmt.Sprintf("%s/eth/v2/beacon/blocks/%s", n.rpc, strconv.FormatUint(slot, 10))
var res BeaconBlockResponse
err := get(req, &res)
if err != nil {
return consensus_core.BeaconBlock{}, fmt.Errorf("block error: %w", err)
return consensus_core.BeaconBlock{},"", fmt.Errorf("block error: %w", err)
}
return res.Data.Message, nil
return res.Data.Message,res.Version, nil
}
func (n *NimbusRpc) ChainId() (uint64, error) {
req := fmt.Sprintf("%s/eth/v1/config/spec", n.rpc)
@@ -118,6 +118,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/consensus_core.go
type BeaconBlockResponse struct {
Version string
Data BeaconBlockData
}
type BeaconBlockData struct {
2 changes: 1 addition & 1 deletion consensus/rpc/nimbus_rpc_test.go
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ func TestNimbusGetBlock(t *testing.T) {
}))
defer server.Close()
nimbusRpc := NewNimbusRpc(server.URL)
block, err := nimbusRpc.GetBlock(4000)
block,_, err := nimbusRpc.GetBlock(4000)
assert.NoError(t, err)
assert.Equal(t, uint64(4000), block.Slot)
}