Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eip4844: beacon api updates #12234

Merged
merged 16 commits into from
Apr 12, 2023
12 changes: 10 additions & 2 deletions beacon-chain/builder/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type MockBuilderService struct {
HasConfigured bool
Payload *v1.ExecutionPayload
PayloadCapella *v1.ExecutionPayloadCapella
PayloadDeneb *v1.ExecutionPayloadDeneb
ErrSubmitBlindedBlock error
Bid *ethpb.SignedBuilderBid
BidCapella *ethpb.SignedBuilderBidCapella
Expand All @@ -41,9 +42,16 @@ func (s *MockBuilderService) SubmitBlindedBlock(_ context.Context, _ interfaces.
}
return w, s.ErrSubmitBlindedBlock
}
w, err := blocks.WrappedExecutionPayloadCapella(s.PayloadCapella, big.NewInt(0))
if s.PayloadCapella != nil {
w, err := blocks.WrappedExecutionPayloadCapella(s.PayloadCapella, big.NewInt(0))
if err != nil {
return nil, errors.Wrap(err, "could not wrap capella payload")
}
return w, s.ErrSubmitBlindedBlock
}
w, err := blocks.WrappedExecutionPayloadDeneb(s.PayloadDeneb, big.NewInt(0))
if err != nil {
return nil, errors.Wrap(err, "could not wrap capella payload")
return nil, errors.Wrap(err, "could not wrap deneb payload")
}
return w, s.ErrSubmitBlindedBlock
}
Expand Down
190 changes: 113 additions & 77 deletions beacon-chain/rpc/apimiddleware/custom_hooks.go

Large diffs are not rendered by default.

108 changes: 92 additions & 16 deletions beacon-chain/rpc/apimiddleware/custom_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func TestSetInitialPublishBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Altair", func(t *testing.T) {
slot, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
Expand All @@ -440,7 +440,7 @@ func TestSetInitialPublishBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockAltairContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockAltairJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Bellatrix", func(t *testing.T) {
slot, err := slots.EpochStart(params.BeaconConfig().BellatrixForkEpoch)
Expand All @@ -455,7 +455,7 @@ func TestSetInitialPublishBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockBellatrixContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockBellatrixJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Capella", func(t *testing.T) {
params.SetupTestConfigCleanup(t)
Expand All @@ -475,14 +475,14 @@ func TestSetInitialPublishBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockCapellaContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockCapellaJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
}

