mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Various bug fixes in Eth API (#9649)
* fix block structure * correct status code when block is not found * make `/internal` work with events and SSZ * test fix * better block serialize tests Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -131,7 +131,12 @@ func prepareSSZRequestForProxying(m *gateway.ApiProxyMiddleware, endpoint gatewa
|
||||
req.URL.Host = m.GatewayAddress
|
||||
req.RequestURI = ""
|
||||
req.URL.Path = sszPath
|
||||
return gateway.HandleURLParameters(endpoint.Path, req, []string{})
|
||||
if errJson := gateway.HandleURLParameters(endpoint.Path, req, []string{}); errJson != nil {
|
||||
return errJson
|
||||
}
|
||||
// We have to add the prefix after handling parameters because adding the prefix changes URL segment indexing.
|
||||
req.URL.Path = "/internal" + req.URL.Path
|
||||
return nil
|
||||
}
|
||||
|
||||
func serializeMiddlewareResponseIntoSSZ(data string) (sszResponse []byte, errJson gateway.ErrorJson) {
|
||||
@@ -176,7 +181,7 @@ func writeSSZResponseHeaderAndBody(grpcResp *http.Response, w http.ResponseWrite
|
||||
}
|
||||
|
||||
func handleEvents(m *gateway.ApiProxyMiddleware, _ gateway.Endpoint, w http.ResponseWriter, req *http.Request) (handled bool) {
|
||||
sseClient := sse.NewClient("http://" + m.GatewayAddress + req.URL.RequestURI())
|
||||
sseClient := sse.NewClient("http://" + m.GatewayAddress + "/internal" + req.URL.RequestURI())
|
||||
eventChan := make(chan *sse.Event)
|
||||
|
||||
// We use grpc-gateway as the server side of events, not the sse library.
|
||||
|
||||
@@ -59,7 +59,7 @@ func TestPrepareSSZRequestForProxying(t *testing.T) {
|
||||
|
||||
errJson := prepareSSZRequestForProxying(middleware, endpoint, request, "/ssz")
|
||||
require.Equal(t, true, errJson == nil)
|
||||
assert.Equal(t, "/ssz", request.URL.Path)
|
||||
assert.Equal(t, "/internal/ssz", request.URL.Path)
|
||||
}
|
||||
|
||||
func TestSerializeMiddlewareResponseIntoSSZ(t *testing.T) {
|
||||
|
||||
@@ -265,13 +265,13 @@ func serializeV2State(response interface{}) (bool, []byte, gateway.ErrorJson) {
|
||||
}
|
||||
|
||||
type phase0ProduceBlockResponseJson struct {
|
||||
Version string `json:"version"`
|
||||
Data *beaconBlockContainerJson `json:"data"`
|
||||
Version string `json:"version"`
|
||||
Data *beaconBlockJson `json:"data"`
|
||||
}
|
||||
|
||||
type altairProduceBlockResponseJson struct {
|
||||
Version string `json:"version"`
|
||||
Data *beaconBlockAltairContainerJson `json:"data"`
|
||||
Version string `json:"version"`
|
||||
Data *beaconBlockAltairJson `json:"data"`
|
||||
}
|
||||
|
||||
func serializeProducedV2Block(response interface{}) (bool, []byte, gateway.ErrorJson) {
|
||||
@@ -284,16 +284,12 @@ func serializeProducedV2Block(response interface{}) (bool, []byte, gateway.Error
|
||||
if strings.EqualFold(respContainer.Version, strings.ToLower(ethpbv2.Version_PHASE0.String())) {
|
||||
actualRespContainer = &phase0ProduceBlockResponseJson{
|
||||
Version: respContainer.Version,
|
||||
Data: &beaconBlockContainerJson{
|
||||
Message: respContainer.Data.Phase0Block,
|
||||
},
|
||||
Data: respContainer.Data.Phase0Block,
|
||||
}
|
||||
} else {
|
||||
actualRespContainer = &altairProduceBlockResponseJson{
|
||||
Version: respContainer.Version,
|
||||
Data: &beaconBlockAltairContainerJson{
|
||||
Message: respContainer.Data.AltairBlock,
|
||||
},
|
||||
Data: respContainer.Data.AltairBlock,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -403,7 +403,13 @@ func TestSerializeV2Block(t *testing.T) {
|
||||
response := &blockV2ResponseJson{
|
||||
Version: ethpbv2.Version_PHASE0.String(),
|
||||
Data: &signedBeaconBlockContainerV2Json{
|
||||
Phase0Block: &beaconBlockJson{},
|
||||
Phase0Block: &beaconBlockJson{
|
||||
Slot: "1",
|
||||
ProposerIndex: "1",
|
||||
ParentRoot: "root",
|
||||
StateRoot: "root",
|
||||
Body: &beaconBlockBodyJson{},
|
||||
},
|
||||
AltairBlock: nil,
|
||||
Signature: "sig",
|
||||
},
|
||||
@@ -412,7 +418,16 @@ func TestSerializeV2Block(t *testing.T) {
|
||||
require.Equal(t, nil, errJson)
|
||||
require.Equal(t, true, ok)
|
||||
require.NotNil(t, j)
|
||||
require.NoError(t, json.Unmarshal(j, &phase0BlockResponseJson{}))
|
||||
resp := &phase0BlockResponseJson{}
|
||||
require.NoError(t, json.Unmarshal(j, resp))
|
||||
require.NotNil(t, resp.Data)
|
||||
require.NotNil(t, resp.Data.Message)
|
||||
beaconBlock := resp.Data.Message
|
||||
assert.Equal(t, "1", beaconBlock.Slot)
|
||||
assert.Equal(t, "1", beaconBlock.ProposerIndex)
|
||||
assert.Equal(t, "root", beaconBlock.ParentRoot)
|
||||
assert.Equal(t, "root", beaconBlock.StateRoot)
|
||||
require.NotNil(t, beaconBlock.Body)
|
||||
})
|
||||
|
||||
t.Run("Altair", func(t *testing.T) {
|
||||
@@ -420,15 +435,30 @@ func TestSerializeV2Block(t *testing.T) {
|
||||
Version: ethpbv2.Version_ALTAIR.String(),
|
||||
Data: &signedBeaconBlockContainerV2Json{
|
||||
Phase0Block: nil,
|
||||
AltairBlock: &beaconBlockAltairJson{},
|
||||
Signature: "sig",
|
||||
AltairBlock: &beaconBlockAltairJson{
|
||||
Slot: "1",
|
||||
ProposerIndex: "1",
|
||||
ParentRoot: "root",
|
||||
StateRoot: "root",
|
||||
Body: &beaconBlockBodyAltairJson{},
|
||||
},
|
||||
Signature: "sig",
|
||||
},
|
||||
}
|
||||
ok, j, errJson := serializeV2Block(response)
|
||||
require.Equal(t, nil, errJson)
|
||||
require.Equal(t, true, ok)
|
||||
require.NotNil(t, j)
|
||||
require.NoError(t, json.Unmarshal(j, &altairBlockResponseJson{}))
|
||||
resp := &altairBlockResponseJson{}
|
||||
require.NoError(t, json.Unmarshal(j, resp))
|
||||
require.NotNil(t, resp.Data)
|
||||
require.NotNil(t, resp.Data.Message)
|
||||
beaconBlock := resp.Data.Message
|
||||
assert.Equal(t, "1", beaconBlock.Slot)
|
||||
assert.Equal(t, "1", beaconBlock.ProposerIndex)
|
||||
assert.Equal(t, "root", beaconBlock.ParentRoot)
|
||||
assert.Equal(t, "root", beaconBlock.StateRoot)
|
||||
require.NotNil(t, beaconBlock.Body)
|
||||
})
|
||||
|
||||
t.Run("incorrect response type", func(t *testing.T) {
|
||||
@@ -486,7 +516,13 @@ func TestSerializeProduceV2Block(t *testing.T) {
|
||||
response := &produceBlockResponseV2Json{
|
||||
Version: ethpbv2.Version_PHASE0.String(),
|
||||
Data: &beaconBlockContainerV2Json{
|
||||
Phase0Block: &beaconBlockJson{},
|
||||
Phase0Block: &beaconBlockJson{
|
||||
Slot: "1",
|
||||
ProposerIndex: "1",
|
||||
ParentRoot: "root",
|
||||
StateRoot: "root",
|
||||
Body: &beaconBlockBodyJson{},
|
||||
},
|
||||
AltairBlock: nil,
|
||||
},
|
||||
}
|
||||
@@ -494,7 +530,16 @@ func TestSerializeProduceV2Block(t *testing.T) {
|
||||
require.Equal(t, nil, errJson)
|
||||
require.Equal(t, true, ok)
|
||||
require.NotNil(t, j)
|
||||
require.NoError(t, json.Unmarshal(j, &phase0ProduceBlockResponseJson{}))
|
||||
resp := &phase0ProduceBlockResponseJson{}
|
||||
require.NoError(t, json.Unmarshal(j, resp))
|
||||
require.NotNil(t, resp.Data)
|
||||
require.NotNil(t, resp.Data)
|
||||
beaconBlock := resp.Data
|
||||
assert.Equal(t, "1", beaconBlock.Slot)
|
||||
assert.Equal(t, "1", beaconBlock.ProposerIndex)
|
||||
assert.Equal(t, "root", beaconBlock.ParentRoot)
|
||||
assert.Equal(t, "root", beaconBlock.StateRoot)
|
||||
require.NotNil(t, beaconBlock.Body)
|
||||
})
|
||||
|
||||
t.Run("Altair", func(t *testing.T) {
|
||||
@@ -502,14 +547,29 @@ func TestSerializeProduceV2Block(t *testing.T) {
|
||||
Version: ethpbv2.Version_ALTAIR.String(),
|
||||
Data: &beaconBlockContainerV2Json{
|
||||
Phase0Block: nil,
|
||||
AltairBlock: &beaconBlockAltairJson{},
|
||||
AltairBlock: &beaconBlockAltairJson{
|
||||
Slot: "1",
|
||||
ProposerIndex: "1",
|
||||
ParentRoot: "root",
|
||||
StateRoot: "root",
|
||||
Body: &beaconBlockBodyAltairJson{},
|
||||
},
|
||||
},
|
||||
}
|
||||
ok, j, errJson := serializeProducedV2Block(response)
|
||||
require.Equal(t, nil, errJson)
|
||||
require.Equal(t, true, ok)
|
||||
require.NotNil(t, j)
|
||||
require.NoError(t, json.Unmarshal(j, &altairProduceBlockResponseJson{}))
|
||||
resp := &altairProduceBlockResponseJson{}
|
||||
require.NoError(t, json.Unmarshal(j, resp))
|
||||
require.NotNil(t, resp.Data)
|
||||
require.NotNil(t, resp.Data)
|
||||
beaconBlock := resp.Data
|
||||
assert.Equal(t, "1", beaconBlock.Slot)
|
||||
assert.Equal(t, "1", beaconBlock.ProposerIndex)
|
||||
assert.Equal(t, "root", beaconBlock.ParentRoot)
|
||||
assert.Equal(t, "root", beaconBlock.StateRoot)
|
||||
require.NotNil(t, beaconBlock.Body)
|
||||
})
|
||||
|
||||
t.Run("incorrect response type", func(t *testing.T) {
|
||||
|
||||
@@ -298,10 +298,6 @@ type signedBeaconBlockContainerJson struct {
|
||||
Signature string `json:"signature" hex:"true"`
|
||||
}
|
||||
|
||||
type beaconBlockContainerJson struct {
|
||||
Message *beaconBlockJson `json:"message"`
|
||||
}
|
||||
|
||||
type beaconBlockJson struct {
|
||||
Slot string `json:"slot"`
|
||||
ProposerIndex string `json:"proposer_index"`
|
||||
@@ -337,10 +333,6 @@ type signedBeaconBlockAltairContainerJson struct {
|
||||
Signature string `json:"signature" hex:"true"`
|
||||
}
|
||||
|
||||
type beaconBlockAltairContainerJson struct {
|
||||
Message *beaconBlockAltairJson `json:"message"`
|
||||
}
|
||||
|
||||
type beaconBlockAltairJson struct {
|
||||
Slot string `json:"slot"`
|
||||
ProposerIndex string `json:"proposer_index"`
|
||||
|
||||
@@ -508,7 +508,7 @@ func handleGetBlock(blk block.SignedBeaconBlock, err error) error {
|
||||
return status.Errorf(codes.Internal, "Could not get block from block ID: %v", err)
|
||||
}
|
||||
if blk == nil || blk.IsNil() {
|
||||
return status.Errorf(codes.Internal, "Could not find requested block")
|
||||
return status.Errorf(codes.NotFound, "Could not find requested block")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user