Skip to content

Commit

Permalink
add is syncing to beacon
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Dec 5, 2024
1 parent e79a986 commit 214ce72
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/adapters/beaconchain/beaconchain_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ func NewBeaconchainAdapter(beaconChainUrl string) *BeaconchainAdapter {
}
}

// GetSyncingStatus checks if the beacon node is syncing.
func (b *BeaconchainAdapter) GetSyncingStatus() (bool, error) {
url := fmt.Sprintf("%s/eth/v1/node/syncing", b.beaconChainUrl)
resp, err := http.Get(url)
if err != nil {
return false, fmt.Errorf("failed to send request to Beaconchain at %s: %w", url, err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("unexpected status code %d received from Beaconchain", resp.StatusCode)
}

// Parse the response
var result struct {
Data struct {
IsSyncing bool `json:"is_syncing"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return false, fmt.Errorf("failed to decode response from Beaconchain: %w", err)
}

return result.Data.IsSyncing, nil
}

func (b *BeaconchainAdapter) GetValidatorStatus(pubkey string) (domain.ValidatorStatus, error) {
validatorData, err := b.PostStateValidators("finalized", []string{pubkey}, nil)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,22 @@ func TestGetEpochHeaderIntegration(t *testing.T) {
assert.Greater(t, epoch, uint64(0))
t.Logf("Epoch for finalized block: %d", epoch)
}

// TestGetSyncingStatusIntegration tests the GetSyncingStatus method of the BeaconchainAdapter
func TestGetSyncingStatusIntegration(t *testing.T) {
adapter := setupBeaconchainAdapter(t)

// Call the GetSyncingStatus method
isSyncing, err := adapter.GetSyncingStatus()
assert.NoError(t, err)

// Assert the result is a valid boolean
assert.IsType(t, isSyncing, false, "Expected the result to be a boolean")

// Log the result for debugging
if isSyncing {
t.Log("The beacon node is syncing.")
} else {
t.Log("The beacon node is not syncing.")
}
}

0 comments on commit 214ce72

Please sign in to comment.