func TestPreparePublishedBlock(t *testing.T) {
t.Run("Phase 0", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBeaconBlockContainerJson{
PostRequest: &SignedBeaconBlockJson{
Message: &BeaconBlockJson{
Body: &BeaconBlockBodyJson{},
},
Expand All @@ -496,7 +496,7 @@ func TestPreparePublishedBlock(t *testing.T) {

t.Run("Altair", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBeaconBlockAltairContainerJson{
PostRequest: &SignedBeaconBlockAltairJson{
Message: &BeaconBlockAltairJson{
Body: &BeaconBlockBodyAltairJson{},
},
Expand All @@ -510,7 +510,7 @@ func TestPreparePublishedBlock(t *testing.T) {

t.Run("Bellatrix", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBeaconBlockBellatrixContainerJson{
PostRequest: &SignedBeaconBlockBellatrixJson{
Message: &BeaconBlockBellatrixJson{
Body: &BeaconBlockBodyBellatrixJson{},
},
Expand All @@ -532,13 +532,15 @@ func TestSetInitialPublishBlindedBlockPostRequest(t *testing.T) {
params.SetupTestConfigCleanup(t)
cfg := params.BeaconConfig().Copy()
cfg.BellatrixForkEpoch = params.BeaconConfig().AltairForkEpoch + 1
cfg.CapellaForkEpoch = params.BeaconConfig().BellatrixForkEpoch + 1
cfg.DenebForkEpoch = params.BeaconConfig().CapellaForkEpoch + 1
params.OverrideBeaconConfig(cfg)

endpoint := &apimiddleware.Endpoint{}
s := struct {
Message struct {
Slot string
}
} `json:"message"`
}{}
t.Run("Phase 0", func(t *testing.T) {
s.Message = struct{ Slot string }{Slot: "0"}
Expand All @@ -551,7 +553,7 @@ func TestSetInitialPublishBlindedBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlindedBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Altair", func(t *testing.T) {
slot, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
Expand All @@ -566,7 +568,7 @@ func TestSetInitialPublishBlindedBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlindedBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockAltairContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBeaconBlockAltairJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Bellatrix", func(t *testing.T) {
slot, err := slots.EpochStart(params.BeaconConfig().BellatrixForkEpoch)
Expand All @@ -581,42 +583,87 @@ func TestSetInitialPublishBlindedBlockPostRequest(t *testing.T) {
runDefault, errJson := setInitialPublishBlindedBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBlindedBeaconBlockBellatrixContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
assert.Equal(t, reflect.TypeOf(SignedBlindedBeaconBlockBellatrixJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Capella", func(t *testing.T) {
slot, err := slots.EpochStart(params.BeaconConfig().CapellaForkEpoch)
require.NoError(t, err)
s.Message = struct{ Slot string }{Slot: strconv.FormatUint(uint64(slot), 10)}
j, err := json.Marshal(s)
require.NoError(t, err)
var body bytes.Buffer
_, err = body.Write(j)
require.NoError(t, err)
request := httptest.NewRequest("POST", "http://foo.example", &body)
runDefault, errJson := setInitialPublishBlindedBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBlindedBeaconBlockCapellaJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
t.Run("Deneb", func(t *testing.T) {
slot, err := slots.EpochStart(params.BeaconConfig().DenebForkEpoch)
require.NoError(t, err)
denebS := struct {
SignedBlindedBlock struct {
Message struct {
Slot string
} `json:"message"`
} `json:"signed_blinded_block"`
}{}
denebS.SignedBlindedBlock = struct {
Message struct {
Slot string
} `json:"message"`
}{
Message: struct {
Slot string
}{Slot: strconv.FormatUint(uint64(slot), 10)},
}
j, err := json.Marshal(denebS)
require.NoError(t, err)
var body bytes.Buffer
_, err = body.Write(j)
require.NoError(t, err)
request := httptest.NewRequest("POST", "http://foo.example", &body)
runDefault, errJson := setInitialPublishBlindedBlockPostRequest(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
assert.Equal(t, reflect.TypeOf(SignedBlindedBeaconBlockContentsDenebJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name())
})
}

func TestPreparePublishedBlindedBlock(t *testing.T) {
t.Run("Phase 0", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBeaconBlockContainerJson{
PostRequest: &SignedBeaconBlockJson{
Message: &BeaconBlockJson{
Body: &BeaconBlockBodyJson{},
},
},
}
errJson := preparePublishedBlindedBlock(endpoint, nil, nil)
require.Equal(t, true, errJson == nil)
_, ok := endpoint.PostRequest.(*phase0PublishBlockRequestJson)
_, ok := endpoint.PostRequest.(*phase0PublishBlindedBlockRequestJson)
assert.Equal(t, true, ok)
})

t.Run("Altair", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBeaconBlockAltairContainerJson{
PostRequest: &SignedBeaconBlockAltairJson{
Message: &BeaconBlockAltairJson{
Body: &BeaconBlockBodyAltairJson{},
},
},
}
errJson := preparePublishedBlindedBlock(endpoint, nil, nil)
require.Equal(t, true, errJson == nil)
_, ok := endpoint.PostRequest.(*altairPublishBlockRequestJson)
_, ok := endpoint.PostRequest.(*altairPublishBlindedBlockRequestJson)
assert.Equal(t, true, ok)
})

t.Run("Bellatrix", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBlindedBeaconBlockBellatrixContainerJson{
PostRequest: &SignedBlindedBeaconBlockBellatrixJson{
Message: &BlindedBeaconBlockBellatrixJson{
Body: &BlindedBeaconBlockBodyBellatrixJson{},
},
Expand All @@ -628,6 +675,35 @@ func TestPreparePublishedBlindedBlock(t *testing.T) {
assert.Equal(t, true, ok)
})

t.Run("Capella", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBlindedBeaconBlockCapellaJson{
Message: &BlindedBeaconBlockCapellaJson{
Body: &BlindedBeaconBlockBodyCapellaJson{},
},
},
}
errJson := preparePublishedBlindedBlock(endpoint, nil, nil)
require.Equal(t, true, errJson == nil)
_, ok := endpoint.PostRequest.(*capellaPublishBlindedBlockRequestJson)
assert.Equal(t, true, ok)
})

t.Run("Deneb", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SignedBlindedBeaconBlockContentsDenebJson{
SignedBlindedBlock: &SignedBlindedBeaconBlockDenebJson{
Message: &BlindedBeaconBlockDenebJson{},
},
SignedBlindedBlobSidecars: []*SignedBlindedBlobSidecarJson{},
},
}
errJson := preparePublishedBlindedBlock(endpoint, nil, nil)
require.Equal(t, true, errJson == nil)
_, ok := endpoint.PostRequest.(*denebPublishBlindedBlockRequestJson)
assert.Equal(t, true, ok)
})

t.Run("unsupported block type", func(t *testing.T) {
errJson := preparePublishedBlock(&apimiddleware.Endpoint{}, nil, nil)
assert.Equal(t, true, strings.Contains(errJson.Msg(), "unsupported block type"))
Expand Down
47 changes: 38 additions & 9 deletions beacon-chain/rpc/apimiddleware/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type BlockHeaderResponseJson struct {
}

type BlockResponseJson struct {
Data *SignedBeaconBlockContainerJson `json:"data"`
Data *SignedBeaconBlockJson `json:"data"`
}

type BlockV2ResponseJson struct {
Expand Down Expand Up @@ -366,7 +366,7 @@ type BlockRootContainerJson struct {
Root string `json:"root" hex:"true"`
}

type SignedBeaconBlockContainerJson struct {
type SignedBeaconBlockJson struct {
Message *BeaconBlockJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}
Expand Down Expand Up @@ -408,6 +408,14 @@ type SignedBlindedBeaconBlockContainerJson struct {
Signature string `json:"signature" hex:"true"`
}

type SignedBlindedBeaconBlockContentsContainerJson struct {
Phase0Block *SignedBeaconBlockJson `json:"phase0_block"`
AltairBlock *SignedBeaconBlockAltairJson `json:"altair_block"`
BellatrixBlock *SignedBlindedBeaconBlockBellatrixJson `json:"bellatrix_block"`
CapellaBlock *SignedBlindedBeaconBlockCapellaJson `json:"capella_block"`
DenebContents *SignedBlindedBeaconBlockContentsDenebJson `json:"deneb_contents"`
}

type BeaconBlockContainerV2Json struct {
Phase0Block *BeaconBlockJson `json:"phase0_block"`
AltairBlock *BeaconBlockAltairJson `json:"altair_block"`
Expand All @@ -424,41 +432,62 @@ type BlindedBeaconBlockContainerJson struct {
DenebBlock *BlindedBeaconBlockDenebJson `json:"deneb_block"`
}

type SignedBeaconBlockAltairContainerJson struct {
type SignedBeaconBlockAltairJson struct {
Message *BeaconBlockAltairJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBeaconBlockBellatrixContainerJson struct {
type SignedBeaconBlockBellatrixJson struct {
Message *BeaconBlockBellatrixJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBeaconBlockCapellaContainerJson struct {
type SignedBeaconBlockCapellaJson struct {
Message *BeaconBlockCapellaJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBeaconBlockDenebContainerJson struct {
type SignedBeaconBlockDenebJson struct {
Message *BeaconBlockDenebJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBlindedBeaconBlockBellatrixContainerJson struct {
type SignedBlindedBeaconBlockBellatrixJson struct {
Message *BlindedBeaconBlockBellatrixJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBlindedBeaconBlockCapellaContainerJson struct {
type SignedBlindedBeaconBlockCapellaJson struct {
Message *BlindedBeaconBlockCapellaJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBlindedBeaconBlockDenebContainerJson struct {
type SignedBlindedBeaconBlockContentsDenebJson struct {
SignedBlindedBlock *SignedBlindedBeaconBlockDenebJson `json:"signed_blinded_block"`
SignedBlindedBlobSidecars []*SignedBlindedBlobSidecarJson `json:"signed_blinded_blob_sidecars"`
}

type SignedBlindedBeaconBlockDenebJson struct {
Message *BlindedBeaconBlockDenebJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type SignedBlindedBlobSidecarJson struct {
Message *BlindedBlobSidecarJson `json:"message"`
Signature string `json:"signature" hex:"true"`
}

type BlindedBlobSidecarJson struct {
BlockRoot string `json:"block_root" hex:"true"`
Index string `json:"index"`
Slot string `json:"slot"`
BlockParentRoot string `json:"block_parent_root" hex:"true"`
ProposerIndex string `json:"proposer_index"`
BlobRoot string `json:"blob_root" hex:"true"`
KzgCommitment string `json:"kzg_commitment" hex:"true"`
KzgProof string `json:"kzg_proof" hex:"true"`
}

type BeaconBlockAltairJson struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/rpc/eth/beacon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ go_test(
"//beacon-chain/rpc/testutil:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/state-native:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types/blocks:go_default_library",
"//consensus-types/interfaces:go_default_library",
Expand Down
Loading