Skip to content

Commit

Permalink
Added Unit testing (Sentinel/Beacon API) (#9387)
Browse files Browse the repository at this point in the history
## Lists of Bugs found (and fixed)
* version in wrong format.
* Avoid crash in validators endpoint
* Fixed formatting in the archive node sanitizer
* Tracking MetadataV2/MetadataV1/Ping with DiscV5.
* More relevant responses to Status and MetadataV2/V1
  • Loading branch information
Giulio2002 authored Feb 6, 2024
1 parent 63a1422 commit 66b0aa7
Show file tree
Hide file tree
Showing 39 changed files with 1,459 additions and 78 deletions.
2 changes: 1 addition & 1 deletion cl/beacon/beaconhttp/beacon_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (b *BeaconResponse) MarshalJSON() ([]byte, error) {
o["finalized"] = *b.Finalized
}
if b.Version != nil {
o["version"] = *b.Version
o["version"] = clparams.ClVersionToString(*b.Version)
}
if b.ExecutionOptimistic != nil {
o["execution_optimistic"] = *b.ExecutionOptimistic
Expand Down
2 changes: 1 addition & 1 deletion cl/beacon/handler/attestation_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type attestationsRewardsResponse struct {
TotalRewards []TotalReward `json:"total_rewards"`
}

func (a *ApiHandler) getAttestationsRewards(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
ctx := r.Context()

tx, err := a.indiciesDB.BeginRo(ctx)
Expand Down
3 changes: 3 additions & 0 deletions cl/beacon/handler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (a *ApiHandler) GetEth1V1BuilderStatesExpectedWithdrawals(w http.ResponseWr
if err != nil {
return nil, err
}
if a.syncedData.Syncing() {
return nil, beaconhttp.NewEndpointError(http.StatusServiceUnavailable, fmt.Errorf("beacon node is syncing"))
}
if root == headRoot {
s, cn := a.syncedData.HeadState()
defer cn()
Expand Down
5 changes: 3 additions & 2 deletions cl/beacon/handler/data_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler_test
package handler

import (
"embed"
Expand Down Expand Up @@ -44,6 +44,7 @@ func defaultHarnessOpts(c harnessConfig) []beacontest.HarnessOption {
}
}
_, blocks, _, _, postState, handler, _, sm, fcu := setupTestingHandler(c.t, c.v, logger)

var err error

lastBlockRoot, err := blocks[len(blocks)-1].Block.HashSSZ()
Expand Down Expand Up @@ -106,8 +107,8 @@ func defaultHarnessOpts(c harnessConfig) []beacontest.HarnessOption {

fcu.FinalizedCheckpointVal = solid.NewCheckpointFromParameters(common.Hash{1, 2, 3}, 1)
fcu.JustifiedCheckpointVal = solid.NewCheckpointFromParameters(common.Hash{1, 2, 3}, 2)

}
sm.OnHeadState(postState)

return []beacontest.HarnessOption{
beacontest.WithTesting(c.t),
Expand Down
2 changes: 1 addition & 1 deletion cl/beacon/handler/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type genesisResponse struct {
GenesisForkVersion libcommon.Bytes4 `json:"genesis_fork_version"`
}

func (a *ApiHandler) getGenesis(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
func (a *ApiHandler) GetEthV1BeaconGenesis(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
if a.genesisCfg == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("Genesis Config is missing"))
}
Expand Down
14 changes: 7 additions & 7 deletions cl/beacon/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func (a *ApiHandler) init() {
})
r.Route("/beacon", func(r chi.Router) {
r.Route("/rewards", func(r chi.Router) {
r.Post("/sync_committee/{block_id}", beaconhttp.HandleEndpointFunc(a.getSyncCommitteesRewards))
r.Get("/blocks/{block_id}", beaconhttp.HandleEndpointFunc(a.getBlockRewards))
r.Post("/attestations/{epoch}", beaconhttp.HandleEndpointFunc(a.getAttestationsRewards))
r.Post("/sync_committee/{block_id}", beaconhttp.HandleEndpointFunc(a.PostEthV1BeaconRewardsSyncCommittees))
r.Get("/blocks/{block_id}", beaconhttp.HandleEndpointFunc(a.GetEthV1BeaconRewardsBlocks))
r.Post("/attestations/{epoch}", beaconhttp.HandleEndpointFunc(a.PostEthV1BeaconRewardsAttestations))
})
r.Route("/headers", func(r chi.Router) {
r.Get("/", beaconhttp.HandleEndpointFunc(a.getHeaders))
Expand All @@ -88,7 +88,7 @@ func (a *ApiHandler) init() {
r.Get("/{block_id}/attestations", beaconhttp.HandleEndpointFunc(a.getBlockAttestations))
r.Get("/{block_id}/root", beaconhttp.HandleEndpointFunc(a.getBlockRoot))
})
r.Get("/genesis", beaconhttp.HandleEndpointFunc(a.getGenesis))
r.Get("/genesis", beaconhttp.HandleEndpointFunc(a.GetEthV1BeaconGenesis))
r.Get("/blinded_blocks/{block_id}", beaconhttp.HandleEndpointFunc(a.getBlindedBlock))
r.Route("/pool", func(r chi.Router) {
r.Get("/voluntary_exits", beaconhttp.HandleEndpointFunc(a.GetEthV1BeaconPoolVoluntaryExits))
Expand Down Expand Up @@ -119,9 +119,9 @@ func (a *ApiHandler) init() {
r.Get("/validators", http.NotFound)
r.Get("/root", beaconhttp.HandleEndpointFunc(a.getStateRoot))
r.Get("/fork", beaconhttp.HandleEndpointFunc(a.getStateFork))
r.Get("/validators", beaconhttp.HandleEndpointFunc(a.getAllValidators))
r.Get("/validator_balances", beaconhttp.HandleEndpointFunc(a.getAllValidatorsBalances))
r.Get("/validators/{validator_id}", beaconhttp.HandleEndpointFunc(a.getSingleValidator))
r.Get("/validators", beaconhttp.HandleEndpointFunc(a.GetEthV1BeaconStatesValidators))
r.Get("/validator_balances", beaconhttp.HandleEndpointFunc(a.GetEthV1BeaconValidatorsBalances))
r.Get("/validators/{validator_id}", beaconhttp.HandleEndpointFunc(a.GetEthV1BeaconStatesValidator))
})
})
})
Expand Down
10 changes: 10 additions & 0 deletions cl/beacon/handler/harness/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ tests:
- actual_code == 200
- actual.data.address == "0x00000000219ab540356cBB839Cbe05303d7705Fa"
- actual.data.chain_id == "1"
- name: genesis
actual:
handler: i
path: /eth/v1/beacon/genesis
compare:
exprs:
- actual_code == 200
- actual.data.genesis_time == "1606824023"
- actual.data.genesis_validators_root == "0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"
- actual.data.genesis_fork_version == "0xbba4da96"
15 changes: 15 additions & 0 deletions cl/beacon/handler/harness/expected_withdrawals.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests:
- name: head_expected_withdrawals
actual:
handler: i
path: /eth/v1/builder/states/head/expected_withdrawals
expect:
file: "expected_withdrawals_head"
fs: td
- name: head_expected_avg
actual:
handler: i
path: /eth/v1/builder/states/8320/expected_withdrawals
expect:
file: "expected_withdrawals_avg"
fs: td
36 changes: 36 additions & 0 deletions cl/beacon/handler/harness/validators.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tests:
- name: head_validators_all
actual:
handler: i
path: /eth/v1/beacon/states/head/validators
expect:
file: "head_validators_all"
fs: td
- name: validators_some
actual:
handler: i
path: /eth/v1/beacon/states/159/validators?id=0,1,2,0xb0e7791fb972fe014159aa33a98622da3cdc98ff707965e536d8636b5fcc5ac7a91a8c46e59a00dca575af0f18fb13dc&status=active
expect:
file: "validators_some"
fs: td
- name: validator
actual:
handler: i
path: /eth/v1/beacon/states/159/validators/0xb0e7791fb972fe014159aa33a98622da3cdc98ff707965e536d8636b5fcc5ac7a91a8c46e59a00dca575af0f18fb13dc
expect:
file: "validator_1"
fs: td
- name: validator_not_found
actual:
handler: i
path: /eth/v1/beacon/states/159/validators/0xffe7791fb972fe014159aa33a98622da3cdc98ff707965e536d8636b5fcc5ac7a91a8c46e59a00dca575af0f18fb13dc
compare:
exprs:
- "actual_code==404"
- name: head_validators_balances
actual:
handler: i
path: /eth/v1/beacon/states/head/validator_balances
expect:
file: "head_validators_balances"
fs: td
12 changes: 11 additions & 1 deletion cl/beacon/handler/harness_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler_test
package handler

import (
"testing"
Expand Down Expand Up @@ -42,6 +42,16 @@ func TestHarnessBellatrix(t *testing.T) {
beacontest.WithTestFromFs(Harnesses, "attestation_rewards_bellatrix"),
beacontest.WithTestFromFs(Harnesses, "duties_sync_bellatrix"),
beacontest.WithTestFromFs(Harnesses, "lightclient"),
beacontest.WithTestFromFs(Harnesses, "validators"),
)...,
)
}

func TestHarnessCapella(t *testing.T) {
beacontest.Execute(
append(
defaultHarnessOpts(harnessConfig{t: t, v: clparams.CapellaVersion, finalized: true}),
beacontest.WithTestFromFs(Harnesses, "expected_withdrawals"),
)...,
)
}
Expand Down
Loading

0 comments on commit 66b0aa7

Please sign in to comment.