mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
12403d249f
* HTTP proxy server for Eth2 APIs (#8904) * Implement API HTTP proxy server * cleanup + more comments * gateway will no longer be dependent on beaconv1 * handle error during ErrorJson type assertion * simplify handling of endpoint data * fix mux v1 route * use URL encoding for all requests * comment fieldProcessor * fix failing test * change proxy port to not interfere with e2e * gzl * simplify conditional expression * Move appending custom error header to grpcutils package * add api-middleware-port flag * fix documentation for processField * modify e2e port * change field processing error message * better error message for field processing * simplify base64ToHexProcessor * fix json structs * Run several new endpoints through API middleware (#8922) * Implement API HTTP proxy server * cleanup + more comments * gateway will no longer be dependent on beaconv1 * handle error during ErrorJson type assertion * simplify handling of endpoint data * fix mux v1 route * use URL encoding for all requests * comment fieldProcessor * fix failing test * change proxy port to not interfere with e2e * gzl * simplify conditional expression * Move appending custom error header to grpcutils package * add api-middleware-port flag * fix documentation for processField * modify e2e port * change field processing error message * better error message for field processing * simplify base64ToHexProcessor * fix json structs * /eth/v1/beacon/states/{state_id}/validators * /eth/v1/beacon/states/{state_id}/validators/{validator_id} * /eth/v1/beacon/states/{state_id}/validator_balances * /eth/v1/beacon/states/{state_id}/committees * allow skipping base64-encoding for query params * /eth/v1/beacon/pool/attestations * replace break with continue * Remove unused functions (#8924) Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Process SSZ-serialized beacon state through API middleware (#8925) * update field names * Process SSZ-serialized beacon state through API middleware * revert changes to go.mod and go.sum * Revert "Merge branch '__develop' into feature/api-middleware" This reverts commit 7c739a8fd71e2c1e3a14be85abd29a59b57ae9b5, reversing changes made to 2d0f8e012ecb006888ed8e826b45625a3edc2eeb. * update ethereumapis * update validator field name * update deps.bzl * update json tags (#8942) * Run `/node/syncing` through API Middleware (#8944) * add IsSyncing field to grpc response * run /node/syncing through the middleware Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Return HTTP status codes other than 200 and 500 from node and debug endpoints (#8937) * error codes for node endpoints * error codes for debug endpoints * better comment about headers * gzl * review comments * comment on return value * update fakeChecker used for fuzz tests * fix failing tests * Allow to pass URL params literally, without encoding to base64 (#8938) * Allow to pass URL params literally, without encoding to base64 * fix compile error Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Process SSZ-serialized beacon state through API middleware (#8925) * update field names * Process SSZ-serialized beacon state through API middleware * revert changes to go.mod and go.sum * Revert "Merge branch '__develop' into feature/api-middleware" This reverts commit 7c739a8fd71e2c1e3a14be85abd29a59b57ae9b5, reversing changes made to 2d0f8e012ecb006888ed8e826b45625a3edc2eeb. * update ethereumapis * update validator field name * update deps.bzl * update json tags (#8942) * Run `/node/syncing` through API Middleware (#8944) * add IsSyncing field to grpc response * run /node/syncing through the middleware Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Return HTTP status codes other than 200 and 500 from node and debug endpoints (#8937) * error codes for node endpoints * error codes for debug endpoints * better comment about headers * gzl * review comments * comment on return value * update fakeChecker used for fuzz tests * fix failing tests * Allow to pass URL params literally, without encoding to base64 (#8938) * Allow to pass URL params literally, without encoding to base64 * fix compile error Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * unused import * Return correct status codes from beacon endpoints (#8960) * Various API Middleware fixes (#8963) * Return correct status codes from `/states` endpoints * better error messages in debug and node * better error messages in state * returning correct error codes from validator endpoints * correct error codes for getting a block header * gzl * fix err variable name * fix nil block comparison * test fixes * make status enum test better * fix ineffectual assignment * make PR unstuck * return proper status codes * return uppercase keys from /config/spec * return lowercase validator status * convert requested enum values to uppercase * validator fixes * Implement `/beacon/headers` endpoint (#8966) * Refactor API Middleware into more manageable code (#8984) * move endpoint registration out of shared package * divide main function into smaller components * return early on error * implement hooks * implement custom handlers and add documentation * fix test compile error * restrict package visibility * remove redundant error checking * rename file * API Middleware unit tests (#8998) * move endpoint registration out of shared package * divide main function into smaller components * return early on error * implement hooks * implement custom handlers and add documentation * fix test compile error * restrict package visibility * remove redundant error checking * rename file * api_middleware_processing * endpoints * gzl * remove gazelle:ignore * merge * Implement SSZ version of `/blocks/{block_id}` (#8970) * Implement SSZ version of `/blocks/{block_id}` * add dependencies back * fix indentation in deps.bzl * parameterize ssz functions * get block ssz * update ethereumapis dependency * gzl * Do not reuse `Endpoint` structs between API calls (#9007) * code refactor * implement endpoint factory * fix test * fmt * include pbs * gaz * test naming fixes * remove unused code * radek comments * revert endpoint test * bring back bytes test case * move `signedBeaconBlock` to `migration` package * change `fmt.Errorf` to `errors.Wrap` * capitalize SSZ * capitalize URL * more review feedback * rename `handleGetBlockSSZ` to `handleGetBeaconBlockSSZ` * rename `IndexOutOfRangeError` to `ValidatorIndexOutOfRangeError` * simplify parameter names * test header * more corrections * properly allocate array capacity Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Nishant Das <nishdas93@gmail.com>
376 lines
11 KiB
Go
376 lines
11 KiB
Go
package statefetcher
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/interfaces"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestGetState(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
headSlot := types.Slot(123)
|
|
fillSlot := func(state *pb.BeaconState) error {
|
|
state.Slot = headSlot
|
|
return nil
|
|
}
|
|
state, err := testutil.NewBeaconState(testutil.FillRootsNaturalOpt, fillSlot)
|
|
require.NoError(t, err)
|
|
stateRoot, err := state.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("head", func(t *testing.T) {
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
}
|
|
|
|
s, err := p.State(ctx, []byte("head"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("genesis", func(t *testing.T) {
|
|
params.SetupTestConfigCleanup(t)
|
|
cfg := params.BeaconConfig()
|
|
cfg.ConfigName = "test"
|
|
params.OverrideBeaconConfig(cfg)
|
|
|
|
db := testDB.SetupDB(t)
|
|
b := testutil.NewBeaconBlock()
|
|
b.Block.StateRoot = bytesutil.PadTo([]byte("foo"), 32)
|
|
require.NoError(t, db.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b)))
|
|
r, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
|
|
state, err := testutil.NewBeaconState(func(state *pb.BeaconState) error {
|
|
state.BlockRoots[0] = r[:]
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
stateRoot, err := state.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, r))
|
|
require.NoError(t, db.SaveState(ctx, state, r))
|
|
|
|
p := StateProvider{
|
|
BeaconDB: db,
|
|
}
|
|
|
|
s, err := p.State(ctx, []byte("genesis"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("finalized", func(t *testing.T) {
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesByRoot[stateRoot] = state
|
|
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{
|
|
FinalizedCheckPoint: ð.Checkpoint{
|
|
Root: stateRoot[:],
|
|
},
|
|
},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := p.State(ctx, []byte("finalized"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("justified", func(t *testing.T) {
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesByRoot[stateRoot] = state
|
|
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{
|
|
CurrentJustifiedCheckPoint: ð.Checkpoint{
|
|
Root: stateRoot[:],
|
|
},
|
|
},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := p.State(ctx, []byte("justified"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("hex_root", func(t *testing.T) {
|
|
stateId, err := hexutil.Decode("0x" + strings.Repeat("0", 63) + "1")
|
|
require.NoError(t, err)
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesByRoot[bytesutil.ToBytes32(stateId)] = state
|
|
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := p.State(ctx, stateId)
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("hex_root_not_found", func(t *testing.T) {
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
}
|
|
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
|
|
require.NoError(t, err)
|
|
_, err = p.State(ctx, stateId)
|
|
require.ErrorContains(t, "state not found in the last 8192 state roots", err)
|
|
})
|
|
|
|
t.Run("slot", func(t *testing.T) {
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesBySlot[headSlot] = state
|
|
|
|
p := StateProvider{
|
|
GenesisTimeFetcher: &chainMock.ChainService{Slot: &headSlot},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := p.State(ctx, []byte(strconv.FormatUint(uint64(headSlot), 10)))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("slot_too_big", func(t *testing.T) {
|
|
p := StateProvider{
|
|
GenesisTimeFetcher: &chainMock.ChainService{
|
|
Genesis: time.Now(),
|
|
},
|
|
}
|
|
_, err := p.State(ctx, []byte(strconv.FormatUint(1, 10)))
|
|
assert.ErrorContains(t, "slot cannot be in the future", err)
|
|
})
|
|
|
|
t.Run("invalid_state", func(t *testing.T) {
|
|
p := StateProvider{}
|
|
_, err := p.State(ctx, []byte("foo"))
|
|
require.ErrorContains(t, "could not parse state ID", err)
|
|
})
|
|
}
|
|
|
|
func TestGetStateRoot(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
headSlot := types.Slot(123)
|
|
fillSlot := func(state *pb.BeaconState) error {
|
|
state.Slot = headSlot
|
|
return nil
|
|
}
|
|
state, err := testutil.NewBeaconState(testutil.FillRootsNaturalOpt, fillSlot)
|
|
require.NoError(t, err)
|
|
stateRoot, err := state.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("head", func(t *testing.T) {
|
|
b := testutil.NewBeaconBlock()
|
|
b.Block.StateRoot = stateRoot[:]
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{
|
|
State: state,
|
|
Block: interfaces.WrappedPhase0SignedBeaconBlock(b),
|
|
},
|
|
}
|
|
|
|
s, err := p.StateRoot(ctx, []byte("head"))
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot[:], s)
|
|
})
|
|
|
|
t.Run("genesis", func(t *testing.T) {
|
|
db := testDB.SetupDB(t)
|
|
b := testutil.NewBeaconBlock()
|
|
require.NoError(t, db.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b)))
|
|
r, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
|
|
state, err := testutil.NewBeaconState(func(state *pb.BeaconState) error {
|
|
state.BlockRoots[0] = r[:]
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, r))
|
|
require.NoError(t, db.SaveState(ctx, state, r))
|
|
|
|
p := StateProvider{
|
|
BeaconDB: db,
|
|
}
|
|
|
|
s, err := p.StateRoot(ctx, []byte("genesis"))
|
|
require.NoError(t, err)
|
|
genesisBlock, err := db.GenesisBlock(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, genesisBlock.Block().StateRoot(), s)
|
|
})
|
|
|
|
t.Run("finalized", func(t *testing.T) {
|
|
db := testDB.SetupDB(t)
|
|
genesis := bytesutil.ToBytes32([]byte("genesis"))
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis))
|
|
blk := testutil.NewBeaconBlock()
|
|
blk.Block.ParentRoot = genesis[:]
|
|
blk.Block.Slot = 40
|
|
root, err := blk.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
cp := ð.Checkpoint{
|
|
Epoch: 5,
|
|
Root: root[:],
|
|
}
|
|
// a valid chain is required to save finalized checkpoint.
|
|
require.NoError(t, db.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(blk)))
|
|
st, err := testutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
require.NoError(t, st.SetSlot(1))
|
|
// a state is required to save checkpoint
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
|
require.NoError(t, db.SaveFinalizedCheckpoint(ctx, cp))
|
|
|
|
p := StateProvider{
|
|
BeaconDB: db,
|
|
}
|
|
|
|
s, err := p.StateRoot(ctx, []byte("finalized"))
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, blk.Block.StateRoot, s)
|
|
})
|
|
|
|
t.Run("justified", func(t *testing.T) {
|
|
db := testDB.SetupDB(t)
|
|
genesis := bytesutil.ToBytes32([]byte("genesis"))
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis))
|
|
blk := testutil.NewBeaconBlock()
|
|
blk.Block.ParentRoot = genesis[:]
|
|
blk.Block.Slot = 40
|
|
root, err := blk.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
cp := ð.Checkpoint{
|
|
Epoch: 5,
|
|
Root: root[:],
|
|
}
|
|
// a valid chain is required to save finalized checkpoint.
|
|
require.NoError(t, db.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(blk)))
|
|
st, err := testutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
require.NoError(t, st.SetSlot(1))
|
|
// a state is required to save checkpoint
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
|
require.NoError(t, db.SaveJustifiedCheckpoint(ctx, cp))
|
|
|
|
p := StateProvider{
|
|
BeaconDB: db,
|
|
}
|
|
|
|
s, err := p.StateRoot(ctx, []byte("justified"))
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, blk.Block.StateRoot, s)
|
|
})
|
|
|
|
t.Run("hex_root", func(t *testing.T) {
|
|
stateId, err := hexutil.Decode("0x" + strings.Repeat("0", 63) + "1")
|
|
require.NoError(t, err)
|
|
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
}
|
|
|
|
s, err := p.StateRoot(ctx, stateId)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateId, s)
|
|
})
|
|
|
|
t.Run("hex_root_not_found", func(t *testing.T) {
|
|
p := StateProvider{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
}
|
|
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
|
|
require.NoError(t, err)
|
|
_, err = p.StateRoot(ctx, stateId)
|
|
require.ErrorContains(t, "state root not found in the last 8192 state roots", err)
|
|
})
|
|
|
|
t.Run("slot", func(t *testing.T) {
|
|
db := testDB.SetupDB(t)
|
|
genesis := bytesutil.ToBytes32([]byte("genesis"))
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis))
|
|
blk := testutil.NewBeaconBlock()
|
|
blk.Block.ParentRoot = genesis[:]
|
|
blk.Block.Slot = 40
|
|
root, err := blk.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(blk)))
|
|
st, err := testutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
require.NoError(t, st.SetSlot(1))
|
|
// a state is required to save checkpoint
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
|
|
|
slot := types.Slot(40)
|
|
p := StateProvider{
|
|
GenesisTimeFetcher: &chainMock.ChainService{Slot: &slot},
|
|
BeaconDB: db,
|
|
}
|
|
|
|
s, err := p.StateRoot(ctx, []byte(strconv.FormatUint(uint64(slot), 10)))
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, blk.Block.StateRoot, s)
|
|
})
|
|
|
|
t.Run("slot_too_big", func(t *testing.T) {
|
|
p := StateProvider{
|
|
GenesisTimeFetcher: &chainMock.ChainService{
|
|
Genesis: time.Now(),
|
|
},
|
|
}
|
|
_, err := p.StateRoot(ctx, []byte(strconv.FormatUint(1, 10)))
|
|
assert.ErrorContains(t, "slot cannot be in the future", err)
|
|
})
|
|
|
|
t.Run("invalid_state", func(t *testing.T) {
|
|
p := StateProvider{}
|
|
_, err := p.StateRoot(ctx, []byte("foo"))
|
|
require.ErrorContains(t, "could not parse state ID", err)
|
|
})
|
|
}
|
|
|
|
func TestNewStateNotFoundError(t *testing.T) {
|
|
e := NewStateNotFoundError(100)
|
|
assert.Equal(t, "state not found in the last 100 state roots", e.message)
|
|
}
|