mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 19:50:36 +00:00
143 lines
15 KiB
Go
143 lines
15 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"math"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
||
|
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
|
||
|
"github.com/ledgerwatch/erigon/common"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestGetBlockRewards(t *testing.T) {
|
||
|
_, blocks, _, _, _, handler, _, _, fcu := setupTestingHandler(t, clparams.BellatrixVersion)
|
||
|
var err error
|
||
|
fcu.HeadVal, err = blocks[len(blocks)-5].Block.HashSSZ()
|
||
|
require.NoError(t, err)
|
||
|
genesisVal, err := blocks[0].Block.HashSSZ()
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
fcu.HeadSlotVal = blocks[len(blocks)-1].Block.Slot
|
||
|
fcu.FinalizedCheckpointVal = solid.NewCheckpointFromParameters(fcu.HeadVal, math.MaxUint64)
|
||
|
fcu.FinalizedSlotVal = math.MaxUint64
|
||
|
|
||
|
cases := []struct {
|
||
|
blockID string
|
||
|
code int
|
||
|
expectedResp string
|
||
|
}{
|
||
|
{
|
||
|
blockID: "0x" + common.Bytes2Hex(fcu.HeadVal[:]),
|
||
|
code: http.StatusOK,
|
||
|
expectedResp: `{"data":{"proposer_index":"203","attestations":"332205","proposer_slashings":"0","attester_slashings":"0","sync_aggregate":"0","total":"332205"},"finalized":true,"execution_optimistic":false}` + "\n",
|
||
|
},
|
||
|
{
|
||
|
blockID: "0x" + common.Bytes2Hex(genesisVal[:]),
|
||
|
code: http.StatusOK,
|
||
|
expectedResp: `{"data":{"proposer_index":"98","attestations":"332205","proposer_slashings":"0","attester_slashings":"0","sync_aggregate":"0","total":"332205"},"finalized":true,"execution_optimistic":false}` + "\n",
|
||
|
},
|
||
|
{
|
||
|
blockID: "0x" + common.Bytes2Hex(make([]byte, 32)),
|
||
|
code: http.StatusNotFound,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, c := range cases {
|
||
|
t.Run(c.blockID, func(t *testing.T) {
|
||
|
server := httptest.NewServer(handler.mux)
|
||
|
defer server.Close()
|
||
|
// Query the block in the handler with /eth/v2/beacon/blocks/{block_id}
|
||
|
resp, err := http.Get(server.URL + "/eth/v1/beacon/rewards/blocks/" + c.blockID)
|
||
|
require.NoError(t, err)
|
||
|
defer resp.Body.Close()
|
||
|
require.Equal(t, c.code, resp.StatusCode)
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// unmarshal the json
|
||
|
out, err := io.ReadAll(resp.Body)
|
||
|
require.NoError(t, err)
|
||
|
require.Equal(t, c.expectedResp, string(out))
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostSyncCommitteeRewards(t *testing.T) {
|
||
|
_, blocks, _, _, _, handler, _, _, fcu := setupTestingHandler(t, clparams.BellatrixVersion)
|
||
|
var err error
|
||
|
fcu.HeadVal, err = blocks[len(blocks)-1].Block.HashSSZ()
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
fcu.HeadSlotVal = blocks[len(blocks)-1].Block.Slot
|
||
|
fcu.FinalizedSlotVal = math.MaxInt64
|
||
|
|
||
|
fcu.JustifiedCheckpointVal = solid.NewCheckpointFromParameters(fcu.HeadVal, fcu.HeadSlotVal/32)
|
||
|
fcu.FinalizedCheckpointVal = solid.NewCheckpointFromParameters(fcu.HeadVal, 99999999)
|
||
|
|
||
|
cases := []struct {
|
||
|
name string
|
||
|
blockId string
|
||
|
code int
|
||
|
request string
|
||
|
expected string
|
||
|
}{
|
||
|
{
|
||
|
name: "all validators",
|
||
|
blockId: "0x" + common.Bytes2Hex(fcu.HeadVal[:]),
|
||
|
code: http.StatusOK,
|
||
|
expected: `{"data":[{"validator_index":"0","reward":"-698"},{"validator_index":"1","reward":"-698"},{"validator_index":"2","reward":"-698"},{"validator_index":"3","reward":"-698"},{"validator_index":"4","reward":"-698"},{"validator_index":"5","reward":"-698"},{"validator_index":"6","reward":"-698"},{"validator_index":"7","reward":"-698"},{"validator_index":"8","reward":"-698"},{"validator_index":"9","reward":"-698"},{"validator_index":"10","reward":"-698"},{"validator_index":"11","reward":"-698"},{"validator_index":"12","reward":"-698"},{"validator_index":"13","reward":"-698"},{"validator_index":"14","reward":"-698"},{"validator_index":"15","reward":"-698"},{"validator_index":"16","reward":"-698"},{"validator_index":"17","reward":"-698"},{"validator_index":"18","reward":"-698"},{"validator_index":"19","reward":"-698"},{"validator_index":"20","reward":"-698"},{"validator_index":"21","reward":"-698"},{"validator_index":"22","reward":"-698"},{"validator_index":"23","reward":"-698"},{"validator_index":"24","reward":"-698"},{"validator_index":"25","reward":"-698"},{"validator_index":"26","reward":"-698"},{"validator_index":"27","reward":"-698"},{"validator_index":"28","reward":"-698"},{"validator_index":"29","reward":"-698"},{"validator_index":"30","reward":"-698"},{"validator_index":"31","reward":"-698"},{"validator_index":"32","reward":"-698"},{"validator_index":"33","reward":"-698"},{"validator_index":"34","reward":"-698"},{"validator_index":"35","reward":"-698"},{"validator_index":"36","reward":"-698"},{"validator_index":"37","reward":"-698"},{"validator_index":"38","reward":"-698"},{"validator_index":"39","reward":"-698"},{"validator_index":"40","reward":"-698"},{"validator_index":"41","reward":"-698"},{"validator_index":"42","reward":"-698"},{"validator_index":"43","reward":"-698"},{"validator_index":"44","reward":"-698"},{"validator_index":"45","reward":"-698"},{"validator_index":"46","reward":"-698"},{"validator_index":"47","reward":"-698"},{"validator_index":"48","reward":"-698"},{"validator_index":"49","reward":"-698"},{"validator_index":"50","reward":"-698"},{"validator_index":"51","reward":"-698"},{"validator_index":"52","reward":"-698"},{"validator_index":"53","reward":"-698"},{"validator_index":"54","reward":"-698"},{"validator_index":"55","reward":"-698"},{"validator_index":"56","reward":"-698"},{"validator_index":"57","reward":"-698"},{"validator_index":"58","reward":"-698"},{"validator_index":"59","reward":"-698"},{"validator_index":"60","reward":"-698"},{"validator_index":"61","reward":"-698"},{"validator_index":"62","reward":"-698"},{"validator_index":"63","reward":"-698"},{"validator_index":"64","reward":"-698"},{"validator_index":"65","reward":"-698"},{"validator_index":"66","reward":"-698"},{"validator_index":"67","reward":"-698"},{"validator_index":"68","reward":"-698"},{"validator_index":"69","reward":"-698"},{"validator_index":"70","reward":"-698"},{"validator_index":"71","reward":"-698"},{"validator_index":"72","reward":"-698"},{"validator_index":"73","reward":"-698"},{"validator_index":"74","reward":"-698"},{"validator_index":"75","reward":"-698"},{"validator_index":"76","reward":"-698"},{"validator_index":"77","reward":"-698"},{"validator_index":"78","reward":"-698"},{"validator_index":"79","reward":"-698"},{"validator_index":"80","reward":"-698"},{"validator_index":"81","reward":"-698"},{"validator_index":"82","reward":"-698"},{"validator_index":"83","reward":"-698"},{"validator_index":"84","reward":"-698"},{"validator_index":"85","reward":"-698"},{"validator_index":"86","reward":"-698"},{"validator_index":"87","reward":"-698"},{"validator_index":"88","reward":"-698"},{"validator_index":"89","reward":"-698"},{"validator_index":"90","reward":"-698"},{"validator_index":"91","reward":"-698"},{"validator_index":"92","reward":"-698"},{"validator_index":"93","reward":"-698"},{"validator_index":"94","reward":"-698"},{"validator_index":"95","reward":"-698"},{"validator_index":"96","reward":"-698"},{"validator_index":"97","reward":"-698"},{"validator_index":"98","reward":"-698"},{"validator_index":"99"
|
||
|
},
|
||
|
{
|
||
|
blockId: "0x" + common.Bytes2Hex(make([]byte, 32)),
|
||
|
code: http.StatusNotFound,
|
||
|
},
|
||
|
{
|
||
|
name: "2 validators",
|
||
|
blockId: "0x" + common.Bytes2Hex(fcu.HeadVal[:]),
|
||
|
request: `["1","4"]`,
|
||
|
code: http.StatusOK,
|
||
|
expected: `{"data":[{"validator_index":"1","reward":"-698"},{"validator_index":"4","reward":"-698"}],"finalized":true,"execution_optimistic":false}` + "\n", // Add your expected response
|
||
|
},
|
||
|
}
|
||
|
for _, c := range cases {
|
||
|
t.Run(c.name, func(t *testing.T) {
|
||
|
server := httptest.NewServer(handler.mux)
|
||
|
defer server.Close()
|
||
|
url := fmt.Sprintf("%s/eth/v1/beacon/rewards/sync_committee/%s", server.URL, c.blockId)
|
||
|
|
||
|
// Create a request
|
||
|
req, err := http.NewRequest("POST", url, strings.NewReader(c.request))
|
||
|
require.NoError(t, err)
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
|
||
|
// Perform the request
|
||
|
resp, err := http.DefaultClient.Do(req)
|
||
|
require.NoError(t, err)
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
// Check status code
|
||
|
require.Equal(t, c.code, resp.StatusCode)
|
||
|
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Read the response body
|
||
|
out, err := io.ReadAll(resp.Body)
|
||
|
require.NoError(t, err)
|
||
|
if string(out) != c.expected {
|
||
|
panic(string(out))
|
||
|
}
|
||
|
// Compare the response with the expected result
|
||
|
require.Equal(t, c.expected, string(out))
|
||
|
})
|
||
|
}
|
||
|
}
|