From fdd36ff970e32fd4c5f795fa4d8928a867c96667 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 6 Aug 2024 18:07:48 -0400 Subject: [PATCH 01/75] dispatcher --- mod/async/go.mod | 16 +++ mod/async/go.sum | 70 ++++++++++++ mod/async/pkg/dispatcher/dispatcher.go | 95 ++++++++++++++++ mod/async/pkg/dispatcher/errors.go | 28 +++++ mod/async/pkg/dispatcher/event.go | 69 +++++++++++ mod/async/pkg/dispatcher/hood_msg_server.go | 79 +++++++++++++ mod/async/pkg/dispatcher/msg.go | 76 +++++++++++++ mod/async/pkg/dispatcher/types.go | 47 ++++++++ mod/async/pkg/messaging/assert.go | 51 +++++++++ mod/async/pkg/messaging/constants.go | 38 +++++++ mod/async/pkg/messaging/errors.go | 52 +++++++++ mod/async/pkg/messaging/publisher.go | 120 ++++++++++++++++++++ mod/async/pkg/messaging/route.go | 95 ++++++++++++++++ mod/async/pkg/types/event.go | 16 +-- mod/async/pkg/types/message.go | 85 ++++++++++++++ 15 files changed, 929 insertions(+), 8 deletions(-) create mode 100644 mod/async/go.sum create mode 100644 mod/async/pkg/dispatcher/dispatcher.go create mode 100644 mod/async/pkg/dispatcher/errors.go create mode 100644 mod/async/pkg/dispatcher/event.go create mode 100644 mod/async/pkg/dispatcher/hood_msg_server.go create mode 100644 mod/async/pkg/dispatcher/msg.go create mode 100644 mod/async/pkg/dispatcher/types.go create mode 100644 mod/async/pkg/messaging/assert.go create mode 100644 mod/async/pkg/messaging/constants.go create mode 100644 mod/async/pkg/messaging/errors.go create mode 100644 mod/async/pkg/messaging/publisher.go create mode 100644 mod/async/pkg/messaging/route.go create mode 100644 mod/async/pkg/types/message.go diff --git a/mod/async/go.mod b/mod/async/go.mod index 52f1b5068b..e516897968 100644 --- a/mod/async/go.mod +++ b/mod/async/go.mod @@ -1,3 +1,19 @@ module github.com/berachain/beacon-kit/mod/async go 1.22.5 + +require github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 + +require ( + github.com/cockroachdb/errors v1.11.3 // indirect + github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect + github.com/cockroachdb/redact v1.1.5 // indirect + github.com/getsentry/sentry-go v0.28.1 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect + golang.org/x/sys v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect +) diff --git a/mod/async/go.sum b/mod/async/go.sum new file mode 100644 index 0000000000..9dcbe34e08 --- /dev/null +++ b/mod/async/go.sum @@ -0,0 +1,70 @@ +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= +github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= +github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= +github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= +github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= +github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30= +github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k= +github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg= +github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= +github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= +github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go new file mode 100644 index 0000000000..19679654a2 --- /dev/null +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package dispatcher + +import ( + "context" + + "github.com/berachain/beacon-kit/mod/async/pkg/types" +) + +// Dispatcher faciliates asyncronous communication between components, typically +// services. It acts as an API facade to the underlying event and message +// servers. +type Dispatcher struct { + eventServer *EventServer + msgServer *MessageServer +} + +// NewDispatcher creates a new dispatcher. +func NewDispatcher() *Dispatcher { + return &Dispatcher{ + eventServer: NewEventServer(), + msgServer: NewMessageServer(), + } +} + +// Start starts the dispatcher. +func (d *Dispatcher) Start(ctx context.Context) error { + d.eventServer.Start(ctx) + return nil +} + +// ============================== Events =================================== + +// RegisterEventFeed registers the given event feed with the given eventID. +// Any subsequent events with dispatched to this Dispatcher must be +// consistent with the type expected by . +func (d *Dispatcher) RegisterEventFeed(eventID types.EventID, feed publisher) { + d.eventServer.RegisterFeed(eventID, feed) +} + +// Subscribe subscribes the given channel to the event with the given . +// It will error if the channel type does not match the event type corresponding +// to the . +func (d *Dispatcher) Subscribe(eventID types.EventID, ch chan any) error { + return d.eventServer.Subscribe(eventID, ch) +} + +// DispatchEvent dispatches the given event to the event server. +func (d *Dispatcher) DispatchEvent(ctx context.Context, event *types.Event[any]) { + d.eventServer.Dispatch(ctx, event) +} + +// ================================ Messages ================================ + +// RegisterMsgRecipient registers the given channel to the message with the +// given . +func (d *Dispatcher) RegisterMsgRecipient(messageID types.MessageID, ch chan any) error { + return d.msgServer.RegisterRecipient(messageID, ch) +} + +// DispatchRequest dispatches the given request to the message server. +func (d *Dispatcher) DispatchRequest(req types.Message[any], resp any) error { + return d.msgServer.Request(req, resp) +} + +// DispatchResponse dispatches the given response to the message server. +func (d *Dispatcher) DispatchResponse(resp types.Message[any]) error { + return d.msgServer.Respond(resp) +} + +// RegisterRoute registers the given route with the given messageID. +// Any subsequent messages with sent to this Dispatcher must be +// consistent with the type expected by . +func (d *Dispatcher) RegisterMessageRoute(messageID types.MessageID, route messageRoute) { + d.msgServer.RegisterRoute(messageID, route) +} diff --git a/mod/async/pkg/dispatcher/errors.go b/mod/async/pkg/dispatcher/errors.go new file mode 100644 index 0000000000..38c0aa54f5 --- /dev/null +++ b/mod/async/pkg/dispatcher/errors.go @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package dispatcher + +import "github.com/berachain/beacon-kit/mod/errors" + +var ( + ErrFeedNotFound = errors.New("feed not found") + ErrRouteNotFound = errors.New("route not found") +) diff --git a/mod/async/pkg/dispatcher/event.go b/mod/async/pkg/dispatcher/event.go new file mode 100644 index 0000000000..df4532d1ec --- /dev/null +++ b/mod/async/pkg/dispatcher/event.go @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package dispatcher + +import ( + "context" + + "github.com/berachain/beacon-kit/mod/async/pkg/types" +) + +// EventServer asyncronously dispatches events to subscribers. +type EventServer struct { + feeds map[types.EventID]publisher +} + +// NewEventServer creates a new event server. +func NewEventServer() *EventServer { + return &EventServer{ + feeds: make(map[types.EventID]publisher), + } +} + +// Dispatch dispatches the given event to the feed with the given eventID. +func (es *EventServer) Dispatch(ctx context.Context, event *types.Event[any]) { + es.feeds[event.Type()].Publish(ctx, *event) +} + +// Subscribe subscribes the given channel to the feed with the given eventID. +// It will error if the channel type does not match the event type corresponding +// feed. +func (es *EventServer) Subscribe(eventID types.EventID, ch chan any) error { + feed, ok := es.feeds[eventID] + if !ok { + return ErrFeedNotFound + } + return feed.Subscribe(ch) +} + +// Start starts the event server. +func (es *EventServer) Start(ctx context.Context) { + for _, feed := range es.feeds { + go feed.Start(ctx) + } +} + +// RegisterFeed registers the given feed with the given eventID. +// Any subsequent events with dispatched to this EventServer must be +// consistent with the type expected by . +func (es *EventServer) RegisterFeed(eventID types.EventID, feed publisher) { + es.feeds[eventID] = feed +} diff --git a/mod/async/pkg/dispatcher/hood_msg_server.go b/mod/async/pkg/dispatcher/hood_msg_server.go new file mode 100644 index 0000000000..ef30af0108 --- /dev/null +++ b/mod/async/pkg/dispatcher/hood_msg_server.go @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package dispatcher + +import ( + "context" + "fmt" + + "github.com/berachain/beacon-kit/mod/async/pkg/types" +) + +// MsgHandler is a function that handles a message. +type MsgHandler func(req types.Message[any]) (types.Message[any], error) + +// Message server implementation with handlers +// MsgServer2 is a server for handling messages. +// don't use this, just an idea +type MsgServer2 struct { + // routes is a map of message types to the corresponding receiving channels. + routes map[types.MessageID]MsgHandler +} + +// NewMsgServer2 creates a new message server. +func NewMsgServer2() *MsgServer2 { + return &MsgServer2{ + routes: make(map[types.MessageID]MsgHandler), + } +} + +// RegisterHandler registers a handler for a message type. +func (s *MsgServer2) RegisterHandler(messageType types.MessageID, handler MsgHandler) { + s.routes[messageType] = handler +} + +// DispatchAndAwait dispatches a message to the server and returns the response. +func (s *MsgServer2) DispatchAndAwait(ctx context.Context, msg types.Message[any]) (types.Message[any], error) { + handler, ok := s.routes[msg.ID()] + if !ok { + return types.Message[any]{}, fmt.Errorf("no handler found for message type %s", msg.ID()) + } + return handler(msg) +} + +// Dispatch dispatches a message to the server and handles the response. +func (s *MsgServer2) Dispatch( + ctx context.Context, + msg types.Message[any], + handleResp types.ResponseHandler[any], + +) error { + handler, ok := s.routes[msg.ID()] + if !ok { + return fmt.Errorf("no handler found for message type %s", msg.ID()) + } + resp, err := handler(msg) + if err != nil { + return err + } + handleResp(resp) + return nil +} diff --git a/mod/async/pkg/dispatcher/msg.go b/mod/async/pkg/dispatcher/msg.go new file mode 100644 index 0000000000..b63ddf6a30 --- /dev/null +++ b/mod/async/pkg/dispatcher/msg.go @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package dispatcher + +import ( + "github.com/berachain/beacon-kit/mod/async/pkg/types" +) + +type MessageServer struct { + routes map[types.MessageID]messageRoute +} + +func NewMessageServer() *MessageServer { + return &MessageServer{ + routes: make(map[types.MessageID]messageRoute), + } +} + +// Request sends a message to the server and awaits for a response. +// The response is written to the provided response pointer. +func (ms *MessageServer) Request(req types.Message[any], resp any) error { + // send message to request channel and await a response in the response channel + route, ok := ms.routes[req.ID()] + if !ok { + return ErrRouteNotFound + } + route.SendRequest(req) + return route.AwaitResponse(resp) +} + +// Respond sends a response to the route that corresponds to the response's +// messageID. +func (ms *MessageServer) Respond(resp types.Message[any]) error { + route, ok := ms.routes[resp.ID()] + if !ok { + return ErrRouteNotFound + } + route.SendResponse(resp) + return nil +} + +// RegisterRoute registers the route with the given messageID. +// Any subsequent messages with sent to this MessageServer must be +// consistent with the type expected by . +func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route messageRoute) { + ms.routes[messageID] = route +} + +// SetRecipient sets the recipient for the route with the given messageID. +// Errors if the route with the given messageID is not found or the route +// already has a registered recipient. +func (ms *MessageServer) RegisterRecipient(messageID types.MessageID, ch chan any) error { + route, ok := ms.routes[messageID] + if !ok { + return ErrRouteNotFound + } + return route.RegisterRecipient(ch) +} diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go new file mode 100644 index 0000000000..2baa7edb6c --- /dev/null +++ b/mod/async/pkg/dispatcher/types.go @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package dispatcher + +import "context" + +// publisher is the interface that supports basic event feed operations. +type publisher interface { + // Start starts the event feed. + Start(ctx context.Context) + // Publish publishes the given event to the event feed. + Publish(ctx context.Context, event any) + // Subscribe subscribes the given channel to the event feed. + Subscribe(ch any) error + // Unsubscribe unsubscribes the given channel from the event feed. + Unsubscribe(ch any) error +} + +// messageRoute is the interface that supports basic message route operations. +type messageRoute interface { + // RegisterRecipient sets the recipient for the route. + RegisterRecipient(ch chan any) error + // SendRequest sends a request to the recipient. + SendRequest(msg any) error + // SendResponse sends a response to the recipient. + SendResponse(msg any) error + // AwaitResponse awaits a response from the route. + AwaitResponse(emptyResp any) error +} diff --git a/mod/async/pkg/messaging/assert.go b/mod/async/pkg/messaging/assert.go new file mode 100644 index 0000000000..d3a1306418 --- /dev/null +++ b/mod/async/pkg/messaging/assert.go @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package messaging + +import ( + "fmt" +) + +// ensureType ensures that the provided entity is of type T. +// It returns a typed entity or an error if the type is not correct. +func ensureType[T any](e any) (T, error) { + typedE, ok := e.(T) + if !ok { + return *new(T), fmt.Errorf("provided message is not of type %T", *new(T)) + } + return typedE, nil +} + +// assignToResponse assigns the received response to the assignee. +func assignToResponse[RespT any](receivedResponse RespT, emptyAssignee any) error { + // ensure that the assignee is a pointer to the response type + assigneePtr, ok := emptyAssignee.(**RespT) + if !ok { + return errIncompatibleAssigneeType(receivedResponse, emptyAssignee) + } + // ensure that the assignee pointer is not nil + if assigneePtr == nil { + return errIncompatibleAssignee(receivedResponse, emptyAssignee) + } + // assign the received response to the assignee + *assigneePtr = &receivedResponse + return nil +} diff --git a/mod/async/pkg/messaging/constants.go b/mod/async/pkg/messaging/constants.go new file mode 100644 index 0000000000..17a946e993 --- /dev/null +++ b/mod/async/pkg/messaging/constants.go @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package messaging + +import "time" + +const ( + // defaultPublisherTimeout specifies the default timeout when the broker + // tries to send a message to a client, a message is published to the + // broker, or a client subscribes or unsubscribes. + defaultPublisherTimeout = time.Second + + // defaultRouterTimeout specifies the default timeout when the router + // tries to send a message to a client, a message is published to the + // router, or a client subscribes or unsubscribes. + defaultRouterTimeout = time.Second + + // defaultBufferSize specifies the default size of the message buffer. + defaultBufferSize = 10 +) diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go new file mode 100644 index 0000000000..e06272d238 --- /dev/null +++ b/mod/async/pkg/messaging/errors.go @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package messaging + +import ( + "reflect" + + "github.com/berachain/beacon-kit/mod/errors" +) + +// ErrTimeout is the error returned when a broker operation timed out. +var ( + ErrTimeout = errors.New("timeout") + + ErrRouteAlreadySet = errors.New("route already set") + + errIncompatibleAssigneeType = func(assigner interface{}, assignee interface{}) error { + assignerType := reflect.TypeOf(assigner) + assigneeType := reflect.TypeOf(assignee) + return errors.Newf( + "incompatible assignee, expected: %T, received: %T", + assignerType, + assigneeType, + ) + } + + errIncompatibleAssignee = func(assigner interface{}, assignee interface{}) error { + return errors.Newf( + "incompatible assignee, expected: %T, received: %T", + assigner, + assignee, + ) + } +) diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go new file mode 100644 index 0000000000..d989c22cd0 --- /dev/null +++ b/mod/async/pkg/messaging/publisher.go @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package messaging + +import ( + "context" + "sync" + "time" +) + +// Publisher broadcasts msgs to registered clients. +type Publisher[T any] struct { + // clients is a map of subscribed clients. + clients map[chan T]struct{} + // msgs is the channel for publishing new messages. + msgs chan T + // timeout is the timeout for sending a msg to a client. + timeout time.Duration + // mu is the mutex for the clients map. + mu sync.Mutex +} + +// NewPublisher creates a new b. +func NewPublisher[T any](name string) *Publisher[T] { + return &Publisher[T]{ + clients: make(map[chan T]struct{}), + msgs: make(chan T, defaultBufferSize), + timeout: defaultPublisherTimeout, + mu: sync.Mutex{}, + } +} + +// Start starts the broker loop. +func (b *Publisher[T]) Start(ctx context.Context) { + go b.start(ctx) +} + +// start starts the broker loop. +func (b *Publisher[T]) start(ctx context.Context) { + for { + select { + case <-ctx.Done(): + // close all leftover clients and break the broker loop + for client := range b.clients { + b.Unsubscribe(client) + } + return + case msg := <-b.msgs: + // broadcast published msg to all clients + for client := range b.clients { + // send msg to client (or discard msg after timeout) + select { + case client <- msg: + case <-time.After(b.timeout): + } + } + } + } +} + +// Publish publishes a msg to the b. +// Returns ErrTimeout on timeout. +func (b *Publisher[T]) Publish(ctx context.Context, msg any) error { + typedMsg, err := ensureType[T](msg) + if err != nil { + return err + } + select { + case b.msgs <- typedMsg: + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + +// Subscribe registers the provided channel to the publisher, +// Returns ErrTimeout on timeout. +// TODO: see if its possible to accept a channel instead of any +func (b *Publisher[T]) Subscribe(ch any) error { + client, err := ensureType[chan T](ch) + if err != nil { + return err + } + b.mu.Lock() + defer b.mu.Unlock() + b.clients[client] = struct{}{} + return nil +} + +// Unsubscribe removes a client from the publisher. +// Returns an error if the provided channel is not of type chan T. +func (f *Publisher[T]) Unsubscribe(ch any) error { + client, err := ensureType[chan T](ch) + if err != nil { + return err + } + f.mu.Lock() + defer f.mu.Unlock() + delete(f.clients, client) + close(client) + return nil +} diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go new file mode 100644 index 0000000000..c3a10f8925 --- /dev/null +++ b/mod/async/pkg/messaging/route.go @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package messaging + +import ( + "sync" + "time" + + "github.com/berachain/beacon-kit/mod/async/pkg/types" +) + +// Route represents a communication route to a single recipient. +// Invariant: there is exactly no more than one route for each messageID. +type Route[ReqT any, RespT any] struct { + messageID types.MessageID + recipient chan ReqT + responseCh chan RespT + timeout time.Duration + mu sync.Mutex +} + +// NewRoute creates a new route. +func NewRoute[ReqT any, RespT any]( + recipient chan ReqT, + messageID types.MessageID, +) *Route[ReqT, RespT] { + return &Route[ReqT, RespT]{ + messageID: messageID, + responseCh: make(chan RespT), + timeout: defaultRouterTimeout, + mu: sync.Mutex{}, + } +} + +// SetRecipient sets the recipient for the route. +func (r *Route[ReqT, RespT]) RegisterRecipient(ch chan any) error { + if r.recipient != nil { + return ErrRouteAlreadySet + } + typedCh, err := ensureType[chan ReqT](ch) + if err != nil { + return err + } + r.recipient = typedCh + return nil +} + +// SendRequest sends a request to the recipient. +func (r *Route[ReqT, RespT]) SendRequest(msg any) error { + typedMsg, err := ensureType[ReqT](msg) + if err != nil { + return err + } + r.recipient <- typedMsg + return nil +} + +// SendResponse sends a response to the response channel. +func (r *Route[ReqT, RespT]) SendResponse(msg any) error { + typedMsg, err := ensureType[RespT](msg) + if err != nil { + return err + } + r.responseCh <- typedMsg + return nil +} + +// AwaitResponse listens for a response and returns it if it is available +// before the timeout. Otherwise, it returns ErrTimeout. +func (r *Route[ReqT, RespT]) AwaitResponse(emptyResp any) error { + select { + case msg := <-r.responseCh: + return assignToResponse[RespT](msg, emptyResp) + case <-time.After(r.timeout): + return ErrTimeout + } +} diff --git a/mod/async/pkg/types/event.go b/mod/async/pkg/types/event.go index 51238cec51..f218d44526 100644 --- a/mod/async/pkg/types/event.go +++ b/mod/async/pkg/types/event.go @@ -32,8 +32,8 @@ type EventID string type Event[DataT any] struct { // ctx is the context associated with the event. ctx context.Context - // eventType is the name of the event. - eventType EventID + // id is the name of the event. + id EventID // event is the actual beacon event. data DataT // error is the error associated with the event. @@ -47,16 +47,16 @@ func NewEvent[ ctx context.Context, eventType EventID, data DataT, errs ...error, ) *Event[DataT] { return &Event[DataT]{ - ctx: ctx, - eventType: eventType, - data: data, - err: errors.Join(errs...), + ctx: ctx, + id: eventType, + data: data, + err: errors.Join(errs...), } } // Type returns the type of the event. func (e Event[DataT]) Type() EventID { - return e.eventType + return e.id } // Context returns the context associated with the event. @@ -76,5 +76,5 @@ func (e Event[DataT]) Error() error { // Is returns true if the event has the given type. func (e Event[DataT]) Is(eventType EventID) bool { - return e.eventType == eventType + return e.id == eventType } diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go new file mode 100644 index 0000000000..79580c6ed4 --- /dev/null +++ b/mod/async/pkg/types/message.go @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package types + +import ( + "context" + "errors" +) + +// MessageHandler is a function that handles a message and returns a response message. +type MessageHandler[DataT any] func(req Message[DataT]) (resp Message[DataT], err error) + +type ResponseHandler[DataT any] func(resp Message[DataT]) any + +// MessageID represents the type of a message. +type MessageID string + +// A Message is an asynchronous message meant for a single recipient. +type Message[DataT any] struct { + // ctx is the context associated with the event. + ctx context.Context + // id is the name of the event. + id MessageID + // event is the actual beacon event. + data DataT + // err is the error associated with the event. + err error +} + +// NewEvent creates a new Event with the given context and beacon event. +func NewMessage[ + DataT any, +]( + ctx context.Context, messageType MessageID, data DataT, errs ...error, +) *Message[DataT] { + return &Message[DataT]{ + ctx: ctx, + id: messageType, + data: data, + err: errors.Join(errs...), + } +} + +// ID returns the ID of the event. +func (m Message[DataT]) ID() MessageID { + return m.id +} + +// Context returns the context associated with the event. +func (m Message[DataT]) Context() context.Context { + return m.ctx +} + +// Data returns the data associated with the event. +func (m Message[DataT]) Data() DataT { + return m.data +} + +// Error returns the error associated with the event. +func (m Message[DataT]) Error() error { + return m.err +} + +// Is returns true if the event has the given type. +func (m Message[DataT]) Is(messageType MessageID) bool { + return m.id == messageType +} From 9ddb355478ec4122069b3d4fead9049e4e05a33d Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 6 Aug 2024 18:52:57 -0400 Subject: [PATCH 02/75] reorg --- mod/async/pkg/dispatcher/dispatcher.go | 21 ++--- mod/async/pkg/dispatcher/hood_msg_server.go | 79 ------------------ mod/async/pkg/dispatcher/types.go | 40 +++++----- .../pkg/{dispatcher => server}/errors.go | 2 +- mod/async/pkg/{dispatcher => server}/event.go | 14 ++-- mod/async/pkg/{dispatcher => server}/msg.go | 2 +- mod/async/pkg/server/types.go | 47 +++++++++++ mod/async/pkg/types/event.go | 80 ------------------- mod/async/pkg/types/message.go | 39 ++++++--- 9 files changed, 113 insertions(+), 211 deletions(-) delete mode 100644 mod/async/pkg/dispatcher/hood_msg_server.go rename mod/async/pkg/{dispatcher => server}/errors.go (98%) rename mod/async/pkg/{dispatcher => server}/event.go (86%) rename mod/async/pkg/{dispatcher => server}/msg.go (99%) create mode 100644 mod/async/pkg/server/types.go delete mode 100644 mod/async/pkg/types/event.go diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 19679654a2..239ed492ea 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -26,19 +26,22 @@ import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" ) -// Dispatcher faciliates asyncronous communication between components, typically +// Dispatcher faciliates asynchronous communication between components, typically // services. It acts as an API facade to the underlying event and message // servers. type Dispatcher struct { - eventServer *EventServer - msgServer *MessageServer + eventServer EventServer + msgServer MessageServer } // NewDispatcher creates a new dispatcher. -func NewDispatcher() *Dispatcher { +func NewDispatcher( + eventServer EventServer, + msgServer MessageServer, +) *Dispatcher { return &Dispatcher{ - eventServer: NewEventServer(), - msgServer: NewMessageServer(), + eventServer: eventServer, + msgServer: msgServer, } } @@ -53,19 +56,19 @@ func (d *Dispatcher) Start(ctx context.Context) error { // RegisterEventFeed registers the given event feed with the given eventID. // Any subsequent events with dispatched to this Dispatcher must be // consistent with the type expected by . -func (d *Dispatcher) RegisterEventFeed(eventID types.EventID, feed publisher) { +func (d *Dispatcher) RegisterEventFeed(eventID types.MessageID, feed publisher) { d.eventServer.RegisterFeed(eventID, feed) } // Subscribe subscribes the given channel to the event with the given . // It will error if the channel type does not match the event type corresponding // to the . -func (d *Dispatcher) Subscribe(eventID types.EventID, ch chan any) error { +func (d *Dispatcher) Subscribe(eventID types.MessageID, ch chan any) error { return d.eventServer.Subscribe(eventID, ch) } // DispatchEvent dispatches the given event to the event server. -func (d *Dispatcher) DispatchEvent(ctx context.Context, event *types.Event[any]) { +func (d *Dispatcher) DispatchEvent(ctx context.Context, event *types.Message[any]) { d.eventServer.Dispatch(ctx, event) } diff --git a/mod/async/pkg/dispatcher/hood_msg_server.go b/mod/async/pkg/dispatcher/hood_msg_server.go deleted file mode 100644 index ef30af0108..0000000000 --- a/mod/async/pkg/dispatcher/hood_msg_server.go +++ /dev/null @@ -1,79 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package dispatcher - -import ( - "context" - "fmt" - - "github.com/berachain/beacon-kit/mod/async/pkg/types" -) - -// MsgHandler is a function that handles a message. -type MsgHandler func(req types.Message[any]) (types.Message[any], error) - -// Message server implementation with handlers -// MsgServer2 is a server for handling messages. -// don't use this, just an idea -type MsgServer2 struct { - // routes is a map of message types to the corresponding receiving channels. - routes map[types.MessageID]MsgHandler -} - -// NewMsgServer2 creates a new message server. -func NewMsgServer2() *MsgServer2 { - return &MsgServer2{ - routes: make(map[types.MessageID]MsgHandler), - } -} - -// RegisterHandler registers a handler for a message type. -func (s *MsgServer2) RegisterHandler(messageType types.MessageID, handler MsgHandler) { - s.routes[messageType] = handler -} - -// DispatchAndAwait dispatches a message to the server and returns the response. -func (s *MsgServer2) DispatchAndAwait(ctx context.Context, msg types.Message[any]) (types.Message[any], error) { - handler, ok := s.routes[msg.ID()] - if !ok { - return types.Message[any]{}, fmt.Errorf("no handler found for message type %s", msg.ID()) - } - return handler(msg) -} - -// Dispatch dispatches a message to the server and handles the response. -func (s *MsgServer2) Dispatch( - ctx context.Context, - msg types.Message[any], - handleResp types.ResponseHandler[any], - -) error { - handler, ok := s.routes[msg.ID()] - if !ok { - return fmt.Errorf("no handler found for message type %s", msg.ID()) - } - resp, err := handler(msg) - if err != nil { - return err - } - handleResp(resp) - return nil -} diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 2baa7edb6c..d85177e8ed 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -1,26 +1,24 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - package dispatcher -import "context" +import ( + "context" + + "github.com/berachain/beacon-kit/mod/async/pkg/types" +) + +type MessageServer interface { + RegisterRecipient(mID types.MessageID, ch chan any) error + Request(req types.Message[any], resp any) error + Respond(resp types.Message[any]) error + RegisterRoute(mID types.MessageID, route messageRoute) error +} + +type EventServer interface { + Start(ctx context.Context) + RegisterFeed(mID types.MessageID, feed publisher) + Subscribe(mID types.MessageID, ch chan any) error + Dispatch(ctx context.Context, event *types.Message[any]) +} // publisher is the interface that supports basic event feed operations. type publisher interface { diff --git a/mod/async/pkg/dispatcher/errors.go b/mod/async/pkg/server/errors.go similarity index 98% rename from mod/async/pkg/dispatcher/errors.go rename to mod/async/pkg/server/errors.go index 38c0aa54f5..cc5caa28f9 100644 --- a/mod/async/pkg/dispatcher/errors.go +++ b/mod/async/pkg/server/errors.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package dispatcher +package server import "github.com/berachain/beacon-kit/mod/errors" diff --git a/mod/async/pkg/dispatcher/event.go b/mod/async/pkg/server/event.go similarity index 86% rename from mod/async/pkg/dispatcher/event.go rename to mod/async/pkg/server/event.go index df4532d1ec..160d1be960 100644 --- a/mod/async/pkg/dispatcher/event.go +++ b/mod/async/pkg/server/event.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package dispatcher +package server import ( "context" @@ -28,25 +28,25 @@ import ( // EventServer asyncronously dispatches events to subscribers. type EventServer struct { - feeds map[types.EventID]publisher + feeds map[types.MessageID]publisher } // NewEventServer creates a new event server. func NewEventServer() *EventServer { return &EventServer{ - feeds: make(map[types.EventID]publisher), + feeds: make(map[types.MessageID]publisher), } } // Dispatch dispatches the given event to the feed with the given eventID. -func (es *EventServer) Dispatch(ctx context.Context, event *types.Event[any]) { - es.feeds[event.Type()].Publish(ctx, *event) +func (es *EventServer) Dispatch(ctx context.Context, event *types.Message[any]) { + es.feeds[event.ID()].Publish(ctx, *event) } // Subscribe subscribes the given channel to the feed with the given eventID. // It will error if the channel type does not match the event type corresponding // feed. -func (es *EventServer) Subscribe(eventID types.EventID, ch chan any) error { +func (es *EventServer) Subscribe(eventID types.MessageID, ch chan any) error { feed, ok := es.feeds[eventID] if !ok { return ErrFeedNotFound @@ -64,6 +64,6 @@ func (es *EventServer) Start(ctx context.Context) { // RegisterFeed registers the given feed with the given eventID. // Any subsequent events with dispatched to this EventServer must be // consistent with the type expected by . -func (es *EventServer) RegisterFeed(eventID types.EventID, feed publisher) { +func (es *EventServer) RegisterFeed(eventID types.MessageID, feed publisher) { es.feeds[eventID] = feed } diff --git a/mod/async/pkg/dispatcher/msg.go b/mod/async/pkg/server/msg.go similarity index 99% rename from mod/async/pkg/dispatcher/msg.go rename to mod/async/pkg/server/msg.go index b63ddf6a30..edcf0d18f4 100644 --- a/mod/async/pkg/dispatcher/msg.go +++ b/mod/async/pkg/server/msg.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package dispatcher +package server import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" diff --git a/mod/async/pkg/server/types.go b/mod/async/pkg/server/types.go new file mode 100644 index 0000000000..78cf70f4be --- /dev/null +++ b/mod/async/pkg/server/types.go @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package server + +import "context" + +// publisher is the interface that supports basic event feed operations. +type publisher interface { + // Start starts the event feed. + Start(ctx context.Context) + // Publish publishes the given event to the event feed. + Publish(ctx context.Context, event any) + // Subscribe subscribes the given channel to the event feed. + Subscribe(ch any) error + // Unsubscribe unsubscribes the given channel from the event feed. + Unsubscribe(ch any) error +} + +// messageRoute is the interface that supports basic message route operations. +type messageRoute interface { + // RegisterRecipient sets the recipient for the route. + RegisterRecipient(ch chan any) error + // SendRequest sends a request to the recipient. + SendRequest(msg any) error + // SendResponse sends a response to the recipient. + SendResponse(msg any) error + // AwaitResponse awaits a response from the route. + AwaitResponse(emptyResp any) error +} diff --git a/mod/async/pkg/types/event.go b/mod/async/pkg/types/event.go deleted file mode 100644 index f218d44526..0000000000 --- a/mod/async/pkg/types/event.go +++ /dev/null @@ -1,80 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package types - -import ( - "context" - "errors" -) - -// EventID represents the type of an event. -type EventID string - -// Event represents a generic event in the beacon chain. -type Event[DataT any] struct { - // ctx is the context associated with the event. - ctx context.Context - // id is the name of the event. - id EventID - // event is the actual beacon event. - data DataT - // error is the error associated with the event. - err error -} - -// NewEvent creates a new Event with the given context and beacon event. -func NewEvent[ - DataT any, -]( - ctx context.Context, eventType EventID, data DataT, errs ...error, -) *Event[DataT] { - return &Event[DataT]{ - ctx: ctx, - id: eventType, - data: data, - err: errors.Join(errs...), - } -} - -// Type returns the type of the event. -func (e Event[DataT]) Type() EventID { - return e.id -} - -// Context returns the context associated with the event. -func (e Event[DataT]) Context() context.Context { - return e.ctx -} - -// Data returns the data associated with the event. -func (e Event[DataT]) Data() DataT { - return e.data -} - -// Error returns the error associated with the event. -func (e Event[DataT]) Error() error { - return e.err -} - -// Is returns true if the event has the given type. -func (e Event[DataT]) Is(eventType EventID) bool { - return e.id == eventType -} diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index 79580c6ed4..e5c61cba36 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -25,14 +25,6 @@ import ( "errors" ) -// MessageHandler is a function that handles a message and returns a response message. -type MessageHandler[DataT any] func(req Message[DataT]) (resp Message[DataT], err error) - -type ResponseHandler[DataT any] func(resp Message[DataT]) any - -// MessageID represents the type of a message. -type MessageID string - // A Message is an asynchronous message meant for a single recipient. type Message[DataT any] struct { // ctx is the context associated with the event. @@ -45,6 +37,16 @@ type Message[DataT any] struct { err error } +// MessageID represents the type of a message. +type MessageID string + +// EventID is a type alias for a MessageID. +type EventID = MessageID + +// Event acts as a type alias for a Message that is meant to be broadcasted +// to all subscribers. +type Event[DataT any] struct{ Message[DataT] } + // NewEvent creates a new Event with the given context and beacon event. func NewMessage[ DataT any, @@ -59,27 +61,38 @@ func NewMessage[ } } +// NewEvent creates a new Event with the given context and beacon event. +func NewEvent[ + DataT any, +]( + ctx context.Context, messageType EventID, data DataT, errs ...error, +) *Event[DataT] { + return &Event[DataT]{ + Message: *NewMessage(ctx, messageType, data, errs...), + } +} + // ID returns the ID of the event. -func (m Message[DataT]) ID() MessageID { +func (m *Message[DataT]) ID() MessageID { return m.id } // Context returns the context associated with the event. -func (m Message[DataT]) Context() context.Context { +func (m *Message[DataT]) Context() context.Context { return m.ctx } // Data returns the data associated with the event. -func (m Message[DataT]) Data() DataT { +func (m *Message[DataT]) Data() DataT { return m.data } // Error returns the error associated with the event. -func (m Message[DataT]) Error() error { +func (m *Message[DataT]) Error() error { return m.err } // Is returns true if the event has the given type. -func (m Message[DataT]) Is(messageType MessageID) bool { +func (m *Message[DataT]) Is(messageType MessageID) bool { return m.id == messageType } From 6d6c6f319896a559958c56b996fec17dc0cff160 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 00:08:20 -0400 Subject: [PATCH 03/75] sleep --- mod/async/pkg/dispatcher/dispatcher.go | 14 +-- mod/async/pkg/dispatcher/types.go | 50 +++++----- mod/async/pkg/messaging/publisher.go | 55 ++++++----- mod/async/pkg/messaging/route.go | 6 +- mod/async/pkg/server/errors.go | 12 ++- mod/async/pkg/server/event.go | 20 ++-- mod/async/pkg/server/msg.go | 10 +- .../{server/types.go => types/notifiers.go} | 8 +- mod/beacon/blockchain/service.go | 4 +- mod/beacon/validator/service.go | 2 +- mod/da/pkg/da/service.go | 2 +- mod/node-core/pkg/components/dispatcher.go | 41 ++++++++ mod/node-core/pkg/components/events.go | 50 ++++++++++ mod/node-core/pkg/components/messages.go | 79 +++++++++++++++ mod/node-core/pkg/components/middleware.go | 23 ++--- mod/node-core/pkg/components/types.go | 29 ++++++ mod/primitives/pkg/events/events.go | 17 +++- mod/runtime/pkg/middleware/abci.go | 58 +++-------- mod/runtime/pkg/middleware/middleware.go | 96 +++++++++---------- 19 files changed, 382 insertions(+), 194 deletions(-) rename mod/async/pkg/{server/types.go => types/notifiers.go} (93%) create mode 100644 mod/node-core/pkg/components/dispatcher.go create mode 100644 mod/node-core/pkg/components/events.go create mode 100644 mod/node-core/pkg/components/messages.go diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 239ed492ea..73606a7673 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -53,11 +53,11 @@ func (d *Dispatcher) Start(ctx context.Context) error { // ============================== Events =================================== -// RegisterEventFeed registers the given event feed with the given eventID. +// RegisterPublisher registers the given publisher with the given eventID. // Any subsequent events with dispatched to this Dispatcher must be -// consistent with the type expected by . -func (d *Dispatcher) RegisterEventFeed(eventID types.MessageID, feed publisher) { - d.eventServer.RegisterFeed(eventID, feed) +// consistent with the type expected by . +func (d *Dispatcher) RegisterPublisher(eventID types.MessageID, publisher types.Publisher) { + d.eventServer.RegisterPublisher(eventID, publisher) } // Subscribe subscribes the given channel to the event with the given . @@ -68,8 +68,8 @@ func (d *Dispatcher) Subscribe(eventID types.MessageID, ch chan any) error { } // DispatchEvent dispatches the given event to the event server. -func (d *Dispatcher) DispatchEvent(ctx context.Context, event *types.Message[any]) { - d.eventServer.Dispatch(ctx, event) +func (d *Dispatcher) DispatchEvent(ctx context.Context, event *types.Message[any]) error { + return d.eventServer.Publish(ctx, event) } // ================================ Messages ================================ @@ -93,6 +93,6 @@ func (d *Dispatcher) DispatchResponse(resp types.Message[any]) error { // RegisterRoute registers the given route with the given messageID. // Any subsequent messages with sent to this Dispatcher must be // consistent with the type expected by . -func (d *Dispatcher) RegisterMessageRoute(messageID types.MessageID, route messageRoute) { +func (d *Dispatcher) RegisterRoute(messageID types.MessageID, route types.MessageRoute) { d.msgServer.RegisterRoute(messageID, route) } diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index d85177e8ed..d7f8470b1c 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -1,3 +1,23 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + package dispatcher import ( @@ -10,36 +30,12 @@ type MessageServer interface { RegisterRecipient(mID types.MessageID, ch chan any) error Request(req types.Message[any], resp any) error Respond(resp types.Message[any]) error - RegisterRoute(mID types.MessageID, route messageRoute) error + RegisterRoute(mID types.MessageID, route types.MessageRoute) error } type EventServer interface { Start(ctx context.Context) - RegisterFeed(mID types.MessageID, feed publisher) + RegisterPublisher(mID types.MessageID, publisher types.Publisher) Subscribe(mID types.MessageID, ch chan any) error - Dispatch(ctx context.Context, event *types.Message[any]) -} - -// publisher is the interface that supports basic event feed operations. -type publisher interface { - // Start starts the event feed. - Start(ctx context.Context) - // Publish publishes the given event to the event feed. - Publish(ctx context.Context, event any) - // Subscribe subscribes the given channel to the event feed. - Subscribe(ch any) error - // Unsubscribe unsubscribes the given channel from the event feed. - Unsubscribe(ch any) error -} - -// messageRoute is the interface that supports basic message route operations. -type messageRoute interface { - // RegisterRecipient sets the recipient for the route. - RegisterRecipient(ch chan any) error - // SendRequest sends a request to the recipient. - SendRequest(msg any) error - // SendResponse sends a response to the recipient. - SendResponse(msg any) error - // AwaitResponse awaits a response from the route. - AwaitResponse(emptyResp any) error + Publish(ctx context.Context, event *types.Message[any]) error } diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index d989c22cd0..cb423bb220 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -24,10 +24,14 @@ import ( "context" "sync" "time" + + "github.com/berachain/beacon-kit/mod/async/pkg/types" ) -// Publisher broadcasts msgs to registered clients. +// Publisher is responsible for broadcasting all events corresponding to the +// to all registered client channels. type Publisher[T any] struct { + eventID types.EventID // clients is a map of subscribed clients. clients map[chan T]struct{} // msgs is the channel for publishing new messages. @@ -39,8 +43,9 @@ type Publisher[T any] struct { } // NewPublisher creates a new b. -func NewPublisher[T any](name string) *Publisher[T] { +func NewPublisher[T any](eventID string) *Publisher[T] { return &Publisher[T]{ + eventID: types.EventID(eventID), clients: make(map[chan T]struct{}), msgs: make(chan T, defaultBufferSize), timeout: defaultPublisherTimeout, @@ -48,28 +53,32 @@ func NewPublisher[T any](name string) *Publisher[T] { } } -// Start starts the broker loop. -func (b *Publisher[T]) Start(ctx context.Context) { - go b.start(ctx) +func (p *Publisher[T]) EventID() types.EventID { + return p.eventID +} + +// Start starts the publisher loop. +func (p *Publisher[T]) Start(ctx context.Context) { + go p.start(ctx) } -// start starts the broker loop. -func (b *Publisher[T]) start(ctx context.Context) { +// start starts the publisher loop. +func (p *Publisher[T]) start(ctx context.Context) { for { select { case <-ctx.Done(): - // close all leftover clients and break the broker loop - for client := range b.clients { - b.Unsubscribe(client) + // close all leftover clients and break the publisher loop + for client := range p.clients { + p.Unsubscribe(client) } return - case msg := <-b.msgs: + case msg := <-p.msgs: // broadcast published msg to all clients - for client := range b.clients { + for client := range p.clients { // send msg to client (or discard msg after timeout) select { case client <- msg: - case <-time.After(b.timeout): + case <-time.After(p.timeout): } } } @@ -78,13 +87,13 @@ func (b *Publisher[T]) start(ctx context.Context) { // Publish publishes a msg to the b. // Returns ErrTimeout on timeout. -func (b *Publisher[T]) Publish(ctx context.Context, msg any) error { +func (p *Publisher[T]) Publish(ctx context.Context, msg any) error { typedMsg, err := ensureType[T](msg) if err != nil { return err } select { - case b.msgs <- typedMsg: + case p.msgs <- typedMsg: return nil case <-ctx.Done(): return ctx.Err() @@ -94,27 +103,27 @@ func (b *Publisher[T]) Publish(ctx context.Context, msg any) error { // Subscribe registers the provided channel to the publisher, // Returns ErrTimeout on timeout. // TODO: see if its possible to accept a channel instead of any -func (b *Publisher[T]) Subscribe(ch any) error { +func (p *Publisher[T]) Subscribe(ch any) error { client, err := ensureType[chan T](ch) if err != nil { return err } - b.mu.Lock() - defer b.mu.Unlock() - b.clients[client] = struct{}{} + p.mu.Lock() + defer p.mu.Unlock() + p.clients[client] = struct{}{} return nil } // Unsubscribe removes a client from the publisher. // Returns an error if the provided channel is not of type chan T. -func (f *Publisher[T]) Unsubscribe(ch any) error { +func (p *Publisher[T]) Unsubscribe(ch any) error { client, err := ensureType[chan T](ch) if err != nil { return err } - f.mu.Lock() - defer f.mu.Unlock() - delete(f.clients, client) + p.mu.Lock() + defer p.mu.Unlock() + delete(p.clients, client) close(client) return nil } diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index c3a10f8925..1ab75ee29e 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -39,7 +39,6 @@ type Route[ReqT any, RespT any] struct { // NewRoute creates a new route. func NewRoute[ReqT any, RespT any]( - recipient chan ReqT, messageID types.MessageID, ) *Route[ReqT, RespT] { return &Route[ReqT, RespT]{ @@ -50,6 +49,11 @@ func NewRoute[ReqT any, RespT any]( } } +// MessageID returns the message ID that the route is responsible for. +func (r *Route[ReqT, RespT]) MessageID() types.MessageID { + return r.messageID +} + // SetRecipient sets the recipient for the route. func (r *Route[ReqT, RespT]) RegisterRecipient(ch chan any) error { if r.recipient != nil { diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index cc5caa28f9..baf1929377 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -20,9 +20,15 @@ package server -import "github.com/berachain/beacon-kit/mod/errors" +import ( + "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/errors" +) var ( - ErrFeedNotFound = errors.New("feed not found") - ErrRouteNotFound = errors.New("route not found") + ErrFeedNotFound = errors.New("feed not found") + ErrRouteNotFound = errors.New("route not found") + ErrRouteAlreadyRegistered = func(messageID types.MessageID) error { + return errors.Newf("route already registered for messageID: %s", messageID) + } ) diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 160d1be960..10e818b048 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -28,26 +28,26 @@ import ( // EventServer asyncronously dispatches events to subscribers. type EventServer struct { - feeds map[types.MessageID]publisher + publishers map[types.MessageID]types.Publisher } // NewEventServer creates a new event server. func NewEventServer() *EventServer { return &EventServer{ - feeds: make(map[types.MessageID]publisher), + publishers: make(map[types.MessageID]types.Publisher), } } // Dispatch dispatches the given event to the feed with the given eventID. -func (es *EventServer) Dispatch(ctx context.Context, event *types.Message[any]) { - es.feeds[event.ID()].Publish(ctx, *event) +func (es *EventServer) Publish(ctx context.Context, event *types.Message[any]) error { + return es.publishers[event.ID()].Publish(ctx, *event) } // Subscribe subscribes the given channel to the feed with the given eventID. // It will error if the channel type does not match the event type corresponding // feed. func (es *EventServer) Subscribe(eventID types.MessageID, ch chan any) error { - feed, ok := es.feeds[eventID] + feed, ok := es.publishers[eventID] if !ok { return ErrFeedNotFound } @@ -56,14 +56,14 @@ func (es *EventServer) Subscribe(eventID types.MessageID, ch chan any) error { // Start starts the event server. func (es *EventServer) Start(ctx context.Context) { - for _, feed := range es.feeds { + for _, feed := range es.publishers { go feed.Start(ctx) } } -// RegisterFeed registers the given feed with the given eventID. +// RegisterPublisher registers the given publisher with the given eventID. // Any subsequent events with dispatched to this EventServer must be -// consistent with the type expected by . -func (es *EventServer) RegisterFeed(eventID types.MessageID, feed publisher) { - es.feeds[eventID] = feed +// consistent with the type expected by . +func (es *EventServer) RegisterPublisher(eventID types.MessageID, publisher types.Publisher) { + es.publishers[eventID] = publisher } diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index edcf0d18f4..31bec5a1ba 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -25,12 +25,12 @@ import ( ) type MessageServer struct { - routes map[types.MessageID]messageRoute + routes map[types.MessageID]types.MessageRoute } func NewMessageServer() *MessageServer { return &MessageServer{ - routes: make(map[types.MessageID]messageRoute), + routes: make(map[types.MessageID]types.MessageRoute), } } @@ -60,8 +60,12 @@ func (ms *MessageServer) Respond(resp types.Message[any]) error { // RegisterRoute registers the route with the given messageID. // Any subsequent messages with sent to this MessageServer must be // consistent with the type expected by . -func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route messageRoute) { +func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route types.MessageRoute) error { + if ms.routes[messageID] != nil { + return ErrRouteAlreadyRegistered(messageID) + } ms.routes[messageID] = route + return nil } // SetRecipient sets the recipient for the route with the given messageID. diff --git a/mod/async/pkg/server/types.go b/mod/async/pkg/types/notifiers.go similarity index 93% rename from mod/async/pkg/server/types.go rename to mod/async/pkg/types/notifiers.go index 78cf70f4be..9dc15c00f4 100644 --- a/mod/async/pkg/server/types.go +++ b/mod/async/pkg/types/notifiers.go @@ -18,16 +18,16 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package server +package types import "context" // publisher is the interface that supports basic event feed operations. -type publisher interface { +type Publisher interface { // Start starts the event feed. Start(ctx context.Context) // Publish publishes the given event to the event feed. - Publish(ctx context.Context, event any) + Publish(ctx context.Context, event any) error // Subscribe subscribes the given channel to the event feed. Subscribe(ch any) error // Unsubscribe unsubscribes the given channel from the event feed. @@ -35,7 +35,7 @@ type publisher interface { } // messageRoute is the interface that supports basic message route operations. -type messageRoute interface { +type MessageRoute interface { // RegisterRecipient sets the recipient for the route. RegisterRecipient(ch chan any) error // SendRequest sends a request to the recipient. diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 11c63ec0a6..6d6cd99afe 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -199,14 +199,14 @@ func (s *Service[ case <-ctx.Done(): return case msg := <-subBlkCh: - switch msg.Type() { + switch msg.ID() { case events.BeaconBlockReceived: s.handleBeaconBlockReceived(msg) case events.BeaconBlockFinalizedRequest: s.handleBeaconBlockFinalization(msg) } case msg := <-subGenCh: - if msg.Type() == events.GenesisDataProcessRequest { + if msg.ID() == events.GenesisDataProcessRequest { s.handleProcessGenesisDataRequest(msg) } } diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 3d4f45b3cb..2d46fb686e 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -192,7 +192,7 @@ func (s *Service[ case <-ctx.Done(): return case req := <-s.newSlotSub: - if req.Type() == events.NewSlot { + if req.ID() == events.NewSlot { s.handleNewSlot(req) } } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index b3cb234c57..115b7c3bfe 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -103,7 +103,7 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) start( case <-ctx.Done(): return case msg := <-sidecarsCh: - switch msg.Type() { + switch msg.ID() { case events.BlobSidecarsProcessRequest: s.handleBlobSidecarsProcessRequest(msg) case events.BlobSidecarsReceived: diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go new file mode 100644 index 0000000000..3d48808e65 --- /dev/null +++ b/mod/node-core/pkg/components/dispatcher.go @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package components + +import ( + "cosmossdk.io/depinject" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + "github.com/berachain/beacon-kit/mod/async/pkg/server" +) + +// DispatcherInput is the input for the Dispatcher. +type DispatcherInput struct { + depinject.In + EventServer *server.EventServer + MessageServer *server.MessageServer +} + +// ProvideDispatcher provides a new Dispatcher. +func ProvideDispatcher( + in DispatcherInput, +) *dispatcher.Dispatcher { + return dispatcher.NewDispatcher(in.EventServer, in.MessageServer) +} diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go new file mode 100644 index 0000000000..d7b8da64b0 --- /dev/null +++ b/mod/node-core/pkg/components/events.go @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package components + +import ( + "cosmossdk.io/depinject" + "github.com/berachain/beacon-kit/mod/async/pkg/messaging" + "github.com/berachain/beacon-kit/mod/async/pkg/server" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" +) + +// EventServerInput is the input for the event server. +type EventServerInput struct { + depinject.In + BeaconBlockFinalizedPublisher *messaging.Publisher[*BlockMessage] +} + +// ProvideEventServer provides an event server. +func ProvideEventServer(in EventServerInput) *server.EventServer { + es := server.NewEventServer() + es.RegisterPublisher( + in.BeaconBlockFinalizedPublisher.EventID(), + in.BeaconBlockFinalizedPublisher, + ) + return es +} + +// ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block +// finalized events. +func ProvideBeaconBlockFinalizedPublisher() *messaging.Publisher[*BlockMessage] { + return messaging.NewPublisher[*BlockMessage](events.BeaconBlockFinalized) +} diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go new file mode 100644 index 0000000000..cc46fdc188 --- /dev/null +++ b/mod/node-core/pkg/components/messages.go @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package components + +import ( + "cosmossdk.io/depinject" + "github.com/berachain/beacon-kit/mod/async/pkg/messaging" + "github.com/berachain/beacon-kit/mod/async/pkg/server" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" +) + +type MessageServerInput struct { + depinject.In + BuildBlockRoute *messaging.Route[*SlotMessage, *BlockMessage] + BuildSidecarsRoute *messaging.Route[*SlotMessage, *SidecarMessage] + VerifyBlockRoute *messaging.Route[*BlockMessage, *BlockMessage] + FinalizeBlockRoute *messaging.Route[*BlockMessage, *ValidatorUpdateMessage] + ProcessGenesisDataRoute *messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] + ProcessBlobSidecarsRoute *messaging.Route[*SidecarMessage, *SidecarMessage] +} + +func ProvideMessageServer(in MessageServerInput) *server.MessageServer { + ms := server.NewMessageServer() + ms.RegisterRoute(in.BuildBlockRoute.MessageID(), in.BuildBlockRoute) + ms.RegisterRoute(in.BuildSidecarsRoute.MessageID(), in.BuildSidecarsRoute) + ms.RegisterRoute(in.VerifyBlockRoute.MessageID(), in.VerifyBlockRoute) + ms.RegisterRoute(in.FinalizeBlockRoute.MessageID(), in.FinalizeBlockRoute) + ms.RegisterRoute(in.ProcessGenesisDataRoute.MessageID(), in.ProcessGenesisDataRoute) + ms.RegisterRoute(in.ProcessBlobSidecarsRoute.MessageID(), in.ProcessBlobSidecarsRoute) + return ms +} + +// ProvideBuildBlockRoute provides a route for building a beacon block. +func ProvideBuildBlockRoute() *messaging.Route[*SlotMessage, *BlockMessage] { + return messaging.NewRoute[*SlotMessage, *BlockMessage](events.BuildBeaconBlock) +} + +// ProvideBuildSidecarsRoute provides a route for building sidecars. +func ProvideBuildSidecarsRoute() *messaging.Route[*SlotMessage, *SidecarMessage] { + return messaging.NewRoute[*SlotMessage, *SidecarMessage](events.BuildBlobSidecars) +} + +// ProvideVerifyBeaconBlockRoute provides a route for verifying a beacon block. +func ProvideVerifyBeaconBlockRoute() *messaging.Route[*BlockMessage, *BlockMessage] { + return messaging.NewRoute[*BlockMessage, *BlockMessage](events.VerifyBeaconBlock) +} + +// ProvideFinalizeBeaconBlockRoute provides a route for finalizing a beacon block. +func ProvideFinalizeBeaconBlockRoute() *messaging.Route[*BlockMessage, *ValidatorUpdateMessage] { + return messaging.NewRoute[*BlockMessage, *ValidatorUpdateMessage](events.FinalizeBeaconBlock) +} + +// ProvideProcessGenesisDataRoute provides a route for processing genesis data. +func ProvideProcessGenesisDataRoute() *messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] { + return messaging.NewRoute[*GenesisMessage, *ValidatorUpdateMessage](events.ProcessGenesisData) +} + +// ProvideProcessBlobSidecarsRoute provides a route for processing blob sidecars. +func ProvideProcessBlobSidecarsRoute() *messaging.Route[*SidecarMessage, *SidecarMessage] { + return messaging.NewRoute[*SidecarMessage, *SidecarMessage](events.ProcessBlobSidecars) +} diff --git a/mod/node-core/pkg/components/middleware.go b/mod/node-core/pkg/components/middleware.go index 1a9879763f..82d5688fba 100644 --- a/mod/node-core/pkg/components/middleware.go +++ b/mod/node-core/pkg/components/middleware.go @@ -22,6 +22,7 @@ package components import ( "cosmossdk.io/depinject" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/metrics" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -31,14 +32,10 @@ import ( // ABCIMiddlewareInput is the input for the validator middleware provider. type ABCIMiddlewareInput struct { depinject.In - BeaconBlockFeed *BlockBroker - ChainSpec common.ChainSpec - GenesisBroker *GenesisBroker - Logger log.Logger[any] - SidecarsFeed *SidecarsBroker - SlotBroker *SlotBroker - TelemetrySink *metrics.TelemetrySink - ValidatorUpdateBroker *ValidatorUpdateBroker + ChainSpec common.ChainSpec + Logger log.Logger[any] + TelemetrySink *metrics.TelemetrySink + Dispatcher *dispatcher.Dispatcher } // ProvideABCIMiddleware is a depinject provider for the validator @@ -46,10 +43,6 @@ type ABCIMiddlewareInput struct { func ProvideABCIMiddleware( in ABCIMiddlewareInput, ) (*ABCIMiddleware, error) { - validatorUpdatesSub, err := in.ValidatorUpdateBroker.Subscribe() - if err != nil { - return nil, err - } return middleware.NewABCIMiddleware[ *AvailabilityStore, *BeaconBlock, *BlobSidecars, *Deposit, *ExecutionPayload, *Genesis, *SlotData, @@ -57,10 +50,6 @@ func ProvideABCIMiddleware( in.ChainSpec, in.Logger, in.TelemetrySink, - in.GenesisBroker, - in.BeaconBlockFeed, - in.SidecarsFeed, - in.SlotBroker, - validatorUpdatesSub, + in.Dispatcher, ), nil } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index e6d6b1cb47..a75d7fa40b 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -23,6 +23,7 @@ package components import ( "cosmossdk.io/core/appmodule/v2" broker "github.com/berachain/beacon-kit/mod/async/pkg/broker" + "github.com/berachain/beacon-kit/mod/async/pkg/messaging" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" blockstore "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/beacon/blockchain" @@ -433,6 +434,26 @@ type ( ValidatorUpdateEvent = asynctypes.Event[transition.ValidatorUpdates] ) +type ( + // BlockEvent is a type alias for the block event. + BlockMessage = asynctypes.Message[*BeaconBlock] + + // GenesisEvent is a type alias for the genesis event. + GenesisMessage = asynctypes.Message[*Genesis] + + // SidecarEvent is a type alias for the sidecar event. + SidecarMessage = asynctypes.Message[*BlobSidecars] + + // SlotEvent is a type alias for the slot event. + SlotMessage = asynctypes.Message[*SlotData] + + // StatusEvent is a type alias for the status event. + StatusMessage = asynctypes.Message[*service.StatusEvent] + + // ValidatorUpdateEvent is a type alias for the validator update event. + ValidatorUpdateMessage = asynctypes.Message[transition.ValidatorUpdates] +) + /* -------------------------------------------------------------------------- */ /* Brokers */ /* -------------------------------------------------------------------------- */ @@ -457,6 +478,14 @@ type ( ValidatorUpdateBroker = broker.Broker[*ValidatorUpdateEvent] ) +/* -------------------------------------------------------------------------- */ +/* Publishers */ +/* -------------------------------------------------------------------------- */ + +type ( + BeaconBlockFinalizedPublisher = messaging.Publisher[*BeaconBlock] +) + /* -------------------------------------------------------------------------- */ /* Pruners */ /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/events/events.go b/mod/primitives/pkg/events/events.go index 77f4fa17b3..355c6d19be 100644 --- a/mod/primitives/pkg/events/events.go +++ b/mod/primitives/pkg/events/events.go @@ -20,13 +20,13 @@ package events +// TODO: delete const ( NewSlot = "new-slot" BeaconBlockBuilt = "beacon-block-built" BeaconBlockReceived = "beacon-block-received" BeaconBlockVerified = "beacon-block-verified" BeaconBlockFinalizedRequest = "beacon-block-finalized-request" - BeaconBlockFinalized = "beacon-block-finalized" ValidatorSetUpdated = "validator-set-updated" BlobSidecarsBuilt = "blob-sidecars-built" BlobSidecarsReceived = "blob-sidecars-received" @@ -34,3 +34,18 @@ const ( BlobSidecarsProcessed = "blob-sidecars-processed" GenesisDataProcessRequest = "genesis-data-process-request" ) + +// messages +const ( + BuildBeaconBlock = "build-beacon-block" + BuildBlobSidecars = "build-blob-sidecars" + VerifyBeaconBlock = "verify-beacon-block" + FinalizeBeaconBlock = "finalize-beacon-block" + ProcessGenesisData = "process-genesis-data" + ProcessBlobSidecars = "process-blob-sidecars" +) + +// events +const ( + BeaconBlockFinalized = "beacon-block-finalized" +) diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 65e9ed10e2..431fa56c8e 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -48,51 +48,23 @@ func (h *ABCIMiddleware[ bz []byte, ) (transition.ValidatorUpdates, error) { var ( - g errgroup.Group valUpdates transition.ValidatorUpdates - genesisErr error ) data := new(GenesisT) if err := json.Unmarshal(bz, data); err != nil { return nil, err } - // Send a request to the chain service to process the genesis data. - if err := h.genesisBroker.Publish(ctx, asynctypes.NewEvent( - ctx, events.GenesisDataProcessRequest, *data, - )); err != nil { - return nil, err - } - - // Wait for the genesis data to be processed. - g.Go(func() error { - valUpdates, genesisErr = h.waitForGenesisData(ctx) - return genesisErr - }) - if err := g.Wait(); err != nil { + err := h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.GenesisDataProcessRequest, *data, + ), &valUpdates, + ) + if err != nil { return nil, err } - return valUpdates, nil -} -// waitForGenesisData waits for the genesis data to be processed and returns -// the validator updates. -func (h *ABCIMiddleware[ - _, _, _, _, _, GenesisT, _, -]) waitForGenesisData(ctx context.Context) ( - transition.ValidatorUpdates, error) { - select { - case msg := <-h.valUpdateSub: - if msg.Type() != events.ValidatorSetUpdated { - return nil, errors.Wrapf( - ErrUnexpectedEvent, - "unexpected event type: %s", msg.Type(), - ) - } - return msg.Data(), msg.Error() - case <-ctx.Done(): - return nil, ctx.Err() - } + return valUpdates, nil } /* -------------------------------------------------------------------------- */ @@ -245,9 +217,9 @@ func (h *ABCIMiddleware[ case <-ctx.Done(): return ctx.Err() case msg := <-h.blkCh: - if msg.Type() != events.BeaconBlockVerified { + if msg.ID() != events.BeaconBlockVerified { return errors.Wrapf( - ErrUnexpectedEvent, "unexpected event type: %s", msg.Type(), + ErrUnexpectedEvent, "unexpected event type: %s", msg.ID(), ) } return msg.Error() @@ -276,9 +248,9 @@ func (h *ABCIMiddleware[ case <-ctx.Done(): return ctx.Err() case msg := <-h.sidecarsCh: - if msg.Type() != events.BlobSidecarsProcessed { + if msg.ID() != events.BlobSidecarsProcessed { return errors.Wrapf( - ErrUnexpectedEvent, "unexpected event type: %s", msg.Type(), + ErrUnexpectedEvent, "unexpected event type: %s", msg.ID(), ) } return msg.Error() @@ -368,10 +340,10 @@ func (h *ABCIMiddleware[ case <-ctx.Done(): return ctx.Err() case msg := <-h.sidecarsCh: - if msg.Type() != events.BlobSidecarsProcessed { + if msg.ID() != events.BlobSidecarsProcessed { return errors.Wrapf( ErrUnexpectedEvent, - "unexpected event type: %s", msg.Type(), + "unexpected event type: %s", msg.ID(), ) } return msg.Error() @@ -395,10 +367,10 @@ func (h *ABCIMiddleware[ // Wait for the block to be processed. select { case msg := <-h.valUpdateSub: - if msg.Type() != events.ValidatorSetUpdated { + if msg.ID() != events.ValidatorSetUpdated { return nil, errors.Wrapf( ErrUnexpectedEvent, - "unexpected event type: %s", msg.Type(), + "unexpected event type: %s", msg.ID(), ) } return msg.Data(), msg.Error() diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 9c2ea8c9b7..4dd38a7988 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -25,12 +25,12 @@ import ( "encoding/json" "github.com/berachain/beacon-kit/mod/async/pkg/broker" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" @@ -68,6 +68,7 @@ type ABCIMiddleware[ encoding.ABCIRequest, BeaconBlockT, ] + dispatcher *dispatcher.Dispatcher // metrics is the metrics emitter. metrics *ABCIMiddlewareMetrics // logger is the logger for the middleware. @@ -113,11 +114,7 @@ func NewABCIMiddleware[ chainSpec common.ChainSpec, logger log.Logger[any], telemetrySink TelemetrySink, - genesisBroker *broker.Broker[*asynctypes.Event[GenesisT]], - blkBroker *broker.Broker[*asynctypes.Event[BeaconBlockT]], - sidecarsBroker *broker.Broker[*asynctypes.Event[BlobSidecarsT]], - slotBroker *broker.Broker[*asynctypes.Event[SlotDataT]], - valUpdateSub chan *asynctypes.Event[transition.ValidatorUpdates], + dispatcher *dispatcher.Dispatcher, ) *ABCIMiddleware[ AvailabilityStoreT, BeaconBlockT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, @@ -136,12 +133,6 @@ func NewABCIMiddleware[ ]( chainSpec, ), - logger: logger, - metrics: newABCIMiddlewareMetrics(telemetrySink), - genesisBroker: genesisBroker, - blkBroker: blkBroker, - sidecarsBroker: sidecarsBroker, - slotBroker: slotBroker, blkCh: make( chan *asynctypes.Event[BeaconBlockT], 1, @@ -150,7 +141,9 @@ func NewABCIMiddleware[ chan *asynctypes.Event[BlobSidecarsT], 1, ), - valUpdateSub: valUpdateSub, + logger: logger, + metrics: newABCIMiddlewareMetrics(telemetrySink), + dispatcher: dispatcher, } } @@ -166,46 +159,47 @@ func (am *ABCIMiddleware[ func (am *ABCIMiddleware[ _, _, _, _, _, _, _, ]) Start(ctx context.Context) error { - subBlkCh, err := am.blkBroker.Subscribe() - if err != nil { - return err - } - subSidecarsCh, err := am.sidecarsBroker.Subscribe() - if err != nil { - return err - } + // err := am.dispatcher.Subscribe(events.BeaconBlockBuilt, am.blkCh) + // if err != nil { + // return err + // } + + // subSidecarsCh, err := am.sidecarsBroker.Subscribe() + // if err != nil { + // return err + // } - go am.start(ctx, subBlkCh, subSidecarsCh) + // go am.start(ctx, subBlkCh, subSidecarsCh) return nil } -// start starts the middleware. -func (am *ABCIMiddleware[ - _, BeaconBlockT, BlobSidecarsT, _, _, _, _, -]) start( - ctx context.Context, - blkCh chan *asynctypes.Event[BeaconBlockT], - sidecarsCh chan *asynctypes.Event[BlobSidecarsT], -) { - for { - select { - case <-ctx.Done(): - return - case msg := <-blkCh: - switch msg.Type() { - case events.BeaconBlockBuilt: - fallthrough - case events.BeaconBlockVerified: - am.blkCh <- msg - } - case msg := <-sidecarsCh: - switch msg.Type() { - case events.BlobSidecarsBuilt: - fallthrough - case events.BlobSidecarsProcessed: - am.sidecarsCh <- msg - } - } - } -} +// // start starts the middleware. +// func (am *ABCIMiddleware[ +// _, BeaconBlockT, BlobSidecarsT, _, _, _, _, +// ]) start( +// ctx context.Context, +// blkCh chan *asynctypes.Event[BeaconBlockT], +// sidecarsCh chan *asynctypes.Event[BlobSidecarsT], +// ) { +// for { +// select { +// case <-ctx.Done(): +// return +// case msg := <-blkCh: +// switch msg.ID() { +// case events.BeaconBlockBuilt: +// fallthrough +// case events.BeaconBlockVerified: +// am.blkCh <- msg +// } +// case msg := <-sidecarsCh: +// switch msg.ID() { +// case events.BlobSidecarsBuilt: +// fallthrough +// case events.BlobSidecarsProcessed: +// am.sidecarsCh <- msg +// } +// } +// } +// } From 1e6ad38153cdedb10a88434a4f806f3ba9fa4590 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 02:00:10 -0400 Subject: [PATCH 04/75] dispatch middleware --- mod/async/pkg/dispatcher/dispatcher.go | 34 +-- mod/async/pkg/dispatcher/types.go | 8 +- mod/async/pkg/messaging/route.go | 4 +- mod/async/pkg/server/event.go | 4 +- mod/async/pkg/server/msg.go | 8 +- mod/async/pkg/types/message.go | 7 + mod/async/pkg/types/notifiers.go | 2 +- mod/node-core/pkg/components/messages.go | 2 +- mod/primitives/pkg/events/events.go | 2 +- mod/runtime/pkg/middleware/abci.go | 283 ++++++++--------------- mod/runtime/pkg/middleware/middleware.go | 73 ------ 11 files changed, 131 insertions(+), 296 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 73606a7673..fb1852ee03 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -51,6 +51,21 @@ func (d *Dispatcher) Start(ctx context.Context) error { return nil } +// DispatchEvent dispatches the given event to the event server. +func (d *Dispatcher) DispatchEvent(ctx context.Context, event types.MessageI) error { + return d.eventServer.Publish(ctx, event) +} + +// DispatchRequest dispatches the given request to the message server. +func (d *Dispatcher) DispatchRequest(req types.MessageI, resp types.MessageI) error { + return d.msgServer.Request(req, resp) +} + +// DispatchResponse dispatches the given response to the message server. +func (d *Dispatcher) DispatchResponse(resp types.MessageI) error { + return d.msgServer.Respond(resp) +} + // ============================== Events =================================== // RegisterPublisher registers the given publisher with the given eventID. @@ -67,27 +82,12 @@ func (d *Dispatcher) Subscribe(eventID types.MessageID, ch chan any) error { return d.eventServer.Subscribe(eventID, ch) } -// DispatchEvent dispatches the given event to the event server. -func (d *Dispatcher) DispatchEvent(ctx context.Context, event *types.Message[any]) error { - return d.eventServer.Publish(ctx, event) -} - // ================================ Messages ================================ // RegisterMsgRecipient registers the given channel to the message with the // given . -func (d *Dispatcher) RegisterMsgRecipient(messageID types.MessageID, ch chan any) error { - return d.msgServer.RegisterRecipient(messageID, ch) -} - -// DispatchRequest dispatches the given request to the message server. -func (d *Dispatcher) DispatchRequest(req types.Message[any], resp any) error { - return d.msgServer.Request(req, resp) -} - -// DispatchResponse dispatches the given response to the message server. -func (d *Dispatcher) DispatchResponse(resp types.Message[any]) error { - return d.msgServer.Respond(resp) +func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch chan any) error { + return d.msgServer.RegisterReceiver(messageID, ch) } // RegisterRoute registers the given route with the given messageID. diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index d7f8470b1c..344cf8013a 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -27,9 +27,9 @@ import ( ) type MessageServer interface { - RegisterRecipient(mID types.MessageID, ch chan any) error - Request(req types.Message[any], resp any) error - Respond(resp types.Message[any]) error + RegisterReceiver(mID types.MessageID, ch chan any) error + Request(req any, resp any) error + Respond(resp any) error RegisterRoute(mID types.MessageID, route types.MessageRoute) error } @@ -37,5 +37,5 @@ type EventServer interface { Start(ctx context.Context) RegisterPublisher(mID types.MessageID, publisher types.Publisher) Subscribe(mID types.MessageID, ch chan any) error - Publish(ctx context.Context, event *types.Message[any]) error + Publish(ctx context.Context, event types.MessageI) error } diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 1ab75ee29e..d56ac32d6c 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -54,8 +54,8 @@ func (r *Route[ReqT, RespT]) MessageID() types.MessageID { return r.messageID } -// SetRecipient sets the recipient for the route. -func (r *Route[ReqT, RespT]) RegisterRecipient(ch chan any) error { +// RegisterReceiver sets the recipient for the route. +func (r *Route[ReqT, RespT]) RegisterReceiver(ch chan any) error { if r.recipient != nil { return ErrRouteAlreadySet } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 10e818b048..9af1606238 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -39,8 +39,8 @@ func NewEventServer() *EventServer { } // Dispatch dispatches the given event to the feed with the given eventID. -func (es *EventServer) Publish(ctx context.Context, event *types.Message[any]) error { - return es.publishers[event.ID()].Publish(ctx, *event) +func (es *EventServer) Publish(ctx context.Context, event types.MessageI) error { + return es.publishers[event.ID()].Publish(ctx, event) } // Subscribe subscribes the given channel to the feed with the given eventID. diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 31bec5a1ba..cc263730dd 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -36,7 +36,7 @@ func NewMessageServer() *MessageServer { // Request sends a message to the server and awaits for a response. // The response is written to the provided response pointer. -func (ms *MessageServer) Request(req types.Message[any], resp any) error { +func (ms *MessageServer) Request(req types.MessageI, resp types.MessageI) error { // send message to request channel and await a response in the response channel route, ok := ms.routes[req.ID()] if !ok { @@ -48,7 +48,7 @@ func (ms *MessageServer) Request(req types.Message[any], resp any) error { // Respond sends a response to the route that corresponds to the response's // messageID. -func (ms *MessageServer) Respond(resp types.Message[any]) error { +func (ms *MessageServer) Respond(resp types.MessageI) error { route, ok := ms.routes[resp.ID()] if !ok { return ErrRouteNotFound @@ -71,10 +71,10 @@ func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route types.Me // SetRecipient sets the recipient for the route with the given messageID. // Errors if the route with the given messageID is not found or the route // already has a registered recipient. -func (ms *MessageServer) RegisterRecipient(messageID types.MessageID, ch chan any) error { +func (ms *MessageServer) RegisterReceiver(messageID types.MessageID, ch chan any) error { route, ok := ms.routes[messageID] if !ok { return ErrRouteNotFound } - return route.RegisterRecipient(ch) + return route.RegisterReceiver(ch) } diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index e5c61cba36..36d85f672f 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -25,6 +25,13 @@ import ( "errors" ) +type MessageI interface { + ID() MessageID + Context() context.Context + Error() error + Is(MessageID) bool +} + // A Message is an asynchronous message meant for a single recipient. type Message[DataT any] struct { // ctx is the context associated with the event. diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index 9dc15c00f4..a9108c2745 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -37,7 +37,7 @@ type Publisher interface { // messageRoute is the interface that supports basic message route operations. type MessageRoute interface { // RegisterRecipient sets the recipient for the route. - RegisterRecipient(ch chan any) error + RegisterReceiver(ch chan any) error // SendRequest sends a request to the recipient. SendRequest(msg any) error // SendResponse sends a response to the recipient. diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index cc46fdc188..f5cdb9d0aa 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -75,5 +75,5 @@ func ProvideProcessGenesisDataRoute() *messaging.Route[*GenesisMessage, *Validat // ProvideProcessBlobSidecarsRoute provides a route for processing blob sidecars. func ProvideProcessBlobSidecarsRoute() *messaging.Route[*SidecarMessage, *SidecarMessage] { - return messaging.NewRoute[*SidecarMessage, *SidecarMessage](events.ProcessBlobSidecars) + return messaging.NewRoute[*SidecarMessage, *SidecarMessage](events.VerifyBlobSidecars) } diff --git a/mod/primitives/pkg/events/events.go b/mod/primitives/pkg/events/events.go index 355c6d19be..9454098edd 100644 --- a/mod/primitives/pkg/events/events.go +++ b/mod/primitives/pkg/events/events.go @@ -42,7 +42,7 @@ const ( VerifyBeaconBlock = "verify-beacon-block" FinalizeBeaconBlock = "finalize-beacon-block" ProcessGenesisData = "process-genesis-data" - ProcessBlobSidecars = "process-blob-sidecars" + VerifyBlobSidecars = "process-blob-sidecars" ) // events diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 431fa56c8e..9802305bbc 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -33,7 +33,6 @@ import ( "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" cmtabci "github.com/cometbft/cometbft/abci/types" "github.com/cosmos/gogoproto/proto" - "golang.org/x/sync/errgroup" ) /* -------------------------------------------------------------------------- */ @@ -48,7 +47,7 @@ func (h *ABCIMiddleware[ bz []byte, ) (transition.ValidatorUpdates, error) { var ( - valUpdates transition.ValidatorUpdates + valUpdateResp asynctypes.Message[transition.ValidatorUpdates] ) data := new(GenesisT) if err := json.Unmarshal(bz, data); err != nil { @@ -58,13 +57,13 @@ func (h *ABCIMiddleware[ err := h.dispatcher.DispatchRequest( asynctypes.NewMessage( ctx, events.GenesisDataProcessRequest, *data, - ), &valUpdates, + ), &valUpdateResp, ) if err != nil { return nil, err } - return valUpdates, nil + return valUpdateResp.Data(), valUpdateResp.Error() } /* -------------------------------------------------------------------------- */ @@ -73,75 +72,73 @@ func (h *ABCIMiddleware[ // prepareProposal is the internal handler for preparing proposals. func (h *ABCIMiddleware[ - _, _, _, _, _, _, SlotDataT, + _, BeaconBlockT, BlobSidecarsT, _, _, _, SlotDataT, ]) PrepareProposal( ctx context.Context, slotData SlotDataT, ) ([]byte, []byte, error) { var ( - g errgroup.Group startTime = time.Now() beaconBlockErr, sidecarsErr error beaconBlockBz, sidecarsBz []byte + beaconBlockResp asynctypes.Message[BeaconBlockT] + sidecarsResp asynctypes.Message[BlobSidecarsT] ) defer h.metrics.measurePrepareProposalDuration(startTime) - // Send a request to the validator service to give us a beacon block - // and blob sidecards to pass to ABCI. - if err := h.slotBroker.Publish(ctx, asynctypes.NewEvent( - ctx, events.NewSlot, slotData, - )); err != nil { - return nil, nil, err + // request a built beacon block for the given slot + h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.BuildBeaconBlock, slotData, + ), &beaconBlockResp, + ) + + // handle the beacon block response + beaconBlockBz, beaconBlockErr = h.handleBeaconBlockResponse(ctx, beaconBlockResp) + if beaconBlockErr != nil { + return nil, nil, beaconBlockErr + } + + // request the built blob sidecars + h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.BuildBlobSidecars, slotData, + ), &sidecarsResp, + ) + + // handle the sidecars response + sidecarsBz, sidecarsErr = h.handleSidecarResponse(ctx, sidecarsResp) + if sidecarsErr != nil { + return nil, nil, sidecarsErr } - // Wait for the beacon block to be built. - g.Go(func() error { - beaconBlockBz, beaconBlockErr = h.waitforBeaconBlk(ctx) - return beaconBlockErr - }) - - // Wait for the sidecars to be built. - g.Go(func() error { - sidecarsBz, sidecarsErr = h.waitForSidecars(ctx) - return sidecarsErr - }) - - // Wait for both processes to complete and then - // return the appropriate response. - return beaconBlockBz, sidecarsBz, g.Wait() + return beaconBlockBz, sidecarsBz, nil } -// waitForSidecars waits for the sidecars to be built and returns them. +// handleSidecarResponse publishes the sidecars to the gossiper. func (h *ABCIMiddleware[ - _, _, _, _, _, _, _, -]) waitForSidecars(ctx context.Context) ([]byte, error) { - select { - case <-ctx.Done(): - return nil, ctx.Err() - case msg := <-h.sidecarsCh: - if msg.Error() != nil { - return nil, msg.Error() - } - return h.blobGossiper.Publish(ctx, msg.Data()) + _, _, BlobSidecarsT, _, _, _, _, +]) handleSidecarResponse( + ctx context.Context, + sidecarsResp asynctypes.Message[BlobSidecarsT], +) ([]byte, error) { + if sidecarsResp.Error() != nil { + return nil, sidecarsResp.Error() } + return h.blobGossiper.Publish(ctx, sidecarsResp.Data()) } -// waitforBeaconBlk waits for the beacon block to be built and returns it. +// handleBeaconBlockResponse publishes the beacon block to the gossiper. func (h *ABCIMiddleware[ - _, _, _, _, _, _, _, -]) waitforBeaconBlk(ctx context.Context) ([]byte, error) { - select { - case <-ctx.Done(): - return nil, ctx.Err() - case beaconBlock := <-h.blkCh: - if beaconBlock.Error() != nil { - return nil, beaconBlock.Error() - } - return h.beaconBlockGossiper.Publish( - ctx, - beaconBlock.Data(), - ) + _, BeaconBlockT, _, _, _, _, _, +]) handleBeaconBlockResponse( + ctx context.Context, + beaconBlockResp asynctypes.Message[BeaconBlockT], +) ([]byte, error) { + if beaconBlockResp.Error() != nil { + return nil, beaconBlockResp.Error() } + return h.beaconBlockGossiper.Publish(ctx, beaconBlockResp.Data()) } /* -------------------------------------------------------------------------- */ @@ -157,11 +154,12 @@ func (h *ABCIMiddleware[ req proto.Message, ) (proto.Message, error) { var ( - blk BeaconBlockT - sidecars BlobSidecarsT - err error - g, _ = errgroup.WithContext(ctx) - startTime = time.Now() + blk BeaconBlockT + sidecars BlobSidecarsT + err error + startTime = time.Now() + beaconBlockResp asynctypes.Message[BeaconBlockT] + sidecarsResp asynctypes.Message[BlobSidecarsT] ) abciReq, ok := req.(*cmtabci.ProcessProposalRequest) if !ok { @@ -175,95 +173,41 @@ func (h *ABCIMiddleware[ return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - // Begin processing the beacon block. - g.Go(func() error { - return h.verifyBeaconBlock(ctx, blk) - }) + // verify the beacon block + h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.VerifyBeaconBlock, blk, + ), &beaconBlockResp, + ) + + if beaconBlockResp.Error() != nil { + return h.createProcessProposalResponse(beaconBlockResp.Error()) + } // Request the blob sidecars. if sidecars, err = h.blobGossiper.Request(ctx, abciReq); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - // Begin processing the blob sidecars. - g.Go(func() error { - return h.verifyBlobSidecars(ctx, sidecars) - }) - - // Wait for both processes to complete and then - // return the appropriate response.s - return h.createProcessProposalResponse(g.Wait()) -} - -// verifyBeaconBlock handles the processing of the beacon block. -// It requests the block, publishes a received event, and waits for -// verification. -func (h *ABCIMiddleware[ - _, BeaconBlockT, BlobSidecarsT, _, _, _, _, -]) verifyBeaconBlock( - ctx context.Context, - blk BeaconBlockT, -) error { - // Publish the received event. - if err := h.blkBroker.Publish( - ctx, - asynctypes.NewEvent(ctx, events.BeaconBlockReceived, blk, nil), - ); err != nil { - return err - } - - // Wait for a response. - select { - case <-ctx.Done(): - return ctx.Err() - case msg := <-h.blkCh: - if msg.ID() != events.BeaconBlockVerified { - return errors.Wrapf( - ErrUnexpectedEvent, "unexpected event type: %s", msg.ID(), - ) - } - return msg.Error() - } -} + // verify the blob sidecars + h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.VerifyBlobSidecars, sidecars, + ), &sidecarsResp, + ) -// processBlobSidecars handles the processing of blob sidecars. -// It requests the sidecars, publishes a received event, and waits for -// processing. -func (h *ABCIMiddleware[ - _, BeaconBlockT, BlobSidecarsT, _, _, _, _, -]) verifyBlobSidecars( - ctx context.Context, - sidecars BlobSidecarsT, -) error { - // Publish the received event. - if err := h.sidecarsBroker.Publish( - ctx, - asynctypes.NewEvent(ctx, events.BlobSidecarsReceived, sidecars), - ); err != nil { - return err + if sidecarsResp.Error() != nil { + return h.createProcessProposalResponse(sidecarsResp.Error()) } - // Wait for a response. - select { - case <-ctx.Done(): - return ctx.Err() - case msg := <-h.sidecarsCh: - if msg.ID() != events.BlobSidecarsProcessed { - return errors.Wrapf( - ErrUnexpectedEvent, "unexpected event type: %s", msg.ID(), - ) - } - return msg.Error() - } + return h.createProcessProposalResponse(nil) } // createResponse generates the appropriate ProcessProposalResponse based on the // error. func (*ABCIMiddleware[ _, BeaconBlockT, _, BlobSidecarsT, _, _, _, -]) createProcessProposalResponse( - err error, -) (proto.Message, error) { +]) createProcessProposalResponse(err error) (proto.Message, error) { status := cmtabci.PROCESS_PROPOSAL_STATUS_REJECT if !errors.IsFatal(err) { status = cmtabci.PROCESS_PROPOSAL_STATUS_ACCEPT @@ -299,6 +243,10 @@ func (h *ABCIMiddleware[ ]) EndBlock( ctx context.Context, ) (transition.ValidatorUpdates, error) { + var ( + sidecarsResp asynctypes.Message[BlobSidecarsT] + valUpdatesResp asynctypes.Message[transition.ValidatorUpdates] + ) blk, blobs, err := encoding. ExtractBlobsAndBlockFromRequest[BeaconBlockT, BlobSidecarsT]( h.req, @@ -313,68 +261,21 @@ func (h *ABCIMiddleware[ return nil, nil } - // Send the sidecars to the sidecars feed and wait for a response - if err = h.processSidecars(ctx, blobs); err != nil { - return nil, err - } - - // Process the beacon block and return the validator updates. - return h.processBeaconBlock( - ctx, blk, + // verify the blob sidecars + h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.VerifyBlobSidecars, blobs, + ), &sidecarsResp, ) -} - -// processSidecars publishes the sidecars and waits for a response. -func (h *ABCIMiddleware[ - _, _, BlobSidecarsT, _, _, _, _, -]) processSidecars(ctx context.Context, blobs BlobSidecarsT) error { - // Publish the sidecars. - if err := h.sidecarsBroker.Publish(ctx, asynctypes.NewEvent( - ctx, events.BlobSidecarsProcessRequest, blobs, - )); err != nil { - return err + if sidecarsResp.Error() != nil { + return nil, sidecarsResp.Error() } - // Wait for the sidecars to be processed. - select { - case <-ctx.Done(): - return ctx.Err() - case msg := <-h.sidecarsCh: - if msg.ID() != events.BlobSidecarsProcessed { - return errors.Wrapf( - ErrUnexpectedEvent, - "unexpected event type: %s", msg.ID(), - ) - } - return msg.Error() - } -} - -// processBeaconBlock processes the beacon block and returns validator updates. -func (h *ABCIMiddleware[ - _, BeaconBlockT, _, _, _, _, _, -]) processBeaconBlock( - ctx context.Context, blk BeaconBlockT, -) (transition.ValidatorUpdates, error) { - // Publish the verified block event. - if err := h.blkBroker.Publish( - ctx, asynctypes.NewEvent( - ctx, events.BeaconBlockFinalizedRequest, blk, - )); err != nil { - return nil, err - } + h.dispatcher.DispatchRequest( + asynctypes.NewMessage( + ctx, events.FinalizeBeaconBlock, blk, + ), &valUpdatesResp, + ) - // Wait for the block to be processed. - select { - case msg := <-h.valUpdateSub: - if msg.ID() != events.ValidatorSetUpdated { - return nil, errors.Wrapf( - ErrUnexpectedEvent, - "unexpected event type: %s", msg.ID(), - ) - } - return msg.Data(), msg.Error() - case <-ctx.Done(): - return nil, ctx.Err() - } + return valUpdatesResp.Data(), valUpdatesResp.Error() } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 4dd38a7988..1a346a5aa5 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -24,14 +24,11 @@ import ( "context" "encoding/json" - "github.com/berachain/beacon-kit/mod/async/pkg/broker" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" - "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" cmtabci "github.com/cometbft/cometbft/abci/types" @@ -74,28 +71,8 @@ type ABCIMiddleware[ // logger is the logger for the middleware. logger log.Logger[any] - // Feeds - // - // genesisBroker is a feed for genesis data. - genesisBroker *broker.Broker[*asynctypes.Event[GenesisT]] - // blkBroker is a feed for blocks. - blkBroker *broker.Broker[*asynctypes.Event[BeaconBlockT]] - // sidecarsBroker is a feed for sidecars. - sidecarsBroker *broker.Broker[*asynctypes.Event[BlobSidecarsT]] - // slotBroker is a feed for slots. - slotBroker *broker.Broker[*asynctypes.Event[SlotDataT]] - // TODO: this is a temporary hack. req *cmtabci.FinalizeBlockRequest - - // Channels - // blkCh is used to communicate the beacon block to the EndBlock method. - blkCh chan *asynctypes.Event[BeaconBlockT] - // sidecarsCh is used to communicate the sidecars to the EndBlock method. - sidecarsCh chan *asynctypes.Event[BlobSidecarsT] - // valUpdateSub is the channel for listening for incoming validator set - // updates. - valUpdateSub chan *asynctypes.Event[transition.ValidatorUpdates] } // NewABCIMiddleware creates a new instance of the Handler struct. @@ -133,14 +110,6 @@ func NewABCIMiddleware[ ]( chainSpec, ), - blkCh: make( - chan *asynctypes.Event[BeaconBlockT], - 1, - ), - sidecarsCh: make( - chan *asynctypes.Event[BlobSidecarsT], - 1, - ), logger: logger, metrics: newABCIMiddlewareMetrics(telemetrySink), dispatcher: dispatcher, @@ -159,47 +128,5 @@ func (am *ABCIMiddleware[ func (am *ABCIMiddleware[ _, _, _, _, _, _, _, ]) Start(ctx context.Context) error { - - // err := am.dispatcher.Subscribe(events.BeaconBlockBuilt, am.blkCh) - // if err != nil { - // return err - // } - - // subSidecarsCh, err := am.sidecarsBroker.Subscribe() - // if err != nil { - // return err - // } - - // go am.start(ctx, subBlkCh, subSidecarsCh) return nil } - -// // start starts the middleware. -// func (am *ABCIMiddleware[ -// _, BeaconBlockT, BlobSidecarsT, _, _, _, _, -// ]) start( -// ctx context.Context, -// blkCh chan *asynctypes.Event[BeaconBlockT], -// sidecarsCh chan *asynctypes.Event[BlobSidecarsT], -// ) { -// for { -// select { -// case <-ctx.Done(): -// return -// case msg := <-blkCh: -// switch msg.ID() { -// case events.BeaconBlockBuilt: -// fallthrough -// case events.BeaconBlockVerified: -// am.blkCh <- msg -// } -// case msg := <-sidecarsCh: -// switch msg.ID() { -// case events.BlobSidecarsBuilt: -// fallthrough -// case events.BlobSidecarsProcessed: -// am.sidecarsCh <- msg -// } -// } -// } -// } From c61d794f015a27bd28725933ac7d8a53ff318a3c Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 02:24:56 -0400 Subject: [PATCH 05/75] wip --- mod/async/pkg/dispatcher/dispatcher.go | 4 +- mod/async/pkg/dispatcher/types.go | 8 +-- mod/async/pkg/messaging/route.go | 2 +- mod/async/pkg/server/event.go | 2 +- mod/async/pkg/server/msg.go | 2 +- mod/async/pkg/types/notifiers.go | 2 +- mod/beacon/validator/service.go | 64 +++++++++++++------ mod/node-core/pkg/components/defaults.go | 3 + mod/node-core/pkg/components/events.go | 9 +++ mod/node-core/pkg/components/messages.go | 14 ++++ .../pkg/components/validator_service.go | 34 ++++------ 11 files changed, 94 insertions(+), 50 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index fb1852ee03..567ab5f21f 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -78,7 +78,7 @@ func (d *Dispatcher) RegisterPublisher(eventID types.MessageID, publisher types. // Subscribe subscribes the given channel to the event with the given . // It will error if the channel type does not match the event type corresponding // to the . -func (d *Dispatcher) Subscribe(eventID types.MessageID, ch chan any) error { +func (d *Dispatcher) Subscribe(eventID types.MessageID, ch any) error { return d.eventServer.Subscribe(eventID, ch) } @@ -86,7 +86,7 @@ func (d *Dispatcher) Subscribe(eventID types.MessageID, ch chan any) error { // RegisterMsgRecipient registers the given channel to the message with the // given . -func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch chan any) error { +func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch any) error { return d.msgServer.RegisterReceiver(messageID, ch) } diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 344cf8013a..d9a229beda 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -27,15 +27,15 @@ import ( ) type MessageServer interface { - RegisterReceiver(mID types.MessageID, ch chan any) error - Request(req any, resp any) error - Respond(resp any) error + RegisterReceiver(mID types.MessageID, ch any) error + Request(req types.MessageI, resp types.MessageI) error + Respond(resp types.MessageI) error RegisterRoute(mID types.MessageID, route types.MessageRoute) error } type EventServer interface { Start(ctx context.Context) RegisterPublisher(mID types.MessageID, publisher types.Publisher) - Subscribe(mID types.MessageID, ch chan any) error + Subscribe(mID types.MessageID, ch any) error Publish(ctx context.Context, event types.MessageI) error } diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index d56ac32d6c..46d3a6fc20 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -55,7 +55,7 @@ func (r *Route[ReqT, RespT]) MessageID() types.MessageID { } // RegisterReceiver sets the recipient for the route. -func (r *Route[ReqT, RespT]) RegisterReceiver(ch chan any) error { +func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { if r.recipient != nil { return ErrRouteAlreadySet } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 9af1606238..2fc6325dbe 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -46,7 +46,7 @@ func (es *EventServer) Publish(ctx context.Context, event types.MessageI) error // Subscribe subscribes the given channel to the feed with the given eventID. // It will error if the channel type does not match the event type corresponding // feed. -func (es *EventServer) Subscribe(eventID types.MessageID, ch chan any) error { +func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { feed, ok := es.publishers[eventID] if !ok { return ErrFeedNotFound diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index cc263730dd..c9cdf2de00 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -71,7 +71,7 @@ func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route types.Me // SetRecipient sets the recipient for the route with the given messageID. // Errors if the route with the given messageID is not found or the route // already has a registered recipient. -func (ms *MessageServer) RegisterReceiver(messageID types.MessageID, ch chan any) error { +func (ms *MessageServer) RegisterReceiver(messageID types.MessageID, ch any) error { route, ok := ms.routes[messageID] if !ok { return ErrRouteNotFound diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index a9108c2745..1dc4660797 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -37,7 +37,7 @@ type Publisher interface { // messageRoute is the interface that supports basic message route operations. type MessageRoute interface { // RegisterRecipient sets the recipient for the route. - RegisterReceiver(ch chan any) error + RegisterReceiver(ch any) error // SendRequest sends a request to the recipient. SendRequest(msg any) error // SendResponse sends a response to the recipient. diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 2d46fb686e..049fe69cb8 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -23,6 +23,7 @@ package validator import ( "context" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -69,6 +70,8 @@ type Service[ bsb StorageBackend[ BeaconStateT, DepositT, DepositStoreT, ExecutionPayloadHeaderT, ] + // dispatcher is the dispatcher. + dispatcher *dispatcher.Dispatcher // stateProcessor is responsible for processing the state. stateProcessor StateProcessor[ BeaconBlockT, @@ -86,12 +89,12 @@ type Service[ remotePayloadBuilders []PayloadBuilder[BeaconStateT, ExecutionPayloadT] // metrics is a metrics collector. metrics *validatorMetrics - // blkBroker is a publisher for blocks. - blkBroker EventPublisher[*asynctypes.Event[BeaconBlockT]] - // sidecarBroker is a publisher for sidecars. - sidecarBroker EventPublisher[*asynctypes.Event[BlobSidecarsT]] - // newSlotSub is a feed for slots. - newSlotSub chan *asynctypes.Event[SlotDataT] + // // blkBroker is a publisher for blocks. + // blkBroker EventPublisher[*asynctypes.Event[BeaconBlockT]] + // // sidecarBroker is a publisher for sidecars. + // sidecarBroker EventPublisher[*asynctypes.Event[BlobSidecarsT]] + // // newSlotSub is a feed for slots. + // newSlotSub chan *asynctypes.Event[SlotDataT] } // NewService creates a new validator service. @@ -135,9 +138,7 @@ func NewService[ localPayloadBuilder PayloadBuilder[BeaconStateT, ExecutionPayloadT], remotePayloadBuilders []PayloadBuilder[BeaconStateT, ExecutionPayloadT], ts TelemetrySink, - blkBroker EventPublisher[*asynctypes.Event[BeaconBlockT]], - sidecarBroker EventPublisher[*asynctypes.Event[BlobSidecarsT]], - newSlotSub chan *asynctypes.Event[SlotDataT], + dispatcher *dispatcher.Dispatcher, ) *Service[ AttestationDataT, BeaconBlockT, BeaconBlockBodyT, BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, @@ -158,9 +159,7 @@ func NewService[ localPayloadBuilder: localPayloadBuilder, remotePayloadBuilders: remotePayloadBuilders, metrics: newValidatorMetrics(ts), - blkBroker: blkBroker, - sidecarBroker: sidecarBroker, - newSlotSub: newSlotSub, + dispatcher: dispatcher, } } @@ -173,32 +172,59 @@ func (s *Service[ // Start starts the service. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) Start( ctx context.Context, ) error { - go s.start(ctx) + // register a receiver channel for build block requests + buildBlockRequests := make(chan *asynctypes.Event[SlotDataT]) + s.dispatcher.RegisterMsgReceiver(events.BuildBeaconBlock, buildBlockRequests) + + // register a receiver channel for build sidecar requests + buildSidecarRequests := make(chan *asynctypes.Event[SlotDataT]) + s.dispatcher.RegisterMsgReceiver(events.BuildBlobSidecars, buildSidecarRequests) + + // start the service + go s.start(ctx, buildBlockRequests, buildSidecarRequests) return nil } // start starts the service. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) start( ctx context.Context, + buildBlockRequests chan *asynctypes.Event[SlotDataT], + buildSidecarRequests chan *asynctypes.Event[SlotDataT], ) { for { select { case <-ctx.Done(): return - case req := <-s.newSlotSub: - if req.ID() == events.NewSlot { - s.handleNewSlot(req) - } + case req := <-buildBlockRequests: + s.buildBlock(req) + case req := <-buildSidecarRequests: + s.buildSidecar(req) } } } +func (s *Service[ + _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, +]) buildBlock(req *asynctypes.Event[SlotDataT]) { + blk, sidecars, err := s.buildBlockAndSidecars( + req.Context(), req.Data(), + ) + if err != nil { + s.logger.Error("failed to build block", "err", err) + } + s.dispatcher.DispatchResponse(asynctypes.NewMessage( + req.Context(), + events.BuildBeaconBlock, + blk, + )) +} + // handleBlockRequest handles a block request. func (s *Service[ _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, diff --git a/mod/node-core/pkg/components/defaults.go b/mod/node-core/pkg/components/defaults.go index 29b5f596ff..b9ce5db064 100644 --- a/mod/node-core/pkg/components/defaults.go +++ b/mod/node-core/pkg/components/defaults.go @@ -43,6 +43,7 @@ func DefaultComponentsWithStandardTypes() []any { ProvideDepositPruner, ProvideDepositService, ProvideDepositStore, + ProvideDispatcher, ProvideEngineClient, ProvideExecutionEngine, ProvideJWTSecret, @@ -59,5 +60,7 @@ func DefaultComponentsWithStandardTypes() []any { components = append(components, DefaultNodeAPIComponents()...) components = append(components, DefaultNodeAPIHandlers()...) components = append(components, DefaultBrokerProviders()...) + components = append(components, EventServerComponents()...) + components = append(components, MessageServerComponents()...) return components } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index d7b8da64b0..1bc90a32de 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -48,3 +48,12 @@ func ProvideEventServer(in EventServerInput) *server.EventServer { func ProvideBeaconBlockFinalizedPublisher() *messaging.Publisher[*BlockMessage] { return messaging.NewPublisher[*BlockMessage](events.BeaconBlockFinalized) } + +// EventServerComponents returns all the depinject providers for the event +// server. +func EventServerComponents() []any { + return []any{ + ProvideEventServer, + ProvideBeaconBlockFinalizedPublisher, + } +} diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index f5cdb9d0aa..bf1c3ddf41 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -77,3 +77,17 @@ func ProvideProcessGenesisDataRoute() *messaging.Route[*GenesisMessage, *Validat func ProvideProcessBlobSidecarsRoute() *messaging.Route[*SidecarMessage, *SidecarMessage] { return messaging.NewRoute[*SidecarMessage, *SidecarMessage](events.VerifyBlobSidecars) } + +// MessageServerComponents returns all the depinject providers for the message +// server. +func MessageServerComponents() []any { + return []any{ + ProvideMessageServer, + ProvideBuildBlockRoute, + ProvideBuildSidecarsRoute, + ProvideVerifyBeaconBlockRoute, + ProvideFinalizeBeaconBlockRoute, + ProvideProcessGenesisDataRoute, + ProvideProcessBlobSidecarsRoute, + } +} diff --git a/mod/node-core/pkg/components/validator_service.go b/mod/node-core/pkg/components/validator_service.go index 891e0574b7..4b2d552152 100644 --- a/mod/node-core/pkg/components/validator_service.go +++ b/mod/node-core/pkg/components/validator_service.go @@ -23,6 +23,7 @@ package components import ( "cosmossdk.io/depinject" sdklog "cosmossdk.io/log" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" "github.com/berachain/beacon-kit/mod/beacon/validator" "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/log" @@ -34,30 +35,23 @@ import ( // ValidatorServiceInput is the input for the validator service provider. type ValidatorServiceInput struct { depinject.In - BeaconBlockFeed *BlockBroker - BlobProcessor *BlobProcessor - Cfg *config.Config - ChainSpec common.ChainSpec - LocalBuilder *LocalBuilder - Logger log.AdvancedLogger[any, sdklog.Logger] - StateProcessor *StateProcessor - StorageBackend *StorageBackend - Signer crypto.BLSSigner - SidecarsFeed *SidecarsBroker - SidecarFactory *SidecarFactory - SlotBroker *SlotBroker - TelemetrySink *metrics.TelemetrySink + BlobProcessor *BlobProcessor + Cfg *config.Config + ChainSpec common.ChainSpec + Dispatcher *dispatcher.Dispatcher + LocalBuilder *LocalBuilder + Logger log.AdvancedLogger[any, sdklog.Logger] + StateProcessor *StateProcessor + StorageBackend *StorageBackend + Signer crypto.BLSSigner + SidecarFactory *SidecarFactory + TelemetrySink *metrics.TelemetrySink } // ProvideValidatorService is a depinject provider for the validator service. func ProvideValidatorService( in ValidatorServiceInput, ) (*ValidatorService, error) { - slotSubscription, err := in.SlotBroker.Subscribe() - if err != nil { - in.Logger.Error("failed to subscribe to slot feed", "err", err) - return nil, err - } // Build the builder service. return validator.NewService[ *AttestationData, @@ -86,8 +80,6 @@ func ProvideValidatorService( in.LocalBuilder, }, in.TelemetrySink, - in.BeaconBlockFeed, - in.SidecarsFeed, - slotSubscription, + in.Dispatcher, ), nil } From 9df876990965c9a9213eb374cf99f009d06d4803 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 14:34:39 -0400 Subject: [PATCH 06/75] dispatch validator --- mod/async/pkg/dispatcher/dispatcher.go | 6 ++ mod/beacon/block_store/service.go | 4 +- mod/beacon/blockchain/process.go | 4 +- mod/beacon/blockchain/service.go | 14 +-- mod/beacon/validator/block_builder.go | 14 +-- mod/beacon/validator/service.go | 89 +++++++---------- mod/beacon/validator/types.go | 11 +++ mod/da/pkg/da/service.go | 10 +- mod/da/pkg/types/bundle.go | 24 +++++ mod/execution/pkg/deposit/sync.go | 4 +- mod/node-core/pkg/components/events.go | 4 +- mod/node-core/pkg/components/messages.go | 24 ++--- mod/node-core/pkg/components/middleware.go | 2 +- mod/node-core/pkg/components/types.go | 3 + .../pkg/components/validator_service.go | 1 + .../events.go => messages/messages.go} | 13 ++- mod/runtime/pkg/middleware/abci.go | 96 +++++++------------ mod/runtime/pkg/middleware/middleware.go | 10 +- mod/runtime/pkg/middleware/types.go | 6 ++ mod/storage/pkg/pruner/pruner.go | 4 +- 20 files changed, 171 insertions(+), 172 deletions(-) create mode 100644 mod/da/pkg/types/bundle.go rename mod/primitives/pkg/{events/events.go => messages/messages.go} (85%) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 567ab5f21f..46c3cda9de 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -52,16 +52,22 @@ func (d *Dispatcher) Start(ctx context.Context) error { } // DispatchEvent dispatches the given event to the event server. +// It will error if the type is inconsistent with the publisher +// registered for the given eventID. func (d *Dispatcher) DispatchEvent(ctx context.Context, event types.MessageI) error { return d.eventServer.Publish(ctx, event) } // DispatchRequest dispatches the given request to the message server. +// It will error if the and types are inconsistent with the +// route registered for the given messageID. func (d *Dispatcher) DispatchRequest(req types.MessageI, resp types.MessageI) error { return d.msgServer.Request(req, resp) } // DispatchResponse dispatches the given response to the message server. +// It will error if the type is inconsistent with the route registered +// for the given messageID. func (d *Dispatcher) DispatchResponse(resp types.MessageI) error { return d.msgServer.Respond(resp) } diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 859b25e63e..ee9d07c4af 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -25,7 +25,7 @@ import ( asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // NewService creates a new block service. @@ -89,7 +89,7 @@ func (s *Service[BeaconBlockT, _]) listenAndStore( case <-ctx.Done(): return case msg := <-subBlkCh: - if msg.Is(events.BeaconBlockFinalized) { + if msg.Is(messages.BeaconBlockFinalized) { slot := msg.Data().GetSlot() if err := s.store.Set(slot, msg.Data()); err != nil { s.logger.Error( diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index b4beb943cb..de0575bce1 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -25,7 +25,7 @@ import ( "time" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -85,7 +85,7 @@ func (s *Service[ // via ticker later. if err = s.blkBroker.Publish(ctx, asynctypes.NewEvent( - ctx, events.BeaconBlockFinalized, blk, + ctx, messages.BeaconBlockFinalized, blk, ), ); err != nil { return nil, err diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 6d6cd99afe..9e3706be32 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -28,7 +28,7 @@ import ( gethprimitives "github.com/berachain/beacon-kit/mod/geth-primitives" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -200,13 +200,13 @@ func (s *Service[ return case msg := <-subBlkCh: switch msg.ID() { - case events.BeaconBlockReceived: + case messages.BeaconBlockReceived: s.handleBeaconBlockReceived(msg) - case events.BeaconBlockFinalizedRequest: + case messages.BeaconBlockFinalizedRequest: s.handleBeaconBlockFinalization(msg) } case msg := <-subGenCh: - if msg.ID() == events.GenesisDataProcessRequest { + if msg.ID() == messages.GenesisDataProcessRequest { s.handleProcessGenesisDataRequest(msg) } } @@ -232,7 +232,7 @@ func (s *Service[ msg.Context(), asynctypes.NewEvent( msg.Context(), - events.ValidatorSetUpdated, + messages.ValidatorSetUpdated, valUpdates, err, ), @@ -261,7 +261,7 @@ func (s *Service[ msg.Context(), asynctypes.NewEvent( msg.Context(), - events.BeaconBlockVerified, + messages.BeaconBlockVerified, msg.Data(), s.VerifyIncomingBlock(msg.Context(), msg.Data()), ), @@ -292,7 +292,7 @@ func (s *Service[ msg.Context(), asynctypes.NewEvent( msg.Context(), - events.ValidatorSetUpdated, + messages.ValidatorSetUpdated, valUpdates, err, )); err != nil { diff --git a/mod/beacon/validator/block_builder.go b/mod/beacon/validator/block_builder.go index c2e41e808f..1a3f25cc42 100644 --- a/mod/beacon/validator/block_builder.go +++ b/mod/beacon/validator/block_builder.go @@ -38,7 +38,7 @@ import ( // buildBlockAndSidecars builds a new beacon block. func (s *Service[ - AttestationDataT, BeaconBlockT, _, _, + AttestationDataT, BeaconBlockT, _, _, _, BlobSidecarsT, _, _, _, _, _, _, SlashingInfoT, SlotDataT, ]) buildBlockAndSidecars( ctx context.Context, @@ -135,7 +135,7 @@ func (s *Service[ // getEmptyBeaconBlockForSlot creates a new empty block. func (s *Service[ - _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, ]) getEmptyBeaconBlockForSlot( st BeaconStateT, requestedSlot math.Slot, ) (BeaconBlockT, error) { @@ -173,7 +173,7 @@ func (s *Service[ // buildRandaoReveal builds a randao reveal for the given slot. func (s *Service[ - _, _, _, BeaconStateT, _, _, _, _, _, _, ForkDataT, _, _, + _, _, _, _, BeaconStateT, _, _, _, _, _, _, ForkDataT, _, _, ]) buildRandaoReveal( st BeaconStateT, slot math.Slot, @@ -201,7 +201,7 @@ func (s *Service[ // retrieveExecutionPayload retrieves the execution payload for the block. func (s *Service[ - _, BeaconBlockT, _, BeaconStateT, _, _, _, _, + _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, ExecutionPayloadT, ExecutionPayloadHeaderT, _, _, _, ]) retrieveExecutionPayload( ctx context.Context, st BeaconStateT, blk BeaconBlockT, @@ -258,7 +258,7 @@ func (s *Service[ // BuildBlockBody assembles the block body with necessary components. func (s *Service[ - AttestationDataT, BeaconBlockT, _, BeaconStateT, _, + AttestationDataT, BeaconBlockT, _, _, BeaconStateT, _, _, _, Eth1DataT, ExecutionPayloadT, _, _, SlashingInfoT, SlotDataT, ]) buildBlockBody( _ context.Context, @@ -334,7 +334,7 @@ func (s *Service[ // computeAndSetStateRoot computes the state root of an outgoing block // and sets it in the block. func (s *Service[ - _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, ]) computeAndSetStateRoot( ctx context.Context, st BeaconStateT, @@ -355,7 +355,7 @@ func (s *Service[ // computeStateRoot computes the state root of an outgoing block. func (s *Service[ - _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, ]) computeStateRoot( ctx context.Context, st BeaconStateT, diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 049fe69cb8..17335787bf 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -28,7 +28,7 @@ import ( "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -39,6 +39,9 @@ type Service[ AttestationDataT, BeaconBlockT, BeaconBlockBodyT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], + BlockDataT BeaconBlockBundle[ + BeaconBlockT, BlobSidecarsT, + ], BeaconBlockBodyT BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], @@ -104,6 +107,9 @@ func NewService[ AttestationDataT, BeaconBlockT, BeaconBlockBodyT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], + BeaconBlockBundleT BeaconBlockBundle[ + BeaconBlockT, BlobSidecarsT, + ], BeaconBlockBodyT BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], @@ -140,12 +146,12 @@ func NewService[ ts TelemetrySink, dispatcher *dispatcher.Dispatcher, ) *Service[ - AttestationDataT, BeaconBlockT, BeaconBlockBodyT, BeaconStateT, + AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, SlotDataT, ] { return &Service[ - AttestationDataT, BeaconBlockT, BeaconBlockBodyT, BeaconStateT, + AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, SlotDataT, ]{ @@ -165,95 +171,64 @@ func NewService[ // Name returns the name of the service. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, ]) Name() string { return "validator" } -// Start starts the service. +// Start starts the service registers this service with the +// BuildBeaconBlockAndSidecars route and begins listening for requests. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, + _, _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) Start( ctx context.Context, ) error { // register a receiver channel for build block requests - buildBlockRequests := make(chan *asynctypes.Event[SlotDataT]) - s.dispatcher.RegisterMsgReceiver(events.BuildBeaconBlock, buildBlockRequests) - - // register a receiver channel for build sidecar requests - buildSidecarRequests := make(chan *asynctypes.Event[SlotDataT]) - s.dispatcher.RegisterMsgReceiver(events.BuildBlobSidecars, buildSidecarRequests) + buildBlockRequests := make(chan *asynctypes.Message[SlotDataT]) + s.dispatcher.RegisterMsgReceiver(messages.BuildBeaconBlockAndSidecars, buildBlockRequests) // start the service - go s.start(ctx, buildBlockRequests, buildSidecarRequests) + go s.start(ctx, buildBlockRequests) return nil } // start starts the service. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, + _, _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) start( ctx context.Context, - buildBlockRequests chan *asynctypes.Event[SlotDataT], - buildSidecarRequests chan *asynctypes.Event[SlotDataT], + buildBlockBundleRequests chan *asynctypes.Message[SlotDataT], ) { for { select { case <-ctx.Done(): return - case req := <-buildBlockRequests: - s.buildBlock(req) - case req := <-buildSidecarRequests: - s.buildSidecar(req) + case req := <-buildBlockBundleRequests: + s.handleBuildBlockBundleRequest(req) } } } +// handleBuildBlockBundleRequest builds a block and sidecars for the requested +// slot data and dispatches a response containing the built block and sidecars. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, -]) buildBlock(req *asynctypes.Event[SlotDataT]) { + _, _, BeaconBlockBundleT, _, _, _, _, _, _, _, _, _, _, SlotDataT, +]) handleBuildBlockBundleRequest(req *asynctypes.Message[SlotDataT]) { + // build the block and sidecars for the requested slot data blk, sidecars, err := s.buildBlockAndSidecars( req.Context(), req.Data(), ) if err != nil { s.logger.Error("failed to build block", "err", err) } + + // bundle the block and sidecars and dispatch the response + var blkData BeaconBlockBundleT + blkData.SetBeaconBlock(blk) + blkData.SetSidecars(sidecars) s.dispatcher.DispatchResponse(asynctypes.NewMessage( req.Context(), - events.BuildBeaconBlock, - blk, + messages.BuildBeaconBlockAndSidecars, + blkData, )) } - -// handleBlockRequest handles a block request. -func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, -]) handleNewSlot(msg *asynctypes.Event[SlotDataT]) { - blk, sidecars, err := s.buildBlockAndSidecars( - msg.Context(), msg.Data(), - ) - if err != nil { - s.logger.Error("failed to build block", "err", err) - } - - // Publish our built block to the broker. - if blkErr := s.blkBroker.Publish( - msg.Context(), - asynctypes.NewEvent( - msg.Context(), events.BeaconBlockBuilt, blk, err, - )); blkErr != nil { - // Propagate the error from buildBlockAndSidecars - s.logger.Error("failed to publish block", "err", err) - } - - // Publish our built blobs to the broker. - if sidecarsErr := s.sidecarBroker.Publish( - msg.Context(), - asynctypes.NewEvent( - // Propagate the error from buildBlockAndSidecars - msg.Context(), events.BlobSidecarsBuilt, sidecars, err, - ), - ); sidecarsErr != nil { - s.logger.Error("failed to publish sidecars", "err", err) - } -} diff --git a/mod/beacon/validator/types.go b/mod/beacon/validator/types.go index cdbbd327f8..758c248b3a 100644 --- a/mod/beacon/validator/types.go +++ b/mod/beacon/validator/types.go @@ -66,6 +66,17 @@ type BeaconBlock[ GetBody() BeaconBlockBodyT } +// BeaconBlockBundle represents a block data interface. +type BeaconBlockBundle[ + BeaconBlockT any, + BlobSidecarsT any, +] interface { + GetBeaconBlock() BeaconBlockT + GetSidecars() BlobSidecarsT + SetBeaconBlock(BeaconBlockT) + SetSidecars(BlobSidecarsT) +} + // BeaconBlockBody represents a beacon block body interface. type BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT any, diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 115b7c3bfe..3eb906b9f2 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -25,7 +25,7 @@ import ( asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) type Service[ @@ -104,9 +104,9 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) start( return case msg := <-sidecarsCh: switch msg.ID() { - case events.BlobSidecarsProcessRequest: + case messages.BlobSidecarsProcessRequest: s.handleBlobSidecarsProcessRequest(msg) - case events.BlobSidecarsReceived: + case messages.BlobSidecarsReceived: s.handleBlobSidecarsReceived(msg) } } @@ -131,7 +131,7 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) handleBlobSidecarsProcessRequest( if err = s.sidecarsBroker.Publish( msg.Context(), asynctypes.NewEvent( - msg.Context(), events.BlobSidecarsProcessed, msg.Data(), err, + msg.Context(), messages.BlobSidecarsProcessed, msg.Data(), err, )); err != nil { s.logger.Error( "Failed to publish blob sidecars processed event", @@ -158,7 +158,7 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) handleBlobSidecarsReceived( if err = s.sidecarsBroker.Publish( msg.Context(), asynctypes.NewEvent( - msg.Context(), events.BlobSidecarsProcessed, msg.Data(), err, + msg.Context(), messages.BlobSidecarsProcessed, msg.Data(), err, )); err != nil { s.logger.Error( "Failed to publish blob sidecars processed event", diff --git a/mod/da/pkg/types/bundle.go b/mod/da/pkg/types/bundle.go new file mode 100644 index 0000000000..6742eb770b --- /dev/null +++ b/mod/da/pkg/types/bundle.go @@ -0,0 +1,24 @@ +package types + +import "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" + +type BlockBundle struct { + Block *types.BeaconBlock + Sidecars *BlobSidecars +} + +func (bb *BlockBundle) GetBeaconBlock() *types.BeaconBlock { + return bb.Block +} + +func (bb *BlockBundle) GetSidecars() *BlobSidecars { + return bb.Sidecars +} + +func (bb *BlockBundle) SetBeaconBlock(block *types.BeaconBlock) { + bb.Block = block +} + +func (bb *BlockBundle) SetSidecars(sidecars *BlobSidecars) { + bb.Sidecars = sidecars +} diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index 39fcde2b2c..e088b7b04d 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -24,8 +24,8 @@ import ( "context" "time" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // defaultRetryInterval processes a deposit event. @@ -40,7 +40,7 @@ func (s *Service[ case <-ctx.Done(): return case msg := <-s.feed: - if msg.Is(events.BeaconBlockFinalized) { + if msg.Is(messages.BeaconBlockFinalized) { blockNum := msg.Data(). GetBody().GetExecutionPayload().GetNumber() s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 1bc90a32de..ce8ed4b475 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -24,7 +24,7 @@ import ( "cosmossdk.io/depinject" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" "github.com/berachain/beacon-kit/mod/async/pkg/server" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // EventServerInput is the input for the event server. @@ -46,7 +46,7 @@ func ProvideEventServer(in EventServerInput) *server.EventServer { // ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block // finalized events. func ProvideBeaconBlockFinalizedPublisher() *messaging.Publisher[*BlockMessage] { - return messaging.NewPublisher[*BlockMessage](events.BeaconBlockFinalized) + return messaging.NewPublisher[*BlockMessage](messages.BeaconBlockFinalized) } // EventServerComponents returns all the depinject providers for the event diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index bf1c3ddf41..99e07435ea 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -24,7 +24,7 @@ import ( "cosmossdk.io/depinject" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" "github.com/berachain/beacon-kit/mod/async/pkg/server" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) type MessageServerInput struct { @@ -48,34 +48,29 @@ func ProvideMessageServer(in MessageServerInput) *server.MessageServer { return ms } -// ProvideBuildBlockRoute provides a route for building a beacon block. -func ProvideBuildBlockRoute() *messaging.Route[*SlotMessage, *BlockMessage] { - return messaging.NewRoute[*SlotMessage, *BlockMessage](events.BuildBeaconBlock) -} - -// ProvideBuildSidecarsRoute provides a route for building sidecars. -func ProvideBuildSidecarsRoute() *messaging.Route[*SlotMessage, *SidecarMessage] { - return messaging.NewRoute[*SlotMessage, *SidecarMessage](events.BuildBlobSidecars) +// ProvideBuildBlockAndSidecarsRoute provides a route for building a beacon block. +func ProvideBuildBlockAndSidecarsRoute() *messaging.Route[*SlotMessage, *BlockMessage] { + return messaging.NewRoute[*SlotMessage, *BlockMessage](messages.BuildBeaconBlockAndSidecars) } // ProvideVerifyBeaconBlockRoute provides a route for verifying a beacon block. func ProvideVerifyBeaconBlockRoute() *messaging.Route[*BlockMessage, *BlockMessage] { - return messaging.NewRoute[*BlockMessage, *BlockMessage](events.VerifyBeaconBlock) + return messaging.NewRoute[*BlockMessage, *BlockMessage](messages.VerifyBeaconBlock) } // ProvideFinalizeBeaconBlockRoute provides a route for finalizing a beacon block. func ProvideFinalizeBeaconBlockRoute() *messaging.Route[*BlockMessage, *ValidatorUpdateMessage] { - return messaging.NewRoute[*BlockMessage, *ValidatorUpdateMessage](events.FinalizeBeaconBlock) + return messaging.NewRoute[*BlockMessage, *ValidatorUpdateMessage](messages.FinalizeBeaconBlock) } // ProvideProcessGenesisDataRoute provides a route for processing genesis data. func ProvideProcessGenesisDataRoute() *messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] { - return messaging.NewRoute[*GenesisMessage, *ValidatorUpdateMessage](events.ProcessGenesisData) + return messaging.NewRoute[*GenesisMessage, *ValidatorUpdateMessage](messages.ProcessGenesisData) } // ProvideProcessBlobSidecarsRoute provides a route for processing blob sidecars. func ProvideProcessBlobSidecarsRoute() *messaging.Route[*SidecarMessage, *SidecarMessage] { - return messaging.NewRoute[*SidecarMessage, *SidecarMessage](events.VerifyBlobSidecars) + return messaging.NewRoute[*SidecarMessage, *SidecarMessage](messages.VerifyBlobSidecars) } // MessageServerComponents returns all the depinject providers for the message @@ -83,8 +78,7 @@ func ProvideProcessBlobSidecarsRoute() *messaging.Route[*SidecarMessage, *Sideca func MessageServerComponents() []any { return []any{ ProvideMessageServer, - ProvideBuildBlockRoute, - ProvideBuildSidecarsRoute, + ProvideBuildBlockAndSidecarsRoute, ProvideVerifyBeaconBlockRoute, ProvideFinalizeBeaconBlockRoute, ProvideProcessGenesisDataRoute, diff --git a/mod/node-core/pkg/components/middleware.go b/mod/node-core/pkg/components/middleware.go index 82d5688fba..f37ba3e688 100644 --- a/mod/node-core/pkg/components/middleware.go +++ b/mod/node-core/pkg/components/middleware.go @@ -44,7 +44,7 @@ func ProvideABCIMiddleware( in ABCIMiddlewareInput, ) (*ABCIMiddleware, error) { return middleware.NewABCIMiddleware[ - *AvailabilityStore, *BeaconBlock, *BlobSidecars, + *AvailabilityStore, *BeaconBlock, *BeaconBlockBundle, *BlobSidecars, *Deposit, *ExecutionPayload, *Genesis, *SlotData, ]( in.ChainSpec, diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index a75d7fa40b..1a8c7db4b5 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -74,6 +74,7 @@ type ( ABCIMiddleware = middleware.ABCIMiddleware[ *AvailabilityStore, *BeaconBlock, + *BeaconBlockBundle, *BlobSidecars, *Deposit, *ExecutionPayload, @@ -96,6 +97,7 @@ type ( // BeaconBlock type aliases. BeaconBlock = types.BeaconBlock + BeaconBlockBundle = datypes.BlockBundle BeaconBlockBody = types.BeaconBlockBody BeaconBlockHeader = types.BeaconBlockHeader @@ -387,6 +389,7 @@ type ( ValidatorService = validator.Service[ *AttestationData, *BeaconBlock, + *BeaconBlockBundle, *BeaconBlockBody, *BeaconState, *BlobSidecars, diff --git a/mod/node-core/pkg/components/validator_service.go b/mod/node-core/pkg/components/validator_service.go index 4b2d552152..e3c66a3723 100644 --- a/mod/node-core/pkg/components/validator_service.go +++ b/mod/node-core/pkg/components/validator_service.go @@ -56,6 +56,7 @@ func ProvideValidatorService( return validator.NewService[ *AttestationData, *BeaconBlock, + *BeaconBlockBundle, *BeaconBlockBody, *BeaconState, *BlobSidecars, diff --git a/mod/primitives/pkg/events/events.go b/mod/primitives/pkg/messages/messages.go similarity index 85% rename from mod/primitives/pkg/events/events.go rename to mod/primitives/pkg/messages/messages.go index 9454098edd..412cff243a 100644 --- a/mod/primitives/pkg/events/events.go +++ b/mod/primitives/pkg/messages/messages.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package events +package messages // TODO: delete const ( @@ -37,12 +37,11 @@ const ( // messages const ( - BuildBeaconBlock = "build-beacon-block" - BuildBlobSidecars = "build-blob-sidecars" - VerifyBeaconBlock = "verify-beacon-block" - FinalizeBeaconBlock = "finalize-beacon-block" - ProcessGenesisData = "process-genesis-data" - VerifyBlobSidecars = "process-blob-sidecars" + BuildBeaconBlockAndSidecars = "build-beacon-block" + VerifyBeaconBlock = "verify-beacon-block" + FinalizeBeaconBlock = "finalize-beacon-block" + ProcessGenesisData = "process-genesis-data" + VerifyBlobSidecars = "process-blob-sidecars" ) // events diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 9802305bbc..6a7b84de49 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -27,8 +27,8 @@ import ( asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" cmtabci "github.com/cometbft/cometbft/abci/types" @@ -41,7 +41,7 @@ import ( // InitGenesis is called by the base app to initialize the state of the. func (h *ABCIMiddleware[ - _, _, _, _, _, GenesisT, _, + _, _, _, _, _, _, GenesisT, _, ]) InitGenesis( ctx context.Context, bz []byte, @@ -56,7 +56,7 @@ func (h *ABCIMiddleware[ err := h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, events.GenesisDataProcessRequest, *data, + ctx, messages.GenesisDataProcessRequest, *data, ), &valUpdateResp, ) if err != nil { @@ -72,73 +72,51 @@ func (h *ABCIMiddleware[ // prepareProposal is the internal handler for preparing proposals. func (h *ABCIMiddleware[ - _, BeaconBlockT, BlobSidecarsT, _, _, _, SlotDataT, + _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, SlotDataT, ]) PrepareProposal( ctx context.Context, slotData SlotDataT, ) ([]byte, []byte, error) { var ( - startTime = time.Now() - beaconBlockErr, sidecarsErr error - beaconBlockBz, sidecarsBz []byte - beaconBlockResp asynctypes.Message[BeaconBlockT] - sidecarsResp asynctypes.Message[BlobSidecarsT] + startTime = time.Now() + beaconBlkBundleResp asynctypes.Message[BeaconBlockBundleT] ) defer h.metrics.measurePrepareProposalDuration(startTime) // request a built beacon block for the given slot h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, events.BuildBeaconBlock, slotData, - ), &beaconBlockResp, - ) - - // handle the beacon block response - beaconBlockBz, beaconBlockErr = h.handleBeaconBlockResponse(ctx, beaconBlockResp) - if beaconBlockErr != nil { - return nil, nil, beaconBlockErr - } - - // request the built blob sidecars - h.dispatcher.DispatchRequest( - asynctypes.NewMessage( - ctx, events.BuildBlobSidecars, slotData, - ), &sidecarsResp, + ctx, messages.BuildBeaconBlockAndSidecars, slotData, + ), &beaconBlkBundleResp, ) - // handle the sidecars response - sidecarsBz, sidecarsErr = h.handleSidecarResponse(ctx, sidecarsResp) - if sidecarsErr != nil { - return nil, nil, sidecarsErr - } - - return beaconBlockBz, sidecarsBz, nil + // gossip the built beacon block and blob sidecars + return h.handleBeaconBlockBundleResponse(ctx, beaconBlkBundleResp) } -// handleSidecarResponse publishes the sidecars to the gossiper. +// handleBeaconBlockBundleResponse gossips the built beacon block and blob +// sidecars to the network. func (h *ABCIMiddleware[ - _, _, BlobSidecarsT, _, _, _, _, -]) handleSidecarResponse( + _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, _, +]) handleBeaconBlockBundleResponse( ctx context.Context, - sidecarsResp asynctypes.Message[BlobSidecarsT], -) ([]byte, error) { - if sidecarsResp.Error() != nil { - return nil, sidecarsResp.Error() + bbbResp asynctypes.Message[BeaconBlockBundleT], +) ([]byte, []byte, error) { + // handle response errors + if bbbResp.Error() != nil { + return nil, nil, bbbResp.Error() } - return h.blobGossiper.Publish(ctx, sidecarsResp.Data()) -} - -// handleBeaconBlockResponse publishes the beacon block to the gossiper. -func (h *ABCIMiddleware[ - _, BeaconBlockT, _, _, _, _, _, -]) handleBeaconBlockResponse( - ctx context.Context, - beaconBlockResp asynctypes.Message[BeaconBlockT], -) ([]byte, error) { - if beaconBlockResp.Error() != nil { - return nil, beaconBlockResp.Error() + // gossip beacon block + bbBz, bbErr := h.beaconBlockGossiper.Publish(ctx, bbbResp.Data().GetBeaconBlock()) + if bbErr != nil { + return nil, nil, bbErr + } + // gossip blob sidecars + scBz, scErr := h.blobGossiper.Publish(ctx, bbbResp.Data().GetSidecars()) + if scErr != nil { + return nil, nil, scErr } - return h.beaconBlockGossiper.Publish(ctx, beaconBlockResp.Data()) + return bbBz, scBz, nil } /* -------------------------------------------------------------------------- */ @@ -148,7 +126,7 @@ func (h *ABCIMiddleware[ // ProcessProposal processes the proposal for the ABCI middleware. // It handles both the beacon block and blob sidecars concurrently. func (h *ABCIMiddleware[ - _, BeaconBlockT, BlobSidecarsT, _, _, _, _, + _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, ]) ProcessProposal( ctx context.Context, req proto.Message, @@ -176,7 +154,7 @@ func (h *ABCIMiddleware[ // verify the beacon block h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, events.VerifyBeaconBlock, blk, + ctx, messages.VerifyBeaconBlock, blk, ), &beaconBlockResp, ) @@ -192,7 +170,7 @@ func (h *ABCIMiddleware[ // verify the blob sidecars h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, events.VerifyBlobSidecars, sidecars, + ctx, messages.VerifyBlobSidecars, sidecars, ), &sidecarsResp, ) @@ -206,7 +184,7 @@ func (h *ABCIMiddleware[ // createResponse generates the appropriate ProcessProposalResponse based on the // error. func (*ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, + _, BeaconBlockT, _, _, BlobSidecarsT, _, _, _, ]) createProcessProposalResponse(err error) (proto.Message, error) { status := cmtabci.PROCESS_PROPOSAL_STATUS_REJECT if !errors.IsFatal(err) { @@ -224,7 +202,7 @@ func (*ABCIMiddleware[ // is responsible for aggregating oracle data from each validator and writing // the oracle data to the store. func (h *ABCIMiddleware[ - _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, ]) PreBlock( _ context.Context, req proto.Message, ) error { @@ -239,7 +217,7 @@ func (h *ABCIMiddleware[ // EndBlock returns the validator set updates from the beacon state. func (h *ABCIMiddleware[ - _, BeaconBlockT, BlobSidecarsT, _, _, _, _, + _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, ]) EndBlock( ctx context.Context, ) (transition.ValidatorUpdates, error) { @@ -264,7 +242,7 @@ func (h *ABCIMiddleware[ // verify the blob sidecars h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, events.VerifyBlobSidecars, blobs, + ctx, messages.VerifyBlobSidecars, blobs, ), &sidecarsResp, ) if sidecarsResp.Error() != nil { @@ -273,7 +251,7 @@ func (h *ABCIMiddleware[ h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, events.FinalizeBeaconBlock, blk, + ctx, messages.FinalizeBeaconBlock, blk, ), &valUpdatesResp, ) diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 1a346a5aa5..7c8fab794a 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -38,6 +38,7 @@ import ( type ABCIMiddleware[ AvailabilityStoreT any, BeaconBlockT BeaconBlock[BeaconBlockT], + BeaconBlockBundleT BeaconBlockBundle[BeaconBlockT, BlobSidecarsT], BlobSidecarsT interface { constraints.SSZMarshallable Empty() BlobSidecarsT @@ -79,6 +80,7 @@ type ABCIMiddleware[ func NewABCIMiddleware[ AvailabilityStoreT any, BeaconBlockT BeaconBlock[BeaconBlockT], + BeaconBlockBundleT BeaconBlockBundle[BeaconBlockT, BlobSidecarsT], BlobSidecarsT interface { constraints.SSZMarshallable Empty() BlobSidecarsT @@ -93,11 +95,11 @@ func NewABCIMiddleware[ telemetrySink TelemetrySink, dispatcher *dispatcher.Dispatcher, ) *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BlobSidecarsT, DepositT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ] { return &ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BlobSidecarsT, DepositT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ]{ chainSpec: chainSpec, @@ -118,7 +120,7 @@ func NewABCIMiddleware[ // Name returns the name of the middleware. func (am *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BlobSidecarsT, DepositT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ]) Name() string { return "abci-middleware" @@ -126,7 +128,7 @@ func (am *ABCIMiddleware[ // Start the middleware. func (am *ABCIMiddleware[ - _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, ]) Start(ctx context.Context) error { return nil } diff --git a/mod/runtime/pkg/middleware/types.go b/mod/runtime/pkg/middleware/types.go index 77bb32831f..01e0674d35 100644 --- a/mod/runtime/pkg/middleware/types.go +++ b/mod/runtime/pkg/middleware/types.go @@ -39,6 +39,12 @@ type BeaconBlock[SelfT any] interface { NewFromSSZ([]byte, uint32) (SelfT, error) } +// BeaconBlockBundle +type BeaconBlockBundle[BeaconBlockT any, BlobSidecarsT any] interface { + GetBeaconBlock() BeaconBlockT + GetSidecars() BlobSidecarsT +} + // TelemetrySink is an interface for sending metrics to a telemetry backend. type TelemetrySink interface { // MeasureSince measures the time since the given time. diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 0a7aa757fd..89bcff6d9c 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -30,7 +30,7 @@ import ( "context" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // Compile-time check to ensure pruner implements the Pruner interface. @@ -85,7 +85,7 @@ func (p *pruner[_, _, _]) start(ctx context.Context) { case <-ctx.Done(): return case event := <-p.feed: - if event.Is(events.BeaconBlockFinalized) { + if event.Is(messages.BeaconBlockFinalized) { start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { p.logger.Error("‼️ error pruning index ‼️", "error", err) From bf8787be86ee1fe9fd596ce8b3218bda476fd40c Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 14:46:21 -0400 Subject: [PATCH 07/75] types --- mod/node-core/pkg/components/block_store.go | 10 ++-- mod/node-core/pkg/components/dispatcher.go | 7 +-- mod/node-core/pkg/components/events.go | 6 +- mod/node-core/pkg/components/messages.go | 28 +++++----- mod/node-core/pkg/components/middleware.go | 3 +- mod/node-core/pkg/components/types.go | 55 ++++++++++++++++--- .../pkg/components/validator_service.go | 3 +- 7 files changed, 74 insertions(+), 38 deletions(-) diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index eb1eab57bf..3b2c9b4d98 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -24,6 +24,7 @@ import ( "cosmossdk.io/depinject" "cosmossdk.io/log" storev2 "cosmossdk.io/store/v2/db" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" blockservice "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" @@ -59,11 +60,10 @@ func ProvideBlockStore( // BlockPrunerInput is the input for the block pruner. type BlockPrunerInput struct { depinject.In - - BlockBroker *BlockBroker - BlockStore *BlockStore - Config *config.Config - Logger log.Logger + BlockStore *BlockStore + Config *config.Config + Dispatcher *dispatcher.Dispatcher + Logger log.Logger } // ProvideBlockPruner provides a block pruner for the depinject framework. diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index 3d48808e65..55a5835ffc 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -23,19 +23,18 @@ package components import ( "cosmossdk.io/depinject" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/server" ) // DispatcherInput is the input for the Dispatcher. type DispatcherInput struct { depinject.In - EventServer *server.EventServer - MessageServer *server.MessageServer + EventServer *EventServer + MessageServer *MessageServer } // ProvideDispatcher provides a new Dispatcher. func ProvideDispatcher( in DispatcherInput, -) *dispatcher.Dispatcher { +) *Dispatcher { return dispatcher.NewDispatcher(in.EventServer, in.MessageServer) } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index ce8ed4b475..94d2de7417 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -30,11 +30,11 @@ import ( // EventServerInput is the input for the event server. type EventServerInput struct { depinject.In - BeaconBlockFinalizedPublisher *messaging.Publisher[*BlockMessage] + BeaconBlockFinalizedPublisher *BeaconBlockFinalizedPublisher } // ProvideEventServer provides an event server. -func ProvideEventServer(in EventServerInput) *server.EventServer { +func ProvideEventServer(in EventServerInput) *EventServer { es := server.NewEventServer() es.RegisterPublisher( in.BeaconBlockFinalizedPublisher.EventID(), @@ -45,7 +45,7 @@ func ProvideEventServer(in EventServerInput) *server.EventServer { // ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block // finalized events. -func ProvideBeaconBlockFinalizedPublisher() *messaging.Publisher[*BlockMessage] { +func ProvideBeaconBlockFinalizedPublisher() *BeaconBlockFinalizedPublisher { return messaging.NewPublisher[*BlockMessage](messages.BeaconBlockFinalized) } diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index 99e07435ea..6ab40704af 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -29,18 +29,16 @@ import ( type MessageServerInput struct { depinject.In - BuildBlockRoute *messaging.Route[*SlotMessage, *BlockMessage] - BuildSidecarsRoute *messaging.Route[*SlotMessage, *SidecarMessage] - VerifyBlockRoute *messaging.Route[*BlockMessage, *BlockMessage] - FinalizeBlockRoute *messaging.Route[*BlockMessage, *ValidatorUpdateMessage] - ProcessGenesisDataRoute *messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] - ProcessBlobSidecarsRoute *messaging.Route[*SidecarMessage, *SidecarMessage] + BuildBlockAndSidecarsRoute *BuildBlockAndSidecarsRoute + VerifyBlockRoute *VerifyBlockRoute + FinalizeBlockRoute *FinalizeBlockRoute + ProcessGenesisDataRoute *ProcessGenesisDataRoute + ProcessBlobSidecarsRoute *ProcessBlobSidecarsRoute } -func ProvideMessageServer(in MessageServerInput) *server.MessageServer { +func ProvideMessageServer(in MessageServerInput) *MessageServer { ms := server.NewMessageServer() - ms.RegisterRoute(in.BuildBlockRoute.MessageID(), in.BuildBlockRoute) - ms.RegisterRoute(in.BuildSidecarsRoute.MessageID(), in.BuildSidecarsRoute) + ms.RegisterRoute(in.BuildBlockAndSidecarsRoute.MessageID(), in.BuildBlockAndSidecarsRoute) ms.RegisterRoute(in.VerifyBlockRoute.MessageID(), in.VerifyBlockRoute) ms.RegisterRoute(in.FinalizeBlockRoute.MessageID(), in.FinalizeBlockRoute) ms.RegisterRoute(in.ProcessGenesisDataRoute.MessageID(), in.ProcessGenesisDataRoute) @@ -49,27 +47,27 @@ func ProvideMessageServer(in MessageServerInput) *server.MessageServer { } // ProvideBuildBlockAndSidecarsRoute provides a route for building a beacon block. -func ProvideBuildBlockAndSidecarsRoute() *messaging.Route[*SlotMessage, *BlockMessage] { - return messaging.NewRoute[*SlotMessage, *BlockMessage](messages.BuildBeaconBlockAndSidecars) +func ProvideBuildBlockAndSidecarsRoute() *BuildBlockAndSidecarsRoute { + return messaging.NewRoute[*SlotMessage, *BlockBundleMessage](messages.BuildBeaconBlockAndSidecars) } // ProvideVerifyBeaconBlockRoute provides a route for verifying a beacon block. -func ProvideVerifyBeaconBlockRoute() *messaging.Route[*BlockMessage, *BlockMessage] { +func ProvideVerifyBeaconBlockRoute() *VerifyBlockRoute { return messaging.NewRoute[*BlockMessage, *BlockMessage](messages.VerifyBeaconBlock) } // ProvideFinalizeBeaconBlockRoute provides a route for finalizing a beacon block. -func ProvideFinalizeBeaconBlockRoute() *messaging.Route[*BlockMessage, *ValidatorUpdateMessage] { +func ProvideFinalizeBeaconBlockRoute() *FinalizeBlockRoute { return messaging.NewRoute[*BlockMessage, *ValidatorUpdateMessage](messages.FinalizeBeaconBlock) } // ProvideProcessGenesisDataRoute provides a route for processing genesis data. -func ProvideProcessGenesisDataRoute() *messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] { +func ProvideProcessGenesisDataRoute() *ProcessGenesisDataRoute { return messaging.NewRoute[*GenesisMessage, *ValidatorUpdateMessage](messages.ProcessGenesisData) } // ProvideProcessBlobSidecarsRoute provides a route for processing blob sidecars. -func ProvideProcessBlobSidecarsRoute() *messaging.Route[*SidecarMessage, *SidecarMessage] { +func ProvideProcessBlobSidecarsRoute() *ProcessBlobSidecarsRoute { return messaging.NewRoute[*SidecarMessage, *SidecarMessage](messages.VerifyBlobSidecars) } diff --git a/mod/node-core/pkg/components/middleware.go b/mod/node-core/pkg/components/middleware.go index f37ba3e688..7b2f1043b7 100644 --- a/mod/node-core/pkg/components/middleware.go +++ b/mod/node-core/pkg/components/middleware.go @@ -22,7 +22,6 @@ package components import ( "cosmossdk.io/depinject" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/metrics" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -35,7 +34,7 @@ type ABCIMiddlewareInput struct { ChainSpec common.ChainSpec Logger log.Logger[any] TelemetrySink *metrics.TelemetrySink - Dispatcher *dispatcher.Dispatcher + Dispatcher *Dispatcher } // ProvideABCIMiddleware is a depinject provider for the validator diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 7bd709e0c5..a7fbf374e1 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -23,7 +23,9 @@ package components import ( "cosmossdk.io/core/appmodule/v2" broker "github.com/berachain/beacon-kit/mod/async/pkg/broker" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" + asyncserver "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" blockstore "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/beacon/blockchain" @@ -437,22 +439,25 @@ type ( ) type ( - // BlockEvent is a type alias for the block event. + // BlockMessage is a type alias for the block message. BlockMessage = asynctypes.Message[*BeaconBlock] - // GenesisEvent is a type alias for the genesis event. + // BlockBundleMessage is a type alias for the block bundle message. + BlockBundleMessage = asynctypes.Message[*BeaconBlockBundle] + + // GenesisMessage is a type alias for the genesis message. GenesisMessage = asynctypes.Message[*Genesis] - // SidecarEvent is a type alias for the sidecar event. + // SidecarMessage is a type alias for the sidecar message. SidecarMessage = asynctypes.Message[*BlobSidecars] - // SlotEvent is a type alias for the slot event. + // SlotMessage is a type alias for the slot message. SlotMessage = asynctypes.Message[*SlotData] - // StatusEvent is a type alias for the status event. + // StatusMessage is a type alias for the status message. StatusMessage = asynctypes.Message[*service.StatusEvent] - // ValidatorUpdateEvent is a type alias for the validator update event. + // ValidatorUpdateMessage is a type alias for the validator update message. ValidatorUpdateMessage = asynctypes.Message[transition.ValidatorUpdates] ) @@ -485,7 +490,43 @@ type ( /* -------------------------------------------------------------------------- */ type ( - BeaconBlockFinalizedPublisher = messaging.Publisher[*BeaconBlock] + BeaconBlockFinalizedPublisher = messaging.Publisher[*BlockMessage] +) + +/* -------------------------------------------------------------------------- */ +/* Routes */ +/* -------------------------------------------------------------------------- */ + +type ( + // BuildBlockAndSidecarsRoute is a type alias for the build block and sidecars route. + BuildBlockAndSidecarsRoute = messaging.Route[*SlotMessage, *BlockBundleMessage] + + // VerifyBlockRoute is a type alias for the verify block route. + VerifyBlockRoute = messaging.Route[*BlockMessage, *BlockMessage] + + // FinalizeBlockRoute is a type alias for the finalize block route. + FinalizeBlockRoute = messaging.Route[*BlockMessage, *ValidatorUpdateMessage] + + // ProcessGenesisDataRoute is a type alias for the process genesis data route. + ProcessGenesisDataRoute = messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] + + // ProcessBlobSidecarsRoute is a type alias for the process blob sidecars route. + ProcessBlobSidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] +) + +/* -------------------------------------------------------------------------- */ +/* Dispatcher */ +/* -------------------------------------------------------------------------- */ + +type ( + // Dispatcher is a type alias for the dispatcher. + Dispatcher = dispatcher.Dispatcher + + // EventServer is a type alias for the event server. + EventServer = asyncserver.EventServer + + // MessageServer is a type alias for the messages server. + MessageServer = asyncserver.MessageServer ) /* -------------------------------------------------------------------------- */ diff --git a/mod/node-core/pkg/components/validator_service.go b/mod/node-core/pkg/components/validator_service.go index e3c66a3723..b89244996a 100644 --- a/mod/node-core/pkg/components/validator_service.go +++ b/mod/node-core/pkg/components/validator_service.go @@ -23,7 +23,6 @@ package components import ( "cosmossdk.io/depinject" sdklog "cosmossdk.io/log" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" "github.com/berachain/beacon-kit/mod/beacon/validator" "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/log" @@ -38,7 +37,7 @@ type ValidatorServiceInput struct { BlobProcessor *BlobProcessor Cfg *config.Config ChainSpec common.ChainSpec - Dispatcher *dispatcher.Dispatcher + Dispatcher *Dispatcher LocalBuilder *LocalBuilder Logger log.AdvancedLogger[any, sdklog.Logger] StateProcessor *StateProcessor From c999d17bf99a0b8e6f4159a5bfd47a1c1b46e8f4 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 15:05:43 -0400 Subject: [PATCH 08/75] dispatcher pruner deposit --- mod/beacon/block_store/types.go | 2 +- mod/execution/pkg/deposit/types.go | 2 +- .../pkg/components/availability_store.go | 4 +-- mod/node-core/pkg/components/block_store.go | 15 ++++------ mod/node-core/pkg/components/brokers.go | 2 +- .../pkg/components/deposit_service.go | 16 +++++----- mod/node-core/pkg/components/deposit_store.go | 4 +-- mod/node-core/pkg/components/types.go | 10 ++++--- mod/storage/pkg/pruner/pruner.go | 30 ++++++++++--------- 9 files changed, 42 insertions(+), 43 deletions(-) diff --git a/mod/beacon/block_store/types.go b/mod/beacon/block_store/types.go index 48722a9dbe..5433735256 100644 --- a/mod/beacon/block_store/types.go +++ b/mod/beacon/block_store/types.go @@ -42,7 +42,7 @@ type BlockStore[BeaconBlockT BeaconBlock] interface { // Event is an interface for block events. type Event[BeaconBlockT BeaconBlock] interface { // Type returns the type of the event. - Type() asynctypes.EventID + ID() asynctypes.EventID // Is returns true if the event is of the given type. Is(asynctypes.EventID) bool // Data returns the data of the event. diff --git a/mod/execution/pkg/deposit/types.go b/mod/execution/pkg/deposit/types.go index a94dda411c..99beea8e8b 100644 --- a/mod/execution/pkg/deposit/types.go +++ b/mod/execution/pkg/deposit/types.go @@ -53,7 +53,7 @@ type BlockEvent[ BeaconBlockT BeaconBlock[DepositT, BeaconBlockBodyT, ExecutionPayloadT], ExecutionPayloadT ExecutionPayload, ] interface { - Type() asynctypes.EventID + ID() asynctypes.EventID Is(asynctypes.EventID) bool Data() BeaconBlockT } diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index da3b6b6624..b54ffbbb17 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -98,7 +98,7 @@ func ProvideAvailabilityPruner( // build the availability pruner if IndexDB is available. return pruner.NewPruner[ *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, *IndexDB, ]( in.Logger.With("service", manager.AvailabilityPrunerName), @@ -107,7 +107,7 @@ func ProvideAvailabilityPruner( subCh, dastore.BuildPruneRangeFn[ *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, ](in.ChainSpec), ), nil } diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 3b2c9b4d98..9c079d74d1 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -28,6 +28,7 @@ import ( blockservice "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/storage/pkg/block" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -70,24 +71,20 @@ type BlockPrunerInput struct { func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { - subCh, err := in.BlockBroker.Subscribe() - if err != nil { - in.Logger.Error("failed to subscribe to block feed", "err", err) - return nil, err - } - + var finalizedBlkCh chan *FinalizedBlockEvent + in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh) return pruner.NewPruner[ *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, *BlockStore, ]( in.Logger.With("service", manager.BlockPrunerName), in.BlockStore, manager.BlockPrunerName, - subCh, + finalizedBlkCh, blockservice.BuildPruneRangeFn[ *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, ](in.Config.BlockStoreService), ), nil } diff --git a/mod/node-core/pkg/components/brokers.go b/mod/node-core/pkg/components/brokers.go index 0cc9d84d2c..a8decc82e9 100644 --- a/mod/node-core/pkg/components/brokers.go +++ b/mod/node-core/pkg/components/brokers.go @@ -33,7 +33,7 @@ func ProvideBlobBroker() *SidecarsBroker { // ProvideBlockBroker provides a block feed for the depinject framework. func ProvideBlockBroker() *BlockBroker { - return broker.New[*BlockEvent]( + return broker.New[*FinalizedBlockEvent]( "blk-broker", ) } diff --git a/mod/node-core/pkg/components/deposit_service.go b/mod/node-core/pkg/components/deposit_service.go index c1ad506765..eaee55fc90 100644 --- a/mod/node-core/pkg/components/deposit_service.go +++ b/mod/node-core/pkg/components/deposit_service.go @@ -21,8 +21,6 @@ package components import ( - "errors" - "cosmossdk.io/depinject" sdklog "cosmossdk.io/log" "github.com/berachain/beacon-kit/mod/execution/pkg/deposit" @@ -30,15 +28,16 @@ import ( "github.com/berachain/beacon-kit/mod/node-core/pkg/components/metrics" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // DepositServiceIn is the input for the deposit service. type DepositServiceIn struct { depinject.In BeaconDepositContract *DepositContract - BlockBroker *BlockBroker ChainSpec common.ChainSpec DepositStore *DepositStore + Dispatcher *Dispatcher EngineClient *EngineClient Logger log.AdvancedLogger[any, sdklog.Logger] TelemetrySink *metrics.TelemetrySink @@ -47,17 +46,16 @@ type DepositServiceIn struct { // ProvideDepositService provides the deposit service to the depinject // framework. func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { - blkSub, err := in.BlockBroker.Subscribe() - if err != nil { - in.Logger.Error("failed to subscribe to block feed", "err", err) - return nil, errors.New("failed to subscribe to block feed") + var finalizedBlkCh chan *FinalizedBlockEvent + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + return nil, err } // Build the deposit service. return deposit.NewService[ *BeaconBlockBody, *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, *DepositStore, *ExecutionPayload, ]( @@ -66,6 +64,6 @@ func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { in.TelemetrySink, in.DepositStore, in.BeaconDepositContract, - blkSub, + finalizedBlkCh, ), nil } diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index 10f3676a86..cc9a7be57d 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -77,7 +77,7 @@ func ProvideDepositPruner( return pruner.NewPruner[ *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, *DepositStore, ]( in.Logger.With("service", manager.DepositPrunerName), @@ -87,7 +87,7 @@ func ProvideDepositPruner( deposit.BuildPruneRangeFn[ *BeaconBlockBody, *BeaconBlock, - *BlockEvent, + *FinalizedBlockEvent, *Deposit, *ExecutionPayload, WithdrawalCredentials, diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index a7fbf374e1..8c9208abba 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -209,7 +209,7 @@ type ( DepositService = deposit.Service[ *BeaconBlock, *BeaconBlockBody, - *BlockEvent, + *FinalizedBlockEvent, *Deposit, *ExecutionPayload, WithdrawalCredentials, @@ -419,8 +419,10 @@ type ( /* -------------------------------------------------------------------------- */ type ( - // BlockEvent is a type alias for the block event. - BlockEvent = asynctypes.Event[*BeaconBlock] + // FinalizedBlockEvent is a type alias for the block event. + FinalizedBlockEvent = asynctypes.Event[*BeaconBlock] + + // TODO: delete all of these when done // GenesisEvent is a type alias for the genesis event. GenesisEvent = asynctypes.Event[*Genesis] @@ -473,7 +475,7 @@ type ( SidecarsBroker = broker.Broker[*SidecarEvent] // BlockBroker is a type alias for the block feed. - BlockBroker = broker.Broker[*BlockEvent] + BlockBroker = broker.Broker[*FinalizedBlockEvent] // SlotBroker is a type alias for the slot feed. SlotBroker = broker.Broker[*SlotEvent] diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 89bcff6d9c..07c8dc7fed 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -45,11 +45,11 @@ type pruner[ BlockEventT BlockEvent[BeaconBlockT], PrunableT Prunable, ] struct { - prunable Prunable - logger log.Logger[any] - name string - feed chan BlockEventT - pruneRangeFn func(BlockEventT) (uint64, uint64) + prunable Prunable + logger log.Logger[any] + name string + finalizedBlockCh chan BlockEventT + pruneRangeFn func(BlockEventT) (uint64, uint64) } // NewPruner creates a new Pruner. @@ -61,30 +61,32 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - feed chan BlockEventT, + finalizedBlockCh chan BlockEventT, pruneRangeFn func(BlockEventT) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, BlockEventT, PrunableT]{ - logger: logger, - prunable: prunable, - name: name, - feed: feed, - pruneRangeFn: pruneRangeFn, + logger: logger, + prunable: prunable, + name: name, + finalizedBlockCh: finalizedBlockCh, + pruneRangeFn: pruneRangeFn, } } // Start starts the Pruner by listening for new indexes to prune. -func (p *pruner[_, _, _]) Start(ctx context.Context) { +func (p *pruner[_, BlockEventT, _]) Start(ctx context.Context) { go p.start(ctx) } // start listens for new indexes to prune. -func (p *pruner[_, _, _]) start(ctx context.Context) { +func (p *pruner[_, BlockEventT, _]) start( + ctx context.Context, +) { for { select { case <-ctx.Done(): return - case event := <-p.feed: + case event := <-p.finalizedBlockCh: if event.Is(messages.BeaconBlockFinalized) { start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { From cc7567d67ecb3cae62284e31253d206967dd90da Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 17:48:51 -0400 Subject: [PATCH 09/75] dispatch da service and add more routes --- mod/async/pkg/types/notifiers.go | 2 + mod/beacon/block_store/service.go | 38 +++--- mod/da/pkg/da/service.go | 129 ++++++++++-------- mod/da/pkg/da/types.go | 10 -- mod/execution/pkg/deposit/service.go | 6 +- mod/execution/pkg/deposit/sync.go | 11 +- .../pkg/components/availability_store.go | 9 +- mod/node-core/pkg/components/blobs.go | 7 +- mod/node-core/pkg/components/block_store.go | 7 +- .../pkg/components/block_store_service.go | 10 +- mod/node-core/pkg/components/brokers.go | 79 ----------- mod/node-core/pkg/components/deposit_store.go | 9 +- mod/node-core/pkg/components/messages.go | 85 +++++++----- mod/node-core/pkg/components/types.go | 32 +++-- mod/primitives/pkg/messages/messages.go | 26 ++-- mod/runtime/pkg/middleware/abci.go | 6 +- mod/storage/pkg/pruner/pruner.go | 9 +- 17 files changed, 206 insertions(+), 269 deletions(-) delete mode 100644 mod/node-core/pkg/components/brokers.go diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index 1dc4660797..6537f37bd0 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -44,4 +44,6 @@ type MessageRoute interface { SendResponse(msg any) error // AwaitResponse awaits a response from the route. AwaitResponse(emptyResp any) error + // MessageID returns the message ID that the route is responsible for. + MessageID() MessageID } diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index ee9d07c4af..8fc088f12f 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -23,6 +23,7 @@ package blockstore import ( "context" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" @@ -35,14 +36,15 @@ func NewService[ ]( config Config, logger log.Logger[any], - blkBroker EventFeed[*asynctypes.Event[BeaconBlockT]], + dispatcher *dispatcher.Dispatcher, + // blkBroker EventFeed[*asynctypes.Event[BeaconBlockT]], store BlockStoreT, ) *Service[BeaconBlockT, BlockStoreT] { return &Service[BeaconBlockT, BlockStoreT]{ - config: config, - logger: logger, - blkBroker: blkBroker, - store: store, + config: config, + logger: logger, + dispatcher: dispatcher, + store: store, } } @@ -54,9 +56,9 @@ type Service[ // config is the configuration for the block service. config Config // logger is used for logging information and errors. - logger log.Logger[any] - blkBroker EventFeed[*asynctypes.Event[BeaconBlockT]] - store BlockStoreT + logger log.Logger[any] + dispatcher *dispatcher.Dispatcher + store BlockStoreT } // Name returns the name of the service. @@ -65,17 +67,17 @@ func (s *Service[_, _]) Name() string { } // Start starts the block service. -func (s *Service[_, _]) Start(ctx context.Context) error { +func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { if !s.config.Enabled { s.logger.Warn("block service is disabled, skipping storing blocks") return nil } - subBlkCh, err := s.blkBroker.Subscribe() - if err != nil { + var finalizedBlkCh chan *asynctypes.Event[BeaconBlockT] + if err := s.dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err } - go s.listenAndStore(ctx, subBlkCh) + go s.listenAndStore(ctx, finalizedBlkCh) return nil } @@ -89,13 +91,11 @@ func (s *Service[BeaconBlockT, _]) listenAndStore( case <-ctx.Done(): return case msg := <-subBlkCh: - if msg.Is(messages.BeaconBlockFinalized) { - slot := msg.Data().GetSlot() - if err := s.store.Set(slot, msg.Data()); err != nil { - s.logger.Error( - "failed to store block", "slot", slot, "error", err, - ) - } + slot := msg.Data().GetSlot() + if err := s.store.Set(slot, msg.Data()); err != nil { + s.logger.Error( + "failed to store block", "slot", slot, "error", err, + ) } } } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 3eb906b9f2..5ca256dc03 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -23,6 +23,7 @@ package da import ( "context" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" @@ -33,7 +34,6 @@ type Service[ BeaconBlockBodyT any, BlobSidecarsT BlobSidecar, //nolint:lll // formatter. - EventPublisherSubscriberT EventPublisherSubscriber[*asynctypes.Event[BlobSidecarsT]], ExecutionPayloadT any, ] struct { avs AvailabilityStoreT @@ -41,8 +41,8 @@ type Service[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ] - sidecarsBroker EventPublisherSubscriberT - logger log.Logger[any] + dispatcher *dispatcher.Dispatcher + logger log.Logger[any] } // NewService returns a new DA service. @@ -53,7 +53,6 @@ func NewService[ BeaconBlockBodyT any, BlobSidecarsT BlobSidecar, //nolint:lll // formatter. - EventPublisherSubscriberT EventPublisherSubscriber[*asynctypes.Event[BlobSidecarsT]], ExecutionPayloadT any, ]( avs AvailabilityStoreT, @@ -61,63 +60,72 @@ func NewService[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ], - sidecarsBroker EventPublisherSubscriberT, + dispatcher *dispatcher.Dispatcher, logger log.Logger[any], ) *Service[ AvailabilityStoreT, BeaconBlockBodyT, - BlobSidecarsT, EventPublisherSubscriberT, ExecutionPayloadT, + BlobSidecarsT, ExecutionPayloadT, ] { return &Service[ AvailabilityStoreT, BeaconBlockBodyT, - BlobSidecarsT, EventPublisherSubscriberT, ExecutionPayloadT, + BlobSidecarsT, ExecutionPayloadT, ]{ - avs: avs, - bp: bp, - sidecarsBroker: sidecarsBroker, - logger: logger, + avs: avs, + bp: bp, + dispatcher: dispatcher, + logger: logger, } } // Name returns the name of the service. -func (s *Service[_, _, _, _, _]) Name() string { +func (s *Service[_, _, _, _]) Name() string { return "da" } -// Start starts the service. -func (s *Service[_, _, _, _, _]) Start(ctx context.Context) error { - subSidecarsCh, err := s.sidecarsBroker.Subscribe() - if err != nil { - return err - } - go s.start(ctx, subSidecarsCh) +// Start registers this service as the recipient of ProcessSidecars and +// VerifySidecars messages, and begins listening for these requests. +func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { + // register as recipient of ProcessSidecars messages. + var sidecarsProcessRequests chan *asynctypes.Message[BlobSidecarsT] + s.dispatcher.RegisterMsgReceiver(messages.ProcessSidecars, &sidecarsProcessRequests) + + // register as recipient of VerifySidecars messages. + var sidecarVerifyRequests chan *asynctypes.Message[BlobSidecarsT] + s.dispatcher.RegisterMsgReceiver(messages.VerifySidecars, &sidecarVerifyRequests) + + // begin listening loop to handle process and verify requests. + go s.start(ctx, sidecarsProcessRequests, sidecarVerifyRequests) return nil } -// start starts the service. -func (s *Service[_, _, BlobSidecarsT, _, _]) start( +// start starts listens for ProcessSidecars and VerifySidecars messages and +// handles them accordingly. +func (s *Service[_, _, BlobSidecarsT, _]) start( ctx context.Context, - sidecarsCh chan *asynctypes.Event[BlobSidecarsT], + sidecarsProcessRequests chan *asynctypes.Message[BlobSidecarsT], + sidecarVerifyRequests chan *asynctypes.Message[BlobSidecarsT], ) { for { select { case <-ctx.Done(): return - case msg := <-sidecarsCh: - switch msg.ID() { - case messages.BlobSidecarsProcessRequest: - s.handleBlobSidecarsProcessRequest(msg) - case messages.BlobSidecarsReceived: - s.handleBlobSidecarsReceived(msg) - } + case msg := <-sidecarsProcessRequests: + s.handleBlobSidecarsProcessRequest(msg) + case msg := <-sidecarVerifyRequests: + s.handleSidecarsVerifyRequest(msg) } } } +/* -------------------------------------------------------------------------- */ +/* Message Handlers */ +/* -------------------------------------------------------------------------- */ + // handleBlobSidecarsProcessRequest handles the BlobSidecarsProcessRequest // event. // It processes the sidecars and publishes a BlobSidecarsProcessed event. -func (s *Service[_, _, BlobSidecarsT, _, _]) handleBlobSidecarsProcessRequest( - msg *asynctypes.Event[BlobSidecarsT], +func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( + msg *asynctypes.Message[BlobSidecarsT], ) { err := s.processSidecars(msg.Context(), msg.Data()) if err != nil { @@ -128,25 +136,24 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) handleBlobSidecarsProcessRequest( ) } - if err = s.sidecarsBroker.Publish( - msg.Context(), - asynctypes.NewEvent( - msg.Context(), messages.BlobSidecarsProcessed, msg.Data(), err, - )); err != nil { - s.logger.Error( - "Failed to publish blob sidecars processed event", - "error", - err, - ) - } + // dispatch a response to acknowledge the request. + s.dispatcher.DispatchResponse( + asynctypes.NewMessage( + msg.Context(), + messages.ProcessSidecars, + msg.Data(), + nil, + ), + ) } -// handleBlobSidecarsReceived handles the BlobSidecarsReceived event. -// It receives the sidecars and publishes a BlobSidecarsProcessed event. -func (s *Service[_, _, BlobSidecarsT, _, _]) handleBlobSidecarsReceived( - msg *asynctypes.Event[BlobSidecarsT], +// handleSidecarsVerifyRequest handles the SidecarsVerifyRequest event. +// It verifies the sidecars and publishes a SidecarsVerified event. +func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( + msg *asynctypes.Message[BlobSidecarsT], ) { - err := s.receiveSidecars(msg.Data()) + // verify the sidecars. + err := s.verifySidecars(msg.Data()) if err != nil { s.logger.Error( "Failed to receive blob sidecars", @@ -155,21 +162,23 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) handleBlobSidecarsReceived( ) } - if err = s.sidecarsBroker.Publish( - msg.Context(), - asynctypes.NewEvent( - msg.Context(), messages.BlobSidecarsProcessed, msg.Data(), err, - )); err != nil { - s.logger.Error( - "Failed to publish blob sidecars processed event", - "error", - err, - ) - } + // dispatch a response to acknowledge the request. + s.dispatcher.DispatchResponse( + asynctypes.NewMessage( + msg.Context(), + messages.VerifySidecars, + msg.Data(), + nil, + ), + ) } +/* -------------------------------------------------------------------------- */ +/* helpers */ +/* -------------------------------------------------------------------------- */ + // ProcessSidecars processes the blob sidecars. -func (s *Service[_, _, BlobSidecarsT, _, _]) processSidecars( +func (s *Service[_, _, BlobSidecarsT, _]) processSidecars( _ context.Context, sidecars BlobSidecarsT, ) error { @@ -182,7 +191,7 @@ func (s *Service[_, _, BlobSidecarsT, _, _]) processSidecars( } // VerifyIncomingBlobs receives blobs from the network and processes them. -func (s *Service[_, _, BlobSidecarsT, _, _]) receiveSidecars( +func (s *Service[_, _, BlobSidecarsT, _]) verifySidecars( sidecars BlobSidecarsT, ) error { // If there are no blobs to verify, return early. diff --git a/mod/da/pkg/da/types.go b/mod/da/pkg/da/types.go index 75b0c7f866..1645f773e5 100644 --- a/mod/da/pkg/da/types.go +++ b/mod/da/pkg/da/types.go @@ -21,8 +21,6 @@ package da import ( - "context" - "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -62,14 +60,6 @@ type BlobSidecar interface { IsNil() bool } -// EventPublisher represents the event publisher interface. -type EventPublisherSubscriber[T any] interface { - // PublishEvent publishes an event. - Publish(context.Context, T) error - // Subscribe subscribes to the event system. - Subscribe() (chan T, error) -} - // StorageBackend defines an interface for accessing various storage components // required by the beacon node. type StorageBackend[ diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 95873ab2c7..5882643c0d 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -46,8 +46,8 @@ type Service[ dc Contract[DepositT] // ds is the deposit store that stores deposits. ds Store[DepositT] - // feed is the block feed that provides block events. - feed chan BlockEventT + // finalizedBlockCh is the channel that provides finalized block events. + finalizedBlockCh chan BlockEventT // metrics is the metrics for the deposit service. metrics *metrics // failedBlocks is a map of blocks that failed to be processed to be @@ -83,7 +83,7 @@ func NewService[ ExecutionPayloadT, WithdrawalCredentialsT, ]{ - feed: feed, + finalizedBlockCh: feed, logger: logger, eth1FollowDistance: eth1FollowDistance, metrics: newMetrics(telemetrySink), diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index e088b7b04d..076c9bdab9 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -25,7 +25,6 @@ import ( "time" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // defaultRetryInterval processes a deposit event. @@ -39,12 +38,10 @@ func (s *Service[ select { case <-ctx.Done(): return - case msg := <-s.feed: - if msg.Is(messages.BeaconBlockFinalized) { - blockNum := msg.Data(). - GetBody().GetExecutionPayload().GetNumber() - s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) - } + case msg := <-s.finalizedBlockCh: + blockNum := msg.Data(). + GetBody().GetExecutionPayload().GetNumber() + s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) } } } diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index b54ffbbb17..6eb2bb4920 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -29,6 +29,7 @@ import ( dastore "github.com/berachain/beacon-kit/mod/da/pkg/store" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/storage/pkg/filedb" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -73,8 +74,8 @@ func ProvideAvailibilityStore( type AvailabilityPrunerInput struct { depinject.In AvailabilityStore *AvailabilityStore - BlockBroker *BlockBroker ChainSpec common.ChainSpec + Dispatcher *Dispatcher Logger log.AdvancedLogger[any, sdklog.Logger] } @@ -89,8 +90,8 @@ func ProvideAvailabilityPruner( return nil, errors.New("availability store does not have a range db") } - subCh, err := in.BlockBroker.Subscribe() - if err != nil { + var finalizedBlkCh chan *FinalizedBlockEvent + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { in.Logger.Error("failed to subscribe to block feed", "err", err) return nil, err } @@ -104,7 +105,7 @@ func ProvideAvailabilityPruner( in.Logger.With("service", manager.AvailabilityPrunerName), rangeDB, manager.AvailabilityPrunerName, - subCh, + finalizedBlkCh, dastore.BuildPruneRangeFn[ *BeaconBlock, *FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/blobs.go b/mod/node-core/pkg/components/blobs.go index 1fc66bd9d9..e6fa6308dd 100644 --- a/mod/node-core/pkg/components/blobs.go +++ b/mod/node-core/pkg/components/blobs.go @@ -97,8 +97,8 @@ type DAServiceIn struct { depinject.In AvailabilityStore *AvailabilityStore - SidecarsBroker *SidecarsBroker BlobProcessor *BlobProcessor + Dispatcher *Dispatcher Logger log.Logger } @@ -108,13 +108,12 @@ func ProvideDAService(in DAServiceIn) *DAService { return da.NewService[ *AvailabilityStore, *BeaconBlockBody, - *BlobSidecars, - *SidecarsBroker, + *Dispatcher, *ExecutionPayload, ]( in.AvailabilityStore, in.BlobProcessor, - in.SidecarsBroker, + in.Dispatcher, in.Logger.With("service", "da"), ) } diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 9c079d74d1..dbe7e03629 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -21,6 +21,8 @@ package components import ( + "fmt" + "cosmossdk.io/depinject" "cosmossdk.io/log" storev2 "cosmossdk.io/store/v2/db" @@ -72,7 +74,10 @@ func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { var finalizedBlkCh chan *FinalizedBlockEvent - in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh) + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + return nil, fmt.Errorf("failed to subscribe to block feed: %w", err) + } + return pruner.NewPruner[ *BeaconBlock, *FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/block_store_service.go b/mod/node-core/pkg/components/block_store_service.go index 7d04e79ff3..8ab0d0c47f 100644 --- a/mod/node-core/pkg/components/block_store_service.go +++ b/mod/node-core/pkg/components/block_store_service.go @@ -31,10 +31,10 @@ import ( type BlockServiceInput struct { depinject.In - BlockBroker *BlockBroker - BlockStore *BlockStore - Config *config.Config - Logger log.Logger[any] + BlockStore *BlockStore + Config *config.Config + Dispatcher *Dispatcher + Logger log.Logger[any] } // ProvideBlockStoreService provides the block service. @@ -42,7 +42,7 @@ func ProvideBlockStoreService(in BlockServiceInput) *BlockStoreService { return blockstore.NewService( in.Config.BlockStoreService, in.Logger, - in.BlockBroker, + in.Dispatcher, in.BlockStore, ) } diff --git a/mod/node-core/pkg/components/brokers.go b/mod/node-core/pkg/components/brokers.go deleted file mode 100644 index a8decc82e9..0000000000 --- a/mod/node-core/pkg/components/brokers.go +++ /dev/null @@ -1,79 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package components - -import ( - "github.com/berachain/beacon-kit/mod/async/pkg/broker" -) - -// ProvideBlobBroker provides a blob feed for the depinject framework. -func ProvideBlobBroker() *SidecarsBroker { - return broker.New[*SidecarEvent]( - "blob-broker", - ) -} - -// ProvideBlockBroker provides a block feed for the depinject framework. -func ProvideBlockBroker() *BlockBroker { - return broker.New[*FinalizedBlockEvent]( - "blk-broker", - ) -} - -// ProvideGenesisBroker provides a genesis feed for the depinject framework. -func ProvideGenesisBroker() *GenesisBroker { - return broker.New[*GenesisEvent]( - "genesis-broker", - ) -} - -// ProvideSlotBroker provides a slot feed for the depinject framework. -func ProvideSlotBroker() *SlotBroker { - return broker.New[*SlotEvent]( - "slot-broker", - ) -} - -// ProvideStatusBroker provides a status feed. -func ProvideStatusBroker() *StatusBroker { - return broker.New[*StatusEvent]( - "status-broker", - ) -} - -// ProvideValidatorUpdateBroker provides a validator updates feed. -func ProvideValidatorUpdateBroker() *ValidatorUpdateBroker { - return broker.New[*ValidatorUpdateEvent]( - "validator-updates-broker", - ) -} - -// DefaultBrokerProviders returns a slice of the default broker providers. -func DefaultBrokerProviders() []interface{} { - return []interface{}{ - ProvideBlobBroker, - ProvideBlockBroker, - ProvideGenesisBroker, - ProvideSlotBroker, - ProvideStatusBroker, - ProvideValidatorUpdateBroker, - } -} diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index cc9a7be57d..b3deb6787e 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -27,6 +27,7 @@ import ( "github.com/berachain/beacon-kit/mod/execution/pkg/deposit" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" depositstore "github.com/berachain/beacon-kit/mod/storage/pkg/deposit" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -59,9 +60,9 @@ func ProvideDepositStore( // DepositPrunerInput is the input for the deposit pruner. type DepositPrunerInput struct { depinject.In - BlockBroker *BlockBroker ChainSpec common.ChainSpec DepositStore *DepositStore + Dispatcher *Dispatcher Logger log.Logger } @@ -69,8 +70,8 @@ type DepositPrunerInput struct { func ProvideDepositPruner( in DepositPrunerInput, ) (DepositPruner, error) { - subCh, err := in.BlockBroker.Subscribe() - if err != nil { + var finalizedBlkCh chan *FinalizedBlockEvent + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { in.Logger.Error("failed to subscribe to block feed", "err", err) return nil, err } @@ -83,7 +84,7 @@ func ProvideDepositPruner( in.Logger.With("service", manager.DepositPrunerName), in.DepositStore, manager.DepositPrunerName, - subCh, + finalizedBlkCh, deposit.BuildPruneRangeFn[ *BeaconBlockBody, *BeaconBlock, diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index 6ab40704af..f1df15e473 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -24,51 +24,66 @@ import ( "cosmossdk.io/depinject" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" "github.com/berachain/beacon-kit/mod/async/pkg/server" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) type MessageServerInput struct { depinject.In - BuildBlockAndSidecarsRoute *BuildBlockAndSidecarsRoute - VerifyBlockRoute *VerifyBlockRoute - FinalizeBlockRoute *FinalizeBlockRoute - ProcessGenesisDataRoute *ProcessGenesisDataRoute - ProcessBlobSidecarsRoute *ProcessBlobSidecarsRoute + Routes []asynctypes.MessageRoute } -func ProvideMessageServer(in MessageServerInput) *MessageServer { +// ProvideMessageServer provides a message server. +func ProvideMessageServer(in MessageServerInput) *server.MessageServer { ms := server.NewMessageServer() - ms.RegisterRoute(in.BuildBlockAndSidecarsRoute.MessageID(), in.BuildBlockAndSidecarsRoute) - ms.RegisterRoute(in.VerifyBlockRoute.MessageID(), in.VerifyBlockRoute) - ms.RegisterRoute(in.FinalizeBlockRoute.MessageID(), in.FinalizeBlockRoute) - ms.RegisterRoute(in.ProcessGenesisDataRoute.MessageID(), in.ProcessGenesisDataRoute) - ms.RegisterRoute(in.ProcessBlobSidecarsRoute.MessageID(), in.ProcessBlobSidecarsRoute) + for _, route := range in.Routes { + ms.RegisterRoute(route.MessageID(), route) + } return ms } -// ProvideBuildBlockAndSidecarsRoute provides a route for building a beacon block. -func ProvideBuildBlockAndSidecarsRoute() *BuildBlockAndSidecarsRoute { - return messaging.NewRoute[*SlotMessage, *BlockBundleMessage](messages.BuildBeaconBlockAndSidecars) -} - -// ProvideVerifyBeaconBlockRoute provides a route for verifying a beacon block. -func ProvideVerifyBeaconBlockRoute() *VerifyBlockRoute { - return messaging.NewRoute[*BlockMessage, *BlockMessage](messages.VerifyBeaconBlock) -} - -// ProvideFinalizeBeaconBlockRoute provides a route for finalizing a beacon block. -func ProvideFinalizeBeaconBlockRoute() *FinalizeBlockRoute { - return messaging.NewRoute[*BlockMessage, *ValidatorUpdateMessage](messages.FinalizeBeaconBlock) -} - -// ProvideProcessGenesisDataRoute provides a route for processing genesis data. -func ProvideProcessGenesisDataRoute() *ProcessGenesisDataRoute { - return messaging.NewRoute[*GenesisMessage, *ValidatorUpdateMessage](messages.ProcessGenesisData) +// RouteFactory creates a new route for the given message ID. +func RouteFactory(mID string) asynctypes.MessageRoute { + switch mID { + case messages.BuildBeaconBlockAndSidecars: + return messaging.NewRoute[ + *SlotMessage, *BlockBundleMessage, + ](messages.BuildBeaconBlockAndSidecars) + case messages.VerifyBeaconBlock: + return messaging.NewRoute[ + *BlockMessage, *BlockMessage, + ](messages.VerifyBeaconBlock) + case messages.FinalizeBeaconBlock: + return messaging.NewRoute[ + *BlockMessage, *ValidatorUpdateMessage, + ](messages.FinalizeBeaconBlock) + case messages.ProcessGenesisData: + return messaging.NewRoute[ + *GenesisMessage, *ValidatorUpdateMessage, + ](messages.ProcessGenesisData) + case messages.VerifySidecars: + return messaging.NewRoute[ + *SidecarMessage, *SidecarMessage, + ](messages.VerifySidecars) + case messages.ProcessSidecars: + return messaging.NewRoute[ + *SidecarMessage, *SidecarMessage, + ](messages.ProcessSidecars) + default: + return nil + } } -// ProvideProcessBlobSidecarsRoute provides a route for processing blob sidecars. -func ProvideProcessBlobSidecarsRoute() *ProcessBlobSidecarsRoute { - return messaging.NewRoute[*SidecarMessage, *SidecarMessage](messages.VerifyBlobSidecars) +// ProvideMessageRoutes provides all the message routes. +func ProvideMessageRoutes() []asynctypes.MessageRoute { + return []asynctypes.MessageRoute{ + RouteFactory(messages.BuildBeaconBlockAndSidecars), + RouteFactory(messages.VerifyBeaconBlock), + RouteFactory(messages.FinalizeBeaconBlock), + RouteFactory(messages.ProcessGenesisData), + RouteFactory(messages.VerifySidecars), + RouteFactory(messages.ProcessSidecars), + } } // MessageServerComponents returns all the depinject providers for the message @@ -76,10 +91,6 @@ func ProvideProcessBlobSidecarsRoute() *ProcessBlobSidecarsRoute { func MessageServerComponents() []any { return []any{ ProvideMessageServer, - ProvideBuildBlockAndSidecarsRoute, - ProvideVerifyBeaconBlockRoute, - ProvideFinalizeBeaconBlockRoute, - ProvideProcessGenesisDataRoute, - ProvideProcessBlobSidecarsRoute, + ProvideMessageRoutes, } } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 8c9208abba..a5e1bb9407 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -22,7 +22,6 @@ package components import ( "cosmossdk.io/core/appmodule/v2" - broker "github.com/berachain/beacon-kit/mod/async/pkg/broker" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" asyncserver "github.com/berachain/beacon-kit/mod/async/pkg/server" @@ -468,23 +467,23 @@ type ( /* -------------------------------------------------------------------------- */ type ( - // GenesisBroker is a type alias for the genesis feed. - GenesisBroker = broker.Broker[*GenesisEvent] +// // GenesisBroker is a type alias for the genesis feed. +// GenesisBroker = broker.Broker[*GenesisEvent] - // SidecarsBroker is a type alias for the blob feed. - SidecarsBroker = broker.Broker[*SidecarEvent] +// // SidecarsBroker is a type alias for the blob feed. +// SidecarsBroker = broker.Broker[*SidecarEvent] - // BlockBroker is a type alias for the block feed. - BlockBroker = broker.Broker[*FinalizedBlockEvent] +// // BlockBroker is a type alias for the block feed. +// BlockBroker = broker.Broker[*FinalizedBlockEvent] - // SlotBroker is a type alias for the slot feed. - SlotBroker = broker.Broker[*SlotEvent] +// // SlotBroker is a type alias for the slot feed. +// SlotBroker = broker.Broker[*SlotEvent] - // StatusBroker is a type alias for the status feed. - StatusBroker = broker.Broker[*StatusEvent] +// // StatusBroker is a type alias for the status feed. +// StatusBroker = broker.Broker[*StatusEvent] - // ValidatorUpdateBroker is a type alias for the validator update feed. - ValidatorUpdateBroker = broker.Broker[*ValidatorUpdateEvent] +// // ValidatorUpdateBroker is a type alias for the validator update feed. +// ValidatorUpdateBroker = broker.Broker[*ValidatorUpdateEvent] ) /* -------------------------------------------------------------------------- */ @@ -512,8 +511,11 @@ type ( // ProcessGenesisDataRoute is a type alias for the process genesis data route. ProcessGenesisDataRoute = messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] - // ProcessBlobSidecarsRoute is a type alias for the process blob sidecars route. - ProcessBlobSidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] + // ProcessSidecarsRoute is a type alias for the process blob sidecars route. + ProcessSidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] + + // VerifySidecarsRoute is a type alias for the verify sidecars route. + VerifySidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/messages/messages.go b/mod/primitives/pkg/messages/messages.go index 412cff243a..90f0fbb0a2 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/primitives/pkg/messages/messages.go @@ -22,17 +22,17 @@ package messages // TODO: delete const ( - NewSlot = "new-slot" - BeaconBlockBuilt = "beacon-block-built" - BeaconBlockReceived = "beacon-block-received" - BeaconBlockVerified = "beacon-block-verified" - BeaconBlockFinalizedRequest = "beacon-block-finalized-request" - ValidatorSetUpdated = "validator-set-updated" - BlobSidecarsBuilt = "blob-sidecars-built" - BlobSidecarsReceived = "blob-sidecars-received" - BlobSidecarsProcessRequest = "blob-sidecars-process-request" - BlobSidecarsProcessed = "blob-sidecars-processed" - GenesisDataProcessRequest = "genesis-data-process-request" +// NewSlot = "new-slot" +// BeaconBlockBuilt = "beacon-block-built" +// BeaconBlockReceived = "beacon-block-received" +// BeaconBlockVerified = "beacon-block-verified" +// BeaconBlockFinalizedRequest = "beacon-block-finalized-request" +// ValidatorSetUpdated = "validator-set-updated" +// BlobSidecarsBuilt = "blob-sidecars-built" +// BlobSidecarsReceived = "blob-sidecars-received" +// BlobSidecarsProcessRequest = "blob-sidecars-process-request" +// BlobSidecarsProcessed = "blob-sidecars-processed" +// GenesisDataProcessRequest = "genesis-data-process-request" ) // messages @@ -41,7 +41,9 @@ const ( VerifyBeaconBlock = "verify-beacon-block" FinalizeBeaconBlock = "finalize-beacon-block" ProcessGenesisData = "process-genesis-data" - VerifyBlobSidecars = "process-blob-sidecars" + VerifySidecars = "verify-sidecars" + ProcessSidecars = "process-sidecars" + ReceiveSidecars = "receive-sidecars" ) // events diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 6a7b84de49..df720cc285 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -56,7 +56,7 @@ func (h *ABCIMiddleware[ err := h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, messages.GenesisDataProcessRequest, *data, + ctx, messages.ProcessGenesisData, *data, ), &valUpdateResp, ) if err != nil { @@ -170,7 +170,7 @@ func (h *ABCIMiddleware[ // verify the blob sidecars h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, messages.VerifyBlobSidecars, sidecars, + ctx, messages.VerifySidecars, sidecars, ), &sidecarsResp, ) @@ -242,7 +242,7 @@ func (h *ABCIMiddleware[ // verify the blob sidecars h.dispatcher.DispatchRequest( asynctypes.NewMessage( - ctx, messages.VerifyBlobSidecars, blobs, + ctx, messages.ProcessSidecars, blobs, ), &sidecarsResp, ) if sidecarsResp.Error() != nil { diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 07c8dc7fed..72dc6bc85c 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -30,7 +30,6 @@ import ( "context" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // Compile-time check to ensure pruner implements the Pruner interface. @@ -87,11 +86,9 @@ func (p *pruner[_, BlockEventT, _]) start( case <-ctx.Done(): return case event := <-p.finalizedBlockCh: - if event.Is(messages.BeaconBlockFinalized) { - start, end := p.pruneRangeFn(event) - if err := p.prunable.Prune(start, end); err != nil { - p.logger.Error("‼️ error pruning index ‼️", "error", err) - } + start, end := p.pruneRangeFn(event) + if err := p.prunable.Prune(start, end); err != nil { + p.logger.Error("‼️ error pruning index ‼️", "error", err) } } } From 9066944e4efd335f2f52eec801de9a08ce9d1d5a Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 17:58:38 -0400 Subject: [PATCH 10/75] mock --- .../pkg/components/service_registry.go | 36 ++++---- mod/node-core/pkg/components/types.go | 1 - .../pkg/service/mocks/dispatcher.mock.go | 82 +++++++++++++++++++ mod/runtime/pkg/service/options.go | 4 +- mod/runtime/pkg/service/registry.go | 19 ++++- mod/runtime/pkg/service/registry_test.go | 6 +- .../pkg/pruner/mocks/block_event.mock.go | 12 +-- 7 files changed, 126 insertions(+), 34 deletions(-) create mode 100644 mod/runtime/pkg/service/mocks/dispatcher.mock.go diff --git a/mod/node-core/pkg/components/service_registry.go b/mod/node-core/pkg/components/service_registry.go index f48d5dc770..b301b1d2e7 100644 --- a/mod/node-core/pkg/components/service_registry.go +++ b/mod/node-core/pkg/components/service_registry.go @@ -30,23 +30,19 @@ import ( // ServiceRegistryInput is the input for the service registry provider. type ServiceRegistryInput struct { depinject.In - ABCIService *ABCIMiddleware - BlockBroker *BlockBroker - BlockStoreService *BlockStoreService - ChainService *ChainService - DAService *DAService - DBManager *DBManager - DepositService *DepositService - EngineClient *EngineClient - GenesisBroker *GenesisBroker - Logger log.Logger - NodeAPIServer *NodeAPIServer - ReportingService *ReportingService - SidecarsBroker *SidecarsBroker - SlotBroker *SlotBroker - TelemetrySink *metrics.TelemetrySink - ValidatorService *ValidatorService - ValidatorUpdateBroker *ValidatorUpdateBroker + ABCIService *ABCIMiddleware + BlockStoreService *BlockStoreService + ChainService *ChainService + DAService *DAService + DBManager *DBManager + DepositService *DepositService + EngineClient *EngineClient + Logger log.Logger + NodeAPIServer *NodeAPIServer + ReportingService *ReportingService + TelemetrySink *metrics.TelemetrySink + ValidatorService *ValidatorService + Dispatcher *Dispatcher } // ProvideServiceRegistry is the depinject provider for the service registry. @@ -54,6 +50,7 @@ func ProvideServiceRegistry( in ServiceRegistryInput, ) *service.Registry { return service.NewRegistry( + in.Dispatcher, service.WithLogger(in.Logger), service.WithService(in.ValidatorService), service.WithService(in.BlockStoreService), @@ -64,11 +61,6 @@ func ProvideServiceRegistry( service.WithService(in.NodeAPIServer), service.WithService(in.ReportingService), service.WithService(in.DBManager), - service.WithService(in.GenesisBroker), - service.WithService(in.BlockBroker), - service.WithService(in.SlotBroker), - service.WithService(in.SidecarsBroker), - service.WithService(in.ValidatorUpdateBroker), service.WithService(in.EngineClient), ) } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index f5814155aa..861a1db524 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -188,7 +188,6 @@ type ( *AvailabilityStore, *BeaconBlockBody, *BlobSidecars, - *SidecarsBroker, *ExecutionPayload, ] diff --git a/mod/runtime/pkg/service/mocks/dispatcher.mock.go b/mod/runtime/pkg/service/mocks/dispatcher.mock.go new file mode 100644 index 0000000000..992d03e4f0 --- /dev/null +++ b/mod/runtime/pkg/service/mocks/dispatcher.mock.go @@ -0,0 +1,82 @@ +// Code generated by mockery v2.44.1. DO NOT EDIT. + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" +) + +// Dispatcher is an autogenerated mock type for the Dispatcher type +type Dispatcher struct { + mock.Mock +} + +type Dispatcher_Expecter struct { + mock *mock.Mock +} + +func (_m *Dispatcher) EXPECT() *Dispatcher_Expecter { + return &Dispatcher_Expecter{mock: &_m.Mock} +} + +// Start provides a mock function with given fields: ctx +func (_m *Dispatcher) Start(ctx context.Context) error { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for Start") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context) error); ok { + r0 = rf(ctx) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Dispatcher_Start_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Start' +type Dispatcher_Start_Call struct { + *mock.Call +} + +// Start is a helper method to define mock.On call +// - ctx context.Context +func (_e *Dispatcher_Expecter) Start(ctx interface{}) *Dispatcher_Start_Call { + return &Dispatcher_Start_Call{Call: _e.mock.On("Start", ctx)} +} + +func (_c *Dispatcher_Start_Call) Run(run func(ctx context.Context)) *Dispatcher_Start_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context)) + }) + return _c +} + +func (_c *Dispatcher_Start_Call) Return(_a0 error) *Dispatcher_Start_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Dispatcher_Start_Call) RunAndReturn(run func(context.Context) error) *Dispatcher_Start_Call { + _c.Call.Return(run) + return _c +} + +// NewDispatcher creates a new instance of Dispatcher. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewDispatcher(t interface { + mock.TestingT + Cleanup(func()) +}) *Dispatcher { + mock := &Dispatcher{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mod/runtime/pkg/service/options.go b/mod/runtime/pkg/service/options.go index 481639b921..f6d43d6a83 100644 --- a/mod/runtime/pkg/service/options.go +++ b/mod/runtime/pkg/service/options.go @@ -20,7 +20,9 @@ package service -import "github.com/berachain/beacon-kit/mod/log" +import ( + "github.com/berachain/beacon-kit/mod/log" +) // RegistryOption is a functional option for the Registry. type RegistryOption func(*Registry) error diff --git a/mod/runtime/pkg/service/registry.go b/mod/runtime/pkg/service/registry.go index 358b400817..a1d5522968 100644 --- a/mod/runtime/pkg/service/registry.go +++ b/mod/runtime/pkg/service/registry.go @@ -35,10 +35,16 @@ type Basic interface { Name() string } +type Dispatcher interface { + Start(ctx context.Context) error +} + // Registry provides a useful pattern for managing services. // It allows for ease of dependency management and ensures services // dependent on others use the same references in memory. type Registry struct { + // dispatcher is the dispatcher for the Registry. + dispatcher Dispatcher // logger is the logger for the Registry. logger log.Logger[any] // services is a map of service type -> service instance. @@ -48,9 +54,12 @@ type Registry struct { } // NewRegistry starts a registry instance for convenience. -func NewRegistry(opts ...RegistryOption) *Registry { +func NewRegistry( + dispatcher Dispatcher, + opts ...RegistryOption) *Registry { r := &Registry{ - services: make(map[string]Basic), + services: make(map[string]Basic), + dispatcher: dispatcher, } for _, opt := range opts { @@ -63,6 +72,12 @@ func NewRegistry(opts ...RegistryOption) *Registry { // StartAll initialized each service in order of registration. func (s *Registry) StartAll(ctx context.Context) error { + // start the dispatcher + if err := s.dispatcher.Start(ctx); err != nil { + return err + } + + // start all services s.logger.Info("Starting services", "num", len(s.serviceTypes)) for _, typeName := range s.serviceTypes { s.logger.Info("Starting service", "type", typeName) diff --git a/mod/runtime/pkg/service/registry_test.go b/mod/runtime/pkg/service/registry_test.go index 4be725d0aa..6ef8ad10b5 100644 --- a/mod/runtime/pkg/service/registry_test.go +++ b/mod/runtime/pkg/service/registry_test.go @@ -35,7 +35,8 @@ import ( func TestRegistry_StartAll(t *testing.T) { logger := noop.NewLogger[any]() - registry := service.NewRegistry(service.WithLogger(logger)) + dispatcher := &mocks.Dispatcher{} + registry := service.NewRegistry(dispatcher, service.WithLogger(logger)) service1 := &mocks.Basic{} service1.On("Start", mock.Anything).Return(nil).Once() @@ -64,7 +65,8 @@ func TestRegistry_StartAll(t *testing.T) { func TestRegistry_FetchService(t *testing.T) { logger := noop.NewLogger[any]() - registry := service.NewRegistry(service.WithLogger(logger)) + dispatcher := &mocks.Dispatcher{} + registry := service.NewRegistry(dispatcher, service.WithLogger(logger)) service1 := new(mocks.Basic) service1.On("Name").Return("Service1") diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index a08e70cc18..e69a1fea56 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -67,7 +67,7 @@ func (_c *BlockEvent_Data_Call[BeaconBlockT]) RunAndReturn(run func() BeaconBloc } // Is provides a mock function with given fields: _a0 -func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.EventID) bool { +func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.MessageID) bool { ret := _m.Called(_a0) if len(ret) == 0 { @@ -75,7 +75,7 @@ func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.EventID) bool { } var r0 bool - if rf, ok := ret.Get(0).(func(types.EventID) bool); ok { + if rf, ok := ret.Get(0).(func(types.MessageID) bool); ok { r0 = rf(_a0) } else { r0 = ret.Get(0).(bool) @@ -90,14 +90,14 @@ type BlockEvent_Is_Call[BeaconBlockT pruner.BeaconBlock] struct { } // Is is a helper method to define mock.On call -// - _a0 types.EventID +// - _a0 types.MessageID func (_e *BlockEvent_Expecter[BeaconBlockT]) Is(_a0 interface{}) *BlockEvent_Is_Call[BeaconBlockT] { return &BlockEvent_Is_Call[BeaconBlockT]{Call: _e.mock.On("Is", _a0)} } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 types.EventID)) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 types.MessageID)) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.EventID)) + run(args[0].(types.MessageID)) }) return _c } @@ -107,7 +107,7 @@ func (_c *BlockEvent_Is_Call[BeaconBlockT]) Return(_a0 bool) *BlockEvent_Is_Call return _c } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(types.EventID) bool) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(types.MessageID) bool) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Return(run) return _c } From 6eb7206bdbd5119ec6164daa7ba3547b69095c68 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 19:05:24 -0400 Subject: [PATCH 11/75] give chainservice a dispatcher --- mod/beacon/block_store/service.go | 2 +- mod/beacon/blockchain/execution_engine.go | 12 +- mod/beacon/blockchain/payload.go | 10 +- mod/beacon/blockchain/process.go | 14 +- mod/beacon/blockchain/receive.go | 4 +- mod/beacon/blockchain/service.go | 162 ++++++++---------- mod/da/pkg/types/bundle.go | 20 +++ .../pkg/components/availability_store.go | 2 +- mod/node-core/pkg/components/block_store.go | 2 +- mod/node-core/pkg/components/chain_service.go | 30 ++-- .../pkg/components/deposit_service.go | 2 +- mod/node-core/pkg/components/deposit_store.go | 2 +- mod/node-core/pkg/components/events.go | 2 +- mod/primitives/pkg/messages/messages.go | 17 +- 14 files changed, 129 insertions(+), 152 deletions(-) diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 8fc088f12f..d6638fd8af 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -73,7 +73,7 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { return nil } var finalizedBlkCh chan *asynctypes.Event[BeaconBlockT] - if err := s.dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + if err := s.dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err } diff --git a/mod/beacon/blockchain/execution_engine.go b/mod/beacon/blockchain/execution_engine.go index 49a9f7b988..03f49567cc 100644 --- a/mod/beacon/blockchain/execution_engine.go +++ b/mod/beacon/blockchain/execution_engine.go @@ -44,7 +44,7 @@ func (s *Service[ return } - if !s.shouldBuildOptimisticPayloads() && s.lb.Enabled() { + if !s.shouldBuildOptimisticPayloads() && s.localBuilder.Enabled() { s.sendNextFCUWithAttributes(ctx, st, blk, lph) } else { s.sendNextFCUWithoutAttributes(ctx, blk, lph) @@ -63,7 +63,7 @@ func (s *Service[ lph ExecutionPayloadHeaderT, ) { stCopy := st.Copy() - if _, err := s.sp.ProcessSlots(stCopy, blk.GetSlot()+1); err != nil { + if _, err := s.stateProcessor.ProcessSlots(stCopy, blk.GetSlot()+1); err != nil { s.logger.Error( "failed to process slots in non-optimistic payload", "error", err, @@ -72,7 +72,7 @@ func (s *Service[ } prevBlockRoot := blk.HashTreeRoot() - if _, err := s.lb.RequestPayloadAsync( + if _, err := s.localBuilder.RequestPayloadAsync( ctx, stCopy, blk.GetSlot()+1, @@ -99,7 +99,7 @@ func (s *Service[ blk BeaconBlockT, lph ExecutionPayloadHeaderT, ) { - if _, _, err := s.ee.NotifyForkchoiceUpdate( + if _, _, err := s.executionEngine.NotifyForkchoiceUpdate( ctx, // TODO: Switch to New(). engineprimitives. @@ -109,7 +109,7 @@ func (s *Service[ SafeBlockHash: lph.GetParentHash(), FinalizedBlockHash: lph.GetParentHash(), }, - s.cs.ActiveForkVersionForSlot(blk.GetSlot()), + s.chainSpec.ActiveForkVersionForSlot(blk.GetSlot()), ), ); err != nil { s.logger.Error( @@ -128,7 +128,7 @@ func (s *Service[ ]) calculateNextTimestamp(blk BeaconBlockT) uint64 { //#nosec:G701 // not an issue in practice. return max( - uint64(time.Now().Unix()+int64(s.cs.TargetSecondsPerEth1Block())), + uint64(time.Now().Unix()+int64(s.chainSpec.TargetSecondsPerEth1Block())), uint64(blk.GetBody().GetExecutionPayload().GetTimestamp()+1), ) } diff --git a/mod/beacon/blockchain/payload.go b/mod/beacon/blockchain/payload.go index ba000a8c3d..49d82e0209 100644 --- a/mod/beacon/blockchain/payload.go +++ b/mod/beacon/blockchain/payload.go @@ -46,7 +46,7 @@ func (s *Service[ // TODO: Verify if the slot number is correct here, I believe in current // form // it should be +1'd. Not a big deal until hardforks are in play though. - if err = s.lb.SendForceHeadFCU(ctx, st, slot+1); err != nil { + if err = s.localBuilder.SendForceHeadFCU(ctx, st, slot+1); err != nil { s.logger.Error( "failed to send force head FCU", "error", err, @@ -117,7 +117,7 @@ func (s *Service[ } // Submit a request for a new payload. - if _, err = s.lb.RequestPayloadAsync( + if _, err = s.localBuilder.RequestPayloadAsync( ctx, st, // We are rebuilding for the current slot. @@ -180,7 +180,7 @@ func (s *Service[ ) // We process the slot to update any RANDAO values. - if _, err := s.sp.ProcessSlots( + if _, err := s.stateProcessor.ProcessSlots( st, slot, ); err != nil { return err @@ -188,13 +188,13 @@ func (s *Service[ // We then trigger a request for the next payload. payload := blk.GetBody().GetExecutionPayload() - if _, err := s.lb.RequestPayloadAsync( + if _, err := s.localBuilder.RequestPayloadAsync( ctx, st, slot, // TODO: this is hood as fuck. max( //#nosec:G701 - uint64(time.Now().Unix()+int64(s.cs.TargetSecondsPerEth1Block())), + uint64(time.Now().Unix()+int64(s.chainSpec.TargetSecondsPerEth1Block())), uint64((payload.GetTimestamp()+1)), ), // The previous block root is simply the root of the block we just diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index c68b39d3bb..29ad39575b 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -37,7 +37,7 @@ func (s *Service[ ctx context.Context, genesisData GenesisT, ) (transition.ValidatorUpdates, error) { - return s.sp.InitializePreminedBeaconStateFromEth1( + return s.stateProcessor.InitializePreminedBeaconStateFromEth1( s.sb.StateFromContext(ctx), genesisData.GetDeposits(), genesisData.GetExecutionPayloadHeader(), @@ -83,13 +83,11 @@ func (s *Service[ // TODO: this is hood as fuck. // We won't send a fcu if the block is bad, should be addressed // via ticker later. - if err = s.blkBroker.Publish(ctx, - asynctypes.NewEvent( - ctx, messages.BeaconBlockFinalized, blk, + s.dispatcher.DispatchEvent( + ctx, asynctypes.NewEvent( + ctx, messages.BeaconBlockFinalizedEvent, blk, ), - ); err != nil { - return nil, err - } + ) go s.sendPostBlockFCU(ctx, st, blk) @@ -106,7 +104,7 @@ func (s *Service[ ) (transition.ValidatorUpdates, error) { startTime := time.Now() defer s.metrics.measureStateTransitionDuration(startTime) - valUpdates, err := s.sp.Transition( + valUpdates, err := s.stateProcessor.Transition( &transition.Context{ Context: ctx, OptimisticEngine: true, diff --git a/mod/beacon/blockchain/receive.go b/mod/beacon/blockchain/receive.go index 6fdbab00d9..374c3882c6 100644 --- a/mod/beacon/blockchain/receive.go +++ b/mod/beacon/blockchain/receive.go @@ -118,7 +118,7 @@ func (s *Service[ ) error { startTime := time.Now() defer s.metrics.measureStateRootVerificationTime(startTime) - if _, err := s.sp.Transition( + if _, err := s.stateProcessor.Transition( // We run with a non-optimistic engine here to ensure // that the proposer does not try to push through a bad block. &transition.Context{ @@ -148,5 +148,5 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, _, _, _, ]) shouldBuildOptimisticPayloads() bool { - return s.optimisticPayloadBuilds && s.lb.Enabled() + return s.optimisticPayloadBuilds && s.localBuilder.Enabled() } diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 293f4c353f..c3338b462c 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -24,6 +24,7 @@ import ( "context" "sync" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -60,15 +61,16 @@ type Service[ ] // logger is used for logging messages in the service. logger log.Logger[any] - // cs holds the chain specifications. - cs common.ChainSpec - // ee is the execution engine responsible for processing execution payloads. - - ee ExecutionEngine[PayloadAttributesT] - // lb is a local builder for constructing new beacon states. - lb LocalBuilder[BeaconStateT] - // sp is the state processor for beacon blocks and states. - sp StateProcessor[ + // chainSpec holds the chain specifications. + chainSpec common.ChainSpec + // dispatcher is the dispatcher for the service. + dispatcher *dispatcher.Dispatcher + // executionEngine is the execution engine responsible for processing execution payloads. + executionEngine ExecutionEngine[PayloadAttributesT] + // localBuilder is a local builder for constructing new beacon states. + localBuilder LocalBuilder[BeaconStateT] + // stateProcessor is the state processor for beacon blocks and states. + stateProcessor StateProcessor[ BeaconBlockT, BeaconStateT, *transition.Context, @@ -77,12 +79,6 @@ type Service[ ] // metrics is the metrics for the service. metrics *chainMetrics - // genesisBroker is the event feed for genesis data. - genesisBroker EventFeed[*asynctypes.Event[GenesisT]] - // blkBroker is the event feed for new blocks. - blkBroker EventFeed[*asynctypes.Event[BeaconBlockT]] - // validatorUpdateBroker is the event feed for validator updates. - validatorUpdateBroker EventFeed[*asynctypes.Event[transition.ValidatorUpdates]] // optimisticPayloadBuilds is a flag used when the optimistic payload // builder is enabled. optimisticPayloadBuilds bool @@ -117,10 +113,11 @@ func NewService[ BeaconStateT, ], logger log.Logger[any], - cs common.ChainSpec, - ee ExecutionEngine[PayloadAttributesT], - lb LocalBuilder[BeaconStateT], - sp StateProcessor[ + chainSpec common.ChainSpec, + dispatcher *dispatcher.Dispatcher, + executionEngine ExecutionEngine[PayloadAttributesT], + localBuilder LocalBuilder[BeaconStateT], + stateProcessor StateProcessor[ BeaconBlockT, BeaconStateT, *transition.Context, @@ -128,10 +125,6 @@ func NewService[ ExecutionPayloadHeaderT, ], ts TelemetrySink, - genesisBroker EventFeed[*asynctypes.Event[GenesisT]], - blkBroker EventFeed[*asynctypes.Event[BeaconBlockT]], - //nolint:lll // annoying formatter. - validatorUpdateBroker EventFeed[*asynctypes.Event[transition.ValidatorUpdates]], optimisticPayloadBuilds bool, ) *Service[ AvailabilityStoreT, BeaconBlockT, BeaconBlockBodyT, BeaconBlockHeaderT, @@ -145,14 +138,12 @@ func NewService[ ]{ sb: sb, logger: logger, - cs: cs, - ee: ee, - lb: lb, - sp: sp, + chainSpec: chainSpec, + dispatcher: dispatcher, + executionEngine: executionEngine, + localBuilder: localBuilder, + stateProcessor: stateProcessor, metrics: newChainMetrics(ts), - genesisBroker: genesisBroker, - blkBroker: blkBroker, - validatorUpdateBroker: validatorUpdateBroker, optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), } @@ -166,17 +157,18 @@ func (s *Service[ } func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) Start(ctx context.Context) error { - subBlkCh, err := s.blkBroker.Subscribe() - if err != nil { - return err - } - subGenCh, err := s.genesisBroker.Subscribe() - if err != nil { - return err - } - go s.start(ctx, subBlkCh, subGenCh) + var finalizeBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT] + s.dispatcher.RegisterMsgReceiver(messages.FinalizeBeaconBlock, finalizeBeaconBlockRequests) + + var verifyBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT] + s.dispatcher.RegisterMsgReceiver(messages.VerifyBeaconBlock, verifyBeaconBlockRequests) + + var processGenDataRequests chan *asynctypes.Message[GenesisT] + s.dispatcher.RegisterMsgReceiver(messages.ProcessGenesisData, processGenDataRequests) + + s.start(ctx, finalizeBeaconBlockRequests, verifyBeaconBlockRequests, processGenDataRequests) return nil } @@ -184,31 +176,33 @@ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) start( ctx context.Context, - subBlkCh chan *asynctypes.Event[BeaconBlockT], - subGenCh chan *asynctypes.Event[GenesisT], + finalizeBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT], + verifyBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT], + processGenDataRequests chan *asynctypes.Message[GenesisT], ) { for { select { case <-ctx.Done(): return - case msg := <-subBlkCh: - switch msg.ID() { - case messages.BeaconBlockReceived: - s.handleBeaconBlockReceived(msg) - case messages.BeaconBlockFinalizedRequest: - s.handleBeaconBlockFinalization(msg) - } - case msg := <-subGenCh: - if msg.ID() == messages.GenesisDataProcessRequest { - s.handleProcessGenesisDataRequest(msg) - } + case msg := <-finalizeBeaconBlockRequests: + s.handleFinalizeBeaconBlockRequest(msg) + case msg := <-verifyBeaconBlockRequests: + s.handleVerifyBeaconBlockRequest(msg) + case msg := <-processGenDataRequests: + s.handleProcessGenesisDataRequest(msg) } } } +/* -------------------------------------------------------------------------- */ +/* Message Handlers */ +/* -------------------------------------------------------------------------- */ + +// handleProcessGenesisDataRequest processes the given genesis data and +// dispatches a response. func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, _, -]) handleProcessGenesisDataRequest(msg *asynctypes.Event[GenesisT]) { +]) handleProcessGenesisDataRequest(msg *asynctypes.Message[GenesisT]) { if msg.Error() != nil { s.logger.Error("Error processing genesis data", "error", msg.Error()) return @@ -220,28 +214,21 @@ func (s *Service[ s.logger.Error("Failed to process genesis data", "error", err) } - // Publish the validator set updated event. - if err = s.validatorUpdateBroker.Publish( - msg.Context(), - asynctypes.NewEvent( + // dispatch a response containing the validator updates + s.dispatcher.DispatchResponse( + asynctypes.NewMessage( msg.Context(), - messages.ValidatorSetUpdated, + messages.ProcessGenesisData, valUpdates, - err, + nil, ), - ); err != nil { - s.logger.Error( - "Failed to publish validator set updated event", - "error", - err, - ) - } + ) } func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, -]) handleBeaconBlockReceived( - msg *asynctypes.Event[BeaconBlockT], +]) handleVerifyBeaconBlockRequest( + msg *asynctypes.Message[BeaconBlockT], ) { // If the block is nil, exit early. if msg.Error() != nil { @@ -249,24 +236,21 @@ func (s *Service[ return } - // Publish the verified block event. - if err := s.blkBroker.Publish( - msg.Context(), - asynctypes.NewEvent( + // dispatch a response with the error result from VerifyIncomingBlock + s.dispatcher.DispatchResponse( + asynctypes.NewMessage( msg.Context(), - messages.BeaconBlockVerified, + messages.VerifyBeaconBlock, msg.Data(), s.VerifyIncomingBlock(msg.Context(), msg.Data()), ), - ); err != nil { - s.logger.Error("Failed to publish verified block", "error", err) - } + ) } func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, -]) handleBeaconBlockFinalization( - msg *asynctypes.Event[BeaconBlockT], +]) handleFinalizeBeaconBlockRequest( + msg *asynctypes.Message[BeaconBlockT], ) { // If there's an error in the event, log it and return if msg.Error() != nil { @@ -274,25 +258,19 @@ func (s *Service[ return } - // Process the verified block + // process the verified block and get the validator updates valUpdates, err := s.ProcessBeaconBlock(msg.Context(), msg.Data()) if err != nil { s.logger.Error("Failed to process verified beacon block", "error", err) } - // Publish the validator set updated event - if err = s.validatorUpdateBroker.Publish( - msg.Context(), - asynctypes.NewEvent( + // dispatch a response with the validator updates + s.dispatcher.DispatchResponse( + asynctypes.NewMessage( msg.Context(), - messages.ValidatorSetUpdated, + messages.FinalizeBeaconBlock, valUpdates, err, - )); err != nil { - s.logger.Error( - "Failed to publish validator set updated event", - "error", - err, - ) - } + ), + ) } diff --git a/mod/da/pkg/types/bundle.go b/mod/da/pkg/types/bundle.go index 6742eb770b..4287595b12 100644 --- a/mod/da/pkg/types/bundle.go +++ b/mod/da/pkg/types/bundle.go @@ -1,3 +1,23 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + package types import "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index 6eb2bb4920..edd492f0bf 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -91,7 +91,7 @@ func ProvideAvailabilityPruner( } var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { in.Logger.Error("failed to subscribe to block feed", "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index dbe7e03629..fb2ad4b547 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -74,7 +74,7 @@ func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { return nil, fmt.Errorf("failed to subscribe to block feed: %w", err) } diff --git a/mod/node-core/pkg/components/chain_service.go b/mod/node-core/pkg/components/chain_service.go index 3de0cbe7b4..97c6b4e6f7 100644 --- a/mod/node-core/pkg/components/chain_service.go +++ b/mod/node-core/pkg/components/chain_service.go @@ -35,20 +35,18 @@ import ( type ChainServiceInput struct { depinject.In - BlockBroker *BlockBroker - ChainSpec common.ChainSpec - Cfg *config.Config - DepositService *DepositService - EngineClient *EngineClient - ExecutionEngine *ExecutionEngine - GenesisBrocker *GenesisBroker - LocalBuilder *LocalBuilder - Logger log.AdvancedLogger[any, sdklog.Logger] - Signer crypto.BLSSigner - StateProcessor *StateProcessor - StorageBackend *StorageBackend - TelemetrySink *metrics.TelemetrySink - ValidatorUpdateBroker *ValidatorUpdateBroker + ChainSpec common.ChainSpec + Cfg *config.Config + DepositService *DepositService + Dispatcher *Dispatcher + EngineClient *EngineClient + ExecutionEngine *ExecutionEngine + LocalBuilder *LocalBuilder + Logger log.AdvancedLogger[any, sdklog.Logger] + Signer crypto.BLSSigner + StateProcessor *StateProcessor + StorageBackend *StorageBackend + TelemetrySink *metrics.TelemetrySink } // ProvideChainService is a depinject provider for the blockchain service. @@ -71,13 +69,11 @@ func ProvideChainService( in.StorageBackend, in.Logger.With("service", "blockchain"), in.ChainSpec, + in.Dispatcher, in.ExecutionEngine, in.LocalBuilder, in.StateProcessor, in.TelemetrySink, - in.GenesisBrocker, - in.BlockBroker, - in.ValidatorUpdateBroker, // If optimistic is enabled, we want to skip post finalization FCUs. in.Cfg.Validator.EnableOptimisticPayloadBuilds, ) diff --git a/mod/node-core/pkg/components/deposit_service.go b/mod/node-core/pkg/components/deposit_service.go index eaee55fc90..e6e9883fd2 100644 --- a/mod/node-core/pkg/components/deposit_service.go +++ b/mod/node-core/pkg/components/deposit_service.go @@ -47,7 +47,7 @@ type DepositServiceIn struct { // framework. func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { return nil, err } diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index b3deb6787e..24e034c5b8 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -71,7 +71,7 @@ func ProvideDepositPruner( in DepositPrunerInput, ) (DepositPruner, error) { var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalized, finalizedBlkCh); err != nil { + if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { in.Logger.Error("failed to subscribe to block feed", "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 94d2de7417..b444a0eb47 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -46,7 +46,7 @@ func ProvideEventServer(in EventServerInput) *EventServer { // ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block // finalized events. func ProvideBeaconBlockFinalizedPublisher() *BeaconBlockFinalizedPublisher { - return messaging.NewPublisher[*BlockMessage](messages.BeaconBlockFinalized) + return messaging.NewPublisher[*BlockMessage](messages.BeaconBlockFinalizedEvent) } // EventServerComponents returns all the depinject providers for the event diff --git a/mod/primitives/pkg/messages/messages.go b/mod/primitives/pkg/messages/messages.go index 90f0fbb0a2..3bf00c8ec7 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/primitives/pkg/messages/messages.go @@ -20,21 +20,6 @@ package messages -// TODO: delete -const ( -// NewSlot = "new-slot" -// BeaconBlockBuilt = "beacon-block-built" -// BeaconBlockReceived = "beacon-block-received" -// BeaconBlockVerified = "beacon-block-verified" -// BeaconBlockFinalizedRequest = "beacon-block-finalized-request" -// ValidatorSetUpdated = "validator-set-updated" -// BlobSidecarsBuilt = "blob-sidecars-built" -// BlobSidecarsReceived = "blob-sidecars-received" -// BlobSidecarsProcessRequest = "blob-sidecars-process-request" -// BlobSidecarsProcessed = "blob-sidecars-processed" -// GenesisDataProcessRequest = "genesis-data-process-request" -) - // messages const ( BuildBeaconBlockAndSidecars = "build-beacon-block" @@ -48,5 +33,5 @@ const ( // events const ( - BeaconBlockFinalized = "beacon-block-finalized" + BeaconBlockFinalizedEvent = "beacon-block-finalized" ) From 66d66ee48da416813da3d08c9963e6849ca41687 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 19:18:43 -0400 Subject: [PATCH 12/75] why was this even here --- mod/execution/pkg/engine/engine.go | 15 ++++----------- mod/node-core/pkg/components/blobs.go | 2 +- mod/node-core/pkg/components/defaults.go | 1 - mod/node-core/pkg/components/engine.go | 2 -- mod/node-core/pkg/components/types.go | 24 ------------------------ 5 files changed, 5 insertions(+), 39 deletions(-) diff --git a/mod/execution/pkg/engine/engine.go b/mod/execution/pkg/engine/engine.go index 0ca4e71711..8e5144e01f 100644 --- a/mod/execution/pkg/engine/engine.go +++ b/mod/execution/pkg/engine/engine.go @@ -24,8 +24,6 @@ import ( "bytes" "context" - broker "github.com/berachain/beacon-kit/mod/async/pkg/broker" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives" engineerrors "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/errors" "github.com/berachain/beacon-kit/mod/errors" @@ -33,7 +31,6 @@ import ( "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" jsonrpc "github.com/berachain/beacon-kit/mod/primitives/pkg/net/json-rpc" - "github.com/berachain/beacon-kit/mod/primitives/pkg/service" ) // Engine is Beacon-Kit's implementation of the `ExecutionEngine` @@ -54,8 +51,6 @@ type Engine[ logger log.Logger[any] // metrics is the metrics for the engine. metrics *engineMetrics - // statusPublisher is the status publishder for the engine. - statusPublisher *broker.Broker[*asynctypes.Event[*service.StatusEvent]] } // New creates a new Engine. @@ -68,9 +63,8 @@ func New[ EncodeIndex(int, *bytes.Buffer) }, ]( - ec *client.EngineClient[ExecutionPayloadT, PayloadAttributesT], + engineClient *client.EngineClient[ExecutionPayloadT, PayloadAttributesT], logger log.Logger[any], - statusPublisher *broker.Broker[*asynctypes.Event[*service.StatusEvent]], telemtrySink TelemetrySink, ) *Engine[ ExecutionPayloadT, PayloadAttributesT, @@ -80,10 +74,9 @@ func New[ ExecutionPayloadT, PayloadAttributesT, PayloadIDT, WithdrawalsT, ]{ - ec: ec, - logger: logger, - metrics: newEngineMetrics(telemtrySink, logger), - statusPublisher: statusPublisher, + ec: engineClient, + logger: logger, + metrics: newEngineMetrics(telemtrySink, logger), } } diff --git a/mod/node-core/pkg/components/blobs.go b/mod/node-core/pkg/components/blobs.go index e6fa6308dd..aec0479896 100644 --- a/mod/node-core/pkg/components/blobs.go +++ b/mod/node-core/pkg/components/blobs.go @@ -108,7 +108,7 @@ func ProvideDAService(in DAServiceIn) *DAService { return da.NewService[ *AvailabilityStore, *BeaconBlockBody, - *Dispatcher, + *BlobSidecars, *ExecutionPayload, ]( in.AvailabilityStore, diff --git a/mod/node-core/pkg/components/defaults.go b/mod/node-core/pkg/components/defaults.go index b9ce5db064..6ac9ac78c5 100644 --- a/mod/node-core/pkg/components/defaults.go +++ b/mod/node-core/pkg/components/defaults.go @@ -59,7 +59,6 @@ func DefaultComponentsWithStandardTypes() []any { } components = append(components, DefaultNodeAPIComponents()...) components = append(components, DefaultNodeAPIHandlers()...) - components = append(components, DefaultBrokerProviders()...) components = append(components, EventServerComponents()...) components = append(components, MessageServerComponents()...) return components diff --git a/mod/node-core/pkg/components/engine.go b/mod/node-core/pkg/components/engine.go index ecbaac8137..89a6c27302 100644 --- a/mod/node-core/pkg/components/engine.go +++ b/mod/node-core/pkg/components/engine.go @@ -67,7 +67,6 @@ type ExecutionEngineInputs struct { depinject.In EngineClient *EngineClient Logger log.AdvancedLogger[any, sdklog.Logger] - StatusBroker *StatusBroker TelemetrySink *metrics.TelemetrySink } @@ -84,7 +83,6 @@ func ProvideExecutionEngine( ]( in.EngineClient, in.Logger.With("service", "execution-engine"), - in.StatusBroker, in.TelemetrySink, ) } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 861a1db524..0c9bbfce99 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -462,30 +462,6 @@ type ( ValidatorUpdateMessage = asynctypes.Message[transition.ValidatorUpdates] ) -/* -------------------------------------------------------------------------- */ -/* Brokers */ -/* -------------------------------------------------------------------------- */ - -type ( -// // GenesisBroker is a type alias for the genesis feed. -// GenesisBroker = broker.Broker[*GenesisEvent] - -// // SidecarsBroker is a type alias for the blob feed. -// SidecarsBroker = broker.Broker[*SidecarEvent] - -// // BlockBroker is a type alias for the block feed. -// BlockBroker = broker.Broker[*FinalizedBlockEvent] - -// // SlotBroker is a type alias for the slot feed. -// SlotBroker = broker.Broker[*SlotEvent] - -// // StatusBroker is a type alias for the status feed. -// StatusBroker = broker.Broker[*StatusEvent] - -// // ValidatorUpdateBroker is a type alias for the validator update feed. -// ValidatorUpdateBroker = broker.Broker[*ValidatorUpdateEvent] -) - /* -------------------------------------------------------------------------- */ /* Publishers */ /* -------------------------------------------------------------------------- */ From cc96b118706f00c0b9da4496a333035833541595 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 19:19:43 -0400 Subject: [PATCH 13/75] goodbye broker --- mod/async/pkg/broker/broker.go | 110 --------------------------- mod/async/pkg/broker/constants.go | 32 -------- mod/async/pkg/broker/errors.go | 28 ------- mod/async/pkg/messaging/constants.go | 4 +- mod/async/pkg/messaging/errors.go | 2 +- mod/beacon/block_store/service.go | 1 - mod/beacon/validator/service.go | 6 -- 7 files changed, 3 insertions(+), 180 deletions(-) delete mode 100644 mod/async/pkg/broker/broker.go delete mode 100644 mod/async/pkg/broker/constants.go delete mode 100644 mod/async/pkg/broker/errors.go diff --git a/mod/async/pkg/broker/broker.go b/mod/async/pkg/broker/broker.go deleted file mode 100644 index 0febf78961..0000000000 --- a/mod/async/pkg/broker/broker.go +++ /dev/null @@ -1,110 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package broker - -import ( - "context" - "time" -) - -// Broker broadcasts msgs to registered clients. -type Broker[T any] struct { - // name of the message broker. - name string - // clients is a map of registered clients. - clients map[chan T]struct{} - // msgs is the channel for publishing new messages. - msgs chan T - // timeout is the timeout for sending a msg to a client. - timeout time.Duration -} - -// New creates a new b. -func New[T any](name string) *Broker[T] { - return &Broker[T]{ - clients: make(map[chan T]struct{}), - msgs: make(chan T, defaultBufferSize), - timeout: defaultTimeout, - name: name, - } -} - -// Name returns the name of the msg broker. -func (b *Broker[T]) Name() string { - return b.name -} - -// Start starts the broker loop. -func (b *Broker[T]) Start(ctx context.Context) error { - go b.start(ctx) - return nil -} - -// start starts the broker loop. -func (b *Broker[T]) start(ctx context.Context) { - for { - select { - case <-ctx.Done(): - // close all leftover clients and break the broker loop - for client := range b.clients { - b.Unsubscribe(client) - } - return - case msg := <-b.msgs: - // broadcast published msg to all clients - for client := range b.clients { - // send msg to client (or discard msg after timeout) - select { - case client <- msg: - case <-time.After(b.timeout): - } - } - } - } -} - -// Publish publishes a msg to the b. -// Returns ErrTimeout on timeout. -func (b *Broker[T]) Publish(ctx context.Context, msg T) error { - select { - case b.msgs <- msg: - return nil - case <-ctx.Done(): - return ctx.Err() - } -} - -// Subscribe registers a new client to the broker and returns it to the caller. -// Returns ErrTimeout on timeout. -func (b *Broker[T]) Subscribe() (chan T, error) { - client := make(chan T) - b.clients[client] = struct{}{} - return client, nil -} - -// Unsubscribe removes a client from the b. -// Returns ErrTimeout on timeout. -func (b *Broker[T]) Unsubscribe(client chan T) { - // Remove the client from the broker - delete(b.clients, client) - // close the client channel - close(client) -} diff --git a/mod/async/pkg/broker/constants.go b/mod/async/pkg/broker/constants.go deleted file mode 100644 index aa54901023..0000000000 --- a/mod/async/pkg/broker/constants.go +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package broker - -import "time" - -const ( - // defaultTimeout specifies the default timeout when the broker - // tries to send a message to a client, a message is published to the - // broker, or a client subscribes or unsubscribes. - defaultTimeout = time.Second - // defaultBufferSize specifies the default size of the message buffer. - defaultBufferSize = 10 -) diff --git a/mod/async/pkg/broker/errors.go b/mod/async/pkg/broker/errors.go deleted file mode 100644 index f0d88700fe..0000000000 --- a/mod/async/pkg/broker/errors.go +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package broker - -import ( - "errors" -) - -// ErrTimeout is the error returned when a broker operation timed out. -var ErrTimeout = errors.New("timeout") diff --git a/mod/async/pkg/messaging/constants.go b/mod/async/pkg/messaging/constants.go index 17a946e993..31f388be00 100644 --- a/mod/async/pkg/messaging/constants.go +++ b/mod/async/pkg/messaging/constants.go @@ -23,9 +23,9 @@ package messaging import "time" const ( - // defaultPublisherTimeout specifies the default timeout when the broker + // defaultPublisherTimeout specifies the default timeout when the publisher // tries to send a message to a client, a message is published to the - // broker, or a client subscribes or unsubscribes. + // publisher, or a client subscribes or unsubscribes. defaultPublisherTimeout = time.Second // defaultRouterTimeout specifies the default timeout when the router diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index e06272d238..6ca9316221 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -26,7 +26,7 @@ import ( "github.com/berachain/beacon-kit/mod/errors" ) -// ErrTimeout is the error returned when a broker operation timed out. +// ErrTimeout is the error returned when a dispatch operation timed out. var ( ErrTimeout = errors.New("timeout") diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index d6638fd8af..0d7fe108f3 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -37,7 +37,6 @@ func NewService[ config Config, logger log.Logger[any], dispatcher *dispatcher.Dispatcher, - // blkBroker EventFeed[*asynctypes.Event[BeaconBlockT]], store BlockStoreT, ) *Service[BeaconBlockT, BlockStoreT] { return &Service[BeaconBlockT, BlockStoreT]{ diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 17335787bf..e5102f3c59 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -92,12 +92,6 @@ type Service[ remotePayloadBuilders []PayloadBuilder[BeaconStateT, ExecutionPayloadT] // metrics is a metrics collector. metrics *validatorMetrics - // // blkBroker is a publisher for blocks. - // blkBroker EventPublisher[*asynctypes.Event[BeaconBlockT]] - // // sidecarBroker is a publisher for sidecars. - // sidecarBroker EventPublisher[*asynctypes.Event[BlobSidecarsT]] - // // newSlotSub is a feed for slots. - // newSlotSub chan *asynctypes.Event[SlotDataT] } // NewService creates a new validator service. From 70c0f36b7ae8ff4ff115cc6288473f351e86a3f4 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 19:44:53 -0400 Subject: [PATCH 14/75] tidy --- mod/beacon/go.mod | 8 ++++++-- mod/beacon/go.sum | 8 ++------ mod/da/go.mod | 9 +++++++-- mod/da/go.sum | 8 ++------ mod/node-core/go.mod | 16 ++++++++++++++-- mod/node-core/go.sum | 26 ++------------------------ mod/runtime/go.mod | 8 +++++--- mod/runtime/go.sum | 14 ++++---------- 8 files changed, 42 insertions(+), 55 deletions(-) diff --git a/mod/beacon/go.mod b/mod/beacon/go.mod index 621d33d611..734cde67ce 100644 --- a/mod/beacon/go.mod +++ b/mod/beacon/go.mod @@ -2,12 +2,16 @@ module github.com/berachain/beacon-kit/mod/beacon go 1.22.5 -replace github.com/berachain/beacon-kit/mod/engine-primitives => ../engine-primitives +replace ( + github.com/berachain/beacon-kit/mod/async => ../async + github.com/berachain/beacon-kit/mod/engine-primitives => ../engine-primitives + github.com/berachain/beacon-kit/mod/primitives => ../primitives +) require ( github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240610210054-bfdc14c4013c - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e golang.org/x/sync v0.8.0 diff --git a/mod/beacon/go.sum b/mod/beacon/go.sum index a35de61d3f..7c584945f5 100644 --- a/mod/beacon/go.sum +++ b/mod/beacon/go.sum @@ -8,18 +8,14 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd h1:YXZN5EGMFId+MlDU1HTo672c1+oAC2ubRzRJFriaNP4= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd h1:jD/ggR959ZX+lqxsMzoRJzrGvFK7PI6UmgnRwOTh4S4= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:iXa+Q+i0q+GCpLzkusulO57K5vlkDgM77jtfMr3QdFA= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c h1:7f9dLYGOCMoV7LxT6YRmVSWLTPbGTTcxDPLPLvHGrOk= github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:nFybcw/ZhJ6Gu66dna301W2I7u61skm2HfHxQmdR68Q= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e h1:JQ+QbZcnecWYooLXWVbWZTbuHas9G68pHIxT/3uVlpI= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:v5JKfVqYEGFMTJIDrOV8m/dutSpw7mlif6wW3v26k68= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= diff --git a/mod/da/go.mod b/mod/da/go.mod index e8e06e43b6..4c14674aae 100644 --- a/mod/da/go.mod +++ b/mod/da/go.mod @@ -2,12 +2,17 @@ module github.com/berachain/beacon-kit/mod/da go 1.22.5 +replace ( + github.com/berachain/beacon-kit/mod/async => ../async + github.com/berachain/beacon-kit/mod/primitives => ../primitives +) + require ( - github.com/berachain/beacon-kit/mod/async v0.0.0-20240624003607-df94860f8eeb + github.com/berachain/beacon-kit/mod/async v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240624033454-8f3451361f44 github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e github.com/crate-crypto/go-kzg-4844 v1.1.0 diff --git a/mod/da/go.sum b/mod/da/go.sum index 00284fa38c..a2df690864 100644 --- a/mod/da/go.sum +++ b/mod/da/go.sum @@ -8,8 +8,6 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624003607-df94860f8eeb h1:4N/wng6MC9Kz5O7diKcJI/k7gnP0o/5hPu+cYZIJpCc= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624003607-df94860f8eeb/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8= github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df h1:I0qrcOnLkWjruhCWJMY04Cc9KT78WaY4aXCIoUj9+V4= @@ -18,14 +16,12 @@ github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1 github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:ZU1bq1BMt6b0kPRAw+A3kP7FlSd5DSQNYePD5qL9zfQ= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e h1:b8OmtWGjhaZENqiE3P1bjegLY6rOYqsROKRaSTjcHt8= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:GO3AOOJLKmCuU9UcyChO8zVd3gprJd57Vl4jcVB6oLY= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df h1:6MJllcmMFt6dtvftM5zmdl1WVDpqZkNy3hFXVZtNV0s= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df/go.mod h1:yRD7rmnyaaqgq/6+eIVqvSkFJXuLXpBddUu59HUOrtc= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= github.com/berachain/beacon-kit/mod/log v0.0.0-20240624033454-8f3451361f44 h1:R4+mGOZDYA93rD4AUbYq4fSWmRsJhnlB/ww1ap09WOY= github.com/berachain/beacon-kit/mod/log v0.0.0-20240624033454-8f3451361f44/go.mod h1:xP5KcG56VfbPgz2ZRHerxm90MkjXMSDaGZNOOO5yfH4= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e h1:JQ+QbZcnecWYooLXWVbWZTbuHas9G68pHIxT/3uVlpI= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:v5JKfVqYEGFMTJIDrOV8m/dutSpw7mlif6wW3v26k68= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= diff --git a/mod/node-core/go.mod b/mod/node-core/go.mod index 6747b85063..69d1d9a58f 100644 --- a/mod/node-core/go.mod +++ b/mod/node-core/go.mod @@ -6,6 +6,18 @@ replace ( // The following are required to build with the latest version of the cosmos-sdk main branch: cosmossdk.io/api => cosmossdk.io/api v0.7.3-0.20240806152830-8fb47b368cd4 cosmossdk.io/core/testing => cosmossdk.io/core/testing v0.0.0-20240806152830-8fb47b368cd4 + // required until merged onto main + github.com/berachain/beacon-kit/mod/async => ../async + github.com/berachain/beacon-kit/mod/beacon => ../beacon + github.com/berachain/beacon-kit/mod/da => ../da + github.com/berachain/beacon-kit/mod/execution => ../execution + github.com/berachain/beacon-kit/mod/node-api => ../node-api + github.com/berachain/beacon-kit/mod/node-api/engines => ../node-api/engines + github.com/berachain/beacon-kit/mod/payload => ../payload + github.com/berachain/beacon-kit/mod/primitives => ../primitives + github.com/berachain/beacon-kit/mod/runtime => ../runtime + github.com/berachain/beacon-kit/mod/state-transition => ../state-transition + github.com/berachain/beacon-kit/mod/storage => ../storage github.com/cosmos/cosmos-sdk => github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240806162353-1fa473c35c9a ) @@ -23,7 +35,7 @@ require ( github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/execution v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/log v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/node-api v0.0.0-20240806160829-cde2d1347e7e @@ -90,7 +102,7 @@ require ( github.com/DataDog/zstd v1.5.6 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/berachain/beacon-kit/mod/async v0.0.0-20240705193247-d464364483df + github.com/berachain/beacon-kit/mod/async v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd // indirect github.com/bgentry/speakeasy v0.2.0 // indirect github.com/bits-and-blooms/bitset v1.13.0 // indirect diff --git a/mod/node-core/go.sum b/mod/node-core/go.sum index 67aa936863..b77ada6f85 100644 --- a/mod/node-core/go.sum +++ b/mod/node-core/go.sum @@ -72,10 +72,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240705193247-d464364483df h1:f44S4M0BLiTLeX+lGvAkt1+tAM8ri+q2XsPQOl962FE= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240705193247-d464364483df/go.mod h1:MNNFLsraZnkZZpm1Cl42Mxnwwql+BWHinn+r6+DrASQ= -github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240718074353-1a991cfeed63 h1:OYw3r0MV8Wt7NipYbQ0bi5yed9ffudYsevAvbhp275Y= -github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240718074353-1a991cfeed63/go.mod h1:dJ4i7uEGjc6KwMrSDexeXdkFqzEOXXOelWLq8KjcNqo= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8= github.com/berachain/beacon-kit/mod/cli v0.0.0-20240806160829-cde2d1347e7e h1:sPD08g07FrZWrDnKDgWXn0P311WRFP+sd6kgBZiqz8k= @@ -86,34 +82,16 @@ github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240723155519-565f208d5482 github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240723155519-565f208d5482/go.mod h1:LuuhwwOod5wfpPJpHhp+QdxkVcNLeadoCSSI32gp6vE= github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e h1:GTeZshNZaH5MnVhSSGj//vxJfv1kM9d6w2CA7O64gJk= github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:ZU1bq1BMt6b0kPRAw+A3kP7FlSd5DSQNYePD5qL9zfQ= -github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df h1:yZtRZBkhDJ2xMlMLx5DdUFtrtH9iWyPNl4HSW2uJyb8= -github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df/go.mod h1:iGTR+iYsqpj79WLX1RUVbV7hDs0rQtr+Iqa/VkAF4x8= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e h1:b8OmtWGjhaZENqiE3P1bjegLY6rOYqsROKRaSTjcHt8= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:GO3AOOJLKmCuU9UcyChO8zVd3gprJd57Vl4jcVB6oLY= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df h1:6MJllcmMFt6dtvftM5zmdl1WVDpqZkNy3hFXVZtNV0s= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df/go.mod h1:yRD7rmnyaaqgq/6+eIVqvSkFJXuLXpBddUu59HUOrtc= -github.com/berachain/beacon-kit/mod/execution v0.0.0-20240705193247-d464364483df h1:H6BHipSq4spa06PGLp8osdJ8LTwz7POQa015RruWJfw= -github.com/berachain/beacon-kit/mod/execution v0.0.0-20240705193247-d464364483df/go.mod h1:RwyZdP3Th3Qxgr/tdmEt0Zev92Ue30/CwEvvwksP76c= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= github.com/berachain/beacon-kit/mod/log v0.0.0-20240705193247-d464364483df h1:SnzeY9SCmKyEx0iGC/C/8E39ozpl/g5yI7lFXpmbMBI= github.com/berachain/beacon-kit/mod/log v0.0.0-20240705193247-d464364483df/go.mod h1:mJ0ZlK+izcPWcveHAtM4+W0a+8jhu5Y4DMPL2Takacg= -github.com/berachain/beacon-kit/mod/node-api v0.0.0-20240806160829-cde2d1347e7e h1:fzW/jTq01yOTdrEoi0cucTqadvbZLROr9dGFtXfjBl8= -github.com/berachain/beacon-kit/mod/node-api v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:wgOdrP96dMsXDXHItO4tRLuf8IaHP14RY4MbUPFAc4s= -github.com/berachain/beacon-kit/mod/node-api/engines v0.0.0-20240806160829-cde2d1347e7e h1:RJSPVKiuK4h2IJEIsJDX2JGbqY3ZdKGzvaEN8Dx7kYc= -github.com/berachain/beacon-kit/mod/node-api/engines v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:lj5dEWEjUn4Mj9/qVrCKrfGqE7FCObk39VfqXs3Eo/E= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ft214cxJaqrRPOuAjpYwgA9AOElJnHrDZZEQ0jZPWwQ= -github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df h1:fLL+7ZZcbVOmE3XE0o+ZGS8zyPLjki7LrZAsXpcG4Sc= -github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df/go.mod h1:wbSa9W1CDDzR9AptQfYf/16bWqktaIQIZdJsuKWeqC8= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e h1:JQ+QbZcnecWYooLXWVbWZTbuHas9G68pHIxT/3uVlpI= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:v5JKfVqYEGFMTJIDrOV8m/dutSpw7mlif6wW3v26k68= -github.com/berachain/beacon-kit/mod/runtime v0.0.0-20240806160829-cde2d1347e7e h1:poP9ooPm9zZIWgaNOHphNWz1mGnNPKvzOuh9e1jA/EM= -github.com/berachain/beacon-kit/mod/runtime v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:2SpsgO8YGfj7mFToN5rRRknOJko2ULCL573O9Oyf8Uc= -github.com/berachain/beacon-kit/mod/state-transition v0.0.0-20240717225334-64ec6650da31 h1:1bJbJcoksyXfYMiga8YxPnkVKqT1lKwym/8kZnEPz58= -github.com/berachain/beacon-kit/mod/state-transition v0.0.0-20240717225334-64ec6650da31/go.mod h1:sIzib45R7B9Q99yvsYUcj2xJZPBpe3J9JbcBDMZNp7E= -github.com/berachain/beacon-kit/mod/storage v0.0.0-20240806160829-cde2d1347e7e h1:eIHdeGNL87GlWxFqAgsuUZpA/EBGvaOWXFMDuXSJgjU= -github.com/berachain/beacon-kit/mod/storage v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:1Hti34fIHtniBapeikvYF3KyA038+6If8BRkRKqWUy4= github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240806162353-1fa473c35c9a h1:XYQBsauxZ2FSY98yu3wAMXy7iz3w/eah5FJw5cGH/48= github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240806162353-1fa473c35c9a/go.mod h1:SUigh/LLjORN8Km5AOOdTc5PHGcy/QMIEHElng0mJJk= github.com/bgentry/speakeasy v0.2.0 h1:tgObeVOf8WAvtuAX6DhJ4xks4CFNwPDZiqzGqIHE51E= diff --git a/mod/runtime/go.mod b/mod/runtime/go.mod index c6c5c49688..91548f5ced 100644 --- a/mod/runtime/go.mod +++ b/mod/runtime/go.mod @@ -6,11 +6,14 @@ replace ( // The following are required to build with the latest version of the cosmos-sdk main branch: cosmossdk.io/api => cosmossdk.io/api v0.7.3-0.20240806152830-8fb47b368cd4 cosmossdk.io/x/staking => cosmossdk.io/x/staking v0.0.0-20240806152830-8fb47b368cd4 + // required until merged onto main + github.com/berachain/beacon-kit/mod/async => ../async + github.com/berachain/beacon-kit/mod/primitives => ../primitives github.com/cosmos/cosmos-sdk => github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240806162353-1fa473c35c9a ) require ( - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240617161612-ab1257fcf5a1 + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e @@ -42,7 +45,6 @@ require ( github.com/cosmos/gogoproto v1.5.0 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect - github.com/ethereum/go-ethereum v1.14.7 // indirect github.com/getsentry/sentry-go v0.28.1 // indirect github.com/go-kit/log v0.2.1 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect @@ -63,7 +65,7 @@ require ( golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect golang.org/x/net v0.27.0 // indirect - golang.org/x/sync v0.8.0 + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect diff --git a/mod/runtime/go.sum b/mod/runtime/go.sum index e579a4ab68..3819ed3700 100644 --- a/mod/runtime/go.sum +++ b/mod/runtime/go.sum @@ -1,21 +1,17 @@ -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624011057-b0afb8163f14 h1:uGcy4mqiyDzjImZsrxjU3U5NKnXpi+2s1cO0cz1LGNY= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624011057-b0afb8163f14/go.mod h1:tzZ5Oyg7BgCViZBkqDwHmfKPNI66xONwE5kWd1P2v7o= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240617161612-ab1257fcf5a1 h1:KlGloi0bl9DevoJPwFPyKcH7fXavgMhUQnwexvFNbY0= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240617161612-ab1257fcf5a1/go.mod h1:iXa+Q+i0q+GCpLzkusulO57K5vlkDgM77jtfMr3QdFA= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c h1:7f9dLYGOCMoV7LxT6YRmVSWLTPbGTTcxDPLPLvHGrOk= github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:nFybcw/ZhJ6Gu66dna301W2I7u61skm2HfHxQmdR68Q= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ft214cxJaqrRPOuAjpYwgA9AOElJnHrDZZEQ0jZPWwQ= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e h1:JQ+QbZcnecWYooLXWVbWZTbuHas9G68pHIxT/3uVlpI= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:v5JKfVqYEGFMTJIDrOV8m/dutSpw7mlif6wW3v26k68= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= github.com/btcsuite/btcd/btcec/v2 v2.3.3/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.5 h1:+wER79R5670vs/ZusMTF1yTcRYE5GUsFbdjdisflzM8= github.com/btcsuite/btcd/btcutil v1.1.5/go.mod h1:PSZZ4UitpLBWzxGd5VGOrLnmOjtPP/a6HaFo12zMs00= -github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ= -github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= @@ -37,8 +33,6 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= -github.com/ethereum/go-ethereum v1.14.7 h1:EHpv3dE8evQmpVEQ/Ne2ahB06n2mQptdwqaMNhAT29g= -github.com/ethereum/go-ethereum v1.14.7/go.mod h1:Mq0biU2jbdmKSZoqOj29017ygFrMnB5/Rifwp980W4o= github.com/ferranbt/fastssz v0.1.4-0.20240629094022-eac385e6ee79 h1:oMYkNRlaY+YxcbYW4U84mQQkujiloBbxQFnTOHUbkec= github.com/ferranbt/fastssz v0.1.4-0.20240629094022-eac385e6ee79/go.mod h1:EGSbefgAPd3M0hlBwOCw4Mkj+0YAaSnXw1QeLasY6XQ= github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k= From c6a8a43401bb742d25f298fd22cd7391478d3558 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 7 Aug 2024 20:21:03 -0400 Subject: [PATCH 15/75] tmr --- mod/async/pkg/server/msg.go | 3 +++ mod/beacon/blockchain/service.go | 2 +- mod/node-core/pkg/components/events.go | 2 +- mod/node-core/pkg/components/types.go | 25 +++++-------------------- mod/runtime/pkg/middleware/abci.go | 4 ++++ mod/runtime/pkg/service/registry.go | 1 + 6 files changed, 15 insertions(+), 22 deletions(-) diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index c9cdf2de00..3bd2b6eae0 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -21,6 +21,8 @@ package server import ( + "fmt" + "github.com/berachain/beacon-kit/mod/async/pkg/types" ) @@ -43,6 +45,7 @@ func (ms *MessageServer) Request(req types.MessageI, resp types.MessageI) error return ErrRouteNotFound } route.SendRequest(req) + fmt.Println("Sent request to route", req.ID()) return route.AwaitResponse(resp) } diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index c3338b462c..f174a96525 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -168,7 +168,7 @@ func (s *Service[ var processGenDataRequests chan *asynctypes.Message[GenesisT] s.dispatcher.RegisterMsgReceiver(messages.ProcessGenesisData, processGenDataRequests) - s.start(ctx, finalizeBeaconBlockRequests, verifyBeaconBlockRequests, processGenDataRequests) + go s.start(ctx, finalizeBeaconBlockRequests, verifyBeaconBlockRequests, processGenDataRequests) return nil } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index b444a0eb47..d92fede25a 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -46,7 +46,7 @@ func ProvideEventServer(in EventServerInput) *EventServer { // ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block // finalized events. func ProvideBeaconBlockFinalizedPublisher() *BeaconBlockFinalizedPublisher { - return messaging.NewPublisher[*BlockMessage](messages.BeaconBlockFinalizedEvent) + return messaging.NewPublisher[*FinalizedBlockEvent](messages.BeaconBlockFinalizedEvent) } // EventServerComponents returns all the depinject providers for the event diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 0c9bbfce99..2700299912 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -414,31 +414,16 @@ type ( ) /* -------------------------------------------------------------------------- */ -/* Events */ +/* Messages */ /* -------------------------------------------------------------------------- */ +// events type ( // FinalizedBlockEvent is a type alias for the block event. FinalizedBlockEvent = asynctypes.Event[*BeaconBlock] - - // TODO: delete all of these when done - - // GenesisEvent is a type alias for the genesis event. - GenesisEvent = asynctypes.Event[*Genesis] - - // SidecarEvent is a type alias for the sidecar event. - SidecarEvent = asynctypes.Event[*BlobSidecars] - - // SlotEvent is a type alias for the slot event. - SlotEvent = asynctypes.Event[*SlotData] - - // StatusEvent is a type alias for the status event. - StatusEvent = asynctypes.Event[*service.StatusEvent] - - // ValidatorUpdateEvent is a type alias for the validator update event. - ValidatorUpdateEvent = asynctypes.Event[transition.ValidatorUpdates] ) +// messages type ( // BlockMessage is a type alias for the block message. BlockMessage = asynctypes.Message[*BeaconBlock] @@ -459,7 +444,7 @@ type ( StatusMessage = asynctypes.Message[*service.StatusEvent] // ValidatorUpdateMessage is a type alias for the validator update message. - ValidatorUpdateMessage = asynctypes.Message[transition.ValidatorUpdates] + ValidatorUpdateMessage = asynctypes.Message[*transition.ValidatorUpdates] ) /* -------------------------------------------------------------------------- */ @@ -467,7 +452,7 @@ type ( /* -------------------------------------------------------------------------- */ type ( - BeaconBlockFinalizedPublisher = messaging.Publisher[*BlockMessage] + BeaconBlockFinalizedPublisher = messaging.Publisher[*FinalizedBlockEvent] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index df720cc285..b467fd0847 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -51,18 +51,22 @@ func (h *ABCIMiddleware[ ) data := new(GenesisT) if err := json.Unmarshal(bz, data); err != nil { + h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } + h.logger.Info("Dispatching ProcessGenesisData request") err := h.dispatcher.DispatchRequest( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, ), &valUpdateResp, ) if err != nil { + h.logger.Error("Failed to dispatch ProcessGenesisData request", "error", err) return nil, err } + h.logger.Info("Genesis initialization completed", "validator_updates", valUpdateResp.Data()) return valUpdateResp.Data(), valUpdateResp.Error() } diff --git a/mod/runtime/pkg/service/registry.go b/mod/runtime/pkg/service/registry.go index a1d5522968..4b5d0bb97b 100644 --- a/mod/runtime/pkg/service/registry.go +++ b/mod/runtime/pkg/service/registry.go @@ -73,6 +73,7 @@ func NewRegistry( // StartAll initialized each service in order of registration. func (s *Registry) StartAll(ctx context.Context) error { // start the dispatcher + s.logger.Info("Starting dispatcher") if err := s.dispatcher.Start(ctx); err != nil { return err } From 748d712003b7552bba7f101c37ce55a12b61d547 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 8 Aug 2024 03:18:49 -0400 Subject: [PATCH 16/75] logger --- mod/async/go.mod | 5 ++++- mod/async/go.sum | 2 ++ mod/async/pkg/dispatcher/dispatcher.go | 6 ++++++ mod/async/pkg/dispatcher/types.go | 3 +++ mod/async/pkg/server/event.go | 6 ++++++ mod/async/pkg/server/msg.go | 6 ++++++ mod/beacon/go.mod | 2 +- mod/beacon/go.sum | 4 ++-- mod/node-core/pkg/components/dispatcher.go | 9 ++++++++- mod/runtime/go.mod | 2 +- mod/runtime/go.sum | 4 ++-- 11 files changed, 41 insertions(+), 8 deletions(-) diff --git a/mod/async/go.mod b/mod/async/go.mod index e516897968..9df86a9b41 100644 --- a/mod/async/go.mod +++ b/mod/async/go.mod @@ -2,7 +2,10 @@ module github.com/berachain/beacon-kit/mod/async go 1.22.5 -require github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 +require ( + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd +) require ( github.com/cockroachdb/errors v1.11.3 // indirect diff --git a/mod/async/go.sum b/mod/async/go.sum index 9dcbe34e08..6ff86e4669 100644 --- a/mod/async/go.sum +++ b/mod/async/go.sum @@ -1,5 +1,7 @@ github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 46c3cda9de..eb2a9e1155 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -24,6 +24,7 @@ import ( "context" "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/log" ) // Dispatcher faciliates asynchronous communication between components, typically @@ -32,16 +33,21 @@ import ( type Dispatcher struct { eventServer EventServer msgServer MessageServer + logger log.Logger[any] } // NewDispatcher creates a new dispatcher. func NewDispatcher( eventServer EventServer, msgServer MessageServer, + logger log.Logger[any], ) *Dispatcher { + eventServer.SetLogger(logger) + msgServer.SetLogger(logger) return &Dispatcher{ eventServer: eventServer, msgServer: msgServer, + logger: logger, } } diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index d9a229beda..6a9409799b 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -24,6 +24,7 @@ import ( "context" "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/log" ) type MessageServer interface { @@ -31,6 +32,7 @@ type MessageServer interface { Request(req types.MessageI, resp types.MessageI) error Respond(resp types.MessageI) error RegisterRoute(mID types.MessageID, route types.MessageRoute) error + SetLogger(logger log.Logger[any]) } type EventServer interface { @@ -38,4 +40,5 @@ type EventServer interface { RegisterPublisher(mID types.MessageID, publisher types.Publisher) Subscribe(mID types.MessageID, ch any) error Publish(ctx context.Context, event types.MessageI) error + SetLogger(logger log.Logger[any]) } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 2fc6325dbe..3131d586f7 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -24,11 +24,13 @@ import ( "context" "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/log" ) // EventServer asyncronously dispatches events to subscribers. type EventServer struct { publishers map[types.MessageID]types.Publisher + logger log.Logger[any] } // NewEventServer creates a new event server. @@ -67,3 +69,7 @@ func (es *EventServer) Start(ctx context.Context) { func (es *EventServer) RegisterPublisher(eventID types.MessageID, publisher types.Publisher) { es.publishers[eventID] = publisher } + +func (es *EventServer) SetLogger(logger log.Logger[any]) { + es.logger = logger +} diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 3bd2b6eae0..50d1623b87 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -24,10 +24,12 @@ import ( "fmt" "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/log" ) type MessageServer struct { routes map[types.MessageID]types.MessageRoute + logger log.Logger[any] } func NewMessageServer() *MessageServer { @@ -81,3 +83,7 @@ func (ms *MessageServer) RegisterReceiver(messageID types.MessageID, ch any) err } return route.RegisterReceiver(ch) } + +func (ms *MessageServer) SetLogger(logger log.Logger[any]) { + ms.logger = logger +} diff --git a/mod/beacon/go.mod b/mod/beacon/go.mod index 734cde67ce..cec9d200b6 100644 --- a/mod/beacon/go.mod +++ b/mod/beacon/go.mod @@ -12,7 +12,7 @@ require ( github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240610210054-bfdc14c4013c github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 - github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e golang.org/x/sync v0.8.0 ) diff --git a/mod/beacon/go.sum b/mod/beacon/go.sum index 7c584945f5..76fd30efcb 100644 --- a/mod/beacon/go.sum +++ b/mod/beacon/go.sum @@ -14,8 +14,8 @@ github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c h1:7f9dLYGOCMoV7LxT6YRmVSWLTPbGTTcxDPLPLvHGrOk= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:nFybcw/ZhJ6Gu66dna301W2I7u61skm2HfHxQmdR68Q= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index 55a5835ffc..2f806f6ec7 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -22,7 +22,9 @@ package components import ( "cosmossdk.io/depinject" + sdklog "cosmossdk.io/log" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + "github.com/berachain/beacon-kit/mod/log" ) // DispatcherInput is the input for the Dispatcher. @@ -30,11 +32,16 @@ type DispatcherInput struct { depinject.In EventServer *EventServer MessageServer *MessageServer + Logger log.AdvancedLogger[any, sdklog.Logger] } // ProvideDispatcher provides a new Dispatcher. func ProvideDispatcher( in DispatcherInput, ) *Dispatcher { - return dispatcher.NewDispatcher(in.EventServer, in.MessageServer) + return dispatcher.NewDispatcher( + in.EventServer, + in.MessageServer, + in.Logger.With("service", "dispatcher"), + ) } diff --git a/mod/runtime/go.mod b/mod/runtime/go.mod index 91548f5ced..99a80883ce 100644 --- a/mod/runtime/go.mod +++ b/mod/runtime/go.mod @@ -14,7 +14,7 @@ replace ( require ( github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 - github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e github.com/cometbft/cometbft v1.0.0-rc1.0.20240805092115-3b2c5d9e1843 diff --git a/mod/runtime/go.sum b/mod/runtime/go.sum index 3819ed3700..4e763c3a10 100644 --- a/mod/runtime/go.sum +++ b/mod/runtime/go.sum @@ -2,8 +2,8 @@ github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256d github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c h1:7f9dLYGOCMoV7LxT6YRmVSWLTPbGTTcxDPLPLvHGrOk= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:nFybcw/ZhJ6Gu66dna301W2I7u61skm2HfHxQmdR68Q= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ft214cxJaqrRPOuAjpYwgA9AOElJnHrDZZEQ0jZPWwQ= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= From 7f2dbfa9be59e079021fe3a496a887b5dfa87bb7 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 8 Aug 2024 15:35:42 -0400 Subject: [PATCH 17/75] checkpoint --- mod/async/pkg/dispatcher/dispatcher.go | 3 +++ mod/async/pkg/messaging/errors.go | 14 +++++++++++++- mod/async/pkg/messaging/route.go | 12 +++++++++--- mod/async/pkg/server/msg.go | 11 +++++------ mod/beacon/blockchain/service.go | 12 +++++++----- mod/da/go.mod | 2 +- mod/da/go.sum | 4 ++-- mod/node-core/go.mod | 2 +- mod/node-core/go.sum | 4 ++-- mod/runtime/pkg/middleware/abci.go | 3 --- 10 files changed, 43 insertions(+), 24 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index eb2a9e1155..51965aab73 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -84,6 +84,7 @@ func (d *Dispatcher) DispatchResponse(resp types.MessageI) error { // Any subsequent events with dispatched to this Dispatcher must be // consistent with the type expected by . func (d *Dispatcher) RegisterPublisher(eventID types.MessageID, publisher types.Publisher) { + d.logger.Info("Publisher registered", "eventID", eventID) d.eventServer.RegisterPublisher(eventID, publisher) } @@ -99,6 +100,7 @@ func (d *Dispatcher) Subscribe(eventID types.MessageID, ch any) error { // RegisterMsgRecipient registers the given channel to the message with the // given . func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch any) error { + d.logger.Info("Message receiver registered", "messageID", messageID) return d.msgServer.RegisterReceiver(messageID, ch) } @@ -106,5 +108,6 @@ func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch any) erro // Any subsequent messages with sent to this Dispatcher must be // consistent with the type expected by . func (d *Dispatcher) RegisterRoute(messageID types.MessageID, route types.MessageRoute) { + d.logger.Info("Route registered", "messageID", messageID) d.msgServer.RegisterRoute(messageID, route) } diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index 6ca9316221..08c661b499 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -22,16 +22,28 @@ package messaging import ( "reflect" + "time" + "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/errors" ) // ErrTimeout is the error returned when a dispatch operation timed out. var ( - ErrTimeout = errors.New("timeout") + ErrTimeout = func(messageID types.MessageID, timeout time.Duration) error { + return errors.Newf("message %s timed out after %s", messageID, timeout) + } ErrRouteAlreadySet = errors.New("route already set") + ErrRegisteringNilChannel = func(messageID types.MessageID) error { + return errors.Newf("cannot register nil channel for route: %s", messageID) + } + + ErrReceiverNotListening = func(messageID types.MessageID) error { + return errors.Newf("receiver may be registered but not listening for route: %s", messageID) + } + errIncompatibleAssigneeType = func(assigner interface{}, assignee interface{}) error { assignerType := reflect.TypeOf(assigner) assigneeType := reflect.TypeOf(assignee) diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 46d3a6fc20..08835ef36f 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -58,6 +58,8 @@ func (r *Route[ReqT, RespT]) MessageID() types.MessageID { func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { if r.recipient != nil { return ErrRouteAlreadySet + } else if ch == nil { + return ErrRegisteringNilChannel(r.messageID) } typedCh, err := ensureType[chan ReqT](ch) if err != nil { @@ -73,8 +75,12 @@ func (r *Route[ReqT, RespT]) SendRequest(msg any) error { if err != nil { return err } - r.recipient <- typedMsg - return nil + select { + case r.recipient <- typedMsg: + return nil + default: + return ErrReceiverNotListening(r.messageID) + } } // SendResponse sends a response to the response channel. @@ -94,6 +100,6 @@ func (r *Route[ReqT, RespT]) AwaitResponse(emptyResp any) error { case msg := <-r.responseCh: return assignToResponse[RespT](msg, emptyResp) case <-time.After(r.timeout): - return ErrTimeout + return ErrTimeout(r.messageID, r.timeout) } } diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 50d1623b87..67bb7f020f 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -21,8 +21,6 @@ package server import ( - "fmt" - "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" ) @@ -46,8 +44,9 @@ func (ms *MessageServer) Request(req types.MessageI, resp types.MessageI) error if !ok { return ErrRouteNotFound } - route.SendRequest(req) - fmt.Println("Sent request to route", req.ID()) + if err := route.SendRequest(req); err != nil { + return err + } return route.AwaitResponse(resp) } @@ -58,8 +57,8 @@ func (ms *MessageServer) Respond(resp types.MessageI) error { if !ok { return ErrRouteNotFound } - route.SendResponse(resp) - return nil + ms.logger.Error("Responding to message", "resp", resp) + return route.SendResponse(resp) } // RegisterRoute registers the route with the given messageID. diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index f174a96525..104359a941 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -159,13 +159,13 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) Start(ctx context.Context) error { - var finalizeBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT] + finalizeBeaconBlockRequests := make(chan *asynctypes.Message[BeaconBlockT]) s.dispatcher.RegisterMsgReceiver(messages.FinalizeBeaconBlock, finalizeBeaconBlockRequests) - var verifyBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT] + verifyBeaconBlockRequests := make(chan *asynctypes.Message[BeaconBlockT]) s.dispatcher.RegisterMsgReceiver(messages.VerifyBeaconBlock, verifyBeaconBlockRequests) - var processGenDataRequests chan *asynctypes.Message[GenesisT] + processGenDataRequests := make(chan *asynctypes.Message[GenesisT]) s.dispatcher.RegisterMsgReceiver(messages.ProcessGenesisData, processGenDataRequests) go s.start(ctx, finalizeBeaconBlockRequests, verifyBeaconBlockRequests, processGenDataRequests) @@ -215,14 +215,16 @@ func (s *Service[ } // dispatch a response containing the validator updates - s.dispatcher.DispatchResponse( + if err := s.dispatcher.DispatchResponse( asynctypes.NewMessage( msg.Context(), messages.ProcessGenesisData, valUpdates, nil, ), - ) + ); err != nil { + s.logger.Error("Failed to dispatch response in handleProcessGenesisDataRequest", "error", err) + } } func (s *Service[ diff --git a/mod/da/go.mod b/mod/da/go.mod index 4c14674aae..775785a4a3 100644 --- a/mod/da/go.mod +++ b/mod/da/go.mod @@ -13,7 +13,7 @@ require ( github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 - github.com/berachain/beacon-kit/mod/log v0.0.0-20240624033454-8f3451361f44 + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240806160829-cde2d1347e7e github.com/crate-crypto/go-kzg-4844 v1.1.0 github.com/ethereum/c-kzg-4844 v1.0.3 diff --git a/mod/da/go.sum b/mod/da/go.sum index a2df690864..a813443f82 100644 --- a/mod/da/go.sum +++ b/mod/da/go.sum @@ -20,8 +20,8 @@ github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240624033454-8f3451361f44 h1:R4+mGOZDYA93rD4AUbYq4fSWmRsJhnlB/ww1ap09WOY= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240624033454-8f3451361f44/go.mod h1:xP5KcG56VfbPgz2ZRHerxm90MkjXMSDaGZNOOO5yfH4= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= diff --git a/mod/node-core/go.mod b/mod/node-core/go.mod index 69d1d9a58f..47e2f31fe1 100644 --- a/mod/node-core/go.mod +++ b/mod/node-core/go.mod @@ -37,7 +37,7 @@ require ( github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/execution v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/log v0.0.0-20240705193247-d464364483df + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/node-api v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/node-api/engines v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df diff --git a/mod/node-core/go.sum b/mod/node-core/go.sum index b77ada6f85..b6b07ff01f 100644 --- a/mod/node-core/go.sum +++ b/mod/node-core/go.sum @@ -88,8 +88,8 @@ github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240705193247-d464364483df h1:SnzeY9SCmKyEx0iGC/C/8E39ozpl/g5yI7lFXpmbMBI= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240705193247-d464364483df/go.mod h1:mJ0ZlK+izcPWcveHAtM4+W0a+8jhu5Y4DMPL2Takacg= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ft214cxJaqrRPOuAjpYwgA9AOElJnHrDZZEQ0jZPWwQ= github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240806162353-1fa473c35c9a h1:XYQBsauxZ2FSY98yu3wAMXy7iz3w/eah5FJw5cGH/48= diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index b467fd0847..63ecff330d 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -54,15 +54,12 @@ func (h *ABCIMiddleware[ h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } - - h.logger.Info("Dispatching ProcessGenesisData request") err := h.dispatcher.DispatchRequest( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, ), &valUpdateResp, ) if err != nil { - h.logger.Error("Failed to dispatch ProcessGenesisData request", "error", err) return nil, err } From c27ada88a088872cd35fb808936b03df4d73a081 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 8 Aug 2024 17:59:58 -0400 Subject: [PATCH 18/75] bointer] --- mod/async/pkg/dispatcher/dispatcher.go | 2 +- mod/async/pkg/dispatcher/types.go | 2 +- mod/async/pkg/messaging/assert.go | 20 ++++++-------------- mod/async/pkg/messaging/errors.go | 11 ----------- mod/async/pkg/server/msg.go | 3 +-- mod/node-core/pkg/components/types.go | 2 +- mod/runtime/pkg/middleware/abci.go | 2 +- 7 files changed, 11 insertions(+), 31 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 51965aab73..8340b826bb 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -67,7 +67,7 @@ func (d *Dispatcher) DispatchEvent(ctx context.Context, event types.MessageI) er // DispatchRequest dispatches the given request to the message server. // It will error if the and types are inconsistent with the // route registered for the given messageID. -func (d *Dispatcher) DispatchRequest(req types.MessageI, resp types.MessageI) error { +func (d *Dispatcher) DispatchRequest(req types.MessageI, resp any) error { return d.msgServer.Request(req, resp) } diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 6a9409799b..8a901f9a38 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -29,7 +29,7 @@ import ( type MessageServer interface { RegisterReceiver(mID types.MessageID, ch any) error - Request(req types.MessageI, resp types.MessageI) error + Request(req types.MessageI, resp any) error Respond(resp types.MessageI) error RegisterRoute(mID types.MessageID, route types.MessageRoute) error SetLogger(logger log.Logger[any]) diff --git a/mod/async/pkg/messaging/assert.go b/mod/async/pkg/messaging/assert.go index d3a1306418..5da01e069b 100644 --- a/mod/async/pkg/messaging/assert.go +++ b/mod/async/pkg/messaging/assert.go @@ -20,32 +20,24 @@ package messaging -import ( - "fmt" -) - // ensureType ensures that the provided entity is of type T. // It returns a typed entity or an error if the type is not correct. func ensureType[T any](e any) (T, error) { typedE, ok := e.(T) if !ok { - return *new(T), fmt.Errorf("provided message is not of type %T", *new(T)) + return *new(T), errIncompatibleAssignee(*new(T), e) } return typedE, nil } // assignToResponse assigns the received response to the assignee. -func assignToResponse[RespT any](receivedResponse RespT, emptyAssignee any) error { - // ensure that the assignee is a pointer to the response type - assigneePtr, ok := emptyAssignee.(**RespT) +func assignToResponse[RespT any](source RespT, dest any) error { + // ensure that the dest is a pointer to the response type + assigneePtr, ok := dest.(*RespT) if !ok { - return errIncompatibleAssigneeType(receivedResponse, emptyAssignee) - } - // ensure that the assignee pointer is not nil - if assigneePtr == nil { - return errIncompatibleAssignee(receivedResponse, emptyAssignee) + return errIncompatibleAssignee(new(RespT), dest) } // assign the received response to the assignee - *assigneePtr = &receivedResponse + *assigneePtr = source return nil } diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index 08c661b499..d56c1d3584 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -21,7 +21,6 @@ package messaging import ( - "reflect" "time" "github.com/berachain/beacon-kit/mod/async/pkg/types" @@ -44,16 +43,6 @@ var ( return errors.Newf("receiver may be registered but not listening for route: %s", messageID) } - errIncompatibleAssigneeType = func(assigner interface{}, assignee interface{}) error { - assignerType := reflect.TypeOf(assigner) - assigneeType := reflect.TypeOf(assignee) - return errors.Newf( - "incompatible assignee, expected: %T, received: %T", - assignerType, - assigneeType, - ) - } - errIncompatibleAssignee = func(assigner interface{}, assignee interface{}) error { return errors.Newf( "incompatible assignee, expected: %T, received: %T", diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 67bb7f020f..bbcd02559e 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -38,7 +38,7 @@ func NewMessageServer() *MessageServer { // Request sends a message to the server and awaits for a response. // The response is written to the provided response pointer. -func (ms *MessageServer) Request(req types.MessageI, resp types.MessageI) error { +func (ms *MessageServer) Request(req types.MessageI, resp any) error { // send message to request channel and await a response in the response channel route, ok := ms.routes[req.ID()] if !ok { @@ -57,7 +57,6 @@ func (ms *MessageServer) Respond(resp types.MessageI) error { if !ok { return ErrRouteNotFound } - ms.logger.Error("Responding to message", "resp", resp) return route.SendResponse(resp) } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 2700299912..5163eaac8b 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -444,7 +444,7 @@ type ( StatusMessage = asynctypes.Message[*service.StatusEvent] // ValidatorUpdateMessage is a type alias for the validator update message. - ValidatorUpdateMessage = asynctypes.Message[*transition.ValidatorUpdates] + ValidatorUpdateMessage = asynctypes.Message[transition.ValidatorUpdates] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 63ecff330d..229cb8da6b 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -47,7 +47,7 @@ func (h *ABCIMiddleware[ bz []byte, ) (transition.ValidatorUpdates, error) { var ( - valUpdateResp asynctypes.Message[transition.ValidatorUpdates] + valUpdateResp *asynctypes.Message[transition.ValidatorUpdates] ) data := new(GenesisT) if err := json.Unmarshal(bz, data); err != nil { From bc43819b7560585779453e7e96ce8a1163139a9b Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 8 Aug 2024 18:50:01 -0400 Subject: [PATCH 19/75] cleanup with arch --- mod/async/pkg/dispatcher/dispatcher.go | 4 ++-- mod/async/pkg/dispatcher/types.go | 2 +- mod/async/pkg/messaging/assert.go | 16 +++++++++------- mod/async/pkg/messaging/publisher.go | 3 ++- mod/async/pkg/messaging/route.go | 14 +++++++------- mod/async/pkg/server/event.go | 4 ++-- mod/async/pkg/server/msg.go | 4 ++-- mod/async/pkg/types/message.go | 2 -- mod/async/pkg/types/notifiers.go | 6 +++--- mod/beacon/blockchain/process.go | 2 +- 10 files changed, 29 insertions(+), 28 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 8340b826bb..e23e7a3058 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -60,8 +60,8 @@ func (d *Dispatcher) Start(ctx context.Context) error { // DispatchEvent dispatches the given event to the event server. // It will error if the type is inconsistent with the publisher // registered for the given eventID. -func (d *Dispatcher) DispatchEvent(ctx context.Context, event types.MessageI) error { - return d.eventServer.Publish(ctx, event) +func (d *Dispatcher) DispatchEvent(event types.MessageI) error { + return d.eventServer.Publish(event) } // DispatchRequest dispatches the given request to the message server. diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 8a901f9a38..0ddd3e80d0 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -39,6 +39,6 @@ type EventServer interface { Start(ctx context.Context) RegisterPublisher(mID types.MessageID, publisher types.Publisher) Subscribe(mID types.MessageID, ch any) error - Publish(ctx context.Context, event types.MessageI) error + Publish(event types.MessageI) error SetLogger(logger log.Logger[any]) } diff --git a/mod/async/pkg/messaging/assert.go b/mod/async/pkg/messaging/assert.go index 5da01e069b..0293b245b2 100644 --- a/mod/async/pkg/messaging/assert.go +++ b/mod/async/pkg/messaging/assert.go @@ -30,14 +30,16 @@ func ensureType[T any](e any) (T, error) { return typedE, nil } -// assignToResponse assigns the received response to the assignee. -func assignToResponse[RespT any](source RespT, dest any) error { - // ensure that the dest is a pointer to the response type - assigneePtr, ok := dest.(*RespT) +// assign assigns the source value to the dest +// by pointing destPtr to the source value. +// contract: destPtr -> *T +func assign[T any](source T, destPtr any) error { + // ensure that the destPtr is a pointer to the response type + typedDestPtr, ok := destPtr.(*T) if !ok { - return errIncompatibleAssignee(new(RespT), dest) + return errIncompatibleAssignee(new(T), destPtr) } - // assign the received response to the assignee - *assigneePtr = source + // assign the underlying value of destPtr to the source value + *typedDestPtr = source return nil } diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index cb423bb220..6b6dc31927 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -87,11 +87,12 @@ func (p *Publisher[T]) start(ctx context.Context) { // Publish publishes a msg to the b. // Returns ErrTimeout on timeout. -func (p *Publisher[T]) Publish(ctx context.Context, msg any) error { +func (p *Publisher[T]) Publish(msg types.MessageI) error { typedMsg, err := ensureType[T](msg) if err != nil { return err } + ctx := msg.Context() select { case p.msgs <- typedMsg: return nil diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 08835ef36f..84400b10a8 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -70,8 +70,8 @@ func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { } // SendRequest sends a request to the recipient. -func (r *Route[ReqT, RespT]) SendRequest(msg any) error { - typedMsg, err := ensureType[ReqT](msg) +func (r *Route[ReqT, RespT]) SendRequest(req types.MessageI) error { + typedMsg, err := ensureType[ReqT](req) if err != nil { return err } @@ -84,8 +84,8 @@ func (r *Route[ReqT, RespT]) SendRequest(msg any) error { } // SendResponse sends a response to the response channel. -func (r *Route[ReqT, RespT]) SendResponse(msg any) error { - typedMsg, err := ensureType[RespT](msg) +func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { + typedMsg, err := ensureType[RespT](resp) if err != nil { return err } @@ -95,10 +95,10 @@ func (r *Route[ReqT, RespT]) SendResponse(msg any) error { // AwaitResponse listens for a response and returns it if it is available // before the timeout. Otherwise, it returns ErrTimeout. -func (r *Route[ReqT, RespT]) AwaitResponse(emptyResp any) error { +func (r *Route[ReqT, RespT]) AwaitResponse(respPtr any) error { select { - case msg := <-r.responseCh: - return assignToResponse[RespT](msg, emptyResp) + case resp := <-r.responseCh: + return assign(resp, respPtr) case <-time.After(r.timeout): return ErrTimeout(r.messageID, r.timeout) } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 3131d586f7..8375181f97 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -41,8 +41,8 @@ func NewEventServer() *EventServer { } // Dispatch dispatches the given event to the feed with the given eventID. -func (es *EventServer) Publish(ctx context.Context, event types.MessageI) error { - return es.publishers[event.ID()].Publish(ctx, event) +func (es *EventServer) Publish(event types.MessageI) error { + return es.publishers[event.ID()].Publish(event) } // Subscribe subscribes the given channel to the feed with the given eventID. diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index bbcd02559e..0ae4c58b6f 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -38,7 +38,7 @@ func NewMessageServer() *MessageServer { // Request sends a message to the server and awaits for a response. // The response is written to the provided response pointer. -func (ms *MessageServer) Request(req types.MessageI, resp any) error { +func (ms *MessageServer) Request(req types.MessageI, respPtr any) error { // send message to request channel and await a response in the response channel route, ok := ms.routes[req.ID()] if !ok { @@ -47,7 +47,7 @@ func (ms *MessageServer) Request(req types.MessageI, resp any) error { if err := route.SendRequest(req); err != nil { return err } - return route.AwaitResponse(resp) + return route.AwaitResponse(respPtr) } // Respond sends a response to the route that corresponds to the response's diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index 36d85f672f..5fd85d97b9 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -28,8 +28,6 @@ import ( type MessageI interface { ID() MessageID Context() context.Context - Error() error - Is(MessageID) bool } // A Message is an asynchronous message meant for a single recipient. diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index 6537f37bd0..c3f8850847 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -27,7 +27,7 @@ type Publisher interface { // Start starts the event feed. Start(ctx context.Context) // Publish publishes the given event to the event feed. - Publish(ctx context.Context, event any) error + Publish(event MessageI) error // Subscribe subscribes the given channel to the event feed. Subscribe(ch any) error // Unsubscribe unsubscribes the given channel from the event feed. @@ -39,9 +39,9 @@ type MessageRoute interface { // RegisterRecipient sets the recipient for the route. RegisterReceiver(ch any) error // SendRequest sends a request to the recipient. - SendRequest(msg any) error + SendRequest(msg MessageI) error // SendResponse sends a response to the recipient. - SendResponse(msg any) error + SendResponse(msg MessageI) error // AwaitResponse awaits a response from the route. AwaitResponse(emptyResp any) error // MessageID returns the message ID that the route is responsible for. diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 29ad39575b..2fcfd24805 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -84,7 +84,7 @@ func (s *Service[ // We won't send a fcu if the block is bad, should be addressed // via ticker later. s.dispatcher.DispatchEvent( - ctx, asynctypes.NewEvent( + asynctypes.NewEvent( ctx, messages.BeaconBlockFinalizedEvent, blk, ), ) From 9213b65e0b985f0bb97003fcf61c47754188713e Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 8 Aug 2024 18:52:43 -0400 Subject: [PATCH 20/75] rename --- mod/async/pkg/dispatcher/dispatcher.go | 12 ++++++------ mod/beacon/blockchain/process.go | 2 +- mod/beacon/blockchain/service.go | 6 +++--- mod/beacon/validator/service.go | 2 +- mod/da/pkg/da/service.go | 4 ++-- mod/runtime/pkg/middleware/abci.go | 12 ++++++------ 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index e23e7a3058..61731d451b 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -57,24 +57,24 @@ func (d *Dispatcher) Start(ctx context.Context) error { return nil } -// DispatchEvent dispatches the given event to the event server. +// Publish dispatches the given event to the event server. // It will error if the type is inconsistent with the publisher // registered for the given eventID. -func (d *Dispatcher) DispatchEvent(event types.MessageI) error { +func (d *Dispatcher) Publish(event types.MessageI) error { return d.eventServer.Publish(event) } -// DispatchRequest dispatches the given request to the message server. +// Request dispatches the given request to the message server. // It will error if the and types are inconsistent with the // route registered for the given messageID. -func (d *Dispatcher) DispatchRequest(req types.MessageI, resp any) error { +func (d *Dispatcher) Request(req types.MessageI, resp any) error { return d.msgServer.Request(req, resp) } -// DispatchResponse dispatches the given response to the message server. +// Respond dispatches the given response to the message server. // It will error if the type is inconsistent with the route registered // for the given messageID. -func (d *Dispatcher) DispatchResponse(resp types.MessageI) error { +func (d *Dispatcher) Respond(resp types.MessageI) error { return d.msgServer.Respond(resp) } diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 2fcfd24805..380dcb3535 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -83,7 +83,7 @@ func (s *Service[ // TODO: this is hood as fuck. // We won't send a fcu if the block is bad, should be addressed // via ticker later. - s.dispatcher.DispatchEvent( + s.dispatcher.Publish( asynctypes.NewEvent( ctx, messages.BeaconBlockFinalizedEvent, blk, ), diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 104359a941..2afc9c5ed5 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -215,7 +215,7 @@ func (s *Service[ } // dispatch a response containing the validator updates - if err := s.dispatcher.DispatchResponse( + if err := s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.ProcessGenesisData, @@ -239,7 +239,7 @@ func (s *Service[ } // dispatch a response with the error result from VerifyIncomingBlock - s.dispatcher.DispatchResponse( + s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.VerifyBeaconBlock, @@ -267,7 +267,7 @@ func (s *Service[ } // dispatch a response with the validator updates - s.dispatcher.DispatchResponse( + s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.FinalizeBeaconBlock, diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index e5102f3c59..3c9b5a0cc7 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -220,7 +220,7 @@ func (s *Service[ var blkData BeaconBlockBundleT blkData.SetBeaconBlock(blk) blkData.SetSidecars(sidecars) - s.dispatcher.DispatchResponse(asynctypes.NewMessage( + s.dispatcher.Respond(asynctypes.NewMessage( req.Context(), messages.BuildBeaconBlockAndSidecars, blkData, diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 5ca256dc03..0cd51ae6d6 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -137,7 +137,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( } // dispatch a response to acknowledge the request. - s.dispatcher.DispatchResponse( + s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.ProcessSidecars, @@ -163,7 +163,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( } // dispatch a response to acknowledge the request. - s.dispatcher.DispatchResponse( + s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.VerifySidecars, diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 229cb8da6b..d8a3853d85 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -54,7 +54,7 @@ func (h *ABCIMiddleware[ h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } - err := h.dispatcher.DispatchRequest( + err := h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, ), &valUpdateResp, @@ -85,7 +85,7 @@ func (h *ABCIMiddleware[ defer h.metrics.measurePrepareProposalDuration(startTime) // request a built beacon block for the given slot - h.dispatcher.DispatchRequest( + h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.BuildBeaconBlockAndSidecars, slotData, ), &beaconBlkBundleResp, @@ -153,7 +153,7 @@ func (h *ABCIMiddleware[ } // verify the beacon block - h.dispatcher.DispatchRequest( + h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.VerifyBeaconBlock, blk, ), &beaconBlockResp, @@ -169,7 +169,7 @@ func (h *ABCIMiddleware[ } // verify the blob sidecars - h.dispatcher.DispatchRequest( + h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.VerifySidecars, sidecars, ), &sidecarsResp, @@ -241,7 +241,7 @@ func (h *ABCIMiddleware[ } // verify the blob sidecars - h.dispatcher.DispatchRequest( + h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.ProcessSidecars, blobs, ), &sidecarsResp, @@ -250,7 +250,7 @@ func (h *ABCIMiddleware[ return nil, sidecarsResp.Error() } - h.dispatcher.DispatchRequest( + h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.FinalizeBeaconBlock, blk, ), &valUpdatesResp, From b6cb638bc772e4fb40fa41bcefcb85c0485d238e Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 8 Aug 2024 19:20:34 -0400 Subject: [PATCH 21/75] fixes & cleanup --- mod/beacon/block_store/service.go | 4 +- mod/beacon/blockchain/service.go | 41 ++++++++++++++----- mod/beacon/validator/service.go | 12 ++++-- mod/da/pkg/da/service.go | 18 +++++--- .../pkg/components/availability_store.go | 9 ++-- mod/node-core/pkg/components/block_store.go | 12 +++--- .../pkg/components/deposit_service.go | 8 +++- mod/node-core/pkg/components/deposit_store.go | 9 ++-- mod/runtime/pkg/middleware/abci.go | 14 ++++--- 9 files changed, 88 insertions(+), 39 deletions(-) diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 0d7fe108f3..0530b0b840 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -71,7 +71,9 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { s.logger.Warn("block service is disabled, skipping storing blocks") return nil } - var finalizedBlkCh chan *asynctypes.Event[BeaconBlockT] + + // subscribe a channel to the finalized block events. + var finalizedBlkCh = make(chan *asynctypes.Event[BeaconBlockT]) if err := s.dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 2afc9c5ed5..c410ab9cd0 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -156,22 +156,40 @@ func (s *Service[ return "blockchain" } +// Start sets up the service to listen for FinalizeBeaconBlock, +// VerifyBeaconBlock, and ProcessGenesisData requests, and handles them +// accordingly. func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) Start(ctx context.Context) error { - finalizeBeaconBlockRequests := make(chan *asynctypes.Message[BeaconBlockT]) - s.dispatcher.RegisterMsgReceiver(messages.FinalizeBeaconBlock, finalizeBeaconBlockRequests) - - verifyBeaconBlockRequests := make(chan *asynctypes.Message[BeaconBlockT]) - s.dispatcher.RegisterMsgReceiver(messages.VerifyBeaconBlock, verifyBeaconBlockRequests) - - processGenDataRequests := make(chan *asynctypes.Message[GenesisT]) - s.dispatcher.RegisterMsgReceiver(messages.ProcessGenesisData, processGenDataRequests) + // register a channel as the receiver for FinalizeBeaconBlock: + finalizeBlkReqs := make(chan *asynctypes.Message[BeaconBlockT]) + if err := s.dispatcher.RegisterMsgReceiver( + messages.FinalizeBeaconBlock, finalizeBlkReqs, + ); err != nil { + return err + } + // register a channel as the receiver for VerifyBeaconBlock: + verifyBlkReqs := make(chan *asynctypes.Message[BeaconBlockT]) + if err := s.dispatcher.RegisterMsgReceiver( + messages.VerifyBeaconBlock, verifyBlkReqs, + ); err != nil { + return err + } + // register a channel as the receiver for ProcessGenesisData: + processGenReqs := make(chan *asynctypes.Message[GenesisT]) + if err := s.dispatcher.RegisterMsgReceiver( + messages.ProcessGenesisData, processGenReqs, + ); err != nil { + return err + } - go s.start(ctx, finalizeBeaconBlockRequests, verifyBeaconBlockRequests, processGenDataRequests) + // start a goroutine to listen for requests and handle accordingly + go s.start(ctx, finalizeBlkReqs, verifyBlkReqs, processGenReqs) return nil } +// start starts the service. func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) start( @@ -223,7 +241,10 @@ func (s *Service[ nil, ), ); err != nil { - s.logger.Error("Failed to dispatch response in handleProcessGenesisDataRequest", "error", err) + s.logger.Error( + "Failed to dispatch response in process genesis data", + "error", err, + ) } } diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 3c9b5a0cc7..9f85f604d3 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -178,11 +178,15 @@ func (s *Service[ ctx context.Context, ) error { // register a receiver channel for build block requests - buildBlockRequests := make(chan *asynctypes.Message[SlotDataT]) - s.dispatcher.RegisterMsgReceiver(messages.BuildBeaconBlockAndSidecars, buildBlockRequests) + buildBlkReqs := make(chan *asynctypes.Message[SlotDataT]) + if err := s.dispatcher.RegisterMsgReceiver( + messages.BuildBeaconBlockAndSidecars, buildBlkReqs, + ); err != nil { + return err + } - // start the service - go s.start(ctx, buildBlockRequests) + // start a goroutine to listen for requests and handle accordingly + go s.start(ctx, buildBlkReqs) return nil } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 0cd51ae6d6..e16009c9f3 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -86,14 +86,22 @@ func (s *Service[_, _, _, _]) Name() string { // VerifySidecars messages, and begins listening for these requests. func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { // register as recipient of ProcessSidecars messages. - var sidecarsProcessRequests chan *asynctypes.Message[BlobSidecarsT] - s.dispatcher.RegisterMsgReceiver(messages.ProcessSidecars, &sidecarsProcessRequests) + sidecarsProcessRequests := make(chan *asynctypes.Message[BlobSidecarsT]) + if err := s.dispatcher.RegisterMsgReceiver( + messages.ProcessSidecars, sidecarsProcessRequests, + ); err != nil { + return err + } // register as recipient of VerifySidecars messages. - var sidecarVerifyRequests chan *asynctypes.Message[BlobSidecarsT] - s.dispatcher.RegisterMsgReceiver(messages.VerifySidecars, &sidecarVerifyRequests) + sidecarVerifyRequests := make(chan *asynctypes.Message[BlobSidecarsT]) + if err := s.dispatcher.RegisterMsgReceiver( + messages.VerifySidecars, sidecarVerifyRequests, + ); err != nil { + return err + } - // begin listening loop to handle process and verify requests. + // start a goroutine to listen for requests and handle accordingly go s.start(ctx, sidecarsProcessRequests, sidecarVerifyRequests) return nil } diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index edd492f0bf..cef82d0981 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -90,9 +90,12 @@ func ProvideAvailabilityPruner( return nil, errors.New("availability store does not have a range db") } - var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { - in.Logger.Error("failed to subscribe to block feed", "err", err) + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index fb2ad4b547..0e18be153a 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -21,8 +21,6 @@ package components import ( - "fmt" - "cosmossdk.io/depinject" "cosmossdk.io/log" storev2 "cosmossdk.io/store/v2/db" @@ -73,9 +71,13 @@ type BlockPrunerInput struct { func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { - var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { - return nil, fmt.Errorf("failed to subscribe to block feed: %w", err) + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) + return nil, err } return pruner.NewPruner[ diff --git a/mod/node-core/pkg/components/deposit_service.go b/mod/node-core/pkg/components/deposit_service.go index e6e9883fd2..c2e3611cf2 100644 --- a/mod/node-core/pkg/components/deposit_service.go +++ b/mod/node-core/pkg/components/deposit_service.go @@ -46,8 +46,12 @@ type DepositServiceIn struct { // ProvideDepositService provides the deposit service to the depinject // framework. func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { - var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index 24e034c5b8..d9a275d96b 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -70,9 +70,12 @@ type DepositPrunerInput struct { func ProvideDepositPruner( in DepositPrunerInput, ) (DepositPruner, error) { - var finalizedBlkCh chan *FinalizedBlockEvent - if err := in.Dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { - in.Logger.Error("failed to subscribe to block feed", "err", err) + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) return nil, err } diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index d8a3853d85..0e35f55547 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -54,6 +54,8 @@ func (h *ABCIMiddleware[ h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } + + // request for validator updates err := h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, @@ -80,7 +82,7 @@ func (h *ABCIMiddleware[ ) ([]byte, []byte, error) { var ( startTime = time.Now() - beaconBlkBundleResp asynctypes.Message[BeaconBlockBundleT] + beaconBlkBundleResp *asynctypes.Message[BeaconBlockBundleT] ) defer h.metrics.measurePrepareProposalDuration(startTime) @@ -101,7 +103,7 @@ func (h *ABCIMiddleware[ _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, _, ]) handleBeaconBlockBundleResponse( ctx context.Context, - bbbResp asynctypes.Message[BeaconBlockBundleT], + bbbResp *asynctypes.Message[BeaconBlockBundleT], ) ([]byte, []byte, error) { // handle response errors if bbbResp.Error() != nil { @@ -137,8 +139,8 @@ func (h *ABCIMiddleware[ sidecars BlobSidecarsT err error startTime = time.Now() - beaconBlockResp asynctypes.Message[BeaconBlockT] - sidecarsResp asynctypes.Message[BlobSidecarsT] + beaconBlockResp *asynctypes.Message[BeaconBlockT] + sidecarsResp *asynctypes.Message[BlobSidecarsT] ) abciReq, ok := req.(*cmtabci.ProcessProposalRequest) if !ok { @@ -223,8 +225,8 @@ func (h *ABCIMiddleware[ ctx context.Context, ) (transition.ValidatorUpdates, error) { var ( - sidecarsResp asynctypes.Message[BlobSidecarsT] - valUpdatesResp asynctypes.Message[transition.ValidatorUpdates] + sidecarsResp *asynctypes.Message[BlobSidecarsT] + valUpdatesResp *asynctypes.Message[transition.ValidatorUpdates] ) blk, blobs, err := encoding. ExtractBlobsAndBlockFromRequest[BeaconBlockT, BlobSidecarsT]( From a0fcd08401ef12b23342c9f28ecf0b41722b78f0 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 03:04:13 -0400 Subject: [PATCH 22/75] checkpoint --- mod/async/pkg/messaging/route.go | 6 ++++++ mod/beacon/block_store/service.go | 4 ++-- mod/beacon/validator/service.go | 27 ++++++++++++------------ mod/beacon/validator/types.go | 4 ++-- mod/da/pkg/types/bundle.go | 28 ++++++++++++------------- mod/node-core/pkg/components/types.go | 5 ++++- mod/primitives/pkg/messages/messages.go | 2 +- mod/runtime/pkg/middleware/abci.go | 6 ++++-- 8 files changed, 47 insertions(+), 35 deletions(-) diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 84400b10a8..6702152eb0 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -21,6 +21,7 @@ package messaging import ( + "fmt" "sync" "time" @@ -85,10 +86,12 @@ func (r *Route[ReqT, RespT]) SendRequest(req types.MessageI) error { // SendResponse sends a response to the response channel. func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { + fmt.Println("SENDING RESPONSE", resp) typedMsg, err := ensureType[RespT](resp) if err != nil { return err } + fmt.Println("GOOD TYPE", typedMsg) r.responseCh <- typedMsg return nil } @@ -96,10 +99,13 @@ func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { // AwaitResponse listens for a response and returns it if it is available // before the timeout. Otherwise, it returns ErrTimeout. func (r *Route[ReqT, RespT]) AwaitResponse(respPtr any) error { + fmt.Println("awaiting RESPONSESESESE") select { case resp := <-r.responseCh: + fmt.Println("GOT RESPONSESESESE") return assign(resp, respPtr) case <-time.After(r.timeout): + fmt.Println("TIMED OUT GREENENENENENENENENNENENENENENENENENEN") return ErrTimeout(r.messageID, r.timeout) } } diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 0530b0b840..ea4647d3ab 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -85,13 +85,13 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { // listenAndStore listens for blocks and stores them in the KVStore. func (s *Service[BeaconBlockT, _]) listenAndStore( ctx context.Context, - subBlkCh <-chan *asynctypes.Event[BeaconBlockT], + finalizeBlockCh <-chan *asynctypes.Event[BeaconBlockT], ) { for { select { case <-ctx.Done(): return - case msg := <-subBlkCh: + case msg := <-finalizeBlockCh: slot := msg.Data().GetSlot() if err := s.store.Set(slot, msg.Data()); err != nil { s.logger.Error( diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 9f85f604d3..30053d1b81 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -39,8 +39,8 @@ type Service[ AttestationDataT, BeaconBlockT, BeaconBlockBodyT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], - BlockDataT BeaconBlockBundle[ - BeaconBlockT, BlobSidecarsT, + BeaconBlockBundleT BeaconBlockBundle[ + BeaconBlockBundleT, BeaconBlockT, BlobSidecarsT, ], BeaconBlockBodyT BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, @@ -102,7 +102,7 @@ func NewService[ Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], BeaconBlockBundleT BeaconBlockBundle[ - BeaconBlockT, BlobSidecarsT, + BeaconBlockBundleT, BeaconBlockT, BlobSidecarsT, ], BeaconBlockBodyT BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, @@ -178,15 +178,15 @@ func (s *Service[ ctx context.Context, ) error { // register a receiver channel for build block requests - buildBlkReqs := make(chan *asynctypes.Message[SlotDataT]) + buildBlkBundleReqs := make(chan *asynctypes.Message[SlotDataT]) if err := s.dispatcher.RegisterMsgReceiver( - messages.BuildBeaconBlockAndSidecars, buildBlkReqs, + messages.BuildBeaconBlockAndSidecars, buildBlkBundleReqs, ); err != nil { return err } // start a goroutine to listen for requests and handle accordingly - go s.start(ctx, buildBlkReqs) + go s.start(ctx, buildBlkBundleReqs) return nil } @@ -195,13 +195,13 @@ func (s *Service[ _, _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) start( ctx context.Context, - buildBlockBundleRequests chan *asynctypes.Message[SlotDataT], + buildBlkBundleReqs chan *asynctypes.Message[SlotDataT], ) { for { select { case <-ctx.Done(): return - case req := <-buildBlockBundleRequests: + case req := <-buildBlkBundleReqs: s.handleBuildBlockBundleRequest(req) } } @@ -221,12 +221,13 @@ func (s *Service[ } // bundle the block and sidecars and dispatch the response + // blkData := *new(BeaconBlockBundleT) var blkData BeaconBlockBundleT - blkData.SetBeaconBlock(blk) - blkData.SetSidecars(sidecars) - s.dispatcher.Respond(asynctypes.NewMessage( + if err := s.dispatcher.Respond(asynctypes.NewMessage( req.Context(), messages.BuildBeaconBlockAndSidecars, - blkData, - )) + blkData.New(blk, sidecars), + )); err != nil { + s.logger.Error("failed to respond", "err", err) + } } diff --git a/mod/beacon/validator/types.go b/mod/beacon/validator/types.go index aabf49bf01..11799d5095 100644 --- a/mod/beacon/validator/types.go +++ b/mod/beacon/validator/types.go @@ -67,13 +67,13 @@ type BeaconBlock[ // BeaconBlockBundle represents a block data interface. type BeaconBlockBundle[ + T any, BeaconBlockT any, BlobSidecarsT any, ] interface { + New(BeaconBlockT, BlobSidecarsT) T GetBeaconBlock() BeaconBlockT GetSidecars() BlobSidecarsT - SetBeaconBlock(BeaconBlockT) - SetSidecars(BlobSidecarsT) } // BeaconBlockBody represents a beacon block body interface. diff --git a/mod/da/pkg/types/bundle.go b/mod/da/pkg/types/bundle.go index 4287595b12..1267376eee 100644 --- a/mod/da/pkg/types/bundle.go +++ b/mod/da/pkg/types/bundle.go @@ -20,25 +20,25 @@ package types -import "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" +type BlockBundle[BeaconBlockT any, BlobSidecarsT any] struct { + Block BeaconBlockT + Sidecars BlobSidecarsT +} -type BlockBundle struct { - Block *types.BeaconBlock - Sidecars *BlobSidecars +func (bb *BlockBundle[BeaconBlockT, BlobSidecarsT]) New( + block BeaconBlockT, + sidecars BlobSidecarsT, +) *BlockBundle[BeaconBlockT, BlobSidecarsT] { + return &BlockBundle[BeaconBlockT, BlobSidecarsT]{ + Block: block, + Sidecars: sidecars, + } } -func (bb *BlockBundle) GetBeaconBlock() *types.BeaconBlock { +func (bb *BlockBundle[BeaconBlockT, _]) GetBeaconBlock() BeaconBlockT { return bb.Block } -func (bb *BlockBundle) GetSidecars() *BlobSidecars { +func (bb *BlockBundle[_, BlobSidecarsT]) GetSidecars() BlobSidecarsT { return bb.Sidecars } - -func (bb *BlockBundle) SetBeaconBlock(block *types.BeaconBlock) { - bb.Block = block -} - -func (bb *BlockBundle) SetSidecars(sidecars *BlobSidecars) { - bb.Sidecars = sidecars -} diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 5163eaac8b..bdf0cce42c 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -98,7 +98,10 @@ type ( // BeaconBlock type aliases. BeaconBlock = types.BeaconBlock - BeaconBlockBundle = datypes.BlockBundle + BeaconBlockBundle = datypes.BlockBundle[ + *BeaconBlock, + *BlobSidecars, + ] BeaconBlockBody = types.BeaconBlockBody BeaconBlockHeader = types.BeaconBlockHeader diff --git a/mod/primitives/pkg/messages/messages.go b/mod/primitives/pkg/messages/messages.go index 3bf00c8ec7..2a98ed0b4b 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/primitives/pkg/messages/messages.go @@ -22,7 +22,7 @@ package messages // messages const ( - BuildBeaconBlockAndSidecars = "build-beacon-block" + BuildBeaconBlockAndSidecars = "build-beacon-block-and-sidecars" VerifyBeaconBlock = "verify-beacon-block" FinalizeBeaconBlock = "finalize-beacon-block" ProcessGenesisData = "process-genesis-data" diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 0e35f55547..edc423b083 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -87,11 +87,13 @@ func (h *ABCIMiddleware[ defer h.metrics.measurePrepareProposalDuration(startTime) // request a built beacon block for the given slot - h.dispatcher.Request( + if err := h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.BuildBeaconBlockAndSidecars, slotData, ), &beaconBlkBundleResp, - ) + ); err != nil { + return nil, nil, err + } // gossip the built beacon block and blob sidecars return h.handleBeaconBlockBundleResponse(ctx, beaconBlkBundleResp) From da942ef05ca190b5ba353252624cb2131e01e5f0 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 03:40:41 -0400 Subject: [PATCH 23/75] bet --- mod/async/pkg/messaging/constants.go | 3 ++- mod/async/pkg/messaging/errors.go | 9 +++++++-- mod/async/pkg/messaging/route.go | 10 +++------- mod/beacon/validator/service.go | 3 ++- mod/node-core/pkg/components/messages.go | 24 ++++++++++++------------ mod/runtime/pkg/middleware/abci.go | 9 ++++----- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/mod/async/pkg/messaging/constants.go b/mod/async/pkg/messaging/constants.go index 31f388be00..f53506c168 100644 --- a/mod/async/pkg/messaging/constants.go +++ b/mod/async/pkg/messaging/constants.go @@ -22,6 +22,7 @@ package messaging import "time" +// TODO: make timeout configurable thorugh config/context const ( // defaultPublisherTimeout specifies the default timeout when the publisher // tries to send a message to a client, a message is published to the @@ -31,7 +32,7 @@ const ( // defaultRouterTimeout specifies the default timeout when the router // tries to send a message to a client, a message is published to the // router, or a client subscribes or unsubscribes. - defaultRouterTimeout = time.Second + defaultRouterTimeout = 2 * time.Second // defaultBufferSize specifies the default size of the message buffer. defaultBufferSize = 10 diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index d56c1d3584..6ca23beaa8 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -28,6 +28,8 @@ import ( ) // ErrTimeout is the error returned when a dispatch operation timed out. +// +//nolint:lll // long errors var ( ErrTimeout = func(messageID types.MessageID, timeout time.Duration) error { return errors.Newf("message %s timed out after %s", messageID, timeout) @@ -39,8 +41,11 @@ var ( return errors.Newf("cannot register nil channel for route: %s", messageID) } - ErrReceiverNotListening = func(messageID types.MessageID) error { - return errors.Newf("receiver may be registered but not listening for route: %s", messageID) + ErrReceiverNotReady = func(messageID types.MessageID) error { + return errors.Newf( + "receiver channel is either full, closed, or not listening. Route: %s", + messageID, + ) } errIncompatibleAssignee = func(assigner interface{}, assignee interface{}) error { diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 6702152eb0..3195882e5c 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -21,7 +21,6 @@ package messaging import ( - "fmt" "sync" "time" @@ -76,22 +75,22 @@ func (r *Route[ReqT, RespT]) SendRequest(req types.MessageI) error { if err != nil { return err } + select { case r.recipient <- typedMsg: return nil default: - return ErrReceiverNotListening(r.messageID) + // Channel is full or closed + return ErrReceiverNotReady(r.messageID) } } // SendResponse sends a response to the response channel. func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { - fmt.Println("SENDING RESPONSE", resp) typedMsg, err := ensureType[RespT](resp) if err != nil { return err } - fmt.Println("GOOD TYPE", typedMsg) r.responseCh <- typedMsg return nil } @@ -99,13 +98,10 @@ func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { // AwaitResponse listens for a response and returns it if it is available // before the timeout. Otherwise, it returns ErrTimeout. func (r *Route[ReqT, RespT]) AwaitResponse(respPtr any) error { - fmt.Println("awaiting RESPONSESESESE") select { case resp := <-r.responseCh: - fmt.Println("GOT RESPONSESESESE") return assign(resp, respPtr) case <-time.After(r.timeout): - fmt.Println("TIMED OUT GREENENENENENENENENNENENENENENENENENEN") return ErrTimeout(r.messageID, r.timeout) } } diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 30053d1b81..288b0adb98 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -223,10 +223,11 @@ func (s *Service[ // bundle the block and sidecars and dispatch the response // blkData := *new(BeaconBlockBundleT) var blkData BeaconBlockBundleT + blkData = blkData.New(blk, sidecars) if err := s.dispatcher.Respond(asynctypes.NewMessage( req.Context(), messages.BuildBeaconBlockAndSidecars, - blkData.New(blk, sidecars), + blkData, )); err != nil { s.logger.Error("failed to respond", "err", err) } diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index f1df15e473..2402866158 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -42,6 +42,18 @@ func ProvideMessageServer(in MessageServerInput) *server.MessageServer { return ms } +// ProvideMessageRoutes provides all the message routes. +func ProvideMessageRoutes() []asynctypes.MessageRoute { + return []asynctypes.MessageRoute{ + RouteFactory(messages.BuildBeaconBlockAndSidecars), + RouteFactory(messages.VerifyBeaconBlock), + RouteFactory(messages.FinalizeBeaconBlock), + RouteFactory(messages.ProcessGenesisData), + RouteFactory(messages.VerifySidecars), + RouteFactory(messages.ProcessSidecars), + } +} + // RouteFactory creates a new route for the given message ID. func RouteFactory(mID string) asynctypes.MessageRoute { switch mID { @@ -74,18 +86,6 @@ func RouteFactory(mID string) asynctypes.MessageRoute { } } -// ProvideMessageRoutes provides all the message routes. -func ProvideMessageRoutes() []asynctypes.MessageRoute { - return []asynctypes.MessageRoute{ - RouteFactory(messages.BuildBeaconBlockAndSidecars), - RouteFactory(messages.VerifyBeaconBlock), - RouteFactory(messages.FinalizeBeaconBlock), - RouteFactory(messages.ProcessGenesisData), - RouteFactory(messages.VerifySidecars), - RouteFactory(messages.ProcessSidecars), - } -} - // MessageServerComponents returns all the depinject providers for the message // server. func MessageServerComponents() []any { diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index edc423b083..95866a37c1 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -48,24 +48,23 @@ func (h *ABCIMiddleware[ ) (transition.ValidatorUpdates, error) { var ( valUpdateResp *asynctypes.Message[transition.ValidatorUpdates] + err error ) data := new(GenesisT) - if err := json.Unmarshal(bz, data); err != nil { + if err = json.Unmarshal(bz, data); err != nil { h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } // request for validator updates - err := h.dispatcher.Request( + if err = h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, ), &valUpdateResp, - ) - if err != nil { + ); err != nil { return nil, err } - h.logger.Info("Genesis initialization completed", "validator_updates", valUpdateResp.Data()) return valUpdateResp.Data(), valUpdateResp.Error() } From 82d08cd0e25846785d1d12be691d8fe5e5d64857 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 11:20:46 -0400 Subject: [PATCH 24/75] lint --- mod/async/pkg/dispatcher/dispatcher.go | 20 +++++++---- mod/async/pkg/messaging/errors.go | 21 ++++++----- mod/async/pkg/messaging/publisher.go | 4 ++- mod/async/pkg/messaging/route.go | 8 ++--- mod/async/pkg/server/errors.go | 4 ++- mod/async/pkg/server/event.go | 4 ++- mod/async/pkg/server/msg.go | 10 ++++-- mod/beacon/block_store/service.go | 4 ++- mod/beacon/blockchain/execution_engine.go | 5 +-- mod/beacon/blockchain/process.go | 6 ++-- mod/beacon/blockchain/service.go | 35 ++++++++++++++----- mod/beacon/validator/service.go | 28 +++++++++------ mod/da/pkg/da/service.go | 27 ++++++++------ mod/node-core/pkg/components/events.go | 4 ++- mod/node-core/pkg/components/messages.go | 10 ++++-- .../pkg/components/service_registry.go | 2 -- mod/node-core/pkg/components/types.go | 15 +++++--- mod/primitives/pkg/messages/messages.go | 4 +-- mod/runtime/pkg/middleware/abci.go | 35 ++++++++++++------- mod/runtime/pkg/middleware/middleware.go | 8 ----- mod/runtime/pkg/middleware/types.go | 3 +- 21 files changed, 164 insertions(+), 93 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 61731d451b..14636935eb 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -27,9 +27,9 @@ import ( "github.com/berachain/beacon-kit/mod/log" ) -// Dispatcher faciliates asynchronous communication between components, typically -// services. It acts as an API facade to the underlying event and message -// servers. +// Dispatcher faciliates asynchronous communication between components, +// typically services. It acts as an API facade to the underlying event and +// message servers. type Dispatcher struct { eventServer EventServer msgServer MessageServer @@ -83,7 +83,9 @@ func (d *Dispatcher) Respond(resp types.MessageI) error { // RegisterPublisher registers the given publisher with the given eventID. // Any subsequent events with dispatched to this Dispatcher must be // consistent with the type expected by . -func (d *Dispatcher) RegisterPublisher(eventID types.MessageID, publisher types.Publisher) { +func (d *Dispatcher) RegisterPublisher( + eventID types.MessageID, publisher types.Publisher, +) { d.logger.Info("Publisher registered", "eventID", eventID) d.eventServer.RegisterPublisher(eventID, publisher) } @@ -99,7 +101,9 @@ func (d *Dispatcher) Subscribe(eventID types.MessageID, ch any) error { // RegisterMsgRecipient registers the given channel to the message with the // given . -func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch any) error { +func (d *Dispatcher) RegisterMsgReceiver( + messageID types.MessageID, ch any, +) error { d.logger.Info("Message receiver registered", "messageID", messageID) return d.msgServer.RegisterReceiver(messageID, ch) } @@ -107,7 +111,9 @@ func (d *Dispatcher) RegisterMsgReceiver(messageID types.MessageID, ch any) erro // RegisterRoute registers the given route with the given messageID. // Any subsequent messages with sent to this Dispatcher must be // consistent with the type expected by . -func (d *Dispatcher) RegisterRoute(messageID types.MessageID, route types.MessageRoute) { +func (d *Dispatcher) RegisterRoute( + messageID types.MessageID, route types.MessageRoute, +) error { d.logger.Info("Route registered", "messageID", messageID) - d.msgServer.RegisterRoute(messageID, route) + return d.msgServer.RegisterRoute(messageID, route) } diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index 6ca23beaa8..629b7cbbd1 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -27,28 +27,31 @@ import ( "github.com/berachain/beacon-kit/mod/errors" ) -// ErrTimeout is the error returned when a dispatch operation timed out. +// errTimeout is the error returned when a dispatch operation timed out. // -//nolint:lll // long errors +//nolint:gochecknoglobals // errors var ( - ErrTimeout = func(messageID types.MessageID, timeout time.Duration) error { + errTimeout = func(messageID types.MessageID, timeout time.Duration) error { return errors.Newf("message %s timed out after %s", messageID, timeout) } - ErrRouteAlreadySet = errors.New("route already set") + errRouteAlreadySet = errors.New("route already set") - ErrRegisteringNilChannel = func(messageID types.MessageID) error { - return errors.Newf("cannot register nil channel for route: %s", messageID) + errRegisteringNilChannel = func(messageID types.MessageID) error { + return errors.Newf("cannot register nil channel for route: %s", + messageID) } - ErrReceiverNotReady = func(messageID types.MessageID) error { + errReceiverNotReady = func(messageID types.MessageID) error { return errors.Newf( - "receiver channel is either full, closed, or not listening. Route: %s", + "receiver channel is full, closed, or not listening. Route: %s", messageID, ) } - errIncompatibleAssignee = func(assigner interface{}, assignee interface{}) error { + errIncompatibleAssignee = func( + assigner interface{}, assignee interface{}, + ) error { return errors.Newf( "incompatible assignee, expected: %T, received: %T", assigner, diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index 6b6dc31927..da71d5e90c 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -69,7 +69,9 @@ func (p *Publisher[T]) start(ctx context.Context) { case <-ctx.Done(): // close all leftover clients and break the publisher loop for client := range p.clients { - p.Unsubscribe(client) + if err := p.Unsubscribe(client); err != nil { + panic(err) + } } return case msg := <-p.msgs: diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 3195882e5c..220af69373 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -57,9 +57,9 @@ func (r *Route[ReqT, RespT]) MessageID() types.MessageID { // RegisterReceiver sets the recipient for the route. func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { if r.recipient != nil { - return ErrRouteAlreadySet + return errRouteAlreadySet } else if ch == nil { - return ErrRegisteringNilChannel(r.messageID) + return errRegisteringNilChannel(r.messageID) } typedCh, err := ensureType[chan ReqT](ch) if err != nil { @@ -81,7 +81,7 @@ func (r *Route[ReqT, RespT]) SendRequest(req types.MessageI) error { return nil default: // Channel is full or closed - return ErrReceiverNotReady(r.messageID) + return errReceiverNotReady(r.messageID) } } @@ -102,6 +102,6 @@ func (r *Route[ReqT, RespT]) AwaitResponse(respPtr any) error { case resp := <-r.responseCh: return assign(resp, respPtr) case <-time.After(r.timeout): - return ErrTimeout(r.messageID, r.timeout) + return errTimeout(r.messageID, r.timeout) } } diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index baf1929377..a1fb778ce9 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -25,10 +25,12 @@ import ( "github.com/berachain/beacon-kit/mod/errors" ) +//nolint:gochecknoglobals // errors var ( ErrFeedNotFound = errors.New("feed not found") ErrRouteNotFound = errors.New("route not found") ErrRouteAlreadyRegistered = func(messageID types.MessageID) error { - return errors.Newf("route already registered for messageID: %s", messageID) + return errors.Newf("route already registered for messageID: %s", + messageID) } ) diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 8375181f97..a0cc3dcf91 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -66,7 +66,9 @@ func (es *EventServer) Start(ctx context.Context) { // RegisterPublisher registers the given publisher with the given eventID. // Any subsequent events with dispatched to this EventServer must be // consistent with the type expected by . -func (es *EventServer) RegisterPublisher(eventID types.MessageID, publisher types.Publisher) { +func (es *EventServer) RegisterPublisher( + eventID types.MessageID, publisher types.Publisher, +) { es.publishers[eventID] = publisher } diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 0ae4c58b6f..8a4a3bb901 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -39,7 +39,7 @@ func NewMessageServer() *MessageServer { // Request sends a message to the server and awaits for a response. // The response is written to the provided response pointer. func (ms *MessageServer) Request(req types.MessageI, respPtr any) error { - // send message to request channel and await a response in the response channel + // send request and await response route, ok := ms.routes[req.ID()] if !ok { return ErrRouteNotFound @@ -63,7 +63,9 @@ func (ms *MessageServer) Respond(resp types.MessageI) error { // RegisterRoute registers the route with the given messageID. // Any subsequent messages with sent to this MessageServer must be // consistent with the type expected by . -func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route types.MessageRoute) error { +func (ms *MessageServer) RegisterRoute( + messageID types.MessageID, route types.MessageRoute, +) error { if ms.routes[messageID] != nil { return ErrRouteAlreadyRegistered(messageID) } @@ -74,7 +76,9 @@ func (ms *MessageServer) RegisterRoute(messageID types.MessageID, route types.Me // SetRecipient sets the recipient for the route with the given messageID. // Errors if the route with the given messageID is not found or the route // already has a registered recipient. -func (ms *MessageServer) RegisterReceiver(messageID types.MessageID, ch any) error { +func (ms *MessageServer) RegisterReceiver( + messageID types.MessageID, ch any, +) error { route, ok := ms.routes[messageID] if !ok { return ErrRouteNotFound diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index ea4647d3ab..1395ae864c 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -74,7 +74,9 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { // subscribe a channel to the finalized block events. var finalizedBlkCh = make(chan *asynctypes.Event[BeaconBlockT]) - if err := s.dispatcher.Subscribe(messages.BeaconBlockFinalizedEvent, finalizedBlkCh); err != nil { + if err := s.dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err } diff --git a/mod/beacon/blockchain/execution_engine.go b/mod/beacon/blockchain/execution_engine.go index 03f49567cc..56e7d34a5d 100644 --- a/mod/beacon/blockchain/execution_engine.go +++ b/mod/beacon/blockchain/execution_engine.go @@ -53,6 +53,8 @@ func (s *Service[ // sendNextFCUWithAttributes sends a forkchoice update to the execution // client with attributes. +// +//nolint:lll // it's fine func (s *Service[ _, BeaconBlockT, _, _, BeaconStateT, _, _, ExecutionPayloadHeaderT, _, _, _, @@ -83,8 +85,7 @@ func (s *Service[ ); err != nil { s.logger.Error( "failed to send forkchoice update with attributes in non-optimistic payload", - "error", - err, + "error", err, ) } } diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 380dcb3535..c0e96d8c90 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -83,11 +83,13 @@ func (s *Service[ // TODO: this is hood as fuck. // We won't send a fcu if the block is bad, should be addressed // via ticker later. - s.dispatcher.Publish( + if err = s.dispatcher.Publish( asynctypes.NewEvent( ctx, messages.BeaconBlockFinalizedEvent, blk, ), - ) + ); err != nil { + return nil, err + } go s.sendPostBlockFCU(ctx, st, blk) diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index c410ab9cd0..31f00cd4e7 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -65,7 +65,8 @@ type Service[ chainSpec common.ChainSpec // dispatcher is the dispatcher for the service. dispatcher *dispatcher.Dispatcher - // executionEngine is the execution engine responsible for processing execution payloads. + // executionEngine is the execution engine responsible for processing + // execution payloads. executionEngine ExecutionEngine[PayloadAttributesT] // localBuilder is a local builder for constructing new beacon states. localBuilder LocalBuilder[BeaconStateT] @@ -221,19 +222,23 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, _, ]) handleProcessGenesisDataRequest(msg *asynctypes.Message[GenesisT]) { + var ( + valUpdates transition.ValidatorUpdates + err error + ) if msg.Error() != nil { s.logger.Error("Error processing genesis data", "error", msg.Error()) return } // Process the genesis data. - valUpdates, err := s.ProcessGenesisData(msg.Context(), msg.Data()) + valUpdates, err = s.ProcessGenesisData(msg.Context(), msg.Data()) if err != nil { s.logger.Error("Failed to process genesis data", "error", err) } // dispatch a response containing the validator updates - if err := s.dispatcher.Respond( + if err = s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.ProcessGenesisData, @@ -260,14 +265,19 @@ func (s *Service[ } // dispatch a response with the error result from VerifyIncomingBlock - s.dispatcher.Respond( + if err := s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.VerifyBeaconBlock, msg.Data(), s.VerifyIncomingBlock(msg.Context(), msg.Data()), ), - ) + ); err != nil { + s.logger.Error( + "Failed to dispatch response in verify beacon block", + "error", err, + ) + } } func (s *Service[ @@ -275,6 +285,10 @@ func (s *Service[ ]) handleFinalizeBeaconBlockRequest( msg *asynctypes.Message[BeaconBlockT], ) { + var ( + valUpdates transition.ValidatorUpdates + err error + ) // If there's an error in the event, log it and return if msg.Error() != nil { s.logger.Error("Error verifying beacon block", "error", msg.Error()) @@ -282,18 +296,23 @@ func (s *Service[ } // process the verified block and get the validator updates - valUpdates, err := s.ProcessBeaconBlock(msg.Context(), msg.Data()) + valUpdates, err = s.ProcessBeaconBlock(msg.Context(), msg.Data()) if err != nil { s.logger.Error("Failed to process verified beacon block", "error", err) } // dispatch a response with the validator updates - s.dispatcher.Respond( + if err = s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.FinalizeBeaconBlock, valUpdates, err, ), - ) + ); err != nil { + s.logger.Error( + "Failed to dispatch response in finalize beacon block", + "error", err, + ) + } } diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 288b0adb98..a5c828f4c7 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -140,14 +140,16 @@ func NewService[ ts TelemetrySink, dispatcher *dispatcher.Dispatcher, ) *Service[ - AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, BeaconStateT, - BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, - ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, SlotDataT, + AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, + BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, + ExecutionPayloadT, ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, + SlotDataT, ] { return &Service[ - AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, BeaconStateT, - BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, - ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, SlotDataT, + AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, + BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, + ExecutionPayloadT, ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, + SlotDataT, ]{ cfg: cfg, logger: logger, @@ -210,10 +212,17 @@ func (s *Service[ // handleBuildBlockBundleRequest builds a block and sidecars for the requested // slot data and dispatches a response containing the built block and sidecars. func (s *Service[ - _, _, BeaconBlockBundleT, _, _, _, _, _, _, _, _, _, _, SlotDataT, + _, BeaconBlockT, BeaconBlockBundleT, _, _, BlobSidecarsT, _, _, _, _, _, _, + _, SlotDataT, ]) handleBuildBlockBundleRequest(req *asynctypes.Message[SlotDataT]) { + var ( + blk BeaconBlockT + sidecars BlobSidecarsT + blkData BeaconBlockBundleT + err error + ) // build the block and sidecars for the requested slot data - blk, sidecars, err := s.buildBlockAndSidecars( + blk, sidecars, err = s.buildBlockAndSidecars( req.Context(), req.Data(), ) if err != nil { @@ -222,9 +231,8 @@ func (s *Service[ // bundle the block and sidecars and dispatch the response // blkData := *new(BeaconBlockBundleT) - var blkData BeaconBlockBundleT blkData = blkData.New(blk, sidecars) - if err := s.dispatcher.Respond(asynctypes.NewMessage( + if err = s.dispatcher.Respond(asynctypes.NewMessage( req.Context(), messages.BuildBeaconBlockAndSidecars, blkData, diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index e16009c9f3..208ef01c2e 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -33,7 +33,7 @@ type Service[ AvailabilityStoreT AvailabilityStore[BeaconBlockBodyT, BlobSidecarsT], BeaconBlockBodyT any, BlobSidecarsT BlobSidecar, - //nolint:lll // formatter. + ExecutionPayloadT any, ] struct { avs AvailabilityStoreT @@ -52,7 +52,7 @@ func NewService[ ], BeaconBlockBodyT any, BlobSidecarsT BlobSidecar, - //nolint:lll // formatter. + ExecutionPayloadT any, ]( avs AvailabilityStoreT, @@ -85,9 +85,10 @@ func (s *Service[_, _, _, _]) Name() string { // Start registers this service as the recipient of ProcessSidecars and // VerifySidecars messages, and begins listening for these requests. func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { + var err error // register as recipient of ProcessSidecars messages. sidecarsProcessRequests := make(chan *asynctypes.Message[BlobSidecarsT]) - if err := s.dispatcher.RegisterMsgReceiver( + if err = s.dispatcher.RegisterMsgReceiver( messages.ProcessSidecars, sidecarsProcessRequests, ); err != nil { return err @@ -95,7 +96,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { // register as recipient of VerifySidecars messages. sidecarVerifyRequests := make(chan *asynctypes.Message[BlobSidecarsT]) - if err := s.dispatcher.RegisterMsgReceiver( + if err = s.dispatcher.RegisterMsgReceiver( messages.VerifySidecars, sidecarVerifyRequests, ); err != nil { return err @@ -135,7 +136,8 @@ func (s *Service[_, _, BlobSidecarsT, _]) start( func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( msg *asynctypes.Message[BlobSidecarsT], ) { - err := s.processSidecars(msg.Context(), msg.Data()) + var err error + err = s.processSidecars(msg.Context(), msg.Data()) if err != nil { s.logger.Error( "Failed to process blob sidecars", @@ -145,14 +147,16 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( } // dispatch a response to acknowledge the request. - s.dispatcher.Respond( + if err = s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.ProcessSidecars, msg.Data(), nil, ), - ) + ); err != nil { + s.logger.Error("failed to respond", "err", err) + } } // handleSidecarsVerifyRequest handles the SidecarsVerifyRequest event. @@ -160,8 +164,9 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( msg *asynctypes.Message[BlobSidecarsT], ) { + var err error // verify the sidecars. - err := s.verifySidecars(msg.Data()) + err = s.verifySidecars(msg.Data()) if err != nil { s.logger.Error( "Failed to receive blob sidecars", @@ -171,14 +176,16 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( } // dispatch a response to acknowledge the request. - s.dispatcher.Respond( + if err = s.dispatcher.Respond( asynctypes.NewMessage( msg.Context(), messages.VerifySidecars, msg.Data(), nil, ), - ) + ); err != nil { + s.logger.Error("failed to respond", "err", err) + } } /* -------------------------------------------------------------------------- */ diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index d92fede25a..4f38ed9b19 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -46,7 +46,9 @@ func ProvideEventServer(in EventServerInput) *EventServer { // ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block // finalized events. func ProvideBeaconBlockFinalizedPublisher() *BeaconBlockFinalizedPublisher { - return messaging.NewPublisher[*FinalizedBlockEvent](messages.BeaconBlockFinalizedEvent) + return messaging.NewPublisher[*FinalizedBlockEvent]( + messages.BeaconBlockFinalizedEvent, + ) } // EventServerComponents returns all the depinject providers for the event diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index 2402866158..a70309be35 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -34,12 +34,16 @@ type MessageServerInput struct { } // ProvideMessageServer provides a message server. -func ProvideMessageServer(in MessageServerInput) *server.MessageServer { +func ProvideMessageServer(in MessageServerInput) ( + *server.MessageServer, error, +) { ms := server.NewMessageServer() for _, route := range in.Routes { - ms.RegisterRoute(route.MessageID(), route) + if err := ms.RegisterRoute(route.MessageID(), route); err != nil { + return nil, err + } } - return ms + return ms, nil } // ProvideMessageRoutes provides all the message routes. diff --git a/mod/node-core/pkg/components/service_registry.go b/mod/node-core/pkg/components/service_registry.go index b301b1d2e7..797a815f04 100644 --- a/mod/node-core/pkg/components/service_registry.go +++ b/mod/node-core/pkg/components/service_registry.go @@ -30,7 +30,6 @@ import ( // ServiceRegistryInput is the input for the service registry provider. type ServiceRegistryInput struct { depinject.In - ABCIService *ABCIMiddleware BlockStoreService *BlockStoreService ChainService *ChainService DAService *DAService @@ -57,7 +56,6 @@ func ProvideServiceRegistry( service.WithService(in.ChainService), service.WithService(in.DAService), service.WithService(in.DepositService), - service.WithService(in.ABCIService), service.WithService(in.NodeAPIServer), service.WithService(in.ReportingService), service.WithService(in.DBManager), diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index bdf0cce42c..caa452a1c0 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -420,13 +420,13 @@ type ( /* Messages */ /* -------------------------------------------------------------------------- */ -// events +// events. type ( // FinalizedBlockEvent is a type alias for the block event. FinalizedBlockEvent = asynctypes.Event[*BeaconBlock] ) -// messages +// messages. type ( // BlockMessage is a type alias for the block message. BlockMessage = asynctypes.Message[*BeaconBlock] @@ -463,8 +463,11 @@ type ( /* -------------------------------------------------------------------------- */ type ( - // BuildBlockAndSidecarsRoute is a type alias for the build block and sidecars route. - BuildBlockAndSidecarsRoute = messaging.Route[*SlotMessage, *BlockBundleMessage] + // BuildBlockAndSidecarsRoute is a type alias for the build block and + // sidecars route. + BuildBlockAndSidecarsRoute = messaging.Route[ + *SlotMessage, *BlockBundleMessage, + ] // VerifyBlockRoute is a type alias for the verify block route. VerifyBlockRoute = messaging.Route[*BlockMessage, *BlockMessage] @@ -473,7 +476,9 @@ type ( FinalizeBlockRoute = messaging.Route[*BlockMessage, *ValidatorUpdateMessage] // ProcessGenesisDataRoute is a type alias for the process genesis data route. - ProcessGenesisDataRoute = messaging.Route[*GenesisMessage, *ValidatorUpdateMessage] + ProcessGenesisDataRoute = messaging.Route[ + *GenesisMessage, *ValidatorUpdateMessage, + ] // ProcessSidecarsRoute is a type alias for the process blob sidecars route. ProcessSidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] diff --git a/mod/primitives/pkg/messages/messages.go b/mod/primitives/pkg/messages/messages.go index 2a98ed0b4b..f617ccf987 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/primitives/pkg/messages/messages.go @@ -20,7 +20,7 @@ package messages -// messages +// messages. const ( BuildBeaconBlockAndSidecars = "build-beacon-block-and-sidecars" VerifyBeaconBlock = "verify-beacon-block" @@ -31,7 +31,7 @@ const ( ReceiveSidecars = "receive-sidecars" ) -// events +// events. const ( BeaconBlockFinalizedEvent = "beacon-block-finalized" ) diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 95866a37c1..a717c57c06 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -111,7 +111,9 @@ func (h *ABCIMiddleware[ return nil, nil, bbbResp.Error() } // gossip beacon block - bbBz, bbErr := h.beaconBlockGossiper.Publish(ctx, bbbResp.Data().GetBeaconBlock()) + bbBz, bbErr := h.beaconBlockGossiper.Publish( + ctx, bbbResp.Data().GetBeaconBlock(), + ) if bbErr != nil { return nil, nil, bbErr } @@ -156,11 +158,13 @@ func (h *ABCIMiddleware[ } // verify the beacon block - h.dispatcher.Request( + if err = h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.VerifyBeaconBlock, blk, ), &beaconBlockResp, - ) + ); err != nil { + return h.createProcessProposalResponse(errors.WrapNonFatal(err)) + } if beaconBlockResp.Error() != nil { return h.createProcessProposalResponse(beaconBlockResp.Error()) @@ -172,11 +176,13 @@ func (h *ABCIMiddleware[ } // verify the blob sidecars - h.dispatcher.Request( + if err = h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.VerifySidecars, sidecars, ), &sidecarsResp, - ) + ); err != nil { + return h.createProcessProposalResponse(errors.WrapNonFatal(err)) + } if sidecarsResp.Error() != nil { return h.createProcessProposalResponse(sidecarsResp.Error()) @@ -228,8 +234,11 @@ func (h *ABCIMiddleware[ var ( sidecarsResp *asynctypes.Message[BlobSidecarsT] valUpdatesResp *asynctypes.Message[transition.ValidatorUpdates] + blk BeaconBlockT + blobs BlobSidecarsT + err error ) - blk, blobs, err := encoding. + blk, blobs, err = encoding. ExtractBlobsAndBlockFromRequest[BeaconBlockT, BlobSidecarsT]( h.req, BeaconBlockTxIndex, @@ -243,21 +252,23 @@ func (h *ABCIMiddleware[ return nil, nil } - // verify the blob sidecars - h.dispatcher.Request( + // process the blob sidecars. + if err = h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.ProcessSidecars, blobs, ), &sidecarsResp, - ) - if sidecarsResp.Error() != nil { + ); err != nil { return nil, sidecarsResp.Error() } - h.dispatcher.Request( + // finalize the beacon block. + if err = h.dispatcher.Request( asynctypes.NewMessage( ctx, messages.FinalizeBeaconBlock, blk, ), &valUpdatesResp, - ) + ); err != nil { + return nil, valUpdatesResp.Error() + } return valUpdatesResp.Data(), valUpdatesResp.Error() } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 7c8fab794a..95a7300665 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -21,7 +21,6 @@ package middleware import ( - "context" "encoding/json" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" @@ -125,10 +124,3 @@ func (am *ABCIMiddleware[ ]) Name() string { return "abci-middleware" } - -// Start the middleware. -func (am *ABCIMiddleware[ - _, _, _, _, _, _, _, _, -]) Start(ctx context.Context) error { - return nil -} diff --git a/mod/runtime/pkg/middleware/types.go b/mod/runtime/pkg/middleware/types.go index 01e0674d35..c8c2b080f7 100644 --- a/mod/runtime/pkg/middleware/types.go +++ b/mod/runtime/pkg/middleware/types.go @@ -39,7 +39,8 @@ type BeaconBlock[SelfT any] interface { NewFromSSZ([]byte, uint32) (SelfT, error) } -// BeaconBlockBundle +// BeaconBlockBundle is a bundle of a beacon block and its corresponding blob +// sidecars. type BeaconBlockBundle[BeaconBlockT any, BlobSidecarsT any] interface { GetBeaconBlock() BeaconBlockT GetSidecars() BlobSidecarsT From 1792026c892da8984edd6a3b865679ec0eb10764 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 11:24:14 -0400 Subject: [PATCH 25/75] comments --- mod/async/pkg/server/errors.go | 2 +- mod/async/pkg/server/event.go | 16 ++++++++-------- mod/async/pkg/server/msg.go | 3 +++ mod/async/pkg/types/message.go | 2 ++ mod/async/pkg/types/notifiers.go | 10 +++++----- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index a1fb778ce9..2c429df8bf 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -27,7 +27,7 @@ import ( //nolint:gochecknoglobals // errors var ( - ErrFeedNotFound = errors.New("feed not found") + ErrPublisherNotFound = errors.New("feed not found") ErrRouteNotFound = errors.New("route not found") ErrRouteAlreadyRegistered = func(messageID types.MessageID) error { return errors.Newf("route already registered for messageID: %s", diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index a0cc3dcf91..764455655a 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -40,26 +40,26 @@ func NewEventServer() *EventServer { } } -// Dispatch dispatches the given event to the feed with the given eventID. +// Dispatch dispatches the given event to the publisher with the given eventID. func (es *EventServer) Publish(event types.MessageI) error { return es.publishers[event.ID()].Publish(event) } -// Subscribe subscribes the given channel to the feed with the given eventID. +// Subscribe subscribes the given channel to the publisher with the given eventID. // It will error if the channel type does not match the event type corresponding -// feed. +// publisher. func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { - feed, ok := es.publishers[eventID] + publisher, ok := es.publishers[eventID] if !ok { - return ErrFeedNotFound + return ErrPublisherNotFound } - return feed.Subscribe(ch) + return publisher.Subscribe(ch) } // Start starts the event server. func (es *EventServer) Start(ctx context.Context) { - for _, feed := range es.publishers { - go feed.Start(ctx) + for _, publisher := range es.publishers { + go publisher.Start(ctx) } } diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 8a4a3bb901..9ff50b29bc 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -25,11 +25,13 @@ import ( "github.com/berachain/beacon-kit/mod/log" ) +// MessageServer is a server for sending and receiving messages. type MessageServer struct { routes map[types.MessageID]types.MessageRoute logger log.Logger[any] } +// NewMessageServer creates a new message server. func NewMessageServer() *MessageServer { return &MessageServer{ routes: make(map[types.MessageID]types.MessageRoute), @@ -86,6 +88,7 @@ func (ms *MessageServer) RegisterReceiver( return route.RegisterReceiver(ch) } +// SetLogger sets the logger for the message server. func (ms *MessageServer) SetLogger(logger log.Logger[any]) { ms.logger = logger } diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index 5fd85d97b9..8f37d70776 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -25,6 +25,8 @@ import ( "errors" ) +// MessageI defines the minimal interface that the dispatcher expects from a +// message. type MessageI interface { ID() MessageID Context() context.Context diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index c3f8850847..0ecd4f9dad 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -22,15 +22,15 @@ package types import "context" -// publisher is the interface that supports basic event feed operations. +// publisher is the interface that supports basic event publisher operations. type Publisher interface { - // Start starts the event feed. + // Start starts the event publisher. Start(ctx context.Context) - // Publish publishes the given event to the event feed. + // Publish publishes the given event to the event publisher. Publish(event MessageI) error - // Subscribe subscribes the given channel to the event feed. + // Subscribe subscribes the given channel to the event publisher. Subscribe(ch any) error - // Unsubscribe unsubscribes the given channel from the event feed. + // Unsubscribe unsubscribes the given channel from the event publisher. Unsubscribe(ch any) error } From 9b4a337ed2fcc6fb3d64958f4f42cf1adf8124ef Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 12:08:25 -0400 Subject: [PATCH 26/75] pruner --- mod/async/pkg/dispatcher/dispatcher.go | 12 ++--- mod/async/pkg/server/event.go | 6 +-- mod/beacon/block_store/service.go | 24 +++++----- mod/beacon/block_store/types.go | 4 +- mod/beacon/blockchain/process.go | 2 +- mod/beacon/blockchain/service.go | 37 ++++++++------- mod/beacon/validator/service.go | 14 +++--- mod/da/pkg/da/service.go | 34 +++++++------- .../pkg/components/availability_store.go | 12 +---- mod/node-core/pkg/components/block_store.go | 12 +---- mod/node-core/pkg/components/deposit_store.go | 12 +---- mod/runtime/pkg/middleware/abci.go | 12 ++--- mod/storage/pkg/manager/manager_test.go | 27 +++++++++-- mod/storage/pkg/manager/types.go | 1 + .../pkg/pruner/mocks/block_event.mock.go | 45 +++++++++++++++++++ mod/storage/pkg/pruner/pruner.go | 38 +++++++++++----- mod/storage/pkg/pruner/pruner_test.go | 45 ++++++++++++++++--- mod/storage/pkg/pruner/types.go | 1 + 18 files changed, 213 insertions(+), 125 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 14636935eb..eeaf980034 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -57,24 +57,24 @@ func (d *Dispatcher) Start(ctx context.Context) error { return nil } -// Publish dispatches the given event to the event server. +// PublishEvent dispatches the given event to the event server. // It will error if the type is inconsistent with the publisher // registered for the given eventID. -func (d *Dispatcher) Publish(event types.MessageI) error { +func (d *Dispatcher) PublishEvent(event types.MessageI) error { return d.eventServer.Publish(event) } -// Request dispatches the given request to the message server. +// SendRequest dispatches the given request to the message server. // It will error if the and types are inconsistent with the // route registered for the given messageID. -func (d *Dispatcher) Request(req types.MessageI, resp any) error { +func (d *Dispatcher) SendRequest(req types.MessageI, resp any) error { return d.msgServer.Request(req, resp) } -// Respond dispatches the given response to the message server. +// SendResponse dispatches the given response to the message server. // It will error if the type is inconsistent with the route registered // for the given messageID. -func (d *Dispatcher) Respond(resp types.MessageI) error { +func (d *Dispatcher) SendResponse(resp types.MessageI) error { return d.msgServer.Respond(resp) } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 764455655a..770093d186 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -45,9 +45,9 @@ func (es *EventServer) Publish(event types.MessageI) error { return es.publishers[event.ID()].Publish(event) } -// Subscribe subscribes the given channel to the publisher with the given eventID. -// It will error if the channel type does not match the event type corresponding -// publisher. +// Subscribe subscribes the given channel to the publisher with the given +// eventID. It will error if the channel type does not match the event type +// corresponding to the publisher. func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { publisher, ok := es.publishers[eventID] if !ok { diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 1395ae864c..c0e551e3e7 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -40,10 +40,11 @@ func NewService[ store BlockStoreT, ) *Service[BeaconBlockT, BlockStoreT] { return &Service[BeaconBlockT, BlockStoreT]{ - config: config, - logger: logger, - dispatcher: dispatcher, - store: store, + config: config, + logger: logger, + dispatcher: dispatcher, + store: store, + finalizeBlkReqs: make(chan *asynctypes.Message[BeaconBlockT]), } } @@ -55,9 +56,10 @@ type Service[ // config is the configuration for the block service. config Config // logger is used for logging information and errors. - logger log.Logger[any] - dispatcher *dispatcher.Dispatcher - store BlockStoreT + logger log.Logger[any] + dispatcher *dispatcher.Dispatcher + store BlockStoreT + finalizeBlkReqs chan *asynctypes.Message[BeaconBlockT] } // Name returns the name of the service. @@ -73,27 +75,25 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { } // subscribe a channel to the finalized block events. - var finalizedBlkCh = make(chan *asynctypes.Event[BeaconBlockT]) if err := s.dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + messages.BeaconBlockFinalizedEvent, s.finalizeBlkReqs, ); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err } - go s.listenAndStore(ctx, finalizedBlkCh) + go s.listenAndStore(ctx) return nil } // listenAndStore listens for blocks and stores them in the KVStore. func (s *Service[BeaconBlockT, _]) listenAndStore( ctx context.Context, - finalizeBlockCh <-chan *asynctypes.Event[BeaconBlockT], ) { for { select { case <-ctx.Done(): return - case msg := <-finalizeBlockCh: + case msg := <-s.finalizeBlkReqs: slot := msg.Data().GetSlot() if err := s.store.Set(slot, msg.Data()); err != nil { s.logger.Error( diff --git a/mod/beacon/block_store/types.go b/mod/beacon/block_store/types.go index 5433735256..00a21469d8 100644 --- a/mod/beacon/block_store/types.go +++ b/mod/beacon/block_store/types.go @@ -41,9 +41,9 @@ type BlockStore[BeaconBlockT BeaconBlock] interface { // Event is an interface for block events. type Event[BeaconBlockT BeaconBlock] interface { - // Type returns the type of the event. + // ID returns the id of the event. ID() asynctypes.EventID - // Is returns true if the event is of the given type. + // Is returns true if the event is of the given id. Is(asynctypes.EventID) bool // Data returns the data of the event. Data() BeaconBlockT diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index c0e96d8c90..1ad7f11609 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -83,7 +83,7 @@ func (s *Service[ // TODO: this is hood as fuck. // We won't send a fcu if the block is bad, should be addressed // via ticker later. - if err = s.dispatcher.Publish( + if err = s.dispatcher.PublishEvent( asynctypes.NewEvent( ctx, messages.BeaconBlockFinalizedEvent, blk, ), diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 31f00cd4e7..58d6fcac0e 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -85,6 +85,10 @@ type Service[ optimisticPayloadBuilds bool // forceStartupSyncOnce is used to force a sync of the startup head. forceStartupSyncOnce *sync.Once + + finalizeBlkReqs chan *asynctypes.Message[BeaconBlockT] + verifyBlkReqs chan *asynctypes.Message[BeaconBlockT] + processGenReqs chan *asynctypes.Message[GenesisT] } // NewService creates a new validator service. @@ -147,6 +151,9 @@ func NewService[ metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), + finalizeBlkReqs: make(chan *asynctypes.Message[BeaconBlockT]), + verifyBlkReqs: make(chan *asynctypes.Message[BeaconBlockT]), + processGenReqs: make(chan *asynctypes.Message[GenesisT]), } } @@ -164,50 +171,42 @@ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) Start(ctx context.Context) error { // register a channel as the receiver for FinalizeBeaconBlock: - finalizeBlkReqs := make(chan *asynctypes.Message[BeaconBlockT]) if err := s.dispatcher.RegisterMsgReceiver( - messages.FinalizeBeaconBlock, finalizeBlkReqs, + messages.FinalizeBeaconBlock, s.finalizeBlkReqs, ); err != nil { return err } // register a channel as the receiver for VerifyBeaconBlock: - verifyBlkReqs := make(chan *asynctypes.Message[BeaconBlockT]) if err := s.dispatcher.RegisterMsgReceiver( - messages.VerifyBeaconBlock, verifyBlkReqs, + messages.VerifyBeaconBlock, s.verifyBlkReqs, ); err != nil { return err } // register a channel as the receiver for ProcessGenesisData: - processGenReqs := make(chan *asynctypes.Message[GenesisT]) if err := s.dispatcher.RegisterMsgReceiver( - messages.ProcessGenesisData, processGenReqs, + messages.ProcessGenesisData, s.processGenReqs, ); err != nil { return err } // start a goroutine to listen for requests and handle accordingly - go s.start(ctx, finalizeBlkReqs, verifyBlkReqs, processGenReqs) + go s.start(ctx) return nil } // start starts the service. func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, -]) start( - ctx context.Context, - finalizeBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT], - verifyBeaconBlockRequests chan *asynctypes.Message[BeaconBlockT], - processGenDataRequests chan *asynctypes.Message[GenesisT], -) { +]) start(ctx context.Context) { for { select { case <-ctx.Done(): return - case msg := <-finalizeBeaconBlockRequests: + case msg := <-s.finalizeBlkReqs: s.handleFinalizeBeaconBlockRequest(msg) - case msg := <-verifyBeaconBlockRequests: + case msg := <-s.verifyBlkReqs: s.handleVerifyBeaconBlockRequest(msg) - case msg := <-processGenDataRequests: + case msg := <-s.processGenReqs: s.handleProcessGenesisDataRequest(msg) } } @@ -238,7 +237,7 @@ func (s *Service[ } // dispatch a response containing the validator updates - if err = s.dispatcher.Respond( + if err = s.dispatcher.SendResponse( asynctypes.NewMessage( msg.Context(), messages.ProcessGenesisData, @@ -265,7 +264,7 @@ func (s *Service[ } // dispatch a response with the error result from VerifyIncomingBlock - if err := s.dispatcher.Respond( + if err := s.dispatcher.SendResponse( asynctypes.NewMessage( msg.Context(), messages.VerifyBeaconBlock, @@ -302,7 +301,7 @@ func (s *Service[ } // dispatch a response with the validator updates - if err = s.dispatcher.Respond( + if err = s.dispatcher.SendResponse( asynctypes.NewMessage( msg.Context(), messages.FinalizeBeaconBlock, diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index a5c828f4c7..3b768c5fc8 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -56,6 +56,7 @@ type Service[ SlashingInfoT any, SlotDataT SlotData[AttestationDataT, SlashingInfoT], ] struct { + buildBlkBundleReqs chan *asynctypes.Message[SlotDataT] // cfg is the validator config. cfg *Config // logger is a logger. @@ -153,6 +154,7 @@ func NewService[ ]{ cfg: cfg, logger: logger, + buildBlkBundleReqs: make(chan *asynctypes.Message[SlotDataT]), bsb: bsb, chainSpec: chainSpec, signer: signer, @@ -179,16 +181,15 @@ func (s *Service[ ]) Start( ctx context.Context, ) error { - // register a receiver channel for build block requests - buildBlkBundleReqs := make(chan *asynctypes.Message[SlotDataT]) + // register the receiver channel for build block requests if err := s.dispatcher.RegisterMsgReceiver( - messages.BuildBeaconBlockAndSidecars, buildBlkBundleReqs, + messages.BuildBeaconBlockAndSidecars, s.buildBlkBundleReqs, ); err != nil { return err } // start a goroutine to listen for requests and handle accordingly - go s.start(ctx, buildBlkBundleReqs) + go s.start(ctx) return nil } @@ -197,13 +198,12 @@ func (s *Service[ _, _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) start( ctx context.Context, - buildBlkBundleReqs chan *asynctypes.Message[SlotDataT], ) { for { select { case <-ctx.Done(): return - case req := <-buildBlkBundleReqs: + case req := <-s.buildBlkBundleReqs: s.handleBuildBlockBundleRequest(req) } } @@ -232,7 +232,7 @@ func (s *Service[ // bundle the block and sidecars and dispatch the response // blkData := *new(BeaconBlockBundleT) blkData = blkData.New(blk, sidecars) - if err = s.dispatcher.Respond(asynctypes.NewMessage( + if err = s.dispatcher.SendResponse(asynctypes.NewMessage( req.Context(), messages.BuildBeaconBlockAndSidecars, blkData, diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 208ef01c2e..da6153000b 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -41,8 +41,10 @@ type Service[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ] - dispatcher *dispatcher.Dispatcher - logger log.Logger[any] + dispatcher *dispatcher.Dispatcher + logger log.Logger[any] + processSidecarRequests chan *asynctypes.Message[BlobSidecarsT] + verifySidecarRequests chan *asynctypes.Message[BlobSidecarsT] } // NewService returns a new DA service. @@ -70,10 +72,12 @@ func NewService[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ]{ - avs: avs, - bp: bp, - dispatcher: dispatcher, - logger: logger, + avs: avs, + bp: bp, + dispatcher: dispatcher, + logger: logger, + processSidecarRequests: make(chan *asynctypes.Message[BlobSidecarsT]), + verifySidecarRequests: make(chan *asynctypes.Message[BlobSidecarsT]), } } @@ -87,23 +91,21 @@ func (s *Service[_, _, _, _]) Name() string { func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { var err error // register as recipient of ProcessSidecars messages. - sidecarsProcessRequests := make(chan *asynctypes.Message[BlobSidecarsT]) if err = s.dispatcher.RegisterMsgReceiver( - messages.ProcessSidecars, sidecarsProcessRequests, + messages.ProcessSidecars, s.processSidecarRequests, ); err != nil { return err } // register as recipient of VerifySidecars messages. - sidecarVerifyRequests := make(chan *asynctypes.Message[BlobSidecarsT]) if err = s.dispatcher.RegisterMsgReceiver( - messages.VerifySidecars, sidecarVerifyRequests, + messages.VerifySidecars, s.verifySidecarRequests, ); err != nil { return err } // start a goroutine to listen for requests and handle accordingly - go s.start(ctx, sidecarsProcessRequests, sidecarVerifyRequests) + go s.start(ctx) return nil } @@ -111,16 +113,14 @@ func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { // handles them accordingly. func (s *Service[_, _, BlobSidecarsT, _]) start( ctx context.Context, - sidecarsProcessRequests chan *asynctypes.Message[BlobSidecarsT], - sidecarVerifyRequests chan *asynctypes.Message[BlobSidecarsT], ) { for { select { case <-ctx.Done(): return - case msg := <-sidecarsProcessRequests: + case msg := <-s.processSidecarRequests: s.handleBlobSidecarsProcessRequest(msg) - case msg := <-sidecarVerifyRequests: + case msg := <-s.verifySidecarRequests: s.handleSidecarsVerifyRequest(msg) } } @@ -147,7 +147,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( } // dispatch a response to acknowledge the request. - if err = s.dispatcher.Respond( + if err = s.dispatcher.SendResponse( asynctypes.NewMessage( msg.Context(), messages.ProcessSidecars, @@ -176,7 +176,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( } // dispatch a response to acknowledge the request. - if err = s.dispatcher.Respond( + if err = s.dispatcher.SendResponse( asynctypes.NewMessage( msg.Context(), messages.VerifySidecars, diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index cef82d0981..4b2bb5f697 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -90,15 +90,6 @@ func ProvideAvailabilityPruner( return nil, errors.New("availability store does not have a range db") } - var finalizedBlkCh = make(chan *FinalizedBlockEvent) - if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, - ); err != nil { - in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) - return nil, err - } - // build the availability pruner if IndexDB is available. return pruner.NewPruner[ *BeaconBlock, @@ -108,7 +99,8 @@ func ProvideAvailabilityPruner( in.Logger.With("service", manager.AvailabilityPrunerName), rangeDB, manager.AvailabilityPrunerName, - finalizedBlkCh, + messages.BeaconBlockFinalizedEvent, + in.Dispatcher, dastore.BuildPruneRangeFn[ *BeaconBlock, *FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 0e18be153a..01d1b5987c 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -71,15 +71,6 @@ type BlockPrunerInput struct { func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { - var finalizedBlkCh = make(chan *FinalizedBlockEvent) - if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, - ); err != nil { - in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) - return nil, err - } - return pruner.NewPruner[ *BeaconBlock, *FinalizedBlockEvent, @@ -88,7 +79,8 @@ func ProvideBlockPruner( in.Logger.With("service", manager.BlockPrunerName), in.BlockStore, manager.BlockPrunerName, - finalizedBlkCh, + messages.BeaconBlockFinalizedEvent, + in.Dispatcher, blockservice.BuildPruneRangeFn[ *BeaconBlock, *FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index d9a275d96b..e7781a3c76 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -70,15 +70,6 @@ type DepositPrunerInput struct { func ProvideDepositPruner( in DepositPrunerInput, ) (DepositPruner, error) { - var finalizedBlkCh = make(chan *FinalizedBlockEvent) - if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, - ); err != nil { - in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) - return nil, err - } - return pruner.NewPruner[ *BeaconBlock, *FinalizedBlockEvent, @@ -87,7 +78,8 @@ func ProvideDepositPruner( in.Logger.With("service", manager.DepositPrunerName), in.DepositStore, manager.DepositPrunerName, - finalizedBlkCh, + messages.BeaconBlockFinalizedEvent, + in.Dispatcher, deposit.BuildPruneRangeFn[ *BeaconBlockBody, *BeaconBlock, diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index a717c57c06..ff73601395 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -57,7 +57,7 @@ func (h *ABCIMiddleware[ } // request for validator updates - if err = h.dispatcher.Request( + if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, ), &valUpdateResp, @@ -86,7 +86,7 @@ func (h *ABCIMiddleware[ defer h.metrics.measurePrepareProposalDuration(startTime) // request a built beacon block for the given slot - if err := h.dispatcher.Request( + if err := h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.BuildBeaconBlockAndSidecars, slotData, ), &beaconBlkBundleResp, @@ -158,7 +158,7 @@ func (h *ABCIMiddleware[ } // verify the beacon block - if err = h.dispatcher.Request( + if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.VerifyBeaconBlock, blk, ), &beaconBlockResp, @@ -176,7 +176,7 @@ func (h *ABCIMiddleware[ } // verify the blob sidecars - if err = h.dispatcher.Request( + if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.VerifySidecars, sidecars, ), &sidecarsResp, @@ -253,7 +253,7 @@ func (h *ABCIMiddleware[ } // process the blob sidecars. - if err = h.dispatcher.Request( + if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.ProcessSidecars, blobs, ), &sidecarsResp, @@ -262,7 +262,7 @@ func (h *ABCIMiddleware[ } // finalize the beacon block. - if err = h.dispatcher.Request( + if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.FinalizeBeaconBlock, blk, ), &valUpdatesResp, diff --git a/mod/storage/pkg/manager/manager_test.go b/mod/storage/pkg/manager/manager_test.go index 17eb5ce99a..8cf8f828a3 100644 --- a/mod/storage/pkg/manager/manager_test.go +++ b/mod/storage/pkg/manager/manager_test.go @@ -31,6 +31,9 @@ import ( "time" "cosmossdk.io/log" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + "github.com/berachain/beacon-kit/mod/async/pkg/server" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -39,24 +42,26 @@ import ( func TestDBManager_Start(t *testing.T) { mockPrunable := new(mocks.Prunable) + logger := log.NewNopLogger() - ch := make(chan manager.BlockEvent[manager.BeaconBlock], 1) + dispatcher := setupDispatcher(logger) pruneParamsFn := func(_ manager.BlockEvent[manager.BeaconBlock]) (uint64, uint64) { return 0, 0 } - logger := log.NewNopLogger() p1 := pruner.NewPruner[ manager.BeaconBlock, manager.BlockEvent[manager.BeaconBlock], *mocks.Prunable, - ](logger, mockPrunable, "pruner1", ch, pruneParamsFn) + ](logger, mockPrunable, "pruner1", messages.BeaconBlockFinalizedEvent, + dispatcher, pruneParamsFn) p2 := pruner.NewPruner[ manager.BeaconBlock, manager.BlockEvent[manager.BeaconBlock], *mocks.Prunable, - ](logger, mockPrunable, "pruner2", ch, pruneParamsFn) + ](logger, mockPrunable, "pruner2", messages.BeaconBlockFinalizedEvent, + dispatcher, pruneParamsFn) m, err := manager.NewDBManager(logger, p1, p2) require.NoError(t, err) @@ -69,3 +74,17 @@ func TestDBManager_Start(t *testing.T) { time.Sleep(100 * time.Millisecond) mockPrunable.AssertNotCalled(t, "PruneFromInclusive") } + +// setupDispatcher sets up a dispatcher with an event server and message server. +func setupDispatcher(logger log.Logger) *dispatcher.Dispatcher { + ch := make(chan manager.BlockEvent[manager.BeaconBlock], 1) + es := server.NewEventServer() + es.Subscribe(messages.BeaconBlockFinalizedEvent, ch) + ms := server.NewMessageServer() + dispatcher := dispatcher.NewDispatcher( + es, + ms, + logger, + ) + return dispatcher +} diff --git a/mod/storage/pkg/manager/types.go b/mod/storage/pkg/manager/types.go index d4b5ae2ca6..2629363909 100644 --- a/mod/storage/pkg/manager/types.go +++ b/mod/storage/pkg/manager/types.go @@ -37,6 +37,7 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { + ID() asynctypes.EventID Is(asynctypes.EventID) bool Data() BeaconBlockT } diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index e69a1fea56..b0629b4a32 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -66,6 +66,51 @@ func (_c *BlockEvent_Data_Call[BeaconBlockT]) RunAndReturn(run func() BeaconBloc return _c } +// ID provides a mock function with given fields: +func (_m *BlockEvent[BeaconBlockT]) ID() types.MessageID { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for ID") + } + + var r0 types.MessageID + if rf, ok := ret.Get(0).(func() types.MessageID); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(types.MessageID) + } + + return r0 +} + +// BlockEvent_ID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ID' +type BlockEvent_ID_Call[BeaconBlockT pruner.BeaconBlock] struct { + *mock.Call +} + +// ID is a helper method to define mock.On call +func (_e *BlockEvent_Expecter[BeaconBlockT]) ID() *BlockEvent_ID_Call[BeaconBlockT] { + return &BlockEvent_ID_Call[BeaconBlockT]{Call: _e.mock.On("ID")} +} + +func (_c *BlockEvent_ID_Call[BeaconBlockT]) Run(run func()) *BlockEvent_ID_Call[BeaconBlockT] { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 types.MessageID) *BlockEvent_ID_Call[BeaconBlockT] { + _c.Call.Return(_a0) + return _c +} + +func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() types.MessageID) *BlockEvent_ID_Call[BeaconBlockT] { + _c.Call.Return(run) + return _c +} + // Is provides a mock function with given fields: _a0 func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.MessageID) bool { ret := _m.Called(_a0) diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 72dc6bc85c..74f35f88aa 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -29,6 +29,8 @@ package pruner import ( "context" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" ) @@ -44,11 +46,13 @@ type pruner[ BlockEventT BlockEvent[BeaconBlockT], PrunableT Prunable, ] struct { - prunable Prunable - logger log.Logger[any] - name string - finalizedBlockCh chan BlockEventT - pruneRangeFn func(BlockEventT) (uint64, uint64) + prunable Prunable + logger log.Logger[any] + name string + finalizedBlkEvents chan BlockEventT + finalizedBlockEventID types.EventID + dispatcher *dispatcher.Dispatcher + pruneRangeFn func(BlockEventT) (uint64, uint64) } // NewPruner creates a new Pruner. @@ -60,20 +64,30 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - finalizedBlockCh chan BlockEventT, + finalizedBlockEventID types.EventID, + dispatcher *dispatcher.Dispatcher, pruneRangeFn func(BlockEventT) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, BlockEventT, PrunableT]{ - logger: logger, - prunable: prunable, - name: name, - finalizedBlockCh: finalizedBlockCh, - pruneRangeFn: pruneRangeFn, + logger: logger, + prunable: prunable, + name: name, + finalizedBlkEvents: make(chan BlockEventT), + finalizedBlockEventID: finalizedBlockEventID, + dispatcher: dispatcher, + pruneRangeFn: pruneRangeFn, } } // Start starts the Pruner by listening for new indexes to prune. func (p *pruner[_, BlockEventT, _]) Start(ctx context.Context) { + if err := p.dispatcher.Subscribe( + p.finalizedBlockEventID, p.finalizedBlkEvents, + ); err != nil { + p.logger.Error("failed to subscribe to event", "event", + p.finalizedBlockEventID, "err", err) + return + } go p.start(ctx) } @@ -85,7 +99,7 @@ func (p *pruner[_, BlockEventT, _]) start( select { case <-ctx.Done(): return - case event := <-p.finalizedBlockCh: + case event := <-p.finalizedBlkEvents: start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { p.logger.Error("‼️ error pruning index ‼️", "error", err) diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 0c664b99d4..6651a8a6f7 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -31,7 +31,12 @@ import ( "time" "cosmossdk.io/log" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + "github.com/berachain/beacon-kit/mod/async/pkg/messaging" + "github.com/berachain/beacon-kit/mod/async/pkg/server" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" "github.com/stretchr/testify/mock" @@ -70,7 +75,7 @@ func TestPruner(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { logger := log.NewNopLogger() - ch := make(chan pruner.BlockEvent[pruner.BeaconBlock]) + dispatcher := setupDispatcher(logger) mockPrunable := new(mocks.Prunable) mockPrunable.On("Prune", mock.Anything, mock.Anything). Return(nil) @@ -80,7 +85,8 @@ func TestPruner(t *testing.T) { pruner.BeaconBlock, pruner.BlockEvent[pruner.BeaconBlock], pruner.Prunable, - ](logger, mockPrunable, "TestPruner", ch, pruneRangeFn) + ](logger, mockPrunable, "TestPruner", + messages.BeaconBlockFinalizedEvent, dispatcher, pruneRangeFn) ctx, cancel := context.WithCancel(context.Background()) // need to ensure goroutine is stopped @@ -92,10 +98,12 @@ func TestPruner(t *testing.T) { for _, index := range tt.pruneIndexes { block := mocks.BeaconBlock{} block.On("GetSlot").Return(math.U64(index)) - event := mocks.BlockEvent[pruner.BeaconBlock]{} - event.On("Data").Return(&block) - event.On("Is", mock.Anything).Return(true) - ch <- &event + event := asynctypes.NewEvent[pruner.BeaconBlock]( + context.Background(), + messages.BeaconBlockFinalizedEvent, + &block, + ) + dispatcher.PublishEvent(event) } // some time for the goroutine to process the requests @@ -120,3 +128,28 @@ func TestPruner(t *testing.T) { }) } } + +// setupDispatcher sets up a dispatcher with an event server and message server. +func setupDispatcher(logger log.Logger) *dispatcher.Dispatcher { + finBlkPublisher := messaging.NewPublisher[*pruner.BlockEvent[pruner.BeaconBlock]]( + messages.BeaconBlockFinalizedEvent, + ) + es := server.NewEventServer() + ms := server.NewMessageServer() + d := dispatcher.NewDispatcher( + es, + ms, + logger, + ) + + d.RegisterPublisher( + finBlkPublisher.EventID(), + finBlkPublisher, + ) + + // Subscribe to the event after creating the dispatcher + ch := make(chan pruner.BlockEvent[pruner.BeaconBlock]) + d.Subscribe(messages.BeaconBlockFinalizedEvent, ch) + + return d +} diff --git a/mod/storage/pkg/pruner/types.go b/mod/storage/pkg/pruner/types.go index 2e97436760..2ae38279c3 100644 --- a/mod/storage/pkg/pruner/types.go +++ b/mod/storage/pkg/pruner/types.go @@ -39,6 +39,7 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { + ID() asynctypes.EventID Is(asynctypes.EventID) bool Data() BeaconBlockT } From 125ad2941158ac47bfcfb0af312a2b082e8d9cd2 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 12:15:10 -0400 Subject: [PATCH 27/75] fixes --- mod/beacon/block_store/service.go | 26 +++++++++++++++----------- mod/beacon/blockchain/service.go | 8 ++++++-- mod/storage/pkg/pruner/pruner_test.go | 2 ++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index c0e551e3e7..bdac6daeb7 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -40,11 +40,12 @@ func NewService[ store BlockStoreT, ) *Service[BeaconBlockT, BlockStoreT] { return &Service[BeaconBlockT, BlockStoreT]{ - config: config, - logger: logger, - dispatcher: dispatcher, - store: store, - finalizeBlkReqs: make(chan *asynctypes.Message[BeaconBlockT]), + config: config, + logger: logger, + dispatcher: dispatcher, + store: store, + // finalizedBlkEvents is a channel for receiving finalized block events. + finalizedBlkEvents: make(chan *asynctypes.Event[BeaconBlockT]), } } @@ -56,10 +57,13 @@ type Service[ // config is the configuration for the block service. config Config // logger is used for logging information and errors. - logger log.Logger[any] - dispatcher *dispatcher.Dispatcher - store BlockStoreT - finalizeBlkReqs chan *asynctypes.Message[BeaconBlockT] + logger log.Logger[any] + // dispatcher is the dispatcher for the service. + dispatcher *dispatcher.Dispatcher + // store is the block store for the service. + store BlockStoreT + // finalizedBlkEvents is a channel for receiving finalized block events. + finalizedBlkEvents chan *asynctypes.Event[BeaconBlockT] } // Name returns the name of the service. @@ -76,7 +80,7 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { // subscribe a channel to the finalized block events. if err := s.dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, s.finalizeBlkReqs, + messages.BeaconBlockFinalizedEvent, s.finalizedBlkEvents, ); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err @@ -93,7 +97,7 @@ func (s *Service[BeaconBlockT, _]) listenAndStore( select { case <-ctx.Done(): return - case msg := <-s.finalizeBlkReqs: + case msg := <-s.finalizedBlkEvents: slot := msg.Data().GetSlot() if err := s.store.Set(slot, msg.Data()); err != nil { s.logger.Error( diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 58d6fcac0e..9a81ecc788 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -86,9 +86,13 @@ type Service[ // forceStartupSyncOnce is used to force a sync of the startup head. forceStartupSyncOnce *sync.Once + // finalizeBlkReqs is a channel for receiving finalize beacon block + // requests. finalizeBlkReqs chan *asynctypes.Message[BeaconBlockT] - verifyBlkReqs chan *asynctypes.Message[BeaconBlockT] - processGenReqs chan *asynctypes.Message[GenesisT] + // verifyBlkReqs is a channel for receiving verify beacon block requests. + verifyBlkReqs chan *asynctypes.Message[BeaconBlockT] + // processGenReqs is a channel for receiving process genesis data requests. + processGenReqs chan *asynctypes.Message[GenesisT] } // NewService creates a new validator service. diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 6651a8a6f7..01ef129867 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -130,6 +130,8 @@ func TestPruner(t *testing.T) { } // setupDispatcher sets up a dispatcher with an event server and message server. +// +//nolint:lll //annoying linter func setupDispatcher(logger log.Logger) *dispatcher.Dispatcher { finBlkPublisher := messaging.NewPublisher[*pruner.BlockEvent[pruner.BeaconBlock]]( messages.BeaconBlockFinalizedEvent, From ac6d1cb52b872fdb87b71e1d2fe4cb9c04a86a0b Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 12:44:09 -0400 Subject: [PATCH 28/75] revert pruner change --- .../pkg/components/availability_store.go | 12 ++++- mod/node-core/pkg/components/block_store.go | 12 ++++- mod/node-core/pkg/components/deposit_store.go | 12 ++++- mod/storage/pkg/manager/manager_test.go | 27 ++--------- mod/storage/pkg/manager/types.go | 1 - .../pkg/pruner/mocks/block_event.mock.go | 45 ------------------ mod/storage/pkg/pruner/pruner.go | 38 +++++---------- mod/storage/pkg/pruner/pruner_test.go | 47 +++---------------- mod/storage/pkg/pruner/types.go | 1 - 9 files changed, 52 insertions(+), 143 deletions(-) diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index 4b2bb5f697..cef82d0981 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -90,6 +90,15 @@ func ProvideAvailabilityPruner( return nil, errors.New("availability store does not have a range db") } + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) + return nil, err + } + // build the availability pruner if IndexDB is available. return pruner.NewPruner[ *BeaconBlock, @@ -99,8 +108,7 @@ func ProvideAvailabilityPruner( in.Logger.With("service", manager.AvailabilityPrunerName), rangeDB, manager.AvailabilityPrunerName, - messages.BeaconBlockFinalizedEvent, - in.Dispatcher, + finalizedBlkCh, dastore.BuildPruneRangeFn[ *BeaconBlock, *FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 01d1b5987c..0e18be153a 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -71,6 +71,15 @@ type BlockPrunerInput struct { func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) + return nil, err + } + return pruner.NewPruner[ *BeaconBlock, *FinalizedBlockEvent, @@ -79,8 +88,7 @@ func ProvideBlockPruner( in.Logger.With("service", manager.BlockPrunerName), in.BlockStore, manager.BlockPrunerName, - messages.BeaconBlockFinalizedEvent, - in.Dispatcher, + finalizedBlkCh, blockservice.BuildPruneRangeFn[ *BeaconBlock, *FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index e7781a3c76..d9a275d96b 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -70,6 +70,15 @@ type DepositPrunerInput struct { func ProvideDepositPruner( in DepositPrunerInput, ) (DepositPruner, error) { + var finalizedBlkCh = make(chan *FinalizedBlockEvent) + if err := in.Dispatcher.Subscribe( + messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + ); err != nil { + in.Logger.Error("failed to subscribe to event", "event", + messages.BeaconBlockFinalizedEvent, "err", err) + return nil, err + } + return pruner.NewPruner[ *BeaconBlock, *FinalizedBlockEvent, @@ -78,8 +87,7 @@ func ProvideDepositPruner( in.Logger.With("service", manager.DepositPrunerName), in.DepositStore, manager.DepositPrunerName, - messages.BeaconBlockFinalizedEvent, - in.Dispatcher, + finalizedBlkCh, deposit.BuildPruneRangeFn[ *BeaconBlockBody, *BeaconBlock, diff --git a/mod/storage/pkg/manager/manager_test.go b/mod/storage/pkg/manager/manager_test.go index 8cf8f828a3..17eb5ce99a 100644 --- a/mod/storage/pkg/manager/manager_test.go +++ b/mod/storage/pkg/manager/manager_test.go @@ -31,9 +31,6 @@ import ( "time" "cosmossdk.io/log" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/server" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -42,26 +39,24 @@ import ( func TestDBManager_Start(t *testing.T) { mockPrunable := new(mocks.Prunable) - logger := log.NewNopLogger() - dispatcher := setupDispatcher(logger) + ch := make(chan manager.BlockEvent[manager.BeaconBlock], 1) pruneParamsFn := func(_ manager.BlockEvent[manager.BeaconBlock]) (uint64, uint64) { return 0, 0 } + logger := log.NewNopLogger() p1 := pruner.NewPruner[ manager.BeaconBlock, manager.BlockEvent[manager.BeaconBlock], *mocks.Prunable, - ](logger, mockPrunable, "pruner1", messages.BeaconBlockFinalizedEvent, - dispatcher, pruneParamsFn) + ](logger, mockPrunable, "pruner1", ch, pruneParamsFn) p2 := pruner.NewPruner[ manager.BeaconBlock, manager.BlockEvent[manager.BeaconBlock], *mocks.Prunable, - ](logger, mockPrunable, "pruner2", messages.BeaconBlockFinalizedEvent, - dispatcher, pruneParamsFn) + ](logger, mockPrunable, "pruner2", ch, pruneParamsFn) m, err := manager.NewDBManager(logger, p1, p2) require.NoError(t, err) @@ -74,17 +69,3 @@ func TestDBManager_Start(t *testing.T) { time.Sleep(100 * time.Millisecond) mockPrunable.AssertNotCalled(t, "PruneFromInclusive") } - -// setupDispatcher sets up a dispatcher with an event server and message server. -func setupDispatcher(logger log.Logger) *dispatcher.Dispatcher { - ch := make(chan manager.BlockEvent[manager.BeaconBlock], 1) - es := server.NewEventServer() - es.Subscribe(messages.BeaconBlockFinalizedEvent, ch) - ms := server.NewMessageServer() - dispatcher := dispatcher.NewDispatcher( - es, - ms, - logger, - ) - return dispatcher -} diff --git a/mod/storage/pkg/manager/types.go b/mod/storage/pkg/manager/types.go index 2629363909..d4b5ae2ca6 100644 --- a/mod/storage/pkg/manager/types.go +++ b/mod/storage/pkg/manager/types.go @@ -37,7 +37,6 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { - ID() asynctypes.EventID Is(asynctypes.EventID) bool Data() BeaconBlockT } diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index b0629b4a32..e69a1fea56 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -66,51 +66,6 @@ func (_c *BlockEvent_Data_Call[BeaconBlockT]) RunAndReturn(run func() BeaconBloc return _c } -// ID provides a mock function with given fields: -func (_m *BlockEvent[BeaconBlockT]) ID() types.MessageID { - ret := _m.Called() - - if len(ret) == 0 { - panic("no return value specified for ID") - } - - var r0 types.MessageID - if rf, ok := ret.Get(0).(func() types.MessageID); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(types.MessageID) - } - - return r0 -} - -// BlockEvent_ID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ID' -type BlockEvent_ID_Call[BeaconBlockT pruner.BeaconBlock] struct { - *mock.Call -} - -// ID is a helper method to define mock.On call -func (_e *BlockEvent_Expecter[BeaconBlockT]) ID() *BlockEvent_ID_Call[BeaconBlockT] { - return &BlockEvent_ID_Call[BeaconBlockT]{Call: _e.mock.On("ID")} -} - -func (_c *BlockEvent_ID_Call[BeaconBlockT]) Run(run func()) *BlockEvent_ID_Call[BeaconBlockT] { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 types.MessageID) *BlockEvent_ID_Call[BeaconBlockT] { - _c.Call.Return(_a0) - return _c -} - -func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() types.MessageID) *BlockEvent_ID_Call[BeaconBlockT] { - _c.Call.Return(run) - return _c -} - // Is provides a mock function with given fields: _a0 func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.MessageID) bool { ret := _m.Called(_a0) diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 74f35f88aa..72dc6bc85c 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -29,8 +29,6 @@ package pruner import ( "context" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" ) @@ -46,13 +44,11 @@ type pruner[ BlockEventT BlockEvent[BeaconBlockT], PrunableT Prunable, ] struct { - prunable Prunable - logger log.Logger[any] - name string - finalizedBlkEvents chan BlockEventT - finalizedBlockEventID types.EventID - dispatcher *dispatcher.Dispatcher - pruneRangeFn func(BlockEventT) (uint64, uint64) + prunable Prunable + logger log.Logger[any] + name string + finalizedBlockCh chan BlockEventT + pruneRangeFn func(BlockEventT) (uint64, uint64) } // NewPruner creates a new Pruner. @@ -64,30 +60,20 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - finalizedBlockEventID types.EventID, - dispatcher *dispatcher.Dispatcher, + finalizedBlockCh chan BlockEventT, pruneRangeFn func(BlockEventT) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, BlockEventT, PrunableT]{ - logger: logger, - prunable: prunable, - name: name, - finalizedBlkEvents: make(chan BlockEventT), - finalizedBlockEventID: finalizedBlockEventID, - dispatcher: dispatcher, - pruneRangeFn: pruneRangeFn, + logger: logger, + prunable: prunable, + name: name, + finalizedBlockCh: finalizedBlockCh, + pruneRangeFn: pruneRangeFn, } } // Start starts the Pruner by listening for new indexes to prune. func (p *pruner[_, BlockEventT, _]) Start(ctx context.Context) { - if err := p.dispatcher.Subscribe( - p.finalizedBlockEventID, p.finalizedBlkEvents, - ); err != nil { - p.logger.Error("failed to subscribe to event", "event", - p.finalizedBlockEventID, "err", err) - return - } go p.start(ctx) } @@ -99,7 +85,7 @@ func (p *pruner[_, BlockEventT, _]) start( select { case <-ctx.Done(): return - case event := <-p.finalizedBlkEvents: + case event := <-p.finalizedBlockCh: start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { p.logger.Error("‼️ error pruning index ‼️", "error", err) diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 01ef129867..0c664b99d4 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -31,12 +31,7 @@ import ( "time" "cosmossdk.io/log" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/messaging" - "github.com/berachain/beacon-kit/mod/async/pkg/server" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" "github.com/stretchr/testify/mock" @@ -75,7 +70,7 @@ func TestPruner(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { logger := log.NewNopLogger() - dispatcher := setupDispatcher(logger) + ch := make(chan pruner.BlockEvent[pruner.BeaconBlock]) mockPrunable := new(mocks.Prunable) mockPrunable.On("Prune", mock.Anything, mock.Anything). Return(nil) @@ -85,8 +80,7 @@ func TestPruner(t *testing.T) { pruner.BeaconBlock, pruner.BlockEvent[pruner.BeaconBlock], pruner.Prunable, - ](logger, mockPrunable, "TestPruner", - messages.BeaconBlockFinalizedEvent, dispatcher, pruneRangeFn) + ](logger, mockPrunable, "TestPruner", ch, pruneRangeFn) ctx, cancel := context.WithCancel(context.Background()) // need to ensure goroutine is stopped @@ -98,12 +92,10 @@ func TestPruner(t *testing.T) { for _, index := range tt.pruneIndexes { block := mocks.BeaconBlock{} block.On("GetSlot").Return(math.U64(index)) - event := asynctypes.NewEvent[pruner.BeaconBlock]( - context.Background(), - messages.BeaconBlockFinalizedEvent, - &block, - ) - dispatcher.PublishEvent(event) + event := mocks.BlockEvent[pruner.BeaconBlock]{} + event.On("Data").Return(&block) + event.On("Is", mock.Anything).Return(true) + ch <- &event } // some time for the goroutine to process the requests @@ -128,30 +120,3 @@ func TestPruner(t *testing.T) { }) } } - -// setupDispatcher sets up a dispatcher with an event server and message server. -// -//nolint:lll //annoying linter -func setupDispatcher(logger log.Logger) *dispatcher.Dispatcher { - finBlkPublisher := messaging.NewPublisher[*pruner.BlockEvent[pruner.BeaconBlock]]( - messages.BeaconBlockFinalizedEvent, - ) - es := server.NewEventServer() - ms := server.NewMessageServer() - d := dispatcher.NewDispatcher( - es, - ms, - logger, - ) - - d.RegisterPublisher( - finBlkPublisher.EventID(), - finBlkPublisher, - ) - - // Subscribe to the event after creating the dispatcher - ch := make(chan pruner.BlockEvent[pruner.BeaconBlock]) - d.Subscribe(messages.BeaconBlockFinalizedEvent, ch) - - return d -} diff --git a/mod/storage/pkg/pruner/types.go b/mod/storage/pkg/pruner/types.go index 2ae38279c3..2e97436760 100644 --- a/mod/storage/pkg/pruner/types.go +++ b/mod/storage/pkg/pruner/types.go @@ -39,7 +39,6 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { - ID() asynctypes.EventID Is(asynctypes.EventID) bool Data() BeaconBlockT } From 0a0735ab7b4de912a1245e49db9a622f8a1c8042 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 12:53:27 -0400 Subject: [PATCH 29/75] pass dispatcher in deposit service --- mod/async/pkg/server/errors.go | 4 ++- mod/async/pkg/server/event.go | 2 +- mod/execution/pkg/deposit/service.go | 36 +++++++++++++------ mod/execution/pkg/deposit/sync.go | 2 +- .../pkg/components/deposit_service.go | 12 ++----- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index 2c429df8bf..ede1874a8e 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -27,7 +27,9 @@ import ( //nolint:gochecknoglobals // errors var ( - ErrPublisherNotFound = errors.New("feed not found") + ErrPublisherNotFound = func(eventID types.EventID) error { + return errors.Newf("publisher not found for eventID: %s", eventID) + } ErrRouteNotFound = errors.New("route not found") ErrRouteAlreadyRegistered = func(messageID types.MessageID) error { return errors.Newf("route already registered for messageID: %s", diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 770093d186..7cdf3fd14b 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -51,7 +51,7 @@ func (es *EventServer) Publish(event types.MessageI) error { func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { publisher, ok := es.publishers[eventID] if !ok { - return ErrPublisherNotFound + return ErrPublisherNotFound(eventID) } return publisher.Subscribe(ch) } diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 5882643c0d..7edd9ef592 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -23,6 +23,8 @@ package deposit import ( "context" + "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -46,8 +48,12 @@ type Service[ dc Contract[DepositT] // ds is the deposit store that stores deposits. ds Store[DepositT] - // finalizedBlockCh is the channel that provides finalized block events. - finalizedBlockCh chan BlockEventT + // dispatcher is the dispatcher for the service. + dispatcher *dispatcher.Dispatcher + // finalizedBlockEventID is the event ID for the finalized block event. + finalizedBlockEventID asynctypes.EventID + // finalizedBlockEvents is the channel that provides finalized block events. + finalizedBlockEvents chan BlockEventT // metrics is the metrics for the deposit service. metrics *metrics // failedBlocks is a map of blocks that failed to be processed to be @@ -73,7 +79,8 @@ func NewService[ telemetrySink TelemetrySink, ds Store[DepositT], dc Contract[DepositT], - feed chan BlockEventT, + finalizedBlockEventID asynctypes.EventID, + dispatcher *dispatcher.Dispatcher, ) *Service[ BeaconBlockT, BeaconBlockBodyT, BlockEventT, DepositT, ExecutionPayloadT, WithdrawalCredentialsT, @@ -83,13 +90,15 @@ func NewService[ ExecutionPayloadT, WithdrawalCredentialsT, ]{ - finalizedBlockCh: feed, - logger: logger, - eth1FollowDistance: eth1FollowDistance, - metrics: newMetrics(telemetrySink), - dc: dc, - ds: ds, - failedBlocks: make(map[math.Slot]struct{}), + dc: dc, + dispatcher: dispatcher, + ds: ds, + eth1FollowDistance: eth1FollowDistance, + failedBlocks: make(map[math.Slot]struct{}), + finalizedBlockEventID: finalizedBlockEventID, + finalizedBlockEvents: make(chan BlockEventT), + logger: logger, + metrics: newMetrics(telemetrySink), } } @@ -97,6 +106,13 @@ func NewService[ func (s *Service[ _, _, _, _, _, _, ]) Start(ctx context.Context) error { + if err := s.dispatcher.Subscribe( + s.finalizedBlockEventID, s.finalizedBlockEvents, + ); err != nil { + s.logger.Error("failed to subscribe to event", "event", + s.finalizedBlockEventID, "err", err) + return err + } go s.depositFetcher(ctx) go s.depositCatchupFetcher(ctx) return nil diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index 076c9bdab9..842d0b2d3a 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -38,7 +38,7 @@ func (s *Service[ select { case <-ctx.Done(): return - case msg := <-s.finalizedBlockCh: + case msg := <-s.finalizedBlockEvents: blockNum := msg.Data(). GetBody().GetExecutionPayload().GetNumber() s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) diff --git a/mod/node-core/pkg/components/deposit_service.go b/mod/node-core/pkg/components/deposit_service.go index c2e3611cf2..1c3b723bc2 100644 --- a/mod/node-core/pkg/components/deposit_service.go +++ b/mod/node-core/pkg/components/deposit_service.go @@ -46,15 +46,6 @@ type DepositServiceIn struct { // ProvideDepositService provides the deposit service to the depinject // framework. func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { - var finalizedBlkCh = make(chan *FinalizedBlockEvent) - if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, - ); err != nil { - in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) - return nil, err - } - // Build the deposit service. return deposit.NewService[ *BeaconBlockBody, @@ -68,6 +59,7 @@ func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { in.TelemetrySink, in.DepositStore, in.BeaconDepositContract, - finalizedBlkCh, + messages.BeaconBlockFinalizedEvent, + in.Dispatcher, ), nil } From c84a80d9145a260723239197db9c0508ae070aea Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 12:59:16 -0400 Subject: [PATCH 30/75] registry --- mod/async/pkg/dispatcher/dispatcher.go | 4 ++++ mod/node-core/pkg/components/service_registry.go | 2 +- mod/runtime/pkg/service/registry.go | 12 +----------- mod/runtime/pkg/service/registry_test.go | 6 ++---- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index eeaf980034..517bb8c543 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -117,3 +117,7 @@ func (d *Dispatcher) RegisterRoute( d.logger.Info("Route registered", "messageID", messageID) return d.msgServer.RegisterRoute(messageID, route) } + +func (d *Dispatcher) Name() string { + return "dispatcher" +} diff --git a/mod/node-core/pkg/components/service_registry.go b/mod/node-core/pkg/components/service_registry.go index 797a815f04..6d18cc689e 100644 --- a/mod/node-core/pkg/components/service_registry.go +++ b/mod/node-core/pkg/components/service_registry.go @@ -49,8 +49,8 @@ func ProvideServiceRegistry( in ServiceRegistryInput, ) *service.Registry { return service.NewRegistry( - in.Dispatcher, service.WithLogger(in.Logger), + service.WithService(in.Dispatcher), service.WithService(in.ValidatorService), service.WithService(in.BlockStoreService), service.WithService(in.ChainService), diff --git a/mod/runtime/pkg/service/registry.go b/mod/runtime/pkg/service/registry.go index 4b5d0bb97b..dff8fcd4b7 100644 --- a/mod/runtime/pkg/service/registry.go +++ b/mod/runtime/pkg/service/registry.go @@ -43,8 +43,6 @@ type Dispatcher interface { // It allows for ease of dependency management and ensures services // dependent on others use the same references in memory. type Registry struct { - // dispatcher is the dispatcher for the Registry. - dispatcher Dispatcher // logger is the logger for the Registry. logger log.Logger[any] // services is a map of service type -> service instance. @@ -55,11 +53,9 @@ type Registry struct { // NewRegistry starts a registry instance for convenience. func NewRegistry( - dispatcher Dispatcher, opts ...RegistryOption) *Registry { r := &Registry{ - services: make(map[string]Basic), - dispatcher: dispatcher, + services: make(map[string]Basic), } for _, opt := range opts { @@ -72,12 +68,6 @@ func NewRegistry( // StartAll initialized each service in order of registration. func (s *Registry) StartAll(ctx context.Context) error { - // start the dispatcher - s.logger.Info("Starting dispatcher") - if err := s.dispatcher.Start(ctx); err != nil { - return err - } - // start all services s.logger.Info("Starting services", "num", len(s.serviceTypes)) for _, typeName := range s.serviceTypes { diff --git a/mod/runtime/pkg/service/registry_test.go b/mod/runtime/pkg/service/registry_test.go index 6ef8ad10b5..4be725d0aa 100644 --- a/mod/runtime/pkg/service/registry_test.go +++ b/mod/runtime/pkg/service/registry_test.go @@ -35,8 +35,7 @@ import ( func TestRegistry_StartAll(t *testing.T) { logger := noop.NewLogger[any]() - dispatcher := &mocks.Dispatcher{} - registry := service.NewRegistry(dispatcher, service.WithLogger(logger)) + registry := service.NewRegistry(service.WithLogger(logger)) service1 := &mocks.Basic{} service1.On("Start", mock.Anything).Return(nil).Once() @@ -65,8 +64,7 @@ func TestRegistry_StartAll(t *testing.T) { func TestRegistry_FetchService(t *testing.T) { logger := noop.NewLogger[any]() - dispatcher := &mocks.Dispatcher{} - registry := service.NewRegistry(dispatcher, service.WithLogger(logger)) + registry := service.NewRegistry(service.WithLogger(logger)) service1 := new(mocks.Basic) service1.On("Name").Return("Service1") From a57efa2a1de4f80e04ff51ae04a670dd873d8b3a Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 13:07:07 -0400 Subject: [PATCH 31/75] provide --- mod/async/pkg/dispatcher/types.go | 4 +-- mod/async/pkg/server/event.go | 1 + mod/node-core/pkg/components/defaults.go | 4 +-- mod/node-core/pkg/components/dispatcher.go | 34 ++++++++++++++++++---- mod/node-core/pkg/components/events.go | 17 +---------- mod/node-core/pkg/components/messages.go | 22 ++------------ 6 files changed, 36 insertions(+), 46 deletions(-) diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 0ddd3e80d0..fb39df1d73 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -37,8 +37,8 @@ type MessageServer interface { type EventServer interface { Start(ctx context.Context) - RegisterPublisher(mID types.MessageID, publisher types.Publisher) - Subscribe(mID types.MessageID, ch any) error + RegisterPublisher(mID types.EventID, publisher types.Publisher) + Subscribe(mID types.EventID, ch any) error Publish(event types.MessageI) error SetLogger(logger log.Logger[any]) } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 7cdf3fd14b..af25cd920e 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -72,6 +72,7 @@ func (es *EventServer) RegisterPublisher( es.publishers[eventID] = publisher } +// SetLogger sets the logger for the event server. func (es *EventServer) SetLogger(logger log.Logger[any]) { es.logger = logger } diff --git a/mod/node-core/pkg/components/defaults.go b/mod/node-core/pkg/components/defaults.go index 6ac9ac78c5..d8d438f17e 100644 --- a/mod/node-core/pkg/components/defaults.go +++ b/mod/node-core/pkg/components/defaults.go @@ -43,7 +43,6 @@ func DefaultComponentsWithStandardTypes() []any { ProvideDepositPruner, ProvideDepositService, ProvideDepositStore, - ProvideDispatcher, ProvideEngineClient, ProvideExecutionEngine, ProvideJWTSecret, @@ -59,7 +58,6 @@ func DefaultComponentsWithStandardTypes() []any { } components = append(components, DefaultNodeAPIComponents()...) components = append(components, DefaultNodeAPIHandlers()...) - components = append(components, EventServerComponents()...) - components = append(components, MessageServerComponents()...) + components = append(components, DefaultDispatcherComponents()...) return components } diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index 2f806f6ec7..edf0828d0d 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -24,24 +24,48 @@ import ( "cosmossdk.io/depinject" sdklog "cosmossdk.io/log" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" ) // DispatcherInput is the input for the Dispatcher. type DispatcherInput struct { depinject.In - EventServer *EventServer - MessageServer *MessageServer - Logger log.AdvancedLogger[any, sdklog.Logger] + EventServer *EventServer + MessageServer *MessageServer + Logger log.AdvancedLogger[any, sdklog.Logger] + BeaconBlockFinalizedPublisher *BeaconBlockFinalizedPublisher + Routes []asynctypes.MessageRoute } // ProvideDispatcher provides a new Dispatcher. func ProvideDispatcher( in DispatcherInput, -) *Dispatcher { - return dispatcher.NewDispatcher( +) (*Dispatcher, error) { + d := dispatcher.NewDispatcher( in.EventServer, in.MessageServer, in.Logger.With("service", "dispatcher"), ) + d.RegisterPublisher( + in.BeaconBlockFinalizedPublisher.EventID(), + in.BeaconBlockFinalizedPublisher, + ) + for _, route := range in.Routes { + if err := d.RegisterRoute(route.MessageID(), route); err != nil { + return nil, err + } + } + + return d, nil +} + +func DefaultDispatcherComponents() []any { + return []any{ + ProvideDispatcher, + ProvideMessageRoutes, + ProvideBeaconBlockFinalizedPublisher, + ProvideMessageServer, + ProvideEventServer, + } } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 4f38ed9b19..e9cde9e5e7 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -30,17 +30,11 @@ import ( // EventServerInput is the input for the event server. type EventServerInput struct { depinject.In - BeaconBlockFinalizedPublisher *BeaconBlockFinalizedPublisher } // ProvideEventServer provides an event server. func ProvideEventServer(in EventServerInput) *EventServer { - es := server.NewEventServer() - es.RegisterPublisher( - in.BeaconBlockFinalizedPublisher.EventID(), - in.BeaconBlockFinalizedPublisher, - ) - return es + return server.NewEventServer() } // ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block @@ -50,12 +44,3 @@ func ProvideBeaconBlockFinalizedPublisher() *BeaconBlockFinalizedPublisher { messages.BeaconBlockFinalizedEvent, ) } - -// EventServerComponents returns all the depinject providers for the event -// server. -func EventServerComponents() []any { - return []any{ - ProvideEventServer, - ProvideBeaconBlockFinalizedPublisher, - } -} diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index a70309be35..a9eb5832ab 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -30,20 +30,11 @@ import ( type MessageServerInput struct { depinject.In - Routes []asynctypes.MessageRoute } // ProvideMessageServer provides a message server. -func ProvideMessageServer(in MessageServerInput) ( - *server.MessageServer, error, -) { - ms := server.NewMessageServer() - for _, route := range in.Routes { - if err := ms.RegisterRoute(route.MessageID(), route); err != nil { - return nil, err - } - } - return ms, nil +func ProvideMessageServer(in MessageServerInput) *server.MessageServer { + return server.NewMessageServer() } // ProvideMessageRoutes provides all the message routes. @@ -89,12 +80,3 @@ func RouteFactory(mID string) asynctypes.MessageRoute { return nil } } - -// MessageServerComponents returns all the depinject providers for the message -// server. -func MessageServerComponents() []any { - return []any{ - ProvideMessageServer, - ProvideMessageRoutes, - } -} From 6806275a2a64b0320510b2742e4b81920e8f74cc Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 13:34:27 -0400 Subject: [PATCH 32/75] nil --- mod/async/pkg/server/event.go | 6 +++++- mod/node-core/pkg/components/events.go | 8 +------- mod/node-core/pkg/components/messages.go | 7 +------ 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index af25cd920e..d7e63e7a92 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -42,7 +42,11 @@ func NewEventServer() *EventServer { // Dispatch dispatches the given event to the publisher with the given eventID. func (es *EventServer) Publish(event types.MessageI) error { - return es.publishers[event.ID()].Publish(event) + publisher, ok := es.publishers[event.ID()] + if !ok { + return ErrPublisherNotFound(event.ID()) + } + return publisher.Publish(event) } // Subscribe subscribes the given channel to the publisher with the given diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index e9cde9e5e7..93e5cd9038 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -21,19 +21,13 @@ package components import ( - "cosmossdk.io/depinject" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" "github.com/berachain/beacon-kit/mod/async/pkg/server" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) -// EventServerInput is the input for the event server. -type EventServerInput struct { - depinject.In -} - // ProvideEventServer provides an event server. -func ProvideEventServer(in EventServerInput) *EventServer { +func ProvideEventServer() *EventServer { return server.NewEventServer() } diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index a9eb5832ab..ee80d215e0 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -21,19 +21,14 @@ package components import ( - "cosmossdk.io/depinject" "github.com/berachain/beacon-kit/mod/async/pkg/messaging" "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) -type MessageServerInput struct { - depinject.In -} - // ProvideMessageServer provides a message server. -func ProvideMessageServer(in MessageServerInput) *server.MessageServer { +func ProvideMessageServer() *server.MessageServer { return server.NewMessageServer() } From 687bf5fb5dc1d5d89556c5b1926af7b79efd63b5 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 14:05:11 -0400 Subject: [PATCH 33/75] bet --- mod/async/pkg/messaging/route.go | 16 ++++++++-------- mod/da/pkg/da/service.go | 3 +-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 220af69373..8b2c90caae 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -30,11 +30,11 @@ import ( // Route represents a communication route to a single recipient. // Invariant: there is exactly no more than one route for each messageID. type Route[ReqT any, RespT any] struct { - messageID types.MessageID - recipient chan ReqT - responseCh chan RespT - timeout time.Duration - mu sync.Mutex + messageID types.MessageID + recipientCh chan ReqT + responseCh chan RespT + timeout time.Duration + mu sync.Mutex } // NewRoute creates a new route. @@ -56,7 +56,7 @@ func (r *Route[ReqT, RespT]) MessageID() types.MessageID { // RegisterReceiver sets the recipient for the route. func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { - if r.recipient != nil { + if r.recipientCh != nil { return errRouteAlreadySet } else if ch == nil { return errRegisteringNilChannel(r.messageID) @@ -65,7 +65,7 @@ func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { if err != nil { return err } - r.recipient = typedCh + r.recipientCh = typedCh return nil } @@ -77,7 +77,7 @@ func (r *Route[ReqT, RespT]) SendRequest(req types.MessageI) error { } select { - case r.recipient <- typedMsg: + case r.recipientCh <- typedMsg: return nil default: // Channel is full or closed diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index da6153000b..3a9100d538 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -166,8 +166,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( ) { var err error // verify the sidecars. - err = s.verifySidecars(msg.Data()) - if err != nil { + if err = s.verifySidecars(msg.Data()); err != nil { s.logger.Error( "Failed to receive blob sidecars", "error", From ec0bf81df34e1b48fa9fbfde69e1271e72be5dff Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 16:30:16 -0400 Subject: [PATCH 34/75] merge main --- build/tools/go.mod | 2 +- build/tools/go.sum | 4 +- mod/consensus/go.mod | 4 +- mod/consensus/pkg/cometbft/consensus.go | 33 ---- mod/consensus/pkg/cometbft/helpers.go | 23 --- mod/consensus/pkg/cometbft/types.go | 6 +- mod/node-core/go.mod | 4 +- mod/node-core/go.sum | 8 +- mod/node-core/pkg/builder/baseapp_options.go | 9 - mod/node-core/pkg/builder/builder.go | 3 +- mod/node-core/pkg/builder/config.go | 10 +- mod/node-core/pkg/components/module/module.go | 49 +----- .../components/module/proto/buf.gen.gogo.yaml | 34 ---- .../module/proto/buf.gen.pulsar.yaml | 42 ----- .../pkg/components/module/proto/buf.lock | 28 --- .../pkg/components/module/proto/buf.yaml | 51 ------ mod/runtime/go.mod | 36 +++- mod/runtime/go.sum | 73 +++++++- mod/runtime/pkg/cosmos/baseapp/abci.go | 33 ++-- mod/runtime/pkg/cosmos/baseapp/baseapp.go | 69 +++----- mod/runtime/pkg/cosmos/baseapp/options.go | 16 +- mod/runtime/pkg/cosmos/runtime/app.go | 161 +++++++----------- mod/runtime/pkg/cosmos/runtime/builder.go | 3 +- mod/runtime/pkg/cosmos/runtime/export.go | 27 ++- mod/runtime/pkg/cosmos/runtime/module.go | 16 +- .../pkg/cosmos/runtime/types.go} | 35 +++- mod/runtime/pkg/middleware/abci.go | 29 +--- mod/runtime/pkg/middleware/middleware.go | 4 - 28 files changed, 290 insertions(+), 522 deletions(-) delete mode 100644 mod/node-core/pkg/components/module/proto/buf.gen.gogo.yaml delete mode 100644 mod/node-core/pkg/components/module/proto/buf.gen.pulsar.yaml delete mode 100644 mod/node-core/pkg/components/module/proto/buf.lock delete mode 100644 mod/node-core/pkg/components/module/proto/buf.yaml rename mod/{node-core/pkg/components/module/proto/module/v1alpha1/module.proto => runtime/pkg/cosmos/runtime/types.go} (57%) diff --git a/build/tools/go.mod b/build/tools/go.mod index f13ea3b660..9525dfd590 100644 --- a/build/tools/go.mod +++ b/build/tools/go.mod @@ -98,7 +98,7 @@ require ( github.com/distribution/reference v0.6.0 // indirect github.com/docker/cli v26.1.4+incompatible // indirect github.com/docker/distribution v2.8.3+incompatible // indirect - github.com/docker/docker v27.1.0+incompatible // indirect + github.com/docker/docker v27.1.1+incompatible // indirect github.com/docker/docker-credential-helpers v0.8.2 // indirect github.com/docker/go-connections v0.5.0 // indirect github.com/docker/go-units v0.5.0 // indirect diff --git a/build/tools/go.sum b/build/tools/go.sum index 7883c977d5..595291c76d 100644 --- a/build/tools/go.sum +++ b/build/tools/go.sum @@ -199,8 +199,8 @@ github.com/docker/cli v26.1.4+incompatible h1:I8PHdc0MtxEADqYJZvhBrW9bo8gawKwwen github.com/docker/cli v26.1.4+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk= github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v27.1.0+incompatible h1:rEHVQc4GZ0MIQKifQPHSFGV/dVgaZafgRf8fCPtDYBs= -github.com/docker/docker v27.1.0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY= +github.com/docker/docker v27.1.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker-credential-helpers v0.8.2 h1:bX3YxiGzFP5sOXWc3bTPEXdEaZSeVMrFgOr3T+zrFAo= github.com/docker/docker-credential-helpers v0.8.2/go.mod h1:P3ci7E3lwkZg6XiHdRKft1KckHiO9a2rNtyFbZ/ry9M= github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c= diff --git a/mod/consensus/go.mod b/mod/consensus/go.mod index b1bf0a05d1..5bf838d6b4 100644 --- a/mod/consensus/go.mod +++ b/mod/consensus/go.mod @@ -14,13 +14,11 @@ replace ( ) require ( - cosmossdk.io/core v0.12.1-0.20240806152830-8fb47b368cd4 github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 github.com/cometbft/cometbft v1.0.0-rc1.0.20240806094948-2c4293ef36c4 github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240806094948-2c4293ef36c4 github.com/cosmos/cosmos-sdk v0.53.0 github.com/cosmos/gogoproto v1.5.0 - github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 ) require ( @@ -28,6 +26,7 @@ require ( buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect cosmossdk.io/api v0.7.5 // indirect cosmossdk.io/collections v0.4.0 // indirect + cosmossdk.io/core v0.12.1-0.20240806152830-8fb47b368cd4 // indirect cosmossdk.io/depinject v1.0.0 // indirect cosmossdk.io/errors v1.0.1 // indirect cosmossdk.io/log v1.4.0 // indirect @@ -102,6 +101,7 @@ require ( github.com/rs/zerolog v1.33.0 // indirect github.com/sagikazarmark/locafero v0.6.0 // indirect github.com/sasha-s/go-deadlock v0.3.1 // indirect + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/mod/consensus/pkg/cometbft/consensus.go b/mod/consensus/pkg/cometbft/consensus.go index 9b6d99f8a4..5b48fc0aad 100644 --- a/mod/consensus/pkg/cometbft/consensus.go +++ b/mod/consensus/pkg/cometbft/consensus.go @@ -21,11 +21,8 @@ package cometbft import ( - "context" - cmtabci "github.com/cometbft/cometbft/abci/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/sourcegraph/conc/iter" ) // ConsensusEngine is used to decouple the Comet consensus engine from @@ -76,18 +73,6 @@ func NewConsensusEngine[ } } -func (c *ConsensusEngine[_, _, _, _, _, ValidatorUpdateT]) InitGenesis( - ctx context.Context, - genesisBz []byte, -) ([]ValidatorUpdateT, error) { - updates, err := c.Middleware.InitGenesis(ctx, genesisBz) - if err != nil { - return nil, err - } - // Convert updates into the Cosmos SDK format. - return iter.MapErr(updates, convertValidatorUpdate[ValidatorUpdateT]) -} - // TODO: Decouple Comet Types func (c *ConsensusEngine[_, _, _, _, _, _]) PrepareProposal( ctx sdk.Context, @@ -123,21 +108,3 @@ func (c *ConsensusEngine[_, _, _, _, _, ValidatorUpdateT]) ProcessProposal( } return resp.(*cmtabci.ProcessProposalResponse), nil } - -// TODO: Decouple Comet Types -func (c *ConsensusEngine[_, _, _, _, _, ValidatorUpdateT]) PreBlock( - ctx sdk.Context, - req *cmtabci.FinalizeBlockRequest, -) error { - return c.Middleware.PreBlock(ctx, req) -} - -func (c *ConsensusEngine[_, _, _, _, _, ValidatorUpdateT]) EndBlock( - ctx context.Context, -) ([]ValidatorUpdateT, error) { - updates, err := c.Middleware.EndBlock(ctx) - if err != nil { - return nil, err - } - return iter.MapErr(updates, convertValidatorUpdate[ValidatorUpdateT]) -} diff --git a/mod/consensus/pkg/cometbft/helpers.go b/mod/consensus/pkg/cometbft/helpers.go index b98fc9d87c..fed343728c 100644 --- a/mod/consensus/pkg/cometbft/helpers.go +++ b/mod/consensus/pkg/cometbft/helpers.go @@ -23,35 +23,12 @@ package cometbft import ( "sort" - appmodulev2 "cosmossdk.io/core/appmodule/v2" - "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" - "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" cmtabci "github.com/cometbft/cometbft/abci/types" v1 "github.com/cometbft/cometbft/api/cometbft/abci/v1" sdk "github.com/cosmos/cosmos-sdk/types" ) -// convertValidatorUpdate abstracts the conversion of a -// transition.ValidatorUpdate to an appmodulev2.ValidatorUpdate. -// TODO: this is so hood, bktypes -> sdktypes -> generic is crazy -// maybe make this some kind of codec/func that can be passed in? -func convertValidatorUpdate[ValidatorUpdateT any]( - u **transition.ValidatorUpdate, -) (ValidatorUpdateT, error) { - var valUpdate ValidatorUpdateT - update := *u - if update == nil { - return valUpdate, ErrUndefinedValidatorUpdate - } - return any(appmodulev2.ValidatorUpdate{ - PubKey: update.Pubkey[:], - PubKeyType: crypto.CometBLSType, - //#nosec:G701 // this is safe. - Power: int64(update.EffectiveBalance.Unwrap()), - }).(ValidatorUpdateT), nil -} - // convertPrepareProposalToSlotData converts a prepare proposal request to // a slot data. func (c *ConsensusEngine[ diff --git a/mod/consensus/pkg/cometbft/types.go b/mod/consensus/pkg/cometbft/types.go index e0d65a452d..654288ef3c 100644 --- a/mod/consensus/pkg/cometbft/types.go +++ b/mod/consensus/pkg/cometbft/types.go @@ -60,8 +60,10 @@ type Middleware[ ProcessProposal( ctx context.Context, req proto.Message, ) (proto.Message, error) - PreBlock(_ context.Context, req proto.Message) error - EndBlock(ctx context.Context) (transition.ValidatorUpdates, error) + FinalizeBlock( + ctx context.Context, + req proto.Message, + ) (transition.ValidatorUpdates, error) } // SlashingInfo is an interface for accessing the slashing info. diff --git a/mod/node-core/go.mod b/mod/node-core/go.mod index 88664fa0f5..c78146edce 100644 --- a/mod/node-core/go.mod +++ b/mod/node-core/go.mod @@ -30,8 +30,8 @@ require ( github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240718074353-1a991cfeed63 github.com/berachain/beacon-kit/mod/cli v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240723155519-565f208d5482 - github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e + github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 + github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 diff --git a/mod/node-core/go.sum b/mod/node-core/go.sum index c5242e1e2d..27b4f112c7 100644 --- a/mod/node-core/go.sum +++ b/mod/node-core/go.sum @@ -78,10 +78,10 @@ github.com/berachain/beacon-kit/mod/cli v0.0.0-20240806160829-cde2d1347e7e h1:sP github.com/berachain/beacon-kit/mod/cli v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:Sm5yEcVlCChGKhzVWYRYbQ8Z6EU9qDRp00UoGlC5HHQ= github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df h1:I0qrcOnLkWjruhCWJMY04Cc9KT78WaY4aXCIoUj9+V4= github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df/go.mod h1:xTHttdHtO63VauLvYve/J3DBLtXJKNk0dw5RhAo636k= -github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240723155519-565f208d5482 h1:o0JB2+luyAGmNF0B2byfAXcAXD/vA2x881miqwyPlAQ= -github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240723155519-565f208d5482/go.mod h1:LuuhwwOod5wfpPJpHhp+QdxkVcNLeadoCSSI32gp6vE= -github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e h1:GTeZshNZaH5MnVhSSGj//vxJfv1kM9d6w2CA7O64gJk= -github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:ZU1bq1BMt6b0kPRAw+A3kP7FlSd5DSQNYePD5qL9zfQ= +github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 h1:LvqiLOa8L5CKacYyphFHv363Y07nH4DJObuik0qSJ8c= +github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:AabKi0yGSwa1FW9fZnuQXJi6OUcUzIWa4o4e8zm/5RM= +github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 h1:VN+glgL0JqQXf/N/8toDa9EnehltgBD4DbsHx65Macc= +github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:A3Ohrylv4Dx2sf8EKJImC7ii091dUaWEAMp/AK5N6+s= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 h1:wVWkiiERY/7kaXvE/VNPPUtYp/l8ky6QSuKM3ThVMXU= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:LiOiqrJhhLH/GPo0XE5fel3EYyi7X6dwBOyTqZakTeQ= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= diff --git a/mod/node-core/pkg/builder/baseapp_options.go b/mod/node-core/pkg/builder/baseapp_options.go index 86d12709ea..1c8013bb75 100644 --- a/mod/node-core/pkg/builder/baseapp_options.go +++ b/mod/node-core/pkg/builder/baseapp_options.go @@ -69,15 +69,6 @@ func WithProcessProposal( } } -// WithPreBlocker sets the pre-blocker to the baseapp. -func WithPreBlocker( - preBlocker sdk.PreBlocker, -) func(bApp *baseapp.BaseApp) { - return func(bApp *baseapp.BaseApp) { - bApp.SetPreBlocker(preBlocker) - } -} - // DefaultBaseappOptions returns the default baseapp options provided by the // Cosmos SDK. func DefaultBaseappOptions( diff --git a/mod/node-core/pkg/builder/builder.go b/mod/node-core/pkg/builder/builder.go index 6f82ec7fdc..a9f4d0f4be 100644 --- a/mod/node-core/pkg/builder/builder.go +++ b/mod/node-core/pkg/builder/builder.go @@ -113,13 +113,12 @@ func (nb *NodeBuilder[NodeT]) Build( // set the application to a new BeaconApp with necessary ABCI handlers nb.node.RegisterApp( runtime.NewBeaconKitApp( - db, traceStore, true, appBuilder, + db, traceStore, true, appBuilder, abciMiddleware, append( DefaultBaseappOptions(appOpts), WithCometParamStore(chainSpec), WithPrepareProposal(consensusEngine.PrepareProposal), WithProcessProposal(consensusEngine.ProcessProposal), - WithPreBlocker(consensusEngine.PreBlock), )..., ), ) diff --git a/mod/node-core/pkg/builder/config.go b/mod/node-core/pkg/builder/config.go index 50e7f23bb0..e050adaa94 100644 --- a/mod/node-core/pkg/builder/config.go +++ b/mod/node-core/pkg/builder/config.go @@ -36,14 +36,8 @@ func DefaultDepInjectConfig() depinject.Config { appconfig.Compose(&appv1alpha1.Config{ Modules: []*appv1alpha1.ModuleConfig{ { - Name: "runtime", - Config: appconfig.WrapAny(&runtimev1alpha1.Module{ - AppName: "BeaconKit", - PreBlockers: []string{}, - BeginBlockers: []string{}, - EndBlockers: []string{beacon.ModuleName}, - InitGenesis: []string{beacon.ModuleName}, - }), + Name: "runtime", + Config: appconfig.WrapAny(&runtimev1alpha1.Module{}), }, { Name: beacon.ModuleName, diff --git a/mod/node-core/pkg/components/module/module.go b/mod/node-core/pkg/components/module/module.go index 9fb02e3db6..25a44f0b19 100644 --- a/mod/node-core/pkg/components/module/module.go +++ b/mod/node-core/pkg/components/module/module.go @@ -26,11 +26,8 @@ import ( "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/registry" "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" - "github.com/berachain/beacon-kit/mod/consensus/pkg/cometbft" - consruntimetypes "github.com/berachain/beacon-kit/mod/consensus/pkg/types" "github.com/berachain/beacon-kit/mod/node-core/pkg/components" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" - "github.com/cosmos/cosmos-sdk/types/module" ) const ( @@ -41,9 +38,7 @@ const ( ) var ( - _ appmodule.AppModule = AppModule{} - _ module.HasABCIGenesis = AppModule{} - _ module.HasABCIEndBlock = AppModule{} + _ appmodule.AppModule = AppModule{} ) // AppModule implements an application module for the beacon module. @@ -113,45 +108,3 @@ func (am AppModule) ExportGenesis( ]{}, ) } - -// InitGenesis initializes the beacon module's state from a provided genesis -// state. -func (am AppModule) InitGenesis( - ctx context.Context, - bz json.RawMessage, -) ([]appmodule.ValidatorUpdate, error) { - return cometbft.NewConsensusEngine[ - *types.AttestationData, - *components.BeaconState, - *types.SlashingInfo, - *consruntimetypes.SlotData[ - *types.AttestationData, - *types.SlashingInfo, - ], - components.StorageBackend, - appmodule.ValidatorUpdate, - ]( - am.ABCIMiddleware, - *am.StorageBackend, - ).InitGenesis(ctx, bz) -} - -// EndBlock returns the validator set updates from the beacon state. -func (am AppModule) EndBlock( - ctx context.Context, -) ([]appmodule.ValidatorUpdate, error) { - return cometbft.NewConsensusEngine[ - *types.AttestationData, - *components.BeaconState, - *types.SlashingInfo, - *consruntimetypes.SlotData[ - *types.AttestationData, - *types.SlashingInfo, - ], - components.StorageBackend, - appmodule.ValidatorUpdate, - ]( - am.ABCIMiddleware, - *am.StorageBackend, - ).EndBlock(ctx) -} diff --git a/mod/node-core/pkg/components/module/proto/buf.gen.gogo.yaml b/mod/node-core/pkg/components/module/proto/buf.gen.gogo.yaml deleted file mode 100644 index 4007bd0c11..0000000000 --- a/mod/node-core/pkg/components/module/proto/buf.gen.gogo.yaml +++ /dev/null @@ -1,34 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# Copyright (c) 2024 Berachain Foundation -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - - -version: v1 -plugins: - - name: gocosmos - out: . - opt: plugins=grpc,Mgoogle/protobuf/duration.proto=github.com/cosmos/gogoproto/types,Mgoogle/protobuf/struct.proto=github.com/cosmos/gogoproto/types,Mgoogle/protobuf/timestamp.proto=github.com/cosmos/gogoproto/types,Mgoogle/protobuf/wrappers.proto=github.com/cosmos/gogoproto/types,Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types,Mcosmos/orm/v1alpha1/orm.proto=cosmossdk.io/api/cosmos/orm/v1alpha1 - - name: grpc-gateway - out: . - opt: logtostderr=true,allow_colon_final_segments=true \ No newline at end of file diff --git a/mod/node-core/pkg/components/module/proto/buf.gen.pulsar.yaml b/mod/node-core/pkg/components/module/proto/buf.gen.pulsar.yaml deleted file mode 100644 index f6dbf7dac3..0000000000 --- a/mod/node-core/pkg/components/module/proto/buf.gen.pulsar.yaml +++ /dev/null @@ -1,42 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# Copyright (c) 2024 Berachain Foundation -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - -version: v1 -managed: - enabled: true - go_package_prefix: - default: cosmossdk.io/api - except: - - buf.build/googleapis/googleapis - - buf.build/cosmos/gogo-proto - - buf.build/cosmos/cosmos-proto - override: -plugins: - - name: go-pulsar - out: ../api - opt: paths=source_relative - - name: go-grpc - out: ../api - opt: paths=source_relative \ No newline at end of file diff --git a/mod/node-core/pkg/components/module/proto/buf.lock b/mod/node-core/pkg/components/module/proto/buf.lock deleted file mode 100644 index 46ffe146c7..0000000000 --- a/mod/node-core/pkg/components/module/proto/buf.lock +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by buf. DO NOT EDIT. -version: v1 -deps: - - remote: buf.build - owner: cosmos - repository: cosmos-proto - commit: 1935555c206d4afb9e94615dfd0fad31 - digest: shake256:c74d91a3ac7ae07d579e90eee33abf9b29664047ac8816500cf22c081fec0d72d62c89ce0bebafc1f6fec7aa5315be72606717740ca95007248425102c365377 - - remote: buf.build - owner: cosmos - repository: cosmos-sdk - commit: eb4a9a3157744366b662ccb9c9dedfa8 - digest: shake256:f982729e74b39afb51d810e43c46c5f0cdc1a4d7f95609810e1cb3cf987fcaf59b4f2692adc3bef127eb7bf2ff7eaece7d52ceb189bc3c3d6712279bffe123a0 - - remote: buf.build - owner: cosmos - repository: gogo-proto - commit: 5e5b9fdd01804356895f8f79a6f1ddc1 - digest: shake256:0b85da49e2e5f9ebc4806eae058e2f56096ff3b1c59d1fb7c190413dd15f45dd456f0b69ced9059341c80795d2b6c943de15b120a9e0308b499e43e4b5fc2952 - - remote: buf.build - owner: googleapis - repository: googleapis - commit: a86849a25cc04f4dbe9b15ddddfbc488 - digest: shake256:e19143328f8cbfe13fc226aeee5e63773ca494693a72740a7560664270039a380d94a1344234b88c7691311460df9a9b1c2982190d0a2612eae80368718e1943 - - remote: buf.build - owner: protocolbuffers - repository: wellknowntypes - commit: 657250e6a39648cbb169d079a60bd9ba - digest: shake256:00de25001b8dd2e29d85fc4bcc3ede7aed886d76d67f5e0f7a9b320b90f871d3eb73507d50818d823a0512f3f8db77a11c043685528403e31ff3fef18323a9fb diff --git a/mod/node-core/pkg/components/module/proto/buf.yaml b/mod/node-core/pkg/components/module/proto/buf.yaml deleted file mode 100644 index e55044d960..0000000000 --- a/mod/node-core/pkg/components/module/proto/buf.yaml +++ /dev/null @@ -1,51 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# Copyright (c) 2024 Berachain Foundation -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - -# This module represents buf.build/berachain/beacon-kit -version: v1 -name: buf.build/berachain/beacon-kit-x-beacon -deps: - - buf.build/cosmos/cosmos-proto - - buf.build/cosmos/gogo-proto - - buf.build/googleapis/googleapis - - buf.build/cosmos/cosmos-sdk - -breaking: - use: - - FILE -lint: - allow_comment_ignores: true - use: - - DEFAULT - - COMMENTS - - FILE_LOWER_SNAKE_CASE - except: - - UNARY_RPC - - COMMENT_FIELD - - SERVICE_SUFFIX - - PACKAGE_VERSION_SUFFIX - - RPC_REQUEST_STANDARD_NAME - - ENUM_NO_ALLOW_ALIAS - - PACKAGE_DIRECTORY_MATCH diff --git a/mod/runtime/go.mod b/mod/runtime/go.mod index 766ddb26de..7333de281a 100644 --- a/mod/runtime/go.mod +++ b/mod/runtime/go.mod @@ -23,6 +23,8 @@ require ( cosmossdk.io/errors v1.0.1 cosmossdk.io/log v1.4.0 cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc + github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 + github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd @@ -31,6 +33,7 @@ require ( github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240806094948-2c4293ef36c4 github.com/cosmos/cosmos-db v1.0.2 github.com/cosmos/cosmos-sdk v0.53.0 + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 github.com/stretchr/testify v1.9.0 ) @@ -41,7 +44,6 @@ require ( cosmossdk.io/math v1.3.0 // indirect cosmossdk.io/x/auth v0.0.0-20240806152830-8fb47b368cd4 // indirect cosmossdk.io/x/bank v0.0.0-20240806152830-8fb47b368cd4 // indirect - cosmossdk.io/x/consensus v0.0.0-20240806152830-8fb47b368cd4 // indirect cosmossdk.io/x/staking v0.0.0-20240806152830-8fb47b368cd4 // indirect cosmossdk.io/x/tx v0.13.4-0.20240623110059-dec2d5583e39 // indirect filippo.io/edwards25519 v1.1.0 // indirect @@ -50,15 +52,20 @@ require ( github.com/DataDog/datadog-go v4.8.3+incompatible // indirect github.com/DataDog/zstd v1.5.6 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect + github.com/VictoriaMetrics/fastcache v1.12.2 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db // indirect + github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df // indirect + github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 // indirect + github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e // indirect github.com/bgentry/speakeasy v0.2.0 // indirect - github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect + github.com/bits-and-blooms/bitset v1.13.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cockroachdb/fifo v0.0.0-20240616162244-4768e80dfb9a // indirect github.com/cockroachdb/pebble v1.1.1 // indirect github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect github.com/cometbft/cometbft-db v0.13.0 // indirect + github.com/consensys/bavard v0.1.13 // indirect + github.com/consensys/gnark-crypto v0.13.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect github.com/cosmos/crypto v0.1.2 // indirect @@ -67,24 +74,33 @@ require ( github.com/cosmos/iavl v1.2.1-0.20240725141113-7adc688cf179 // indirect github.com/cosmos/ics23/go v0.10.0 // indirect github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect + github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect + github.com/crate-crypto/go-kzg-4844 v1.1.0 // indirect github.com/danieljoos/wincred v1.2.1 // indirect + github.com/deckarep/golang-set/v2 v2.6.0 // indirect github.com/dgraph-io/badger/v4 v4.2.0 // indirect github.com/dgraph-io/ristretto v0.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.7.0 // indirect github.com/emicklei/dot v1.6.2 // indirect + github.com/ethereum/c-kzg-4844 v1.0.3 // indirect + github.com/ethereum/go-ethereum v1.14.7 // indirect + github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/ferranbt/fastssz v0.1.4-0.20240629094022-eac385e6ee79 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-kit/kit v0.13.0 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/goccy/go-json v0.10.3 // indirect github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect + github.com/gofrs/flock v0.12.1 // indirect github.com/gogo/googleapis v1.4.1 // indirect github.com/golang/glog v1.2.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect github.com/google/btree v1.1.2 // indirect github.com/google/flatbuffers v24.3.25+incompatible // indirect + github.com/google/gofuzz v1.2.0 // indirect github.com/gorilla/handlers v1.5.2 // indirect github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect @@ -92,10 +108,10 @@ require ( github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect github.com/hashicorp/go-metrics v0.5.3 // indirect - github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/hashicorp/golang-lru v1.0.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/hdevalence/ed25519consensus v0.2.0 // indirect + github.com/holiman/bloomfilter/v2 v2.0.3 // indirect github.com/iancoleman/strcase v0.3.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/karalabe/ssz v0.2.1-0.20240724074312-3d1ff7a6f7c4 // indirect @@ -105,11 +121,13 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/mtibben/percent v0.2.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/onsi/gomega v1.33.1 // indirect + github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/prometheus/client_golang v1.19.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect @@ -118,10 +136,11 @@ require ( github.com/prysmaticlabs/go-bitfield v0.0.0-20240618144021-706c95b2dd15 // indirect github.com/prysmaticlabs/gohashtree v0.0.4-beta.0.20240624100937-73632381301b // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/rs/zerolog v1.33.0 // indirect github.com/sagikazarmark/locafero v0.6.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect + github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/cobra v1.8.1 // indirect @@ -131,6 +150,10 @@ require ( github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect github.com/tendermint/go-amino v0.16.0 // indirect github.com/tidwall/btree v1.7.0 // indirect + github.com/tklauser/go-sysconf v0.3.14 // indirect + github.com/tklauser/numcpus v0.8.0 // indirect + github.com/umbracle/fastrlp v0.1.0 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect github.com/zondax/hid v0.9.2 // indirect github.com/zondax/ledger-go v0.14.3 // indirect gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b // indirect @@ -143,6 +166,7 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gotest.tools/v3 v3.5.1 // indirect pgregory.net/rapid v1.1.0 // indirect + rsc.io/tmplfunc v0.0.3 // indirect sigs.k8s.io/yaml v1.4.0 // indirect ) diff --git a/mod/runtime/go.sum b/mod/runtime/go.sum index 6861416afc..dcba71ab07 100644 --- a/mod/runtime/go.sum +++ b/mod/runtime/go.sum @@ -48,21 +48,33 @@ github.com/DataDog/zstd v1.5.6 h1:LbEglqepa/ipmmQJUDnSsfvA8e8IStVcGaFWDuxvGOY= github.com/DataDog/zstd v1.5.6/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI= +github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= +github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= -github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= +github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg= +github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8= +github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 h1:LvqiLOa8L5CKacYyphFHv363Y07nH4DJObuik0qSJ8c= +github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:AabKi0yGSwa1FW9fZnuQXJi6OUcUzIWa4o4e8zm/5RM= +github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 h1:VN+glgL0JqQXf/N/8toDa9EnehltgBD4DbsHx65Macc= +github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:A3Ohrylv4Dx2sf8EKJImC7ii091dUaWEAMp/AK5N6+s= +github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 h1:wVWkiiERY/7kaXvE/VNPPUtYp/l8ky6QSuKM3ThVMXU= +github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:LiOiqrJhhLH/GPo0XE5fel3EYyi7X6dwBOyTqZakTeQ= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= +github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= +github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= @@ -71,6 +83,8 @@ github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240808182639-7bdbf06a94f2 h1:4 github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240808182639-7bdbf06a94f2/go.mod h1:DUyJJMMuFJ9OZAhnFMLA0KTFGoVw61p8wnqtV3Wgx3c= github.com/bgentry/speakeasy v0.2.0 h1:tgObeVOf8WAvtuAX6DhJ4xks4CFNwPDZiqzGqIHE51E= github.com/bgentry/speakeasy v0.2.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= +github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= github.com/btcsuite/btcd/btcec/v2 v2.3.3/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.5 h1:+wER79R5670vs/ZusMTF1yTcRYE5GUsFbdjdisflzM8= @@ -79,6 +93,7 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6 github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= @@ -114,6 +129,10 @@ github.com/cometbft/cometbft-db v0.13.0 h1:Ea0YyR4phCwdOzFo1IWoN9fZz1DHFh6w59b3T github.com/cometbft/cometbft-db v0.13.0/go.mod h1:KiHYylxxaQrOxdiTOpJMhwiGvOKpiCdaqkAM9JcxcJ8= github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240806094948-2c4293ef36c4 h1:dEOaTNsJrOTOSysBgAE9pK6zeNASUR6gsFW8s0zcRRU= github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240806094948-2c4293ef36c4/go.mod h1:NDFKiBBD8HJC6QQLAoUI99YhsiRZtg2+FJWfk6A6m6o= +github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= +github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= +github.com/consensys/gnark-crypto v0.13.0 h1:VPULb/v6bbYELAPTDFINEVaMTTybV5GLxDdcjnS+4oc= +github.com/consensys/gnark-crypto v0.13.0/go.mod h1:wKqwsieaKPThcFkHe0d0zMsbHEUWFmZcG7KBCse210o= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cosmos/btcutil v1.0.5 h1:t+ZFcX77LpKtDBhjucvnOH8C2l2ioGsBNEQ3jef8xFk= github.com/cosmos/btcutil v1.0.5/go.mod h1:IyB7iuqZMJlthe2tkIFL33xPyzbFYP0XVdS8P5lUPis= @@ -137,6 +156,10 @@ github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980 github.com/cosmos/ledger-cosmos-go v0.13.3 h1:7ehuBGuyIytsXbd4MP43mLeoN2LTOEnk5nvue4rK+yM= github.com/cosmos/ledger-cosmos-go v0.13.3/go.mod h1:HENcEP+VtahZFw38HZ3+LS3Iv5XV6svsnkk9vdJtLr8= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a h1:W8mUrRp6NOVl3J+MYp5kPMoUZPp7aOYHtaua31lwRHg= +github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a/go.mod h1:sTwzHBvIzm2RfVCGNEBZgRyjwK40bVoun3ZnGOCafNM= +github.com/crate-crypto/go-kzg-4844 v1.1.0 h1:EN/u9k2TF6OWSHrCCDBBU6GLNMq88OspHHlMnHfoyU4= +github.com/crate-crypto/go-kzg-4844 v1.1.0/go.mod h1:JolLjpSff1tCCJKaJx4psrlEdlXuJEC996PL3tTAFks= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/danieljoos/wincred v1.2.1 h1:dl9cBrupW8+r5250DYkYxocLeZ1Y4vB1kxgtjxw8GQs= github.com/danieljoos/wincred v1.2.1/go.mod h1:uGaFL9fDn3OLTvzCGulzE+SzjEe5NGlh5FdCcyfPwps= @@ -144,6 +167,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM= +github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg= @@ -169,6 +194,12 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/ethereum/c-kzg-4844 v1.0.3 h1:IEnbOHwjixW2cTvKRUlAAUOeleV7nNM/umJR+qy4WDs= +github.com/ethereum/c-kzg-4844 v1.0.3/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= +github.com/ethereum/go-ethereum v1.14.7 h1:EHpv3dE8evQmpVEQ/Ne2ahB06n2mQptdwqaMNhAT29g= +github.com/ethereum/go-ethereum v1.14.7/go.mod h1:Mq0biU2jbdmKSZoqOj29017ygFrMnB5/Rifwp980W4o= +github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 h1:KrE8I4reeVvf7C1tm8elRjj4BdscTYzz/WAbYyf/JI4= +github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0/go.mod h1:D9AJLVXSyZQXJQVk8oh1EwjISE+sJTn2duYIZC0dy3w= github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= @@ -199,6 +230,9 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= @@ -206,6 +240,8 @@ github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E= +github.com/gofrs/flock v0.12.1/go.mod h1:9zxTsyu5xtJ9DK+1tFZyibEV7y3uwDxPPfbxeeHCoD0= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/googleapis v1.4.1-0.20201022092350-68b0159b7869/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c= @@ -266,6 +302,7 @@ github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/orderedcode v0.0.1 h1:UzfcAexk9Vhv8+9pNOgRu41f16lHq725vPwnSeiG/Us= github.com/google/orderedcode v0.0.1/go.mod h1:iVyU4/qPKHY5h/wSd6rZZCDcLJNxiWO6dvsYES2Sb20= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w= @@ -304,6 +341,8 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU= github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo= +github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= +github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA= github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs= github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= @@ -335,6 +374,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= +github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/linxGnu/grocksdb v1.9.2 h1:O3mzvO0wuzQ9mtlHbDrShixyVjVbmuqTjFrzlf43wZ8= @@ -347,6 +390,9 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/minio/highwayhash v1.0.3 h1:kbnuUMoHYyVl7szWjSxJnxw11k2U709jqFPPmIUyD6Q= github.com/minio/highwayhash v1.0.3/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ= @@ -356,6 +402,9 @@ github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJ github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY= +github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU= +github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -374,6 +423,8 @@ github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a h1:dl github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= @@ -429,6 +480,9 @@ github.com/prysmaticlabs/gohashtree v0.0.4-beta.0.20240624100937-73632381301b h1 github.com/prysmaticlabs/gohashtree v0.0.4-beta.0.20240624100937-73632381301b/go.mod h1:HRuvtXLZ4WkaB1MItToVH2e8ZwKwZPY5/Rcby+CvvLY= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= @@ -445,6 +499,8 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0= github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= @@ -488,9 +544,17 @@ github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2l github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI= github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= +github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU= +github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY= +github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY= +github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= +github.com/umbracle/fastrlp v0.1.0 h1:V0W3f6ZKWqbu1KggdhnRWOi+t7+PfL3VyAffJqayI5s= +github.com/umbracle/fastrlp v0.1.0/go.mod h1:5RHgqiFjd4vLJESMWagP/E7su+5Gzk0iqqmrotR8WdA= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/zondax/hid v0.9.2 h1:WCJFnEDMiqGF64nlZz28E9qLVZ0KSJ7xpc5DLEyma2U= github.com/zondax/hid v0.9.2/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM= github.com/zondax/ledger-go v0.14.3 h1:wEpJt2CEcBJ428md/5MgSLsXLBos98sBOyxNmCjfUCw= @@ -557,6 +621,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -576,9 +641,11 @@ golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -675,5 +742,7 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw= pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= +rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= +rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/mod/runtime/pkg/cosmos/baseapp/abci.go b/mod/runtime/pkg/cosmos/baseapp/abci.go index d723a85c92..f239a12480 100644 --- a/mod/runtime/pkg/cosmos/baseapp/abci.go +++ b/mod/runtime/pkg/cosmos/baseapp/abci.go @@ -37,6 +37,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/gogoproto/proto" + "github.com/sourcegraph/conc/iter" ) func (app *BaseApp) InitChain( @@ -439,14 +440,6 @@ func (app *BaseApp) internalFinalizeBlock( WithHeaderHash(req.Hash)) } - if err := app.preBlock(req); err != nil { - return nil, err - } - - if _, err := app.beginBlock(req); err != nil { - return nil, err - } - // First check for an abort signal after beginBlock, as it's the first place // we spend any significant amount of time. select { @@ -484,12 +477,24 @@ func (app *BaseApp) internalFinalizeBlock( }) } - endBlock, err := app.endBlock(app.finalizeBlockState.Context()) + finalizeBlock, err := app.finalizeBlocker( + app.finalizeBlockState.Context(), + req, + ) + if err != nil { + return nil, err + } + + valUpdates, err := iter.MapErr( + finalizeBlock, + convertValidatorUpdate[abci.ValidatorUpdate], + ) if err != nil { return nil, err } - // check after endBlock if we should abort, to avoid propagating the result + // check after finalizeBlock if we should abort, to avoid propagating the + // result select { case <-ctx.Done(): return nil, ctx.Err() @@ -500,17 +505,13 @@ func (app *BaseApp) internalFinalizeBlock( cp := app.GetConsensusParams(app.finalizeBlockState.Context()) return &abci.FinalizeBlockResponse{ TxResults: txResults, - ValidatorUpdates: endBlock.ValidatorUpdates, + ValidatorUpdates: valUpdates, ConsensusParamUpdates: &cp, }, nil } // FinalizeBlock will execute the block proposal provided by -// RequestFinalizeBlock. Specifically, it will execute an application's -// BeginBlock (if defined), followed -// by the transactions in the proposal, finally followed by the application's -// EndBlock (if defined). -// +// RequestFinalizeBlock. // For each raw transaction, i.e. a byte slice, BaseApp will only execute it if // it adheres to the sdk.Tx interface. Otherwise, the raw transaction will be // skipped. This is to support compatibility with proposers injecting vote diff --git a/mod/runtime/pkg/cosmos/baseapp/baseapp.go b/mod/runtime/pkg/cosmos/baseapp/baseapp.go index ace07b9f5b..c6219fad6b 100644 --- a/mod/runtime/pkg/cosmos/baseapp/baseapp.go +++ b/mod/runtime/pkg/cosmos/baseapp/baseapp.go @@ -30,11 +30,14 @@ import ( "cosmossdk.io/store" storemetrics "cosmossdk.io/store/metrics" storetypes "cosmossdk.io/store/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" abci "github.com/cometbft/cometbft/api/cometbft/abci/v1" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" dbm "github.com/cosmos/cosmos-db" servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/gogoproto/proto" ) type ( @@ -62,12 +65,10 @@ type BaseApp struct { db dbm.DB // common DB backend cms storetypes.CommitMultiStore // Main (uncached) state - initChainer sdk.InitChainer // ABCI InitChain handler - preBlocker sdk.PreBlocker // logic to run before BeginBlocker - beginBlocker sdk.BeginBlocker // (legacy ABCI) BeginBlock handler - endBlocker sdk.EndBlocker // (legacy ABCI) EndBlock handler - processProposal sdk.ProcessProposalHandler // ABCI ProcessProposal handler - prepareProposal sdk.PrepareProposalHandler // ABCI PrepareProposal handler + initChainer sdk.InitChainer // ABCI InitChain handler + finalizeBlocker func(context.Context, proto.Message) (transition.ValidatorUpdates, error) // (legacy ABCI) EndBlock handler + processProposal sdk.ProcessProposalHandler // ABCI ProcessProposal handler + prepareProposal sdk.PrepareProposalHandler // ABCI PrepareProposal handler // volatile states: // @@ -390,42 +391,6 @@ func (app *BaseApp) validateFinalizeBlockHeight( return nil } -func (app *BaseApp) preBlock( - req *abci.FinalizeBlockRequest, -) error { - if app.preBlocker != nil { - ctx := app.finalizeBlockState.Context() - if err := app.preBlocker(ctx, req); err != nil { - return err - } - // ConsensusParams can change in preblocker, so we need to - // write the consensus parameters in store to context - ctx = ctx.WithConsensusParams(app.GetConsensusParams(ctx)) - app.finalizeBlockState.SetContext(ctx) - } - return nil -} - -func (app *BaseApp) beginBlock( - _ *abci.FinalizeBlockRequest, -) (sdk.BeginBlock, error) { - if app.beginBlocker != nil { - return app.beginBlocker(app.finalizeBlockState.Context()) - } - - return sdk.BeginBlock{}, nil -} - -// endBlock is an application-defined function that is called after transactions -// have been processed in FinalizeBlock. -func (app *BaseApp) endBlock(_ context.Context) (sdk.EndBlock, error) { - if app.endBlocker != nil { - return app.endBlocker(app.finalizeBlockState.Context()) - } - - return sdk.EndBlock{}, nil -} - // Close is called in start cmd to gracefully cleanup resources. func (app *BaseApp) Close() error { var errs []error @@ -440,3 +405,23 @@ func (app *BaseApp) Close() error { return errors.Join(errs...) } + +// convertValidatorUpdate abstracts the conversion of a +// transition.ValidatorUpdate to an appmodulev2.ValidatorUpdate. +// TODO: this is so hood, bktypes -> sdktypes -> generic is crazy +// maybe make this some kind of codec/func that can be passed in? +func convertValidatorUpdate[ValidatorUpdateT any]( + u **transition.ValidatorUpdate, +) (ValidatorUpdateT, error) { + var valUpdate ValidatorUpdateT + update := *u + if update == nil { + return valUpdate, errors.New("undefined validator update") + } + return any(abci.ValidatorUpdate{ + PubKeyBytes: update.Pubkey[:], + PubKeyType: crypto.CometBLSType, + //#nosec:G701 // this is safe. + Power: int64(update.EffectiveBalance.Unwrap()), + }).(ValidatorUpdateT), nil +} diff --git a/mod/runtime/pkg/cosmos/baseapp/options.go b/mod/runtime/pkg/cosmos/baseapp/options.go index e3e2bcd8f8..640909efe4 100644 --- a/mod/runtime/pkg/cosmos/baseapp/options.go +++ b/mod/runtime/pkg/cosmos/baseapp/options.go @@ -27,7 +27,9 @@ import ( pruningtypes "cosmossdk.io/store/pruning/types" storetypes "cosmossdk.io/store/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/gogoproto/proto" ) // File for storing in-package BaseApp optional functions, @@ -109,16 +111,10 @@ func (app *BaseApp) SetInitChainer(initChainer sdk.InitChainer) { app.initChainer = initChainer } -func (app *BaseApp) SetPreBlocker(preBlocker sdk.PreBlocker) { - app.preBlocker = preBlocker -} - -func (app *BaseApp) SetBeginBlocker(beginBlocker sdk.BeginBlocker) { - app.beginBlocker = beginBlocker -} - -func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) { - app.endBlocker = endBlocker +func (app *BaseApp) SetFinalizeBlocker( + finalizeBlocker func(context.Context, proto.Message) (transition.ValidatorUpdates, error), +) { + app.finalizeBlocker = finalizeBlocker } // SetProcessProposal sets the process proposal function for the BaseApp. diff --git a/mod/runtime/pkg/cosmos/runtime/app.go b/mod/runtime/pkg/cosmos/runtime/app.go index e404aaddec..b6029cc8ce 100644 --- a/mod/runtime/pkg/cosmos/runtime/app.go +++ b/mod/runtime/pkg/cosmos/runtime/app.go @@ -21,40 +21,37 @@ package runtime import ( + "context" "encoding/json" - "fmt" + "errors" "io" runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/cosmos/baseapp" abci "github.com/cometbft/cometbft/api/cometbft/abci/v1" dbm "github.com/cosmos/cosmos-db" servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/gogoproto/proto" + "github.com/sourcegraph/conc/iter" ) var _ servertypes.Application = &App{} -// App is a wrapper around BaseApp and ModuleManager that can be used in hybrid -// app.go/app config scenarios or directly as a servertypes.Application -// instance. -// To get an instance of *App, *AppBuilder must be requested as a dependency -// in a container which declares the runtime module and the AppBuilder.Build() -// method must be called. -// // App can be used to create a hybrid app.go setup where some configuration is // done declaratively with an app config and the rest of it is done the old way. // See simapp/app.go for an example of this setup. type App struct { *baseapp.BaseApp - ModuleManager *module.Manager - config *runtimev1alpha1.Module - storeKeys []storetypes.StoreKey - logger log.Logger + Middleware Middleware + config *runtimev1alpha1.Module + storeKeys []storetypes.StoreKey + logger log.Logger // initChainer is the init chainer function defined by the app config. // this is only required if the chain wants to add special InitChainer // logic. @@ -67,9 +64,12 @@ func NewBeaconKitApp( traceStore io.Writer, loadLatest bool, appBuilder *AppBuilder, + middleware Middleware, baseAppOptions ...func(*baseapp.BaseApp), ) *App { - app := &App{} + app := &App{ + Middleware: middleware, + } // Build the runtime.App using the app builder. app = appBuilder.Build(db, traceStore, baseAppOptions...) @@ -82,63 +82,10 @@ func NewBeaconKitApp( return app } -// RegisterModules registers the provided modules with the module manager and -// the basic module manager. This is the primary hook for integrating with -// modules which are not registered using the app config. -func (a *App) RegisterModules(modules ...module.AppModule) error { - for _, appModule := range modules { - name := appModule.Name() - if _, ok := a.ModuleManager.Modules[name]; ok { - return fmt.Errorf("AppModule named %q already exists", name) - } - } - - return nil -} - -// RegisterStores registers the provided store keys. -// This method should only be used for registering extra stores -// which is necessary for modules that not registered using the app config. -// To be used in combination of RegisterModules. -func (a *App) RegisterStores(keys ...storetypes.StoreKey) error { - a.MountStores(keys...) - - return nil -} - // Load finishes all initialization operations and loads the app. func (a *App) Load(loadLatest bool) error { - if len(a.config.GetInitGenesis()) != 0 { - a.ModuleManager.SetOrderInitGenesis(a.config.GetInitGenesis()...) - if a.initChainer == nil { - a.SetInitChainer(a.InitChainer) - } - } - - if len(a.config.GetExportGenesis()) != 0 { - a.ModuleManager.SetOrderExportGenesis(a.config.GetExportGenesis()...) - } else if len(a.config.GetInitGenesis()) != 0 { - a.ModuleManager.SetOrderExportGenesis(a.config.GetInitGenesis()...) - } - - if len(a.config.GetPreBlockers()) != 0 { - a.ModuleManager.SetOrderPreBlockers(a.config.GetPreBlockers()...) - a.SetPreBlocker(a.PreBlocker) - } - - if len(a.config.GetBeginBlockers()) != 0 { - a.ModuleManager.SetOrderBeginBlockers(a.config.GetBeginBlockers()...) - a.SetBeginBlocker(a.BeginBlocker) - } - - if len(a.config.GetEndBlockers()) != 0 { - a.ModuleManager.SetOrderEndBlockers(a.config.GetEndBlockers()...) - a.SetEndBlocker(a.EndBlocker) - } - - if len(a.config.GetOrderMigrations()) != 0 { - a.ModuleManager.SetOrderMigrations(a.config.GetOrderMigrations()...) - } + a.SetInitChainer(a.InitChainer) + a.SetFinalizeBlocker(a.FinalizeBlocker) if loadLatest { if err := a.LoadLatestVersion(); err != nil { @@ -149,35 +96,12 @@ func (a *App) Load(loadLatest bool) error { return nil } -// PreBlocker application updates every pre block. -func (a *App) PreBlocker(ctx sdk.Context, _ *abci.FinalizeBlockRequest) error { - return a.ModuleManager.PreBlock(ctx) -} - -// BeginBlocker application updates every begin block. -func (a *App) BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error) { - return a.ModuleManager.BeginBlock(ctx) -} - -// EndBlocker application updates every end block. -func (a *App) EndBlocker(ctx sdk.Context) (sdk.EndBlock, error) { - return a.ModuleManager.EndBlock(ctx) -} - -// Precommiter application updates every commit. -func (a *App) Precommiter(ctx sdk.Context) { - err := a.ModuleManager.Precommit(ctx) - if err != nil { - panic(err) - } -} - -// PrepareCheckStater application updates every commit. -func (a *App) PrepareCheckStater(ctx sdk.Context) { - err := a.ModuleManager.PrepareCheckState(ctx) - if err != nil { - panic(err) - } +// FinalizeBlocker application updates every end block. +func (a *App) FinalizeBlocker( + ctx context.Context, + req proto.Message, +) (transition.ValidatorUpdates, error) { + return a.Middleware.FinalizeBlock(ctx, req) } // InitChainer initializes the chain. @@ -189,10 +113,49 @@ func (a *App) InitChainer( if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil { return nil, err } - return a.ModuleManager.InitGenesis(ctx, genesisState) + valUpdates, err := a.Middleware.InitGenesis( + ctx, + []byte(genesisState["beacon"]), + ) + if err != nil { + return nil, err + } + + convertedValUpdates, err := iter.MapErr( + valUpdates, + convertValidatorUpdate[abci.ValidatorUpdate], + ) + if err != nil { + return nil, err + } + + return &abci.InitChainResponse{ + Validators: convertedValUpdates, + }, nil + } // LoadHeight loads a particular height. func (a *App) LoadHeight(height int64) error { return a.LoadVersion(height) } + +// convertValidatorUpdate abstracts the conversion of a +// transition.ValidatorUpdate to an appmodulev2.ValidatorUpdate. +// TODO: this is so hood, bktypes -> sdktypes -> generic is crazy +// maybe make this some kind of codec/func that can be passed in? +func convertValidatorUpdate[ValidatorUpdateT any]( + u **transition.ValidatorUpdate, +) (ValidatorUpdateT, error) { + var valUpdate ValidatorUpdateT + update := *u + if update == nil { + return valUpdate, errors.New("undefined validator update") + } + return any(abci.ValidatorUpdate{ + PubKeyBytes: update.Pubkey[:], + PubKeyType: crypto.CometBLSType, + //#nosec:G701 // this is safe. + Power: int64(update.EffectiveBalance.Unwrap()), + }).(ValidatorUpdateT), nil +} diff --git a/mod/runtime/pkg/cosmos/runtime/builder.go b/mod/runtime/pkg/cosmos/runtime/builder.go index 978ef694f0..5f9f5e4b7d 100644 --- a/mod/runtime/pkg/cosmos/runtime/builder.go +++ b/mod/runtime/pkg/cosmos/runtime/builder.go @@ -32,7 +32,8 @@ import ( // (as *AppBuilder) which can be used to create an app which is compatible with // the existing app.go initialization conventions. type AppBuilder struct { - app *App + app *App + Middleware Middleware } // Build builds an *App instance. diff --git a/mod/runtime/pkg/cosmos/runtime/export.go b/mod/runtime/pkg/cosmos/runtime/export.go index 9d6cc434fb..768b0216cd 100644 --- a/mod/runtime/pkg/cosmos/runtime/export.go +++ b/mod/runtime/pkg/cosmos/runtime/export.go @@ -21,7 +21,6 @@ package runtime import ( - "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" cmttypes "github.com/cometbft/cometbft/types" servertypes "github.com/cosmos/cosmos-sdk/server/types" @@ -48,26 +47,26 @@ func (app *App) ExportAppStateAndValidators( panic("not supported, just look at the genesis file u goofy") } - genState, err := app.ModuleManager.ExportGenesisForModules( - ctx, - modulesToExport, - ) - if err != nil { - return servertypes.ExportedApp{}, err - } + // genState, err := app.ModuleManager.ExportGenesisForModules( + // ctx, + // modulesToExport, + // ) + // if err != nil { + // return servertypes.ExportedApp{}, err + // } - appState, err := json.MarshalIndent(genState, "", " ") - if err != nil { - return servertypes.ExportedApp{}, err - } + // appState, err := json.MarshalIndent(genState, "", " ") + // if err != nil { + // return servertypes.ExportedApp{}, err + // } // TODO: Pull these in from the BeaconKeeper, should be easy. validators := []cmttypes.GenesisValidator(nil) return servertypes.ExportedApp{ - AppState: appState, + AppState: nil, Validators: validators, Height: height, ConsensusParams: app.BaseApp.GetConsensusParams(ctx), - }, err + }, nil } diff --git a/mod/runtime/pkg/cosmos/runtime/module.go b/mod/runtime/pkg/cosmos/runtime/module.go index 75b673f536..11e0a96ca8 100644 --- a/mod/runtime/pkg/cosmos/runtime/module.go +++ b/mod/runtime/pkg/cosmos/runtime/module.go @@ -31,7 +31,6 @@ import ( "cosmossdk.io/depinject/appconfig" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types/module" ) @@ -51,7 +50,6 @@ func init() { appconfig.RegisterModule(&runtimev1alpha1.Module{}, appconfig.Provide( ProvideApp, - codec.ProvideAddressCodec, ProvideKVStoreKey, ProvideEnvironment, ProvideModuleManager, @@ -60,29 +58,27 @@ func init() { ) } -func ProvideApp() ( +func ProvideApp(middleware Middleware) ( *AppBuilder, appmodule.AppModule, error, ) { - app := &App{} - return &AppBuilder{app}, appModule{app}, nil + app := &App{Middleware: middleware} + return &AppBuilder{app: app}, appModule{app}, nil } type AppInputs struct { depinject.In - Logger log.Logger - Config *runtimev1alpha1.Module - AppBuilder *AppBuilder - ModuleManager *module.Manager + Logger log.Logger + Config *runtimev1alpha1.Module + AppBuilder *AppBuilder } func SetupAppBuilder(inputs AppInputs) { app := inputs.AppBuilder.app app.config = inputs.Config app.logger = inputs.Logger - app.ModuleManager = inputs.ModuleManager } func registerStoreKey(wrapper *AppBuilder, key storetypes.StoreKey) { diff --git a/mod/node-core/pkg/components/module/proto/module/v1alpha1/module.proto b/mod/runtime/pkg/cosmos/runtime/types.go similarity index 57% rename from mod/node-core/pkg/components/module/proto/module/v1alpha1/module.proto rename to mod/runtime/pkg/cosmos/runtime/types.go index 2ba86a5b7f..4960140bef 100644 --- a/mod/node-core/pkg/components/module/proto/module/v1alpha1/module.proto +++ b/mod/runtime/pkg/cosmos/runtime/types.go @@ -18,13 +18,36 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -syntax = "proto3"; +package runtime -package beacon.module.v1alpha1; +import ( + "context" -import "cosmos/app/v1alpha1/module.proto"; + ctypes "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" + "github.com/berachain/beacon-kit/mod/consensus/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" + "github.com/cosmos/gogoproto/proto" +) -// Module is the config object of the evm module. -message Module { - option (cosmos.app.v1alpha1.module) = {go_import: "github.com/berachain/beacon-kit/mod/node-core/pkg/components/module"}; +type Middleware interface { + InitGenesis( + ctx context.Context, + bz []byte, + ) (transition.ValidatorUpdates, error) + + PrepareProposal( + ctx context.Context, + slotData *types.SlotData[ + *ctypes.AttestationData, + *ctypes.SlashingInfo], + ) ([]byte, []byte, error) + + ProcessProposal( + ctx context.Context, + req proto.Message, + ) (proto.Message, error) + + FinalizeBlock( + ctx context.Context, req proto.Message, + ) (transition.ValidatorUpdates, error) } diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index ff73601395..239261c03d 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -208,28 +208,11 @@ func (*ABCIMiddleware[ /* FinalizeBlock */ /* -------------------------------------------------------------------------- */ -// PreBlock is called by the base app before the block is finalized. It -// is responsible for aggregating oracle data from each validator and writing -// the oracle data to the store. -func (h *ABCIMiddleware[ - _, _, _, _, _, _, _, _, -]) PreBlock( - _ context.Context, req proto.Message, -) error { - abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) - if !ok { - return ErrInvalidFinalizeBlockRequestType - } - h.req = abciReq - - return nil -} - // EndBlock returns the validator set updates from the beacon state. func (h *ABCIMiddleware[ _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, -]) EndBlock( - ctx context.Context, +]) FinalizeBlock( + ctx context.Context, req proto.Message, ) (transition.ValidatorUpdates, error) { var ( sidecarsResp *asynctypes.Message[BlobSidecarsT] @@ -238,13 +221,17 @@ func (h *ABCIMiddleware[ blobs BlobSidecarsT err error ) + abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) + if !ok { + return nil, ErrInvalidFinalizeBlockRequestType + } blk, blobs, err = encoding. ExtractBlobsAndBlockFromRequest[BeaconBlockT, BlobSidecarsT]( - h.req, + abciReq, BeaconBlockTxIndex, BlobSidecarsTxIndex, h.chainSpec.ActiveForkVersionForSlot( - math.Slot(h.req.Height), + math.Slot(abciReq.Height), )) if err != nil { // If we don't have a block, we can't do anything. diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 95a7300665..62856959f9 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -30,7 +30,6 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" - cmtabci "github.com/cometbft/cometbft/abci/types" ) // ABCIMiddleware is a middleware between ABCI and the validator logic. @@ -70,9 +69,6 @@ type ABCIMiddleware[ metrics *ABCIMiddlewareMetrics // logger is the logger for the middleware. logger log.Logger[any] - - // TODO: this is a temporary hack. - req *cmtabci.FinalizeBlockRequest } // NewABCIMiddleware creates a new instance of the Handler struct. From 64212d224414b78963c2953d9a34ddcb55f7232e Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 16:36:33 -0400 Subject: [PATCH 35/75] tidy --- mod/execution/go.mod | 6 ++++-- mod/execution/go.sum | 10 ++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mod/execution/go.mod b/mod/execution/go.mod index d163459091..5227221e2f 100644 --- a/mod/execution/go.mod +++ b/mod/execution/go.mod @@ -2,12 +2,14 @@ module github.com/berachain/beacon-kit/mod/execution go 1.22.5 +replace github.com/berachain/beacon-kit/mod/async => ../async + require ( github.com/berachain/beacon-kit/mod/async v0.0.0-20240624204855-d8809d5c8588 github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e - github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/hashicorp/golang-lru/v2 v2.0.7 diff --git a/mod/execution/go.sum b/mod/execution/go.sum index be0fdae574..21c981ca77 100644 --- a/mod/execution/go.sum +++ b/mod/execution/go.sum @@ -8,18 +8,16 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624204855-d8809d5c8588 h1:i2IzAwiYKcYsA/tjQtiLH1tx80RoQ1CvfP9pBZ2JtTg= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624204855-d8809d5c8588/go.mod h1:tzZ5Oyg7BgCViZBkqDwHmfKPNI66xONwE5kWd1P2v7o= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 h1:wVWkiiERY/7kaXvE/VNPPUtYp/l8ky6QSuKM3ThVMXU= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:LiOiqrJhhLH/GPo0XE5fel3EYyi7X6dwBOyTqZakTeQ= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd h1:jD/ggR959ZX+lqxsMzoRJzrGvFK7PI6UmgnRwOTh4S4= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:iXa+Q+i0q+GCpLzkusulO57K5vlkDgM77jtfMr3QdFA= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c h1:7f9dLYGOCMoV7LxT6YRmVSWLTPbGTTcxDPLPLvHGrOk= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:nFybcw/ZhJ6Gu66dna301W2I7u61skm2HfHxQmdR68Q= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 h1:hk8N7Q1CCKMW/05pRu5rbfRnbTUou5TjULaeeRtbU+E= github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:7kNnd9rhYjyZJHuXs/ku5drL9EMM64ekJVR181fGmbM= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= From fd10d2512b9f1d7f4899208f06e59f80addb9869 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 9 Aug 2024 16:59:47 -0400 Subject: [PATCH 36/75] tidy --- mod/beacon/go.mod | 2 +- mod/beacon/go.sum | 8 ++------ mod/node-core/go.mod | 11 +++++------ mod/node-core/go.sum | 20 ++++---------------- mod/runtime/go.mod | 5 ++--- mod/runtime/go.sum | 10 ++++------ 6 files changed, 18 insertions(+), 38 deletions(-) diff --git a/mod/beacon/go.mod b/mod/beacon/go.mod index 52a765a7b5..83704ae40e 100644 --- a/mod/beacon/go.mod +++ b/mod/beacon/go.mod @@ -11,7 +11,7 @@ replace ( require ( github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720 github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 golang.org/x/sync v0.8.0 diff --git a/mod/beacon/go.sum b/mod/beacon/go.sum index d327f5e03a..40895196cc 100644 --- a/mod/beacon/go.sum +++ b/mod/beacon/go.sum @@ -10,16 +10,12 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= -github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 h1:rVltwwt4JAhpvqeRDZ8J07e9XM+jzfDCJEghPA4r5lg= -github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720/go.mod h1:kGxAesqTkt75+QYQ73O9I5PqAicSL9JGh5wypiWgb8I= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd h1:jD/ggR959ZX+lqxsMzoRJzrGvFK7PI6UmgnRwOTh4S4= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:iXa+Q+i0q+GCpLzkusulO57K5vlkDgM77jtfMr3QdFA= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720 h1:qfFjDx7w3uU+zMw5HrghiKPZunxapgCiNHI1XqqtgmA= github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 h1:hk8N7Q1CCKMW/05pRu5rbfRnbTUou5TjULaeeRtbU+E= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:7kNnd9rhYjyZJHuXs/ku5drL9EMM64ekJVR181fGmbM= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= diff --git a/mod/node-core/go.mod b/mod/node-core/go.mod index 9b6c7e5bb8..13fdaa05a8 100644 --- a/mod/node-core/go.mod +++ b/mod/node-core/go.mod @@ -15,6 +15,7 @@ replace ( github.com/berachain/beacon-kit/mod/node-api/engines => ../node-api/engines github.com/berachain/beacon-kit/mod/payload => ../payload github.com/berachain/beacon-kit/mod/primitives => ../primitives + github.com/berachain/beacon-kit/mod/runtime => ../runtime github.com/berachain/beacon-kit/mod/state-transition => ../state-transition github.com/berachain/beacon-kit/mod/storage => ../storage github.com/cosmos/cosmos-sdk => github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240808182639-7bdbf06a94f2 @@ -30,17 +31,15 @@ require ( github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 - github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 - github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 + github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/execution v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd + github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720 github.com/berachain/beacon-kit/mod/node-api v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/node-api/engines v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240809203305-99679c55701d github.com/berachain/beacon-kit/mod/runtime v0.0.0-20240809202957-3e3f169ad720 github.com/berachain/beacon-kit/mod/state-transition v0.0.0-20240717225334-64ec6650da31 github.com/berachain/beacon-kit/mod/storage v0.0.0-20240806160829-cde2d1347e7e @@ -108,7 +107,7 @@ require ( github.com/DataDog/zstd v1.5.6 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/berachain/beacon-kit/mod/async v0.0.0-20240807213340-5779c7a563cd + github.com/berachain/beacon-kit/mod/async v0.0.0-20240809203305-99679c55701d github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd // indirect github.com/bgentry/speakeasy v0.2.0 // indirect github.com/bits-and-blooms/bitset v1.13.0 // indirect diff --git a/mod/node-core/go.sum b/mod/node-core/go.sum index a7ff75bcaf..8a19a93dfb 100644 --- a/mod/node-core/go.sum +++ b/mod/node-core/go.sum @@ -82,28 +82,16 @@ github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:AabKi0yGSwa1FW9fZnuQXJi6OUcUzIWa4o4e8zm/5RM= github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 h1:VN+glgL0JqQXf/N/8toDa9EnehltgBD4DbsHx65Macc= github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:A3Ohrylv4Dx2sf8EKJImC7ii091dUaWEAMp/AK5N6+s= -github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df h1:yZtRZBkhDJ2xMlMLx5DdUFtrtH9iWyPNl4HSW2uJyb8= -github.com/berachain/beacon-kit/mod/da v0.0.0-20240705193247-d464364483df/go.mod h1:iGTR+iYsqpj79WLX1RUVbV7hDs0rQtr+Iqa/VkAF4x8= -github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 h1:wVWkiiERY/7kaXvE/VNPPUtYp/l8ky6QSuKM3ThVMXU= -github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:LiOiqrJhhLH/GPo0XE5fel3EYyi7X6dwBOyTqZakTeQ= +github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 h1:rVltwwt4JAhpvqeRDZ8J07e9XM+jzfDCJEghPA4r5lg= +github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720/go.mod h1:kGxAesqTkt75+QYQ73O9I5PqAicSL9JGh5wypiWgb8I= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720 h1:qfFjDx7w3uU+zMw5HrghiKPZunxapgCiNHI1XqqtgmA= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ft214cxJaqrRPOuAjpYwgA9AOElJnHrDZZEQ0jZPWwQ= -github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df h1:fLL+7ZZcbVOmE3XE0o+ZGS8zyPLjki7LrZAsXpcG4Sc= -github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df/go.mod h1:wbSa9W1CDDzR9AptQfYf/16bWqktaIQIZdJsuKWeqC8= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 h1:hk8N7Q1CCKMW/05pRu5rbfRnbTUou5TjULaeeRtbU+E= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:7kNnd9rhYjyZJHuXs/ku5drL9EMM64ekJVR181fGmbM= -github.com/berachain/beacon-kit/mod/runtime v0.0.0-20240809202957-3e3f169ad720 h1:dSBzTgN/h5WYNVs7Pbo6ZEkHecFUm0miGd8w+XT3BVA= -github.com/berachain/beacon-kit/mod/runtime v0.0.0-20240809202957-3e3f169ad720/go.mod h1:DwPFy0Q9D9zFaeXOrBcGRC1vHmbk4SH1ujT5xFXASd4= -github.com/berachain/beacon-kit/mod/state-transition v0.0.0-20240717225334-64ec6650da31 h1:1bJbJcoksyXfYMiga8YxPnkVKqT1lKwym/8kZnEPz58= -github.com/berachain/beacon-kit/mod/state-transition v0.0.0-20240717225334-64ec6650da31/go.mod h1:sIzib45R7B9Q99yvsYUcj2xJZPBpe3J9JbcBDMZNp7E= -github.com/berachain/beacon-kit/mod/storage v0.0.0-20240806160829-cde2d1347e7e h1:eIHdeGNL87GlWxFqAgsuUZpA/EBGvaOWXFMDuXSJgjU= -github.com/berachain/beacon-kit/mod/storage v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:1Hti34fIHtniBapeikvYF3KyA038+6If8BRkRKqWUy4= github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240808182639-7bdbf06a94f2 h1:4qwOPga+dKeDelSJ6pseasQq6fcjd7iXhah0y7enuco= github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240808182639-7bdbf06a94f2/go.mod h1:DUyJJMMuFJ9OZAhnFMLA0KTFGoVw61p8wnqtV3Wgx3c= github.com/bgentry/speakeasy v0.2.0 h1:tgObeVOf8WAvtuAX6DhJ4xks4CFNwPDZiqzGqIHE51E= diff --git a/mod/runtime/go.mod b/mod/runtime/go.mod index 7e0a2aec98..8f016839f6 100644 --- a/mod/runtime/go.mod +++ b/mod/runtime/go.mod @@ -23,8 +23,8 @@ require ( cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018 - github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c + github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 + github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 github.com/cometbft/cometbft v1.0.0-rc1.0.20240806094948-2c4293ef36c4 @@ -32,7 +32,6 @@ require ( github.com/cosmos/cosmos-db v1.0.2 github.com/cosmos/cosmos-sdk v0.53.0 github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 - github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 github.com/stretchr/testify v1.9.0 ) diff --git a/mod/runtime/go.sum b/mod/runtime/go.sum index 690335a4f1..08e3f5aa8c 100644 --- a/mod/runtime/go.sum +++ b/mod/runtime/go.sum @@ -61,8 +61,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624011057-b0afb8163f14 h1:uGcy4mqiyDzjImZsrxjU3U5NKnXpi+2s1cO0cz1LGNY= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240624011057-b0afb8163f14/go.mod h1:tzZ5Oyg7BgCViZBkqDwHmfKPNI66xONwE5kWd1P2v7o= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8= github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240809163303-a4ebb22fd018 h1:LvqiLOa8L5CKacYyphFHv363Y07nH4DJObuik0qSJ8c= @@ -71,12 +69,12 @@ github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb2 github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240809163303-a4ebb22fd018/go.mod h1:A3Ohrylv4Dx2sf8EKJImC7ii091dUaWEAMp/AK5N6+s= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 h1:wVWkiiERY/7kaXvE/VNPPUtYp/l8ky6QSuKM3ThVMXU= github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:LiOiqrJhhLH/GPo0XE5fel3EYyi7X6dwBOyTqZakTeQ= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df h1:6MJllcmMFt6dtvftM5zmdl1WVDpqZkNy3hFXVZtNV0s= -github.com/berachain/beacon-kit/mod/errors v0.0.0-20240705193247-d464364483df/go.mod h1:yRD7rmnyaaqgq/6+eIVqvSkFJXuLXpBddUu59HUOrtc= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= +github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e h1:0/FDBXtagMkpta/f4J2uAah2NM1G+0dqxngzMzrmbw4= github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c h1:7f9dLYGOCMoV7LxT6YRmVSWLTPbGTTcxDPLPLvHGrOk= -github.com/berachain/beacon-kit/mod/log v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:nFybcw/ZhJ6Gu66dna301W2I7u61skm2HfHxQmdR68Q= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= +github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd h1:QHAukFEWl62kscPjRvpqfxCPvd3RSg8cD4mw8qIBhU4= github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ft214cxJaqrRPOuAjpYwgA9AOElJnHrDZZEQ0jZPWwQ= github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240808182639-7bdbf06a94f2 h1:4qwOPga+dKeDelSJ6pseasQq6fcjd7iXhah0y7enuco= From 3bd43d6e3498f720c4f57b3d6172afb06002fb3b Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 16:12:33 -0400 Subject: [PATCH 37/75] future + modular refactor --- mod/async/pkg/dispatcher/dispatcher.go | 33 ++++---- mod/async/pkg/dispatcher/types.go | 6 +- mod/async/pkg/messaging/assert.go | 14 ---- mod/async/pkg/messaging/publisher.go | 2 +- mod/async/pkg/messaging/route.go | 58 ++++++++------ mod/async/pkg/server/event.go | 2 +- mod/async/pkg/server/msg.go | 8 +- mod/async/pkg/types/future.go | 75 ++++++++++++++++++ mod/async/pkg/types/message.go | 68 +++++++++------- mod/async/pkg/types/notifiers.go | 10 +-- mod/beacon/blockchain/service.go | 18 ++--- mod/beacon/validator/service.go | 17 ++-- mod/da/pkg/da/service.go | 12 +-- mod/node-core/pkg/components/dispatcher.go | 24 ++---- mod/node-core/pkg/components/events.go | 13 +-- mod/node-core/pkg/components/messages.go | 57 +++++--------- mod/node-core/pkg/components/types.go | 51 ++++-------- mod/runtime/pkg/middleware/abci.go | 92 +++++++++++----------- 18 files changed, 303 insertions(+), 257 deletions(-) create mode 100644 mod/async/pkg/types/future.go diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 517bb8c543..7fb3ebb769 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -60,34 +60,36 @@ func (d *Dispatcher) Start(ctx context.Context) error { // PublishEvent dispatches the given event to the event server. // It will error if the type is inconsistent with the publisher // registered for the given eventID. -func (d *Dispatcher) PublishEvent(event types.MessageI) error { +func (d *Dispatcher) PublishEvent(event types.BaseMessage) error { return d.eventServer.Publish(event) } // SendRequest dispatches the given request to the message server. // It will error if the and types are inconsistent with the // route registered for the given messageID. -func (d *Dispatcher) SendRequest(req types.MessageI, resp any) error { - return d.msgServer.Request(req, resp) +func (d *Dispatcher) SendRequest(req types.BaseMessage, future any) error { + return d.msgServer.Request(req, future) } // SendResponse dispatches the given response to the message server. // It will error if the type is inconsistent with the route registered // for the given messageID. -func (d *Dispatcher) SendResponse(resp types.MessageI) error { +func (d *Dispatcher) SendResponse(resp types.BaseMessage) error { return d.msgServer.Respond(resp) } // ============================== Events =================================== -// RegisterPublisher registers the given publisher with the given eventID. +// RegisterPublishers registers the given publisher with the given eventID. // Any subsequent events with dispatched to this Dispatcher must be // consistent with the type expected by . -func (d *Dispatcher) RegisterPublisher( - eventID types.MessageID, publisher types.Publisher, +func (d *Dispatcher) RegisterPublishers( + publishers ...types.Publisher, ) { - d.logger.Info("Publisher registered", "eventID", eventID) - d.eventServer.RegisterPublisher(eventID, publisher) + for _, publisher := range publishers { + d.logger.Info("Publisher registered", "eventID", publisher.EventID()) + d.eventServer.RegisterPublisher(publisher.EventID(), publisher) + } } // Subscribe subscribes the given channel to the event with the given . @@ -108,14 +110,17 @@ func (d *Dispatcher) RegisterMsgReceiver( return d.msgServer.RegisterReceiver(messageID, ch) } -// RegisterRoute registers the given route with the given messageID. +// RegisterRoutes registers the given route with the given messageID. // Any subsequent messages with sent to this Dispatcher must be // consistent with the type expected by . -func (d *Dispatcher) RegisterRoute( - messageID types.MessageID, route types.MessageRoute, +func (d *Dispatcher) RegisterRoutes( + routes ...types.MessageRoute, ) error { - d.logger.Info("Route registered", "messageID", messageID) - return d.msgServer.RegisterRoute(messageID, route) + for _, route := range routes { + d.logger.Info("Route registered", "messageID", route.MessageID()) + d.msgServer.RegisterRoute(route.MessageID(), route) + } + return nil } func (d *Dispatcher) Name() string { diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index fb39df1d73..58f9b3c5eb 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -29,8 +29,8 @@ import ( type MessageServer interface { RegisterReceiver(mID types.MessageID, ch any) error - Request(req types.MessageI, resp any) error - Respond(resp types.MessageI) error + Request(req types.BaseMessage, future any) error + Respond(resp types.BaseMessage) error RegisterRoute(mID types.MessageID, route types.MessageRoute) error SetLogger(logger log.Logger[any]) } @@ -39,6 +39,6 @@ type EventServer interface { Start(ctx context.Context) RegisterPublisher(mID types.EventID, publisher types.Publisher) Subscribe(mID types.EventID, ch any) error - Publish(event types.MessageI) error + Publish(event types.BaseMessage) error SetLogger(logger log.Logger[any]) } diff --git a/mod/async/pkg/messaging/assert.go b/mod/async/pkg/messaging/assert.go index 0293b245b2..c00698966a 100644 --- a/mod/async/pkg/messaging/assert.go +++ b/mod/async/pkg/messaging/assert.go @@ -29,17 +29,3 @@ func ensureType[T any](e any) (T, error) { } return typedE, nil } - -// assign assigns the source value to the dest -// by pointing destPtr to the source value. -// contract: destPtr -> *T -func assign[T any](source T, destPtr any) error { - // ensure that the destPtr is a pointer to the response type - typedDestPtr, ok := destPtr.(*T) - if !ok { - return errIncompatibleAssignee(new(T), destPtr) - } - // assign the underlying value of destPtr to the source value - *typedDestPtr = source - return nil -} diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index da71d5e90c..ffa4725811 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -89,7 +89,7 @@ func (p *Publisher[T]) start(ctx context.Context) { // Publish publishes a msg to the b. // Returns ErrTimeout on timeout. -func (p *Publisher[T]) Publish(msg types.MessageI) error { +func (p *Publisher[T]) Publish(msg types.BaseMessage) error { typedMsg, err := ensureType[T](msg) if err != nil { return err diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 8b2c90caae..c304ed09d0 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -31,8 +31,8 @@ import ( // Invariant: there is exactly no more than one route for each messageID. type Route[ReqT any, RespT any] struct { messageID types.MessageID - recipientCh chan ReqT - responseCh chan RespT + recipientCh chan types.Message[ReqT] + responseCh chan types.Message[RespT] timeout time.Duration mu sync.Mutex } @@ -43,7 +43,7 @@ func NewRoute[ReqT any, RespT any]( ) *Route[ReqT, RespT] { return &Route[ReqT, RespT]{ messageID: messageID, - responseCh: make(chan RespT), + responseCh: make(chan types.Message[RespT]), timeout: defaultRouterTimeout, mu: sync.Mutex{}, } @@ -61,7 +61,7 @@ func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { } else if ch == nil { return errRegisteringNilChannel(r.messageID) } - typedCh, err := ensureType[chan ReqT](ch) + typedCh, err := ensureType[chan types.Message[ReqT]](ch) if err != nil { return err } @@ -69,25 +69,23 @@ func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { return nil } -// SendRequest sends a request to the recipient. -func (r *Route[ReqT, RespT]) SendRequest(req types.MessageI) error { - typedMsg, err := ensureType[ReqT](req) +// SendRequestAsync accepts a future and sends a request to the recipient +// channel. Once the response is available, it will be written to the future. +func (r *Route[ReqT, RespT]) SendRequest( + req types.BaseMessage, future any, +) error { + r.sendRequest(req) + typedFuture, err := ensureType[types.FutureI[RespT]](future) if err != nil { return err } - - select { - case r.recipientCh <- typedMsg: - return nil - default: - // Channel is full or closed - return errReceiverNotReady(r.messageID) - } + go r.populateFuture(typedFuture) + return nil } // SendResponse sends a response to the response channel. -func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { - typedMsg, err := ensureType[RespT](resp) +func (r *Route[ReqT, RespT]) SendResponse(resp types.BaseMessage) error { + typedMsg, err := ensureType[types.Message[RespT]](resp) if err != nil { return err } @@ -95,13 +93,29 @@ func (r *Route[ReqT, RespT]) SendResponse(resp types.MessageI) error { return nil } -// AwaitResponse listens for a response and returns it if it is available -// before the timeout. Otherwise, it returns ErrTimeout. -func (r *Route[ReqT, RespT]) AwaitResponse(respPtr any) error { +// populateFuture sends a done signal to the future when the response is +// available. +func (r *Route[ReqT, RespT]) populateFuture(future types.FutureI[RespT]) { select { case resp := <-r.responseCh: - return assign(resp, respPtr) + future.SetResult(resp.Data(), resp.Error()) case <-time.After(r.timeout): - return errTimeout(r.messageID, r.timeout) + errTimeout(r.messageID, r.timeout) + } +} + +// SendRequest sends a request to the recipient. +func (r *Route[ReqT, RespT]) sendRequest(req types.BaseMessage) error { + typedReq, err := ensureType[types.Message[ReqT]](req) + if err != nil { + return err + } + + select { + case r.recipientCh <- typedReq: + return nil + default: + // Channel is full or closed + return errReceiverNotReady(r.messageID) } } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index d7e63e7a92..a6b089f07a 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -41,7 +41,7 @@ func NewEventServer() *EventServer { } // Dispatch dispatches the given event to the publisher with the given eventID. -func (es *EventServer) Publish(event types.MessageI) error { +func (es *EventServer) Publish(event types.BaseMessage) error { publisher, ok := es.publishers[event.ID()] if !ok { return ErrPublisherNotFound(event.ID()) diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 9ff50b29bc..fe966b8d3c 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -40,21 +40,21 @@ func NewMessageServer() *MessageServer { // Request sends a message to the server and awaits for a response. // The response is written to the provided response pointer. -func (ms *MessageServer) Request(req types.MessageI, respPtr any) error { +func (ms *MessageServer) Request(req types.BaseMessage, future any) error { // send request and await response route, ok := ms.routes[req.ID()] if !ok { return ErrRouteNotFound } - if err := route.SendRequest(req); err != nil { + if err := route.SendRequest(req, future); err != nil { return err } - return route.AwaitResponse(respPtr) + return nil } // Respond sends a response to the route that corresponds to the response's // messageID. -func (ms *MessageServer) Respond(resp types.MessageI) error { +func (ms *MessageServer) Respond(resp types.BaseMessage) error { route, ok := ms.routes[resp.ID()] if !ok { return ErrRouteNotFound diff --git a/mod/async/pkg/types/future.go b/mod/async/pkg/types/future.go new file mode 100644 index 0000000000..9af4b25116 --- /dev/null +++ b/mod/async/pkg/types/future.go @@ -0,0 +1,75 @@ +package types + +import ( + "fmt" + "sync" + "time" +) + +// FutureI is the interface that the future implements. +type FutureI[T any] interface { + // Resolve returns the result of the future, blocking until it's available + // or the context is done. + Resolve() (result T, err error) + // ResolveWithTimeout returns the result of the future, blocking until it's + // available or the timeout is reached. + ResolveWithTimeout(timeout time.Duration) (result T, err error) + // SetResult is called by the router to set the result of the future. + SetResult(result T, err error) + // IsDone returns true if the future has completed (successfully or with an + // error). + IsDone() bool +} + +// Future represents a value that will be available at some point in the future. +type Future[T any] struct { + result T + err error + done chan struct{} + once sync.Once +} + +// NewFuture creates a new Future and starts the given function in a goroutine. +func NewFuture[T any]() *Future[T] { + f := &Future[T]{ + done: make(chan struct{}), + } + return f +} + +// Resolve returns the result of the future, blocking until it's available or +// the context is done. +func (f *Future[T]) Resolve() (T, error) { + <-f.done + return f.result, f.err +} + +// SetResult sets the result of the future. +func (f *Future[T]) SetResult(result T, err error) { + f.result = result + f.err = err + f.once.Do(func() { close(f.done) }) +} + +// GetWithTimeout returns the result of the future, blocking until it's +// available or the timeout is reached. +func (f *Future[T]) ResolveWithTimeout(timeout time.Duration) (T, error) { + select { + case <-f.done: + return f.result, f.err + case <-time.After(timeout): + var zero T + return zero, fmt.Errorf("future timed out") + } +} + +// IsDone returns true if the future has completed (successfully or with an +// error). +func (f *Future[T]) IsDone() bool { + select { + case <-f.done: + return true + default: + return false + } +} diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index 8f37d70776..9c86880dda 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -25,42 +25,34 @@ import ( "errors" ) -// MessageI defines the minimal interface that the dispatcher expects from a -// message. -type MessageI interface { - ID() MessageID - Context() context.Context -} - -// A Message is an asynchronous message meant for a single recipient. -type Message[DataT any] struct { - // ctx is the context associated with the event. - ctx context.Context - // id is the name of the event. - id MessageID - // event is the actual beacon event. - data DataT - // err is the error associated with the event. - err error -} - // MessageID represents the type of a message. type MessageID string // EventID is a type alias for a MessageID. type EventID = MessageID -// Event acts as a type alias for a Message that is meant to be broadcasted -// to all subscribers. -type Event[DataT any] struct{ Message[DataT] } +// BaseMessage defines the minimal interface that the dispatcher expects from a +// message. +type BaseMessage interface { + ID() MessageID + Context() context.Context +} + +// DataMessage defines the interface that the underlying route expects from a +// message with data. +type Message[DataT any] interface { + BaseMessage + Data() DataT + Error() error +} // NewEvent creates a new Event with the given context and beacon event. func NewMessage[ DataT any, ]( ctx context.Context, messageType MessageID, data DataT, errs ...error, -) *Message[DataT] { - return &Message[DataT]{ +) *message[DataT] { + return &message[DataT]{ ctx: ctx, id: messageType, data: data, @@ -68,6 +60,10 @@ func NewMessage[ } } +// Event acts as a type alias for a Message that is meant to be broadcasted +// to all subscribers. +type Event[DataT any] struct{ message[DataT] } + // NewEvent creates a new Event with the given context and beacon event. func NewEvent[ DataT any, @@ -75,31 +71,43 @@ func NewEvent[ ctx context.Context, messageType EventID, data DataT, errs ...error, ) *Event[DataT] { return &Event[DataT]{ - Message: *NewMessage(ctx, messageType, data, errs...), + message: *NewMessage(ctx, messageType, data, errs...), } } +// A Message is a hard type implementation of the Message and Event interfaces. +type message[DataT any] struct { + // ctx is the context associated with the event. + ctx context.Context + // id is the name of the event. + id MessageID + // event is the actual beacon event. + data DataT + // err is the error associated with the event. + err error +} + // ID returns the ID of the event. -func (m *Message[DataT]) ID() MessageID { +func (m *message[DataT]) ID() MessageID { return m.id } // Context returns the context associated with the event. -func (m *Message[DataT]) Context() context.Context { +func (m *message[DataT]) Context() context.Context { return m.ctx } // Data returns the data associated with the event. -func (m *Message[DataT]) Data() DataT { +func (m *message[DataT]) Data() DataT { return m.data } // Error returns the error associated with the event. -func (m *Message[DataT]) Error() error { +func (m *message[DataT]) Error() error { return m.err } // Is returns true if the event has the given type. -func (m *Message[DataT]) Is(messageType MessageID) bool { +func (m *message[DataT]) Is(messageType MessageID) bool { return m.id == messageType } diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index 0ecd4f9dad..fb952fd131 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -27,11 +27,13 @@ type Publisher interface { // Start starts the event publisher. Start(ctx context.Context) // Publish publishes the given event to the event publisher. - Publish(event MessageI) error + Publish(event BaseMessage) error // Subscribe subscribes the given channel to the event publisher. Subscribe(ch any) error // Unsubscribe unsubscribes the given channel from the event publisher. Unsubscribe(ch any) error + // EventID returns the event ID that the publisher is responsible for. + EventID() EventID } // messageRoute is the interface that supports basic message route operations. @@ -39,11 +41,9 @@ type MessageRoute interface { // RegisterRecipient sets the recipient for the route. RegisterReceiver(ch any) error // SendRequest sends a request to the recipient. - SendRequest(msg MessageI) error + SendRequest(msg BaseMessage, future any) error // SendResponse sends a response to the recipient. - SendResponse(msg MessageI) error - // AwaitResponse awaits a response from the route. - AwaitResponse(emptyResp any) error + SendResponse(msg BaseMessage) error // MessageID returns the message ID that the route is responsible for. MessageID() MessageID } diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 9a81ecc788..ec0839625c 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -88,11 +88,11 @@ type Service[ // finalizeBlkReqs is a channel for receiving finalize beacon block // requests. - finalizeBlkReqs chan *asynctypes.Message[BeaconBlockT] + finalizeBlkReqs chan asynctypes.Message[BeaconBlockT] // verifyBlkReqs is a channel for receiving verify beacon block requests. - verifyBlkReqs chan *asynctypes.Message[BeaconBlockT] + verifyBlkReqs chan asynctypes.Message[BeaconBlockT] // processGenReqs is a channel for receiving process genesis data requests. - processGenReqs chan *asynctypes.Message[GenesisT] + processGenReqs chan asynctypes.Message[GenesisT] } // NewService creates a new validator service. @@ -155,9 +155,9 @@ func NewService[ metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), - finalizeBlkReqs: make(chan *asynctypes.Message[BeaconBlockT]), - verifyBlkReqs: make(chan *asynctypes.Message[BeaconBlockT]), - processGenReqs: make(chan *asynctypes.Message[GenesisT]), + finalizeBlkReqs: make(chan asynctypes.Message[BeaconBlockT]), + verifyBlkReqs: make(chan asynctypes.Message[BeaconBlockT]), + processGenReqs: make(chan asynctypes.Message[GenesisT]), } } @@ -224,7 +224,7 @@ func (s *Service[ // dispatches a response. func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, _, -]) handleProcessGenesisDataRequest(msg *asynctypes.Message[GenesisT]) { +]) handleProcessGenesisDataRequest(msg asynctypes.Message[GenesisT]) { var ( valUpdates transition.ValidatorUpdates err error @@ -259,7 +259,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, ]) handleVerifyBeaconBlockRequest( - msg *asynctypes.Message[BeaconBlockT], + msg asynctypes.Message[BeaconBlockT], ) { // If the block is nil, exit early. if msg.Error() != nil { @@ -286,7 +286,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, ]) handleFinalizeBeaconBlockRequest( - msg *asynctypes.Message[BeaconBlockT], + msg asynctypes.Message[BeaconBlockT], ) { var ( valUpdates transition.ValidatorUpdates diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 3b768c5fc8..4cbbe5c068 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -56,7 +56,7 @@ type Service[ SlashingInfoT any, SlotDataT SlotData[AttestationDataT, SlashingInfoT], ] struct { - buildBlkBundleReqs chan *asynctypes.Message[SlotDataT] + buildBlkBundleReqs chan asynctypes.Message[SlotDataT] // cfg is the validator config. cfg *Config // logger is a logger. @@ -154,7 +154,7 @@ func NewService[ ]{ cfg: cfg, logger: logger, - buildBlkBundleReqs: make(chan *asynctypes.Message[SlotDataT]), + buildBlkBundleReqs: make(chan asynctypes.Message[SlotDataT]), bsb: bsb, chainSpec: chainSpec, signer: signer, @@ -214,7 +214,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, BeaconBlockBundleT, _, _, BlobSidecarsT, _, _, _, _, _, _, _, SlotDataT, -]) handleBuildBlockBundleRequest(req *asynctypes.Message[SlotDataT]) { +]) handleBuildBlockBundleRequest(req asynctypes.Message[SlotDataT]) { var ( blk BeaconBlockT sidecars BlobSidecarsT @@ -232,11 +232,12 @@ func (s *Service[ // bundle the block and sidecars and dispatch the response // blkData := *new(BeaconBlockBundleT) blkData = blkData.New(blk, sidecars) - if err = s.dispatcher.SendResponse(asynctypes.NewMessage( - req.Context(), - messages.BuildBeaconBlockAndSidecars, - blkData, - )); err != nil { + if err = s.dispatcher.SendResponse( + asynctypes.NewMessage( + req.Context(), + messages.BuildBeaconBlockAndSidecars, + blkData, + )); err != nil { s.logger.Error("failed to respond", "err", err) } } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 3a9100d538..f31fce0ab2 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -43,8 +43,8 @@ type Service[ ] dispatcher *dispatcher.Dispatcher logger log.Logger[any] - processSidecarRequests chan *asynctypes.Message[BlobSidecarsT] - verifySidecarRequests chan *asynctypes.Message[BlobSidecarsT] + processSidecarRequests chan asynctypes.Message[BlobSidecarsT] + verifySidecarRequests chan asynctypes.Message[BlobSidecarsT] } // NewService returns a new DA service. @@ -76,8 +76,8 @@ func NewService[ bp: bp, dispatcher: dispatcher, logger: logger, - processSidecarRequests: make(chan *asynctypes.Message[BlobSidecarsT]), - verifySidecarRequests: make(chan *asynctypes.Message[BlobSidecarsT]), + processSidecarRequests: make(chan asynctypes.Message[BlobSidecarsT]), + verifySidecarRequests: make(chan asynctypes.Message[BlobSidecarsT]), } } @@ -134,7 +134,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) start( // event. // It processes the sidecars and publishes a BlobSidecarsProcessed event. func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( - msg *asynctypes.Message[BlobSidecarsT], + msg asynctypes.Message[BlobSidecarsT], ) { var err error err = s.processSidecars(msg.Context(), msg.Data()) @@ -162,7 +162,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( // handleSidecarsVerifyRequest handles the SidecarsVerifyRequest event. // It verifies the sidecars and publishes a SidecarsVerified event. func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( - msg *asynctypes.Message[BlobSidecarsT], + msg asynctypes.Message[BlobSidecarsT], ) { var err error // verify the sidecars. diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index edf0828d0d..ffcc52d0be 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -31,11 +31,11 @@ import ( // DispatcherInput is the input for the Dispatcher. type DispatcherInput struct { depinject.In - EventServer *EventServer - MessageServer *MessageServer - Logger log.AdvancedLogger[any, sdklog.Logger] - BeaconBlockFinalizedPublisher *BeaconBlockFinalizedPublisher - Routes []asynctypes.MessageRoute + EventServer *EventServer + MessageServer *MessageServer + Logger log.AdvancedLogger[any, sdklog.Logger] + Publishers []asynctypes.Publisher + Routes []asynctypes.MessageRoute } // ProvideDispatcher provides a new Dispatcher. @@ -47,16 +47,8 @@ func ProvideDispatcher( in.MessageServer, in.Logger.With("service", "dispatcher"), ) - d.RegisterPublisher( - in.BeaconBlockFinalizedPublisher.EventID(), - in.BeaconBlockFinalizedPublisher, - ) - for _, route := range in.Routes { - if err := d.RegisterRoute(route.MessageID(), route); err != nil { - return nil, err - } - } - + d.RegisterPublishers(in.Publishers...) + d.RegisterRoutes(in.Routes...) return d, nil } @@ -64,7 +56,7 @@ func DefaultDispatcherComponents() []any { return []any{ ProvideDispatcher, ProvideMessageRoutes, - ProvideBeaconBlockFinalizedPublisher, + ProvidePublishers, ProvideMessageServer, ProvideEventServer, } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 93e5cd9038..eef0b7942d 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -23,6 +23,7 @@ package components import ( "github.com/berachain/beacon-kit/mod/async/pkg/messaging" "github.com/berachain/beacon-kit/mod/async/pkg/server" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) @@ -31,10 +32,12 @@ func ProvideEventServer() *EventServer { return server.NewEventServer() } -// ProvideBeaconBlockFinalizedPublisher provides a publisher for beacon block +// ProvidePublishers provides a publisher for beacon block // finalized events. -func ProvideBeaconBlockFinalizedPublisher() *BeaconBlockFinalizedPublisher { - return messaging.NewPublisher[*FinalizedBlockEvent]( - messages.BeaconBlockFinalizedEvent, - ) +func ProvidePublishers() []asynctypes.Publisher { + return []asynctypes.Publisher{ + messaging.NewPublisher[*FinalizedBlockEvent]( + messages.BeaconBlockFinalizedEvent, + ), + } } diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go index ee80d215e0..cb47de88ca 100644 --- a/mod/node-core/pkg/components/messages.go +++ b/mod/node-core/pkg/components/messages.go @@ -25,6 +25,7 @@ import ( "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) // ProvideMessageServer provides a message server. @@ -35,43 +36,23 @@ func ProvideMessageServer() *server.MessageServer { // ProvideMessageRoutes provides all the message routes. func ProvideMessageRoutes() []asynctypes.MessageRoute { return []asynctypes.MessageRoute{ - RouteFactory(messages.BuildBeaconBlockAndSidecars), - RouteFactory(messages.VerifyBeaconBlock), - RouteFactory(messages.FinalizeBeaconBlock), - RouteFactory(messages.ProcessGenesisData), - RouteFactory(messages.VerifySidecars), - RouteFactory(messages.ProcessSidecars), - } -} - -// RouteFactory creates a new route for the given message ID. -func RouteFactory(mID string) asynctypes.MessageRoute { - switch mID { - case messages.BuildBeaconBlockAndSidecars: - return messaging.NewRoute[ - *SlotMessage, *BlockBundleMessage, - ](messages.BuildBeaconBlockAndSidecars) - case messages.VerifyBeaconBlock: - return messaging.NewRoute[ - *BlockMessage, *BlockMessage, - ](messages.VerifyBeaconBlock) - case messages.FinalizeBeaconBlock: - return messaging.NewRoute[ - *BlockMessage, *ValidatorUpdateMessage, - ](messages.FinalizeBeaconBlock) - case messages.ProcessGenesisData: - return messaging.NewRoute[ - *GenesisMessage, *ValidatorUpdateMessage, - ](messages.ProcessGenesisData) - case messages.VerifySidecars: - return messaging.NewRoute[ - *SidecarMessage, *SidecarMessage, - ](messages.VerifySidecars) - case messages.ProcessSidecars: - return messaging.NewRoute[ - *SidecarMessage, *SidecarMessage, - ](messages.ProcessSidecars) - default: - return nil + messaging.NewRoute[*SlotData, *BeaconBlockBundle]( + messages.BuildBeaconBlockAndSidecars, + ), + messaging.NewRoute[*BeaconBlock, *BeaconBlock]( + messages.VerifyBeaconBlock, + ), + messaging.NewRoute[*BeaconBlock, transition.ValidatorUpdates]( + messages.FinalizeBeaconBlock, + ), + messaging.NewRoute[*Genesis, transition.ValidatorUpdates]( + messages.ProcessGenesisData, + ), + messaging.NewRoute[*BlobSidecars, *BlobSidecars]( + messages.VerifySidecars, + ), + messaging.NewRoute[*BlobSidecars, *BlobSidecars]( + messages.ProcessSidecars, + ), } } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index caa452a1c0..f3e6dbc406 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -420,20 +420,17 @@ type ( /* Messages */ /* -------------------------------------------------------------------------- */ -// events. +// Events. type ( // FinalizedBlockEvent is a type alias for the block event. FinalizedBlockEvent = asynctypes.Event[*BeaconBlock] ) -// messages. +// Messages. type ( // BlockMessage is a type alias for the block message. BlockMessage = asynctypes.Message[*BeaconBlock] - // BlockBundleMessage is a type alias for the block bundle message. - BlockBundleMessage = asynctypes.Message[*BeaconBlockBundle] - // GenesisMessage is a type alias for the genesis message. GenesisMessage = asynctypes.Message[*Genesis] @@ -445,46 +442,28 @@ type ( // StatusMessage is a type alias for the status message. StatusMessage = asynctypes.Message[*service.StatusEvent] - - // ValidatorUpdateMessage is a type alias for the validator update message. - ValidatorUpdateMessage = asynctypes.Message[transition.ValidatorUpdates] ) -/* -------------------------------------------------------------------------- */ -/* Publishers */ -/* -------------------------------------------------------------------------- */ - +// Futures. type ( - BeaconBlockFinalizedPublisher = messaging.Publisher[*FinalizedBlockEvent] + // BlockFuture is a type alias for a future containing a BeaconBlock. + BlockFuture = asynctypes.Future[*BeaconBlock] + // BlockBundleFuture is a type alias for a future containing a + // BeaconBlockBundle. + BlockBundleFuture = asynctypes.Future[*BeaconBlockBundle] + // SidecarFuture is a type alias for a future containing the BlobSidecars. + SidecarFuture = asynctypes.Future[*BlobSidecars] + // ValidatorUpdateFuture is a type alias for a future containing the + // ValidatorUpdates. + ValidatorUpdateFuture = asynctypes.Future[transition.ValidatorUpdates] ) /* -------------------------------------------------------------------------- */ -/* Routes */ +/* Publishers */ /* -------------------------------------------------------------------------- */ type ( - // BuildBlockAndSidecarsRoute is a type alias for the build block and - // sidecars route. - BuildBlockAndSidecarsRoute = messaging.Route[ - *SlotMessage, *BlockBundleMessage, - ] - - // VerifyBlockRoute is a type alias for the verify block route. - VerifyBlockRoute = messaging.Route[*BlockMessage, *BlockMessage] - - // FinalizeBlockRoute is a type alias for the finalize block route. - FinalizeBlockRoute = messaging.Route[*BlockMessage, *ValidatorUpdateMessage] - - // ProcessGenesisDataRoute is a type alias for the process genesis data route. - ProcessGenesisDataRoute = messaging.Route[ - *GenesisMessage, *ValidatorUpdateMessage, - ] - - // ProcessSidecarsRoute is a type alias for the process blob sidecars route. - ProcessSidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] - - // VerifySidecarsRoute is a type alias for the verify sidecars route. - VerifySidecarsRoute = messaging.Route[*SidecarMessage, *SidecarMessage] + BeaconBlockFinalizedPublisher = messaging.Publisher[*FinalizedBlockEvent] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 239261c03d..2613639ea4 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -46,10 +46,7 @@ func (h *ABCIMiddleware[ ctx context.Context, bz []byte, ) (transition.ValidatorUpdates, error) { - var ( - valUpdateResp *asynctypes.Message[transition.ValidatorUpdates] - err error - ) + var err error data := new(GenesisT) if err = json.Unmarshal(bz, data); err != nil { h.logger.Error("Failed to unmarshal genesis data", "error", err) @@ -57,15 +54,16 @@ func (h *ABCIMiddleware[ } // request for validator updates + valUpdates := asynctypes.NewFuture[transition.ValidatorUpdates]() if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.ProcessGenesisData, *data, - ), &valUpdateResp, + ), valUpdates, ); err != nil { return nil, err } - return valUpdateResp.Data(), valUpdateResp.Error() + return valUpdates.Resolve() } /* -------------------------------------------------------------------------- */ @@ -79,23 +77,28 @@ func (h *ABCIMiddleware[ ctx context.Context, slotData SlotDataT, ) ([]byte, []byte, error) { - var ( - startTime = time.Now() - beaconBlkBundleResp *asynctypes.Message[BeaconBlockBundleT] - ) + var err error + startTime := time.Now() defer h.metrics.measurePrepareProposalDuration(startTime) // request a built beacon block for the given slot - if err := h.dispatcher.SendRequest( + beaconBlkBundleFuture := asynctypes.NewFuture[BeaconBlockBundleT]() + if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.BuildBeaconBlockAndSidecars, slotData, - ), &beaconBlkBundleResp, + ), beaconBlkBundleFuture, ); err != nil { return nil, nil, err } + // resolve the beacon block bundle from the future + beaconBlkBundle, err := beaconBlkBundleFuture.Resolve() + if err != nil { + return nil, nil, err + } + // gossip the built beacon block and blob sidecars - return h.handleBeaconBlockBundleResponse(ctx, beaconBlkBundleResp) + return h.handleBeaconBlockBundleResponse(ctx, beaconBlkBundle) } // handleBeaconBlockBundleResponse gossips the built beacon block and blob @@ -104,21 +107,17 @@ func (h *ABCIMiddleware[ _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, _, ]) handleBeaconBlockBundleResponse( ctx context.Context, - bbbResp *asynctypes.Message[BeaconBlockBundleT], + bbb BeaconBlockBundleT, ) ([]byte, []byte, error) { - // handle response errors - if bbbResp.Error() != nil { - return nil, nil, bbbResp.Error() - } // gossip beacon block bbBz, bbErr := h.beaconBlockGossiper.Publish( - ctx, bbbResp.Data().GetBeaconBlock(), + ctx, bbb.GetBeaconBlock(), ) if bbErr != nil { return nil, nil, bbErr } // gossip blob sidecars - scBz, scErr := h.blobGossiper.Publish(ctx, bbbResp.Data().GetSidecars()) + scBz, scErr := h.blobGossiper.Publish(ctx, bbb.GetSidecars()) if scErr != nil { return nil, nil, scErr } @@ -138,12 +137,10 @@ func (h *ABCIMiddleware[ req proto.Message, ) (proto.Message, error) { var ( - blk BeaconBlockT - sidecars BlobSidecarsT - err error - startTime = time.Now() - beaconBlockResp *asynctypes.Message[BeaconBlockT] - sidecarsResp *asynctypes.Message[BlobSidecarsT] + blk BeaconBlockT + sidecars BlobSidecarsT + err error + startTime = time.Now() ) abciReq, ok := req.(*cmtabci.ProcessProposalRequest) if !ok { @@ -158,34 +155,39 @@ func (h *ABCIMiddleware[ } // verify the beacon block + beaconBlockFuture := asynctypes.NewFuture[BeaconBlockT]() if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.VerifyBeaconBlock, blk, - ), &beaconBlockResp, + ), beaconBlockFuture, ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - if beaconBlockResp.Error() != nil { - return h.createProcessProposalResponse(beaconBlockResp.Error()) - } - // Request the blob sidecars. if sidecars, err = h.blobGossiper.Request(ctx, abciReq); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } // verify the blob sidecars + sidecarsFuture := asynctypes.NewFuture[BlobSidecarsT]() if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.VerifySidecars, sidecars, - ), &sidecarsResp, + ), sidecarsFuture, ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - if sidecarsResp.Error() != nil { - return h.createProcessProposalResponse(sidecarsResp.Error()) + // error if the beacon block or sidecars are invalid + _, err = beaconBlockFuture.Resolve() + if err != nil { + return h.createProcessProposalResponse(err) + } + + _, err = sidecarsFuture.Resolve() + if err != nil { + return h.createProcessProposalResponse(err) } return h.createProcessProposalResponse(nil) @@ -215,11 +217,9 @@ func (h *ABCIMiddleware[ ctx context.Context, req proto.Message, ) (transition.ValidatorUpdates, error) { var ( - sidecarsResp *asynctypes.Message[BlobSidecarsT] - valUpdatesResp *asynctypes.Message[transition.ValidatorUpdates] - blk BeaconBlockT - blobs BlobSidecarsT - err error + blk BeaconBlockT + blobs BlobSidecarsT + err error ) abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) if !ok { @@ -239,23 +239,25 @@ func (h *ABCIMiddleware[ return nil, nil } - // process the blob sidecars. + // process the sidecars. + sidecarsFuture := asynctypes.NewFuture[BlobSidecarsT]() if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.ProcessSidecars, blobs, - ), &sidecarsResp, + ), sidecarsFuture, ); err != nil { - return nil, sidecarsResp.Error() + return nil, err } // finalize the beacon block. + valUpdatesFuture := asynctypes.NewFuture[transition.ValidatorUpdates]() if err = h.dispatcher.SendRequest( asynctypes.NewMessage( ctx, messages.FinalizeBeaconBlock, blk, - ), &valUpdatesResp, + ), valUpdatesFuture, ); err != nil { - return nil, valUpdatesResp.Error() + return nil, err } - return valUpdatesResp.Data(), valUpdatesResp.Error() + return valUpdatesFuture.Resolve() } From 3f42ab85da3baab663928fd82e6d8d03843378ab Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 16:35:13 -0400 Subject: [PATCH 38/75] bet --- mod/async/pkg/dispatcher/dispatcher.go | 15 +++++++++--- mod/async/pkg/dispatcher/types.go | 2 +- mod/async/pkg/messaging/constants.go | 9 ++++---- mod/async/pkg/messaging/errors.go | 3 ++- mod/async/pkg/messaging/route.go | 25 +++++++++++++------- mod/async/pkg/server/errors.go | 3 +++ mod/async/pkg/server/event.go | 6 ++++- mod/async/pkg/types/errors.go | 27 ++++++++++++++++++++++ mod/async/pkg/types/future.go | 23 ++++++++++++++++-- mod/async/pkg/types/message.go | 7 +++--- mod/async/pkg/types/notifiers.go | 2 ++ mod/node-core/pkg/components/dispatcher.go | 8 +++++-- 12 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 mod/async/pkg/types/errors.go diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 7fb3ebb769..7ea308cefb 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -85,11 +85,16 @@ func (d *Dispatcher) SendResponse(resp types.BaseMessage) error { // consistent with the type expected by . func (d *Dispatcher) RegisterPublishers( publishers ...types.Publisher, -) { +) error { + var err error for _, publisher := range publishers { d.logger.Info("Publisher registered", "eventID", publisher.EventID()) - d.eventServer.RegisterPublisher(publisher.EventID(), publisher) + err = d.eventServer.RegisterPublisher(publisher.EventID(), publisher) + if err != nil { + return err + } } + return nil } // Subscribe subscribes the given channel to the event with the given . @@ -116,9 +121,13 @@ func (d *Dispatcher) RegisterMsgReceiver( func (d *Dispatcher) RegisterRoutes( routes ...types.MessageRoute, ) error { + var err error for _, route := range routes { d.logger.Info("Route registered", "messageID", route.MessageID()) - d.msgServer.RegisterRoute(route.MessageID(), route) + err = d.msgServer.RegisterRoute(route.MessageID(), route) + if err != nil { + return err + } } return nil } diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 58f9b3c5eb..d51c2b74f8 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -37,7 +37,7 @@ type MessageServer interface { type EventServer interface { Start(ctx context.Context) - RegisterPublisher(mID types.EventID, publisher types.Publisher) + RegisterPublisher(mID types.EventID, publisher types.Publisher) error Subscribe(mID types.EventID, ch any) error Publish(event types.BaseMessage) error SetLogger(logger log.Logger[any]) diff --git a/mod/async/pkg/messaging/constants.go b/mod/async/pkg/messaging/constants.go index f53506c168..79c7f10602 100644 --- a/mod/async/pkg/messaging/constants.go +++ b/mod/async/pkg/messaging/constants.go @@ -29,10 +29,11 @@ const ( // publisher, or a client subscribes or unsubscribes. defaultPublisherTimeout = time.Second - // defaultRouterTimeout specifies the default timeout when the router - // tries to send a message to a client, a message is published to the - // router, or a client subscribes or unsubscribes. - defaultRouterTimeout = 2 * time.Second + // defaultMaxTimeout specifies the default max timeout for a message to be + // sent to a client, a message to be published, or a client to subscribe or + // unsubscribe. + // TODO: fr implementation + defaultMaxTimeout = 2 * time.Second // defaultBufferSize specifies the default size of the message buffer. defaultBufferSize = 10 diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index 629b7cbbd1..8c7485885e 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -32,7 +32,8 @@ import ( //nolint:gochecknoglobals // errors var ( errTimeout = func(messageID types.MessageID, timeout time.Duration) error { - return errors.Newf("message %s timed out after %s", messageID, timeout) + return errors.Newf("message %s reached the max timeout of %s", + messageID, timeout) } errRouteAlreadySet = errors.New("route already set") diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index c304ed09d0..4d13f0492f 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -30,11 +30,17 @@ import ( // Route represents a communication route to a single recipient. // Invariant: there is exactly no more than one route for each messageID. type Route[ReqT any, RespT any] struct { - messageID types.MessageID + // messageID is the ID of the message that the route is responsible for. + messageID types.MessageID + // recipientCh is the channel to send requests to. recipientCh chan types.Message[ReqT] - responseCh chan types.Message[RespT] - timeout time.Duration - mu sync.Mutex + // responseCh is the channel to send responses to. + responseCh chan types.Message[RespT] + // maxTimeout is the maximum duration to wait for a response + // before considering the request timed out and pruning the thread. + maxTimeout time.Duration + // mu is the mutex to synchronize access to the route. + mu sync.Mutex } // NewRoute creates a new route. @@ -44,7 +50,7 @@ func NewRoute[ReqT any, RespT any]( return &Route[ReqT, RespT]{ messageID: messageID, responseCh: make(chan types.Message[RespT]), - timeout: defaultRouterTimeout, + maxTimeout: defaultMaxTimeout, mu: sync.Mutex{}, } } @@ -74,7 +80,9 @@ func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { func (r *Route[ReqT, RespT]) SendRequest( req types.BaseMessage, future any, ) error { - r.sendRequest(req) + if err := r.sendRequest(req); err != nil { + return err + } typedFuture, err := ensureType[types.FutureI[RespT]](future) if err != nil { return err @@ -99,8 +107,9 @@ func (r *Route[ReqT, RespT]) populateFuture(future types.FutureI[RespT]) { select { case resp := <-r.responseCh: future.SetResult(resp.Data(), resp.Error()) - case <-time.After(r.timeout): - errTimeout(r.messageID, r.timeout) + case <-time.After(r.maxTimeout): + future.SetResult(*new(RespT), errTimeout(r.messageID, r.maxTimeout)) + return } } diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index ede1874a8e..c47b9d322e 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -35,4 +35,7 @@ var ( return errors.Newf("route already registered for messageID: %s", messageID) } + ErrPublisherAlreadyExists = func(eventID types.EventID) error { + return errors.Newf("publisher already exists for eventID: %s", eventID) + } ) diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index a6b089f07a..07345afd49 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -72,8 +72,12 @@ func (es *EventServer) Start(ctx context.Context) { // consistent with the type expected by . func (es *EventServer) RegisterPublisher( eventID types.MessageID, publisher types.Publisher, -) { +) error { + if _, ok := es.publishers[eventID]; ok { + return ErrPublisherAlreadyExists(eventID) + } es.publishers[eventID] = publisher + return nil } // SetLogger sets the logger for the event server. diff --git a/mod/async/pkg/types/errors.go b/mod/async/pkg/types/errors.go new file mode 100644 index 0000000000..3ddfee1834 --- /dev/null +++ b/mod/async/pkg/types/errors.go @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package types + +import "errors" + +var ( + ErrTimeout = errors.New("future timed out") +) diff --git a/mod/async/pkg/types/future.go b/mod/async/pkg/types/future.go index 9af4b25116..d69d5577bc 100644 --- a/mod/async/pkg/types/future.go +++ b/mod/async/pkg/types/future.go @@ -1,7 +1,26 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + package types import ( - "fmt" "sync" "time" ) @@ -59,7 +78,7 @@ func (f *Future[T]) ResolveWithTimeout(timeout time.Duration) (T, error) { return f.result, f.err case <-time.After(timeout): var zero T - return zero, fmt.Errorf("future timed out") + return zero, ErrTimeout } } diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index 9c86880dda..8342794704 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -44,6 +44,7 @@ type Message[DataT any] interface { BaseMessage Data() DataT Error() error + Is(messageType MessageID) bool } // NewEvent creates a new Event with the given context and beacon event. @@ -51,7 +52,7 @@ func NewMessage[ DataT any, ]( ctx context.Context, messageType MessageID, data DataT, errs ...error, -) *message[DataT] { +) Message[DataT] { return &message[DataT]{ ctx: ctx, id: messageType, @@ -62,7 +63,7 @@ func NewMessage[ // Event acts as a type alias for a Message that is meant to be broadcasted // to all subscribers. -type Event[DataT any] struct{ message[DataT] } +type Event[DataT any] struct{ Message[DataT] } // NewEvent creates a new Event with the given context and beacon event. func NewEvent[ @@ -71,7 +72,7 @@ func NewEvent[ ctx context.Context, messageType EventID, data DataT, errs ...error, ) *Event[DataT] { return &Event[DataT]{ - message: *NewMessage(ctx, messageType, data, errs...), + Message: NewMessage(ctx, messageType, data, errs...), } } diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/notifiers.go index fb952fd131..d69266e23d 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/notifiers.go @@ -22,6 +22,8 @@ package types import "context" +// TODO: add dispatcher interface once its stable + // publisher is the interface that supports basic event publisher operations. type Publisher interface { // Start starts the event publisher. diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index ffcc52d0be..d47ca87be2 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -47,8 +47,12 @@ func ProvideDispatcher( in.MessageServer, in.Logger.With("service", "dispatcher"), ) - d.RegisterPublishers(in.Publishers...) - d.RegisterRoutes(in.Routes...) + if err := d.RegisterPublishers(in.Publishers...); err != nil { + return nil, err + } + if err := d.RegisterRoutes(in.Routes...); err != nil { + return nil, err + } return d, nil } From ae045ffc2187cce03cf452e29f5a7e014c9ba7e1 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 16:44:51 -0400 Subject: [PATCH 39/75] nit fixes --- mod/async/pkg/messaging/errors.go | 8 ++++++ mod/async/pkg/messaging/publisher.go | 40 ++++++++++++++++++---------- mod/async/pkg/server/errors.go | 8 +++--- mod/async/pkg/server/event.go | 6 ++--- mod/async/pkg/server/msg.go | 8 +++--- mod/async/pkg/types/errors.go | 2 +- mod/async/pkg/types/future.go | 2 +- 7 files changed, 47 insertions(+), 27 deletions(-) diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index 8c7485885e..87bee97d82 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -31,18 +31,24 @@ import ( // //nolint:gochecknoglobals // errors var ( + // errTimeout is the error returned when a dispatch operation timed out. errTimeout = func(messageID types.MessageID, timeout time.Duration) error { return errors.Newf("message %s reached the max timeout of %s", messageID, timeout) } + // errRouteAlreadySet is the error returned when the route is already set. errRouteAlreadySet = errors.New("route already set") + // errRegisteringNilChannel is the error returned when the channel to + // register is nil. errRegisteringNilChannel = func(messageID types.MessageID) error { return errors.Newf("cannot register nil channel for route: %s", messageID) } + // errReceiverNotReady is the error returned when the receiver channel is + // full, closed, or not listening. errReceiverNotReady = func(messageID types.MessageID) error { return errors.Newf( "receiver channel is full, closed, or not listening. Route: %s", @@ -50,6 +56,8 @@ var ( ) } + // errIncompatibleAssignee is the error returned when the assignee is not + // compatible with the assigner. errIncompatibleAssignee = func( assigner interface{}, assignee interface{}, ) error { diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index ffa4725811..809d4effae 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -42,7 +42,8 @@ type Publisher[T any] struct { mu sync.Mutex } -// NewPublisher creates a new b. +// NewPublisher creates a new publisher publishing events of type T for the +// provided eventID. func NewPublisher[T any](eventID string) *Publisher[T] { return &Publisher[T]{ eventID: types.EventID(eventID), @@ -53,6 +54,7 @@ func NewPublisher[T any](eventID string) *Publisher[T] { } } +// EventID returns the event ID that the publisher is responsible for. func (p *Publisher[T]) EventID() types.EventID { return p.eventID } @@ -68,26 +70,16 @@ func (p *Publisher[T]) start(ctx context.Context) { select { case <-ctx.Done(): // close all leftover clients and break the publisher loop - for client := range p.clients { - if err := p.Unsubscribe(client); err != nil { - panic(err) - } - } + p.shutdown() return case msg := <-p.msgs: // broadcast published msg to all clients - for client := range p.clients { - // send msg to client (or discard msg after timeout) - select { - case client <- msg: - case <-time.After(p.timeout): - } - } + p.broadcast(msg) } } } -// Publish publishes a msg to the b. +// Publish publishes a msg to all subscribers. // Returns ErrTimeout on timeout. func (p *Publisher[T]) Publish(msg types.BaseMessage) error { typedMsg, err := ensureType[T](msg) @@ -130,3 +122,23 @@ func (p *Publisher[T]) Unsubscribe(ch any) error { close(client) return nil } + +// broadcast broadcasts a msg to all clients. +func (p *Publisher[T]) broadcast(msg T) { + for client := range p.clients { + // send msg to client (or discard msg after timeout) + select { + case client <- msg: + case <-time.After(p.timeout): + } + } +} + +// shutdown closes all leftover clients +func (p *Publisher[T]) shutdown() { + for client := range p.clients { + if err := p.Unsubscribe(client); err != nil { + panic(err) + } + } +} diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index c47b9d322e..6282dfb15d 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -27,15 +27,15 @@ import ( //nolint:gochecknoglobals // errors var ( - ErrPublisherNotFound = func(eventID types.EventID) error { + errPublisherNotFound = func(eventID types.EventID) error { return errors.Newf("publisher not found for eventID: %s", eventID) } - ErrRouteNotFound = errors.New("route not found") - ErrRouteAlreadyRegistered = func(messageID types.MessageID) error { + errRouteNotFound = errors.New("route not found") + errRouteAlreadyRegistered = func(messageID types.MessageID) error { return errors.Newf("route already registered for messageID: %s", messageID) } - ErrPublisherAlreadyExists = func(eventID types.EventID) error { + errPublisherAlreadyExists = func(eventID types.EventID) error { return errors.Newf("publisher already exists for eventID: %s", eventID) } ) diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 07345afd49..188ea6bcc3 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -44,7 +44,7 @@ func NewEventServer() *EventServer { func (es *EventServer) Publish(event types.BaseMessage) error { publisher, ok := es.publishers[event.ID()] if !ok { - return ErrPublisherNotFound(event.ID()) + return errPublisherNotFound(event.ID()) } return publisher.Publish(event) } @@ -55,7 +55,7 @@ func (es *EventServer) Publish(event types.BaseMessage) error { func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { publisher, ok := es.publishers[eventID] if !ok { - return ErrPublisherNotFound(eventID) + return errPublisherNotFound(eventID) } return publisher.Subscribe(ch) } @@ -74,7 +74,7 @@ func (es *EventServer) RegisterPublisher( eventID types.MessageID, publisher types.Publisher, ) error { if _, ok := es.publishers[eventID]; ok { - return ErrPublisherAlreadyExists(eventID) + return errPublisherAlreadyExists(eventID) } es.publishers[eventID] = publisher return nil diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index fe966b8d3c..9e8aa6b45c 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -44,7 +44,7 @@ func (ms *MessageServer) Request(req types.BaseMessage, future any) error { // send request and await response route, ok := ms.routes[req.ID()] if !ok { - return ErrRouteNotFound + return errRouteNotFound } if err := route.SendRequest(req, future); err != nil { return err @@ -57,7 +57,7 @@ func (ms *MessageServer) Request(req types.BaseMessage, future any) error { func (ms *MessageServer) Respond(resp types.BaseMessage) error { route, ok := ms.routes[resp.ID()] if !ok { - return ErrRouteNotFound + return errRouteNotFound } return route.SendResponse(resp) } @@ -69,7 +69,7 @@ func (ms *MessageServer) RegisterRoute( messageID types.MessageID, route types.MessageRoute, ) error { if ms.routes[messageID] != nil { - return ErrRouteAlreadyRegistered(messageID) + return errRouteAlreadyRegistered(messageID) } ms.routes[messageID] = route return nil @@ -83,7 +83,7 @@ func (ms *MessageServer) RegisterReceiver( ) error { route, ok := ms.routes[messageID] if !ok { - return ErrRouteNotFound + return errRouteNotFound } return route.RegisterReceiver(ch) } diff --git a/mod/async/pkg/types/errors.go b/mod/async/pkg/types/errors.go index 3ddfee1834..4fc6b7dfb0 100644 --- a/mod/async/pkg/types/errors.go +++ b/mod/async/pkg/types/errors.go @@ -23,5 +23,5 @@ package types import "errors" var ( - ErrTimeout = errors.New("future timed out") + errTimeout = errors.New("future timed out") ) diff --git a/mod/async/pkg/types/future.go b/mod/async/pkg/types/future.go index d69d5577bc..cc2f34d0ed 100644 --- a/mod/async/pkg/types/future.go +++ b/mod/async/pkg/types/future.go @@ -78,7 +78,7 @@ func (f *Future[T]) ResolveWithTimeout(timeout time.Duration) (T, error) { return f.result, f.err case <-time.After(timeout): var zero T - return zero, ErrTimeout + return zero, errTimeout } } From 85b9c7bf171bc3c0756c41f6ef018353102008bf Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 16:58:06 -0400 Subject: [PATCH 40/75] interface --- mod/async/pkg/dispatcher/dispatcher.go | 5 +++ .../pkg/types/{notifiers.go => dispatcher.go} | 36 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) rename mod/async/pkg/types/{notifiers.go => dispatcher.go} (59%) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 7ea308cefb..30fe1a8ad7 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -30,6 +30,11 @@ import ( // Dispatcher faciliates asynchronous communication between components, // typically services. It acts as an API facade to the underlying event and // message servers. +// DispatcherI defines the interface for a dispatcher that facilitates asynchronous communication. + +// Ensure Dispatcher implements DispatcherI +var _ types.Dispatcher = (*Dispatcher)(nil) + type Dispatcher struct { eventServer EventServer msgServer MessageServer diff --git a/mod/async/pkg/types/notifiers.go b/mod/async/pkg/types/dispatcher.go similarity index 59% rename from mod/async/pkg/types/notifiers.go rename to mod/async/pkg/types/dispatcher.go index d69266e23d..1769871e29 100644 --- a/mod/async/pkg/types/notifiers.go +++ b/mod/async/pkg/types/dispatcher.go @@ -22,7 +22,41 @@ package types import "context" -// TODO: add dispatcher interface once its stable +// Dispatcher is the full API for a dispatcher that facilitates the publishing +// of async events and the sending and receiving of async messages. +type Dispatcher interface { + EventDispatcher + MessageDispatcher + // Start starts the dispatcher. + Start(ctx context.Context) error + // RegisterPublishers registers publishers to the dispatcher. + RegisterPublishers(publishers ...Publisher) error + // RegisterRoutes registers message routes to the dispatcher. + RegisterRoutes(routes ...MessageRoute) error + // Name returns the name of the dispatcher. + Name() string +} + +// EventDispatcher is the API for a dispatcher that facilitates the publishing +// of async events. +type EventDispatcher interface { + // PublishEvent publishes an event to the dispatcher. + PublishEvent(event BaseMessage) error + // Subscribe subscribes to an event. + Subscribe(eventID EventID, ch any) error +} + +// MessageDispatcher is the API for a dispatcher that facilitates the sending +// and receiving of async messages. +type MessageDispatcher interface { + // SendRequest sends a request to the dispatcher. + SendRequest(req BaseMessage, future any) error + // SendResponse sends a response to the dispatcher. + SendResponse(resp BaseMessage) error + // RegisterMsgReceiver registers the given channel as the message receiver + // for the given message ID. + RegisterMsgReceiver(messageID MessageID, ch any) error +} // publisher is the interface that supports basic event publisher operations. type Publisher interface { From 63018148a03694d3ab4364dd10a9e9c98da8412e Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 17:03:30 -0400 Subject: [PATCH 41/75] bet --- mod/async/pkg/dispatcher/dispatcher.go | 3 +-- mod/async/pkg/messaging/publisher.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 30fe1a8ad7..c68502064b 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -30,9 +30,8 @@ import ( // Dispatcher faciliates asynchronous communication between components, // typically services. It acts as an API facade to the underlying event and // message servers. -// DispatcherI defines the interface for a dispatcher that facilitates asynchronous communication. -// Ensure Dispatcher implements DispatcherI +// Ensure Dispatcher implements DispatcherI. var _ types.Dispatcher = (*Dispatcher)(nil) type Dispatcher struct { diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index 809d4effae..11b22fe821 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -134,7 +134,7 @@ func (p *Publisher[T]) broadcast(msg T) { } } -// shutdown closes all leftover clients +// shutdown closes all leftover clients. func (p *Publisher[T]) shutdown() { for client := range p.clients { if err := p.Unsubscribe(client); err != nil { From 08fff9837fa00e6c6a7b08fa9ec6ae39d43a988c Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 18:04:48 -0400 Subject: [PATCH 42/75] nilaway --- mod/async/pkg/messaging/errors.go | 5 +++++ mod/async/pkg/messaging/route.go | 3 +++ mod/primitives/pkg/messages/messages.go | 1 - 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go index 87bee97d82..ea60b379d2 100644 --- a/mod/async/pkg/messaging/errors.go +++ b/mod/async/pkg/messaging/errors.go @@ -56,6 +56,11 @@ var ( ) } + errSendingNilResponse = func(messageID types.MessageID) error { + return errors.Newf("cannot send nil response for route: %s", + messageID) + } + // errIncompatibleAssignee is the error returned when the assignee is not // compatible with the assigner. errIncompatibleAssignee = func( diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go index 4d13f0492f..c0bd32c869 100644 --- a/mod/async/pkg/messaging/route.go +++ b/mod/async/pkg/messaging/route.go @@ -97,6 +97,9 @@ func (r *Route[ReqT, RespT]) SendResponse(resp types.BaseMessage) error { if err != nil { return err } + if typedMsg == nil { + return errSendingNilResponse(r.messageID) + } r.responseCh <- typedMsg return nil } diff --git a/mod/primitives/pkg/messages/messages.go b/mod/primitives/pkg/messages/messages.go index f617ccf987..9d3244995f 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/primitives/pkg/messages/messages.go @@ -28,7 +28,6 @@ const ( ProcessGenesisData = "process-genesis-data" VerifySidecars = "verify-sidecars" ProcessSidecars = "process-sidecars" - ReceiveSidecars = "receive-sidecars" ) // events. From cd0b7bec85fd90f3e8467ff51aa3e17c0b7297e0 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 12 Aug 2024 18:55:30 -0400 Subject: [PATCH 43/75] interface --- mod/beacon/block_store/service.go | 5 ++--- mod/beacon/blockchain/service.go | 5 ++--- mod/beacon/validator/service.go | 5 ++--- mod/da/pkg/da/service.go | 5 ++--- mod/execution/pkg/deposit/service.go | 5 ++--- mod/runtime/pkg/middleware/middleware.go | 6 +++--- 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 527f8c36af..d87a0d169f 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -23,7 +23,6 @@ package blockstore import ( "context" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" @@ -36,7 +35,7 @@ func NewService[ ]( config Config, logger log.Logger[any], - dispatcher *dispatcher.Dispatcher, + dispatcher asynctypes.EventDispatcher, store BlockStoreT, ) *Service[BeaconBlockT, BlockStoreT] { return &Service[BeaconBlockT, BlockStoreT]{ @@ -59,7 +58,7 @@ type Service[ // logger is used for logging information and errors. logger log.Logger[any] // dispatcher is the dispatcher for the service. - dispatcher *dispatcher.Dispatcher + dispatcher asynctypes.EventDispatcher // store is the block store for the service. store BlockStoreT // finalizedBlkEvents is a channel for receiving finalized block events. diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index ec0839625c..2b7cfdece7 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -24,7 +24,6 @@ import ( "context" "sync" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -64,7 +63,7 @@ type Service[ // chainSpec holds the chain specifications. chainSpec common.ChainSpec // dispatcher is the dispatcher for the service. - dispatcher *dispatcher.Dispatcher + dispatcher asynctypes.Dispatcher // executionEngine is the execution engine responsible for processing // execution payloads. executionEngine ExecutionEngine[PayloadAttributesT] @@ -123,7 +122,7 @@ func NewService[ ], logger log.Logger[any], chainSpec common.ChainSpec, - dispatcher *dispatcher.Dispatcher, + dispatcher asynctypes.Dispatcher, executionEngine ExecutionEngine[PayloadAttributesT], localBuilder LocalBuilder[BeaconStateT], stateProcessor StateProcessor[ diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 4cbbe5c068..f4d6b4b8f4 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -23,7 +23,6 @@ package validator import ( "context" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -75,7 +74,7 @@ type Service[ BeaconStateT, DepositT, DepositStoreT, ExecutionPayloadHeaderT, ] // dispatcher is the dispatcher. - dispatcher *dispatcher.Dispatcher + dispatcher asynctypes.MessageDispatcher // stateProcessor is responsible for processing the state. stateProcessor StateProcessor[ BeaconBlockT, @@ -139,7 +138,7 @@ func NewService[ localPayloadBuilder PayloadBuilder[BeaconStateT, ExecutionPayloadT], remotePayloadBuilders []PayloadBuilder[BeaconStateT, ExecutionPayloadT], ts TelemetrySink, - dispatcher *dispatcher.Dispatcher, + dispatcher asynctypes.MessageDispatcher, ) *Service[ AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index f31fce0ab2..89f6330ae4 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -23,7 +23,6 @@ package da import ( "context" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" @@ -41,7 +40,7 @@ type Service[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ] - dispatcher *dispatcher.Dispatcher + dispatcher asynctypes.MessageDispatcher logger log.Logger[any] processSidecarRequests chan asynctypes.Message[BlobSidecarsT] verifySidecarRequests chan asynctypes.Message[BlobSidecarsT] @@ -62,7 +61,7 @@ func NewService[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ], - dispatcher *dispatcher.Dispatcher, + dispatcher asynctypes.MessageDispatcher, logger log.Logger[any], ) *Service[ AvailabilityStoreT, BeaconBlockBodyT, diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 7edd9ef592..5e8834c1b0 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -23,7 +23,6 @@ package deposit import ( "context" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" @@ -49,7 +48,7 @@ type Service[ // ds is the deposit store that stores deposits. ds Store[DepositT] // dispatcher is the dispatcher for the service. - dispatcher *dispatcher.Dispatcher + dispatcher asynctypes.EventDispatcher // finalizedBlockEventID is the event ID for the finalized block event. finalizedBlockEventID asynctypes.EventID // finalizedBlockEvents is the channel that provides finalized block events. @@ -80,7 +79,7 @@ func NewService[ ds Store[DepositT], dc Contract[DepositT], finalizedBlockEventID asynctypes.EventID, - dispatcher *dispatcher.Dispatcher, + dispatcher asynctypes.EventDispatcher, ) *Service[ BeaconBlockT, BeaconBlockBodyT, BlockEventT, DepositT, ExecutionPayloadT, WithdrawalCredentialsT, diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 62856959f9..59b0304e97 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -23,7 +23,7 @@ package middleware import ( "encoding/json" - "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -64,7 +64,7 @@ type ABCIMiddleware[ encoding.ABCIRequest, BeaconBlockT, ] - dispatcher *dispatcher.Dispatcher + dispatcher asynctypes.Dispatcher // metrics is the metrics emitter. metrics *ABCIMiddlewareMetrics // logger is the logger for the middleware. @@ -88,7 +88,7 @@ func NewABCIMiddleware[ chainSpec common.ChainSpec, logger log.Logger[any], telemetrySink TelemetrySink, - dispatcher *dispatcher.Dispatcher, + dispatcher asynctypes.Dispatcher, ) *ABCIMiddleware[ AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, From a66708b0964eda806866434a2443b72c73fd3fbe Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 14 Aug 2024 14:23:41 -0400 Subject: [PATCH 44/75] bet --- mod/async/pkg/server/msg.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go index 9e8aa6b45c..5b57b68e79 100644 --- a/mod/async/pkg/server/msg.go +++ b/mod/async/pkg/server/msg.go @@ -46,10 +46,7 @@ func (ms *MessageServer) Request(req types.BaseMessage, future any) error { if !ok { return errRouteNotFound } - if err := route.SendRequest(req, future); err != nil { - return err - } - return nil + return route.SendRequest(req, future) } // Respond sends a response to the route that corresponds to the response's From 584a23d86c616cb1f2e3a35d201a888a2ad48656 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 14 Aug 2024 16:21:38 -0400 Subject: [PATCH 45/75] events --- mod/async/pkg/dispatcher/dispatcher.go | 2 + mod/async/pkg/messaging/publisher.go | 33 ++--- mod/async/pkg/server/event.go | 2 + mod/async/pkg/types/dispatcher.go | 5 +- mod/async/pkg/types/message.go | 10 +- mod/async/pkg/types/subscription.go | 14 ++ mod/beacon/block_store/service.go | 4 +- .../pkg/components/availability_store.go | 6 +- mod/node-core/pkg/components/block_store.go | 6 +- .../pkg/components/deposit_service.go | 2 +- mod/node-core/pkg/components/deposit_store.go | 6 +- mod/node-core/pkg/components/types.go | 2 +- mod/primitives/pkg/messages/events.go | 25 ++++ mod/primitives/pkg/messages/messages.go | 5 - mod/runtime/pkg/middleware/abci.go | 130 ++++++++++-------- mod/runtime/pkg/middleware/middleware.go | 16 +++ 16 files changed, 172 insertions(+), 96 deletions(-) create mode 100644 mod/async/pkg/types/subscription.go create mode 100644 mod/primitives/pkg/messages/events.go diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index c68502064b..7e5f90c07e 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -104,6 +104,8 @@ func (d *Dispatcher) RegisterPublishers( // Subscribe subscribes the given channel to the event with the given . // It will error if the channel type does not match the event type corresponding // to the . +// Contract: the channel must be a Subscription[T], where T is the expected +// type of the event data. func (d *Dispatcher) Subscribe(eventID types.MessageID, ch any) error { return d.eventServer.Subscribe(eventID, ch) } diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index 11b22fe821..0bd84f4d85 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -30,10 +30,10 @@ import ( // Publisher is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Publisher[T any] struct { +type Publisher[T types.Event[T]] struct { eventID types.EventID - // clients is a map of subscribed clients. - clients map[chan T]struct{} + // subscriptions is a map of subscribed subscriptions. + subscriptions map[types.Subscription[T]]struct{} // msgs is the channel for publishing new messages. msgs chan T // timeout is the timeout for sending a msg to a client. @@ -44,13 +44,13 @@ type Publisher[T any] struct { // NewPublisher creates a new publisher publishing events of type T for the // provided eventID. -func NewPublisher[T any](eventID string) *Publisher[T] { +func NewPublisher[T types.Event[T]](eventID string) *Publisher[T] { return &Publisher[T]{ - eventID: types.EventID(eventID), - clients: make(map[chan T]struct{}), - msgs: make(chan T, defaultBufferSize), - timeout: defaultPublisherTimeout, - mu: sync.Mutex{}, + eventID: types.EventID(eventID), + subscriptions: make(map[types.Subscription[T]]struct{}), + msgs: make(chan T, defaultBufferSize), + timeout: defaultPublisherTimeout, + mu: sync.Mutex{}, } } @@ -97,35 +97,36 @@ func (p *Publisher[T]) Publish(msg types.BaseMessage) error { // Subscribe registers the provided channel to the publisher, // Returns ErrTimeout on timeout. -// TODO: see if its possible to accept a channel instead of any +// Contract: the channel must be a Subscription[T], where T is the expected +// type of the event data. func (p *Publisher[T]) Subscribe(ch any) error { - client, err := ensureType[chan T](ch) + client, err := ensureType[types.Subscription[T]](ch) if err != nil { return err } p.mu.Lock() defer p.mu.Unlock() - p.clients[client] = struct{}{} + p.subscriptions[client] = struct{}{} return nil } // Unsubscribe removes a client from the publisher. // Returns an error if the provided channel is not of type chan T. func (p *Publisher[T]) Unsubscribe(ch any) error { - client, err := ensureType[chan T](ch) + client, err := ensureType[types.Subscription[T]](ch) if err != nil { return err } p.mu.Lock() defer p.mu.Unlock() - delete(p.clients, client) + delete(p.subscriptions, client) close(client) return nil } // broadcast broadcasts a msg to all clients. func (p *Publisher[T]) broadcast(msg T) { - for client := range p.clients { + for client := range p.subscriptions { // send msg to client (or discard msg after timeout) select { case client <- msg: @@ -136,7 +137,7 @@ func (p *Publisher[T]) broadcast(msg T) { // shutdown closes all leftover clients. func (p *Publisher[T]) shutdown() { - for client := range p.clients { + for client := range p.subscriptions { if err := p.Unsubscribe(client); err != nil { panic(err) } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index 188ea6bcc3..ecb4be27c4 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -52,6 +52,8 @@ func (es *EventServer) Publish(event types.BaseMessage) error { // Subscribe subscribes the given channel to the publisher with the given // eventID. It will error if the channel type does not match the event type // corresponding to the publisher. +// Contract: the channel must be a Subscription[T], where T is the expected +// type of the event data. func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { publisher, ok := es.publishers[eventID] if !ok { diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index 1769871e29..854a4aa070 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -42,7 +42,10 @@ type Dispatcher interface { type EventDispatcher interface { // PublishEvent publishes an event to the dispatcher. PublishEvent(event BaseMessage) error - // Subscribe subscribes to an event. + // Subscribe subscribes the given channel to all events with the given event + // ID. + // Contract: the channel must be a Subscription[T], where T is the expected + // type of the event data. Subscribe(eventID EventID, ch any) error } diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/message.go index 8342794704..20aa0dba69 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/message.go @@ -47,6 +47,10 @@ type Message[DataT any] interface { Is(messageType MessageID) bool } +type Event[DataT any] interface { + Message[DataT] +} + // NewEvent creates a new Event with the given context and beacon event. func NewMessage[ DataT any, @@ -63,15 +67,15 @@ func NewMessage[ // Event acts as a type alias for a Message that is meant to be broadcasted // to all subscribers. -type Event[DataT any] struct{ Message[DataT] } +type event[DataT any] struct{ Message[DataT] } // NewEvent creates a new Event with the given context and beacon event. func NewEvent[ DataT any, ]( ctx context.Context, messageType EventID, data DataT, errs ...error, -) *Event[DataT] { - return &Event[DataT]{ +) Event[DataT] { + return &event[DataT]{ Message: NewMessage(ctx, messageType, data, errs...), } } diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go new file mode 100644 index 0000000000..d49bd05a8e --- /dev/null +++ b/mod/async/pkg/types/subscription.go @@ -0,0 +1,14 @@ +package types + +import "context" + +type Subscription[T any] chan Event[T] + +func (s Subscription[T]) Await(ctx context.Context) (Event[T], error) { + select { + case event := <-s: + return event, nil + case <-ctx.Done(): + return event[T]{}, ctx.Err() + } +} diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index d87a0d169f..db0ea16382 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -44,7 +44,7 @@ func NewService[ dispatcher: dispatcher, store: store, // finalizedBlkEvents is a channel for receiving finalized block events. - finalizedBlkEvents: make(chan *asynctypes.Event[BeaconBlockT]), + finalizedBlkEvents: make(chan asynctypes.Event[BeaconBlockT]), } } @@ -62,7 +62,7 @@ type Service[ // store is the block store for the service. store BlockStoreT // finalizedBlkEvents is a channel for receiving finalized block events. - finalizedBlkEvents chan *asynctypes.Event[BeaconBlockT] + finalizedBlkEvents chan asynctypes.Event[BeaconBlockT] } // Name returns the name of the service. diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index cef82d0981..b2a214462e 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -90,7 +90,7 @@ func ProvideAvailabilityPruner( return nil, errors.New("availability store does not have a range db") } - var finalizedBlkCh = make(chan *FinalizedBlockEvent) + var finalizedBlkCh = make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( messages.BeaconBlockFinalizedEvent, finalizedBlkCh, ); err != nil { @@ -102,7 +102,7 @@ func ProvideAvailabilityPruner( // build the availability pruner if IndexDB is available. return pruner.NewPruner[ *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, *IndexDB, ]( in.Logger.With("service", manager.AvailabilityPrunerName), @@ -111,7 +111,7 @@ func ProvideAvailabilityPruner( finalizedBlkCh, dastore.BuildPruneRangeFn[ *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, ](in.ChainSpec), ), nil } diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index b448551d0a..0c3dc3e977 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -79,7 +79,7 @@ type BlockPrunerInput struct { func ProvideBlockPruner( in BlockPrunerInput, ) (BlockPruner, error) { - var finalizedBlkCh = make(chan *FinalizedBlockEvent) + var finalizedBlkCh = make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( messages.BeaconBlockFinalizedEvent, finalizedBlkCh, ); err != nil { @@ -90,7 +90,7 @@ func ProvideBlockPruner( return pruner.NewPruner[ *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, *BlockStore, ]( in.Logger.With("service", manager.BlockPrunerName), @@ -99,7 +99,7 @@ func ProvideBlockPruner( finalizedBlkCh, blockservice.BuildPruneRangeFn[ *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, ](in.Config.BlockStoreService), ), nil } diff --git a/mod/node-core/pkg/components/deposit_service.go b/mod/node-core/pkg/components/deposit_service.go index 1c3b723bc2..f4ba710b79 100644 --- a/mod/node-core/pkg/components/deposit_service.go +++ b/mod/node-core/pkg/components/deposit_service.go @@ -50,7 +50,7 @@ func ProvideDepositService(in DepositServiceIn) (*DepositService, error) { return deposit.NewService[ *BeaconBlockBody, *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, *DepositStore, *ExecutionPayload, ]( diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index d9a275d96b..119b9fb52e 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -70,7 +70,7 @@ type DepositPrunerInput struct { func ProvideDepositPruner( in DepositPrunerInput, ) (DepositPruner, error) { - var finalizedBlkCh = make(chan *FinalizedBlockEvent) + var finalizedBlkCh = make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( messages.BeaconBlockFinalizedEvent, finalizedBlkCh, ); err != nil { @@ -81,7 +81,7 @@ func ProvideDepositPruner( return pruner.NewPruner[ *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, *DepositStore, ]( in.Logger.With("service", manager.DepositPrunerName), @@ -91,7 +91,7 @@ func ProvideDepositPruner( deposit.BuildPruneRangeFn[ *BeaconBlockBody, *BeaconBlock, - *FinalizedBlockEvent, + FinalizedBlockEvent, *Deposit, *ExecutionPayload, WithdrawalCredentials, diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 23bd301bf0..7f02b30b5e 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -220,7 +220,7 @@ type ( DepositService = deposit.Service[ *BeaconBlock, *BeaconBlockBody, - *FinalizedBlockEvent, + FinalizedBlockEvent, *Deposit, *ExecutionPayload, WithdrawalCredentials, diff --git a/mod/primitives/pkg/messages/events.go b/mod/primitives/pkg/messages/events.go new file mode 100644 index 0000000000..2df02a2527 --- /dev/null +++ b/mod/primitives/pkg/messages/events.go @@ -0,0 +1,25 @@ +package messages + +// events. +const ( + BeaconBlockFinalizedEvent = "beacon-block-finalized" +) + +// events, topologically sorted. +const ( + // genesis data events + GenesisDataReceived = "genesis-data-received" + GenesisDataProcessed = "genesis-data-processed" + // pre proposal events + NewSlot = "new-slot" + BuiltBeaconBlock = "built-beacon-block" + BuiltSidecars = "built-sidecars" + // proposal processing events + BeaconBlockReceived = "beacon-block-received" + SidecarsReceived = "sidecars-received" + // finalize block events + FinalBeaconBlockReceived = "final-beacon-block-received" + FinalBlobSidecarsReceived = "final-blob-sidecars-received" + FinalValidatorUpdatesProcessed = "final-validator-updates" + FinalSidecarsProcessed = "final-sidecars-processed" +) diff --git a/mod/primitives/pkg/messages/messages.go b/mod/primitives/pkg/messages/messages.go index 9d3244995f..f8e132600c 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/primitives/pkg/messages/messages.go @@ -29,8 +29,3 @@ const ( VerifySidecars = "verify-sidecars" ProcessSidecars = "process-sidecars" ) - -// events. -const ( - BeaconBlockFinalizedEvent = "beacon-block-finalized" -) diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 2613639ea4..a77105e24a 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -46,24 +46,29 @@ func (h *ABCIMiddleware[ ctx context.Context, bz []byte, ) (transition.ValidatorUpdates, error) { - var err error + var ( + err error + gdpEvent asynctypes.Event[transition.ValidatorUpdates] + ) data := new(GenesisT) if err = json.Unmarshal(bz, data); err != nil { h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } + // TODO: implement service // request for validator updates - valUpdates := asynctypes.NewFuture[transition.ValidatorUpdates]() - if err = h.dispatcher.SendRequest( - asynctypes.NewMessage( - ctx, messages.ProcessGenesisData, *data, - ), valUpdates, + if err = h.dispatcher.PublishEvent( + asynctypes.NewEvent(ctx, messages.GenesisDataReceived, *data), ); err != nil { return nil, err } - return valUpdates.Resolve() + gdpEvent, err = h.subGenDataProcessed.Await(ctx) + if err != nil { + return nil, err + } + return gdpEvent.Data(), gdpEvent.Error() } /* -------------------------------------------------------------------------- */ @@ -77,47 +82,65 @@ func (h *ABCIMiddleware[ ctx context.Context, slotData SlotDataT, ) ([]byte, []byte, error) { - var err error - startTime := time.Now() + var ( + err error + builtBBEvent asynctypes.Event[BeaconBlockT] + builtSCEvent asynctypes.Event[BlobSidecarsT] + startTime = time.Now() + ) defer h.metrics.measurePrepareProposalDuration(startTime) - // request a built beacon block for the given slot - beaconBlkBundleFuture := asynctypes.NewFuture[BeaconBlockBundleT]() - if err = h.dispatcher.SendRequest( - asynctypes.NewMessage( - ctx, messages.BuildBeaconBlockAndSidecars, slotData, - ), beaconBlkBundleFuture, + // TODO: implement service + if err = h.dispatcher.PublishEvent( + asynctypes.NewEvent( + ctx, messages.NewSlot, slotData, + ), ); err != nil { return nil, nil, err } - // resolve the beacon block bundle from the future - beaconBlkBundle, err := beaconBlkBundleFuture.Resolve() + // wait for built beacon block + builtBBEvent, err = h.subBuiltBeaconBlock.Await(ctx) if err != nil { return nil, nil, err } + if err = builtBBEvent.Error(); err != nil { + return nil, nil, err + } + + // wait for built sidecars + builtSCEvent, err = h.subBuiltSidecars.Await(ctx) + if err != nil { + return nil, nil, err + } + if err = builtSCEvent.Error(); err != nil { + return nil, nil, err + } // gossip the built beacon block and blob sidecars - return h.handleBeaconBlockBundleResponse(ctx, beaconBlkBundle) + return h.handleBuiltBeaconBlockAndSidecars( + ctx, builtBBEvent.Data(), builtSCEvent.Data(), + ) } // handleBeaconBlockBundleResponse gossips the built beacon block and blob // sidecars to the network. func (h *ABCIMiddleware[ - _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, _, -]) handleBeaconBlockBundleResponse( + _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, +]) handleBuiltBeaconBlockAndSidecars( ctx context.Context, - bbb BeaconBlockBundleT, + bb BeaconBlockT, + sc BlobSidecarsT, ) ([]byte, []byte, error) { // gossip beacon block bbBz, bbErr := h.beaconBlockGossiper.Publish( - ctx, bbb.GetBeaconBlock(), + ctx, bb, ) if bbErr != nil { return nil, nil, bbErr } // gossip blob sidecars - scBz, scErr := h.blobGossiper.Publish(ctx, bbb.GetSidecars()) + scBz, scErr := h.blobGossiper.Publish(ctx, sc) if scErr != nil { return nil, nil, scErr } @@ -137,10 +160,10 @@ func (h *ABCIMiddleware[ req proto.Message, ) (proto.Message, error) { var ( - blk BeaconBlockT - sidecars BlobSidecarsT err error startTime = time.Now() + blk BeaconBlockT + sidecars BlobSidecarsT ) abciReq, ok := req.(*cmtabci.ProcessProposalRequest) if !ok { @@ -154,12 +177,10 @@ func (h *ABCIMiddleware[ return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - // verify the beacon block - beaconBlockFuture := asynctypes.NewFuture[BeaconBlockT]() - if err = h.dispatcher.SendRequest( - asynctypes.NewMessage( - ctx, messages.VerifyBeaconBlock, blk, - ), beaconBlockFuture, + // TODO: implement service + // notify that the beacon block has been received. + if err = h.dispatcher.PublishEvent( + asynctypes.NewEvent(ctx, messages.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -169,27 +190,22 @@ func (h *ABCIMiddleware[ return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - // verify the blob sidecars - sidecarsFuture := asynctypes.NewFuture[BlobSidecarsT]() - if err = h.dispatcher.SendRequest( - asynctypes.NewMessage( - ctx, messages.VerifySidecars, sidecars, - ), sidecarsFuture, + // notify that the sidecars have been received. + if err = h.dispatcher.PublishEvent( + asynctypes.NewEvent(ctx, messages.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - // error if the beacon block or sidecars are invalid - _, err = beaconBlockFuture.Resolve() + // err if the built beacon block or sidecars failed verification. + _, err = h.subBBVerified.Await(ctx) if err != nil { return h.createProcessProposalResponse(err) } - - _, err = sidecarsFuture.Resolve() + _, err = h.subSCVerified.Await(ctx) if err != nil { return h.createProcessProposalResponse(err) } - return h.createProcessProposalResponse(nil) } @@ -217,9 +233,10 @@ func (h *ABCIMiddleware[ ctx context.Context, req proto.Message, ) (transition.ValidatorUpdates, error) { var ( - blk BeaconBlockT - blobs BlobSidecarsT - err error + err error + blk BeaconBlockT + blobs BlobSidecarsT + finalValUpdatesEvent asynctypes.Event[transition.ValidatorUpdates] ) abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) if !ok { @@ -239,25 +256,22 @@ func (h *ABCIMiddleware[ return nil, nil } - // process the sidecars. - sidecarsFuture := asynctypes.NewFuture[BlobSidecarsT]() - if err = h.dispatcher.SendRequest( - asynctypes.NewMessage( - ctx, messages.ProcessSidecars, blobs, - ), sidecarsFuture, + if err = h.dispatcher.PublishEvent( + asynctypes.NewEvent(ctx, messages.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } - // finalize the beacon block. - valUpdatesFuture := asynctypes.NewFuture[transition.ValidatorUpdates]() - if err = h.dispatcher.SendRequest( - asynctypes.NewMessage( - ctx, messages.FinalizeBeaconBlock, blk, - ), valUpdatesFuture, + if err = h.dispatcher.PublishEvent( + asynctypes.NewEvent(ctx, messages.FinalBlobSidecarsReceived, blobs), ); err != nil { return nil, err } - return valUpdatesFuture.Resolve() + finalValUpdatesEvent, err = h.subFinalValidatorUpdates.Await(ctx) + if err != nil { + return nil, err + } + + return finalValUpdatesEvent.Data(), finalValUpdatesEvent.Error() } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 59b0304e97..50a396c28a 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -28,6 +28,7 @@ import ( "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" ) @@ -69,6 +70,14 @@ type ABCIMiddleware[ metrics *ABCIMiddlewareMetrics // logger is the logger for the middleware. logger log.Logger[any] + + // subscriptions + subGenDataProcessed asynctypes.Subscription[transition.ValidatorUpdates] + subBuiltBeaconBlock asynctypes.Subscription[BeaconBlockT] + subBuiltSidecars asynctypes.Subscription[BlobSidecarsT] + subBBVerified asynctypes.Subscription[BeaconBlockT] + subSCVerified asynctypes.Subscription[BlobSidecarsT] + subFinalValidatorUpdates asynctypes.Subscription[transition.ValidatorUpdates] } // NewABCIMiddleware creates a new instance of the Handler struct. @@ -113,6 +122,13 @@ func NewABCIMiddleware[ } } +func (am *ABCIMiddleware[ + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, + ExecutionPayloadT, GenesisT, SlotDataT, +]) Start() asynctypes.Subscription[transition.ValidatorUpdates] { + return am.subFinalValidatorUpdates +} + // Name returns the name of the middleware. func (am *ABCIMiddleware[ AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, From 8ba8d5145a9c848e9b0ba1535dbb707b239550f2 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Wed, 14 Aug 2024 16:52:47 -0400 Subject: [PATCH 46/75] bet --- mod/async/pkg/messaging/publisher.go | 4 +- mod/async/pkg/types/subscription.go | 10 ++-- mod/primitives/pkg/messages/events.go | 4 +- mod/runtime/pkg/middleware/middleware.go | 60 +++++++++++++++++++----- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index 0bd84f4d85..83cbcc53a2 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -30,7 +30,7 @@ import ( // Publisher is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Publisher[T types.Event[T]] struct { +type Publisher[T any] struct { eventID types.EventID // subscriptions is a map of subscribed subscriptions. subscriptions map[types.Subscription[T]]struct{} @@ -44,7 +44,7 @@ type Publisher[T types.Event[T]] struct { // NewPublisher creates a new publisher publishing events of type T for the // provided eventID. -func NewPublisher[T types.Event[T]](eventID string) *Publisher[T] { +func NewPublisher[T any](eventID string) *Publisher[T] { return &Publisher[T]{ eventID: types.EventID(eventID), subscriptions: make(map[types.Subscription[T]]struct{}), diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go index d49bd05a8e..8544fa1720 100644 --- a/mod/async/pkg/types/subscription.go +++ b/mod/async/pkg/types/subscription.go @@ -2,13 +2,17 @@ package types import "context" -type Subscription[T any] chan Event[T] +type Subscription[T any] chan T -func (s Subscription[T]) Await(ctx context.Context) (Event[T], error) { +func NewSubscription[T any]() Subscription[T] { + return make(chan T) +} + +func (s Subscription[T]) Await(ctx context.Context) (T, error) { select { case event := <-s: return event, nil case <-ctx.Done(): - return event[T]{}, ctx.Err() + return *new(T), ctx.Err() } } diff --git a/mod/primitives/pkg/messages/events.go b/mod/primitives/pkg/messages/events.go index 2df02a2527..a5343c9bd0 100644 --- a/mod/primitives/pkg/messages/events.go +++ b/mod/primitives/pkg/messages/events.go @@ -17,9 +17,11 @@ const ( // proposal processing events BeaconBlockReceived = "beacon-block-received" SidecarsReceived = "sidecars-received" + BeaconBlockVerified = "beacon-block-verified" + SidecarsVerified = "sidecars-verified" // finalize block events FinalBeaconBlockReceived = "final-beacon-block-received" FinalBlobSidecarsReceived = "final-blob-sidecars-received" FinalValidatorUpdatesProcessed = "final-validator-updates" - FinalSidecarsProcessed = "final-sidecars-processed" + // FinalSidecarsProcessed = "final-sidecars-processed" ) diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 50a396c28a..2ea55ac608 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -28,6 +28,7 @@ import ( "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" + "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" @@ -71,13 +72,12 @@ type ABCIMiddleware[ // logger is the logger for the middleware. logger log.Logger[any] - // subscriptions - subGenDataProcessed asynctypes.Subscription[transition.ValidatorUpdates] - subBuiltBeaconBlock asynctypes.Subscription[BeaconBlockT] - subBuiltSidecars asynctypes.Subscription[BlobSidecarsT] - subBBVerified asynctypes.Subscription[BeaconBlockT] - subSCVerified asynctypes.Subscription[BlobSidecarsT] - subFinalValidatorUpdates asynctypes.Subscription[transition.ValidatorUpdates] + subGenDataProcessed asynctypes.Subscription[asynctypes.Event[transition.ValidatorUpdates]] + subBuiltBeaconBlock asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] + subBuiltSidecars asynctypes.Subscription[asynctypes.Event[BlobSidecarsT]] + subBBVerified asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] + subSCVerified asynctypes.Subscription[asynctypes.Event[BlobSidecarsT]] + subFinalValidatorUpdates asynctypes.Subscription[asynctypes.Event[transition.ValidatorUpdates]] } // NewABCIMiddleware creates a new instance of the Handler struct. @@ -116,17 +116,53 @@ func NewABCIMiddleware[ ]( chainSpec, ), - logger: logger, - metrics: newABCIMiddlewareMetrics(telemetrySink), - dispatcher: dispatcher, + logger: logger, + metrics: newABCIMiddlewareMetrics(telemetrySink), + dispatcher: dispatcher, + subGenDataProcessed: asynctypes.NewSubscription[asynctypes.Event[transition.ValidatorUpdates]](), + subBuiltBeaconBlock: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), + subBuiltSidecars: asynctypes.NewSubscription[asynctypes.Event[BlobSidecarsT]](), + subBBVerified: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), + subSCVerified: asynctypes.NewSubscription[asynctypes.Event[BlobSidecarsT]](), + subFinalValidatorUpdates: asynctypes.NewSubscription[asynctypes.Event[transition.ValidatorUpdates]](), } } func (am *ABCIMiddleware[ AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, -]) Start() asynctypes.Subscription[transition.ValidatorUpdates] { - return am.subFinalValidatorUpdates +]) Start() error { + if err := am.dispatcher.Subscribe( + messages.GenesisDataProcessed, am.subGenDataProcessed, + ); err != nil { + return err + } + if err := am.dispatcher.Subscribe( + messages.BuiltBeaconBlock, am.subBuiltBeaconBlock, + ); err != nil { + return err + } + if err := am.dispatcher.Subscribe( + messages.BuiltSidecars, am.subBuiltSidecars, + ); err != nil { + return err + } + if err := am.dispatcher.Subscribe( + messages.BeaconBlockVerified, am.subBBVerified, + ); err != nil { + return err + } + if err := am.dispatcher.Subscribe( + messages.SidecarsVerified, am.subSCVerified, + ); err != nil { + return err + } + if err := am.dispatcher.Subscribe( + messages.FinalValidatorUpdatesProcessed, am.subFinalValidatorUpdates, + ); err != nil { + return err + } + return nil } // Name returns the name of the middleware. From 6abc5a8a46e7da466090ff399617db2dcaa662db Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 15 Aug 2024 14:47:06 -0400 Subject: [PATCH 47/75] sub --- mod/async/pkg/messaging/publisher.go | 4 +- mod/async/pkg/types/subscription.go | 54 +++++++++++++++++++++++++- mod/node-core/pkg/components/events.go | 2 +- mod/node-core/pkg/components/types.go | 2 +- mod/primitives/pkg/messages/events.go | 20 ++++++++++ mod/runtime/pkg/middleware/abci.go | 14 +++++-- 6 files changed, 87 insertions(+), 9 deletions(-) diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/messaging/publisher.go index 83cbcc53a2..520d41f3f0 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/messaging/publisher.go @@ -30,7 +30,7 @@ import ( // Publisher is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Publisher[T any] struct { +type Publisher[T types.BaseMessage] struct { eventID types.EventID // subscriptions is a map of subscribed subscriptions. subscriptions map[types.Subscription[T]]struct{} @@ -44,7 +44,7 @@ type Publisher[T any] struct { // NewPublisher creates a new publisher publishing events of type T for the // provided eventID. -func NewPublisher[T any](eventID string) *Publisher[T] { +func NewPublisher[T types.BaseMessage](eventID string) *Publisher[T] { return &Publisher[T]{ eventID: types.EventID(eventID), subscriptions: make(map[types.Subscription[T]]struct{}), diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go index 8544fa1720..5f542b869f 100644 --- a/mod/async/pkg/types/subscription.go +++ b/mod/async/pkg/types/subscription.go @@ -1,13 +1,63 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + package types import "context" -type Subscription[T any] chan T +type EventHandler[T BaseMessage] func(T) error + +// Subscription is a channel that receives events. +type Subscription[T BaseMessage] chan T -func NewSubscription[T any]() Subscription[T] { +// NewSubscription creates a new subscription with an unbuffered channel as the +// underlying type. +func NewSubscription[T BaseMessage]() Subscription[T] { return make(chan T) } +// Clear clears the subscription channel. +func (s Subscription[T]) Clear() { + for len(s) > 0 { + <-s + } +} + +// RegisterHandler will start a goroutine that will trigger the handler for each +// event in the subscription. +func RegisterHandler[T BaseMessage]( + s Subscription[T], handler EventHandler[T], +) { + go func() { + for event := range s { + go func(e T) { + if err := handler(e); err != nil { + panic(err) + } + }(event) + } + }() +} + +// Await will block until an event is received from the subscription or the +// context is canceled. func (s Subscription[T]) Await(ctx context.Context) (T, error) { select { case event := <-s: diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index eef0b7942d..255d7715d5 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -36,7 +36,7 @@ func ProvideEventServer() *EventServer { // finalized events. func ProvidePublishers() []asynctypes.Publisher { return []asynctypes.Publisher{ - messaging.NewPublisher[*FinalizedBlockEvent]( + messaging.NewPublisher[FinalizedBlockEvent]( messages.BeaconBlockFinalizedEvent, ), } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 7f02b30b5e..cb9a91acbb 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -473,7 +473,7 @@ type ( /* -------------------------------------------------------------------------- */ type ( - BeaconBlockFinalizedPublisher = messaging.Publisher[*FinalizedBlockEvent] + BeaconBlockFinalizedPublisher = messaging.Publisher[FinalizedBlockEvent] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/messages/events.go b/mod/primitives/pkg/messages/events.go index a5343c9bd0..d3cf183c7c 100644 --- a/mod/primitives/pkg/messages/events.go +++ b/mod/primitives/pkg/messages/events.go @@ -1,3 +1,23 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + package messages // events. diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index a77105e24a..722c3d9968 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -50,14 +50,15 @@ func (h *ABCIMiddleware[ err error gdpEvent asynctypes.Event[transition.ValidatorUpdates] ) + // in theory this channel should already be empty, but we clear it anyways + h.subGenDataProcessed.Clear() + data := new(GenesisT) if err = json.Unmarshal(bz, data); err != nil { h.logger.Error("Failed to unmarshal genesis data", "error", err) return nil, err } - // TODO: implement service - // request for validator updates if err = h.dispatcher.PublishEvent( asynctypes.NewEvent(ctx, messages.GenesisDataReceived, *data), ); err != nil { @@ -89,8 +90,10 @@ func (h *ABCIMiddleware[ startTime = time.Now() ) defer h.metrics.measurePrepareProposalDuration(startTime) + // in theory these subs should already be empty, but we clear them anyways + h.subBuiltBeaconBlock.Clear() + h.subBuiltSidecars.Clear() - // TODO: implement service if err = h.dispatcher.PublishEvent( asynctypes.NewEvent( ctx, messages.NewSlot, slotData, @@ -165,6 +168,9 @@ func (h *ABCIMiddleware[ blk BeaconBlockT sidecars BlobSidecarsT ) + // in theory these subs should already be empty, but we clear them anyways + h.subBBVerified.Clear() + h.subSCVerified.Clear() abciReq, ok := req.(*cmtabci.ProcessProposalRequest) if !ok { return nil, ErrInvalidProcessProposalRequestType @@ -238,6 +244,8 @@ func (h *ABCIMiddleware[ blobs BlobSidecarsT finalValUpdatesEvent asynctypes.Event[transition.ValidatorUpdates] ) + // in theory this sub should already be empty, but we clear them anyways + h.subFinalValidatorUpdates.Clear() abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) if !ok { return nil, ErrInvalidFinalizeBlockRequestType From b2ca9be8392135318a5134b816c25a0bf3efb30a Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 15 Aug 2024 16:53:04 -0400 Subject: [PATCH 48/75] validator --- mod/async/pkg/types/subscription.go | 25 +++--- mod/beacon/blockchain/service.go | 103 ++++++++++------------- mod/beacon/validator/block_builder.go | 16 ++-- mod/beacon/validator/service.go | 93 ++++++++------------ mod/runtime/pkg/middleware/abci.go | 22 ++--- mod/runtime/pkg/middleware/middleware.go | 53 ++++++------ mod/runtime/pkg/middleware/types.go | 2 + 7 files changed, 144 insertions(+), 170 deletions(-) diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go index 5f542b869f..ba3860ead5 100644 --- a/mod/async/pkg/types/subscription.go +++ b/mod/async/pkg/types/subscription.go @@ -22,8 +22,6 @@ package types import "context" -type EventHandler[T BaseMessage] func(T) error - // Subscription is a channel that receives events. type Subscription[T BaseMessage] chan T @@ -40,18 +38,21 @@ func (s Subscription[T]) Clear() { } } -// RegisterHandler will start a goroutine that will trigger the handler for each -// event in the subscription. -func RegisterHandler[T BaseMessage]( - s Subscription[T], handler EventHandler[T], +// Listen will start a goroutine that will trigger the handler for each event +// in the subscription. +func (s Subscription[T]) Listen( + ctx context.Context, handler func(T), ) { go func() { - for event := range s { - go func(e T) { - if err := handler(e); err != nil { - panic(err) - } - }(event) + for { + select { + case <-ctx.Done(): + return + case event := <-s: + go func(e T) { + handler(e) + }(event) + } } }() } diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 2b7cfdece7..606fbba501 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -85,13 +85,14 @@ type Service[ // forceStartupSyncOnce is used to force a sync of the startup head. forceStartupSyncOnce *sync.Once - // finalizeBlkReqs is a channel for receiving finalize beacon block + // subFinalBlkReceived is a channel for receiving finalize beacon block // requests. - finalizeBlkReqs chan asynctypes.Message[BeaconBlockT] - // verifyBlkReqs is a channel for receiving verify beacon block requests. - verifyBlkReqs chan asynctypes.Message[BeaconBlockT] - // processGenReqs is a channel for receiving process genesis data requests. - processGenReqs chan asynctypes.Message[GenesisT] + subFinalBlkReceived asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] + // subBlockReceived is a channel for receiving verify beacon block requests. + subBlockReceived asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] + // subGenDataReceived is a subscription for receiving genesis data + // received events. + subGenDataReceived asynctypes.Subscription[asynctypes.Event[GenesisT]] } // NewService creates a new validator service. @@ -154,9 +155,9 @@ func NewService[ metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), - finalizeBlkReqs: make(chan asynctypes.Message[BeaconBlockT]), - verifyBlkReqs: make(chan asynctypes.Message[BeaconBlockT]), - processGenReqs: make(chan asynctypes.Message[GenesisT]), + subFinalBlkReceived: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), + subBlockReceived: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), + subGenDataReceived: asynctypes.NewSubscription[asynctypes.Event[GenesisT]](), } } @@ -173,64 +174,51 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) Start(ctx context.Context) error { - // register a channel as the receiver for FinalizeBeaconBlock: - if err := s.dispatcher.RegisterMsgReceiver( - messages.FinalizeBeaconBlock, s.finalizeBlkReqs, + if err := s.dispatcher.Subscribe( + messages.GenesisDataReceived, s.subGenDataReceived, ); err != nil { return err } - // register a channel as the receiver for VerifyBeaconBlock: - if err := s.dispatcher.RegisterMsgReceiver( - messages.VerifyBeaconBlock, s.verifyBlkReqs, + + if err := s.dispatcher.Subscribe( + messages.BeaconBlockReceived, s.subBlockReceived, ); err != nil { return err } - // register a channel as the receiver for ProcessGenesisData: - if err := s.dispatcher.RegisterMsgReceiver( - messages.ProcessGenesisData, s.processGenReqs, + + if err := s.dispatcher.Subscribe( + messages.FinalBeaconBlockReceived, s.subFinalBlkReceived, ); err != nil { return err } // start a goroutine to listen for requests and handle accordingly - go s.start(ctx) + s.listen(ctx) return nil } -// start starts the service. +// listen listens for events and handles them accordingly. func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, -]) start(ctx context.Context) { - for { - select { - case <-ctx.Done(): - return - case msg := <-s.finalizeBlkReqs: - s.handleFinalizeBeaconBlockRequest(msg) - case msg := <-s.verifyBlkReqs: - s.handleVerifyBeaconBlockRequest(msg) - case msg := <-s.processGenReqs: - s.handleProcessGenesisDataRequest(msg) - } - } +]) listen(ctx context.Context) { + s.subGenDataReceived.Listen(ctx, s.handleProcessGenesisDataEvent) + s.subBlockReceived.Listen(ctx, s.handleVerifyBeaconBlockEvent) + s.subFinalBlkReceived.Listen(ctx, s.handleFinalizeBeaconBlockEvent) } /* -------------------------------------------------------------------------- */ /* Message Handlers */ /* -------------------------------------------------------------------------- */ -// handleProcessGenesisDataRequest processes the given genesis data and -// dispatches a response. func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, _, -]) handleProcessGenesisDataRequest(msg asynctypes.Message[GenesisT]) { +]) handleProcessGenesisDataEvent(msg asynctypes.Event[GenesisT]) { var ( valUpdates transition.ValidatorUpdates err error ) if msg.Error() != nil { s.logger.Error("Error processing genesis data", "error", msg.Error()) - return } // Process the genesis data. @@ -239,17 +227,17 @@ func (s *Service[ s.logger.Error("Failed to process genesis data", "error", err) } - // dispatch a response containing the validator updates - if err = s.dispatcher.SendResponse( - asynctypes.NewMessage( + // Emit the event containing the validator updates. + if err = s.dispatcher.PublishEvent( + asynctypes.NewEvent( msg.Context(), - messages.ProcessGenesisData, + messages.GenesisDataProcessed, valUpdates, - nil, + err, ), ); err != nil { s.logger.Error( - "Failed to dispatch response in process genesis data", + "Failed to emit event in process genesis data", "error", err, ) } @@ -257,8 +245,8 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, -]) handleVerifyBeaconBlockRequest( - msg asynctypes.Message[BeaconBlockT], +]) handleVerifyBeaconBlockEvent( + msg asynctypes.Event[BeaconBlockT], ) { // If the block is nil, exit early. if msg.Error() != nil { @@ -266,17 +254,18 @@ func (s *Service[ return } - // dispatch a response with the error result from VerifyIncomingBlock - if err := s.dispatcher.SendResponse( - asynctypes.NewMessage( + // emit a BeaconBlockVerified event with the error result from \ + // VerifyIncomingBlock + if err := s.dispatcher.PublishEvent( + asynctypes.NewEvent( msg.Context(), - messages.VerifyBeaconBlock, + messages.BeaconBlockVerified, msg.Data(), s.VerifyIncomingBlock(msg.Context(), msg.Data()), ), ); err != nil { s.logger.Error( - "Failed to dispatch response in verify beacon block", + "Failed to emit event in verify beacon block", "error", err, ) } @@ -284,8 +273,8 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, -]) handleFinalizeBeaconBlockRequest( - msg asynctypes.Message[BeaconBlockT], +]) handleFinalizeBeaconBlockEvent( + msg asynctypes.Event[BeaconBlockT], ) { var ( valUpdates transition.ValidatorUpdates @@ -303,17 +292,17 @@ func (s *Service[ s.logger.Error("Failed to process verified beacon block", "error", err) } - // dispatch a response with the validator updates - if err = s.dispatcher.SendResponse( - asynctypes.NewMessage( + // Emit the event containing the validator updates. + if err = s.dispatcher.PublishEvent( + asynctypes.NewEvent( msg.Context(), - messages.FinalizeBeaconBlock, + messages.FinalValidatorUpdatesProcessed, valUpdates, err, ), ); err != nil { s.logger.Error( - "Failed to dispatch response in finalize beacon block", + "Failed to emit event in finalize beacon block", "error", err, ) } diff --git a/mod/beacon/validator/block_builder.go b/mod/beacon/validator/block_builder.go index 93b3e0c7cb..c94ff5c16e 100644 --- a/mod/beacon/validator/block_builder.go +++ b/mod/beacon/validator/block_builder.go @@ -37,8 +37,8 @@ import ( // buildBlockAndSidecars builds a new beacon block. func (s *Service[ - AttestationDataT, BeaconBlockT, _, _, _, - BlobSidecarsT, _, _, _, _, _, _, SlashingInfoT, SlotDataT, + AttestationDataT, BeaconBlockT, _, _, BlobSidecarsT, _, _, _, _, _, _, + SlashingInfoT, SlotDataT, ]) buildBlockAndSidecars( ctx context.Context, slotData SlotDataT, @@ -134,7 +134,7 @@ func (s *Service[ // getEmptyBeaconBlockForSlot creates a new empty block. func (s *Service[ - _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _, ]) getEmptyBeaconBlockForSlot( st BeaconStateT, requestedSlot math.Slot, ) (BeaconBlockT, error) { @@ -172,7 +172,7 @@ func (s *Service[ // buildRandaoReveal builds a randao reveal for the given slot. func (s *Service[ - _, _, _, _, BeaconStateT, _, _, _, _, _, _, ForkDataT, _, _, + _, _, _, BeaconStateT, _, _, _, _, _, _, ForkDataT, _, _, ]) buildRandaoReveal( st BeaconStateT, slot math.Slot, @@ -200,7 +200,7 @@ func (s *Service[ // retrieveExecutionPayload retrieves the execution payload for the block. func (s *Service[ - _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, + _, BeaconBlockT, _, BeaconStateT, _, _, _, _, ExecutionPayloadT, ExecutionPayloadHeaderT, _, _, _, ]) retrieveExecutionPayload( ctx context.Context, st BeaconStateT, blk BeaconBlockT, @@ -257,7 +257,7 @@ func (s *Service[ // BuildBlockBody assembles the block body with necessary components. func (s *Service[ - AttestationDataT, BeaconBlockT, _, _, BeaconStateT, _, + AttestationDataT, BeaconBlockT, _, BeaconStateT, _, _, _, Eth1DataT, ExecutionPayloadT, _, _, SlashingInfoT, SlotDataT, ]) buildBlockBody( _ context.Context, @@ -333,7 +333,7 @@ func (s *Service[ // computeAndSetStateRoot computes the state root of an outgoing block // and sets it in the block. func (s *Service[ - _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _, ]) computeAndSetStateRoot( ctx context.Context, st BeaconStateT, @@ -354,7 +354,7 @@ func (s *Service[ // computeStateRoot computes the state root of an outgoing block. func (s *Service[ - _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, + _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _, ]) computeStateRoot( ctx context.Context, st BeaconStateT, diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index f4d6b4b8f4..6f9457e9a3 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -23,7 +23,7 @@ package validator import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" @@ -31,16 +31,13 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) -// Service is responsible for building beacon blocks. +// Service is responsible for building beacon blocks and sidecars. type Service[ AttestationDataT any, BeaconBlockT BeaconBlock[ AttestationDataT, BeaconBlockT, BeaconBlockBodyT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], - BeaconBlockBundleT BeaconBlockBundle[ - BeaconBlockBundleT, BeaconBlockT, BlobSidecarsT, - ], BeaconBlockBodyT BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT, ], @@ -55,7 +52,6 @@ type Service[ SlashingInfoT any, SlotDataT SlotData[AttestationDataT, SlashingInfoT], ] struct { - buildBlkBundleReqs chan asynctypes.Message[SlotDataT] // cfg is the validator config. cfg *Config // logger is a logger. @@ -74,7 +70,7 @@ type Service[ BeaconStateT, DepositT, DepositStoreT, ExecutionPayloadHeaderT, ] // dispatcher is the dispatcher. - dispatcher asynctypes.MessageDispatcher + dispatcher async.EventDispatcher // stateProcessor is responsible for processing the state. stateProcessor StateProcessor[ BeaconBlockT, @@ -92,6 +88,8 @@ type Service[ remotePayloadBuilders []PayloadBuilder[BeaconStateT, ExecutionPayloadT] // metrics is a metrics collector. metrics *validatorMetrics + // subNewSlot is a channel for new slot events. + subNewSlot async.Subscription[async.Event[SlotDataT]] } // NewService creates a new validator service. @@ -138,22 +136,20 @@ func NewService[ localPayloadBuilder PayloadBuilder[BeaconStateT, ExecutionPayloadT], remotePayloadBuilders []PayloadBuilder[BeaconStateT, ExecutionPayloadT], ts TelemetrySink, - dispatcher asynctypes.MessageDispatcher, + dispatcher async.EventDispatcher, ) *Service[ - AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, - BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, - ExecutionPayloadT, ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, - SlotDataT, + AttestationDataT, BeaconBlockT, BeaconBlockBodyT, BeaconStateT, + BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, + ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, SlotDataT, ] { return &Service[ - AttestationDataT, BeaconBlockT, BeaconBlockBundleT, BeaconBlockBodyT, + AttestationDataT, BeaconBlockT, BeaconBlockBodyT, BeaconStateT, BlobSidecarsT, DepositT, DepositStoreT, Eth1DataT, ExecutionPayloadT, ExecutionPayloadHeaderT, ForkDataT, SlashingInfoT, SlotDataT, ]{ cfg: cfg, logger: logger, - buildBlkBundleReqs: make(chan asynctypes.Message[SlotDataT]), bsb: bsb, chainSpec: chainSpec, signer: signer, @@ -163,61 +159,42 @@ func NewService[ remotePayloadBuilders: remotePayloadBuilders, metrics: newValidatorMetrics(ts), dispatcher: dispatcher, + subNewSlot: async.NewSubscription[async.Event[SlotDataT]](), } } // Name returns the name of the service. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, ]) Name() string { return "validator" } -// Start starts the service registers this service with the -// BuildBeaconBlockAndSidecars route and begins listening for requests. +// Start listens for NewSlot events and builds a block and sidecars for the +// requested slot data. func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, + _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, ]) Start( ctx context.Context, ) error { - // register the receiver channel for build block requests - if err := s.dispatcher.RegisterMsgReceiver( - messages.BuildBeaconBlockAndSidecars, s.buildBlkBundleReqs, - ); err != nil { + // subscribe to new slot events + err := s.dispatcher.Subscribe(messages.NewSlot, s.subNewSlot) + if err != nil { return err } - - // start a goroutine to listen for requests and handle accordingly - go s.start(ctx) + // set the handler for new slot events + s.subNewSlot.Listen(ctx, s.handleNewSlot) return nil } -// start starts the service. -func (s *Service[ - _, _, _, _, _, _, _, _, _, _, _, _, _, SlotDataT, -]) start( - ctx context.Context, -) { - for { - select { - case <-ctx.Done(): - return - case req := <-s.buildBlkBundleReqs: - s.handleBuildBlockBundleRequest(req) - } - } -} - -// handleBuildBlockBundleRequest builds a block and sidecars for the requested -// slot data and dispatches a response containing the built block and sidecars. +// handleNewSlot builds a block and sidecars for the requested +// slot data and emits an event containing the built block and sidecars. func (s *Service[ - _, BeaconBlockT, BeaconBlockBundleT, _, _, BlobSidecarsT, _, _, _, _, _, _, - _, SlotDataT, -]) handleBuildBlockBundleRequest(req asynctypes.Message[SlotDataT]) { + _, BeaconBlockT, _, _, BlobSidecarsT, _, _, _, _, _, _, _, SlotDataT, +]) handleNewSlot(req async.Event[SlotDataT]) { var ( blk BeaconBlockT sidecars BlobSidecarsT - blkData BeaconBlockBundleT err error ) // build the block and sidecars for the requested slot data @@ -228,15 +205,17 @@ func (s *Service[ s.logger.Error("failed to build block", "err", err) } - // bundle the block and sidecars and dispatch the response - // blkData := *new(BeaconBlockBundleT) - blkData = blkData.New(blk, sidecars) - if err = s.dispatcher.SendResponse( - asynctypes.NewMessage( - req.Context(), - messages.BuildBeaconBlockAndSidecars, - blkData, - )); err != nil { - s.logger.Error("failed to respond", "err", err) + // emit a built block event with the built block and the error + if bbErr := s.dispatcher.PublishEvent( + async.NewEvent(req.Context(), messages.BuiltBeaconBlock, blk, err), + ); bbErr != nil { + s.logger.Error("failed to dispatch built block", "err", err) + } + + // emit a built sidecars event with the built sidecars and the error + if scErr := s.dispatcher.PublishEvent( + async.NewEvent(req.Context(), messages.BuiltSidecars, sidecars, err), + ); scErr != nil { + s.logger.Error("failed to dispatch built sidecars", "err", err) } } diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 722c3d9968..834c4cd99e 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -24,7 +24,7 @@ import ( "context" "time" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" @@ -48,7 +48,7 @@ func (h *ABCIMiddleware[ ) (transition.ValidatorUpdates, error) { var ( err error - gdpEvent asynctypes.Event[transition.ValidatorUpdates] + gdpEvent async.Event[transition.ValidatorUpdates] ) // in theory this channel should already be empty, but we clear it anyways h.subGenDataProcessed.Clear() @@ -60,7 +60,7 @@ func (h *ABCIMiddleware[ } if err = h.dispatcher.PublishEvent( - asynctypes.NewEvent(ctx, messages.GenesisDataReceived, *data), + async.NewEvent(ctx, messages.GenesisDataReceived, *data), ); err != nil { return nil, err } @@ -85,8 +85,8 @@ func (h *ABCIMiddleware[ ) ([]byte, []byte, error) { var ( err error - builtBBEvent asynctypes.Event[BeaconBlockT] - builtSCEvent asynctypes.Event[BlobSidecarsT] + builtBBEvent async.Event[BeaconBlockT] + builtSCEvent async.Event[BlobSidecarsT] startTime = time.Now() ) defer h.metrics.measurePrepareProposalDuration(startTime) @@ -95,7 +95,7 @@ func (h *ABCIMiddleware[ h.subBuiltSidecars.Clear() if err = h.dispatcher.PublishEvent( - asynctypes.NewEvent( + async.NewEvent( ctx, messages.NewSlot, slotData, ), ); err != nil { @@ -186,7 +186,7 @@ func (h *ABCIMiddleware[ // TODO: implement service // notify that the beacon block has been received. if err = h.dispatcher.PublishEvent( - asynctypes.NewEvent(ctx, messages.BeaconBlockReceived, blk), + async.NewEvent(ctx, messages.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -198,7 +198,7 @@ func (h *ABCIMiddleware[ // notify that the sidecars have been received. if err = h.dispatcher.PublishEvent( - asynctypes.NewEvent(ctx, messages.SidecarsReceived, sidecars), + async.NewEvent(ctx, messages.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -242,7 +242,7 @@ func (h *ABCIMiddleware[ err error blk BeaconBlockT blobs BlobSidecarsT - finalValUpdatesEvent asynctypes.Event[transition.ValidatorUpdates] + finalValUpdatesEvent async.Event[transition.ValidatorUpdates] ) // in theory this sub should already be empty, but we clear them anyways h.subFinalValidatorUpdates.Clear() @@ -265,13 +265,13 @@ func (h *ABCIMiddleware[ } if err = h.dispatcher.PublishEvent( - asynctypes.NewEvent(ctx, messages.FinalBeaconBlockReceived, blk), + async.NewEvent(ctx, messages.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } if err = h.dispatcher.PublishEvent( - asynctypes.NewEvent(ctx, messages.FinalBlobSidecarsReceived, blobs), + async.NewEvent(ctx, messages.FinalBlobSidecarsReceived, blobs), ); err != nil { return nil, err } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 2ea55ac608..8486980f30 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -23,13 +23,12 @@ package middleware import ( "encoding/json" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" - "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" ) @@ -66,21 +65,23 @@ type ABCIMiddleware[ encoding.ABCIRequest, BeaconBlockT, ] - dispatcher asynctypes.Dispatcher + dispatcher types.Dispatcher // metrics is the metrics emitter. metrics *ABCIMiddlewareMetrics // logger is the logger for the middleware. logger log.Logger[any] - - subGenDataProcessed asynctypes.Subscription[asynctypes.Event[transition.ValidatorUpdates]] - subBuiltBeaconBlock asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] - subBuiltSidecars asynctypes.Subscription[asynctypes.Event[BlobSidecarsT]] - subBBVerified asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] - subSCVerified asynctypes.Subscription[asynctypes.Event[BlobSidecarsT]] - subFinalValidatorUpdates asynctypes.Subscription[asynctypes.Event[transition.ValidatorUpdates]] + // subscription channels + subGenDataProcessed types.Subscription[types.Event[validatorUpdates]] + subBuiltBeaconBlock types.Subscription[types.Event[BeaconBlockT]] + subBuiltSidecars types.Subscription[types.Event[BlobSidecarsT]] + subBBVerified types.Subscription[types.Event[BeaconBlockT]] + subSCVerified types.Subscription[types.Event[BlobSidecarsT]] + subFinalValidatorUpdates types.Subscription[types.Event[validatorUpdates]] } // NewABCIMiddleware creates a new instance of the Handler struct. +// +//nolint:lll // long types func NewABCIMiddleware[ AvailabilityStoreT any, BeaconBlockT BeaconBlock[BeaconBlockT], @@ -97,14 +98,14 @@ func NewABCIMiddleware[ chainSpec common.ChainSpec, logger log.Logger[any], telemetrySink TelemetrySink, - dispatcher asynctypes.Dispatcher, + dispatcher types.Dispatcher, ) *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, - ExecutionPayloadT, GenesisT, SlotDataT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, + DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ] { return &ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, - ExecutionPayloadT, GenesisT, SlotDataT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, + DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ]{ chainSpec: chainSpec, blobGossiper: rp2p.NewNoopBlobHandler[ @@ -119,19 +120,21 @@ func NewABCIMiddleware[ logger: logger, metrics: newABCIMiddlewareMetrics(telemetrySink), dispatcher: dispatcher, - subGenDataProcessed: asynctypes.NewSubscription[asynctypes.Event[transition.ValidatorUpdates]](), - subBuiltBeaconBlock: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), - subBuiltSidecars: asynctypes.NewSubscription[asynctypes.Event[BlobSidecarsT]](), - subBBVerified: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), - subSCVerified: asynctypes.NewSubscription[asynctypes.Event[BlobSidecarsT]](), - subFinalValidatorUpdates: asynctypes.NewSubscription[asynctypes.Event[transition.ValidatorUpdates]](), + subGenDataProcessed: types.NewSubscription[types.Event[validatorUpdates]](), + subBuiltBeaconBlock: types.NewSubscription[types.Event[BeaconBlockT]](), + subBuiltSidecars: types.NewSubscription[types.Event[BlobSidecarsT]](), + subBBVerified: types.NewSubscription[types.Event[BeaconBlockT]](), + subSCVerified: types.NewSubscription[types.Event[BlobSidecarsT]](), + subFinalValidatorUpdates: types.NewSubscription[types.Event[validatorUpdates]](), } } +// Start subscribes the middleware to the events it needs to listen for. func (am *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, - ExecutionPayloadT, GenesisT, SlotDataT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, + DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ]) Start() error { + // subGenDat if err := am.dispatcher.Subscribe( messages.GenesisDataProcessed, am.subGenDataProcessed, ); err != nil { @@ -167,8 +170,8 @@ func (am *ABCIMiddleware[ // Name returns the name of the middleware. func (am *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, - ExecutionPayloadT, GenesisT, SlotDataT, + AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, + DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ]) Name() string { return "abci-middleware" } diff --git a/mod/runtime/pkg/middleware/types.go b/mod/runtime/pkg/middleware/types.go index c8c2b080f7..f91e501fef 100644 --- a/mod/runtime/pkg/middleware/types.go +++ b/mod/runtime/pkg/middleware/types.go @@ -79,3 +79,5 @@ type BlockchainService[ blk BeaconBlockT, ) error } + +type validatorUpdates = transition.ValidatorUpdates From 00d6466cd2314d5b4ea3f04744ff3d35eacbd4c7 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 15 Aug 2024 17:15:01 -0400 Subject: [PATCH 49/75] da service --- mod/da/pkg/da/service.go | 102 +++++++++----------------- mod/primitives/pkg/messages/events.go | 2 +- mod/runtime/pkg/middleware/abci.go | 5 +- 3 files changed, 41 insertions(+), 68 deletions(-) diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 89f6330ae4..4aff2066bb 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -23,11 +23,13 @@ package da import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) +// The Data Availability service is responsible for verifying and processing +// incoming blob sidecars. type Service[ AvailabilityStoreT AvailabilityStore[BeaconBlockBodyT, BlobSidecarsT], BeaconBlockBodyT any, @@ -40,10 +42,10 @@ type Service[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ] - dispatcher asynctypes.MessageDispatcher - logger log.Logger[any] - processSidecarRequests chan asynctypes.Message[BlobSidecarsT] - verifySidecarRequests chan asynctypes.Message[BlobSidecarsT] + dispatcher async.EventDispatcher + logger log.Logger[any] + subSidecarsReceived async.Subscription[async.Event[BlobSidecarsT]] + subFinalBlobSidecars async.Subscription[async.Event[BlobSidecarsT]] } // NewService returns a new DA service. @@ -61,7 +63,7 @@ func NewService[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ], - dispatcher asynctypes.MessageDispatcher, + dispatcher async.EventDispatcher, logger log.Logger[any], ) *Service[ AvailabilityStoreT, BeaconBlockBodyT, @@ -71,12 +73,12 @@ func NewService[ AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT, ]{ - avs: avs, - bp: bp, - dispatcher: dispatcher, - logger: logger, - processSidecarRequests: make(chan asynctypes.Message[BlobSidecarsT]), - verifySidecarRequests: make(chan asynctypes.Message[BlobSidecarsT]), + avs: avs, + bp: bp, + dispatcher: dispatcher, + logger: logger, + subSidecarsReceived: async.NewSubscription[async.Event[BlobSidecarsT]](), + subFinalBlobSidecars: async.NewSubscription[async.Event[BlobSidecarsT]](), } } @@ -89,100 +91,68 @@ func (s *Service[_, _, _, _]) Name() string { // VerifySidecars messages, and begins listening for these requests. func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { var err error - // register as recipient of ProcessSidecars messages. - if err = s.dispatcher.RegisterMsgReceiver( - messages.ProcessSidecars, s.processSidecarRequests, + + // subscribe to SidecarsReceived events + if err = s.dispatcher.Subscribe( + messages.SidecarsReceived, s.subSidecarsReceived, ); err != nil { return err } - // register as recipient of VerifySidecars messages. - if err = s.dispatcher.RegisterMsgReceiver( - messages.VerifySidecars, s.verifySidecarRequests, + // subscribe to FinalSidecarsReceived events + if err = s.dispatcher.Subscribe( + messages.FinalSidecarsReceived, s.subFinalBlobSidecars, ); err != nil { return err } - // start a goroutine to listen for requests and handle accordingly - go s.start(ctx) + // listen for events and handle accordingly + s.subSidecarsReceived.Listen(ctx, s.handleSidecarsVerifyRequest) + s.subFinalBlobSidecars.Listen(ctx, s.handleBlobSidecarsProcessRequest) return nil } -// start starts listens for ProcessSidecars and VerifySidecars messages and -// handles them accordingly. -func (s *Service[_, _, BlobSidecarsT, _]) start( - ctx context.Context, -) { - for { - select { - case <-ctx.Done(): - return - case msg := <-s.processSidecarRequests: - s.handleBlobSidecarsProcessRequest(msg) - case msg := <-s.verifySidecarRequests: - s.handleSidecarsVerifyRequest(msg) - } - } -} - /* -------------------------------------------------------------------------- */ -/* Message Handlers */ +/* Event Handlers */ /* -------------------------------------------------------------------------- */ // handleBlobSidecarsProcessRequest handles the BlobSidecarsProcessRequest // event. // It processes the sidecars and publishes a BlobSidecarsProcessed event. func (s *Service[_, _, BlobSidecarsT, _]) handleBlobSidecarsProcessRequest( - msg asynctypes.Message[BlobSidecarsT], + msg async.Event[BlobSidecarsT], ) { - var err error - err = s.processSidecars(msg.Context(), msg.Data()) - if err != nil { + if err := s.processSidecars(msg.Context(), msg.Data()); err != nil { s.logger.Error( "Failed to process blob sidecars", "error", err, ) } - - // dispatch a response to acknowledge the request. - if err = s.dispatcher.SendResponse( - asynctypes.NewMessage( - msg.Context(), - messages.ProcessSidecars, - msg.Data(), - nil, - ), - ); err != nil { - s.logger.Error("failed to respond", "err", err) - } } // handleSidecarsVerifyRequest handles the SidecarsVerifyRequest event. // It verifies the sidecars and publishes a SidecarsVerified event. func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( - msg asynctypes.Message[BlobSidecarsT], + msg async.Event[BlobSidecarsT], ) { - var err error + var sidecarsErr error // verify the sidecars. - if err = s.verifySidecars(msg.Data()); err != nil { + if sidecarsErr = s.verifySidecars(msg.Data()); sidecarsErr != nil { s.logger.Error( "Failed to receive blob sidecars", "error", - err, + sidecarsErr, ) } - // dispatch a response to acknowledge the request. - if err = s.dispatcher.SendResponse( - asynctypes.NewMessage( - msg.Context(), - messages.VerifySidecars, - msg.Data(), - nil, + // emit the sidecars verification event with error from verifySidecars + if err := s.dispatcher.PublishEvent( + async.NewEvent( + msg.Context(), messages.SidecarsVerified, msg.Data(), sidecarsErr, ), ); err != nil { - s.logger.Error("failed to respond", "err", err) + s.logger.Error("failed to publish event", "err", err) } } diff --git a/mod/primitives/pkg/messages/events.go b/mod/primitives/pkg/messages/events.go index d3cf183c7c..26e4c908c5 100644 --- a/mod/primitives/pkg/messages/events.go +++ b/mod/primitives/pkg/messages/events.go @@ -41,7 +41,7 @@ const ( SidecarsVerified = "sidecars-verified" // finalize block events FinalBeaconBlockReceived = "final-beacon-block-received" - FinalBlobSidecarsReceived = "final-blob-sidecars-received" + FinalSidecarsReceived = "final-blob-sidecars-received" FinalValidatorUpdatesProcessed = "final-validator-updates" // FinalSidecarsProcessed = "final-sidecars-processed" ) diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 834c4cd99e..712cf18acc 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -264,18 +264,21 @@ func (h *ABCIMiddleware[ return nil, nil } + // notify that the final beacon block has been received. if err = h.dispatcher.PublishEvent( async.NewEvent(ctx, messages.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } + // notify that the final blob sidecars have been received. if err = h.dispatcher.PublishEvent( - async.NewEvent(ctx, messages.FinalBlobSidecarsReceived, blobs), + async.NewEvent(ctx, messages.FinalSidecarsReceived, blobs), ); err != nil { return nil, err } + // wait for the final validator updates. finalValUpdatesEvent, err = h.subFinalValidatorUpdates.Await(ctx) if err != nil { return nil, err From 1f319a3e377006274c1d6fc089ca8a80272e2a82 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 15 Aug 2024 18:34:12 -0400 Subject: [PATCH 50/75] betbetbet --- mod/async/pkg/dispatcher/dispatcher.go | 87 ++---------- mod/async/pkg/dispatcher/types.go | 17 +-- mod/async/pkg/messaging/errors.go | 75 ---------- mod/async/pkg/messaging/route.go | 133 ------------------ mod/async/pkg/{messaging => notify}/assert.go | 2 +- .../pkg/{messaging => notify}/constants.go | 8 +- .../pkg/notify/errors.go} | 29 ++-- .../pkg/{messaging => notify}/publisher.go | 8 +- mod/async/pkg/server/errors.go | 5 - mod/async/pkg/server/event.go | 21 +-- mod/async/pkg/server/msg.go | 91 ------------ mod/async/pkg/types/dispatcher.go | 25 +--- mod/async/pkg/types/{message.go => event.go} | 64 +++------ mod/async/pkg/types/future.go | 94 ------------- mod/async/pkg/types/subscription.go | 9 +- mod/beacon/block_store/service.go | 81 +++++------ mod/beacon/blockchain/process.go | 4 +- mod/beacon/blockchain/service.go | 14 +- mod/beacon/validator/service.go | 8 +- mod/da/pkg/da/service.go | 11 +- mod/execution/pkg/deposit/service.go | 40 +++--- mod/execution/pkg/deposit/sync.go | 19 +-- mod/execution/pkg/deposit/types.go | 1 + .../pkg/components/availability_store.go | 14 +- mod/node-core/pkg/components/block_store.go | 13 +- .../pkg/components/deposit_service.go | 4 +- mod/node-core/pkg/components/deposit_store.go | 13 +- mod/node-core/pkg/components/dispatcher.go | 14 +- mod/node-core/pkg/components/events.go | 44 +++++- mod/node-core/pkg/components/messages.go | 58 -------- mod/node-core/pkg/components/types.go | 70 +++++---- .../pkg/{messages => events}/events.go | 13 +- mod/runtime/pkg/middleware/abci.go | 14 +- mod/runtime/pkg/middleware/middleware.go | 14 +- mod/storage/pkg/manager/manager_test.go | 4 +- mod/storage/pkg/manager/types.go | 4 + .../mocks/beacon_block_subscription.mock.go | 71 ++++++++++ .../pkg/pruner/mocks/block_event.mock.go | 107 +++++++++++++- mod/storage/pkg/pruner/pruner.go | 46 +++--- mod/storage/pkg/pruner/pruner_test.go | 3 +- mod/storage/pkg/pruner/types.go | 10 ++ 41 files changed, 526 insertions(+), 836 deletions(-) delete mode 100644 mod/async/pkg/messaging/errors.go delete mode 100644 mod/async/pkg/messaging/route.go rename mod/async/pkg/{messaging => notify}/assert.go (98%) rename mod/async/pkg/{messaging => notify}/constants.go (84%) rename mod/{primitives/pkg/messages/messages.go => async/pkg/notify/errors.go} (66%) rename mod/async/pkg/{messaging => notify}/publisher.go (95%) delete mode 100644 mod/async/pkg/server/msg.go rename mod/async/pkg/types/{message.go => event.go} (62%) delete mode 100644 mod/async/pkg/types/future.go delete mode 100644 mod/node-core/pkg/components/messages.go rename mod/primitives/pkg/{messages => events}/events.go (91%) create mode 100644 mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 7e5f90c07e..01d110daec 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -27,117 +27,60 @@ import ( "github.com/berachain/beacon-kit/mod/log" ) -// Dispatcher faciliates asynchronous communication between components, -// typically services. It acts as an API facade to the underlying event and -// message servers. - // Ensure Dispatcher implements DispatcherI. var _ types.Dispatcher = (*Dispatcher)(nil) +// Dispatcher faciliates asynchronous communication between components, +// typically services. It acts as an API facade to the underlying eventserver type Dispatcher struct { eventServer EventServer - msgServer MessageServer logger log.Logger[any] } // NewDispatcher creates a new dispatcher. func NewDispatcher( eventServer EventServer, - msgServer MessageServer, logger log.Logger[any], ) *Dispatcher { eventServer.SetLogger(logger) - msgServer.SetLogger(logger) return &Dispatcher{ eventServer: eventServer, - msgServer: msgServer, logger: logger, } } -// Start starts the dispatcher. +// Start starts the underlying event server. func (d *Dispatcher) Start(ctx context.Context) error { d.eventServer.Start(ctx) return nil } +// Subscribe subscribes the given channel to the event with the given . +// It will error if the channel type does not match the event type corresponding +// to the . +// Contract: the channel must be a Subscription[T], where T is the expected +// type of the event data. +func (d *Dispatcher) Subscribe(eventID types.EventID, ch any) error { + return d.eventServer.Subscribe(eventID, ch) +} + // PublishEvent dispatches the given event to the event server. // It will error if the type is inconsistent with the publisher // registered for the given eventID. -func (d *Dispatcher) PublishEvent(event types.BaseMessage) error { +func (d *Dispatcher) PublishEvent(event types.BaseEvent) error { return d.eventServer.Publish(event) } -// SendRequest dispatches the given request to the message server. -// It will error if the and types are inconsistent with the -// route registered for the given messageID. -func (d *Dispatcher) SendRequest(req types.BaseMessage, future any) error { - return d.msgServer.Request(req, future) -} - -// SendResponse dispatches the given response to the message server. -// It will error if the type is inconsistent with the route registered -// for the given messageID. -func (d *Dispatcher) SendResponse(resp types.BaseMessage) error { - return d.msgServer.Respond(resp) -} - -// ============================== Events =================================== - // RegisterPublishers registers the given publisher with the given eventID. // Any subsequent events with dispatched to this Dispatcher must be // consistent with the type expected by . func (d *Dispatcher) RegisterPublishers( publishers ...types.Publisher, ) error { - var err error - for _, publisher := range publishers { - d.logger.Info("Publisher registered", "eventID", publisher.EventID()) - err = d.eventServer.RegisterPublisher(publisher.EventID(), publisher) - if err != nil { - return err - } - } - return nil -} - -// Subscribe subscribes the given channel to the event with the given . -// It will error if the channel type does not match the event type corresponding -// to the . -// Contract: the channel must be a Subscription[T], where T is the expected -// type of the event data. -func (d *Dispatcher) Subscribe(eventID types.MessageID, ch any) error { - return d.eventServer.Subscribe(eventID, ch) -} - -// ================================ Messages ================================ - -// RegisterMsgRecipient registers the given channel to the message with the -// given . -func (d *Dispatcher) RegisterMsgReceiver( - messageID types.MessageID, ch any, -) error { - d.logger.Info("Message receiver registered", "messageID", messageID) - return d.msgServer.RegisterReceiver(messageID, ch) -} - -// RegisterRoutes registers the given route with the given messageID. -// Any subsequent messages with sent to this Dispatcher must be -// consistent with the type expected by . -func (d *Dispatcher) RegisterRoutes( - routes ...types.MessageRoute, -) error { - var err error - for _, route := range routes { - d.logger.Info("Route registered", "messageID", route.MessageID()) - err = d.msgServer.RegisterRoute(route.MessageID(), route) - if err != nil { - return err - } - } - return nil + return d.eventServer.RegisterPublishers(publishers...) } +// Name returns the name of this service. func (d *Dispatcher) Name() string { return "dispatcher" } diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index d51c2b74f8..51018e238d 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -27,18 +27,15 @@ import ( "github.com/berachain/beacon-kit/mod/log" ) -type MessageServer interface { - RegisterReceiver(mID types.MessageID, ch any) error - Request(req types.BaseMessage, future any) error - Respond(resp types.BaseMessage) error - RegisterRoute(mID types.MessageID, route types.MessageRoute) error - SetLogger(logger log.Logger[any]) -} - type EventServer interface { + // Start starts the event server. Start(ctx context.Context) - RegisterPublisher(mID types.EventID, publisher types.Publisher) error + // RegisterPublishers registers the given publishers with the event server. + RegisterPublishers(publishers ...types.Publisher) error + // Subscribe subscribes the given channel to the event with the given . Subscribe(mID types.EventID, ch any) error - Publish(event types.BaseMessage) error + // Publish dispatches the given event to the event server. + Publish(event types.BaseEvent) error + // SetLogger sets the logger for the event server. SetLogger(logger log.Logger[any]) } diff --git a/mod/async/pkg/messaging/errors.go b/mod/async/pkg/messaging/errors.go deleted file mode 100644 index ea60b379d2..0000000000 --- a/mod/async/pkg/messaging/errors.go +++ /dev/null @@ -1,75 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package messaging - -import ( - "time" - - "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/errors" -) - -// errTimeout is the error returned when a dispatch operation timed out. -// -//nolint:gochecknoglobals // errors -var ( - // errTimeout is the error returned when a dispatch operation timed out. - errTimeout = func(messageID types.MessageID, timeout time.Duration) error { - return errors.Newf("message %s reached the max timeout of %s", - messageID, timeout) - } - - // errRouteAlreadySet is the error returned when the route is already set. - errRouteAlreadySet = errors.New("route already set") - - // errRegisteringNilChannel is the error returned when the channel to - // register is nil. - errRegisteringNilChannel = func(messageID types.MessageID) error { - return errors.Newf("cannot register nil channel for route: %s", - messageID) - } - - // errReceiverNotReady is the error returned when the receiver channel is - // full, closed, or not listening. - errReceiverNotReady = func(messageID types.MessageID) error { - return errors.Newf( - "receiver channel is full, closed, or not listening. Route: %s", - messageID, - ) - } - - errSendingNilResponse = func(messageID types.MessageID) error { - return errors.Newf("cannot send nil response for route: %s", - messageID) - } - - // errIncompatibleAssignee is the error returned when the assignee is not - // compatible with the assigner. - errIncompatibleAssignee = func( - assigner interface{}, assignee interface{}, - ) error { - return errors.Newf( - "incompatible assignee, expected: %T, received: %T", - assigner, - assignee, - ) - } -) diff --git a/mod/async/pkg/messaging/route.go b/mod/async/pkg/messaging/route.go deleted file mode 100644 index c0bd32c869..0000000000 --- a/mod/async/pkg/messaging/route.go +++ /dev/null @@ -1,133 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package messaging - -import ( - "sync" - "time" - - "github.com/berachain/beacon-kit/mod/async/pkg/types" -) - -// Route represents a communication route to a single recipient. -// Invariant: there is exactly no more than one route for each messageID. -type Route[ReqT any, RespT any] struct { - // messageID is the ID of the message that the route is responsible for. - messageID types.MessageID - // recipientCh is the channel to send requests to. - recipientCh chan types.Message[ReqT] - // responseCh is the channel to send responses to. - responseCh chan types.Message[RespT] - // maxTimeout is the maximum duration to wait for a response - // before considering the request timed out and pruning the thread. - maxTimeout time.Duration - // mu is the mutex to synchronize access to the route. - mu sync.Mutex -} - -// NewRoute creates a new route. -func NewRoute[ReqT any, RespT any]( - messageID types.MessageID, -) *Route[ReqT, RespT] { - return &Route[ReqT, RespT]{ - messageID: messageID, - responseCh: make(chan types.Message[RespT]), - maxTimeout: defaultMaxTimeout, - mu: sync.Mutex{}, - } -} - -// MessageID returns the message ID that the route is responsible for. -func (r *Route[ReqT, RespT]) MessageID() types.MessageID { - return r.messageID -} - -// RegisterReceiver sets the recipient for the route. -func (r *Route[ReqT, RespT]) RegisterReceiver(ch any) error { - if r.recipientCh != nil { - return errRouteAlreadySet - } else if ch == nil { - return errRegisteringNilChannel(r.messageID) - } - typedCh, err := ensureType[chan types.Message[ReqT]](ch) - if err != nil { - return err - } - r.recipientCh = typedCh - return nil -} - -// SendRequestAsync accepts a future and sends a request to the recipient -// channel. Once the response is available, it will be written to the future. -func (r *Route[ReqT, RespT]) SendRequest( - req types.BaseMessage, future any, -) error { - if err := r.sendRequest(req); err != nil { - return err - } - typedFuture, err := ensureType[types.FutureI[RespT]](future) - if err != nil { - return err - } - go r.populateFuture(typedFuture) - return nil -} - -// SendResponse sends a response to the response channel. -func (r *Route[ReqT, RespT]) SendResponse(resp types.BaseMessage) error { - typedMsg, err := ensureType[types.Message[RespT]](resp) - if err != nil { - return err - } - if typedMsg == nil { - return errSendingNilResponse(r.messageID) - } - r.responseCh <- typedMsg - return nil -} - -// populateFuture sends a done signal to the future when the response is -// available. -func (r *Route[ReqT, RespT]) populateFuture(future types.FutureI[RespT]) { - select { - case resp := <-r.responseCh: - future.SetResult(resp.Data(), resp.Error()) - case <-time.After(r.maxTimeout): - future.SetResult(*new(RespT), errTimeout(r.messageID, r.maxTimeout)) - return - } -} - -// SendRequest sends a request to the recipient. -func (r *Route[ReqT, RespT]) sendRequest(req types.BaseMessage) error { - typedReq, err := ensureType[types.Message[ReqT]](req) - if err != nil { - return err - } - - select { - case r.recipientCh <- typedReq: - return nil - default: - // Channel is full or closed - return errReceiverNotReady(r.messageID) - } -} diff --git a/mod/async/pkg/messaging/assert.go b/mod/async/pkg/notify/assert.go similarity index 98% rename from mod/async/pkg/messaging/assert.go rename to mod/async/pkg/notify/assert.go index c00698966a..410c4e29ac 100644 --- a/mod/async/pkg/messaging/assert.go +++ b/mod/async/pkg/notify/assert.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package messaging +package notify // ensureType ensures that the provided entity is of type T. // It returns a typed entity or an error if the type is not correct. diff --git a/mod/async/pkg/messaging/constants.go b/mod/async/pkg/notify/constants.go similarity index 84% rename from mod/async/pkg/messaging/constants.go rename to mod/async/pkg/notify/constants.go index 79c7f10602..ad627bc394 100644 --- a/mod/async/pkg/messaging/constants.go +++ b/mod/async/pkg/notify/constants.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package messaging +package notify import "time" @@ -29,12 +29,6 @@ const ( // publisher, or a client subscribes or unsubscribes. defaultPublisherTimeout = time.Second - // defaultMaxTimeout specifies the default max timeout for a message to be - // sent to a client, a message to be published, or a client to subscribe or - // unsubscribe. - // TODO: fr implementation - defaultMaxTimeout = 2 * time.Second - // defaultBufferSize specifies the default size of the message buffer. defaultBufferSize = 10 ) diff --git a/mod/primitives/pkg/messages/messages.go b/mod/async/pkg/notify/errors.go similarity index 66% rename from mod/primitives/pkg/messages/messages.go rename to mod/async/pkg/notify/errors.go index f8e132600c..68abbea1cf 100644 --- a/mod/primitives/pkg/messages/messages.go +++ b/mod/async/pkg/notify/errors.go @@ -18,14 +18,25 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package messages +package notify -// messages. -const ( - BuildBeaconBlockAndSidecars = "build-beacon-block-and-sidecars" - VerifyBeaconBlock = "verify-beacon-block" - FinalizeBeaconBlock = "finalize-beacon-block" - ProcessGenesisData = "process-genesis-data" - VerifySidecars = "verify-sidecars" - ProcessSidecars = "process-sidecars" +import ( + "github.com/berachain/beacon-kit/mod/errors" +) + +// errTimeout is the error returned when a dispatch operation timed out. +// +//nolint:gochecknoglobals // errors +var ( + // errIncompatibleAssignee is the error returned when the assignee is not + // compatible with the assigner. + errIncompatibleAssignee = func( + assigner interface{}, assignee interface{}, + ) error { + return errors.Newf( + "incompatible assignee, expected: %T, received: %T", + assigner, + assignee, + ) + } ) diff --git a/mod/async/pkg/messaging/publisher.go b/mod/async/pkg/notify/publisher.go similarity index 95% rename from mod/async/pkg/messaging/publisher.go rename to mod/async/pkg/notify/publisher.go index 520d41f3f0..3a0fab987f 100644 --- a/mod/async/pkg/messaging/publisher.go +++ b/mod/async/pkg/notify/publisher.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package messaging +package notify import ( "context" @@ -30,7 +30,7 @@ import ( // Publisher is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Publisher[T types.BaseMessage] struct { +type Publisher[T types.BaseEvent] struct { eventID types.EventID // subscriptions is a map of subscribed subscriptions. subscriptions map[types.Subscription[T]]struct{} @@ -44,7 +44,7 @@ type Publisher[T types.BaseMessage] struct { // NewPublisher creates a new publisher publishing events of type T for the // provided eventID. -func NewPublisher[T types.BaseMessage](eventID string) *Publisher[T] { +func NewPublisher[T types.BaseEvent](eventID string) *Publisher[T] { return &Publisher[T]{ eventID: types.EventID(eventID), subscriptions: make(map[types.Subscription[T]]struct{}), @@ -81,7 +81,7 @@ func (p *Publisher[T]) start(ctx context.Context) { // Publish publishes a msg to all subscribers. // Returns ErrTimeout on timeout. -func (p *Publisher[T]) Publish(msg types.BaseMessage) error { +func (p *Publisher[T]) Publish(msg types.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { return err diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index 6282dfb15d..89af5a1a0d 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -30,11 +30,6 @@ var ( errPublisherNotFound = func(eventID types.EventID) error { return errors.Newf("publisher not found for eventID: %s", eventID) } - errRouteNotFound = errors.New("route not found") - errRouteAlreadyRegistered = func(messageID types.MessageID) error { - return errors.Newf("route already registered for messageID: %s", - messageID) - } errPublisherAlreadyExists = func(eventID types.EventID) error { return errors.Newf("publisher already exists for eventID: %s", eventID) } diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go index ecb4be27c4..21feaeb118 100644 --- a/mod/async/pkg/server/event.go +++ b/mod/async/pkg/server/event.go @@ -29,19 +29,19 @@ import ( // EventServer asyncronously dispatches events to subscribers. type EventServer struct { - publishers map[types.MessageID]types.Publisher + publishers map[types.EventID]types.Publisher logger log.Logger[any] } // NewEventServer creates a new event server. func NewEventServer() *EventServer { return &EventServer{ - publishers: make(map[types.MessageID]types.Publisher), + publishers: make(map[types.EventID]types.Publisher), } } // Dispatch dispatches the given event to the publisher with the given eventID. -func (es *EventServer) Publish(event types.BaseMessage) error { +func (es *EventServer) Publish(event types.BaseEvent) error { publisher, ok := es.publishers[event.ID()] if !ok { return errPublisherNotFound(event.ID()) @@ -54,7 +54,7 @@ func (es *EventServer) Publish(event types.BaseMessage) error { // corresponding to the publisher. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. -func (es *EventServer) Subscribe(eventID types.MessageID, ch any) error { +func (es *EventServer) Subscribe(eventID types.EventID, ch any) error { publisher, ok := es.publishers[eventID] if !ok { return errPublisherNotFound(eventID) @@ -72,13 +72,16 @@ func (es *EventServer) Start(ctx context.Context) { // RegisterPublisher registers the given publisher with the given eventID. // Any subsequent events with dispatched to this EventServer must be // consistent with the type expected by . -func (es *EventServer) RegisterPublisher( - eventID types.MessageID, publisher types.Publisher, +func (es *EventServer) RegisterPublishers( + publishers ...types.Publisher, ) error { - if _, ok := es.publishers[eventID]; ok { - return errPublisherAlreadyExists(eventID) + var ok bool + for _, publisher := range publishers { + if _, ok = es.publishers[publisher.EventID()]; ok { + return errPublisherAlreadyExists(publisher.EventID()) + } + es.publishers[publisher.EventID()] = publisher } - es.publishers[eventID] = publisher return nil } diff --git a/mod/async/pkg/server/msg.go b/mod/async/pkg/server/msg.go deleted file mode 100644 index 5b57b68e79..0000000000 --- a/mod/async/pkg/server/msg.go +++ /dev/null @@ -1,91 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package server - -import ( - "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/log" -) - -// MessageServer is a server for sending and receiving messages. -type MessageServer struct { - routes map[types.MessageID]types.MessageRoute - logger log.Logger[any] -} - -// NewMessageServer creates a new message server. -func NewMessageServer() *MessageServer { - return &MessageServer{ - routes: make(map[types.MessageID]types.MessageRoute), - } -} - -// Request sends a message to the server and awaits for a response. -// The response is written to the provided response pointer. -func (ms *MessageServer) Request(req types.BaseMessage, future any) error { - // send request and await response - route, ok := ms.routes[req.ID()] - if !ok { - return errRouteNotFound - } - return route.SendRequest(req, future) -} - -// Respond sends a response to the route that corresponds to the response's -// messageID. -func (ms *MessageServer) Respond(resp types.BaseMessage) error { - route, ok := ms.routes[resp.ID()] - if !ok { - return errRouteNotFound - } - return route.SendResponse(resp) -} - -// RegisterRoute registers the route with the given messageID. -// Any subsequent messages with sent to this MessageServer must be -// consistent with the type expected by . -func (ms *MessageServer) RegisterRoute( - messageID types.MessageID, route types.MessageRoute, -) error { - if ms.routes[messageID] != nil { - return errRouteAlreadyRegistered(messageID) - } - ms.routes[messageID] = route - return nil -} - -// SetRecipient sets the recipient for the route with the given messageID. -// Errors if the route with the given messageID is not found or the route -// already has a registered recipient. -func (ms *MessageServer) RegisterReceiver( - messageID types.MessageID, ch any, -) error { - route, ok := ms.routes[messageID] - if !ok { - return errRouteNotFound - } - return route.RegisterReceiver(ch) -} - -// SetLogger sets the logger for the message server. -func (ms *MessageServer) SetLogger(logger log.Logger[any]) { - ms.logger = logger -} diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index 854a4aa070..3bdb1c44bc 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -26,13 +26,10 @@ import "context" // of async events and the sending and receiving of async messages. type Dispatcher interface { EventDispatcher - MessageDispatcher // Start starts the dispatcher. Start(ctx context.Context) error // RegisterPublishers registers publishers to the dispatcher. RegisterPublishers(publishers ...Publisher) error - // RegisterRoutes registers message routes to the dispatcher. - RegisterRoutes(routes ...MessageRoute) error // Name returns the name of the dispatcher. Name() string } @@ -41,7 +38,7 @@ type Dispatcher interface { // of async events. type EventDispatcher interface { // PublishEvent publishes an event to the dispatcher. - PublishEvent(event BaseMessage) error + PublishEvent(event BaseEvent) error // Subscribe subscribes the given channel to all events with the given event // ID. // Contract: the channel must be a Subscription[T], where T is the expected @@ -49,24 +46,12 @@ type EventDispatcher interface { Subscribe(eventID EventID, ch any) error } -// MessageDispatcher is the API for a dispatcher that facilitates the sending -// and receiving of async messages. -type MessageDispatcher interface { - // SendRequest sends a request to the dispatcher. - SendRequest(req BaseMessage, future any) error - // SendResponse sends a response to the dispatcher. - SendResponse(resp BaseMessage) error - // RegisterMsgReceiver registers the given channel as the message receiver - // for the given message ID. - RegisterMsgReceiver(messageID MessageID, ch any) error -} - // publisher is the interface that supports basic event publisher operations. type Publisher interface { // Start starts the event publisher. Start(ctx context.Context) // Publish publishes the given event to the event publisher. - Publish(event BaseMessage) error + Publish(event BaseEvent) error // Subscribe subscribes the given channel to the event publisher. Subscribe(ch any) error // Unsubscribe unsubscribes the given channel from the event publisher. @@ -80,9 +65,9 @@ type MessageRoute interface { // RegisterRecipient sets the recipient for the route. RegisterReceiver(ch any) error // SendRequest sends a request to the recipient. - SendRequest(msg BaseMessage, future any) error + SendRequest(msg BaseEvent, future any) error // SendResponse sends a response to the recipient. - SendResponse(msg BaseMessage) error + SendResponse(msg BaseEvent) error // MessageID returns the message ID that the route is responsible for. - MessageID() MessageID + MessageID() EventID } diff --git a/mod/async/pkg/types/message.go b/mod/async/pkg/types/event.go similarity index 62% rename from mod/async/pkg/types/message.go rename to mod/async/pkg/types/event.go index 20aa0dba69..6ec58679f2 100644 --- a/mod/async/pkg/types/message.go +++ b/mod/async/pkg/types/event.go @@ -25,50 +25,25 @@ import ( "errors" ) -// MessageID represents the type of a message. -type MessageID string +// EventID represents the type of a message. +type EventID string -// EventID is a type alias for a MessageID. -type EventID = MessageID - -// BaseMessage defines the minimal interface that the dispatcher expects from a +// BaseEvent defines the minimal interface that the dispatcher expects from a // message. -type BaseMessage interface { - ID() MessageID +type BaseEvent interface { + ID() EventID Context() context.Context } -// DataMessage defines the interface that the underlying route expects from a +// Event defines the interface that the underlying route expects from a // message with data. -type Message[DataT any] interface { - BaseMessage +type Event[DataT any] interface { + BaseEvent Data() DataT Error() error - Is(messageType MessageID) bool -} - -type Event[DataT any] interface { - Message[DataT] -} - -// NewEvent creates a new Event with the given context and beacon event. -func NewMessage[ - DataT any, -]( - ctx context.Context, messageType MessageID, data DataT, errs ...error, -) Message[DataT] { - return &message[DataT]{ - ctx: ctx, - id: messageType, - data: data, - err: errors.Join(errs...), - } + Is(messageType EventID) bool } -// Event acts as a type alias for a Message that is meant to be broadcasted -// to all subscribers. -type event[DataT any] struct{ Message[DataT] } - // NewEvent creates a new Event with the given context and beacon event. func NewEvent[ DataT any, @@ -76,16 +51,19 @@ func NewEvent[ ctx context.Context, messageType EventID, data DataT, errs ...error, ) Event[DataT] { return &event[DataT]{ - Message: NewMessage(ctx, messageType, data, errs...), + ctx: ctx, + id: messageType, + data: data, + err: errors.Join(errs...), } } -// A Message is a hard type implementation of the Message and Event interfaces. -type message[DataT any] struct { +// An event is a hard type implementation of the Event interface. +type event[DataT any] struct { // ctx is the context associated with the event. ctx context.Context // id is the name of the event. - id MessageID + id EventID // event is the actual beacon event. data DataT // err is the error associated with the event. @@ -93,26 +71,26 @@ type message[DataT any] struct { } // ID returns the ID of the event. -func (m *message[DataT]) ID() MessageID { +func (m *event[DataT]) ID() EventID { return m.id } // Context returns the context associated with the event. -func (m *message[DataT]) Context() context.Context { +func (m *event[DataT]) Context() context.Context { return m.ctx } // Data returns the data associated with the event. -func (m *message[DataT]) Data() DataT { +func (m *event[DataT]) Data() DataT { return m.data } // Error returns the error associated with the event. -func (m *message[DataT]) Error() error { +func (m *event[DataT]) Error() error { return m.err } // Is returns true if the event has the given type. -func (m *message[DataT]) Is(messageType MessageID) bool { +func (m *event[DataT]) Is(messageType EventID) bool { return m.id == messageType } diff --git a/mod/async/pkg/types/future.go b/mod/async/pkg/types/future.go deleted file mode 100644 index cc2f34d0ed..0000000000 --- a/mod/async/pkg/types/future.go +++ /dev/null @@ -1,94 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package types - -import ( - "sync" - "time" -) - -// FutureI is the interface that the future implements. -type FutureI[T any] interface { - // Resolve returns the result of the future, blocking until it's available - // or the context is done. - Resolve() (result T, err error) - // ResolveWithTimeout returns the result of the future, blocking until it's - // available or the timeout is reached. - ResolveWithTimeout(timeout time.Duration) (result T, err error) - // SetResult is called by the router to set the result of the future. - SetResult(result T, err error) - // IsDone returns true if the future has completed (successfully or with an - // error). - IsDone() bool -} - -// Future represents a value that will be available at some point in the future. -type Future[T any] struct { - result T - err error - done chan struct{} - once sync.Once -} - -// NewFuture creates a new Future and starts the given function in a goroutine. -func NewFuture[T any]() *Future[T] { - f := &Future[T]{ - done: make(chan struct{}), - } - return f -} - -// Resolve returns the result of the future, blocking until it's available or -// the context is done. -func (f *Future[T]) Resolve() (T, error) { - <-f.done - return f.result, f.err -} - -// SetResult sets the result of the future. -func (f *Future[T]) SetResult(result T, err error) { - f.result = result - f.err = err - f.once.Do(func() { close(f.done) }) -} - -// GetWithTimeout returns the result of the future, blocking until it's -// available or the timeout is reached. -func (f *Future[T]) ResolveWithTimeout(timeout time.Duration) (T, error) { - select { - case <-f.done: - return f.result, f.err - case <-time.After(timeout): - var zero T - return zero, errTimeout - } -} - -// IsDone returns true if the future has completed (successfully or with an -// error). -func (f *Future[T]) IsDone() bool { - select { - case <-f.done: - return true - default: - return false - } -} diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go index ba3860ead5..fe8602146b 100644 --- a/mod/async/pkg/types/subscription.go +++ b/mod/async/pkg/types/subscription.go @@ -23,11 +23,11 @@ package types import "context" // Subscription is a channel that receives events. -type Subscription[T BaseMessage] chan T +type Subscription[T BaseEvent] chan T // NewSubscription creates a new subscription with an unbuffered channel as the // underlying type. -func NewSubscription[T BaseMessage]() Subscription[T] { +func NewSubscription[T BaseEvent]() Subscription[T] { return make(chan T) } @@ -67,3 +67,8 @@ func (s Subscription[T]) Await(ctx context.Context) (T, error) { return *new(T), ctx.Err() } } + +// Chan returns the underlying channel of the subscription. +func (s Subscription[T]) Chan() chan T { + return s +} diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index db0ea16382..2910349624 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -23,31 +23,11 @@ package blockstore import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) -// NewService creates a new block service. -func NewService[ - BeaconBlockT BeaconBlock, - BlockStoreT BlockStore[BeaconBlockT], -]( - config Config, - logger log.Logger[any], - dispatcher asynctypes.EventDispatcher, - store BlockStoreT, -) *Service[BeaconBlockT, BlockStoreT] { - return &Service[BeaconBlockT, BlockStoreT]{ - config: config, - logger: logger, - dispatcher: dispatcher, - store: store, - // finalizedBlkEvents is a channel for receiving finalized block events. - finalizedBlkEvents: make(chan asynctypes.Event[BeaconBlockT]), - } -} - // Service is a Service that listens for blocks and stores them in a KVStore. type Service[ BeaconBlockT BeaconBlock, @@ -58,11 +38,32 @@ type Service[ // logger is used for logging information and errors. logger log.Logger[any] // dispatcher is the dispatcher for the service. - dispatcher asynctypes.EventDispatcher + dispatcher async.EventDispatcher // store is the block store for the service. store BlockStoreT - // finalizedBlkEvents is a channel for receiving finalized block events. - finalizedBlkEvents chan asynctypes.Event[BeaconBlockT] + // subFinalizedBlkEvents is a channel for receiving finalized block events. + subFinalizedBlkEvents async.Subscription[async.Event[BeaconBlockT]] +} + +// NewService creates a new block service. +// +//nolint:lll // long type +func NewService[ + BeaconBlockT BeaconBlock, + BlockStoreT BlockStore[BeaconBlockT], +]( + config Config, + logger log.Logger[any], + dispatcher async.EventDispatcher, + store BlockStoreT, +) *Service[BeaconBlockT, BlockStoreT] { + return &Service[BeaconBlockT, BlockStoreT]{ + config: config, + logger: logger, + dispatcher: dispatcher, + store: store, + subFinalizedBlkEvents: async.NewSubscription[async.Event[BeaconBlockT]](), + } } // Name returns the name of the service. @@ -79,30 +80,24 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { // subscribe a channel to the finalized block events. if err := s.dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, s.finalizedBlkEvents, + events.BeaconBlockFinalizedEvent, s.subFinalizedBlkEvents, ); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err } - go s.listenAndStore(ctx) + + // listen for finalized block events and store the blocks in the KVStore. + s.subFinalizedBlkEvents.Listen(ctx, s.onFinalizeBlock) return nil } -// listenAndStore listens for blocks and stores them in the KVStore. -func (s *Service[BeaconBlockT, _]) listenAndStore( - ctx context.Context, -) { - for { - select { - case <-ctx.Done(): - return - case msg := <-s.finalizedBlkEvents: - slot := msg.Data().GetSlot() - if err := s.store.Set(msg.Data()); err != nil { - s.logger.Error( - "failed to store block", "slot", slot, "error", err, - ) - } - } +// onFinalizeBlock is triggered when a finalized block event is received. +// It stores the block in the KVStore. +func (s *Service[BeaconBlockT, _]) onFinalizeBlock(event async.Event[BeaconBlockT]) { + slot := event.Data().GetSlot() + if err := s.store.Set(event.Data()); err != nil { + s.logger.Error( + "failed to store block", "slot", slot, "error", err, + ) } } diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 1ad7f11609..09cac32f9f 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -25,7 +25,7 @@ import ( "time" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -85,7 +85,7 @@ func (s *Service[ // via ticker later. if err = s.dispatcher.PublishEvent( asynctypes.NewEvent( - ctx, messages.BeaconBlockFinalizedEvent, blk, + ctx, events.BeaconBlockFinalizedEvent, blk, ), ); err != nil { return nil, err diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 606fbba501..5518abe68c 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -27,7 +27,7 @@ import ( asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -175,19 +175,19 @@ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) Start(ctx context.Context) error { if err := s.dispatcher.Subscribe( - messages.GenesisDataReceived, s.subGenDataReceived, + events.GenesisDataReceived, s.subGenDataReceived, ); err != nil { return err } if err := s.dispatcher.Subscribe( - messages.BeaconBlockReceived, s.subBlockReceived, + events.BeaconBlockReceived, s.subBlockReceived, ); err != nil { return err } if err := s.dispatcher.Subscribe( - messages.FinalBeaconBlockReceived, s.subFinalBlkReceived, + events.FinalBeaconBlockReceived, s.subFinalBlkReceived, ); err != nil { return err } @@ -231,7 +231,7 @@ func (s *Service[ if err = s.dispatcher.PublishEvent( asynctypes.NewEvent( msg.Context(), - messages.GenesisDataProcessed, + events.GenesisDataProcessed, valUpdates, err, ), @@ -259,7 +259,7 @@ func (s *Service[ if err := s.dispatcher.PublishEvent( asynctypes.NewEvent( msg.Context(), - messages.BeaconBlockVerified, + events.BeaconBlockVerified, msg.Data(), s.VerifyIncomingBlock(msg.Context(), msg.Data()), ), @@ -296,7 +296,7 @@ func (s *Service[ if err = s.dispatcher.PublishEvent( asynctypes.NewEvent( msg.Context(), - messages.FinalValidatorUpdatesProcessed, + events.FinalValidatorUpdatesProcessed, valUpdates, err, ), diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 66543b39a6..48c119e418 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -27,7 +27,7 @@ import ( "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -175,7 +175,7 @@ func (s *Service[ ctx context.Context, ) error { // subscribe to new slot events - err := s.dispatcher.Subscribe(messages.NewSlot, s.subNewSlot) + err := s.dispatcher.Subscribe(events.NewSlot, s.subNewSlot) if err != nil { return err } @@ -204,14 +204,14 @@ func (s *Service[ // emit a built block event with the built block and the error if bbErr := s.dispatcher.PublishEvent( - async.NewEvent(req.Context(), messages.BuiltBeaconBlock, blk, err), + async.NewEvent(req.Context(), events.BuiltBeaconBlock, blk, err), ); bbErr != nil { s.logger.Error("failed to dispatch built block", "err", err) } // emit a built sidecars event with the built sidecars and the error if scErr := s.dispatcher.PublishEvent( - async.NewEvent(req.Context(), messages.BuiltSidecars, sidecars, err), + async.NewEvent(req.Context(), events.BuiltSidecars, sidecars, err), ); scErr != nil { s.logger.Error("failed to dispatch built sidecars", "err", err) } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 4aff2066bb..f8a0eca3d4 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -25,11 +25,13 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) // The Data Availability service is responsible for verifying and processing // incoming blob sidecars. +// +//nolint:lll // long types type Service[ AvailabilityStoreT AvailabilityStore[BeaconBlockBodyT, BlobSidecarsT], BeaconBlockBodyT any, @@ -94,14 +96,14 @@ func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { // subscribe to SidecarsReceived events if err = s.dispatcher.Subscribe( - messages.SidecarsReceived, s.subSidecarsReceived, + events.SidecarsReceived, s.subSidecarsReceived, ); err != nil { return err } // subscribe to FinalSidecarsReceived events if err = s.dispatcher.Subscribe( - messages.FinalSidecarsReceived, s.subFinalBlobSidecars, + events.FinalSidecarsReceived, s.subFinalBlobSidecars, ); err != nil { return err } @@ -149,7 +151,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( // emit the sidecars verification event with error from verifySidecars if err := s.dispatcher.PublishEvent( async.NewEvent( - msg.Context(), messages.SidecarsVerified, msg.Data(), sidecarsErr, + msg.Context(), events.SidecarsVerified, msg.Data(), sidecarsErr, ), ); err != nil { s.logger.Error("failed to publish event", "err", err) @@ -195,6 +197,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) verifySidecars( return err } + //nolint:lll // logging s.logger.Info( "Blob sidecars verification succeeded - accepting incoming blob sidecars", "num_blobs", diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 5e8834c1b0..167300edb0 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -23,7 +23,7 @@ package deposit import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -48,11 +48,11 @@ type Service[ // ds is the deposit store that stores deposits. ds Store[DepositT] // dispatcher is the dispatcher for the service. - dispatcher asynctypes.EventDispatcher + dispatcher async.EventDispatcher // finalizedBlockEventID is the event ID for the finalized block event. - finalizedBlockEventID asynctypes.EventID - // finalizedBlockEvents is the channel that provides finalized block events. - finalizedBlockEvents chan BlockEventT + finalizedBlockEventID async.EventID + // subFinalizedBlockEvents is the channel that provides finalized block events. + subFinalizedBlockEvents async.Subscription[BlockEventT] // metrics is the metrics for the deposit service. metrics *metrics // failedBlocks is a map of blocks that failed to be processed to be @@ -78,8 +78,8 @@ func NewService[ telemetrySink TelemetrySink, ds Store[DepositT], dc Contract[DepositT], - finalizedBlockEventID asynctypes.EventID, - dispatcher asynctypes.EventDispatcher, + finalizedBlockEventID async.EventID, + dispatcher async.EventDispatcher, ) *Service[ BeaconBlockT, BeaconBlockBodyT, BlockEventT, DepositT, ExecutionPayloadT, WithdrawalCredentialsT, @@ -89,15 +89,15 @@ func NewService[ ExecutionPayloadT, WithdrawalCredentialsT, ]{ - dc: dc, - dispatcher: dispatcher, - ds: ds, - eth1FollowDistance: eth1FollowDistance, - failedBlocks: make(map[math.Slot]struct{}), - finalizedBlockEventID: finalizedBlockEventID, - finalizedBlockEvents: make(chan BlockEventT), - logger: logger, - metrics: newMetrics(telemetrySink), + dc: dc, + dispatcher: dispatcher, + ds: ds, + eth1FollowDistance: eth1FollowDistance, + failedBlocks: make(map[math.Slot]struct{}), + finalizedBlockEventID: finalizedBlockEventID, + subFinalizedBlockEvents: async.NewSubscription[BlockEventT](), + logger: logger, + metrics: newMetrics(telemetrySink), } } @@ -106,13 +106,17 @@ func (s *Service[ _, _, _, _, _, _, ]) Start(ctx context.Context) error { if err := s.dispatcher.Subscribe( - s.finalizedBlockEventID, s.finalizedBlockEvents, + s.finalizedBlockEventID, s.subFinalizedBlockEvents, ); err != nil { s.logger.Error("failed to subscribe to event", "event", s.finalizedBlockEventID, "err", err) return err } - go s.depositFetcher(ctx) + + // Listen for finalized block events and fetch deposits for the block. + s.subFinalizedBlockEvents.Listen(ctx, s.getDepositFetcher(ctx)) + + // Catchup deposits for failed blocks. go s.depositCatchupFetcher(ctx) return nil } diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index 842d0b2d3a..9a081e6bf5 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -30,19 +30,14 @@ import ( // defaultRetryInterval processes a deposit event. const defaultRetryInterval = 20 * time.Second -// depositFetcher processes a deposit event. +// getDepositFetcher returns a function that retrieves the block number from the +// event and fetches and stores the deposits for that block. func (s *Service[ - _, _, _, _, _, _, -]) depositFetcher(ctx context.Context) { - for { - select { - case <-ctx.Done(): - return - case msg := <-s.finalizedBlockEvents: - blockNum := msg.Data(). - GetBody().GetExecutionPayload().GetNumber() - s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) - } + _, _, BlockEventT, _, _, _, +]) getDepositFetcher(ctx context.Context) func(event BlockEventT) { + return func(event BlockEventT) { + blockNum := event.Data().GetBody().GetExecutionPayload().GetNumber() + s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) } } diff --git a/mod/execution/pkg/deposit/types.go b/mod/execution/pkg/deposit/types.go index 99beea8e8b..9f4b5dca51 100644 --- a/mod/execution/pkg/deposit/types.go +++ b/mod/execution/pkg/deposit/types.go @@ -56,6 +56,7 @@ type BlockEvent[ ID() asynctypes.EventID Is(asynctypes.EventID) bool Data() BeaconBlockT + Context() context.Context } // ExecutionPayload is an interface for execution payloads. diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index 07118616ba..b535bb4104 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -25,10 +25,11 @@ import ( "os" "cosmossdk.io/depinject" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" dastore "github.com/berachain/beacon-kit/mod/da/pkg/store" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/storage/pkg/filedb" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -97,12 +98,15 @@ func ProvideAvailabilityPruner[ return nil, errors.New("availability store does not have a range db") } - var finalizedBlkCh = make(chan FinalizedBlockEvent) + // TODO: add dispatcher field in the pruner or something, the provider + // should not execute any business logic. + // create new subscription for finalized blocks. + subFinalizedBlocks := async.NewSubscription[FinalizedBlockEvent]() if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + events.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) + events.BeaconBlockFinalizedEvent, "err", err) return nil, err } @@ -115,7 +119,7 @@ func ProvideAvailabilityPruner[ in.Logger.With("service", manager.AvailabilityPrunerName), rangeDB, manager.AvailabilityPrunerName, - finalizedBlkCh, + subFinalizedBlocks, dastore.BuildPruneRangeFn[ *BeaconBlock, FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 1c9de8e978..2e5e065ff4 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -24,12 +24,13 @@ import ( "cosmossdk.io/depinject" storev2 "cosmossdk.io/store/v2/db" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" blockservice "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/storage/pkg/block" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -86,12 +87,14 @@ func ProvideBlockPruner[ ]( in BlockPrunerInput[LoggerT], ) (BlockPruner, error) { - var finalizedBlkCh = make(chan FinalizedBlockEvent) + // TODO: provider should not execute any business logic. + // create new subscription for finalized blocks. + subFinalizedBlocks := async.NewSubscription[FinalizedBlockEvent]() if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + events.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) + events.BeaconBlockFinalizedEvent, "err", err) return nil, err } @@ -103,7 +106,7 @@ func ProvideBlockPruner[ in.Logger.With("service", manager.BlockPrunerName), in.BlockStore, manager.BlockPrunerName, - finalizedBlkCh, + subFinalizedBlocks, blockservice.BuildPruneRangeFn[ *BeaconBlock, FinalizedBlockEvent, diff --git a/mod/node-core/pkg/components/deposit_service.go b/mod/node-core/pkg/components/deposit_service.go index b7b5b2aa68..69a21f4397 100644 --- a/mod/node-core/pkg/components/deposit_service.go +++ b/mod/node-core/pkg/components/deposit_service.go @@ -26,8 +26,8 @@ import ( "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/metrics" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" ) // DepositServiceIn is the input for the deposit service. @@ -64,7 +64,7 @@ func ProvideDepositService[ in.TelemetrySink, in.DepositStore, in.BeaconDepositContract, - messages.BeaconBlockFinalizedEvent, + events.BeaconBlockFinalizedEvent, in.Dispatcher, ), nil } diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index 05989b609b..a71224c09d 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -23,11 +23,12 @@ package components import ( "cosmossdk.io/depinject" storev2 "cosmossdk.io/store/v2/db" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/execution/pkg/deposit" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" depositstore "github.com/berachain/beacon-kit/mod/storage/pkg/deposit" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -74,15 +75,17 @@ func ProvideDepositPruner[ ]( in DepositPrunerInput[LoggerT], ) (DepositPruner, error) { - var finalizedBlkCh = make(chan FinalizedBlockEvent) + // initialize a subscription for finalized blocks. + subFinalizedBlocks := async.NewSubscription[FinalizedBlockEvent]() if err := in.Dispatcher.Subscribe( - messages.BeaconBlockFinalizedEvent, finalizedBlkCh, + events.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { in.Logger.Error("failed to subscribe to event", "event", - messages.BeaconBlockFinalizedEvent, "err", err) + events.BeaconBlockFinalizedEvent, "err", err) return nil, err } + // return a new pruner for the deposit store. return pruner.NewPruner[ *BeaconBlock, FinalizedBlockEvent, @@ -91,7 +94,7 @@ func ProvideDepositPruner[ in.Logger.With("service", manager.DepositPrunerName), in.DepositStore, manager.DepositPrunerName, - finalizedBlkCh, + subFinalizedBlocks, deposit.BuildPruneRangeFn[ *BeaconBlockBody, *BeaconBlock, diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index d47ca87be2..20cf75f09c 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -31,11 +31,9 @@ import ( // DispatcherInput is the input for the Dispatcher. type DispatcherInput struct { depinject.In - EventServer *EventServer - MessageServer *MessageServer - Logger log.AdvancedLogger[any, sdklog.Logger] - Publishers []asynctypes.Publisher - Routes []asynctypes.MessageRoute + EventServer *EventServer + Logger log.AdvancedLogger[any, sdklog.Logger] + Publishers []asynctypes.Publisher } // ProvideDispatcher provides a new Dispatcher. @@ -44,24 +42,18 @@ func ProvideDispatcher( ) (*Dispatcher, error) { d := dispatcher.NewDispatcher( in.EventServer, - in.MessageServer, in.Logger.With("service", "dispatcher"), ) if err := d.RegisterPublishers(in.Publishers...); err != nil { return nil, err } - if err := d.RegisterRoutes(in.Routes...); err != nil { - return nil, err - } return d, nil } func DefaultDispatcherComponents() []any { return []any{ ProvideDispatcher, - ProvideMessageRoutes, ProvidePublishers, - ProvideMessageServer, ProvideEventServer, } } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 255d7715d5..3643a27c73 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -21,10 +21,10 @@ package components import ( - "github.com/berachain/beacon-kit/mod/async/pkg/messaging" + "github.com/berachain/beacon-kit/mod/async/pkg/notify" "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) // ProvideEventServer provides an event server. @@ -36,8 +36,44 @@ func ProvideEventServer() *EventServer { // finalized events. func ProvidePublishers() []asynctypes.Publisher { return []asynctypes.Publisher{ - messaging.NewPublisher[FinalizedBlockEvent]( - messages.BeaconBlockFinalizedEvent, + notify.NewPublisher[GenesisDataReceivedEvent]( + events.GenesisDataReceived, + ), + notify.NewPublisher[GenesisDataProcessedEvent]( + events.GenesisDataProcessed, + ), + notify.NewPublisher[NewSlotEvent]( + events.NewSlot, + ), + notify.NewPublisher[BuiltBeaconBlockEvent]( + events.BuiltBeaconBlock, + ), + notify.NewPublisher[BuiltSidecarsEvent]( + events.BuiltSidecars, + ), + notify.NewPublisher[BeaconBlockReceivedEvent]( + events.BeaconBlockReceived, + ), + notify.NewPublisher[SidecarsReceivedEvent]( + events.SidecarsReceived, + ), + notify.NewPublisher[BeaconBlockVerifiedEvent]( + events.BeaconBlockVerified, + ), + notify.NewPublisher[SidecarsVerifiedEvent]( + events.SidecarsVerified, + ), + notify.NewPublisher[FinalBeaconBlockReceivedEvent]( + events.FinalBeaconBlockReceived, + ), + notify.NewPublisher[FinalSidecarsReceivedEvent]( + events.FinalSidecarsReceived, + ), + notify.NewPublisher[FinalValidatorUpdatesProcessedEvent]( + events.FinalValidatorUpdatesProcessed, + ), + notify.NewPublisher[FinalizedBlockEvent]( + events.BeaconBlockFinalizedEvent, ), } } diff --git a/mod/node-core/pkg/components/messages.go b/mod/node-core/pkg/components/messages.go deleted file mode 100644 index cb47de88ca..0000000000 --- a/mod/node-core/pkg/components/messages.go +++ /dev/null @@ -1,58 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package components - -import ( - "github.com/berachain/beacon-kit/mod/async/pkg/messaging" - "github.com/berachain/beacon-kit/mod/async/pkg/server" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" - "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" -) - -// ProvideMessageServer provides a message server. -func ProvideMessageServer() *server.MessageServer { - return server.NewMessageServer() -} - -// ProvideMessageRoutes provides all the message routes. -func ProvideMessageRoutes() []asynctypes.MessageRoute { - return []asynctypes.MessageRoute{ - messaging.NewRoute[*SlotData, *BeaconBlockBundle]( - messages.BuildBeaconBlockAndSidecars, - ), - messaging.NewRoute[*BeaconBlock, *BeaconBlock]( - messages.VerifyBeaconBlock, - ), - messaging.NewRoute[*BeaconBlock, transition.ValidatorUpdates]( - messages.FinalizeBeaconBlock, - ), - messaging.NewRoute[*Genesis, transition.ValidatorUpdates]( - messages.ProcessGenesisData, - ), - messaging.NewRoute[*BlobSidecars, *BlobSidecars]( - messages.VerifySidecars, - ), - messaging.NewRoute[*BlobSidecars, *BlobSidecars]( - messages.ProcessSidecars, - ), - } -} diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 60cfb0a449..2279d6981f 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -23,7 +23,7 @@ package components import ( "cosmossdk.io/core/appmodule/v2" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/messaging" + "github.com/berachain/beacon-kit/mod/async/pkg/notify" asyncserver "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" blockstore "github.com/berachain/beacon-kit/mod/beacon/block_store" @@ -430,7 +430,46 @@ type ( /* -------------------------------------------------------------------------- */ // Events. +// +//nolint:lll // long generic types type ( + + // GenesisDataReceivedEvent is a type alias for the genesis data received event. + GenesisDataReceivedEvent = asynctypes.Event[*Genesis] + + // GenesisDataProcessedEvent is a type alias for the genesis data processed event. + GenesisDataProcessedEvent = asynctypes.Event[transition.ValidatorUpdates] + + // NewSlotEvent is a type alias for the new slot event. + NewSlotEvent = asynctypes.Event[*SlotData] + + // BuiltBeaconBlockEvent is a type alias for the built beacon block event. + BuiltBeaconBlockEvent = asynctypes.Event[*BeaconBlock] + + // BuiltSidecarsEvent is a type alias for the built sidecars event. + BuiltSidecarsEvent = asynctypes.Event[*BlobSidecars] + + // BeaconBlockReceivedEvent is a type alias for the beacon block received event. + BeaconBlockReceivedEvent = asynctypes.Event[*BeaconBlock] + + // SidecarsReceivedEvent is a type alias for the sidecars received event. + SidecarsReceivedEvent = asynctypes.Event[*BlobSidecars] + + // BeaconBlockVerifiedEvent is a type alias for the beacon block verified event. + BeaconBlockVerifiedEvent = asynctypes.Event[*BeaconBlock] + + // SidecarsVerifiedEvent is a type alias for the sidecars verified event. + SidecarsVerifiedEvent = asynctypes.Event[*BlobSidecars] + + // FinalBeaconBlockReceivedEvent is a type alias for the final beacon block received event. + FinalBeaconBlockReceivedEvent = asynctypes.Event[*BeaconBlock] + + // FinalSidecarsReceivedEvent is a type alias for the final sidecars received event. + FinalSidecarsReceivedEvent = asynctypes.Event[*BlobSidecars] + + // FinalValidatorUpdatesProcessedEvent is a type alias for the final validator updates processed event. + FinalValidatorUpdatesProcessedEvent = asynctypes.Event[transition.ValidatorUpdates] + // FinalizedBlockEvent is a type alias for the block event. FinalizedBlockEvent = asynctypes.Event[*BeaconBlock] ) @@ -438,33 +477,19 @@ type ( // Messages. type ( // BlockMessage is a type alias for the block message. - BlockMessage = asynctypes.Message[*BeaconBlock] + BlockMessage = asynctypes.Event[*BeaconBlock] // GenesisMessage is a type alias for the genesis message. - GenesisMessage = asynctypes.Message[*Genesis] + GenesisMessage = asynctypes.Event[*Genesis] // SidecarMessage is a type alias for the sidecar message. - SidecarMessage = asynctypes.Message[*BlobSidecars] + SidecarMessage = asynctypes.Event[*BlobSidecars] // SlotMessage is a type alias for the slot message. - SlotMessage = asynctypes.Message[*SlotData] + SlotMessage = asynctypes.Event[*SlotData] // StatusMessage is a type alias for the status message. - StatusMessage = asynctypes.Message[*service.StatusEvent] -) - -// Futures. -type ( - // BlockFuture is a type alias for a future containing a BeaconBlock. - BlockFuture = asynctypes.Future[*BeaconBlock] - // BlockBundleFuture is a type alias for a future containing a - // BeaconBlockBundle. - BlockBundleFuture = asynctypes.Future[*BeaconBlockBundle] - // SidecarFuture is a type alias for a future containing the BlobSidecars. - SidecarFuture = asynctypes.Future[*BlobSidecars] - // ValidatorUpdateFuture is a type alias for a future containing the - // ValidatorUpdates. - ValidatorUpdateFuture = asynctypes.Future[transition.ValidatorUpdates] + StatusMessage = asynctypes.Event[*service.StatusEvent] ) /* -------------------------------------------------------------------------- */ @@ -472,7 +497,7 @@ type ( /* -------------------------------------------------------------------------- */ type ( - BeaconBlockFinalizedPublisher = messaging.Publisher[FinalizedBlockEvent] + BeaconBlockFinalizedPublisher = notify.Publisher[FinalizedBlockEvent] ) /* -------------------------------------------------------------------------- */ @@ -485,9 +510,6 @@ type ( // EventServer is a type alias for the event server. EventServer = asyncserver.EventServer - - // MessageServer is a type alias for the messages server. - MessageServer = asyncserver.MessageServer ) /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/messages/events.go b/mod/primitives/pkg/events/events.go similarity index 91% rename from mod/primitives/pkg/messages/events.go rename to mod/primitives/pkg/events/events.go index 26e4c908c5..dd1afcae65 100644 --- a/mod/primitives/pkg/messages/events.go +++ b/mod/primitives/pkg/events/events.go @@ -18,30 +18,29 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package messages - -// events. -const ( - BeaconBlockFinalizedEvent = "beacon-block-finalized" -) +package events // events, topologically sorted. const ( + // genesis data events GenesisDataReceived = "genesis-data-received" GenesisDataProcessed = "genesis-data-processed" + // pre proposal events NewSlot = "new-slot" BuiltBeaconBlock = "built-beacon-block" BuiltSidecars = "built-sidecars" + // proposal processing events BeaconBlockReceived = "beacon-block-received" SidecarsReceived = "sidecars-received" BeaconBlockVerified = "beacon-block-verified" SidecarsVerified = "sidecars-verified" + // finalize block events FinalBeaconBlockReceived = "final-beacon-block-received" FinalSidecarsReceived = "final-blob-sidecars-received" FinalValidatorUpdatesProcessed = "final-validator-updates" - // FinalSidecarsProcessed = "final-sidecars-processed" + BeaconBlockFinalizedEvent = "beacon-block-finalized" ) diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 712cf18acc..210d8c76ba 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -27,8 +27,8 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" cmtabci "github.com/cometbft/cometbft/abci/types" @@ -60,7 +60,7 @@ func (h *ABCIMiddleware[ } if err = h.dispatcher.PublishEvent( - async.NewEvent(ctx, messages.GenesisDataReceived, *data), + async.NewEvent(ctx, events.GenesisDataReceived, *data), ); err != nil { return nil, err } @@ -96,7 +96,7 @@ func (h *ABCIMiddleware[ if err = h.dispatcher.PublishEvent( async.NewEvent( - ctx, messages.NewSlot, slotData, + ctx, events.NewSlot, slotData, ), ); err != nil { return nil, nil, err @@ -186,7 +186,7 @@ func (h *ABCIMiddleware[ // TODO: implement service // notify that the beacon block has been received. if err = h.dispatcher.PublishEvent( - async.NewEvent(ctx, messages.BeaconBlockReceived, blk), + async.NewEvent(ctx, events.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -198,7 +198,7 @@ func (h *ABCIMiddleware[ // notify that the sidecars have been received. if err = h.dispatcher.PublishEvent( - async.NewEvent(ctx, messages.SidecarsReceived, sidecars), + async.NewEvent(ctx, events.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -266,14 +266,14 @@ func (h *ABCIMiddleware[ // notify that the final beacon block has been received. if err = h.dispatcher.PublishEvent( - async.NewEvent(ctx, messages.FinalBeaconBlockReceived, blk), + async.NewEvent(ctx, events.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } // notify that the final blob sidecars have been received. if err = h.dispatcher.PublishEvent( - async.NewEvent(ctx, messages.FinalSidecarsReceived, blobs), + async.NewEvent(ctx, events.FinalSidecarsReceived, blobs), ); err != nil { return nil, err } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 8486980f30..098d179089 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -28,7 +28,7 @@ import ( "github.com/berachain/beacon-kit/mod/p2p" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" - "github.com/berachain/beacon-kit/mod/primitives/pkg/messages" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" ) @@ -136,32 +136,32 @@ func (am *ABCIMiddleware[ ]) Start() error { // subGenDat if err := am.dispatcher.Subscribe( - messages.GenesisDataProcessed, am.subGenDataProcessed, + events.GenesisDataProcessed, am.subGenDataProcessed, ); err != nil { return err } if err := am.dispatcher.Subscribe( - messages.BuiltBeaconBlock, am.subBuiltBeaconBlock, + events.BuiltBeaconBlock, am.subBuiltBeaconBlock, ); err != nil { return err } if err := am.dispatcher.Subscribe( - messages.BuiltSidecars, am.subBuiltSidecars, + events.BuiltSidecars, am.subBuiltSidecars, ); err != nil { return err } if err := am.dispatcher.Subscribe( - messages.BeaconBlockVerified, am.subBBVerified, + events.BeaconBlockVerified, am.subBBVerified, ); err != nil { return err } if err := am.dispatcher.Subscribe( - messages.SidecarsVerified, am.subSCVerified, + events.SidecarsVerified, am.subSCVerified, ); err != nil { return err } if err := am.dispatcher.Subscribe( - messages.FinalValidatorUpdatesProcessed, am.subFinalValidatorUpdates, + events.FinalValidatorUpdatesProcessed, am.subFinalValidatorUpdates, ); err != nil { return err } diff --git a/mod/storage/pkg/manager/manager_test.go b/mod/storage/pkg/manager/manager_test.go index 17eb5ce99a..f6f6f75a02 100644 --- a/mod/storage/pkg/manager/manager_test.go +++ b/mod/storage/pkg/manager/manager_test.go @@ -31,6 +31,7 @@ import ( "time" "cosmossdk.io/log" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -39,8 +40,7 @@ import ( func TestDBManager_Start(t *testing.T) { mockPrunable := new(mocks.Prunable) - - ch := make(chan manager.BlockEvent[manager.BeaconBlock], 1) + ch := async.NewSubscription[manager.BlockEvent[manager.BeaconBlock]]() pruneParamsFn := func(_ manager.BlockEvent[manager.BeaconBlock]) (uint64, uint64) { return 0, 0 diff --git a/mod/storage/pkg/manager/types.go b/mod/storage/pkg/manager/types.go index d4b5ae2ca6..e4a8b76729 100644 --- a/mod/storage/pkg/manager/types.go +++ b/mod/storage/pkg/manager/types.go @@ -26,6 +26,8 @@ package manager import ( + "context" + asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -38,5 +40,7 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { Is(asynctypes.EventID) bool + ID() asynctypes.EventID Data() BeaconBlockT + Context() context.Context } diff --git a/mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go b/mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go new file mode 100644 index 0000000000..87e75ae02c --- /dev/null +++ b/mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go @@ -0,0 +1,71 @@ +// Code generated by mockery v2.44.1. DO NOT EDIT. + +package mocks + +import ( + context "context" + + pruner "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" + mock "github.com/stretchr/testify/mock" +) + +// BeaconBlockSubscription is an autogenerated mock type for the BeaconBlockSubscription type +type BeaconBlockSubscription[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]] struct { + mock.Mock +} + +type BeaconBlockSubscription_Expecter[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]] struct { + mock *mock.Mock +} + +func (_m *BeaconBlockSubscription[BeaconBlockT, BlockEventT]) EXPECT() *BeaconBlockSubscription_Expecter[BeaconBlockT, BlockEventT] { + return &BeaconBlockSubscription_Expecter[BeaconBlockT, BlockEventT]{mock: &_m.Mock} +} + +// Listen provides a mock function with given fields: ctx, fn +func (_m *BeaconBlockSubscription[BeaconBlockT, BlockEventT]) Listen(ctx context.Context, fn func(BlockEventT)) { + _m.Called(ctx, fn) +} + +// BeaconBlockSubscription_Listen_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Listen' +type BeaconBlockSubscription_Listen_Call[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]] struct { + *mock.Call +} + +// Listen is a helper method to define mock.On call +// - ctx context.Context +// - fn func(BlockEventT) +func (_e *BeaconBlockSubscription_Expecter[BeaconBlockT, BlockEventT]) Listen(ctx interface{}, fn interface{}) *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { + return &BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]{Call: _e.mock.On("Listen", ctx, fn)} +} + +func (_c *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]) Run(run func(ctx context.Context, fn func(BlockEventT))) *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(func(BlockEventT))) + }) + return _c +} + +func (_c *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]) Return() *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { + _c.Call.Return() + return _c +} + +func (_c *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]) RunAndReturn(run func(context.Context, func(BlockEventT))) *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { + _c.Call.Return(run) + return _c +} + +// NewBeaconBlockSubscription creates a new instance of BeaconBlockSubscription. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewBeaconBlockSubscription[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]](t interface { + mock.TestingT + Cleanup(func()) +}) *BeaconBlockSubscription[BeaconBlockT, BlockEventT] { + mock := &BeaconBlockSubscription[BeaconBlockT, BlockEventT]{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index e69a1fea56..d39df6b022 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -3,9 +3,12 @@ package mocks import ( - types "github.com/berachain/beacon-kit/mod/async/pkg/types" + context "context" + pruner "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" mock "github.com/stretchr/testify/mock" + + types "github.com/berachain/beacon-kit/mod/async/pkg/types" ) // BlockEvent is an autogenerated mock type for the BlockEvent type @@ -21,6 +24,53 @@ func (_m *BlockEvent[BeaconBlockT]) EXPECT() *BlockEvent_Expecter[BeaconBlockT] return &BlockEvent_Expecter[BeaconBlockT]{mock: &_m.Mock} } +// Context provides a mock function with given fields: +func (_m *BlockEvent[BeaconBlockT]) Context() context.Context { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Context") + } + + var r0 context.Context + if rf, ok := ret.Get(0).(func() context.Context); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(context.Context) + } + } + + return r0 +} + +// BlockEvent_Context_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Context' +type BlockEvent_Context_Call[BeaconBlockT pruner.BeaconBlock] struct { + *mock.Call +} + +// Context is a helper method to define mock.On call +func (_e *BlockEvent_Expecter[BeaconBlockT]) Context() *BlockEvent_Context_Call[BeaconBlockT] { + return &BlockEvent_Context_Call[BeaconBlockT]{Call: _e.mock.On("Context")} +} + +func (_c *BlockEvent_Context_Call[BeaconBlockT]) Run(run func()) *BlockEvent_Context_Call[BeaconBlockT] { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *BlockEvent_Context_Call[BeaconBlockT]) Return(_a0 context.Context) *BlockEvent_Context_Call[BeaconBlockT] { + _c.Call.Return(_a0) + return _c +} + +func (_c *BlockEvent_Context_Call[BeaconBlockT]) RunAndReturn(run func() context.Context) *BlockEvent_Context_Call[BeaconBlockT] { + _c.Call.Return(run) + return _c +} + // Data provides a mock function with given fields: func (_m *BlockEvent[BeaconBlockT]) Data() BeaconBlockT { ret := _m.Called() @@ -66,8 +116,53 @@ func (_c *BlockEvent_Data_Call[BeaconBlockT]) RunAndReturn(run func() BeaconBloc return _c } +// ID provides a mock function with given fields: +func (_m *BlockEvent[BeaconBlockT]) ID() types.EventID { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for ID") + } + + var r0 types.EventID + if rf, ok := ret.Get(0).(func() types.EventID); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(types.EventID) + } + + return r0 +} + +// BlockEvent_ID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ID' +type BlockEvent_ID_Call[BeaconBlockT pruner.BeaconBlock] struct { + *mock.Call +} + +// ID is a helper method to define mock.On call +func (_e *BlockEvent_Expecter[BeaconBlockT]) ID() *BlockEvent_ID_Call[BeaconBlockT] { + return &BlockEvent_ID_Call[BeaconBlockT]{Call: _e.mock.On("ID")} +} + +func (_c *BlockEvent_ID_Call[BeaconBlockT]) Run(run func()) *BlockEvent_ID_Call[BeaconBlockT] { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 types.EventID) *BlockEvent_ID_Call[BeaconBlockT] { + _c.Call.Return(_a0) + return _c +} + +func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() types.EventID) *BlockEvent_ID_Call[BeaconBlockT] { + _c.Call.Return(run) + return _c +} + // Is provides a mock function with given fields: _a0 -func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.MessageID) bool { +func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.EventID) bool { ret := _m.Called(_a0) if len(ret) == 0 { @@ -75,7 +170,7 @@ func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.MessageID) bool { } var r0 bool - if rf, ok := ret.Get(0).(func(types.MessageID) bool); ok { + if rf, ok := ret.Get(0).(func(types.EventID) bool); ok { r0 = rf(_a0) } else { r0 = ret.Get(0).(bool) @@ -95,9 +190,9 @@ func (_e *BlockEvent_Expecter[BeaconBlockT]) Is(_a0 interface{}) *BlockEvent_Is_ return &BlockEvent_Is_Call[BeaconBlockT]{Call: _e.mock.On("Is", _a0)} } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 types.MessageID)) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 types.EventID)) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.MessageID)) + run(args[0].(types.EventID)) }) return _c } @@ -107,7 +202,7 @@ func (_c *BlockEvent_Is_Call[BeaconBlockT]) Return(_a0 bool) *BlockEvent_Is_Call return _c } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(types.MessageID) bool) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(types.EventID) bool) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Return(run) return _c } diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 72dc6bc85c..f41594bb6e 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -44,11 +44,11 @@ type pruner[ BlockEventT BlockEvent[BeaconBlockT], PrunableT Prunable, ] struct { - prunable Prunable - logger log.Logger[any] - name string - finalizedBlockCh chan BlockEventT - pruneRangeFn func(BlockEventT) (uint64, uint64) + prunable Prunable + logger log.Logger[any] + name string + subBeaconBlockFinalized BeaconBlockSubscription[BeaconBlockT, BlockEventT] + pruneRangeFn func(BlockEventT) (uint64, uint64) } // NewPruner creates a new Pruner. @@ -60,37 +60,29 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - finalizedBlockCh chan BlockEventT, + subBeaconBlockFinalized BeaconBlockSubscription[BeaconBlockT, BlockEventT], pruneRangeFn func(BlockEventT) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, BlockEventT, PrunableT]{ - logger: logger, - prunable: prunable, - name: name, - finalizedBlockCh: finalizedBlockCh, - pruneRangeFn: pruneRangeFn, + logger: logger, + prunable: prunable, + name: name, + subBeaconBlockFinalized: subBeaconBlockFinalized, + pruneRangeFn: pruneRangeFn, } } // Start starts the Pruner by listening for new indexes to prune. -func (p *pruner[_, BlockEventT, _]) Start(ctx context.Context) { - go p.start(ctx) +func (p *pruner[BeaconBlockT, BlockEventT, _]) Start(ctx context.Context) { + p.subBeaconBlockFinalized.Listen(ctx, p.onFinalizeBlock) } -// start listens for new indexes to prune. -func (p *pruner[_, BlockEventT, _]) start( - ctx context.Context, -) { - for { - select { - case <-ctx.Done(): - return - case event := <-p.finalizedBlockCh: - start, end := p.pruneRangeFn(event) - if err := p.prunable.Prune(start, end); err != nil { - p.logger.Error("‼️ error pruning index ‼️", "error", err) - } - } +// onFinalizeBlock will prune the prunable store based on the received +// finalized block event. +func (p *pruner[BeaconBlockT, BlockEventT, _]) onFinalizeBlock(event BlockEventT) { + start, end := p.pruneRangeFn(event) + if err := p.prunable.Prune(start, end); err != nil { + p.logger.Error("‼️ error pruning index ‼️", "error", err) } } diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 0c664b99d4..39b84a10b0 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -31,6 +31,7 @@ import ( "time" "cosmossdk.io/log" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -70,7 +71,7 @@ func TestPruner(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { logger := log.NewNopLogger() - ch := make(chan pruner.BlockEvent[pruner.BeaconBlock]) + ch := async.NewSubscription[pruner.BlockEvent[pruner.BeaconBlock]]() mockPrunable := new(mocks.Prunable) mockPrunable.On("Prune", mock.Anything, mock.Anything). Return(nil) diff --git a/mod/storage/pkg/pruner/types.go b/mod/storage/pkg/pruner/types.go index 2e97436760..7d3f058bcc 100644 --- a/mod/storage/pkg/pruner/types.go +++ b/mod/storage/pkg/pruner/types.go @@ -32,6 +32,14 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) +// BeaconBlockSubscription is an interface for beacon block subscriptions. +type BeaconBlockSubscription[ + BeaconBlockT BeaconBlock, + BlockEventT BlockEvent[BeaconBlockT], +] interface { + Listen(ctx context.Context, fn func(BlockEventT)) +} + // BeaconBlock is an interface for beacon blocks. type BeaconBlock interface { GetSlot() math.U64 @@ -41,6 +49,8 @@ type BeaconBlock interface { type BlockEvent[BeaconBlockT BeaconBlock] interface { Is(asynctypes.EventID) bool Data() BeaconBlockT + Context() context.Context + ID() asynctypes.EventID } // Prunable is an interface representing a store that can be pruned. From 807363163a5dbab5d11a6270b233d91abf414db5 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Thu, 15 Aug 2024 18:38:04 -0400 Subject: [PATCH 51/75] tidy --- mod/async/pkg/notify/errors.go | 6 ++++-- mod/async/pkg/server/errors.go | 14 ++++++++++++-- mod/beacon/go.mod | 1 - 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/mod/async/pkg/notify/errors.go b/mod/async/pkg/notify/errors.go index 68abbea1cf..b3049d40ab 100644 --- a/mod/async/pkg/notify/errors.go +++ b/mod/async/pkg/notify/errors.go @@ -28,13 +28,15 @@ import ( // //nolint:gochecknoglobals // errors var ( + ErrIncompatible = errors.New("incompatible assignee") // errIncompatibleAssignee is the error returned when the assignee is not // compatible with the assigner. errIncompatibleAssignee = func( assigner interface{}, assignee interface{}, ) error { - return errors.Newf( - "incompatible assignee, expected: %T, received: %T", + return errors.Wrapf( + ErrIncompatible, + "expected: %T, received: %T", assigner, assignee, ) diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/server/errors.go index 89af5a1a0d..7bf7a29549 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/server/errors.go @@ -27,10 +27,20 @@ import ( //nolint:gochecknoglobals // errors var ( + ErrNotFound = errors.New("not found") + ErrAlreadyExists = errors.New("already exists") errPublisherNotFound = func(eventID types.EventID) error { - return errors.Newf("publisher not found for eventID: %s", eventID) + return errors.Wrapf( + ErrNotFound, + "publisher not found for eventID: %s", + eventID, + ) } errPublisherAlreadyExists = func(eventID types.EventID) error { - return errors.Newf("publisher already exists for eventID: %s", eventID) + return errors.Wrapf( + ErrAlreadyExists, + "publisher already exists for eventID: %s", + eventID, + ) } ) diff --git a/mod/beacon/go.mod b/mod/beacon/go.mod index 870dbb08dc..8f97ff8fb7 100644 --- a/mod/beacon/go.mod +++ b/mod/beacon/go.mod @@ -81,7 +81,6 @@ require ( github.com/yusufpapurcu/wmi v1.2.4 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect - golang.org/x/net v0.28.0 // indirect golang.org/x/sys v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect google.golang.org/protobuf v1.34.2 // indirect From 55241fee578bca5531e65b065f283ab1a178f762 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 02:13:28 -0400 Subject: [PATCH 52/75] builds --- mod/node-core/pkg/components/defaults.go | 5 ++++- mod/node-core/pkg/components/dispatcher.go | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/mod/node-core/pkg/components/defaults.go b/mod/node-core/pkg/components/defaults.go index f08cdf1e89..421afa1b71 100644 --- a/mod/node-core/pkg/components/defaults.go +++ b/mod/node-core/pkg/components/defaults.go @@ -65,6 +65,10 @@ func DefaultComponents() []any { // TODO Hacks ProvideKVStoreService, ProvideKVStoreKey, + // loggerT im gonna kms + ProvideDispatcher[LoggerT], + ProvidePublishers, + ProvideEventServer, } components = append(components, ProvideNodeAPIServer[LoggerT], @@ -72,6 +76,5 @@ func DefaultComponents() []any { ProvideNodeAPIBackend, ) components = append(components, DefaultNodeAPIHandlers()...) - components = append(components, DefaultDispatcherComponents()...) return components } diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index 20cf75f09c..6c5e5250eb 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -22,23 +22,26 @@ package components import ( "cosmossdk.io/depinject" - sdklog "cosmossdk.io/log" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" ) // DispatcherInput is the input for the Dispatcher. -type DispatcherInput struct { +type DispatcherInput[ + LoggerT log.AdvancedLogger[any, LoggerT], +] struct { depinject.In EventServer *EventServer - Logger log.AdvancedLogger[any, sdklog.Logger] + Logger LoggerT Publishers []asynctypes.Publisher } // ProvideDispatcher provides a new Dispatcher. -func ProvideDispatcher( - in DispatcherInput, +func ProvideDispatcher[ + LoggerT log.AdvancedLogger[any, LoggerT], +]( + in DispatcherInput[LoggerT], ) (*Dispatcher, error) { d := dispatcher.NewDispatcher( in.EventServer, @@ -50,9 +53,11 @@ func ProvideDispatcher( return d, nil } -func DefaultDispatcherComponents() []any { +func DefaultDispatcherComponents[ + LoggerT log.AdvancedLogger[any, LoggerT], +]() []any { return []any{ - ProvideDispatcher, + ProvideDispatcher[LoggerT], ProvidePublishers, ProvideEventServer, } From 2488a157b2b6c60894735ff1fd0f0a5f9034e5a2 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 02:41:27 -0400 Subject: [PATCH 53/75] bet --- mod/async/pkg/dispatcher/dispatcher.go | 2 +- mod/async/pkg/{notify => publisher}/assert.go | 2 +- .../pkg/{notify => publisher}/constants.go | 2 +- mod/async/pkg/{notify => publisher}/errors.go | 2 +- .../pkg/{notify => publisher}/publisher.go | 8 +-- mod/async/pkg/types/dispatcher.go | 12 ---- mod/async/pkg/types/subscription.go | 7 ++- mod/beacon/blockchain/service.go | 62 ++++++++++--------- mod/da/pkg/da/service.go | 4 +- mod/node-core/pkg/components/events.go | 28 ++++----- mod/node-core/pkg/components/types.go | 4 +- mod/runtime/pkg/middleware/abci.go | 2 + 12 files changed, 68 insertions(+), 67 deletions(-) rename mod/async/pkg/{notify => publisher}/assert.go (98%) rename mod/async/pkg/{notify => publisher}/constants.go (98%) rename mod/async/pkg/{notify => publisher}/errors.go (98%) rename mod/async/pkg/{notify => publisher}/publisher.go (95%) diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 01d110daec..b2e9b881cc 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -31,7 +31,7 @@ import ( var _ types.Dispatcher = (*Dispatcher)(nil) // Dispatcher faciliates asynchronous communication between components, -// typically services. It acts as an API facade to the underlying eventserver +// typically services. It acts as an API facade to the underlying eventserver. type Dispatcher struct { eventServer EventServer logger log.Logger[any] diff --git a/mod/async/pkg/notify/assert.go b/mod/async/pkg/publisher/assert.go similarity index 98% rename from mod/async/pkg/notify/assert.go rename to mod/async/pkg/publisher/assert.go index 410c4e29ac..de4663c25d 100644 --- a/mod/async/pkg/notify/assert.go +++ b/mod/async/pkg/publisher/assert.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package notify +package publisher // ensureType ensures that the provided entity is of type T. // It returns a typed entity or an error if the type is not correct. diff --git a/mod/async/pkg/notify/constants.go b/mod/async/pkg/publisher/constants.go similarity index 98% rename from mod/async/pkg/notify/constants.go rename to mod/async/pkg/publisher/constants.go index ad627bc394..05de997293 100644 --- a/mod/async/pkg/notify/constants.go +++ b/mod/async/pkg/publisher/constants.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package notify +package publisher import "time" diff --git a/mod/async/pkg/notify/errors.go b/mod/async/pkg/publisher/errors.go similarity index 98% rename from mod/async/pkg/notify/errors.go rename to mod/async/pkg/publisher/errors.go index b3049d40ab..f2c2f65c00 100644 --- a/mod/async/pkg/notify/errors.go +++ b/mod/async/pkg/publisher/errors.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package notify +package publisher import ( "github.com/berachain/beacon-kit/mod/errors" diff --git a/mod/async/pkg/notify/publisher.go b/mod/async/pkg/publisher/publisher.go similarity index 95% rename from mod/async/pkg/notify/publisher.go rename to mod/async/pkg/publisher/publisher.go index 3a0fab987f..1984b801dd 100644 --- a/mod/async/pkg/notify/publisher.go +++ b/mod/async/pkg/publisher/publisher.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package notify +package publisher import ( "context" @@ -42,9 +42,9 @@ type Publisher[T types.BaseEvent] struct { mu sync.Mutex } -// NewPublisher creates a new publisher publishing events of type T for the +// New creates a new publisher publishing events of type T for the // provided eventID. -func NewPublisher[T types.BaseEvent](eventID string) *Publisher[T] { +func New[T types.BaseEvent](eventID string) *Publisher[T] { return &Publisher[T]{ eventID: types.EventID(eventID), subscriptions: make(map[types.Subscription[T]]struct{}), @@ -84,7 +84,7 @@ func (p *Publisher[T]) start(ctx context.Context) { func (p *Publisher[T]) Publish(msg types.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { - return err + return errIncompatibleAssignee(msg, *new(T)) } ctx := msg.Context() select { diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index 3bdb1c44bc..70cfd1379f 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -59,15 +59,3 @@ type Publisher interface { // EventID returns the event ID that the publisher is responsible for. EventID() EventID } - -// messageRoute is the interface that supports basic message route operations. -type MessageRoute interface { - // RegisterRecipient sets the recipient for the route. - RegisterReceiver(ch any) error - // SendRequest sends a request to the recipient. - SendRequest(msg BaseEvent, future any) error - // SendResponse sends a response to the recipient. - SendResponse(msg BaseEvent) error - // MessageID returns the message ID that the route is responsible for. - MessageID() EventID -} diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go index fe8602146b..7f6b3fe8a3 100644 --- a/mod/async/pkg/types/subscription.go +++ b/mod/async/pkg/types/subscription.go @@ -20,7 +20,10 @@ package types -import "context" +import ( + "context" + "fmt" +) // Subscription is a channel that receives events. type Subscription[T BaseEvent] chan T @@ -60,8 +63,10 @@ func (s Subscription[T]) Listen( // Await will block until an event is received from the subscription or the // context is canceled. func (s Subscription[T]) Await(ctx context.Context) (T, error) { + fmt.Println("AWAITING EVENT") select { case event := <-s: + fmt.Println("GOT EVENT") return event, nil case <-ctx.Done(): return *new(T), ctx.Err() diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 5518abe68c..1fde0d2be9 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -22,9 +22,10 @@ package blockchain import ( "context" + "fmt" "sync" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/events" @@ -63,7 +64,7 @@ type Service[ // chainSpec holds the chain specifications. chainSpec common.ChainSpec // dispatcher is the dispatcher for the service. - dispatcher asynctypes.Dispatcher + dispatcher async.Dispatcher // executionEngine is the execution engine responsible for processing // execution payloads. executionEngine ExecutionEngine[PayloadAttributesT] @@ -87,12 +88,12 @@ type Service[ // subFinalBlkReceived is a channel for receiving finalize beacon block // requests. - subFinalBlkReceived asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] + subFinalBlkReceived async.Subscription[async.Event[BeaconBlockT]] // subBlockReceived is a channel for receiving verify beacon block requests. - subBlockReceived asynctypes.Subscription[asynctypes.Event[BeaconBlockT]] + subBlockReceived async.Subscription[async.Event[BeaconBlockT]] // subGenDataReceived is a subscription for receiving genesis data // received events. - subGenDataReceived asynctypes.Subscription[asynctypes.Event[GenesisT]] + subGenDataReceived async.Subscription[async.Event[GenesisT]] } // NewService creates a new validator service. @@ -123,7 +124,7 @@ func NewService[ ], logger log.Logger[any], chainSpec common.ChainSpec, - dispatcher asynctypes.Dispatcher, + dispatcher async.Dispatcher, executionEngine ExecutionEngine[PayloadAttributesT], localBuilder LocalBuilder[BeaconStateT], stateProcessor StateProcessor[ @@ -155,9 +156,9 @@ func NewService[ metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), - subFinalBlkReceived: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), - subBlockReceived: asynctypes.NewSubscription[asynctypes.Event[BeaconBlockT]](), - subGenDataReceived: asynctypes.NewSubscription[asynctypes.Event[GenesisT]](), + subFinalBlkReceived: async.NewSubscription[async.Event[BeaconBlockT]](), + subBlockReceived: async.NewSubscription[async.Event[BeaconBlockT]](), + subGenDataReceived: async.NewSubscription[async.Event[GenesisT]](), } } @@ -212,41 +213,44 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, _, -]) handleProcessGenesisDataEvent(msg asynctypes.Event[GenesisT]) { +]) handleProcessGenesisDataEvent(msg async.Event[GenesisT]) { var ( valUpdates transition.ValidatorUpdates - err error + genesisErr error ) if msg.Error() != nil { s.logger.Error("Error processing genesis data", "error", msg.Error()) } // Process the genesis data. - valUpdates, err = s.ProcessGenesisData(msg.Context(), msg.Data()) - if err != nil { - s.logger.Error("Failed to process genesis data", "error", err) + valUpdates, genesisErr = s.ProcessGenesisData(msg.Context(), msg.Data()) + if genesisErr != nil { + s.logger.Error("Failed to process genesis data", "error", genesisErr) } + fmt.Println("GEN DATA PROCESSED") // Emit the event containing the validator updates. - if err = s.dispatcher.PublishEvent( - asynctypes.NewEvent( + if err := s.dispatcher.PublishEvent( + async.NewEvent( msg.Context(), events.GenesisDataProcessed, valUpdates, - err, + genesisErr, ), ); err != nil { s.logger.Error( "Failed to emit event in process genesis data", "error", err, ) + panic(err) } + fmt.Println("EMITTED EVENT") } func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, ]) handleVerifyBeaconBlockEvent( - msg asynctypes.Event[BeaconBlockT], + msg async.Event[BeaconBlockT], ) { // If the block is nil, exit early. if msg.Error() != nil { @@ -257,7 +261,7 @@ func (s *Service[ // emit a BeaconBlockVerified event with the error result from \ // VerifyIncomingBlock if err := s.dispatcher.PublishEvent( - asynctypes.NewEvent( + async.NewEvent( msg.Context(), events.BeaconBlockVerified, msg.Data(), @@ -274,11 +278,11 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, _, ]) handleFinalizeBeaconBlockEvent( - msg asynctypes.Event[BeaconBlockT], + msg async.Event[BeaconBlockT], ) { var ( - valUpdates transition.ValidatorUpdates - err error + valUpdates transition.ValidatorUpdates + finalizeErr error ) // If there's an error in the event, log it and return if msg.Error() != nil { @@ -287,18 +291,20 @@ func (s *Service[ } // process the verified block and get the validator updates - valUpdates, err = s.ProcessBeaconBlock(msg.Context(), msg.Data()) - if err != nil { - s.logger.Error("Failed to process verified beacon block", "error", err) + valUpdates, finalizeErr = s.ProcessBeaconBlock(msg.Context(), msg.Data()) + if finalizeErr != nil { + s.logger.Error("Failed to process verified beacon block", + "error", finalizeErr, + ) } // Emit the event containing the validator updates. - if err = s.dispatcher.PublishEvent( - asynctypes.NewEvent( + if err := s.dispatcher.PublishEvent( + async.NewEvent( msg.Context(), events.FinalValidatorUpdatesProcessed, valUpdates, - err, + finalizeErr, ), ); err != nil { s.logger.Error( diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index f8a0eca3d4..dd6cb99ccc 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -31,7 +31,7 @@ import ( // The Data Availability service is responsible for verifying and processing // incoming blob sidecars. // -//nolint:lll // long types + type Service[ AvailabilityStoreT AvailabilityStore[BeaconBlockBodyT, BlobSidecarsT], BeaconBlockBodyT any, @@ -197,7 +197,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) verifySidecars( return err } - //nolint:lll // logging + s.logger.Info( "Blob sidecars verification succeeded - accepting incoming blob sidecars", "num_blobs", diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 3643a27c73..156a1fb4a0 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -21,7 +21,7 @@ package components import ( - "github.com/berachain/beacon-kit/mod/async/pkg/notify" + "github.com/berachain/beacon-kit/mod/async/pkg/publisher" "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/events" @@ -36,43 +36,43 @@ func ProvideEventServer() *EventServer { // finalized events. func ProvidePublishers() []asynctypes.Publisher { return []asynctypes.Publisher{ - notify.NewPublisher[GenesisDataReceivedEvent]( + publisher.New[GenesisDataReceivedEvent]( events.GenesisDataReceived, ), - notify.NewPublisher[GenesisDataProcessedEvent]( + publisher.New[GenesisDataProcessedEvent]( events.GenesisDataProcessed, ), - notify.NewPublisher[NewSlotEvent]( + publisher.New[NewSlotEvent]( events.NewSlot, ), - notify.NewPublisher[BuiltBeaconBlockEvent]( + publisher.New[BuiltBeaconBlockEvent]( events.BuiltBeaconBlock, ), - notify.NewPublisher[BuiltSidecarsEvent]( + publisher.New[BuiltSidecarsEvent]( events.BuiltSidecars, ), - notify.NewPublisher[BeaconBlockReceivedEvent]( + publisher.New[BeaconBlockReceivedEvent]( events.BeaconBlockReceived, ), - notify.NewPublisher[SidecarsReceivedEvent]( + publisher.New[SidecarsReceivedEvent]( events.SidecarsReceived, ), - notify.NewPublisher[BeaconBlockVerifiedEvent]( + publisher.New[BeaconBlockVerifiedEvent]( events.BeaconBlockVerified, ), - notify.NewPublisher[SidecarsVerifiedEvent]( + publisher.New[SidecarsVerifiedEvent]( events.SidecarsVerified, ), - notify.NewPublisher[FinalBeaconBlockReceivedEvent]( + publisher.New[FinalBeaconBlockReceivedEvent]( events.FinalBeaconBlockReceived, ), - notify.NewPublisher[FinalSidecarsReceivedEvent]( + publisher.New[FinalSidecarsReceivedEvent]( events.FinalSidecarsReceived, ), - notify.NewPublisher[FinalValidatorUpdatesProcessedEvent]( + publisher.New[FinalValidatorUpdatesProcessedEvent]( events.FinalValidatorUpdatesProcessed, ), - notify.NewPublisher[FinalizedBlockEvent]( + publisher.New[FinalizedBlockEvent]( events.BeaconBlockFinalizedEvent, ), } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 2279d6981f..b5d35e6dbc 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -23,7 +23,7 @@ package components import ( "cosmossdk.io/core/appmodule/v2" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/notify" + "github.com/berachain/beacon-kit/mod/async/pkg/publisher" asyncserver "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" blockstore "github.com/berachain/beacon-kit/mod/beacon/block_store" @@ -497,7 +497,7 @@ type ( /* -------------------------------------------------------------------------- */ type ( - BeaconBlockFinalizedPublisher = notify.Publisher[FinalizedBlockEvent] + BeaconBlockFinalizedPublisher = publisher.Publisher[FinalizedBlockEvent] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 210d8c76ba..1de13f1a19 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -22,6 +22,7 @@ package middleware import ( "context" + "fmt" "time" async "github.com/berachain/beacon-kit/mod/async/pkg/types" @@ -69,6 +70,7 @@ func (h *ABCIMiddleware[ if err != nil { return nil, err } + fmt.Println("GOT IT BACK") return gdpEvent.Data(), gdpEvent.Error() } From 7c4a05d685aac67e2c7f5f22e8ccd1dc586695ad Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 03:38:52 -0400 Subject: [PATCH 54/75] omg im so dumb --- mod/async/pkg/publisher/publisher.go | 2 +- mod/async/pkg/types/event.go | 6 +-- mod/async/pkg/types/subscription.go | 3 -- mod/beacon/blockchain/service.go | 3 -- .../pkg/components/service_registry.go | 2 + mod/runtime/pkg/middleware/abci.go | 46 +++++++++++-------- mod/runtime/pkg/middleware/constants.go | 4 ++ mod/runtime/pkg/middleware/middleware.go | 17 +++---- .../pkg/pruner/mocks/block_event.mock.go | 2 +- 9 files changed, 46 insertions(+), 39 deletions(-) diff --git a/mod/async/pkg/publisher/publisher.go b/mod/async/pkg/publisher/publisher.go index 1984b801dd..5c45dee49d 100644 --- a/mod/async/pkg/publisher/publisher.go +++ b/mod/async/pkg/publisher/publisher.go @@ -84,7 +84,7 @@ func (p *Publisher[T]) start(ctx context.Context) { func (p *Publisher[T]) Publish(msg types.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { - return errIncompatibleAssignee(msg, *new(T)) + return err } ctx := msg.Context() select { diff --git a/mod/async/pkg/types/event.go b/mod/async/pkg/types/event.go index 6ec58679f2..d5d2c03c60 100644 --- a/mod/async/pkg/types/event.go +++ b/mod/async/pkg/types/event.go @@ -41,18 +41,18 @@ type Event[DataT any] interface { BaseEvent Data() DataT Error() error - Is(messageType EventID) bool + Is(id EventID) bool } // NewEvent creates a new Event with the given context and beacon event. func NewEvent[ DataT any, ]( - ctx context.Context, messageType EventID, data DataT, errs ...error, + ctx context.Context, id EventID, data DataT, errs ...error, ) Event[DataT] { return &event[DataT]{ ctx: ctx, - id: messageType, + id: id, data: data, err: errors.Join(errs...), } diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go index 7f6b3fe8a3..0720babc15 100644 --- a/mod/async/pkg/types/subscription.go +++ b/mod/async/pkg/types/subscription.go @@ -22,7 +22,6 @@ package types import ( "context" - "fmt" ) // Subscription is a channel that receives events. @@ -63,10 +62,8 @@ func (s Subscription[T]) Listen( // Await will block until an event is received from the subscription or the // context is canceled. func (s Subscription[T]) Await(ctx context.Context) (T, error) { - fmt.Println("AWAITING EVENT") select { case event := <-s: - fmt.Println("GOT EVENT") return event, nil case <-ctx.Done(): return *new(T), ctx.Err() diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 1fde0d2be9..07fc0a14cd 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -22,7 +22,6 @@ package blockchain import ( "context" - "fmt" "sync" async "github.com/berachain/beacon-kit/mod/async/pkg/types" @@ -227,7 +226,6 @@ func (s *Service[ if genesisErr != nil { s.logger.Error("Failed to process genesis data", "error", genesisErr) } - fmt.Println("GEN DATA PROCESSED") // Emit the event containing the validator updates. if err := s.dispatcher.PublishEvent( @@ -244,7 +242,6 @@ func (s *Service[ ) panic(err) } - fmt.Println("EMITTED EVENT") } func (s *Service[ diff --git a/mod/node-core/pkg/components/service_registry.go b/mod/node-core/pkg/components/service_registry.go index e922543d8a..9294add654 100644 --- a/mod/node-core/pkg/components/service_registry.go +++ b/mod/node-core/pkg/components/service_registry.go @@ -32,6 +32,7 @@ type ServiceRegistryInput[ LoggerT log.AdvancedLogger[any, LoggerT], ] struct { depinject.In + Middleware *ABCIMiddleware BlockStoreService *BlockStoreService ChainService *ChainService DAService *DAService @@ -54,6 +55,7 @@ func ProvideServiceRegistry[ ) *service.Registry { return service.NewRegistry( service.WithLogger(in.Logger), + service.WithService(in.Middleware), service.WithService(in.Dispatcher), service.WithService(in.ValidatorService), service.WithService(in.BlockStoreService), diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 1de13f1a19..2c16619ae2 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -22,7 +22,6 @@ package middleware import ( "context" - "fmt" "time" async "github.com/berachain/beacon-kit/mod/async/pkg/types" @@ -48,9 +47,11 @@ func (h *ABCIMiddleware[ bz []byte, ) (transition.ValidatorUpdates, error) { var ( - err error - gdpEvent async.Event[transition.ValidatorUpdates] + err error + gdpEvent async.Event[transition.ValidatorUpdates] + awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) + defer cancel() // in theory this channel should already be empty, but we clear it anyways h.subGenDataProcessed.Clear() @@ -66,11 +67,11 @@ func (h *ABCIMiddleware[ return nil, err } - gdpEvent, err = h.subGenDataProcessed.Await(ctx) + gdpEvent, err = h.subGenDataProcessed.Await(awaitCtx) if err != nil { return nil, err } - fmt.Println("GOT IT BACK") + return gdpEvent.Data(), gdpEvent.Error() } @@ -86,11 +87,13 @@ func (h *ABCIMiddleware[ slotData SlotDataT, ) ([]byte, []byte, error) { var ( - err error - builtBBEvent async.Event[BeaconBlockT] - builtSCEvent async.Event[BlobSidecarsT] - startTime = time.Now() + err error + builtBBEvent async.Event[BeaconBlockT] + builtSCEvent async.Event[BlobSidecarsT] + startTime = time.Now() + awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) + defer cancel() defer h.metrics.measurePrepareProposalDuration(startTime) // in theory these subs should already be empty, but we clear them anyways h.subBuiltBeaconBlock.Clear() @@ -105,7 +108,7 @@ func (h *ABCIMiddleware[ } // wait for built beacon block - builtBBEvent, err = h.subBuiltBeaconBlock.Await(ctx) + builtBBEvent, err = h.subBuiltBeaconBlock.Await(awaitCtx) if err != nil { return nil, nil, err } @@ -114,7 +117,7 @@ func (h *ABCIMiddleware[ } // wait for built sidecars - builtSCEvent, err = h.subBuiltSidecars.Await(ctx) + builtSCEvent, err = h.subBuiltSidecars.Await(awaitCtx) if err != nil { return nil, nil, err } @@ -165,12 +168,14 @@ func (h *ABCIMiddleware[ req proto.Message, ) (proto.Message, error) { var ( - err error - startTime = time.Now() - blk BeaconBlockT - sidecars BlobSidecarsT + err error + startTime = time.Now() + blk BeaconBlockT + sidecars BlobSidecarsT + awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) - // in theory these subs should already be empty, but we clear them anyways + defer cancel() + // in theory these subs should already be empty, probably redundant h.subBBVerified.Clear() h.subSCVerified.Clear() abciReq, ok := req.(*cmtabci.ProcessProposalRequest) @@ -185,7 +190,6 @@ func (h *ABCIMiddleware[ return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } - // TODO: implement service // notify that the beacon block has been received. if err = h.dispatcher.PublishEvent( async.NewEvent(ctx, events.BeaconBlockReceived, blk), @@ -206,11 +210,11 @@ func (h *ABCIMiddleware[ } // err if the built beacon block or sidecars failed verification. - _, err = h.subBBVerified.Await(ctx) + _, err = h.subBBVerified.Await(awaitCtx) if err != nil { return h.createProcessProposalResponse(err) } - _, err = h.subSCVerified.Await(ctx) + _, err = h.subSCVerified.Await(awaitCtx) if err != nil { return h.createProcessProposalResponse(err) } @@ -245,7 +249,9 @@ func (h *ABCIMiddleware[ blk BeaconBlockT blobs BlobSidecarsT finalValUpdatesEvent async.Event[transition.ValidatorUpdates] + awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) + defer cancel() // in theory this sub should already be empty, but we clear them anyways h.subFinalValidatorUpdates.Clear() abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) @@ -281,7 +287,7 @@ func (h *ABCIMiddleware[ } // wait for the final validator updates. - finalValUpdatesEvent, err = h.subFinalValidatorUpdates.Await(ctx) + finalValUpdatesEvent, err = h.subFinalValidatorUpdates.Await(awaitCtx) if err != nil { return nil, err } diff --git a/mod/runtime/pkg/middleware/constants.go b/mod/runtime/pkg/middleware/constants.go index 4862a9a5b9..283b75aa59 100644 --- a/mod/runtime/pkg/middleware/constants.go +++ b/mod/runtime/pkg/middleware/constants.go @@ -20,6 +20,8 @@ package middleware +import "time" + const ( // BeaconBlockTxIndex represents the index of the beacon block transaction. // It is the first transaction in the tx list. @@ -27,4 +29,6 @@ const ( // BlobSidecarsTxIndex represents the index of the blob sidecar transaction. // It follows the beacon block transaction in the tx list. BlobSidecarsTxIndex + // AwaitTimeout is the timeout for awaiting events. + AwaitTimeout = 2 * time.Second ) diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 098d179089..9d2d86f0b1 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -21,6 +21,7 @@ package middleware import ( + "context" "encoding/json" "github.com/berachain/beacon-kit/mod/async/pkg/types" @@ -133,34 +134,34 @@ func NewABCIMiddleware[ func (am *ABCIMiddleware[ AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, -]) Start() error { - // subGenDat - if err := am.dispatcher.Subscribe( +]) Start(_ context.Context) error { + var err error + if err = am.dispatcher.Subscribe( events.GenesisDataProcessed, am.subGenDataProcessed, ); err != nil { return err } - if err := am.dispatcher.Subscribe( + if err = am.dispatcher.Subscribe( events.BuiltBeaconBlock, am.subBuiltBeaconBlock, ); err != nil { return err } - if err := am.dispatcher.Subscribe( + if err = am.dispatcher.Subscribe( events.BuiltSidecars, am.subBuiltSidecars, ); err != nil { return err } - if err := am.dispatcher.Subscribe( + if err = am.dispatcher.Subscribe( events.BeaconBlockVerified, am.subBBVerified, ); err != nil { return err } - if err := am.dispatcher.Subscribe( + if err = am.dispatcher.Subscribe( events.SidecarsVerified, am.subSCVerified, ); err != nil { return err } - if err := am.dispatcher.Subscribe( + if err = am.dispatcher.Subscribe( events.FinalValidatorUpdatesProcessed, am.subFinalValidatorUpdates, ); err != nil { return err diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index d39df6b022..5a23af48d4 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -185,7 +185,7 @@ type BlockEvent_Is_Call[BeaconBlockT pruner.BeaconBlock] struct { } // Is is a helper method to define mock.On call -// - _a0 types.MessageID +// - _a0 types.EventID func (_e *BlockEvent_Expecter[BeaconBlockT]) Is(_a0 interface{}) *BlockEvent_Is_Call[BeaconBlockT] { return &BlockEvent_Is_Call[BeaconBlockT]{Call: _e.mock.On("Is", _a0)} } From 55837916b020d2b6b58c45d40a825371636bc60a Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 10:29:26 -0400 Subject: [PATCH 55/75] temp --- mod/async/pkg/dispatcher/types.go | 3 ++- mod/async/pkg/types/errors.go | 27 --------------------------- mod/da/pkg/da/service.go | 1 - 3 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 mod/async/pkg/types/errors.go diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go index 51018e238d..c41f780405 100644 --- a/mod/async/pkg/dispatcher/types.go +++ b/mod/async/pkg/dispatcher/types.go @@ -32,7 +32,8 @@ type EventServer interface { Start(ctx context.Context) // RegisterPublishers registers the given publishers with the event server. RegisterPublishers(publishers ...types.Publisher) error - // Subscribe subscribes the given channel to the event with the given . + // Subscribe subscribes the given channel to the event with the given + // . Subscribe(mID types.EventID, ch any) error // Publish dispatches the given event to the event server. Publish(event types.BaseEvent) error diff --git a/mod/async/pkg/types/errors.go b/mod/async/pkg/types/errors.go deleted file mode 100644 index 4fc6b7dfb0..0000000000 --- a/mod/async/pkg/types/errors.go +++ /dev/null @@ -1,27 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package types - -import "errors" - -var ( - errTimeout = errors.New("future timed out") -) diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index dd6cb99ccc..08bc700179 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -197,7 +197,6 @@ func (s *Service[_, _, BlobSidecarsT, _]) verifySidecars( return err } - s.logger.Info( "Blob sidecars verification succeeded - accepting incoming blob sidecars", "num_blobs", From 9b951e0ff40c6997370513b0978e4c920f78ae6d Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 11:01:59 -0400 Subject: [PATCH 56/75] bet --- mod/beacon/block_store/service.go | 6 +++--- mod/primitives/pkg/events/events.go | 8 ++++---- mod/runtime/pkg/middleware/abci.go | 1 - mod/storage/pkg/pruner/pruner.go | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 2910349624..9ec31e4256 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -46,8 +46,6 @@ type Service[ } // NewService creates a new block service. -// -//nolint:lll // long type func NewService[ BeaconBlockT BeaconBlock, BlockStoreT BlockStore[BeaconBlockT], @@ -93,7 +91,9 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { // onFinalizeBlock is triggered when a finalized block event is received. // It stores the block in the KVStore. -func (s *Service[BeaconBlockT, _]) onFinalizeBlock(event async.Event[BeaconBlockT]) { +func (s *Service[BeaconBlockT, _]) onFinalizeBlock( + event async.Event[BeaconBlockT], +) { slot := event.Data().GetSlot() if err := s.store.Set(event.Data()); err != nil { s.logger.Error( diff --git a/mod/primitives/pkg/events/events.go b/mod/primitives/pkg/events/events.go index dd1afcae65..6d02f7b2e6 100644 --- a/mod/primitives/pkg/events/events.go +++ b/mod/primitives/pkg/events/events.go @@ -23,22 +23,22 @@ package events // events, topologically sorted. const ( - // genesis data events + // genesis data events. GenesisDataReceived = "genesis-data-received" GenesisDataProcessed = "genesis-data-processed" - // pre proposal events + // pre proposal events. NewSlot = "new-slot" BuiltBeaconBlock = "built-beacon-block" BuiltSidecars = "built-sidecars" - // proposal processing events + // proposal processing events. BeaconBlockReceived = "beacon-block-received" SidecarsReceived = "sidecars-received" BeaconBlockVerified = "beacon-block-verified" SidecarsVerified = "sidecars-verified" - // finalize block events + // finalize block events. FinalBeaconBlockReceived = "final-beacon-block-received" FinalSidecarsReceived = "final-blob-sidecars-received" FinalValidatorUpdatesProcessed = "final-validator-updates" diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 2c16619ae2..766e22bf31 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -268,7 +268,6 @@ func (h *ABCIMiddleware[ )) if err != nil { // If we don't have a block, we can't do anything. - //nolint:nilerr // by design. return nil, nil } diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index f41594bb6e..227041f612 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -79,7 +79,7 @@ func (p *pruner[BeaconBlockT, BlockEventT, _]) Start(ctx context.Context) { // onFinalizeBlock will prune the prunable store based on the received // finalized block event. -func (p *pruner[BeaconBlockT, BlockEventT, _]) onFinalizeBlock(event BlockEventT) { +func (p *pruner[_, BlockEventT, _]) onFinalizeBlock(event BlockEventT) { start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { p.logger.Error("‼️ error pruning index ‼️", "error", err) From d00a0c65f0addad07729ee69d51a178e41347c6c Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 16:52:24 -0400 Subject: [PATCH 57/75] bet --- mod/async/pkg/{publisher => broker}/assert.go | 2 +- .../publisher.go => broker/broker.go} | 50 +++--- .../pkg/{publisher => broker}/constants.go | 6 +- mod/async/pkg/{publisher => broker}/errors.go | 2 +- mod/async/pkg/dispatcher/dispatcher.go | 77 ++++++---- .../pkg/{server => dispatcher}/errors.go | 10 +- mod/async/pkg/dispatcher/types.go | 42 ----- mod/async/pkg/server/event.go | 91 ----------- mod/async/pkg/types/dispatcher.go | 6 +- mod/async/pkg/types/subscription.go | 76 --------- mod/beacon/block_store/service.go | 18 ++- mod/beacon/blockchain/execution_engine.go | 12 +- mod/beacon/blockchain/payload.go | 10 +- mod/beacon/blockchain/process.go | 4 +- mod/beacon/blockchain/receive.go | 4 +- mod/beacon/blockchain/service.go | 53 ++++--- mod/beacon/validator/service.go | 19 ++- mod/da/pkg/da/service.go | 24 ++- .../mocks/blobs_bundle.mock.go | 2 +- .../mocks/built_execution_payload_env.mock.go | 2 +- .../mocks/payload_attributer.mock.go | 2 +- mod/execution/pkg/deposit/service.go | 19 ++- mod/execution/pkg/deposit/sync.go | 10 +- .../backend/mocks/availability_store.mock.go | 2 +- .../backend/mocks/beacon_block_header.mock.go | 2 +- .../backend/mocks/beacon_state.mock.go | 2 +- .../backend/mocks/block_store.mock.go | 2 +- .../backend/mocks/deposit_store.mock.go | 2 +- mod/node-api/backend/mocks/node.mock.go | 2 +- .../backend/mocks/state_processor.mock.go | 2 +- .../backend/mocks/storage_backend.mock.go | 2 +- mod/node-api/backend/mocks/validator.mock.go | 2 +- mod/node-api/backend/mocks/withdrawal.mock.go | 2 +- .../mocks/withdrawal_credentials.mock.go | 2 +- .../pkg/components/availability_store.go | 3 +- mod/node-core/pkg/components/block_store.go | 3 +- mod/node-core/pkg/components/defaults.go | 2 - mod/node-core/pkg/components/deposit_store.go | 3 +- mod/node-core/pkg/components/dispatcher.go | 20 +-- mod/node-core/pkg/components/events.go | 38 ++--- mod/node-core/pkg/components/types.go | 8 +- .../pkg/crypto/mocks/bls_signer.mock.go | 2 +- mod/runtime/pkg/middleware/abci.go | 144 +++++++++++++----- mod/runtime/pkg/middleware/middleware.go | 24 +-- mod/runtime/pkg/service/mocks/basic.mock.go | 2 +- .../pkg/service/mocks/dispatcher.mock.go | 2 +- .../pkg/service/mocks/registry_option.mock.go | 2 +- mod/storage/pkg/interfaces/mocks/db.mock.go | 2 +- mod/storage/pkg/manager/manager_test.go | 3 +- .../pkg/pruner/mocks/beacon_block.mock.go | 2 +- .../pkg/pruner/mocks/block_event.mock.go | 2 +- mod/storage/pkg/pruner/mocks/prunable.mock.go | 2 +- mod/storage/pkg/pruner/mocks/pruner.mock.go | 2 +- mod/storage/pkg/pruner/pruner.go | 19 ++- mod/storage/pkg/pruner/pruner_test.go | 3 +- mod/storage/pkg/pruner/types.go | 8 - 56 files changed, 380 insertions(+), 479 deletions(-) rename mod/async/pkg/{publisher => broker}/assert.go (98%) rename mod/async/pkg/{publisher/publisher.go => broker/broker.go} (69%) rename mod/async/pkg/{publisher => broker}/constants.go (90%) rename mod/async/pkg/{publisher => broker}/errors.go (98%) rename mod/async/pkg/{server => dispatcher}/errors.go (84%) delete mode 100644 mod/async/pkg/dispatcher/types.go delete mode 100644 mod/async/pkg/server/event.go delete mode 100644 mod/async/pkg/types/subscription.go diff --git a/mod/async/pkg/publisher/assert.go b/mod/async/pkg/broker/assert.go similarity index 98% rename from mod/async/pkg/publisher/assert.go rename to mod/async/pkg/broker/assert.go index de4663c25d..1c9f00932a 100644 --- a/mod/async/pkg/publisher/assert.go +++ b/mod/async/pkg/broker/assert.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package publisher +package broker // ensureType ensures that the provided entity is of type T. // It returns a typed entity or an error if the type is not correct. diff --git a/mod/async/pkg/publisher/publisher.go b/mod/async/pkg/broker/broker.go similarity index 69% rename from mod/async/pkg/publisher/publisher.go rename to mod/async/pkg/broker/broker.go index 5c45dee49d..5fbc9125ec 100644 --- a/mod/async/pkg/publisher/publisher.go +++ b/mod/async/pkg/broker/broker.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package publisher +package broker import ( "context" @@ -28,12 +28,12 @@ import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" ) -// Publisher is responsible for broadcasting all events corresponding to the +// Broker is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Publisher[T types.BaseEvent] struct { +type Broker[T types.BaseEvent] struct { eventID types.EventID // subscriptions is a map of subscribed subscriptions. - subscriptions map[types.Subscription[T]]struct{} + subscriptions map[chan T]struct{} // msgs is the channel for publishing new messages. msgs chan T // timeout is the timeout for sending a msg to a client. @@ -42,34 +42,34 @@ type Publisher[T types.BaseEvent] struct { mu sync.Mutex } -// New creates a new publisher publishing events of type T for the +// New creates a new broker publishing events of type T for the // provided eventID. -func New[T types.BaseEvent](eventID string) *Publisher[T] { - return &Publisher[T]{ +func New[T types.BaseEvent](eventID string) *Broker[T] { + return &Broker[T]{ eventID: types.EventID(eventID), - subscriptions: make(map[types.Subscription[T]]struct{}), + subscriptions: make(map[chan T]struct{}), msgs: make(chan T, defaultBufferSize), - timeout: defaultPublisherTimeout, + timeout: defaultBrokerTimeout, mu: sync.Mutex{}, } } -// EventID returns the event ID that the publisher is responsible for. -func (p *Publisher[T]) EventID() types.EventID { +// EventID returns the event ID that the broker is responsible for. +func (p *Broker[T]) EventID() types.EventID { return p.eventID } -// Start starts the publisher loop. -func (p *Publisher[T]) Start(ctx context.Context) { +// Start starts the broker loop. +func (p *Broker[T]) Start(ctx context.Context) { go p.start(ctx) } -// start starts the publisher loop. -func (p *Publisher[T]) start(ctx context.Context) { +// start starts the broker loop. +func (p *Broker[T]) start(ctx context.Context) { for { select { case <-ctx.Done(): - // close all leftover clients and break the publisher loop + // close all leftover clients and break the broker loop p.shutdown() return case msg := <-p.msgs: @@ -81,7 +81,7 @@ func (p *Publisher[T]) start(ctx context.Context) { // Publish publishes a msg to all subscribers. // Returns ErrTimeout on timeout. -func (p *Publisher[T]) Publish(msg types.BaseEvent) error { +func (p *Broker[T]) Publish(msg types.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { return err @@ -95,12 +95,12 @@ func (p *Publisher[T]) Publish(msg types.BaseEvent) error { } } -// Subscribe registers the provided channel to the publisher, +// Subscribe registers the provided channel to the broker, // Returns ErrTimeout on timeout. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. -func (p *Publisher[T]) Subscribe(ch any) error { - client, err := ensureType[types.Subscription[T]](ch) +func (p *Broker[T]) Subscribe(ch any) error { + client, err := ensureType[chan T](ch) if err != nil { return err } @@ -110,10 +110,10 @@ func (p *Publisher[T]) Subscribe(ch any) error { return nil } -// Unsubscribe removes a client from the publisher. +// Unsubscribe removes a client from the broker. // Returns an error if the provided channel is not of type chan T. -func (p *Publisher[T]) Unsubscribe(ch any) error { - client, err := ensureType[types.Subscription[T]](ch) +func (p *Broker[T]) Unsubscribe(ch any) error { + client, err := ensureType[chan T](ch) if err != nil { return err } @@ -125,7 +125,7 @@ func (p *Publisher[T]) Unsubscribe(ch any) error { } // broadcast broadcasts a msg to all clients. -func (p *Publisher[T]) broadcast(msg T) { +func (p *Broker[T]) broadcast(msg T) { for client := range p.subscriptions { // send msg to client (or discard msg after timeout) select { @@ -136,7 +136,7 @@ func (p *Publisher[T]) broadcast(msg T) { } // shutdown closes all leftover clients. -func (p *Publisher[T]) shutdown() { +func (p *Broker[T]) shutdown() { for client := range p.subscriptions { if err := p.Unsubscribe(client); err != nil { panic(err) diff --git a/mod/async/pkg/publisher/constants.go b/mod/async/pkg/broker/constants.go similarity index 90% rename from mod/async/pkg/publisher/constants.go rename to mod/async/pkg/broker/constants.go index 05de997293..445e2821fa 100644 --- a/mod/async/pkg/publisher/constants.go +++ b/mod/async/pkg/broker/constants.go @@ -18,16 +18,16 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package publisher +package broker import "time" // TODO: make timeout configurable thorugh config/context const ( - // defaultPublisherTimeout specifies the default timeout when the publisher + // defaultBrokerTimeout specifies the default timeout when the publisher // tries to send a message to a client, a message is published to the // publisher, or a client subscribes or unsubscribes. - defaultPublisherTimeout = time.Second + defaultBrokerTimeout = time.Second // defaultBufferSize specifies the default size of the message buffer. defaultBufferSize = 10 diff --git a/mod/async/pkg/publisher/errors.go b/mod/async/pkg/broker/errors.go similarity index 98% rename from mod/async/pkg/publisher/errors.go rename to mod/async/pkg/broker/errors.go index f2c2f65c00..c93cf172c1 100644 --- a/mod/async/pkg/publisher/errors.go +++ b/mod/async/pkg/broker/errors.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package publisher +package broker import ( "github.com/berachain/beacon-kit/mod/errors" diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index b2e9b881cc..443308ba5d 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -27,60 +27,71 @@ import ( "github.com/berachain/beacon-kit/mod/log" ) -// Ensure Dispatcher implements DispatcherI. var _ types.Dispatcher = (*Dispatcher)(nil) // Dispatcher faciliates asynchronous communication between components, -// typically services. It acts as an API facade to the underlying eventserver. +// typically services. type Dispatcher struct { - eventServer EventServer - logger log.Logger[any] + brokers map[types.EventID]types.Broker + logger log.Logger[any] } -// NewDispatcher creates a new dispatcher. -func NewDispatcher( - eventServer EventServer, +// NewDispatcher creates a new event server. +func New( logger log.Logger[any], ) *Dispatcher { - eventServer.SetLogger(logger) return &Dispatcher{ - eventServer: eventServer, - logger: logger, + brokers: make(map[types.EventID]types.Broker), + logger: logger, } } -// Start starts the underlying event server. -func (d *Dispatcher) Start(ctx context.Context) error { - d.eventServer.Start(ctx) - return nil +// Publish dispatches the given event to the broker with the given eventID. +func (d *Dispatcher) PublishEvent(event types.BaseEvent) error { + broker, ok := d.brokers[event.ID()] + if !ok { + return errBrokerNotFound(event.ID()) + } + return broker.Publish(event) } -// Subscribe subscribes the given channel to the event with the given . -// It will error if the channel type does not match the event type corresponding -// to the . +// Subscribe subscribes the given channel to the broker with the given +// eventID. It will error if the channel type does not match the event type +// corresponding to the broker. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. func (d *Dispatcher) Subscribe(eventID types.EventID, ch any) error { - return d.eventServer.Subscribe(eventID, ch) -} - -// PublishEvent dispatches the given event to the event server. -// It will error if the type is inconsistent with the publisher -// registered for the given eventID. -func (d *Dispatcher) PublishEvent(event types.BaseEvent) error { - return d.eventServer.Publish(event) + broker, ok := d.brokers[eventID] + if !ok { + return errBrokerNotFound(eventID) + } + return broker.Subscribe(ch) } -// RegisterPublishers registers the given publisher with the given eventID. -// Any subsequent events with dispatched to this Dispatcher must be -// consistent with the type expected by . -func (d *Dispatcher) RegisterPublishers( - publishers ...types.Publisher, -) error { - return d.eventServer.RegisterPublishers(publishers...) +// Start will start all the brokers in the Dispatcher. +func (d *Dispatcher) Start(ctx context.Context) error { + for _, broker := range d.brokers { + go broker.Start(ctx) + } + return nil } -// Name returns the name of this service. func (d *Dispatcher) Name() string { return "dispatcher" } + +// RegisterBrokers registers the given brokers with the given eventIDs. +// Any subsequent events with dispatched to this Dispatcher must be +// consistent with the type expected by . +func (d *Dispatcher) RegisterBrokers( + brokers ...types.Broker, +) error { + var ok bool + for _, broker := range brokers { + if _, ok = d.brokers[broker.EventID()]; ok { + return errBrokerAlreadyExists(broker.EventID()) + } + d.brokers[broker.EventID()] = broker + } + return nil +} diff --git a/mod/async/pkg/server/errors.go b/mod/async/pkg/dispatcher/errors.go similarity index 84% rename from mod/async/pkg/server/errors.go rename to mod/async/pkg/dispatcher/errors.go index 7bf7a29549..86c9efb0f9 100644 --- a/mod/async/pkg/server/errors.go +++ b/mod/async/pkg/dispatcher/errors.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package server +package dispatcher import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" @@ -27,16 +27,16 @@ import ( //nolint:gochecknoglobals // errors var ( - ErrNotFound = errors.New("not found") - ErrAlreadyExists = errors.New("already exists") - errPublisherNotFound = func(eventID types.EventID) error { + ErrNotFound = errors.New("not found") + ErrAlreadyExists = errors.New("already exists") + errBrokerNotFound = func(eventID types.EventID) error { return errors.Wrapf( ErrNotFound, "publisher not found for eventID: %s", eventID, ) } - errPublisherAlreadyExists = func(eventID types.EventID) error { + errBrokerAlreadyExists = func(eventID types.EventID) error { return errors.Wrapf( ErrAlreadyExists, "publisher already exists for eventID: %s", diff --git a/mod/async/pkg/dispatcher/types.go b/mod/async/pkg/dispatcher/types.go deleted file mode 100644 index c41f780405..0000000000 --- a/mod/async/pkg/dispatcher/types.go +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package dispatcher - -import ( - "context" - - "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/log" -) - -type EventServer interface { - // Start starts the event server. - Start(ctx context.Context) - // RegisterPublishers registers the given publishers with the event server. - RegisterPublishers(publishers ...types.Publisher) error - // Subscribe subscribes the given channel to the event with the given - // . - Subscribe(mID types.EventID, ch any) error - // Publish dispatches the given event to the event server. - Publish(event types.BaseEvent) error - // SetLogger sets the logger for the event server. - SetLogger(logger log.Logger[any]) -} diff --git a/mod/async/pkg/server/event.go b/mod/async/pkg/server/event.go deleted file mode 100644 index 21feaeb118..0000000000 --- a/mod/async/pkg/server/event.go +++ /dev/null @@ -1,91 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package server - -import ( - "context" - - "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/log" -) - -// EventServer asyncronously dispatches events to subscribers. -type EventServer struct { - publishers map[types.EventID]types.Publisher - logger log.Logger[any] -} - -// NewEventServer creates a new event server. -func NewEventServer() *EventServer { - return &EventServer{ - publishers: make(map[types.EventID]types.Publisher), - } -} - -// Dispatch dispatches the given event to the publisher with the given eventID. -func (es *EventServer) Publish(event types.BaseEvent) error { - publisher, ok := es.publishers[event.ID()] - if !ok { - return errPublisherNotFound(event.ID()) - } - return publisher.Publish(event) -} - -// Subscribe subscribes the given channel to the publisher with the given -// eventID. It will error if the channel type does not match the event type -// corresponding to the publisher. -// Contract: the channel must be a Subscription[T], where T is the expected -// type of the event data. -func (es *EventServer) Subscribe(eventID types.EventID, ch any) error { - publisher, ok := es.publishers[eventID] - if !ok { - return errPublisherNotFound(eventID) - } - return publisher.Subscribe(ch) -} - -// Start starts the event server. -func (es *EventServer) Start(ctx context.Context) { - for _, publisher := range es.publishers { - go publisher.Start(ctx) - } -} - -// RegisterPublisher registers the given publisher with the given eventID. -// Any subsequent events with dispatched to this EventServer must be -// consistent with the type expected by . -func (es *EventServer) RegisterPublishers( - publishers ...types.Publisher, -) error { - var ok bool - for _, publisher := range publishers { - if _, ok = es.publishers[publisher.EventID()]; ok { - return errPublisherAlreadyExists(publisher.EventID()) - } - es.publishers[publisher.EventID()] = publisher - } - return nil -} - -// SetLogger sets the logger for the event server. -func (es *EventServer) SetLogger(logger log.Logger[any]) { - es.logger = logger -} diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index 70cfd1379f..3f69d8cd5f 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -28,8 +28,8 @@ type Dispatcher interface { EventDispatcher // Start starts the dispatcher. Start(ctx context.Context) error - // RegisterPublishers registers publishers to the dispatcher. - RegisterPublishers(publishers ...Publisher) error + // RegisterBrokers registers brokers to the dispatcher. + RegisterBrokers(brokers ...Broker) error // Name returns the name of the dispatcher. Name() string } @@ -47,7 +47,7 @@ type EventDispatcher interface { } // publisher is the interface that supports basic event publisher operations. -type Publisher interface { +type Broker interface { // Start starts the event publisher. Start(ctx context.Context) // Publish publishes the given event to the event publisher. diff --git a/mod/async/pkg/types/subscription.go b/mod/async/pkg/types/subscription.go deleted file mode 100644 index 0720babc15..0000000000 --- a/mod/async/pkg/types/subscription.go +++ /dev/null @@ -1,76 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package types - -import ( - "context" -) - -// Subscription is a channel that receives events. -type Subscription[T BaseEvent] chan T - -// NewSubscription creates a new subscription with an unbuffered channel as the -// underlying type. -func NewSubscription[T BaseEvent]() Subscription[T] { - return make(chan T) -} - -// Clear clears the subscription channel. -func (s Subscription[T]) Clear() { - for len(s) > 0 { - <-s - } -} - -// Listen will start a goroutine that will trigger the handler for each event -// in the subscription. -func (s Subscription[T]) Listen( - ctx context.Context, handler func(T), -) { - go func() { - for { - select { - case <-ctx.Done(): - return - case event := <-s: - go func(e T) { - handler(e) - }(event) - } - } - }() -} - -// Await will block until an event is received from the subscription or the -// context is canceled. -func (s Subscription[T]) Await(ctx context.Context) (T, error) { - select { - case event := <-s: - return event, nil - case <-ctx.Done(): - return *new(T), ctx.Err() - } -} - -// Chan returns the underlying channel of the subscription. -func (s Subscription[T]) Chan() chan T { - return s -} diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 9ec31e4256..e6faa367c2 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -42,7 +42,7 @@ type Service[ // store is the block store for the service. store BlockStoreT // subFinalizedBlkEvents is a channel for receiving finalized block events. - subFinalizedBlkEvents async.Subscription[async.Event[BeaconBlockT]] + subFinalizedBlkEvents chan async.Event[BeaconBlockT] } // NewService creates a new block service. @@ -60,7 +60,7 @@ func NewService[ logger: logger, dispatcher: dispatcher, store: store, - subFinalizedBlkEvents: async.NewSubscription[async.Event[BeaconBlockT]](), + subFinalizedBlkEvents: make(chan async.Event[BeaconBlockT]), } } @@ -84,11 +84,21 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { return err } - // listen for finalized block events and store the blocks in the KVStore. - s.subFinalizedBlkEvents.Listen(ctx, s.onFinalizeBlock) + go s.listen(ctx) return nil } +func (s *Service[BeaconBlockT, BlockStoreT]) listen(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case event := <-s.subFinalizedBlkEvents: + s.onFinalizeBlock(event) + } + } +} + // onFinalizeBlock is triggered when a finalized block event is received. // It stores the block in the KVStore. func (s *Service[BeaconBlockT, _]) onFinalizeBlock( diff --git a/mod/beacon/blockchain/execution_engine.go b/mod/beacon/blockchain/execution_engine.go index 56e7d34a5d..5604429c84 100644 --- a/mod/beacon/blockchain/execution_engine.go +++ b/mod/beacon/blockchain/execution_engine.go @@ -44,7 +44,7 @@ func (s *Service[ return } - if !s.shouldBuildOptimisticPayloads() && s.localBuilder.Enabled() { + if !s.shouldBuildOptimisticPayloads() && s.lb.Enabled() { s.sendNextFCUWithAttributes(ctx, st, blk, lph) } else { s.sendNextFCUWithoutAttributes(ctx, blk, lph) @@ -65,7 +65,7 @@ func (s *Service[ lph ExecutionPayloadHeaderT, ) { stCopy := st.Copy() - if _, err := s.stateProcessor.ProcessSlots(stCopy, blk.GetSlot()+1); err != nil { + if _, err := s.sp.ProcessSlots(stCopy, blk.GetSlot()+1); err != nil { s.logger.Error( "failed to process slots in non-optimistic payload", "error", err, @@ -74,7 +74,7 @@ func (s *Service[ } prevBlockRoot := blk.HashTreeRoot() - if _, err := s.localBuilder.RequestPayloadAsync( + if _, err := s.lb.RequestPayloadAsync( ctx, stCopy, blk.GetSlot()+1, @@ -100,7 +100,7 @@ func (s *Service[ blk BeaconBlockT, lph ExecutionPayloadHeaderT, ) { - if _, _, err := s.executionEngine.NotifyForkchoiceUpdate( + if _, _, err := s.ee.NotifyForkchoiceUpdate( ctx, // TODO: Switch to New(). engineprimitives. @@ -110,7 +110,7 @@ func (s *Service[ SafeBlockHash: lph.GetParentHash(), FinalizedBlockHash: lph.GetParentHash(), }, - s.chainSpec.ActiveForkVersionForSlot(blk.GetSlot()), + s.cs.ActiveForkVersionForSlot(blk.GetSlot()), ), ); err != nil { s.logger.Error( @@ -129,7 +129,7 @@ func (s *Service[ ]) calculateNextTimestamp(blk BeaconBlockT) uint64 { //#nosec:G701 // not an issue in practice. return max( - uint64(time.Now().Unix()+int64(s.chainSpec.TargetSecondsPerEth1Block())), + uint64(time.Now().Unix()+int64(s.cs.TargetSecondsPerEth1Block())), uint64(blk.GetBody().GetExecutionPayload().GetTimestamp()+1), ) } diff --git a/mod/beacon/blockchain/payload.go b/mod/beacon/blockchain/payload.go index 49d82e0209..ba000a8c3d 100644 --- a/mod/beacon/blockchain/payload.go +++ b/mod/beacon/blockchain/payload.go @@ -46,7 +46,7 @@ func (s *Service[ // TODO: Verify if the slot number is correct here, I believe in current // form // it should be +1'd. Not a big deal until hardforks are in play though. - if err = s.localBuilder.SendForceHeadFCU(ctx, st, slot+1); err != nil { + if err = s.lb.SendForceHeadFCU(ctx, st, slot+1); err != nil { s.logger.Error( "failed to send force head FCU", "error", err, @@ -117,7 +117,7 @@ func (s *Service[ } // Submit a request for a new payload. - if _, err = s.localBuilder.RequestPayloadAsync( + if _, err = s.lb.RequestPayloadAsync( ctx, st, // We are rebuilding for the current slot. @@ -180,7 +180,7 @@ func (s *Service[ ) // We process the slot to update any RANDAO values. - if _, err := s.stateProcessor.ProcessSlots( + if _, err := s.sp.ProcessSlots( st, slot, ); err != nil { return err @@ -188,13 +188,13 @@ func (s *Service[ // We then trigger a request for the next payload. payload := blk.GetBody().GetExecutionPayload() - if _, err := s.localBuilder.RequestPayloadAsync( + if _, err := s.lb.RequestPayloadAsync( ctx, st, slot, // TODO: this is hood as fuck. max( //#nosec:G701 - uint64(time.Now().Unix()+int64(s.chainSpec.TargetSecondsPerEth1Block())), + uint64(time.Now().Unix()+int64(s.cs.TargetSecondsPerEth1Block())), uint64((payload.GetTimestamp()+1)), ), // The previous block root is simply the root of the block we just diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 09cac32f9f..5f3638d7ec 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -37,7 +37,7 @@ func (s *Service[ ctx context.Context, genesisData GenesisT, ) (transition.ValidatorUpdates, error) { - return s.stateProcessor.InitializePreminedBeaconStateFromEth1( + return s.sp.InitializePreminedBeaconStateFromEth1( s.sb.StateFromContext(ctx), genesisData.GetDeposits(), genesisData.GetExecutionPayloadHeader(), @@ -106,7 +106,7 @@ func (s *Service[ ) (transition.ValidatorUpdates, error) { startTime := time.Now() defer s.metrics.measureStateTransitionDuration(startTime) - valUpdates, err := s.stateProcessor.Transition( + valUpdates, err := s.sp.Transition( &transition.Context{ Context: ctx, OptimisticEngine: true, diff --git a/mod/beacon/blockchain/receive.go b/mod/beacon/blockchain/receive.go index 49613326dd..79d78c9775 100644 --- a/mod/beacon/blockchain/receive.go +++ b/mod/beacon/blockchain/receive.go @@ -118,7 +118,7 @@ func (s *Service[ ) error { startTime := time.Now() defer s.metrics.measureStateRootVerificationTime(startTime) - if _, err := s.stateProcessor.Transition( + if _, err := s.sp.Transition( // We run with a non-optimistic engine here to ensure // that the proposer does not try to push through a bad block. &transition.Context{ @@ -148,5 +148,5 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, _, _, _, ]) shouldBuildOptimisticPayloads() bool { - return s.optimisticPayloadBuilds && s.localBuilder.Enabled() + return s.optimisticPayloadBuilds && s.lb.Enabled() } diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 07fc0a14cd..713ed0c611 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -60,17 +60,17 @@ type Service[ ] // logger is used for logging messages in the service. logger log.Logger[any] - // chainSpec holds the chain specifications. - chainSpec common.ChainSpec + // cs holds the chain specifications. + cs common.ChainSpec // dispatcher is the dispatcher for the service. dispatcher async.Dispatcher - // executionEngine is the execution engine responsible for processing + // ee is the execution engine responsible for processing // execution payloads. - executionEngine ExecutionEngine[PayloadAttributesT] - // localBuilder is a local builder for constructing new beacon states. - localBuilder LocalBuilder[BeaconStateT] - // stateProcessor is the state processor for beacon blocks and states. - stateProcessor StateProcessor[ + ee ExecutionEngine[PayloadAttributesT] + // lb is a local builder for constructing new beacon states. + lb LocalBuilder[BeaconStateT] + // sp is the state processor for beacon blocks and states. + sp StateProcessor[ BeaconBlockT, BeaconStateT, *transition.Context, @@ -87,12 +87,12 @@ type Service[ // subFinalBlkReceived is a channel for receiving finalize beacon block // requests. - subFinalBlkReceived async.Subscription[async.Event[BeaconBlockT]] + subFinalBlkReceived chan async.Event[BeaconBlockT] // subBlockReceived is a channel for receiving verify beacon block requests. - subBlockReceived async.Subscription[async.Event[BeaconBlockT]] + subBlockReceived chan async.Event[BeaconBlockT] // subGenDataReceived is a subscription for receiving genesis data // received events. - subGenDataReceived async.Subscription[async.Event[GenesisT]] + subGenDataReceived chan async.Event[GenesisT] } // NewService creates a new validator service. @@ -147,17 +147,17 @@ func NewService[ ]{ sb: sb, logger: logger, - chainSpec: chainSpec, + cs: chainSpec, dispatcher: dispatcher, - executionEngine: executionEngine, - localBuilder: localBuilder, - stateProcessor: stateProcessor, + ee: executionEngine, + lb: localBuilder, + sp: stateProcessor, metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), - subFinalBlkReceived: async.NewSubscription[async.Event[BeaconBlockT]](), - subBlockReceived: async.NewSubscription[async.Event[BeaconBlockT]](), - subGenDataReceived: async.NewSubscription[async.Event[GenesisT]](), + subFinalBlkReceived: make(chan async.Event[BeaconBlockT]), + subBlockReceived: make(chan async.Event[BeaconBlockT]), + subGenDataReceived: make(chan async.Event[GenesisT]), } } @@ -193,7 +193,7 @@ func (s *Service[ } // start a goroutine to listen for requests and handle accordingly - s.listen(ctx) + go s.listen(ctx) return nil } @@ -201,9 +201,18 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, ]) listen(ctx context.Context) { - s.subGenDataReceived.Listen(ctx, s.handleProcessGenesisDataEvent) - s.subBlockReceived.Listen(ctx, s.handleVerifyBeaconBlockEvent) - s.subFinalBlkReceived.Listen(ctx, s.handleFinalizeBeaconBlockEvent) + for { + select { + case <-ctx.Done(): + return + case event := <-s.subGenDataReceived: + s.handleProcessGenesisDataEvent(event) + case event := <-s.subBlockReceived: + s.handleVerifyBeaconBlockEvent(event) + case event := <-s.subFinalBlkReceived: + s.handleFinalizeBeaconBlockEvent(event) + } + } } /* -------------------------------------------------------------------------- */ diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 48c119e418..1f49fee22f 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -89,7 +89,7 @@ type Service[ // metrics is a metrics collector. metrics *validatorMetrics // subNewSlot is a channel for new slot events. - subNewSlot async.Subscription[async.Event[SlotDataT]] + subNewSlot chan async.Event[SlotDataT] } // NewService creates a new validator service. @@ -156,7 +156,7 @@ func NewService[ remotePayloadBuilders: remotePayloadBuilders, metrics: newValidatorMetrics(ts), dispatcher: dispatcher, - subNewSlot: async.NewSubscription[async.Event[SlotDataT]](), + subNewSlot: make(chan async.Event[SlotDataT]), } } @@ -180,10 +180,23 @@ func (s *Service[ return err } // set the handler for new slot events - s.subNewSlot.Listen(ctx, s.handleNewSlot) + go s.listen(ctx) return nil } +func (s *Service[_, _, _, _, _, _, _, _, _, _, _, _, SlotDataT]) listen( + ctx context.Context, +) { + for { + select { + case <-ctx.Done(): + return + case event := <-s.subNewSlot: + s.handleNewSlot(event) + } + } +} + // handleNewSlot builds a block and sidecars for the requested // slot data and emits an event containing the built block and sidecars. func (s *Service[ diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 08bc700179..1b057854da 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -46,8 +46,8 @@ type Service[ ] dispatcher async.EventDispatcher logger log.Logger[any] - subSidecarsReceived async.Subscription[async.Event[BlobSidecarsT]] - subFinalBlobSidecars async.Subscription[async.Event[BlobSidecarsT]] + subSidecarsReceived chan async.Event[BlobSidecarsT] + subFinalBlobSidecars chan async.Event[BlobSidecarsT] } // NewService returns a new DA service. @@ -79,8 +79,8 @@ func NewService[ bp: bp, dispatcher: dispatcher, logger: logger, - subSidecarsReceived: async.NewSubscription[async.Event[BlobSidecarsT]](), - subFinalBlobSidecars: async.NewSubscription[async.Event[BlobSidecarsT]](), + subSidecarsReceived: make(chan async.Event[BlobSidecarsT]), + subFinalBlobSidecars: make(chan async.Event[BlobSidecarsT]), } } @@ -109,11 +109,23 @@ func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { } // listen for events and handle accordingly - s.subSidecarsReceived.Listen(ctx, s.handleSidecarsVerifyRequest) - s.subFinalBlobSidecars.Listen(ctx, s.handleBlobSidecarsProcessRequest) + go s.listen(ctx) return nil } +func (s *Service[_, _, BlobSidecarsT, _]) listen(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case event := <-s.subSidecarsReceived: + s.handleSidecarsVerifyRequest(event) + case event := <-s.subFinalBlobSidecars: + s.handleBlobSidecarsProcessRequest(event) + } + } +} + /* -------------------------------------------------------------------------- */ /* Event Handlers */ /* -------------------------------------------------------------------------- */ diff --git a/mod/engine-primitives/pkg/engine-primitives/mocks/blobs_bundle.mock.go b/mod/engine-primitives/pkg/engine-primitives/mocks/blobs_bundle.mock.go index 5c48437385..643f38b5ea 100644 --- a/mod/engine-primitives/pkg/engine-primitives/mocks/blobs_bundle.mock.go +++ b/mod/engine-primitives/pkg/engine-primitives/mocks/blobs_bundle.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/engine-primitives/pkg/engine-primitives/mocks/built_execution_payload_env.mock.go b/mod/engine-primitives/pkg/engine-primitives/mocks/built_execution_payload_env.mock.go index ddc576a370..06cb850b26 100644 --- a/mod/engine-primitives/pkg/engine-primitives/mocks/built_execution_payload_env.mock.go +++ b/mod/engine-primitives/pkg/engine-primitives/mocks/built_execution_payload_env.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/engine-primitives/pkg/engine-primitives/mocks/payload_attributer.mock.go b/mod/engine-primitives/pkg/engine-primitives/mocks/payload_attributer.mock.go index a86b639042..2a4c2099f7 100644 --- a/mod/engine-primitives/pkg/engine-primitives/mocks/payload_attributer.mock.go +++ b/mod/engine-primitives/pkg/engine-primitives/mocks/payload_attributer.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 167300edb0..2722d84a07 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -52,7 +52,7 @@ type Service[ // finalizedBlockEventID is the event ID for the finalized block event. finalizedBlockEventID async.EventID // subFinalizedBlockEvents is the channel that provides finalized block events. - subFinalizedBlockEvents async.Subscription[BlockEventT] + subFinalizedBlockEvents chan BlockEventT // metrics is the metrics for the deposit service. metrics *metrics // failedBlocks is a map of blocks that failed to be processed to be @@ -95,7 +95,7 @@ func NewService[ eth1FollowDistance: eth1FollowDistance, failedBlocks: make(map[math.Slot]struct{}), finalizedBlockEventID: finalizedBlockEventID, - subFinalizedBlockEvents: async.NewSubscription[BlockEventT](), + subFinalizedBlockEvents: make(chan BlockEventT), logger: logger, metrics: newMetrics(telemetrySink), } @@ -114,13 +114,26 @@ func (s *Service[ } // Listen for finalized block events and fetch deposits for the block. - s.subFinalizedBlockEvents.Listen(ctx, s.getDepositFetcher(ctx)) + go s.listen(ctx) // Catchup deposits for failed blocks. go s.depositCatchupFetcher(ctx) return nil } +func (s *Service[ + _, _, BlockEventT, _, _, _, +]) listen(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case event := <-s.subFinalizedBlockEvents: + s.depositFetcher(ctx, event) + } + } +} + // Name returns the name of the service. func (s *Service[ _, _, _, _, _, _, diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index 9a081e6bf5..4fa4b0b9a8 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -30,15 +30,13 @@ import ( // defaultRetryInterval processes a deposit event. const defaultRetryInterval = 20 * time.Second -// getDepositFetcher returns a function that retrieves the block number from the +// depositFetcher returns a function that retrieves the block number from the // event and fetches and stores the deposits for that block. func (s *Service[ _, _, BlockEventT, _, _, _, -]) getDepositFetcher(ctx context.Context) func(event BlockEventT) { - return func(event BlockEventT) { - blockNum := event.Data().GetBody().GetExecutionPayload().GetNumber() - s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) - } +]) depositFetcher(ctx context.Context, event BlockEventT) { + blockNum := event.Data().GetBody().GetExecutionPayload().GetNumber() + s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) } // depositCatchupFetcher fetches deposits for blocks that failed to be diff --git a/mod/node-api/backend/mocks/availability_store.mock.go b/mod/node-api/backend/mocks/availability_store.mock.go index f3dbec242d..82771d4eeb 100644 --- a/mod/node-api/backend/mocks/availability_store.mock.go +++ b/mod/node-api/backend/mocks/availability_store.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/beacon_block_header.mock.go b/mod/node-api/backend/mocks/beacon_block_header.mock.go index 770dfb0300..71cc794175 100644 --- a/mod/node-api/backend/mocks/beacon_block_header.mock.go +++ b/mod/node-api/backend/mocks/beacon_block_header.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/beacon_state.mock.go b/mod/node-api/backend/mocks/beacon_state.mock.go index a785681175..eeab5dbbc5 100644 --- a/mod/node-api/backend/mocks/beacon_state.mock.go +++ b/mod/node-api/backend/mocks/beacon_state.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/block_store.mock.go b/mod/node-api/backend/mocks/block_store.mock.go index 104e7570a3..c9cdae9a04 100644 --- a/mod/node-api/backend/mocks/block_store.mock.go +++ b/mod/node-api/backend/mocks/block_store.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/deposit_store.mock.go b/mod/node-api/backend/mocks/deposit_store.mock.go index da5be359a8..f1a67dac17 100644 --- a/mod/node-api/backend/mocks/deposit_store.mock.go +++ b/mod/node-api/backend/mocks/deposit_store.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/node.mock.go b/mod/node-api/backend/mocks/node.mock.go index 9bcf49e752..8b13f2787c 100644 --- a/mod/node-api/backend/mocks/node.mock.go +++ b/mod/node-api/backend/mocks/node.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/state_processor.mock.go b/mod/node-api/backend/mocks/state_processor.mock.go index b25569a298..55800177e0 100644 --- a/mod/node-api/backend/mocks/state_processor.mock.go +++ b/mod/node-api/backend/mocks/state_processor.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/storage_backend.mock.go b/mod/node-api/backend/mocks/storage_backend.mock.go index 8b1f802a35..1d74a05ea4 100644 --- a/mod/node-api/backend/mocks/storage_backend.mock.go +++ b/mod/node-api/backend/mocks/storage_backend.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/validator.mock.go b/mod/node-api/backend/mocks/validator.mock.go index ddbafeb79e..3f583ba403 100644 --- a/mod/node-api/backend/mocks/validator.mock.go +++ b/mod/node-api/backend/mocks/validator.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/withdrawal.mock.go b/mod/node-api/backend/mocks/withdrawal.mock.go index ac66e7554c..59a2e187ad 100644 --- a/mod/node-api/backend/mocks/withdrawal.mock.go +++ b/mod/node-api/backend/mocks/withdrawal.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-api/backend/mocks/withdrawal_credentials.mock.go b/mod/node-api/backend/mocks/withdrawal_credentials.mock.go index de251b671a..c89c289782 100644 --- a/mod/node-api/backend/mocks/withdrawal_credentials.mock.go +++ b/mod/node-api/backend/mocks/withdrawal_credentials.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index b535bb4104..ae6353f20f 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -25,7 +25,6 @@ import ( "os" "cosmossdk.io/depinject" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" dastore "github.com/berachain/beacon-kit/mod/da/pkg/store" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" @@ -101,7 +100,7 @@ func ProvideAvailabilityPruner[ // TODO: add dispatcher field in the pruner or something, the provider // should not execute any business logic. // create new subscription for finalized blocks. - subFinalizedBlocks := async.NewSubscription[FinalizedBlockEvent]() + subFinalizedBlocks := make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( events.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 2e5e065ff4..2439547a88 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -24,7 +24,6 @@ import ( "cosmossdk.io/depinject" storev2 "cosmossdk.io/store/v2/db" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" blockservice "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/log" @@ -89,7 +88,7 @@ func ProvideBlockPruner[ ) (BlockPruner, error) { // TODO: provider should not execute any business logic. // create new subscription for finalized blocks. - subFinalizedBlocks := async.NewSubscription[FinalizedBlockEvent]() + subFinalizedBlocks := make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( events.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { diff --git a/mod/node-core/pkg/components/defaults.go b/mod/node-core/pkg/components/defaults.go index 421afa1b71..ff631738da 100644 --- a/mod/node-core/pkg/components/defaults.go +++ b/mod/node-core/pkg/components/defaults.go @@ -65,10 +65,8 @@ func DefaultComponents() []any { // TODO Hacks ProvideKVStoreService, ProvideKVStoreKey, - // loggerT im gonna kms ProvideDispatcher[LoggerT], ProvidePublishers, - ProvideEventServer, } components = append(components, ProvideNodeAPIServer[LoggerT], diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index a71224c09d..90c79e094a 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -23,7 +23,6 @@ package components import ( "cosmossdk.io/depinject" storev2 "cosmossdk.io/store/v2/db" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/execution/pkg/deposit" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" @@ -76,7 +75,7 @@ func ProvideDepositPruner[ in DepositPrunerInput[LoggerT], ) (DepositPruner, error) { // initialize a subscription for finalized blocks. - subFinalizedBlocks := async.NewSubscription[FinalizedBlockEvent]() + subFinalizedBlocks := make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( events.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { diff --git a/mod/node-core/pkg/components/dispatcher.go b/mod/node-core/pkg/components/dispatcher.go index 6c5e5250eb..8de12189cf 100644 --- a/mod/node-core/pkg/components/dispatcher.go +++ b/mod/node-core/pkg/components/dispatcher.go @@ -32,9 +32,8 @@ type DispatcherInput[ LoggerT log.AdvancedLogger[any, LoggerT], ] struct { depinject.In - EventServer *EventServer - Logger LoggerT - Publishers []asynctypes.Publisher + Logger LoggerT + Publishers []asynctypes.Broker } // ProvideDispatcher provides a new Dispatcher. @@ -43,22 +42,11 @@ func ProvideDispatcher[ ]( in DispatcherInput[LoggerT], ) (*Dispatcher, error) { - d := dispatcher.NewDispatcher( - in.EventServer, + d := dispatcher.New( in.Logger.With("service", "dispatcher"), ) - if err := d.RegisterPublishers(in.Publishers...); err != nil { + if err := d.RegisterBrokers(in.Publishers...); err != nil { return nil, err } return d, nil } - -func DefaultDispatcherComponents[ - LoggerT log.AdvancedLogger[any, LoggerT], -]() []any { - return []any{ - ProvideDispatcher[LoggerT], - ProvidePublishers, - ProvideEventServer, - } -} diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 156a1fb4a0..689da5f459 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -21,58 +21,52 @@ package components import ( - "github.com/berachain/beacon-kit/mod/async/pkg/publisher" - "github.com/berachain/beacon-kit/mod/async/pkg/server" + "github.com/berachain/beacon-kit/mod/async/pkg/broker" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) -// ProvideEventServer provides an event server. -func ProvideEventServer() *EventServer { - return server.NewEventServer() -} - // ProvidePublishers provides a publisher for beacon block // finalized events. -func ProvidePublishers() []asynctypes.Publisher { - return []asynctypes.Publisher{ - publisher.New[GenesisDataReceivedEvent]( +func ProvidePublishers() []asynctypes.Broker { + return []asynctypes.Broker{ + broker.New[GenesisDataReceivedEvent]( events.GenesisDataReceived, ), - publisher.New[GenesisDataProcessedEvent]( + broker.New[GenesisDataProcessedEvent]( events.GenesisDataProcessed, ), - publisher.New[NewSlotEvent]( + broker.New[NewSlotEvent]( events.NewSlot, ), - publisher.New[BuiltBeaconBlockEvent]( + broker.New[BuiltBeaconBlockEvent]( events.BuiltBeaconBlock, ), - publisher.New[BuiltSidecarsEvent]( + broker.New[BuiltSidecarsEvent]( events.BuiltSidecars, ), - publisher.New[BeaconBlockReceivedEvent]( + broker.New[BeaconBlockReceivedEvent]( events.BeaconBlockReceived, ), - publisher.New[SidecarsReceivedEvent]( + broker.New[SidecarsReceivedEvent]( events.SidecarsReceived, ), - publisher.New[BeaconBlockVerifiedEvent]( + broker.New[BeaconBlockVerifiedEvent]( events.BeaconBlockVerified, ), - publisher.New[SidecarsVerifiedEvent]( + broker.New[SidecarsVerifiedEvent]( events.SidecarsVerified, ), - publisher.New[FinalBeaconBlockReceivedEvent]( + broker.New[FinalBeaconBlockReceivedEvent]( events.FinalBeaconBlockReceived, ), - publisher.New[FinalSidecarsReceivedEvent]( + broker.New[FinalSidecarsReceivedEvent]( events.FinalSidecarsReceived, ), - publisher.New[FinalValidatorUpdatesProcessedEvent]( + broker.New[FinalValidatorUpdatesProcessedEvent]( events.FinalValidatorUpdatesProcessed, ), - publisher.New[FinalizedBlockEvent]( + broker.New[FinalizedBlockEvent]( events.BeaconBlockFinalizedEvent, ), } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index b5d35e6dbc..3fa5ddf121 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -22,9 +22,8 @@ package components import ( "cosmossdk.io/core/appmodule/v2" + "github.com/berachain/beacon-kit/mod/async/pkg/broker" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - "github.com/berachain/beacon-kit/mod/async/pkg/publisher" - asyncserver "github.com/berachain/beacon-kit/mod/async/pkg/server" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" blockstore "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/beacon/blockchain" @@ -497,7 +496,7 @@ type ( /* -------------------------------------------------------------------------- */ type ( - BeaconBlockFinalizedPublisher = publisher.Publisher[FinalizedBlockEvent] + BeaconBlockFinalizedPublisher = broker.Broker[FinalizedBlockEvent] ) /* -------------------------------------------------------------------------- */ @@ -507,9 +506,6 @@ type ( type ( // Dispatcher is a type alias for the dispatcher. Dispatcher = dispatcher.Dispatcher - - // EventServer is a type alias for the event server. - EventServer = asyncserver.EventServer ) /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/crypto/mocks/bls_signer.mock.go b/mod/primitives/pkg/crypto/mocks/bls_signer.mock.go index 9fb73fde2a..a0bd7b8e42 100644 --- a/mod/primitives/pkg/crypto/mocks/bls_signer.mock.go +++ b/mod/primitives/pkg/crypto/mocks/bls_signer.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 766e22bf31..e925205ac8 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -47,13 +47,12 @@ func (h *ABCIMiddleware[ bz []byte, ) (transition.ValidatorUpdates, error) { var ( - err error - gdpEvent async.Event[transition.ValidatorUpdates] - awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) + err error + waitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) defer cancel() - // in theory this channel should already be empty, but we clear it anyways - h.subGenDataProcessed.Clear() + // TODO: in theory the GenesisDataReceived channel should be empty, but we + // should clear it anyways here to ensure that data is valid. data := new(GenesisT) if err = json.Unmarshal(bz, data); err != nil { @@ -66,13 +65,22 @@ func (h *ABCIMiddleware[ ); err != nil { return nil, err } + return h.waitForGenesisProcessed(waitCtx) +} - gdpEvent, err = h.subGenDataProcessed.Await(awaitCtx) - if err != nil { - return nil, err +// waitForGenesisProcessed waits until the genesis data has been processed and +// returns the validator updates, or err if the context is cancelled. +func (h *ABCIMiddleware[ + _, _, _, _, _, _, _, _, +]) waitForGenesisProcessed( + ctx context.Context, +) (transition.ValidatorUpdates, error) { + select { + case <-ctx.Done(): + return nil, ctx.Err() + case gdpEvent := <-h.subGenDataProcessed: + return gdpEvent.Data(), gdpEvent.Error() } - - return gdpEvent.Data(), gdpEvent.Error() } /* -------------------------------------------------------------------------- */ @@ -88,16 +96,15 @@ func (h *ABCIMiddleware[ ) ([]byte, []byte, error) { var ( err error - builtBBEvent async.Event[BeaconBlockT] - builtSCEvent async.Event[BlobSidecarsT] + builtBeaconBlock BeaconBlockT + builtSidecars BlobSidecarsT startTime = time.Now() awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) + // TODO: clear the built beacon block and sidecars channels, else we may + // end up handling old data from previous slots. defer cancel() defer h.metrics.measurePrepareProposalDuration(startTime) - // in theory these subs should already be empty, but we clear them anyways - h.subBuiltBeaconBlock.Clear() - h.subBuiltSidecars.Clear() if err = h.dispatcher.PublishEvent( async.NewEvent( @@ -108,29 +115,51 @@ func (h *ABCIMiddleware[ } // wait for built beacon block - builtBBEvent, err = h.subBuiltBeaconBlock.Await(awaitCtx) + builtBeaconBlock, err = h.waitForBuiltBeaconBlock(awaitCtx) if err != nil { return nil, nil, err } - if err = builtBBEvent.Error(); err != nil { - return nil, nil, err - } // wait for built sidecars - builtSCEvent, err = h.subBuiltSidecars.Await(awaitCtx) + builtSidecars, err = h.waitForBuiltSidecars(awaitCtx) if err != nil { return nil, nil, err } - if err = builtSCEvent.Error(); err != nil { - return nil, nil, err - } // gossip the built beacon block and blob sidecars return h.handleBuiltBeaconBlockAndSidecars( - ctx, builtBBEvent.Data(), builtSCEvent.Data(), + ctx, builtBeaconBlock, builtSidecars, ) } +// waitForBuiltBeaconBlock waits for the built beacon block to be received. +func (h *ABCIMiddleware[ + _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, SlotDataT, +]) waitForBuiltBeaconBlock( + ctx context.Context, +) (BeaconBlockT, error) { + select { + case <-ctx.Done(): + return *new(BeaconBlockT), ctx.Err() + case bbEvent := <-h.subBuiltBeaconBlock: + return bbEvent.Data(), bbEvent.Error() + } +} + +// waitForBuiltSidecars waits for the built sidecars to be received. +func (h *ABCIMiddleware[ + _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, SlotDataT, +]) waitForBuiltSidecars( + ctx context.Context, +) (BlobSidecarsT, error) { + select { + case <-ctx.Done(): + return *new(BlobSidecarsT), ctx.Err() + case scEvent := <-h.subBuiltSidecars: + return scEvent.Data(), scEvent.Error() + } +} + // handleBeaconBlockBundleResponse gossips the built beacon block and blob // sidecars to the network. func (h *ABCIMiddleware[ @@ -175,9 +204,6 @@ func (h *ABCIMiddleware[ awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) defer cancel() - // in theory these subs should already be empty, probably redundant - h.subBBVerified.Clear() - h.subSCVerified.Clear() abciReq, ok := req.(*cmtabci.ProcessProposalRequest) if !ok { return nil, ErrInvalidProcessProposalRequestType @@ -210,17 +236,46 @@ func (h *ABCIMiddleware[ } // err if the built beacon block or sidecars failed verification. - _, err = h.subBBVerified.Await(awaitCtx) + _, err = h.waitForBeaconBlockVerification(awaitCtx) if err != nil { return h.createProcessProposalResponse(err) } - _, err = h.subSCVerified.Await(awaitCtx) + _, err = h.waitForSidecarVerification(awaitCtx) if err != nil { return h.createProcessProposalResponse(err) } return h.createProcessProposalResponse(nil) } +// waitForBeaconBlockVerification waits for the built beacon block to be +// verified. +func (h *ABCIMiddleware[ + _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, +]) waitForBeaconBlockVerification( + ctx context.Context, +) (BeaconBlockT, error) { + select { + case <-ctx.Done(): + return *new(BeaconBlockT), ctx.Err() + case vEvent := <-h.subBBVerified: + return vEvent.Data(), vEvent.Error() + } +} + +// waitForSidecarVerification waits for the built sidecars to be verified. +func (h *ABCIMiddleware[ + _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, +]) waitForSidecarVerification( + ctx context.Context, +) (BlobSidecarsT, error) { + select { + case <-ctx.Done(): + return *new(BlobSidecarsT), ctx.Err() + case vEvent := <-h.subSCVerified: + return vEvent.Data(), vEvent.Error() + } +} + // createResponse generates the appropriate ProcessProposalResponse based on the // error. func (*ABCIMiddleware[ @@ -245,15 +300,13 @@ func (h *ABCIMiddleware[ ctx context.Context, req proto.Message, ) (transition.ValidatorUpdates, error) { var ( - err error - blk BeaconBlockT - blobs BlobSidecarsT - finalValUpdatesEvent async.Event[transition.ValidatorUpdates] - awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) + err error + blk BeaconBlockT + blobs BlobSidecarsT + finalValUpdates transition.ValidatorUpdates + awaitCtx, cancel = context.WithTimeout(ctx, AwaitTimeout) ) defer cancel() - // in theory this sub should already be empty, but we clear them anyways - h.subFinalValidatorUpdates.Clear() abciReq, ok := req.(*cmtabci.FinalizeBlockRequest) if !ok { return nil, ErrInvalidFinalizeBlockRequestType @@ -286,10 +339,25 @@ func (h *ABCIMiddleware[ } // wait for the final validator updates. - finalValUpdatesEvent, err = h.subFinalValidatorUpdates.Await(awaitCtx) + finalValUpdates, err = h.waitForFinalValidatorUpdates(awaitCtx) if err != nil { return nil, err } - return finalValUpdatesEvent.Data(), finalValUpdatesEvent.Error() + return finalValUpdates, nil +} + +// waitForFinalValidatorUpdates waits for the final validator updates to be +// received. +func (h *ABCIMiddleware[ + _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, +]) waitForFinalValidatorUpdates( + ctx context.Context, +) (transition.ValidatorUpdates, error) { + select { + case <-ctx.Done(): + return nil, ctx.Err() + case event := <-h.subFinalValidatorUpdates: + return event.Data(), event.Error() + } } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 9d2d86f0b1..5368034966 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -72,12 +72,12 @@ type ABCIMiddleware[ // logger is the logger for the middleware. logger log.Logger[any] // subscription channels - subGenDataProcessed types.Subscription[types.Event[validatorUpdates]] - subBuiltBeaconBlock types.Subscription[types.Event[BeaconBlockT]] - subBuiltSidecars types.Subscription[types.Event[BlobSidecarsT]] - subBBVerified types.Subscription[types.Event[BeaconBlockT]] - subSCVerified types.Subscription[types.Event[BlobSidecarsT]] - subFinalValidatorUpdates types.Subscription[types.Event[validatorUpdates]] + subGenDataProcessed chan types.Event[validatorUpdates] + subBuiltBeaconBlock chan types.Event[BeaconBlockT] + subBuiltSidecars chan types.Event[BlobSidecarsT] + subBBVerified chan types.Event[BeaconBlockT] + subSCVerified chan types.Event[BlobSidecarsT] + subFinalValidatorUpdates chan types.Event[validatorUpdates] } // NewABCIMiddleware creates a new instance of the Handler struct. @@ -121,12 +121,12 @@ func NewABCIMiddleware[ logger: logger, metrics: newABCIMiddlewareMetrics(telemetrySink), dispatcher: dispatcher, - subGenDataProcessed: types.NewSubscription[types.Event[validatorUpdates]](), - subBuiltBeaconBlock: types.NewSubscription[types.Event[BeaconBlockT]](), - subBuiltSidecars: types.NewSubscription[types.Event[BlobSidecarsT]](), - subBBVerified: types.NewSubscription[types.Event[BeaconBlockT]](), - subSCVerified: types.NewSubscription[types.Event[BlobSidecarsT]](), - subFinalValidatorUpdates: types.NewSubscription[types.Event[validatorUpdates]](), + subGenDataProcessed: make(chan types.Event[validatorUpdates]), + subBuiltBeaconBlock: make(chan types.Event[BeaconBlockT]), + subBuiltSidecars: make(chan types.Event[BlobSidecarsT]), + subBBVerified: make(chan types.Event[BeaconBlockT]), + subSCVerified: make(chan types.Event[BlobSidecarsT]), + subFinalValidatorUpdates: make(chan types.Event[validatorUpdates]), } } diff --git a/mod/runtime/pkg/service/mocks/basic.mock.go b/mod/runtime/pkg/service/mocks/basic.mock.go index 3b38040653..b5a166a800 100644 --- a/mod/runtime/pkg/service/mocks/basic.mock.go +++ b/mod/runtime/pkg/service/mocks/basic.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/runtime/pkg/service/mocks/dispatcher.mock.go b/mod/runtime/pkg/service/mocks/dispatcher.mock.go index 992d03e4f0..4302b03893 100644 --- a/mod/runtime/pkg/service/mocks/dispatcher.mock.go +++ b/mod/runtime/pkg/service/mocks/dispatcher.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/runtime/pkg/service/mocks/registry_option.mock.go b/mod/runtime/pkg/service/mocks/registry_option.mock.go index 530e656459..7642016d34 100644 --- a/mod/runtime/pkg/service/mocks/registry_option.mock.go +++ b/mod/runtime/pkg/service/mocks/registry_option.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/storage/pkg/interfaces/mocks/db.mock.go b/mod/storage/pkg/interfaces/mocks/db.mock.go index 7ffc71a9e1..e18d61a317 100644 --- a/mod/storage/pkg/interfaces/mocks/db.mock.go +++ b/mod/storage/pkg/interfaces/mocks/db.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/storage/pkg/manager/manager_test.go b/mod/storage/pkg/manager/manager_test.go index f6f6f75a02..ab2672a122 100644 --- a/mod/storage/pkg/manager/manager_test.go +++ b/mod/storage/pkg/manager/manager_test.go @@ -31,7 +31,6 @@ import ( "time" "cosmossdk.io/log" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -40,7 +39,7 @@ import ( func TestDBManager_Start(t *testing.T) { mockPrunable := new(mocks.Prunable) - ch := async.NewSubscription[manager.BlockEvent[manager.BeaconBlock]]() + ch := make(chan manager.BlockEvent[manager.BeaconBlock]) pruneParamsFn := func(_ manager.BlockEvent[manager.BeaconBlock]) (uint64, uint64) { return 0, 0 diff --git a/mod/storage/pkg/pruner/mocks/beacon_block.mock.go b/mod/storage/pkg/pruner/mocks/beacon_block.mock.go index 14c7a66d22..a4719bb56b 100644 --- a/mod/storage/pkg/pruner/mocks/beacon_block.mock.go +++ b/mod/storage/pkg/pruner/mocks/beacon_block.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index 5a23af48d4..fed37040a6 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/storage/pkg/pruner/mocks/prunable.mock.go b/mod/storage/pkg/pruner/mocks/prunable.mock.go index 5958e29fc4..41d0283c75 100644 --- a/mod/storage/pkg/pruner/mocks/prunable.mock.go +++ b/mod/storage/pkg/pruner/mocks/prunable.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/storage/pkg/pruner/mocks/pruner.mock.go b/mod/storage/pkg/pruner/mocks/pruner.mock.go index cb882d94f3..80f931b735 100644 --- a/mod/storage/pkg/pruner/mocks/pruner.mock.go +++ b/mod/storage/pkg/pruner/mocks/pruner.mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. +// Code generated by mockery v2.44.2. DO NOT EDIT. package mocks diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 227041f612..b71f1244a4 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -47,7 +47,7 @@ type pruner[ prunable Prunable logger log.Logger[any] name string - subBeaconBlockFinalized BeaconBlockSubscription[BeaconBlockT, BlockEventT] + subBeaconBlockFinalized chan BlockEventT pruneRangeFn func(BlockEventT) (uint64, uint64) } @@ -60,7 +60,7 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - subBeaconBlockFinalized BeaconBlockSubscription[BeaconBlockT, BlockEventT], + subBeaconBlockFinalized chan BlockEventT, pruneRangeFn func(BlockEventT) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, BlockEventT, PrunableT]{ @@ -74,7 +74,20 @@ func NewPruner[ // Start starts the Pruner by listening for new indexes to prune. func (p *pruner[BeaconBlockT, BlockEventT, _]) Start(ctx context.Context) { - p.subBeaconBlockFinalized.Listen(ctx, p.onFinalizeBlock) + go p.listen(ctx) +} + +// listen listens for new finalized blocks and prunes the prunable store based +// on the received finalized block event. +func (p *pruner[_, BlockEventT, _]) listen(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case event := <-p.subBeaconBlockFinalized: + p.onFinalizeBlock(event) + } + } } // onFinalizeBlock will prune the prunable store based on the received diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 39b84a10b0..0c664b99d4 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -31,7 +31,6 @@ import ( "time" "cosmossdk.io/log" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -71,7 +70,7 @@ func TestPruner(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { logger := log.NewNopLogger() - ch := async.NewSubscription[pruner.BlockEvent[pruner.BeaconBlock]]() + ch := make(chan pruner.BlockEvent[pruner.BeaconBlock]) mockPrunable := new(mocks.Prunable) mockPrunable.On("Prune", mock.Anything, mock.Anything). Return(nil) diff --git a/mod/storage/pkg/pruner/types.go b/mod/storage/pkg/pruner/types.go index 7d3f058bcc..403a65a1cf 100644 --- a/mod/storage/pkg/pruner/types.go +++ b/mod/storage/pkg/pruner/types.go @@ -32,14 +32,6 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) -// BeaconBlockSubscription is an interface for beacon block subscriptions. -type BeaconBlockSubscription[ - BeaconBlockT BeaconBlock, - BlockEventT BlockEvent[BeaconBlockT], -] interface { - Listen(ctx context.Context, fn func(BlockEventT)) -} - // BeaconBlock is an interface for beacon blocks. type BeaconBlock interface { GetSlot() math.U64 From b254bf29c317ae4c307ccb59fc1cb327f1d38739 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 17:16:01 -0400 Subject: [PATCH 58/75] delete mock --- .../mocks/beacon_block_subscription.mock.go | 71 ------------------- 1 file changed, 71 deletions(-) delete mode 100644 mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go diff --git a/mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go b/mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go deleted file mode 100644 index 87e75ae02c..0000000000 --- a/mod/storage/pkg/pruner/mocks/beacon_block_subscription.mock.go +++ /dev/null @@ -1,71 +0,0 @@ -// Code generated by mockery v2.44.1. DO NOT EDIT. - -package mocks - -import ( - context "context" - - pruner "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" - mock "github.com/stretchr/testify/mock" -) - -// BeaconBlockSubscription is an autogenerated mock type for the BeaconBlockSubscription type -type BeaconBlockSubscription[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]] struct { - mock.Mock -} - -type BeaconBlockSubscription_Expecter[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]] struct { - mock *mock.Mock -} - -func (_m *BeaconBlockSubscription[BeaconBlockT, BlockEventT]) EXPECT() *BeaconBlockSubscription_Expecter[BeaconBlockT, BlockEventT] { - return &BeaconBlockSubscription_Expecter[BeaconBlockT, BlockEventT]{mock: &_m.Mock} -} - -// Listen provides a mock function with given fields: ctx, fn -func (_m *BeaconBlockSubscription[BeaconBlockT, BlockEventT]) Listen(ctx context.Context, fn func(BlockEventT)) { - _m.Called(ctx, fn) -} - -// BeaconBlockSubscription_Listen_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Listen' -type BeaconBlockSubscription_Listen_Call[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]] struct { - *mock.Call -} - -// Listen is a helper method to define mock.On call -// - ctx context.Context -// - fn func(BlockEventT) -func (_e *BeaconBlockSubscription_Expecter[BeaconBlockT, BlockEventT]) Listen(ctx interface{}, fn interface{}) *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { - return &BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]{Call: _e.mock.On("Listen", ctx, fn)} -} - -func (_c *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]) Run(run func(ctx context.Context, fn func(BlockEventT))) *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(func(BlockEventT))) - }) - return _c -} - -func (_c *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]) Return() *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { - _c.Call.Return() - return _c -} - -func (_c *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT]) RunAndReturn(run func(context.Context, func(BlockEventT))) *BeaconBlockSubscription_Listen_Call[BeaconBlockT, BlockEventT] { - _c.Call.Return(run) - return _c -} - -// NewBeaconBlockSubscription creates a new instance of BeaconBlockSubscription. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func NewBeaconBlockSubscription[BeaconBlockT pruner.BeaconBlock, BlockEventT pruner.BlockEvent[BeaconBlockT]](t interface { - mock.TestingT - Cleanup(func()) -}) *BeaconBlockSubscription[BeaconBlockT, BlockEventT] { - mock := &BeaconBlockSubscription[BeaconBlockT, BlockEventT]{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} From 2a30b9f2a03e1583c0a4c836be837fe7d29752e1 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 17:23:05 -0400 Subject: [PATCH 59/75] remove bundle --- mod/beacon/validator/types.go | 11 ------ mod/da/pkg/types/bundle.go | 44 ---------------------- mod/node-core/pkg/components/middleware.go | 2 +- mod/node-core/pkg/components/types.go | 5 --- mod/runtime/pkg/middleware/abci.go | 26 ++++++------- mod/runtime/pkg/middleware/middleware.go | 12 ++---- mod/runtime/pkg/middleware/types.go | 7 ---- 7 files changed, 18 insertions(+), 89 deletions(-) delete mode 100644 mod/da/pkg/types/bundle.go diff --git a/mod/beacon/validator/types.go b/mod/beacon/validator/types.go index 11799d5095..9abc0a8344 100644 --- a/mod/beacon/validator/types.go +++ b/mod/beacon/validator/types.go @@ -65,17 +65,6 @@ type BeaconBlock[ GetBody() BeaconBlockBodyT } -// BeaconBlockBundle represents a block data interface. -type BeaconBlockBundle[ - T any, - BeaconBlockT any, - BlobSidecarsT any, -] interface { - New(BeaconBlockT, BlobSidecarsT) T - GetBeaconBlock() BeaconBlockT - GetSidecars() BlobSidecarsT -} - // BeaconBlockBody represents a beacon block body interface. type BeaconBlockBody[ AttestationDataT, DepositT, Eth1DataT, ExecutionPayloadT, SlashingInfoT any, diff --git a/mod/da/pkg/types/bundle.go b/mod/da/pkg/types/bundle.go deleted file mode 100644 index 1267376eee..0000000000 --- a/mod/da/pkg/types/bundle.go +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package types - -type BlockBundle[BeaconBlockT any, BlobSidecarsT any] struct { - Block BeaconBlockT - Sidecars BlobSidecarsT -} - -func (bb *BlockBundle[BeaconBlockT, BlobSidecarsT]) New( - block BeaconBlockT, - sidecars BlobSidecarsT, -) *BlockBundle[BeaconBlockT, BlobSidecarsT] { - return &BlockBundle[BeaconBlockT, BlobSidecarsT]{ - Block: block, - Sidecars: sidecars, - } -} - -func (bb *BlockBundle[BeaconBlockT, _]) GetBeaconBlock() BeaconBlockT { - return bb.Block -} - -func (bb *BlockBundle[_, BlobSidecarsT]) GetSidecars() BlobSidecarsT { - return bb.Sidecars -} diff --git a/mod/node-core/pkg/components/middleware.go b/mod/node-core/pkg/components/middleware.go index 4d51e51b27..02708be2fa 100644 --- a/mod/node-core/pkg/components/middleware.go +++ b/mod/node-core/pkg/components/middleware.go @@ -47,7 +47,7 @@ func ProvideABCIMiddleware[ in ABCIMiddlewareInput[LoggerT], ) (*ABCIMiddleware, error) { return middleware.NewABCIMiddleware[ - *AvailabilityStore, *BeaconBlock, *BeaconBlockBundle, *BlobSidecars, + *AvailabilityStore, *BeaconBlock, *BlobSidecars, *Deposit, *ExecutionPayload, *Genesis, *SlotData, ]( in.ChainSpec, diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 3fa5ddf121..8cd8b37cba 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -74,7 +74,6 @@ type ( ABCIMiddleware = middleware.ABCIMiddleware[ *AvailabilityStore, *BeaconBlock, - *BeaconBlockBundle, *BlobSidecars, *Deposit, *ExecutionPayload, @@ -97,10 +96,6 @@ type ( // BeaconBlock type aliases. BeaconBlock = types.BeaconBlock - BeaconBlockBundle = datypes.BlockBundle[ - *BeaconBlock, - *BlobSidecars, - ] BeaconBlockBody = types.BeaconBlockBody BeaconBlockHeader = types.BeaconBlockHeader diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index e925205ac8..f25b21297b 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -41,7 +41,7 @@ import ( // InitGenesis is called by the base app to initialize the state of the. func (h *ABCIMiddleware[ - _, _, _, _, _, _, GenesisT, _, + _, _, _, _, _, GenesisT, _, ]) InitGenesis( ctx context.Context, bz []byte, @@ -71,7 +71,7 @@ func (h *ABCIMiddleware[ // waitForGenesisProcessed waits until the genesis data has been processed and // returns the validator updates, or err if the context is cancelled. func (h *ABCIMiddleware[ - _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, ]) waitForGenesisProcessed( ctx context.Context, ) (transition.ValidatorUpdates, error) { @@ -89,7 +89,7 @@ func (h *ABCIMiddleware[ // prepareProposal is the internal handler for preparing proposals. func (h *ABCIMiddleware[ - _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, SlotDataT, + _, BeaconBlockT, BlobSidecarsT, _, _, _, SlotDataT, ]) PrepareProposal( ctx context.Context, slotData SlotDataT, @@ -134,7 +134,7 @@ func (h *ABCIMiddleware[ // waitForBuiltBeaconBlock waits for the built beacon block to be received. func (h *ABCIMiddleware[ - _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, SlotDataT, + _, BeaconBlockT, BlobSidecarsT, _, _, _, SlotDataT, ]) waitForBuiltBeaconBlock( ctx context.Context, ) (BeaconBlockT, error) { @@ -148,7 +148,7 @@ func (h *ABCIMiddleware[ // waitForBuiltSidecars waits for the built sidecars to be received. func (h *ABCIMiddleware[ - _, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, _, _, _, SlotDataT, + _, BeaconBlockT, BlobSidecarsT, _, _, _, SlotDataT, ]) waitForBuiltSidecars( ctx context.Context, ) (BlobSidecarsT, error) { @@ -160,10 +160,10 @@ func (h *ABCIMiddleware[ } } -// handleBeaconBlockBundleResponse gossips the built beacon block and blob +// handleBuiltBeaconBlockAndSidecars gossips the built beacon block and blob // sidecars to the network. func (h *ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, + _, BeaconBlockT, BlobSidecarsT, _, _, _, _, ]) handleBuiltBeaconBlockAndSidecars( ctx context.Context, bb BeaconBlockT, @@ -191,7 +191,7 @@ func (h *ABCIMiddleware[ // ProcessProposal processes the proposal for the ABCI middleware. // It handles both the beacon block and blob sidecars concurrently. func (h *ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, + _, BeaconBlockT, BlobSidecarsT, _, _, _, _, ]) ProcessProposal( ctx context.Context, req proto.Message, @@ -250,7 +250,7 @@ func (h *ABCIMiddleware[ // waitForBeaconBlockVerification waits for the built beacon block to be // verified. func (h *ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, + _, BeaconBlockT, _, _, _, _, _, ]) waitForBeaconBlockVerification( ctx context.Context, ) (BeaconBlockT, error) { @@ -264,7 +264,7 @@ func (h *ABCIMiddleware[ // waitForSidecarVerification waits for the built sidecars to be verified. func (h *ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, + _, _, BlobSidecarsT, _, _, _, _, ]) waitForSidecarVerification( ctx context.Context, ) (BlobSidecarsT, error) { @@ -279,7 +279,7 @@ func (h *ABCIMiddleware[ // createResponse generates the appropriate ProcessProposalResponse based on the // error. func (*ABCIMiddleware[ - _, BeaconBlockT, _, _, BlobSidecarsT, _, _, _, + _, _, _, _, _, _, _, ]) createProcessProposalResponse(err error) (proto.Message, error) { status := cmtabci.PROCESS_PROPOSAL_STATUS_REJECT if !errors.IsFatal(err) { @@ -295,7 +295,7 @@ func (*ABCIMiddleware[ // EndBlock returns the validator set updates from the beacon state. func (h *ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, + _, BeaconBlockT, BlobSidecarsT, _, _, _, _, ]) FinalizeBlock( ctx context.Context, req proto.Message, ) (transition.ValidatorUpdates, error) { @@ -350,7 +350,7 @@ func (h *ABCIMiddleware[ // waitForFinalValidatorUpdates waits for the final validator updates to be // received. func (h *ABCIMiddleware[ - _, BeaconBlockT, _, BlobSidecarsT, _, _, _, _, + _, _, _, _, _, _, _, ]) waitForFinalValidatorUpdates( ctx context.Context, ) (transition.ValidatorUpdates, error) { diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 5368034966..9f660645fe 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -38,7 +38,6 @@ import ( type ABCIMiddleware[ AvailabilityStoreT any, BeaconBlockT BeaconBlock[BeaconBlockT], - BeaconBlockBundleT BeaconBlockBundle[BeaconBlockT, BlobSidecarsT], BlobSidecarsT interface { constraints.SSZMarshallable Empty() BlobSidecarsT @@ -86,7 +85,6 @@ type ABCIMiddleware[ func NewABCIMiddleware[ AvailabilityStoreT any, BeaconBlockT BeaconBlock[BeaconBlockT], - BeaconBlockBundleT BeaconBlockBundle[BeaconBlockT, BlobSidecarsT], BlobSidecarsT interface { constraints.SSZMarshallable Empty() BlobSidecarsT @@ -101,11 +99,11 @@ func NewABCIMiddleware[ telemetrySink TelemetrySink, dispatcher types.Dispatcher, ) *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, + AvailabilityStoreT, BeaconBlockT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ] { return &ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, + AvailabilityStoreT, BeaconBlockT, BlobSidecarsT, DepositT, ExecutionPayloadT, GenesisT, SlotDataT, ]{ chainSpec: chainSpec, @@ -132,8 +130,7 @@ func NewABCIMiddleware[ // Start subscribes the middleware to the events it needs to listen for. func (am *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, - DepositT, ExecutionPayloadT, GenesisT, SlotDataT, + _, _, _, _, _, _, _, ]) Start(_ context.Context) error { var err error if err = am.dispatcher.Subscribe( @@ -171,8 +168,7 @@ func (am *ABCIMiddleware[ // Name returns the name of the middleware. func (am *ABCIMiddleware[ - AvailabilityStoreT, BeaconBlockT, BeaconBlockBundleT, BlobSidecarsT, - DepositT, ExecutionPayloadT, GenesisT, SlotDataT, + _, _, _, _, _, _, _, ]) Name() string { return "abci-middleware" } diff --git a/mod/runtime/pkg/middleware/types.go b/mod/runtime/pkg/middleware/types.go index f91e501fef..a6e820d890 100644 --- a/mod/runtime/pkg/middleware/types.go +++ b/mod/runtime/pkg/middleware/types.go @@ -39,13 +39,6 @@ type BeaconBlock[SelfT any] interface { NewFromSSZ([]byte, uint32) (SelfT, error) } -// BeaconBlockBundle is a bundle of a beacon block and its corresponding blob -// sidecars. -type BeaconBlockBundle[BeaconBlockT any, BlobSidecarsT any] interface { - GetBeaconBlock() BeaconBlockT - GetSidecars() BlobSidecarsT -} - // TelemetrySink is an interface for sending metrics to a telemetry backend. type TelemetrySink interface { // MeasureSince measures the time since the given time. From ce033706658a0e2e9b175d1ad7332a612b8ba9e8 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 17:26:45 -0400 Subject: [PATCH 60/75] bet --- contracts/src/eip4788/BeaconVerifier.sol | 12 +++--------- contracts/src/eip4788/SSZ.sol | 8 ++------ contracts/src/eip4788/Verifier.sol | 4 +--- contracts/src/eip4788/interfaces/IBeaconVerifier.sol | 4 +--- contracts/test/BeaconDepositContract.t.sol | 8 ++------ mod/async/pkg/dispatcher/dispatcher.go | 2 +- mod/async/pkg/types/dispatcher.go | 5 +++-- mod/beacon/blockchain/execution_engine.go | 2 +- mod/beacon/blockchain/process.go | 2 +- mod/beacon/blockchain/service.go | 6 +++--- mod/beacon/validator/service.go | 4 ++-- mod/da/pkg/da/service.go | 2 +- mod/runtime/pkg/middleware/abci.go | 12 ++++++------ mod/runtime/pkg/middleware/middleware.go | 2 +- 14 files changed, 28 insertions(+), 45 deletions(-) diff --git a/contracts/src/eip4788/BeaconVerifier.sol b/contracts/src/eip4788/BeaconVerifier.sol index eb9eb41be4..b1559e5ea1 100644 --- a/contracts/src/eip4788/BeaconVerifier.sol +++ b/contracts/src/eip4788/BeaconVerifier.sol @@ -49,9 +49,7 @@ contract BeaconVerifier is Verifier, Ownable, IBeaconVerifier { return true; } - function setZeroValidatorPubkeyGIndex( - uint256 _zeroValidatorPubkeyGIndex - ) + function setZeroValidatorPubkeyGIndex(uint256 _zeroValidatorPubkeyGIndex) external onlyOwner { @@ -59,9 +57,7 @@ contract BeaconVerifier is Verifier, Ownable, IBeaconVerifier { emit ZeroValidatorPubkeyGIndexChanged(_zeroValidatorPubkeyGIndex); } - function setExecutionNumberGIndex( - uint256 _executionNumberGIndex - ) + function setExecutionNumberGIndex(uint256 _executionNumberGIndex) external onlyOwner { @@ -84,9 +80,7 @@ contract BeaconVerifier is Verifier, Ownable, IBeaconVerifier { /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @inheritdoc IBeaconVerifier - function getParentBeaconBlockRootAt( - uint64 timestamp - ) + function getParentBeaconBlockRootAt(uint64 timestamp) external view returns (bytes32) diff --git a/contracts/src/eip4788/SSZ.sol b/contracts/src/eip4788/SSZ.sol index 6e9c90994d..ba842fe904 100644 --- a/contracts/src/eip4788/SSZ.sol +++ b/contracts/src/eip4788/SSZ.sol @@ -12,9 +12,7 @@ library SSZ { error BranchHasExtraItem(); error InvalidValidatorPubkeyLength(); - function validatorPubkeyHashTreeRoot( - bytes memory pubkey - ) + function validatorPubkeyHashTreeRoot(bytes memory pubkey) internal view returns (bytes32 root) @@ -34,9 +32,7 @@ library SSZ { } } - function addressHashTreeRoot( - address v - ) + function addressHashTreeRoot(address v) internal pure returns (bytes32 root) diff --git a/contracts/src/eip4788/Verifier.sol b/contracts/src/eip4788/Verifier.sol index 73cc778e63..68dd04e479 100644 --- a/contracts/src/eip4788/Verifier.sol +++ b/contracts/src/eip4788/Verifier.sol @@ -10,9 +10,7 @@ abstract contract Verifier { error InvalidProof(); error RootNotFound(); - function getParentBlockRoot( - uint64 ts - ) + function getParentBlockRoot(uint64 ts) internal view returns (bytes32 root) diff --git a/contracts/src/eip4788/interfaces/IBeaconVerifier.sol b/contracts/src/eip4788/interfaces/IBeaconVerifier.sol index d950638376..a3eefc6841 100644 --- a/contracts/src/eip4788/interfaces/IBeaconVerifier.sol +++ b/contracts/src/eip4788/interfaces/IBeaconVerifier.sol @@ -32,9 +32,7 @@ interface IBeaconVerifier { function executionFeeRecipientGIndex() external view returns (uint256); /// @notice Get the parent beacon block root from the given timestamp. - function getParentBeaconBlockRootAt( - uint64 timestamp - ) + function getParentBeaconBlockRootAt(uint64 timestamp) external view returns (bytes32); diff --git a/contracts/test/BeaconDepositContract.t.sol b/contracts/test/BeaconDepositContract.t.sol index 192b38aabc..947bd640e2 100644 --- a/contracts/test/BeaconDepositContract.t.sol +++ b/contracts/test/BeaconDepositContract.t.sol @@ -60,9 +60,7 @@ contract DepositContractTest is SoladyTest { ); } - function testFuzz_DepositWrongCredentials( - bytes calldata credentials - ) + function testFuzz_DepositWrongCredentials(bytes calldata credentials) public { vm.assume(credentials.length != 32); @@ -119,9 +117,7 @@ contract DepositContractTest is SoladyTest { ); } - function testFuzz_DepositNativeWrongMinAmount( - uint256 amountInEther - ) + function testFuzz_DepositNativeWrongMinAmount(uint256 amountInEther) public { amountInEther = _bound(amountInEther, 1, 31); diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 443308ba5d..4af7ed60c7 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -47,7 +47,7 @@ func New( } // Publish dispatches the given event to the broker with the given eventID. -func (d *Dispatcher) PublishEvent(event types.BaseEvent) error { +func (d *Dispatcher) Publish(event types.BaseEvent) error { broker, ok := d.brokers[event.ID()] if !ok { return errBrokerNotFound(event.ID()) diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index 3f69d8cd5f..39f92f448f 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -37,13 +37,14 @@ type Dispatcher interface { // EventDispatcher is the API for a dispatcher that facilitates the publishing // of async events. type EventDispatcher interface { - // PublishEvent publishes an event to the dispatcher. - PublishEvent(event BaseEvent) error + // Publish publishes an event to the dispatcher. + Publish(event BaseEvent) error // Subscribe subscribes the given channel to all events with the given event // ID. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. Subscribe(eventID EventID, ch any) error + // TODO: add unsubscribe } // publisher is the interface that supports basic event publisher operations. diff --git a/mod/beacon/blockchain/execution_engine.go b/mod/beacon/blockchain/execution_engine.go index 5604429c84..eeef91e07f 100644 --- a/mod/beacon/blockchain/execution_engine.go +++ b/mod/beacon/blockchain/execution_engine.go @@ -54,7 +54,7 @@ func (s *Service[ // sendNextFCUWithAttributes sends a forkchoice update to the execution // client with attributes. // -//nolint:lll // it's fine + func (s *Service[ _, BeaconBlockT, _, _, BeaconStateT, _, _, ExecutionPayloadHeaderT, _, _, _, diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 5f3638d7ec..fd791393ed 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -83,7 +83,7 @@ func (s *Service[ // TODO: this is hood as fuck. // We won't send a fcu if the block is bad, should be addressed // via ticker later. - if err = s.dispatcher.PublishEvent( + if err = s.dispatcher.Publish( asynctypes.NewEvent( ctx, events.BeaconBlockFinalizedEvent, blk, ), diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 713ed0c611..de05419087 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -237,7 +237,7 @@ func (s *Service[ } // Emit the event containing the validator updates. - if err := s.dispatcher.PublishEvent( + if err := s.dispatcher.Publish( async.NewEvent( msg.Context(), events.GenesisDataProcessed, @@ -266,7 +266,7 @@ func (s *Service[ // emit a BeaconBlockVerified event with the error result from \ // VerifyIncomingBlock - if err := s.dispatcher.PublishEvent( + if err := s.dispatcher.Publish( async.NewEvent( msg.Context(), events.BeaconBlockVerified, @@ -305,7 +305,7 @@ func (s *Service[ } // Emit the event containing the validator updates. - if err := s.dispatcher.PublishEvent( + if err := s.dispatcher.Publish( async.NewEvent( msg.Context(), events.FinalValidatorUpdatesProcessed, diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 1f49fee22f..63f549e44c 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -216,14 +216,14 @@ func (s *Service[ } // emit a built block event with the built block and the error - if bbErr := s.dispatcher.PublishEvent( + if bbErr := s.dispatcher.Publish( async.NewEvent(req.Context(), events.BuiltBeaconBlock, blk, err), ); bbErr != nil { s.logger.Error("failed to dispatch built block", "err", err) } // emit a built sidecars event with the built sidecars and the error - if scErr := s.dispatcher.PublishEvent( + if scErr := s.dispatcher.Publish( async.NewEvent(req.Context(), events.BuiltSidecars, sidecars, err), ); scErr != nil { s.logger.Error("failed to dispatch built sidecars", "err", err) diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 1b057854da..a0ab6b1b73 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -161,7 +161,7 @@ func (s *Service[_, _, BlobSidecarsT, _]) handleSidecarsVerifyRequest( } // emit the sidecars verification event with error from verifySidecars - if err := s.dispatcher.PublishEvent( + if err := s.dispatcher.Publish( async.NewEvent( msg.Context(), events.SidecarsVerified, msg.Data(), sidecarsErr, ), diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index f25b21297b..b8ff2248fe 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -60,7 +60,7 @@ func (h *ABCIMiddleware[ return nil, err } - if err = h.dispatcher.PublishEvent( + if err = h.dispatcher.Publish( async.NewEvent(ctx, events.GenesisDataReceived, *data), ); err != nil { return nil, err @@ -106,7 +106,7 @@ func (h *ABCIMiddleware[ defer cancel() defer h.metrics.measurePrepareProposalDuration(startTime) - if err = h.dispatcher.PublishEvent( + if err = h.dispatcher.Publish( async.NewEvent( ctx, events.NewSlot, slotData, ), @@ -217,7 +217,7 @@ func (h *ABCIMiddleware[ } // notify that the beacon block has been received. - if err = h.dispatcher.PublishEvent( + if err = h.dispatcher.Publish( async.NewEvent(ctx, events.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) @@ -229,7 +229,7 @@ func (h *ABCIMiddleware[ } // notify that the sidecars have been received. - if err = h.dispatcher.PublishEvent( + if err = h.dispatcher.Publish( async.NewEvent(ctx, events.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) @@ -325,14 +325,14 @@ func (h *ABCIMiddleware[ } // notify that the final beacon block has been received. - if err = h.dispatcher.PublishEvent( + if err = h.dispatcher.Publish( async.NewEvent(ctx, events.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } // notify that the final blob sidecars have been received. - if err = h.dispatcher.PublishEvent( + if err = h.dispatcher.Publish( async.NewEvent(ctx, events.FinalSidecarsReceived, blobs), ); err != nil { return nil, err diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 9f660645fe..071b2acdde 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -81,7 +81,7 @@ type ABCIMiddleware[ // NewABCIMiddleware creates a new instance of the Handler struct. // -//nolint:lll // long types + func NewABCIMiddleware[ AvailabilityStoreT any, BeaconBlockT BeaconBlock[BeaconBlockT], From 07756516943ba3309d917d44b107672dc8a864ad Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 17:28:58 -0400 Subject: [PATCH 61/75] oops --- contracts/src/eip4788/BeaconVerifier.sol | 12 +++++++++--- contracts/src/eip4788/SSZ.sol | 8 ++++++-- contracts/src/eip4788/Verifier.sol | 4 +++- contracts/src/eip4788/interfaces/IBeaconVerifier.sol | 4 +++- contracts/test/BeaconDepositContract.t.sol | 8 ++++++-- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/contracts/src/eip4788/BeaconVerifier.sol b/contracts/src/eip4788/BeaconVerifier.sol index b1559e5ea1..eb9eb41be4 100644 --- a/contracts/src/eip4788/BeaconVerifier.sol +++ b/contracts/src/eip4788/BeaconVerifier.sol @@ -49,7 +49,9 @@ contract BeaconVerifier is Verifier, Ownable, IBeaconVerifier { return true; } - function setZeroValidatorPubkeyGIndex(uint256 _zeroValidatorPubkeyGIndex) + function setZeroValidatorPubkeyGIndex( + uint256 _zeroValidatorPubkeyGIndex + ) external onlyOwner { @@ -57,7 +59,9 @@ contract BeaconVerifier is Verifier, Ownable, IBeaconVerifier { emit ZeroValidatorPubkeyGIndexChanged(_zeroValidatorPubkeyGIndex); } - function setExecutionNumberGIndex(uint256 _executionNumberGIndex) + function setExecutionNumberGIndex( + uint256 _executionNumberGIndex + ) external onlyOwner { @@ -80,7 +84,9 @@ contract BeaconVerifier is Verifier, Ownable, IBeaconVerifier { /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @inheritdoc IBeaconVerifier - function getParentBeaconBlockRootAt(uint64 timestamp) + function getParentBeaconBlockRootAt( + uint64 timestamp + ) external view returns (bytes32) diff --git a/contracts/src/eip4788/SSZ.sol b/contracts/src/eip4788/SSZ.sol index ba842fe904..6e9c90994d 100644 --- a/contracts/src/eip4788/SSZ.sol +++ b/contracts/src/eip4788/SSZ.sol @@ -12,7 +12,9 @@ library SSZ { error BranchHasExtraItem(); error InvalidValidatorPubkeyLength(); - function validatorPubkeyHashTreeRoot(bytes memory pubkey) + function validatorPubkeyHashTreeRoot( + bytes memory pubkey + ) internal view returns (bytes32 root) @@ -32,7 +34,9 @@ library SSZ { } } - function addressHashTreeRoot(address v) + function addressHashTreeRoot( + address v + ) internal pure returns (bytes32 root) diff --git a/contracts/src/eip4788/Verifier.sol b/contracts/src/eip4788/Verifier.sol index 68dd04e479..73cc778e63 100644 --- a/contracts/src/eip4788/Verifier.sol +++ b/contracts/src/eip4788/Verifier.sol @@ -10,7 +10,9 @@ abstract contract Verifier { error InvalidProof(); error RootNotFound(); - function getParentBlockRoot(uint64 ts) + function getParentBlockRoot( + uint64 ts + ) internal view returns (bytes32 root) diff --git a/contracts/src/eip4788/interfaces/IBeaconVerifier.sol b/contracts/src/eip4788/interfaces/IBeaconVerifier.sol index a3eefc6841..d950638376 100644 --- a/contracts/src/eip4788/interfaces/IBeaconVerifier.sol +++ b/contracts/src/eip4788/interfaces/IBeaconVerifier.sol @@ -32,7 +32,9 @@ interface IBeaconVerifier { function executionFeeRecipientGIndex() external view returns (uint256); /// @notice Get the parent beacon block root from the given timestamp. - function getParentBeaconBlockRootAt(uint64 timestamp) + function getParentBeaconBlockRootAt( + uint64 timestamp + ) external view returns (bytes32); diff --git a/contracts/test/BeaconDepositContract.t.sol b/contracts/test/BeaconDepositContract.t.sol index 947bd640e2..192b38aabc 100644 --- a/contracts/test/BeaconDepositContract.t.sol +++ b/contracts/test/BeaconDepositContract.t.sol @@ -60,7 +60,9 @@ contract DepositContractTest is SoladyTest { ); } - function testFuzz_DepositWrongCredentials(bytes calldata credentials) + function testFuzz_DepositWrongCredentials( + bytes calldata credentials + ) public { vm.assume(credentials.length != 32); @@ -117,7 +119,9 @@ contract DepositContractTest is SoladyTest { ); } - function testFuzz_DepositNativeWrongMinAmount(uint256 amountInEther) + function testFuzz_DepositNativeWrongMinAmount( + uint256 amountInEther + ) public { amountInEther = _bound(amountInEther, 1, 31); From c5022ae97a159fb1f525d70a9b4ebca7fe47eb51 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 18:59:24 -0400 Subject: [PATCH 62/75] name --- mod/beacon/block_store/service.go | 4 ++-- mod/beacon/blockchain/service.go | 6 +++--- mod/beacon/validator/service.go | 4 ++-- mod/da/pkg/da/service.go | 4 ++-- mod/execution/pkg/deposit/service.go | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index e6faa367c2..34f3ca4c02 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -84,11 +84,11 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { return err } - go s.listen(ctx) + go s.eventLoop(ctx) return nil } -func (s *Service[BeaconBlockT, BlockStoreT]) listen(ctx context.Context) { +func (s *Service[BeaconBlockT, BlockStoreT]) eventLoop(ctx context.Context) { for { select { case <-ctx.Done(): diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index de05419087..63ab13f857 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -193,14 +193,14 @@ func (s *Service[ } // start a goroutine to listen for requests and handle accordingly - go s.listen(ctx) + go s.eventLoop(ctx) return nil } -// listen listens for events and handles them accordingly. +// eventLoop listens for events and handles them accordingly. func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, _, -]) listen(ctx context.Context) { +]) eventLoop(ctx context.Context) { for { select { case <-ctx.Done(): diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 63f549e44c..e3838a08b3 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -180,11 +180,11 @@ func (s *Service[ return err } // set the handler for new slot events - go s.listen(ctx) + go s.eventLoop(ctx) return nil } -func (s *Service[_, _, _, _, _, _, _, _, _, _, _, _, SlotDataT]) listen( +func (s *Service[_, _, _, _, _, _, _, _, _, _, _, _, SlotDataT]) eventLoop( ctx context.Context, ) { for { diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index a0ab6b1b73..3a5f0fb489 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -109,11 +109,11 @@ func (s *Service[_, _, BlobSidecarsT, _]) Start(ctx context.Context) error { } // listen for events and handle accordingly - go s.listen(ctx) + go s.eventLoop(ctx) return nil } -func (s *Service[_, _, BlobSidecarsT, _]) listen(ctx context.Context) { +func (s *Service[_, _, BlobSidecarsT, _]) eventLoop(ctx context.Context) { for { select { case <-ctx.Done(): diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index e5b638b58b..6adc60545c 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -102,7 +102,7 @@ func (s *Service[ } // Listen for finalized block events and fetch deposits for the block. - go s.listen(ctx) + go s.eventLoop(ctx) // Catchup deposits for failed blocks. go s.depositCatchupFetcher(ctx) @@ -111,7 +111,7 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, -]) listen(ctx context.Context) { +]) eventLoop(ctx context.Context) { for { select { case <-ctx.Done(): From 5c7c518bf1d78723ef4f6a2820d8b9bb15b15869 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Fri, 16 Aug 2024 19:42:32 -0400 Subject: [PATCH 63/75] tidy sync --- mod/beacon/go.sum | 2 -- mod/node-core/go.mod | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/mod/beacon/go.sum b/mod/beacon/go.sum index ccc0b0f46c..0fd76c1e14 100644 --- a/mod/beacon/go.sum +++ b/mod/beacon/go.sum @@ -8,8 +8,6 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240816230528-f52c938c20cc h1:wImM7/CL8FOZp9O7Q104rgJjchyZ9VSyCx6c2YhXRs0= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240816230528-f52c938c20cc/go.mod h1:CEFntRxY0/vpr5Rt/++/EfkwPaFWfOr677fjFDO8dqA= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A= diff --git a/mod/node-core/go.mod b/mod/node-core/go.mod index d8aa4310ec..cb984c61f1 100644 --- a/mod/node-core/go.mod +++ b/mod/node-core/go.mod @@ -107,7 +107,7 @@ require ( github.com/DataDog/zstd v1.5.6 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/berachain/beacon-kit/mod/async v0.0.0-20240809203305-99679c55701d + github.com/berachain/beacon-kit/mod/async v0.0.0-20240816230528-f52c938c20cc github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd // indirect github.com/bgentry/speakeasy v0.2.0 // indirect github.com/bits-and-blooms/bitset v1.13.0 // indirect From 7a69ea1ced191c40397035aa69b02b7074880991 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sat, 17 Aug 2024 14:06:36 -0400 Subject: [PATCH 64/75] names --- mod/beacon/blockchain/service.go | 12 ++++++------ mod/da/pkg/da/service.go | 12 ++++++------ mod/storage/pkg/pruner/pruner.go | 9 +++++---- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index ab149571a3..2bd06a76e8 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -204,11 +204,11 @@ func (s *Service[ case <-ctx.Done(): return case event := <-s.subGenDataReceived: - s.handleProcessGenesisDataEvent(event) + s.handleGenDataReceived(event) case event := <-s.subBlockReceived: - s.handleVerifyBeaconBlockEvent(event) + s.handleBeaconBlockReceived(event) case event := <-s.subFinalBlkReceived: - s.handleFinalizeBeaconBlockEvent(event) + s.handleFinalBeaconBlockReceived(event) } } } @@ -219,7 +219,7 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, -]) handleProcessGenesisDataEvent(msg async.Event[GenesisT]) { +]) handleGenDataReceived(msg async.Event[GenesisT]) { var ( valUpdates transition.ValidatorUpdates genesisErr error @@ -253,7 +253,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, -]) handleVerifyBeaconBlockEvent( +]) handleBeaconBlockReceived( msg async.Event[BeaconBlockT], ) { // If the block is nil, exit early. @@ -281,7 +281,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, -]) handleFinalizeBeaconBlockEvent( +]) handleFinalBeaconBlockReceived( msg async.Event[BeaconBlockT], ) { var ( diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index f2848fa19a..f65c32894c 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -108,9 +108,9 @@ func (s *Service[_, BlobSidecarsT]) eventLoop(ctx context.Context) { case <-ctx.Done(): return case event := <-s.subSidecarsReceived: - s.handleSidecarsVerifyRequest(event) + s.handleSidecarsReceived(event) case event := <-s.subFinalBlobSidecars: - s.handleBlobSidecarsProcessRequest(event) + s.handleFinalSidecarsReceived(event) } } } @@ -119,10 +119,10 @@ func (s *Service[_, BlobSidecarsT]) eventLoop(ctx context.Context) { /* Event Handlers */ /* -------------------------------------------------------------------------- */ -// handleBlobSidecarsProcessRequest handles the BlobSidecarsProcessRequest +// handleFinalSidecarsReceived handles the BlobSidecarsProcessRequest // event. // It processes the sidecars and publishes a BlobSidecarsProcessed event. -func (s *Service[_, BlobSidecarsT]) handleBlobSidecarsProcessRequest( +func (s *Service[_, BlobSidecarsT]) handleFinalSidecarsReceived( msg async.Event[BlobSidecarsT], ) { if err := s.processSidecars(msg.Context(), msg.Data()); err != nil { @@ -134,9 +134,9 @@ func (s *Service[_, BlobSidecarsT]) handleBlobSidecarsProcessRequest( } } -// handleSidecarsVerifyRequest handles the SidecarsVerifyRequest event. +// handleSidecarsReceived handles the SidecarsVerifyRequest event. // It verifies the sidecars and publishes a SidecarsVerified event. -func (s *Service[_, BlobSidecarsT]) handleSidecarsVerifyRequest( +func (s *Service[_, BlobSidecarsT]) handleSidecarsReceived( msg async.Event[BlobSidecarsT], ) { var sidecarsErr error diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index d4f5ce6552..952b36bd2f 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -61,10 +61,11 @@ func NewPruner[ pruneRangeFn func(asynctypes.Event[BeaconBlockT]) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, PrunableT]{ - logger: logger, - prunable: prunable, - name: name, - pruneRangeFn: pruneRangeFn, + logger: logger, + prunable: prunable, + name: name, + pruneRangeFn: pruneRangeFn, + subBeaconBlockFinalized: subBeaconBlockFinalized, } } From 691683497df7e523877bd53c262854f5fc8b6b23 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sun, 18 Aug 2024 01:35:49 -0400 Subject: [PATCH 65/75] bet --- mod/async/go.mod | 1 + mod/async/go.sum | 2 + mod/async/pkg/broker/broker.go | 14 +-- mod/async/pkg/dispatcher/dispatcher.go | 9 +- mod/async/pkg/dispatcher/errors.go | 6 +- mod/async/pkg/types/dispatcher.go | 14 ++- mod/beacon/block_store/pruner.go | 9 +- mod/beacon/block_store/service.go | 6 +- mod/beacon/block_store/types.go | 6 +- mod/beacon/blockchain/process.go | 3 +- mod/beacon/blockchain/service.go | 24 ++--- mod/beacon/go.mod | 2 +- mod/beacon/validator/service.go | 10 +- mod/da/go.mod | 2 +- mod/da/pkg/da/service.go | 14 +-- mod/da/pkg/store/pruner.go | 6 +- mod/da/pkg/store/pruner_test.go | 6 +- mod/execution/go.mod | 2 +- mod/execution/go.sum | 4 +- mod/execution/pkg/deposit/pruner.go | 6 +- mod/execution/pkg/deposit/service.go | 4 +- mod/execution/pkg/deposit/sync.go | 4 +- mod/execution/pkg/deposit/types.go | 6 +- mod/node-core/go.mod | 2 +- mod/node-core/pkg/components/types.go | 38 ++++---- mod/primitives/pkg/events/event.go | 93 +++++++++++++++++++ .../pkg/events/{events.go => id.go} | 5 +- mod/runtime/go.mod | 2 +- mod/runtime/pkg/middleware/abci.go | 13 ++- mod/runtime/pkg/middleware/middleware.go | 24 ++--- mod/storage/pkg/manager/manager_test.go | 6 +- mod/storage/pkg/manager/types.go | 6 +- .../pkg/pruner/mocks/block_event.mock.go | 28 +++--- mod/storage/pkg/pruner/pruner.go | 12 +-- mod/storage/pkg/pruner/pruner_test.go | 7 +- mod/storage/pkg/pruner/types.go | 6 +- 36 files changed, 252 insertions(+), 150 deletions(-) create mode 100644 mod/primitives/pkg/events/event.go rename mod/primitives/pkg/events/{events.go => id.go} (94%) diff --git a/mod/async/go.mod b/mod/async/go.mod index f9ff02c670..1ae49ca69d 100644 --- a/mod/async/go.mod +++ b/mod/async/go.mod @@ -5,6 +5,7 @@ go 1.23.0 require ( github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 ) require ( diff --git a/mod/async/go.sum b/mod/async/go.sum index 6ff86e4669..c31ac5adf3 100644 --- a/mod/async/go.sum +++ b/mod/async/go.sum @@ -2,6 +2,8 @@ github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0/go.mod h1:og0jtHZosPDTyhge9tMBlRItoZ4Iv3aZFM9n4QDpcdo= github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= +github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 h1:2yo+tgSE9kwk/yaapVFwa87mweovWt4HOrXr3SH3zhk= +github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573/go.mod h1:Cv49avty5oqeCGw0zP8aLpTsVlhL9pfmWEuyoGzUuQA= github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= diff --git a/mod/async/pkg/broker/broker.go b/mod/async/pkg/broker/broker.go index 5fbc9125ec..62416276e6 100644 --- a/mod/async/pkg/broker/broker.go +++ b/mod/async/pkg/broker/broker.go @@ -25,13 +25,13 @@ import ( "sync" "time" - "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) // Broker is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Broker[T types.BaseEvent] struct { - eventID types.EventID +type Broker[T events.BaseEvent] struct { + eventID events.EventID // subscriptions is a map of subscribed subscriptions. subscriptions map[chan T]struct{} // msgs is the channel for publishing new messages. @@ -44,9 +44,9 @@ type Broker[T types.BaseEvent] struct { // New creates a new broker publishing events of type T for the // provided eventID. -func New[T types.BaseEvent](eventID string) *Broker[T] { +func New[T events.BaseEvent](eventID string) *Broker[T] { return &Broker[T]{ - eventID: types.EventID(eventID), + eventID: events.EventID(eventID), subscriptions: make(map[chan T]struct{}), msgs: make(chan T, defaultBufferSize), timeout: defaultBrokerTimeout, @@ -55,7 +55,7 @@ func New[T types.BaseEvent](eventID string) *Broker[T] { } // EventID returns the event ID that the broker is responsible for. -func (p *Broker[T]) EventID() types.EventID { +func (p *Broker[T]) EventID() events.EventID { return p.eventID } @@ -81,7 +81,7 @@ func (p *Broker[T]) start(ctx context.Context) { // Publish publishes a msg to all subscribers. // Returns ErrTimeout on timeout. -func (p *Broker[T]) Publish(msg types.BaseEvent) error { +func (p *Broker[T]) Publish(msg events.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { return err diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index 4af7ed60c7..f52380b760 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -25,6 +25,7 @@ import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) var _ types.Dispatcher = (*Dispatcher)(nil) @@ -32,7 +33,7 @@ var _ types.Dispatcher = (*Dispatcher)(nil) // Dispatcher faciliates asynchronous communication between components, // typically services. type Dispatcher struct { - brokers map[types.EventID]types.Broker + brokers map[events.EventID]types.Broker logger log.Logger[any] } @@ -41,13 +42,13 @@ func New( logger log.Logger[any], ) *Dispatcher { return &Dispatcher{ - brokers: make(map[types.EventID]types.Broker), + brokers: make(map[events.EventID]types.Broker), logger: logger, } } // Publish dispatches the given event to the broker with the given eventID. -func (d *Dispatcher) Publish(event types.BaseEvent) error { +func (d *Dispatcher) Publish(event events.BaseEvent) error { broker, ok := d.brokers[event.ID()] if !ok { return errBrokerNotFound(event.ID()) @@ -60,7 +61,7 @@ func (d *Dispatcher) Publish(event types.BaseEvent) error { // corresponding to the broker. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. -func (d *Dispatcher) Subscribe(eventID types.EventID, ch any) error { +func (d *Dispatcher) Subscribe(eventID events.EventID, ch any) error { broker, ok := d.brokers[eventID] if !ok { return errBrokerNotFound(eventID) diff --git a/mod/async/pkg/dispatcher/errors.go b/mod/async/pkg/dispatcher/errors.go index 86c9efb0f9..3f85b20ce5 100644 --- a/mod/async/pkg/dispatcher/errors.go +++ b/mod/async/pkg/dispatcher/errors.go @@ -21,22 +21,22 @@ package dispatcher import ( - "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/errors" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) //nolint:gochecknoglobals // errors var ( ErrNotFound = errors.New("not found") ErrAlreadyExists = errors.New("already exists") - errBrokerNotFound = func(eventID types.EventID) error { + errBrokerNotFound = func(eventID events.EventID) error { return errors.Wrapf( ErrNotFound, "publisher not found for eventID: %s", eventID, ) } - errBrokerAlreadyExists = func(eventID types.EventID) error { + errBrokerAlreadyExists = func(eventID events.EventID) error { return errors.Wrapf( ErrAlreadyExists, "publisher already exists for eventID: %s", diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index 39f92f448f..f35f725858 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -20,7 +20,11 @@ package types -import "context" +import ( + "context" + + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" +) // Dispatcher is the full API for a dispatcher that facilitates the publishing // of async events and the sending and receiving of async messages. @@ -38,12 +42,12 @@ type Dispatcher interface { // of async events. type EventDispatcher interface { // Publish publishes an event to the dispatcher. - Publish(event BaseEvent) error + Publish(event events.BaseEvent) error // Subscribe subscribes the given channel to all events with the given event // ID. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. - Subscribe(eventID EventID, ch any) error + Subscribe(eventID events.EventID, ch any) error // TODO: add unsubscribe } @@ -52,11 +56,11 @@ type Broker interface { // Start starts the event publisher. Start(ctx context.Context) // Publish publishes the given event to the event publisher. - Publish(event BaseEvent) error + Publish(event events.BaseEvent) error // Subscribe subscribes the given channel to the event publisher. Subscribe(ch any) error // Unsubscribe unsubscribes the given channel from the event publisher. Unsubscribe(ch any) error // EventID returns the event ID that the publisher is responsible for. - EventID() EventID + EventID() events.EventID } diff --git a/mod/beacon/block_store/pruner.go b/mod/beacon/block_store/pruner.go index b8f46105ef..f414f6dbfe 100644 --- a/mod/beacon/block_store/pruner.go +++ b/mod/beacon/block_store/pruner.go @@ -17,17 +17,18 @@ // EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. - package blockstore -import async "github.com/berachain/beacon-kit/mod/async/pkg/types" +import ( + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" +) // BuildPruneRangeFn builds a function that returns the range of blocks to // prune. func BuildPruneRangeFn[BeaconBlockT BeaconBlock]( cfg Config, -) func(async.Event[BeaconBlockT]) (uint64, uint64) { - return func(event async.Event[BeaconBlockT]) (uint64, uint64) { +) func(events.Event[BeaconBlockT]) (uint64, uint64) { + return func(event events.Event[BeaconBlockT]) (uint64, uint64) { blk := event.Data() if blk.GetSlot().Unwrap() < cfg.AvailabilityWindow { return 1, 1 diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 34f3ca4c02..547ca94c42 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -42,7 +42,7 @@ type Service[ // store is the block store for the service. store BlockStoreT // subFinalizedBlkEvents is a channel for receiving finalized block events. - subFinalizedBlkEvents chan async.Event[BeaconBlockT] + subFinalizedBlkEvents chan events.Event[BeaconBlockT] } // NewService creates a new block service. @@ -60,7 +60,7 @@ func NewService[ logger: logger, dispatcher: dispatcher, store: store, - subFinalizedBlkEvents: make(chan async.Event[BeaconBlockT]), + subFinalizedBlkEvents: make(chan events.Event[BeaconBlockT]), } } @@ -102,7 +102,7 @@ func (s *Service[BeaconBlockT, BlockStoreT]) eventLoop(ctx context.Context) { // onFinalizeBlock is triggered when a finalized block event is received. // It stores the block in the KVStore. func (s *Service[BeaconBlockT, _]) onFinalizeBlock( - event async.Event[BeaconBlockT], + event events.Event[BeaconBlockT], ) { slot := event.Data().GetSlot() if err := s.store.Set(event.Data()); err != nil { diff --git a/mod/beacon/block_store/types.go b/mod/beacon/block_store/types.go index 2dfded798a..06aeda980f 100644 --- a/mod/beacon/block_store/types.go +++ b/mod/beacon/block_store/types.go @@ -21,8 +21,8 @@ package blockstore import ( - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -42,9 +42,9 @@ type BlockStore[BeaconBlockT BeaconBlock] interface { // Event is an interface for block events. type Event[BeaconBlockT BeaconBlock] interface { // ID returns the id of the event. - ID() async.EventID + ID() events.EventID // Is returns true if the event is of the given id. - Is(async.EventID) bool + Is(events.EventID) bool // Data returns the data of the event. Data() BeaconBlockT } diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 1de162e4df..11546d19d9 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -24,7 +24,6 @@ import ( "context" "time" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -84,7 +83,7 @@ func (s *Service[ // We won't send a fcu if the block is bad, should be addressed // via ticker later. if err = s.dispatcher.Publish( - asynctypes.NewEvent( + events.NewEvent( ctx, events.BeaconBlockFinalizedEvent, blk, ), ); err != nil { diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index 2bd06a76e8..fad8bff74e 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -86,12 +86,12 @@ type Service[ // subFinalBlkReceived is a channel for receiving finalize beacon block // requests. - subFinalBlkReceived chan async.Event[BeaconBlockT] + subFinalBlkReceived chan events.Event[BeaconBlockT] // subBlockReceived is a channel for receiving verify beacon block requests. - subBlockReceived chan async.Event[BeaconBlockT] + subBlockReceived chan events.Event[BeaconBlockT] // subGenDataReceived is a subscription for receiving genesis data // received events. - subGenDataReceived chan async.Event[GenesisT] + subGenDataReceived chan events.Event[GenesisT] } // NewService creates a new validator service. @@ -153,9 +153,9 @@ func NewService[ metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), - subFinalBlkReceived: make(chan async.Event[BeaconBlockT]), - subBlockReceived: make(chan async.Event[BeaconBlockT]), - subGenDataReceived: make(chan async.Event[GenesisT]), + subFinalBlkReceived: make(chan events.Event[BeaconBlockT]), + subBlockReceived: make(chan events.Event[BeaconBlockT]), + subGenDataReceived: make(chan events.Event[GenesisT]), } } @@ -219,7 +219,7 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, -]) handleGenDataReceived(msg async.Event[GenesisT]) { +]) handleGenDataReceived(msg events.Event[GenesisT]) { var ( valUpdates transition.ValidatorUpdates genesisErr error @@ -236,7 +236,7 @@ func (s *Service[ // Emit the event containing the validator updates. if err := s.dispatcher.Publish( - async.NewEvent( + events.NewEvent( msg.Context(), events.GenesisDataProcessed, valUpdates, @@ -254,7 +254,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, ]) handleBeaconBlockReceived( - msg async.Event[BeaconBlockT], + msg events.Event[BeaconBlockT], ) { // If the block is nil, exit early. if msg.Error() != nil { @@ -265,7 +265,7 @@ func (s *Service[ // emit a BeaconBlockVerified event with the error result from \ // VerifyIncomingBlock if err := s.dispatcher.Publish( - async.NewEvent( + events.NewEvent( msg.Context(), events.BeaconBlockVerified, msg.Data(), @@ -282,7 +282,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, ]) handleFinalBeaconBlockReceived( - msg async.Event[BeaconBlockT], + msg events.Event[BeaconBlockT], ) { var ( valUpdates transition.ValidatorUpdates @@ -304,7 +304,7 @@ func (s *Service[ // Emit the event containing the validator updates. if err := s.dispatcher.Publish( - async.NewEvent( + events.NewEvent( msg.Context(), events.FinalValidatorUpdatesProcessed, valUpdates, diff --git a/mod/beacon/go.mod b/mod/beacon/go.mod index 5f99072ae2..4ed63f10cf 100644 --- a/mod/beacon/go.mod +++ b/mod/beacon/go.mod @@ -13,7 +13,7 @@ require ( github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240809202957-3e3f169ad720 - github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 golang.org/x/sync v0.8.0 ) diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index e3838a08b3..b68bcc906c 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -89,7 +89,7 @@ type Service[ // metrics is a metrics collector. metrics *validatorMetrics // subNewSlot is a channel for new slot events. - subNewSlot chan async.Event[SlotDataT] + subNewSlot chan events.Event[SlotDataT] } // NewService creates a new validator service. @@ -156,7 +156,7 @@ func NewService[ remotePayloadBuilders: remotePayloadBuilders, metrics: newValidatorMetrics(ts), dispatcher: dispatcher, - subNewSlot: make(chan async.Event[SlotDataT]), + subNewSlot: make(chan events.Event[SlotDataT]), } } @@ -201,7 +201,7 @@ func (s *Service[_, _, _, _, _, _, _, _, _, _, _, _, SlotDataT]) eventLoop( // slot data and emits an event containing the built block and sidecars. func (s *Service[ _, BeaconBlockT, _, _, BlobSidecarsT, _, _, _, _, _, _, _, SlotDataT, -]) handleNewSlot(req async.Event[SlotDataT]) { +]) handleNewSlot(req events.Event[SlotDataT]) { var ( blk BeaconBlockT sidecars BlobSidecarsT @@ -217,14 +217,14 @@ func (s *Service[ // emit a built block event with the built block and the error if bbErr := s.dispatcher.Publish( - async.NewEvent(req.Context(), events.BuiltBeaconBlock, blk, err), + events.NewEvent(req.Context(), events.BuiltBeaconBlock, blk, err), ); bbErr != nil { s.logger.Error("failed to dispatch built block", "err", err) } // emit a built sidecars event with the built sidecars and the error if scErr := s.dispatcher.Publish( - async.NewEvent(req.Context(), events.BuiltSidecars, sidecars, err), + events.NewEvent(req.Context(), events.BuiltSidecars, sidecars, err), ); scErr != nil { s.logger.Error("failed to dispatch built sidecars", "err", err) } diff --git a/mod/da/go.mod b/mod/da/go.mod index b90fb682b5..f702da2f88 100644 --- a/mod/da/go.mod +++ b/mod/da/go.mod @@ -14,7 +14,7 @@ require ( github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd - github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 github.com/crate-crypto/go-kzg-4844 v1.1.0 github.com/ethereum/c-kzg-4844 v1.0.3 github.com/karalabe/ssz v0.2.1-0.20240724074312-3d1ff7a6f7c4 diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index f65c32894c..a06be22bd6 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -43,8 +43,8 @@ type Service[ ] dispatcher async.EventDispatcher logger log.Logger[any] - subSidecarsReceived chan async.Event[BlobSidecarsT] - subFinalBlobSidecars chan async.Event[BlobSidecarsT] + subSidecarsReceived chan events.Event[BlobSidecarsT] + subFinalBlobSidecars chan events.Event[BlobSidecarsT] } // NewService returns a new DA service. @@ -68,8 +68,8 @@ func NewService[ bp: bp, dispatcher: dispatcher, logger: logger, - subSidecarsReceived: make(chan async.Event[BlobSidecarsT]), - subFinalBlobSidecars: make(chan async.Event[BlobSidecarsT]), + subSidecarsReceived: make(chan events.Event[BlobSidecarsT]), + subFinalBlobSidecars: make(chan events.Event[BlobSidecarsT]), } } @@ -123,7 +123,7 @@ func (s *Service[_, BlobSidecarsT]) eventLoop(ctx context.Context) { // event. // It processes the sidecars and publishes a BlobSidecarsProcessed event. func (s *Service[_, BlobSidecarsT]) handleFinalSidecarsReceived( - msg async.Event[BlobSidecarsT], + msg events.Event[BlobSidecarsT], ) { if err := s.processSidecars(msg.Context(), msg.Data()); err != nil { s.logger.Error( @@ -137,7 +137,7 @@ func (s *Service[_, BlobSidecarsT]) handleFinalSidecarsReceived( // handleSidecarsReceived handles the SidecarsVerifyRequest event. // It verifies the sidecars and publishes a SidecarsVerified event. func (s *Service[_, BlobSidecarsT]) handleSidecarsReceived( - msg async.Event[BlobSidecarsT], + msg events.Event[BlobSidecarsT], ) { var sidecarsErr error // verify the sidecars. @@ -151,7 +151,7 @@ func (s *Service[_, BlobSidecarsT]) handleSidecarsReceived( // emit the sidecars verification event with error from verifySidecars if err := s.dispatcher.Publish( - async.NewEvent( + events.NewEvent( msg.Context(), events.SidecarsVerified, msg.Data(), sidecarsErr, ), ); err != nil { diff --git a/mod/da/pkg/store/pruner.go b/mod/da/pkg/store/pruner.go index 8a7ac76a17..206c0cab0f 100644 --- a/mod/da/pkg/store/pruner.go +++ b/mod/da/pkg/store/pruner.go @@ -21,14 +21,14 @@ package store import ( - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) func BuildPruneRangeFn[BeaconBlockT BeaconBlock]( cs common.ChainSpec, -) func(async.Event[BeaconBlockT]) (uint64, uint64) { - return func(event async.Event[BeaconBlockT]) (uint64, uint64) { +) func(events.Event[BeaconBlockT]) (uint64, uint64) { + return func(event events.Event[BeaconBlockT]) (uint64, uint64) { window := cs.MinEpochsForBlobsSidecarsRequest() * cs.SlotsPerEpoch() if event.Data().GetSlot().Unwrap() < window { return 0, 0 diff --git a/mod/da/pkg/store/pruner_test.go b/mod/da/pkg/store/pruner_test.go index b61b6eb5d0..c32a75290f 100644 --- a/mod/da/pkg/store/pruner_test.go +++ b/mod/da/pkg/store/pruner_test.go @@ -24,11 +24,11 @@ import ( "context" "testing" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/chain-spec/pkg/chain" "github.com/berachain/beacon-kit/mod/da/pkg/store" "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/stretchr/testify/require" ) @@ -118,9 +118,9 @@ func TestBuildPruneRangeFn(t *testing.T) { pruneFn := store.BuildPruneRangeFn[MockBeaconBlock]( cs, ) - event := asynctypes.NewEvent[MockBeaconBlock]( + event := events.NewEvent[MockBeaconBlock]( context.Background(), - asynctypes.EventID("mock"), + events.EventID("mock"), MockBeaconBlock{ slot: tt.eventSlot, }, diff --git a/mod/execution/go.mod b/mod/execution/go.mod index 8c4bfc2deb..7e29c1bb6e 100644 --- a/mod/execution/go.mod +++ b/mod/execution/go.mod @@ -10,7 +10,7 @@ require ( github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd - github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 github.com/ethereum/go-ethereum v1.14.7 ) diff --git a/mod/execution/go.sum b/mod/execution/go.sum index 5334e8ef29..ef305b679e 100644 --- a/mod/execution/go.sum +++ b/mod/execution/go.sum @@ -18,8 +18,8 @@ github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1 github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:7/SXz8S5VpFl2thcKuBdu1OId+SgI1o4N+S1FB92Zw8= github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd h1:DYSjsq80Omqqlt+z2VcYsSxjZpLqCDRz7CvUDBrLDJE= github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd/go.mod h1:BilVBmqKhC4GXYCaIs8QnKaR14kpn3YmF5uYBdayF9I= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 h1:hk8N7Q1CCKMW/05pRu5rbfRnbTUou5TjULaeeRtbU+E= -github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:7kNnd9rhYjyZJHuXs/ku5drL9EMM64ekJVR181fGmbM= +github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 h1:2yo+tgSE9kwk/yaapVFwa87mweovWt4HOrXr3SH3zhk= +github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573/go.mod h1:Cv49avty5oqeCGw0zP8aLpTsVlhL9pfmWEuyoGzUuQA= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0= diff --git a/mod/execution/pkg/deposit/pruner.go b/mod/execution/pkg/deposit/pruner.go index 04ab0379b2..c68d0cff04 100644 --- a/mod/execution/pkg/deposit/pruner.go +++ b/mod/execution/pkg/deposit/pruner.go @@ -21,8 +21,8 @@ package deposit import ( - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -33,8 +33,8 @@ func BuildPruneRangeFn[ }, DepositT Deposit[DepositT, WithdrawalCredentialsT], WithdrawalCredentialsT any, -](cs common.ChainSpec) func(asynctypes.Event[BeaconBlockT]) (uint64, uint64) { - return func(event asynctypes.Event[BeaconBlockT]) (uint64, uint64) { +](cs common.ChainSpec) func(events.Event[BeaconBlockT]) (uint64, uint64) { + return func(event events.Event[BeaconBlockT]) (uint64, uint64) { deposits := event.Data().GetBody().GetDeposits() if len(deposits) == 0 || cs.MaxDepositsPerBlock() == 0 { return 0, 0 diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 6adc60545c..8568264689 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -48,7 +48,7 @@ type Service[ // dispatcher is the dispatcher for the service. dispatcher async.EventDispatcher // subFinalizedBlockEvents is the channel that provides finalized block events. - subFinalizedBlockEvents chan async.Event[BeaconBlockT] + subFinalizedBlockEvents chan events.Event[BeaconBlockT] // metrics is the metrics for the deposit service. metrics *metrics // failedBlocks is a map of blocks that failed to be processed to be @@ -83,7 +83,7 @@ func NewService[ ds: ds, eth1FollowDistance: eth1FollowDistance, failedBlocks: make(map[math.Slot]struct{}), - subFinalizedBlockEvents: make(chan async.Event[BeaconBlockT]), + subFinalizedBlockEvents: make(chan events.Event[BeaconBlockT]), logger: logger, metrics: newMetrics(telemetrySink), } diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index bf93e39497..901119b4d2 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -24,7 +24,7 @@ import ( "context" "time" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -35,7 +35,7 @@ const defaultRetryInterval = 20 * time.Second // event and fetches and stores the deposits for that block. func (s *Service[ BeaconBlockT, _, _, _, _, -]) depositFetcher(ctx context.Context, event async.Event[BeaconBlockT]) { +]) depositFetcher(ctx context.Context, event events.Event[BeaconBlockT]) { blockNum := event.Data().GetBody().GetExecutionPayload().GetNumber() s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) } diff --git a/mod/execution/pkg/deposit/types.go b/mod/execution/pkg/deposit/types.go index b95b441f41..9c9de85567 100644 --- a/mod/execution/pkg/deposit/types.go +++ b/mod/execution/pkg/deposit/types.go @@ -23,8 +23,8 @@ package deposit import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -49,8 +49,8 @@ type BlockEvent[ BeaconBlockT BeaconBlock[BeaconBlockBodyT], ExecutionPayloadT ExecutionPayload, ] interface { - ID() asynctypes.EventID - Is(asynctypes.EventID) bool + ID() events.EventID + Is(events.EventID) bool Data() BeaconBlockT Context() context.Context } diff --git a/mod/node-core/go.mod b/mod/node-core/go.mod index cb984c61f1..c003f7cd89 100644 --- a/mod/node-core/go.mod +++ b/mod/node-core/go.mod @@ -39,7 +39,7 @@ require ( github.com/berachain/beacon-kit/mod/node-api v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/node-api/engines v0.0.0-20240806160829-cde2d1347e7e github.com/berachain/beacon-kit/mod/payload v0.0.0-20240705193247-d464364483df - github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240809203305-99679c55701d + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 github.com/berachain/beacon-kit/mod/runtime v0.0.0-20240809202957-3e3f169ad720 github.com/berachain/beacon-kit/mod/state-transition v0.0.0-20240717225334-64ec6650da31 github.com/berachain/beacon-kit/mod/storage v0.0.0-20240806160829-cde2d1347e7e diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 63576abaf5..0c1f9a1c24 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -24,7 +24,6 @@ import ( "cosmossdk.io/core/appmodule/v2" "github.com/berachain/beacon-kit/mod/async/pkg/broker" "github.com/berachain/beacon-kit/mod/async/pkg/dispatcher" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" blockstore "github.com/berachain/beacon-kit/mod/beacon/block_store" "github.com/berachain/beacon-kit/mod/beacon/blockchain" "github.com/berachain/beacon-kit/mod/beacon/validator" @@ -55,6 +54,7 @@ import ( nodetypes "github.com/berachain/beacon-kit/mod/node-core/pkg/types" "github.com/berachain/beacon-kit/mod/payload/pkg/attributes" payloadbuilder "github.com/berachain/beacon-kit/mod/payload/pkg/builder" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/service" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/middleware" @@ -422,61 +422,61 @@ type ( type ( // GenesisDataReceivedEvent is a type alias for the genesis data received event. - GenesisDataReceivedEvent = asynctypes.Event[*Genesis] + GenesisDataReceivedEvent = events.Event[*Genesis] // GenesisDataProcessedEvent is a type alias for the genesis data processed event. - GenesisDataProcessedEvent = asynctypes.Event[transition.ValidatorUpdates] + GenesisDataProcessedEvent = events.Event[transition.ValidatorUpdates] // NewSlotEvent is a type alias for the new slot event. - NewSlotEvent = asynctypes.Event[*SlotData] + NewSlotEvent = events.Event[*SlotData] // BuiltBeaconBlockEvent is a type alias for the built beacon block event. - BuiltBeaconBlockEvent = asynctypes.Event[*BeaconBlock] + BuiltBeaconBlockEvent = events.Event[*BeaconBlock] // BuiltSidecarsEvent is a type alias for the built sidecars event. - BuiltSidecarsEvent = asynctypes.Event[*BlobSidecars] + BuiltSidecarsEvent = events.Event[*BlobSidecars] // BeaconBlockReceivedEvent is a type alias for the beacon block received event. - BeaconBlockReceivedEvent = asynctypes.Event[*BeaconBlock] + BeaconBlockReceivedEvent = events.Event[*BeaconBlock] // SidecarsReceivedEvent is a type alias for the sidecars received event. - SidecarsReceivedEvent = asynctypes.Event[*BlobSidecars] + SidecarsReceivedEvent = events.Event[*BlobSidecars] // BeaconBlockVerifiedEvent is a type alias for the beacon block verified event. - BeaconBlockVerifiedEvent = asynctypes.Event[*BeaconBlock] + BeaconBlockVerifiedEvent = events.Event[*BeaconBlock] // SidecarsVerifiedEvent is a type alias for the sidecars verified event. - SidecarsVerifiedEvent = asynctypes.Event[*BlobSidecars] + SidecarsVerifiedEvent = events.Event[*BlobSidecars] // FinalBeaconBlockReceivedEvent is a type alias for the final beacon block received event. - FinalBeaconBlockReceivedEvent = asynctypes.Event[*BeaconBlock] + FinalBeaconBlockReceivedEvent = events.Event[*BeaconBlock] // FinalSidecarsReceivedEvent is a type alias for the final sidecars received event. - FinalSidecarsReceivedEvent = asynctypes.Event[*BlobSidecars] + FinalSidecarsReceivedEvent = events.Event[*BlobSidecars] // FinalValidatorUpdatesProcessedEvent is a type alias for the final validator updates processed event. - FinalValidatorUpdatesProcessedEvent = asynctypes.Event[transition.ValidatorUpdates] + FinalValidatorUpdatesProcessedEvent = events.Event[transition.ValidatorUpdates] // FinalizedBlockEvent is a type alias for the block event. - FinalizedBlockEvent = asynctypes.Event[*BeaconBlock] + FinalizedBlockEvent = events.Event[*BeaconBlock] ) // Messages. type ( // BlockMessage is a type alias for the block message. - BlockMessage = asynctypes.Event[*BeaconBlock] + BlockMessage = events.Event[*BeaconBlock] // GenesisMessage is a type alias for the genesis message. - GenesisMessage = asynctypes.Event[*Genesis] + GenesisMessage = events.Event[*Genesis] // SidecarMessage is a type alias for the sidecar message. - SidecarMessage = asynctypes.Event[*BlobSidecars] + SidecarMessage = events.Event[*BlobSidecars] // SlotMessage is a type alias for the slot message. - SlotMessage = asynctypes.Event[*SlotData] + SlotMessage = events.Event[*SlotData] // StatusMessage is a type alias for the status message. - StatusMessage = asynctypes.Event[*service.StatusEvent] + StatusMessage = events.Event[*service.StatusEvent] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/events/event.go b/mod/primitives/pkg/events/event.go new file mode 100644 index 0000000000..bb31b709e5 --- /dev/null +++ b/mod/primitives/pkg/events/event.go @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package events + +import ( + "context" + "errors" +) + +// BaseEvent defines the minimal interface that the dispatcher expects from a +// message. +type BaseEvent interface { + ID() EventID + Context() context.Context +} + +// Event defines the interface that the underlying route expects from a +// message with data. +type Event[DataT any] interface { + BaseEvent + Data() DataT + Error() error + Is(id EventID) bool +} + +// NewEvent creates a new Event with the given context and beacon event. +func NewEvent[ + DataT any, +]( + ctx context.Context, id EventID, data DataT, errs ...error, +) Event[DataT] { + return &event[DataT]{ + ctx: ctx, + id: id, + data: data, + err: errors.Join(errs...), + } +} + +// An event is a hard type implementation of the Event interface. +type event[DataT any] struct { + // ctx is the context associated with the event. + ctx context.Context + // id is the name of the event. + id EventID + // event is the actual beacon event. + data DataT + // err is the error associated with the event. + err error +} + +// ID returns the ID of the event. +func (m *event[DataT]) ID() EventID { + return m.id +} + +// Context returns the context associated with the event. +func (m *event[DataT]) Context() context.Context { + return m.ctx +} + +// Data returns the data associated with the event. +func (m *event[DataT]) Data() DataT { + return m.data +} + +// Error returns the error associated with the event. +func (m *event[DataT]) Error() error { + return m.err +} + +// Is returns true if the event has the given type. +func (m *event[DataT]) Is(messageType EventID) bool { + return m.id == messageType +} diff --git a/mod/primitives/pkg/events/events.go b/mod/primitives/pkg/events/id.go similarity index 94% rename from mod/primitives/pkg/events/events.go rename to mod/primitives/pkg/events/id.go index 6d02f7b2e6..db5baaf1d2 100644 --- a/mod/primitives/pkg/events/events.go +++ b/mod/primitives/pkg/events/id.go @@ -20,7 +20,10 @@ package events -// events, topologically sorted. +// EventID represents the type of a message. +type EventID string + +// event ids, topologically sorted. const ( // genesis data events. diff --git a/mod/runtime/go.mod b/mod/runtime/go.mod index f5994a57ea..910565ea85 100644 --- a/mod/runtime/go.mod +++ b/mod/runtime/go.mod @@ -24,7 +24,7 @@ require ( github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd github.com/berachain/beacon-kit/mod/p2p v0.0.0-20240618214413-d5ec0e66b3dd - github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240808194557-e72e74f58197 + github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240816231924-221a061d3573 github.com/cometbft/cometbft v1.0.0-rc1.0.20240806094948-2c4293ef36c4 github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240806094948-2c4293ef36c4 github.com/cosmos/cosmos-db v1.0.2 diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 6ffc91ce00..5e36d9acb1 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -24,7 +24,6 @@ import ( "context" "time" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" "github.com/berachain/beacon-kit/mod/primitives/pkg/events" @@ -61,7 +60,7 @@ func (h *ABCIMiddleware[ } if err = h.dispatcher.Publish( - async.NewEvent(ctx, events.GenesisDataReceived, *data), + events.NewEvent(ctx, events.GenesisDataReceived, *data), ); err != nil { return nil, err } @@ -107,7 +106,7 @@ func (h *ABCIMiddleware[ defer h.metrics.measurePrepareProposalDuration(startTime) if err = h.dispatcher.Publish( - async.NewEvent( + events.NewEvent( ctx, events.NewSlot, slotData, ), ); err != nil { @@ -218,7 +217,7 @@ func (h *ABCIMiddleware[ // notify that the beacon block has been received. if err = h.dispatcher.Publish( - async.NewEvent(ctx, events.BeaconBlockReceived, blk), + events.NewEvent(ctx, events.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -230,7 +229,7 @@ func (h *ABCIMiddleware[ // notify that the sidecars have been received. if err = h.dispatcher.Publish( - async.NewEvent(ctx, events.SidecarsReceived, sidecars), + events.NewEvent(ctx, events.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -328,14 +327,14 @@ func (h *ABCIMiddleware[ // notify that the final beacon block has been received. if err = h.dispatcher.Publish( - async.NewEvent(ctx, events.FinalBeaconBlockReceived, blk), + events.NewEvent(ctx, events.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } // notify that the final blob sidecars have been received. if err = h.dispatcher.Publish( - async.NewEvent(ctx, events.FinalSidecarsReceived, blobs), + events.NewEvent(ctx, events.FinalSidecarsReceived, blobs), ); err != nil { return nil, err } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 4b25037c08..22b23eb8db 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -68,12 +68,12 @@ type ABCIMiddleware[ // logger is the logger for the middleware. logger log.Logger[any] // subscription channels - subGenDataProcessed chan types.Event[validatorUpdates] - subBuiltBeaconBlock chan types.Event[BeaconBlockT] - subBuiltSidecars chan types.Event[BlobSidecarsT] - subBBVerified chan types.Event[BeaconBlockT] - subSCVerified chan types.Event[BlobSidecarsT] - subFinalValidatorUpdates chan types.Event[validatorUpdates] + subGenDataProcessed chan events.Event[validatorUpdates] + subBuiltBeaconBlock chan events.Event[BeaconBlockT] + subBuiltSidecars chan events.Event[BlobSidecarsT] + subBBVerified chan events.Event[BeaconBlockT] + subSCVerified chan events.Event[BlobSidecarsT] + subFinalValidatorUpdates chan events.Event[validatorUpdates] } // NewABCIMiddleware creates a new instance of the Handler struct. @@ -111,12 +111,12 @@ func NewABCIMiddleware[ logger: logger, metrics: newABCIMiddlewareMetrics(telemetrySink), dispatcher: dispatcher, - subGenDataProcessed: make(chan types.Event[validatorUpdates]), - subBuiltBeaconBlock: make(chan types.Event[BeaconBlockT]), - subBuiltSidecars: make(chan types.Event[BlobSidecarsT]), - subBBVerified: make(chan types.Event[BeaconBlockT]), - subSCVerified: make(chan types.Event[BlobSidecarsT]), - subFinalValidatorUpdates: make(chan types.Event[validatorUpdates]), + subGenDataProcessed: make(chan events.Event[validatorUpdates]), + subBuiltBeaconBlock: make(chan events.Event[BeaconBlockT]), + subBuiltSidecars: make(chan events.Event[BlobSidecarsT]), + subBBVerified: make(chan events.Event[BeaconBlockT]), + subSCVerified: make(chan events.Event[BlobSidecarsT]), + subFinalValidatorUpdates: make(chan events.Event[validatorUpdates]), } } diff --git a/mod/storage/pkg/manager/manager_test.go b/mod/storage/pkg/manager/manager_test.go index a440604265..0ad82d8b12 100644 --- a/mod/storage/pkg/manager/manager_test.go +++ b/mod/storage/pkg/manager/manager_test.go @@ -31,7 +31,7 @@ import ( "time" "cosmossdk.io/log" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -40,9 +40,9 @@ import ( func TestDBManager_Start(t *testing.T) { mockPrunable := new(mocks.Prunable) - ch := make(chan asynctypes.Event[manager.BeaconBlock]) + ch := make(chan events.Event[manager.BeaconBlock]) pruneParamsFn := func( - _ asynctypes.Event[manager.BeaconBlock], + _ events.Event[manager.BeaconBlock], ) (uint64, uint64) { return 0, 0 } diff --git a/mod/storage/pkg/manager/types.go b/mod/storage/pkg/manager/types.go index e4a8b76729..2eff00dcc7 100644 --- a/mod/storage/pkg/manager/types.go +++ b/mod/storage/pkg/manager/types.go @@ -28,7 +28,7 @@ package manager import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -39,8 +39,8 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { - Is(asynctypes.EventID) bool - ID() asynctypes.EventID + Is(events.EventID) bool + ID() events.EventID Data() BeaconBlockT Context() context.Context } diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index fed37040a6..4ef0b9960e 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -5,10 +5,10 @@ package mocks import ( context "context" - pruner "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" + events "github.com/berachain/beacon-kit/mod/primitives/pkg/events" mock "github.com/stretchr/testify/mock" - types "github.com/berachain/beacon-kit/mod/async/pkg/types" + pruner "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" ) // BlockEvent is an autogenerated mock type for the BlockEvent type @@ -117,18 +117,18 @@ func (_c *BlockEvent_Data_Call[BeaconBlockT]) RunAndReturn(run func() BeaconBloc } // ID provides a mock function with given fields: -func (_m *BlockEvent[BeaconBlockT]) ID() types.EventID { +func (_m *BlockEvent[BeaconBlockT]) ID() events.EventID { ret := _m.Called() if len(ret) == 0 { panic("no return value specified for ID") } - var r0 types.EventID - if rf, ok := ret.Get(0).(func() types.EventID); ok { + var r0 events.EventID + if rf, ok := ret.Get(0).(func() events.EventID); ok { r0 = rf() } else { - r0 = ret.Get(0).(types.EventID) + r0 = ret.Get(0).(events.EventID) } return r0 @@ -151,18 +151,18 @@ func (_c *BlockEvent_ID_Call[BeaconBlockT]) Run(run func()) *BlockEvent_ID_Call[ return _c } -func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 types.EventID) *BlockEvent_ID_Call[BeaconBlockT] { +func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 events.EventID) *BlockEvent_ID_Call[BeaconBlockT] { _c.Call.Return(_a0) return _c } -func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() types.EventID) *BlockEvent_ID_Call[BeaconBlockT] { +func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() events.EventID) *BlockEvent_ID_Call[BeaconBlockT] { _c.Call.Return(run) return _c } // Is provides a mock function with given fields: _a0 -func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.EventID) bool { +func (_m *BlockEvent[BeaconBlockT]) Is(_a0 events.EventID) bool { ret := _m.Called(_a0) if len(ret) == 0 { @@ -170,7 +170,7 @@ func (_m *BlockEvent[BeaconBlockT]) Is(_a0 types.EventID) bool { } var r0 bool - if rf, ok := ret.Get(0).(func(types.EventID) bool); ok { + if rf, ok := ret.Get(0).(func(events.EventID) bool); ok { r0 = rf(_a0) } else { r0 = ret.Get(0).(bool) @@ -185,14 +185,14 @@ type BlockEvent_Is_Call[BeaconBlockT pruner.BeaconBlock] struct { } // Is is a helper method to define mock.On call -// - _a0 types.EventID +// - _a0 events.EventID func (_e *BlockEvent_Expecter[BeaconBlockT]) Is(_a0 interface{}) *BlockEvent_Is_Call[BeaconBlockT] { return &BlockEvent_Is_Call[BeaconBlockT]{Call: _e.mock.On("Is", _a0)} } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 types.EventID)) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 events.EventID)) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.EventID)) + run(args[0].(events.EventID)) }) return _c } @@ -202,7 +202,7 @@ func (_c *BlockEvent_Is_Call[BeaconBlockT]) Return(_a0 bool) *BlockEvent_Is_Call return _c } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(types.EventID) bool) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(events.EventID) bool) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Return(run) return _c } diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index 952b36bd2f..ab1ed92461 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -29,8 +29,8 @@ package pruner import ( "context" - asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) // Compile-time check to ensure pruner implements the Pruner interface. @@ -45,8 +45,8 @@ type pruner[ prunable Prunable logger log.Logger[any] name string - subBeaconBlockFinalized chan asynctypes.Event[BeaconBlockT] - pruneRangeFn func(asynctypes.Event[BeaconBlockT]) (uint64, uint64) + subBeaconBlockFinalized chan events.Event[BeaconBlockT] + pruneRangeFn func(events.Event[BeaconBlockT]) (uint64, uint64) } // NewPruner creates a new Pruner. @@ -57,8 +57,8 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - subBeaconBlockFinalized chan asynctypes.Event[BeaconBlockT], - pruneRangeFn func(asynctypes.Event[BeaconBlockT]) (uint64, uint64), + subBeaconBlockFinalized chan events.Event[BeaconBlockT], + pruneRangeFn func(events.Event[BeaconBlockT]) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, PrunableT]{ logger: logger, @@ -90,7 +90,7 @@ func (p *pruner[_, PrunableT]) listen(ctx context.Context) { // onFinalizeBlock will prune the prunable store based on the received // finalized block event. func (p *pruner[BeaconBlockT, PrunableT]) onFinalizeBlock( - event asynctypes.Event[BeaconBlockT], + event events.Event[BeaconBlockT], ) { start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 02667ab60f..8058978930 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -31,7 +31,6 @@ import ( "time" "cosmossdk.io/log" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -40,7 +39,7 @@ import ( ) func pruneRangeFn[BlockT pruner.BeaconBlock]( - event async.Event[BlockT], + event events.Event[BlockT], ) (uint64, uint64) { slot := event.Data().GetSlot().Unwrap() return slot, slot @@ -72,7 +71,7 @@ func TestPruner(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { logger := log.NewNopLogger() - ch := make(chan async.Event[pruner.BeaconBlock]) + ch := make(chan events.Event[pruner.BeaconBlock]) mockPrunable := new(mocks.Prunable) mockPrunable.On("Prune", mock.Anything, mock.Anything). Return(nil) @@ -93,7 +92,7 @@ func TestPruner(t *testing.T) { for _, index := range tt.pruneIndexes { block := mocks.BeaconBlock{} block.On("GetSlot").Return(math.U64(index)) - event := async.NewEvent[pruner.BeaconBlock]( + event := events.NewEvent[pruner.BeaconBlock]( context.Background(), events.BeaconBlockFinalizedEvent, &block, diff --git a/mod/storage/pkg/pruner/types.go b/mod/storage/pkg/pruner/types.go index 0a3f288644..f12384252e 100644 --- a/mod/storage/pkg/pruner/types.go +++ b/mod/storage/pkg/pruner/types.go @@ -28,7 +28,7 @@ package pruner import ( "context" - async "github.com/berachain/beacon-kit/mod/async/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -39,10 +39,10 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { - Is(async.EventID) bool + Is(events.EventID) bool Data() BeaconBlockT Context() context.Context - ID() async.EventID + ID() events.EventID } // Prunable is an interface representing a store that can be pruned. From 00b4f2049cf59b25dd0daf72a7d0ce2a5eccb6d3 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sun, 18 Aug 2024 01:36:55 -0400 Subject: [PATCH 66/75] oops --- mod/beacon/blockchain/process.go | 2 +- mod/beacon/blockchain/service.go | 6 +++--- mod/beacon/validator/service.go | 4 ++-- mod/da/pkg/da/service.go | 2 +- mod/da/pkg/store/pruner_test.go | 2 +- mod/primitives/pkg/events/event.go | 4 ++-- mod/runtime/pkg/middleware/abci.go | 12 ++++++------ mod/storage/pkg/pruner/pruner_test.go | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index 11546d19d9..a132f85e37 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -83,7 +83,7 @@ func (s *Service[ // We won't send a fcu if the block is bad, should be addressed // via ticker later. if err = s.dispatcher.Publish( - events.NewEvent( + events.New( ctx, events.BeaconBlockFinalizedEvent, blk, ), ); err != nil { diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index fad8bff74e..cc72a8972e 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -236,7 +236,7 @@ func (s *Service[ // Emit the event containing the validator updates. if err := s.dispatcher.Publish( - events.NewEvent( + events.New( msg.Context(), events.GenesisDataProcessed, valUpdates, @@ -265,7 +265,7 @@ func (s *Service[ // emit a BeaconBlockVerified event with the error result from \ // VerifyIncomingBlock if err := s.dispatcher.Publish( - events.NewEvent( + events.New( msg.Context(), events.BeaconBlockVerified, msg.Data(), @@ -304,7 +304,7 @@ func (s *Service[ // Emit the event containing the validator updates. if err := s.dispatcher.Publish( - events.NewEvent( + events.New( msg.Context(), events.FinalValidatorUpdatesProcessed, valUpdates, diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index b68bcc906c..496c8c22e2 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -217,14 +217,14 @@ func (s *Service[ // emit a built block event with the built block and the error if bbErr := s.dispatcher.Publish( - events.NewEvent(req.Context(), events.BuiltBeaconBlock, blk, err), + events.New(req.Context(), events.BuiltBeaconBlock, blk, err), ); bbErr != nil { s.logger.Error("failed to dispatch built block", "err", err) } // emit a built sidecars event with the built sidecars and the error if scErr := s.dispatcher.Publish( - events.NewEvent(req.Context(), events.BuiltSidecars, sidecars, err), + events.New(req.Context(), events.BuiltSidecars, sidecars, err), ); scErr != nil { s.logger.Error("failed to dispatch built sidecars", "err", err) } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index a06be22bd6..7f4fd9bbe9 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -151,7 +151,7 @@ func (s *Service[_, BlobSidecarsT]) handleSidecarsReceived( // emit the sidecars verification event with error from verifySidecars if err := s.dispatcher.Publish( - events.NewEvent( + events.New( msg.Context(), events.SidecarsVerified, msg.Data(), sidecarsErr, ), ); err != nil { diff --git a/mod/da/pkg/store/pruner_test.go b/mod/da/pkg/store/pruner_test.go index c32a75290f..7058b0d5fa 100644 --- a/mod/da/pkg/store/pruner_test.go +++ b/mod/da/pkg/store/pruner_test.go @@ -118,7 +118,7 @@ func TestBuildPruneRangeFn(t *testing.T) { pruneFn := store.BuildPruneRangeFn[MockBeaconBlock]( cs, ) - event := events.NewEvent[MockBeaconBlock]( + event := events.New[MockBeaconBlock]( context.Background(), events.EventID("mock"), MockBeaconBlock{ diff --git a/mod/primitives/pkg/events/event.go b/mod/primitives/pkg/events/event.go index bb31b709e5..b7b060a6e6 100644 --- a/mod/primitives/pkg/events/event.go +++ b/mod/primitives/pkg/events/event.go @@ -41,8 +41,8 @@ type Event[DataT any] interface { Is(id EventID) bool } -// NewEvent creates a new Event with the given context and beacon event. -func NewEvent[ +// New creates a new Event with the given context and beacon event. +func New[ DataT any, ]( ctx context.Context, id EventID, data DataT, errs ...error, diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 5e36d9acb1..754808e297 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -60,7 +60,7 @@ func (h *ABCIMiddleware[ } if err = h.dispatcher.Publish( - events.NewEvent(ctx, events.GenesisDataReceived, *data), + events.New(ctx, events.GenesisDataReceived, *data), ); err != nil { return nil, err } @@ -106,7 +106,7 @@ func (h *ABCIMiddleware[ defer h.metrics.measurePrepareProposalDuration(startTime) if err = h.dispatcher.Publish( - events.NewEvent( + events.New( ctx, events.NewSlot, slotData, ), ); err != nil { @@ -217,7 +217,7 @@ func (h *ABCIMiddleware[ // notify that the beacon block has been received. if err = h.dispatcher.Publish( - events.NewEvent(ctx, events.BeaconBlockReceived, blk), + events.New(ctx, events.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -229,7 +229,7 @@ func (h *ABCIMiddleware[ // notify that the sidecars have been received. if err = h.dispatcher.Publish( - events.NewEvent(ctx, events.SidecarsReceived, sidecars), + events.New(ctx, events.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -327,14 +327,14 @@ func (h *ABCIMiddleware[ // notify that the final beacon block has been received. if err = h.dispatcher.Publish( - events.NewEvent(ctx, events.FinalBeaconBlockReceived, blk), + events.New(ctx, events.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } // notify that the final blob sidecars have been received. if err = h.dispatcher.Publish( - events.NewEvent(ctx, events.FinalSidecarsReceived, blobs), + events.New(ctx, events.FinalSidecarsReceived, blobs), ); err != nil { return nil, err } diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 8058978930..2aeb594ab8 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -92,7 +92,7 @@ func TestPruner(t *testing.T) { for _, index := range tt.pruneIndexes { block := mocks.BeaconBlock{} block.On("GetSlot").Return(math.U64(index)) - event := events.NewEvent[pruner.BeaconBlock]( + event := events.New[pruner.BeaconBlock]( context.Background(), events.BeaconBlockFinalizedEvent, &block, From 5bb13ebb4e4eb91905a204f6b6a9ba168a616005 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sun, 18 Aug 2024 01:43:12 -0400 Subject: [PATCH 67/75] tidy --- mod/storage/go.mod | 1 - mod/storage/go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/mod/storage/go.mod b/mod/storage/go.mod index ac6bfafb41..eef62015a3 100644 --- a/mod/storage/go.mod +++ b/mod/storage/go.mod @@ -55,7 +55,6 @@ require ( cosmossdk.io/x/tx v0.13.4-0.20240623110059-dec2d5583e39 // indirect github.com/DataDog/zstd v1.5.6 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cockroachdb/errors v1.11.3 // indirect diff --git a/mod/storage/go.sum b/mod/storage/go.sum index 8e90a65e2d..f589833038 100644 --- a/mod/storage/go.sum +++ b/mod/storage/go.sum @@ -51,8 +51,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd h1:YXZN5EGMFId+MlDU1HTo672c1+oAC2ubRzRJFriaNP4= -github.com/berachain/beacon-kit/mod/async v0.0.0-20240618214413-d5ec0e66b3dd/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db h1:vGczI1vJ6s86tSDS4tsllzlWZUVZ42xZ710GoHMd4to= github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240703145037-b5612ab256db/go.mod h1:rbvfJqTKUIckels2AlWy+XuG+UGnegoFQuHC+TUg+zA= github.com/berachain/beacon-kit/mod/errors v0.0.0-20240617161612-ab1257fcf5a1 h1:KlGloi0bl9DevoJPwFPyKcH7fXavgMhUQnwexvFNbY0= From aaf9bd6e35d4d85f70c0484af76020745a716dc9 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sun, 18 Aug 2024 18:46:55 -0400 Subject: [PATCH 68/75] delete async/event.go --- mod/async/pkg/types/event.go | 96 ----------------------- mod/beacon/blockchain/execution_engine.go | 2 - mod/beacon/blockchain/service.go | 4 +- 3 files changed, 2 insertions(+), 100 deletions(-) delete mode 100644 mod/async/pkg/types/event.go diff --git a/mod/async/pkg/types/event.go b/mod/async/pkg/types/event.go deleted file mode 100644 index d5d2c03c60..0000000000 --- a/mod/async/pkg/types/event.go +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package types - -import ( - "context" - "errors" -) - -// EventID represents the type of a message. -type EventID string - -// BaseEvent defines the minimal interface that the dispatcher expects from a -// message. -type BaseEvent interface { - ID() EventID - Context() context.Context -} - -// Event defines the interface that the underlying route expects from a -// message with data. -type Event[DataT any] interface { - BaseEvent - Data() DataT - Error() error - Is(id EventID) bool -} - -// NewEvent creates a new Event with the given context and beacon event. -func NewEvent[ - DataT any, -]( - ctx context.Context, id EventID, data DataT, errs ...error, -) Event[DataT] { - return &event[DataT]{ - ctx: ctx, - id: id, - data: data, - err: errors.Join(errs...), - } -} - -// An event is a hard type implementation of the Event interface. -type event[DataT any] struct { - // ctx is the context associated with the event. - ctx context.Context - // id is the name of the event. - id EventID - // event is the actual beacon event. - data DataT - // err is the error associated with the event. - err error -} - -// ID returns the ID of the event. -func (m *event[DataT]) ID() EventID { - return m.id -} - -// Context returns the context associated with the event. -func (m *event[DataT]) Context() context.Context { - return m.ctx -} - -// Data returns the data associated with the event. -func (m *event[DataT]) Data() DataT { - return m.data -} - -// Error returns the error associated with the event. -func (m *event[DataT]) Error() error { - return m.err -} - -// Is returns true if the event has the given type. -func (m *event[DataT]) Is(messageType EventID) bool { - return m.id == messageType -} diff --git a/mod/beacon/blockchain/execution_engine.go b/mod/beacon/blockchain/execution_engine.go index c1b53759a8..1b3d84d45c 100644 --- a/mod/beacon/blockchain/execution_engine.go +++ b/mod/beacon/blockchain/execution_engine.go @@ -53,8 +53,6 @@ func (s *Service[ // sendNextFCUWithAttributes sends a forkchoice update to the execution // client with attributes. -// - func (s *Service[ _, BeaconBlockT, _, _, BeaconStateT, _, _, ExecutionPayloadHeaderT, _, _, diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index cc72a8972e..d6b32d226c 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -208,7 +208,7 @@ func (s *Service[ case event := <-s.subBlockReceived: s.handleBeaconBlockReceived(event) case event := <-s.subFinalBlkReceived: - s.handleFinalBeaconBlockReceived(event) + s.handleBeaconBlockFinalization(event) } } } @@ -281,7 +281,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, -]) handleFinalBeaconBlockReceived( +]) handleBeaconBlockFinalization( msg events.Event[BeaconBlockT], ) { var ( From e3a7cef51b90ec811ad1f83854574711cb8a3049 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sun, 18 Aug 2024 19:07:40 -0400 Subject: [PATCH 69/75] rename events -> async --- mod/async/pkg/broker/broker.go | 14 +++---- mod/async/pkg/dispatcher/dispatcher.go | 10 ++--- mod/async/pkg/dispatcher/errors.go | 6 +-- mod/async/pkg/types/dispatcher.go | 10 ++--- mod/beacon/block_store/pruner.go | 6 +-- mod/beacon/block_store/service.go | 10 ++--- mod/beacon/block_store/types.go | 6 +-- mod/beacon/blockchain/process.go | 6 +-- mod/beacon/blockchain/service.go | 38 +++++++++---------- mod/beacon/validator/service.go | 14 +++---- mod/da/pkg/da/service.go | 22 +++++------ mod/da/pkg/store/pruner.go | 6 +-- mod/da/pkg/store/pruner_test.go | 6 +-- mod/execution/pkg/deposit/pruner.go | 6 +-- mod/execution/pkg/deposit/service.go | 10 ++--- mod/execution/pkg/deposit/sync.go | 4 +- mod/execution/pkg/deposit/types.go | 6 +-- .../pkg/components/availability_store.go | 6 +-- mod/node-core/pkg/components/block_store.go | 6 +-- mod/node-core/pkg/components/deposit_store.go | 6 +-- mod/node-core/pkg/components/events.go | 28 +++++++------- mod/node-core/pkg/components/types.go | 38 +++++++++---------- mod/primitives/pkg/{events => async}/event.go | 6 +-- mod/primitives/pkg/{events => async}/id.go | 2 +- mod/runtime/pkg/middleware/abci.go | 16 ++++---- mod/runtime/pkg/middleware/middleware.go | 38 +++++++++---------- mod/storage/pkg/manager/manager_test.go | 6 +-- mod/storage/pkg/manager/types.go | 6 +-- .../pkg/pruner/mocks/block_event.mock.go | 27 ++++++------- mod/storage/pkg/pruner/pruner.go | 12 +++--- mod/storage/pkg/pruner/pruner_test.go | 10 ++--- mod/storage/pkg/pruner/types.go | 6 +-- 32 files changed, 197 insertions(+), 196 deletions(-) rename mod/primitives/pkg/{events => async}/event.go (95%) rename mod/primitives/pkg/{events => async}/id.go (99%) diff --git a/mod/async/pkg/broker/broker.go b/mod/async/pkg/broker/broker.go index 62416276e6..9bccfaf211 100644 --- a/mod/async/pkg/broker/broker.go +++ b/mod/async/pkg/broker/broker.go @@ -25,13 +25,13 @@ import ( "sync" "time" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // Broker is responsible for broadcasting all events corresponding to the // to all registered client channels. -type Broker[T events.BaseEvent] struct { - eventID events.EventID +type Broker[T async.BaseEvent] struct { + eventID async.EventID // subscriptions is a map of subscribed subscriptions. subscriptions map[chan T]struct{} // msgs is the channel for publishing new messages. @@ -44,9 +44,9 @@ type Broker[T events.BaseEvent] struct { // New creates a new broker publishing events of type T for the // provided eventID. -func New[T events.BaseEvent](eventID string) *Broker[T] { +func New[T async.BaseEvent](eventID string) *Broker[T] { return &Broker[T]{ - eventID: events.EventID(eventID), + eventID: async.EventID(eventID), subscriptions: make(map[chan T]struct{}), msgs: make(chan T, defaultBufferSize), timeout: defaultBrokerTimeout, @@ -55,7 +55,7 @@ func New[T events.BaseEvent](eventID string) *Broker[T] { } // EventID returns the event ID that the broker is responsible for. -func (p *Broker[T]) EventID() events.EventID { +func (p *Broker[T]) EventID() async.EventID { return p.eventID } @@ -81,7 +81,7 @@ func (p *Broker[T]) start(ctx context.Context) { // Publish publishes a msg to all subscribers. // Returns ErrTimeout on timeout. -func (p *Broker[T]) Publish(msg events.BaseEvent) error { +func (p *Broker[T]) Publish(msg async.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { return err diff --git a/mod/async/pkg/dispatcher/dispatcher.go b/mod/async/pkg/dispatcher/dispatcher.go index f52380b760..2b8a0b7c95 100644 --- a/mod/async/pkg/dispatcher/dispatcher.go +++ b/mod/async/pkg/dispatcher/dispatcher.go @@ -25,7 +25,7 @@ import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) var _ types.Dispatcher = (*Dispatcher)(nil) @@ -33,7 +33,7 @@ var _ types.Dispatcher = (*Dispatcher)(nil) // Dispatcher faciliates asynchronous communication between components, // typically services. type Dispatcher struct { - brokers map[events.EventID]types.Broker + brokers map[async.EventID]types.Broker logger log.Logger[any] } @@ -42,13 +42,13 @@ func New( logger log.Logger[any], ) *Dispatcher { return &Dispatcher{ - brokers: make(map[events.EventID]types.Broker), + brokers: make(map[async.EventID]types.Broker), logger: logger, } } // Publish dispatches the given event to the broker with the given eventID. -func (d *Dispatcher) Publish(event events.BaseEvent) error { +func (d *Dispatcher) Publish(event async.BaseEvent) error { broker, ok := d.brokers[event.ID()] if !ok { return errBrokerNotFound(event.ID()) @@ -61,7 +61,7 @@ func (d *Dispatcher) Publish(event events.BaseEvent) error { // corresponding to the broker. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. -func (d *Dispatcher) Subscribe(eventID events.EventID, ch any) error { +func (d *Dispatcher) Subscribe(eventID async.EventID, ch any) error { broker, ok := d.brokers[eventID] if !ok { return errBrokerNotFound(eventID) diff --git a/mod/async/pkg/dispatcher/errors.go b/mod/async/pkg/dispatcher/errors.go index 3f85b20ce5..56a6126c66 100644 --- a/mod/async/pkg/dispatcher/errors.go +++ b/mod/async/pkg/dispatcher/errors.go @@ -22,21 +22,21 @@ package dispatcher import ( "github.com/berachain/beacon-kit/mod/errors" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) //nolint:gochecknoglobals // errors var ( ErrNotFound = errors.New("not found") ErrAlreadyExists = errors.New("already exists") - errBrokerNotFound = func(eventID events.EventID) error { + errBrokerNotFound = func(eventID async.EventID) error { return errors.Wrapf( ErrNotFound, "publisher not found for eventID: %s", eventID, ) } - errBrokerAlreadyExists = func(eventID events.EventID) error { + errBrokerAlreadyExists = func(eventID async.EventID) error { return errors.Wrapf( ErrAlreadyExists, "publisher already exists for eventID: %s", diff --git a/mod/async/pkg/types/dispatcher.go b/mod/async/pkg/types/dispatcher.go index f35f725858..c0dbb9ea68 100644 --- a/mod/async/pkg/types/dispatcher.go +++ b/mod/async/pkg/types/dispatcher.go @@ -23,7 +23,7 @@ package types import ( "context" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // Dispatcher is the full API for a dispatcher that facilitates the publishing @@ -42,12 +42,12 @@ type Dispatcher interface { // of async events. type EventDispatcher interface { // Publish publishes an event to the dispatcher. - Publish(event events.BaseEvent) error + Publish(event async.BaseEvent) error // Subscribe subscribes the given channel to all events with the given event // ID. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. - Subscribe(eventID events.EventID, ch any) error + Subscribe(eventID async.EventID, ch any) error // TODO: add unsubscribe } @@ -56,11 +56,11 @@ type Broker interface { // Start starts the event publisher. Start(ctx context.Context) // Publish publishes the given event to the event publisher. - Publish(event events.BaseEvent) error + Publish(event async.BaseEvent) error // Subscribe subscribes the given channel to the event publisher. Subscribe(ch any) error // Unsubscribe unsubscribes the given channel from the event publisher. Unsubscribe(ch any) error // EventID returns the event ID that the publisher is responsible for. - EventID() events.EventID + EventID() async.EventID } diff --git a/mod/beacon/block_store/pruner.go b/mod/beacon/block_store/pruner.go index f414f6dbfe..82e71dc61b 100644 --- a/mod/beacon/block_store/pruner.go +++ b/mod/beacon/block_store/pruner.go @@ -20,15 +20,15 @@ package blockstore import ( - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // BuildPruneRangeFn builds a function that returns the range of blocks to // prune. func BuildPruneRangeFn[BeaconBlockT BeaconBlock]( cfg Config, -) func(events.Event[BeaconBlockT]) (uint64, uint64) { - return func(event events.Event[BeaconBlockT]) (uint64, uint64) { +) func(async.Event[BeaconBlockT]) (uint64, uint64) { + return func(event async.Event[BeaconBlockT]) (uint64, uint64) { blk := event.Data() if blk.GetSlot().Unwrap() < cfg.AvailabilityWindow { return 1, 1 diff --git a/mod/beacon/block_store/service.go b/mod/beacon/block_store/service.go index 547ca94c42..14a73eff40 100644 --- a/mod/beacon/block_store/service.go +++ b/mod/beacon/block_store/service.go @@ -25,7 +25,7 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + async1 "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // Service is a Service that listens for blocks and stores them in a KVStore. @@ -42,7 +42,7 @@ type Service[ // store is the block store for the service. store BlockStoreT // subFinalizedBlkEvents is a channel for receiving finalized block events. - subFinalizedBlkEvents chan events.Event[BeaconBlockT] + subFinalizedBlkEvents chan async1.Event[BeaconBlockT] } // NewService creates a new block service. @@ -60,7 +60,7 @@ func NewService[ logger: logger, dispatcher: dispatcher, store: store, - subFinalizedBlkEvents: make(chan events.Event[BeaconBlockT]), + subFinalizedBlkEvents: make(chan async1.Event[BeaconBlockT]), } } @@ -78,7 +78,7 @@ func (s *Service[BeaconBlockT, _]) Start(ctx context.Context) error { // subscribe a channel to the finalized block events. if err := s.dispatcher.Subscribe( - events.BeaconBlockFinalizedEvent, s.subFinalizedBlkEvents, + async1.BeaconBlockFinalizedEvent, s.subFinalizedBlkEvents, ); err != nil { s.logger.Error("failed to subscribe to block events", "error", err) return err @@ -102,7 +102,7 @@ func (s *Service[BeaconBlockT, BlockStoreT]) eventLoop(ctx context.Context) { // onFinalizeBlock is triggered when a finalized block event is received. // It stores the block in the KVStore. func (s *Service[BeaconBlockT, _]) onFinalizeBlock( - event events.Event[BeaconBlockT], + event async1.Event[BeaconBlockT], ) { slot := event.Data().GetSlot() if err := s.store.Set(event.Data()); err != nil { diff --git a/mod/beacon/block_store/types.go b/mod/beacon/block_store/types.go index 06aeda980f..b722a43c52 100644 --- a/mod/beacon/block_store/types.go +++ b/mod/beacon/block_store/types.go @@ -21,8 +21,8 @@ package blockstore import ( + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -42,9 +42,9 @@ type BlockStore[BeaconBlockT BeaconBlock] interface { // Event is an interface for block events. type Event[BeaconBlockT BeaconBlock] interface { // ID returns the id of the event. - ID() events.EventID + ID() async.EventID // Is returns true if the event is of the given id. - Is(events.EventID) bool + Is(async.EventID) bool // Data returns the data of the event. Data() BeaconBlockT } diff --git a/mod/beacon/blockchain/process.go b/mod/beacon/blockchain/process.go index a132f85e37..f57c19fb7c 100644 --- a/mod/beacon/blockchain/process.go +++ b/mod/beacon/blockchain/process.go @@ -24,7 +24,7 @@ import ( "context" "time" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -83,8 +83,8 @@ func (s *Service[ // We won't send a fcu if the block is bad, should be addressed // via ticker later. if err = s.dispatcher.Publish( - events.New( - ctx, events.BeaconBlockFinalizedEvent, blk, + async.NewEvent( + ctx, async.BeaconBlockFinalizedEvent, blk, ), ); err != nil { return nil, err diff --git a/mod/beacon/blockchain/service.go b/mod/beacon/blockchain/service.go index d6b32d226c..fe3396763c 100644 --- a/mod/beacon/blockchain/service.go +++ b/mod/beacon/blockchain/service.go @@ -26,8 +26,8 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" + async1 "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -86,12 +86,12 @@ type Service[ // subFinalBlkReceived is a channel for receiving finalize beacon block // requests. - subFinalBlkReceived chan events.Event[BeaconBlockT] + subFinalBlkReceived chan async1.Event[BeaconBlockT] // subBlockReceived is a channel for receiving verify beacon block requests. - subBlockReceived chan events.Event[BeaconBlockT] + subBlockReceived chan async1.Event[BeaconBlockT] // subGenDataReceived is a subscription for receiving genesis data // received events. - subGenDataReceived chan events.Event[GenesisT] + subGenDataReceived chan async1.Event[GenesisT] } // NewService creates a new validator service. @@ -153,9 +153,9 @@ func NewService[ metrics: newChainMetrics(ts), optimisticPayloadBuilds: optimisticPayloadBuilds, forceStartupSyncOnce: new(sync.Once), - subFinalBlkReceived: make(chan events.Event[BeaconBlockT]), - subBlockReceived: make(chan events.Event[BeaconBlockT]), - subGenDataReceived: make(chan events.Event[GenesisT]), + subFinalBlkReceived: make(chan async1.Event[BeaconBlockT]), + subBlockReceived: make(chan async1.Event[BeaconBlockT]), + subGenDataReceived: make(chan async1.Event[GenesisT]), } } @@ -173,19 +173,19 @@ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, GenesisT, _, ]) Start(ctx context.Context) error { if err := s.dispatcher.Subscribe( - events.GenesisDataReceived, s.subGenDataReceived, + async1.GenesisDataReceived, s.subGenDataReceived, ); err != nil { return err } if err := s.dispatcher.Subscribe( - events.BeaconBlockReceived, s.subBlockReceived, + async1.BeaconBlockReceived, s.subBlockReceived, ); err != nil { return err } if err := s.dispatcher.Subscribe( - events.FinalBeaconBlockReceived, s.subFinalBlkReceived, + async1.FinalBeaconBlockReceived, s.subFinalBlkReceived, ); err != nil { return err } @@ -219,7 +219,7 @@ func (s *Service[ func (s *Service[ _, _, _, _, _, _, _, _, GenesisT, _, -]) handleGenDataReceived(msg events.Event[GenesisT]) { +]) handleGenDataReceived(msg async1.Event[GenesisT]) { var ( valUpdates transition.ValidatorUpdates genesisErr error @@ -236,9 +236,9 @@ func (s *Service[ // Emit the event containing the validator updates. if err := s.dispatcher.Publish( - events.New( + async1.NewEvent( msg.Context(), - events.GenesisDataProcessed, + async1.GenesisDataProcessed, valUpdates, genesisErr, ), @@ -254,7 +254,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, ]) handleBeaconBlockReceived( - msg events.Event[BeaconBlockT], + msg async1.Event[BeaconBlockT], ) { // If the block is nil, exit early. if msg.Error() != nil { @@ -265,9 +265,9 @@ func (s *Service[ // emit a BeaconBlockVerified event with the error result from \ // VerifyIncomingBlock if err := s.dispatcher.Publish( - events.New( + async1.NewEvent( msg.Context(), - events.BeaconBlockVerified, + async1.BeaconBlockVerified, msg.Data(), s.VerifyIncomingBlock(msg.Context(), msg.Data()), ), @@ -282,7 +282,7 @@ func (s *Service[ func (s *Service[ _, BeaconBlockT, _, _, _, _, _, _, _, _, ]) handleBeaconBlockFinalization( - msg events.Event[BeaconBlockT], + msg async1.Event[BeaconBlockT], ) { var ( valUpdates transition.ValidatorUpdates @@ -304,9 +304,9 @@ func (s *Service[ // Emit the event containing the validator updates. if err := s.dispatcher.Publish( - events.New( + async1.NewEvent( msg.Context(), - events.FinalValidatorUpdatesProcessed, + async1.FinalValidatorUpdatesProcessed, valUpdates, finalizeErr, ), diff --git a/mod/beacon/validator/service.go b/mod/beacon/validator/service.go index 496c8c22e2..dccf686729 100644 --- a/mod/beacon/validator/service.go +++ b/mod/beacon/validator/service.go @@ -25,9 +25,9 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" + async1 "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) @@ -89,7 +89,7 @@ type Service[ // metrics is a metrics collector. metrics *validatorMetrics // subNewSlot is a channel for new slot events. - subNewSlot chan events.Event[SlotDataT] + subNewSlot chan async1.Event[SlotDataT] } // NewService creates a new validator service. @@ -156,7 +156,7 @@ func NewService[ remotePayloadBuilders: remotePayloadBuilders, metrics: newValidatorMetrics(ts), dispatcher: dispatcher, - subNewSlot: make(chan events.Event[SlotDataT]), + subNewSlot: make(chan async1.Event[SlotDataT]), } } @@ -175,7 +175,7 @@ func (s *Service[ ctx context.Context, ) error { // subscribe to new slot events - err := s.dispatcher.Subscribe(events.NewSlot, s.subNewSlot) + err := s.dispatcher.Subscribe(async1.NewSlot, s.subNewSlot) if err != nil { return err } @@ -201,7 +201,7 @@ func (s *Service[_, _, _, _, _, _, _, _, _, _, _, _, SlotDataT]) eventLoop( // slot data and emits an event containing the built block and sidecars. func (s *Service[ _, BeaconBlockT, _, _, BlobSidecarsT, _, _, _, _, _, _, _, SlotDataT, -]) handleNewSlot(req events.Event[SlotDataT]) { +]) handleNewSlot(req async1.Event[SlotDataT]) { var ( blk BeaconBlockT sidecars BlobSidecarsT @@ -217,14 +217,14 @@ func (s *Service[ // emit a built block event with the built block and the error if bbErr := s.dispatcher.Publish( - events.New(req.Context(), events.BuiltBeaconBlock, blk, err), + async1.NewEvent(req.Context(), async1.BuiltBeaconBlock, blk, err), ); bbErr != nil { s.logger.Error("failed to dispatch built block", "err", err) } // emit a built sidecars event with the built sidecars and the error if scErr := s.dispatcher.Publish( - events.New(req.Context(), events.BuiltSidecars, sidecars, err), + async1.NewEvent(req.Context(), async1.BuiltSidecars, sidecars, err), ); scErr != nil { s.logger.Error("failed to dispatch built sidecars", "err", err) } diff --git a/mod/da/pkg/da/service.go b/mod/da/pkg/da/service.go index 7f4fd9bbe9..61029db397 100644 --- a/mod/da/pkg/da/service.go +++ b/mod/da/pkg/da/service.go @@ -25,7 +25,7 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + async1 "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // The Data Availability service is responsible for verifying and processing @@ -43,8 +43,8 @@ type Service[ ] dispatcher async.EventDispatcher logger log.Logger[any] - subSidecarsReceived chan events.Event[BlobSidecarsT] - subFinalBlobSidecars chan events.Event[BlobSidecarsT] + subSidecarsReceived chan async1.Event[BlobSidecarsT] + subFinalBlobSidecars chan async1.Event[BlobSidecarsT] } // NewService returns a new DA service. @@ -68,8 +68,8 @@ func NewService[ bp: bp, dispatcher: dispatcher, logger: logger, - subSidecarsReceived: make(chan events.Event[BlobSidecarsT]), - subFinalBlobSidecars: make(chan events.Event[BlobSidecarsT]), + subSidecarsReceived: make(chan async1.Event[BlobSidecarsT]), + subFinalBlobSidecars: make(chan async1.Event[BlobSidecarsT]), } } @@ -85,14 +85,14 @@ func (s *Service[_, BlobSidecarsT]) Start(ctx context.Context) error { // subscribe to SidecarsReceived events if err = s.dispatcher.Subscribe( - events.SidecarsReceived, s.subSidecarsReceived, + async1.SidecarsReceived, s.subSidecarsReceived, ); err != nil { return err } // subscribe to FinalSidecarsReceived events if err = s.dispatcher.Subscribe( - events.FinalSidecarsReceived, s.subFinalBlobSidecars, + async1.FinalSidecarsReceived, s.subFinalBlobSidecars, ); err != nil { return err } @@ -123,7 +123,7 @@ func (s *Service[_, BlobSidecarsT]) eventLoop(ctx context.Context) { // event. // It processes the sidecars and publishes a BlobSidecarsProcessed event. func (s *Service[_, BlobSidecarsT]) handleFinalSidecarsReceived( - msg events.Event[BlobSidecarsT], + msg async1.Event[BlobSidecarsT], ) { if err := s.processSidecars(msg.Context(), msg.Data()); err != nil { s.logger.Error( @@ -137,7 +137,7 @@ func (s *Service[_, BlobSidecarsT]) handleFinalSidecarsReceived( // handleSidecarsReceived handles the SidecarsVerifyRequest event. // It verifies the sidecars and publishes a SidecarsVerified event. func (s *Service[_, BlobSidecarsT]) handleSidecarsReceived( - msg events.Event[BlobSidecarsT], + msg async1.Event[BlobSidecarsT], ) { var sidecarsErr error // verify the sidecars. @@ -151,8 +151,8 @@ func (s *Service[_, BlobSidecarsT]) handleSidecarsReceived( // emit the sidecars verification event with error from verifySidecars if err := s.dispatcher.Publish( - events.New( - msg.Context(), events.SidecarsVerified, msg.Data(), sidecarsErr, + async1.NewEvent( + msg.Context(), async1.SidecarsVerified, msg.Data(), sidecarsErr, ), ); err != nil { s.logger.Error("failed to publish event", "err", err) diff --git a/mod/da/pkg/store/pruner.go b/mod/da/pkg/store/pruner.go index 206c0cab0f..a88a0a298e 100644 --- a/mod/da/pkg/store/pruner.go +++ b/mod/da/pkg/store/pruner.go @@ -21,14 +21,14 @@ package store import ( + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" ) func BuildPruneRangeFn[BeaconBlockT BeaconBlock]( cs common.ChainSpec, -) func(events.Event[BeaconBlockT]) (uint64, uint64) { - return func(event events.Event[BeaconBlockT]) (uint64, uint64) { +) func(async.Event[BeaconBlockT]) (uint64, uint64) { + return func(event async.Event[BeaconBlockT]) (uint64, uint64) { window := cs.MinEpochsForBlobsSidecarsRequest() * cs.SlotsPerEpoch() if event.Data().GetSlot().Unwrap() < window { return 0, 0 diff --git a/mod/da/pkg/store/pruner_test.go b/mod/da/pkg/store/pruner_test.go index 7058b0d5fa..6b3b12ee56 100644 --- a/mod/da/pkg/store/pruner_test.go +++ b/mod/da/pkg/store/pruner_test.go @@ -26,9 +26,9 @@ import ( "github.com/berachain/beacon-kit/mod/chain-spec/pkg/chain" "github.com/berachain/beacon-kit/mod/da/pkg/store" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/stretchr/testify/require" ) @@ -118,9 +118,9 @@ func TestBuildPruneRangeFn(t *testing.T) { pruneFn := store.BuildPruneRangeFn[MockBeaconBlock]( cs, ) - event := events.New[MockBeaconBlock]( + event := async.NewEvent[MockBeaconBlock]( context.Background(), - events.EventID("mock"), + async.EventID("mock"), MockBeaconBlock{ slot: tt.eventSlot, }, diff --git a/mod/execution/pkg/deposit/pruner.go b/mod/execution/pkg/deposit/pruner.go index c68d0cff04..c0bc068864 100644 --- a/mod/execution/pkg/deposit/pruner.go +++ b/mod/execution/pkg/deposit/pruner.go @@ -21,8 +21,8 @@ package deposit import ( + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -33,8 +33,8 @@ func BuildPruneRangeFn[ }, DepositT Deposit[DepositT, WithdrawalCredentialsT], WithdrawalCredentialsT any, -](cs common.ChainSpec) func(events.Event[BeaconBlockT]) (uint64, uint64) { - return func(event events.Event[BeaconBlockT]) (uint64, uint64) { +](cs common.ChainSpec) func(async.Event[BeaconBlockT]) (uint64, uint64) { + return func(event async.Event[BeaconBlockT]) (uint64, uint64) { deposits := event.Data().GetBody().GetDeposits() if len(deposits) == 0 || cs.MaxDepositsPerBlock() == 0 { return 0, 0 diff --git a/mod/execution/pkg/deposit/service.go b/mod/execution/pkg/deposit/service.go index 8568264689..934b5716a6 100644 --- a/mod/execution/pkg/deposit/service.go +++ b/mod/execution/pkg/deposit/service.go @@ -25,7 +25,7 @@ import ( async "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + async1 "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -48,7 +48,7 @@ type Service[ // dispatcher is the dispatcher for the service. dispatcher async.EventDispatcher // subFinalizedBlockEvents is the channel that provides finalized block events. - subFinalizedBlockEvents chan events.Event[BeaconBlockT] + subFinalizedBlockEvents chan async1.Event[BeaconBlockT] // metrics is the metrics for the deposit service. metrics *metrics // failedBlocks is a map of blocks that failed to be processed to be @@ -83,7 +83,7 @@ func NewService[ ds: ds, eth1FollowDistance: eth1FollowDistance, failedBlocks: make(map[math.Slot]struct{}), - subFinalizedBlockEvents: make(chan events.Event[BeaconBlockT]), + subFinalizedBlockEvents: make(chan async1.Event[BeaconBlockT]), logger: logger, metrics: newMetrics(telemetrySink), } @@ -94,10 +94,10 @@ func (s *Service[ _, _, _, _, _, ]) Start(ctx context.Context) error { if err := s.dispatcher.Subscribe( - events.BeaconBlockFinalizedEvent, s.subFinalizedBlockEvents, + async1.BeaconBlockFinalizedEvent, s.subFinalizedBlockEvents, ); err != nil { s.logger.Error("failed to subscribe to event", "event", - events.BeaconBlockFinalizedEvent, "err", err) + async1.BeaconBlockFinalizedEvent, "err", err) return err } diff --git a/mod/execution/pkg/deposit/sync.go b/mod/execution/pkg/deposit/sync.go index 901119b4d2..f87281016f 100644 --- a/mod/execution/pkg/deposit/sync.go +++ b/mod/execution/pkg/deposit/sync.go @@ -24,7 +24,7 @@ import ( "context" "time" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -35,7 +35,7 @@ const defaultRetryInterval = 20 * time.Second // event and fetches and stores the deposits for that block. func (s *Service[ BeaconBlockT, _, _, _, _, -]) depositFetcher(ctx context.Context, event events.Event[BeaconBlockT]) { +]) depositFetcher(ctx context.Context, event async.Event[BeaconBlockT]) { blockNum := event.Data().GetBody().GetExecutionPayload().GetNumber() s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance) } diff --git a/mod/execution/pkg/deposit/types.go b/mod/execution/pkg/deposit/types.go index 9c9de85567..6988aef84c 100644 --- a/mod/execution/pkg/deposit/types.go +++ b/mod/execution/pkg/deposit/types.go @@ -23,8 +23,8 @@ package deposit import ( "context" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -49,8 +49,8 @@ type BlockEvent[ BeaconBlockT BeaconBlock[BeaconBlockBodyT], ExecutionPayloadT ExecutionPayload, ] interface { - ID() events.EventID - Is(events.EventID) bool + ID() async.EventID + Is(async.EventID) bool Data() BeaconBlockT Context() context.Context } diff --git a/mod/node-core/pkg/components/availability_store.go b/mod/node-core/pkg/components/availability_store.go index d6c58995c3..da4a4e206e 100644 --- a/mod/node-core/pkg/components/availability_store.go +++ b/mod/node-core/pkg/components/availability_store.go @@ -27,8 +27,8 @@ import ( "cosmossdk.io/depinject" dastore "github.com/berachain/beacon-kit/mod/da/pkg/store" "github.com/berachain/beacon-kit/mod/log" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/storage/pkg/filedb" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -102,10 +102,10 @@ func ProvideAvailabilityPruner[ // create new subscription for finalized blocks. subFinalizedBlocks := make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( - events.BeaconBlockFinalizedEvent, subFinalizedBlocks, + async.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { in.Logger.Error("failed to subscribe to event", "event", - events.BeaconBlockFinalizedEvent, "err", err) + async.BeaconBlockFinalizedEvent, "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/block_store.go b/mod/node-core/pkg/components/block_store.go index 1a97a3f77e..e5b53c615b 100644 --- a/mod/node-core/pkg/components/block_store.go +++ b/mod/node-core/pkg/components/block_store.go @@ -28,8 +28,8 @@ import ( "github.com/berachain/beacon-kit/mod/config" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/storage/pkg/block" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -90,10 +90,10 @@ func ProvideBlockPruner[ // create new subscription for finalized blocks. subFinalizedBlocks := make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( - events.BeaconBlockFinalizedEvent, subFinalizedBlocks, + async.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { in.Logger.Error("failed to subscribe to event", "event", - events.BeaconBlockFinalizedEvent, "err", err) + async.BeaconBlockFinalizedEvent, "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/deposit_store.go b/mod/node-core/pkg/components/deposit_store.go index 2b8d925566..729977dc2f 100644 --- a/mod/node-core/pkg/components/deposit_store.go +++ b/mod/node-core/pkg/components/deposit_store.go @@ -26,8 +26,8 @@ import ( "github.com/berachain/beacon-kit/mod/execution/pkg/deposit" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/node-core/pkg/components/storage" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" depositstore "github.com/berachain/beacon-kit/mod/storage/pkg/deposit" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -77,10 +77,10 @@ func ProvideDepositPruner[ // initialize a subscription for finalized blocks. subFinalizedBlocks := make(chan FinalizedBlockEvent) if err := in.Dispatcher.Subscribe( - events.BeaconBlockFinalizedEvent, subFinalizedBlocks, + async.BeaconBlockFinalizedEvent, subFinalizedBlocks, ); err != nil { in.Logger.Error("failed to subscribe to event", "event", - events.BeaconBlockFinalizedEvent, "err", err) + async.BeaconBlockFinalizedEvent, "err", err) return nil, err } diff --git a/mod/node-core/pkg/components/events.go b/mod/node-core/pkg/components/events.go index 689da5f459..36c3b0b785 100644 --- a/mod/node-core/pkg/components/events.go +++ b/mod/node-core/pkg/components/events.go @@ -23,7 +23,7 @@ package components import ( "github.com/berachain/beacon-kit/mod/async/pkg/broker" asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // ProvidePublishers provides a publisher for beacon block @@ -31,43 +31,43 @@ import ( func ProvidePublishers() []asynctypes.Broker { return []asynctypes.Broker{ broker.New[GenesisDataReceivedEvent]( - events.GenesisDataReceived, + async.GenesisDataReceived, ), broker.New[GenesisDataProcessedEvent]( - events.GenesisDataProcessed, + async.GenesisDataProcessed, ), broker.New[NewSlotEvent]( - events.NewSlot, + async.NewSlot, ), broker.New[BuiltBeaconBlockEvent]( - events.BuiltBeaconBlock, + async.BuiltBeaconBlock, ), broker.New[BuiltSidecarsEvent]( - events.BuiltSidecars, + async.BuiltSidecars, ), broker.New[BeaconBlockReceivedEvent]( - events.BeaconBlockReceived, + async.BeaconBlockReceived, ), broker.New[SidecarsReceivedEvent]( - events.SidecarsReceived, + async.SidecarsReceived, ), broker.New[BeaconBlockVerifiedEvent]( - events.BeaconBlockVerified, + async.BeaconBlockVerified, ), broker.New[SidecarsVerifiedEvent]( - events.SidecarsVerified, + async.SidecarsVerified, ), broker.New[FinalBeaconBlockReceivedEvent]( - events.FinalBeaconBlockReceived, + async.FinalBeaconBlockReceived, ), broker.New[FinalSidecarsReceivedEvent]( - events.FinalSidecarsReceived, + async.FinalSidecarsReceived, ), broker.New[FinalValidatorUpdatesProcessedEvent]( - events.FinalValidatorUpdatesProcessed, + async.FinalValidatorUpdatesProcessed, ), broker.New[FinalizedBlockEvent]( - events.BeaconBlockFinalizedEvent, + async.BeaconBlockFinalizedEvent, ), } } diff --git a/mod/node-core/pkg/components/types.go b/mod/node-core/pkg/components/types.go index 9b5bd05c66..545f791cd2 100644 --- a/mod/node-core/pkg/components/types.go +++ b/mod/node-core/pkg/components/types.go @@ -54,7 +54,7 @@ import ( nodetypes "github.com/berachain/beacon-kit/mod/node-core/pkg/types" "github.com/berachain/beacon-kit/mod/payload/pkg/attributes" payloadbuilder "github.com/berachain/beacon-kit/mod/payload/pkg/builder" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/service" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/middleware" @@ -423,61 +423,61 @@ type ( type ( // GenesisDataReceivedEvent is a type alias for the genesis data received event. - GenesisDataReceivedEvent = events.Event[*Genesis] + GenesisDataReceivedEvent = async.Event[*Genesis] // GenesisDataProcessedEvent is a type alias for the genesis data processed event. - GenesisDataProcessedEvent = events.Event[transition.ValidatorUpdates] + GenesisDataProcessedEvent = async.Event[transition.ValidatorUpdates] // NewSlotEvent is a type alias for the new slot event. - NewSlotEvent = events.Event[*SlotData] + NewSlotEvent = async.Event[*SlotData] // BuiltBeaconBlockEvent is a type alias for the built beacon block event. - BuiltBeaconBlockEvent = events.Event[*BeaconBlock] + BuiltBeaconBlockEvent = async.Event[*BeaconBlock] // BuiltSidecarsEvent is a type alias for the built sidecars event. - BuiltSidecarsEvent = events.Event[*BlobSidecars] + BuiltSidecarsEvent = async.Event[*BlobSidecars] // BeaconBlockReceivedEvent is a type alias for the beacon block received event. - BeaconBlockReceivedEvent = events.Event[*BeaconBlock] + BeaconBlockReceivedEvent = async.Event[*BeaconBlock] // SidecarsReceivedEvent is a type alias for the sidecars received event. - SidecarsReceivedEvent = events.Event[*BlobSidecars] + SidecarsReceivedEvent = async.Event[*BlobSidecars] // BeaconBlockVerifiedEvent is a type alias for the beacon block verified event. - BeaconBlockVerifiedEvent = events.Event[*BeaconBlock] + BeaconBlockVerifiedEvent = async.Event[*BeaconBlock] // SidecarsVerifiedEvent is a type alias for the sidecars verified event. - SidecarsVerifiedEvent = events.Event[*BlobSidecars] + SidecarsVerifiedEvent = async.Event[*BlobSidecars] // FinalBeaconBlockReceivedEvent is a type alias for the final beacon block received event. - FinalBeaconBlockReceivedEvent = events.Event[*BeaconBlock] + FinalBeaconBlockReceivedEvent = async.Event[*BeaconBlock] // FinalSidecarsReceivedEvent is a type alias for the final sidecars received event. - FinalSidecarsReceivedEvent = events.Event[*BlobSidecars] + FinalSidecarsReceivedEvent = async.Event[*BlobSidecars] // FinalValidatorUpdatesProcessedEvent is a type alias for the final validator updates processed event. - FinalValidatorUpdatesProcessedEvent = events.Event[transition.ValidatorUpdates] + FinalValidatorUpdatesProcessedEvent = async.Event[transition.ValidatorUpdates] // FinalizedBlockEvent is a type alias for the block event. - FinalizedBlockEvent = events.Event[*BeaconBlock] + FinalizedBlockEvent = async.Event[*BeaconBlock] ) // Messages. type ( // BlockMessage is a type alias for the block message. - BlockMessage = events.Event[*BeaconBlock] + BlockMessage = async.Event[*BeaconBlock] // GenesisMessage is a type alias for the genesis message. - GenesisMessage = events.Event[*Genesis] + GenesisMessage = async.Event[*Genesis] // SidecarMessage is a type alias for the sidecar message. - SidecarMessage = events.Event[*BlobSidecars] + SidecarMessage = async.Event[*BlobSidecars] // SlotMessage is a type alias for the slot message. - SlotMessage = events.Event[*SlotData] + SlotMessage = async.Event[*SlotData] // StatusMessage is a type alias for the status message. - StatusMessage = events.Event[*service.StatusEvent] + StatusMessage = async.Event[*service.StatusEvent] ) /* -------------------------------------------------------------------------- */ diff --git a/mod/primitives/pkg/events/event.go b/mod/primitives/pkg/async/event.go similarity index 95% rename from mod/primitives/pkg/events/event.go rename to mod/primitives/pkg/async/event.go index b7b060a6e6..68f1cf2781 100644 --- a/mod/primitives/pkg/events/event.go +++ b/mod/primitives/pkg/async/event.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package events +package async import ( "context" @@ -41,8 +41,8 @@ type Event[DataT any] interface { Is(id EventID) bool } -// New creates a new Event with the given context and beacon event. -func New[ +// NewEvent creates a new Event with the given context and beacon event. +func NewEvent[ DataT any, ]( ctx context.Context, id EventID, data DataT, errs ...error, diff --git a/mod/primitives/pkg/events/id.go b/mod/primitives/pkg/async/id.go similarity index 99% rename from mod/primitives/pkg/events/id.go rename to mod/primitives/pkg/async/id.go index db5baaf1d2..69c77fe10c 100644 --- a/mod/primitives/pkg/events/id.go +++ b/mod/primitives/pkg/async/id.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package events +package async // EventID represents the type of a message. type EventID string diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index 754808e297..a9de5fa5ec 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -25,8 +25,8 @@ import ( "time" "github.com/berachain/beacon-kit/mod/errors" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" @@ -60,7 +60,7 @@ func (h *ABCIMiddleware[ } if err = h.dispatcher.Publish( - events.New(ctx, events.GenesisDataReceived, *data), + async.NewEvent(ctx, async.GenesisDataReceived, *data), ); err != nil { return nil, err } @@ -106,8 +106,8 @@ func (h *ABCIMiddleware[ defer h.metrics.measurePrepareProposalDuration(startTime) if err = h.dispatcher.Publish( - events.New( - ctx, events.NewSlot, slotData, + async.NewEvent( + ctx, async.NewSlot, slotData, ), ); err != nil { return nil, nil, err @@ -217,7 +217,7 @@ func (h *ABCIMiddleware[ // notify that the beacon block has been received. if err = h.dispatcher.Publish( - events.New(ctx, events.BeaconBlockReceived, blk), + async.NewEvent(ctx, async.BeaconBlockReceived, blk), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -229,7 +229,7 @@ func (h *ABCIMiddleware[ // notify that the sidecars have been received. if err = h.dispatcher.Publish( - events.New(ctx, events.SidecarsReceived, sidecars), + async.NewEvent(ctx, async.SidecarsReceived, sidecars), ); err != nil { return h.createProcessProposalResponse(errors.WrapNonFatal(err)) } @@ -327,14 +327,14 @@ func (h *ABCIMiddleware[ // notify that the final beacon block has been received. if err = h.dispatcher.Publish( - events.New(ctx, events.FinalBeaconBlockReceived, blk), + async.NewEvent(ctx, async.FinalBeaconBlockReceived, blk), ); err != nil { return nil, err } // notify that the final blob sidecars have been received. if err = h.dispatcher.Publish( - events.New(ctx, events.FinalSidecarsReceived, blobs), + async.NewEvent(ctx, async.FinalSidecarsReceived, blobs), ); err != nil { return nil, err } diff --git a/mod/runtime/pkg/middleware/middleware.go b/mod/runtime/pkg/middleware/middleware.go index 22b23eb8db..5c3d6a12b9 100644 --- a/mod/runtime/pkg/middleware/middleware.go +++ b/mod/runtime/pkg/middleware/middleware.go @@ -27,9 +27,9 @@ import ( "github.com/berachain/beacon-kit/mod/async/pkg/types" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/p2p" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" "github.com/berachain/beacon-kit/mod/runtime/pkg/encoding" rp2p "github.com/berachain/beacon-kit/mod/runtime/pkg/p2p" ) @@ -68,12 +68,12 @@ type ABCIMiddleware[ // logger is the logger for the middleware. logger log.Logger[any] // subscription channels - subGenDataProcessed chan events.Event[validatorUpdates] - subBuiltBeaconBlock chan events.Event[BeaconBlockT] - subBuiltSidecars chan events.Event[BlobSidecarsT] - subBBVerified chan events.Event[BeaconBlockT] - subSCVerified chan events.Event[BlobSidecarsT] - subFinalValidatorUpdates chan events.Event[validatorUpdates] + subGenDataProcessed chan async.Event[validatorUpdates] + subBuiltBeaconBlock chan async.Event[BeaconBlockT] + subBuiltSidecars chan async.Event[BlobSidecarsT] + subBBVerified chan async.Event[BeaconBlockT] + subSCVerified chan async.Event[BlobSidecarsT] + subFinalValidatorUpdates chan async.Event[validatorUpdates] } // NewABCIMiddleware creates a new instance of the Handler struct. @@ -111,12 +111,12 @@ func NewABCIMiddleware[ logger: logger, metrics: newABCIMiddlewareMetrics(telemetrySink), dispatcher: dispatcher, - subGenDataProcessed: make(chan events.Event[validatorUpdates]), - subBuiltBeaconBlock: make(chan events.Event[BeaconBlockT]), - subBuiltSidecars: make(chan events.Event[BlobSidecarsT]), - subBBVerified: make(chan events.Event[BeaconBlockT]), - subSCVerified: make(chan events.Event[BlobSidecarsT]), - subFinalValidatorUpdates: make(chan events.Event[validatorUpdates]), + subGenDataProcessed: make(chan async.Event[validatorUpdates]), + subBuiltBeaconBlock: make(chan async.Event[BeaconBlockT]), + subBuiltSidecars: make(chan async.Event[BlobSidecarsT]), + subBBVerified: make(chan async.Event[BeaconBlockT]), + subSCVerified: make(chan async.Event[BlobSidecarsT]), + subFinalValidatorUpdates: make(chan async.Event[validatorUpdates]), } } @@ -126,32 +126,32 @@ func (am *ABCIMiddleware[_, _, _, _]) Start( ) error { var err error if err = am.dispatcher.Subscribe( - events.GenesisDataProcessed, am.subGenDataProcessed, + async.GenesisDataProcessed, am.subGenDataProcessed, ); err != nil { return err } if err = am.dispatcher.Subscribe( - events.BuiltBeaconBlock, am.subBuiltBeaconBlock, + async.BuiltBeaconBlock, am.subBuiltBeaconBlock, ); err != nil { return err } if err = am.dispatcher.Subscribe( - events.BuiltSidecars, am.subBuiltSidecars, + async.BuiltSidecars, am.subBuiltSidecars, ); err != nil { return err } if err = am.dispatcher.Subscribe( - events.BeaconBlockVerified, am.subBBVerified, + async.BeaconBlockVerified, am.subBBVerified, ); err != nil { return err } if err = am.dispatcher.Subscribe( - events.SidecarsVerified, am.subSCVerified, + async.SidecarsVerified, am.subSCVerified, ); err != nil { return err } if err = am.dispatcher.Subscribe( - events.FinalValidatorUpdatesProcessed, am.subFinalValidatorUpdates, + async.FinalValidatorUpdatesProcessed, am.subFinalValidatorUpdates, ); err != nil { return err } diff --git a/mod/storage/pkg/manager/manager_test.go b/mod/storage/pkg/manager/manager_test.go index 0ad82d8b12..d54e5ca984 100644 --- a/mod/storage/pkg/manager/manager_test.go +++ b/mod/storage/pkg/manager/manager_test.go @@ -31,7 +31,7 @@ import ( "time" "cosmossdk.io/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/storage/pkg/manager" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -40,9 +40,9 @@ import ( func TestDBManager_Start(t *testing.T) { mockPrunable := new(mocks.Prunable) - ch := make(chan events.Event[manager.BeaconBlock]) + ch := make(chan async.Event[manager.BeaconBlock]) pruneParamsFn := func( - _ events.Event[manager.BeaconBlock], + _ async.Event[manager.BeaconBlock], ) (uint64, uint64) { return 0, 0 } diff --git a/mod/storage/pkg/manager/types.go b/mod/storage/pkg/manager/types.go index 2eff00dcc7..fb50b114eb 100644 --- a/mod/storage/pkg/manager/types.go +++ b/mod/storage/pkg/manager/types.go @@ -28,7 +28,7 @@ package manager import ( "context" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -39,8 +39,8 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { - Is(events.EventID) bool - ID() events.EventID + Is(async.EventID) bool + ID() async.EventID Data() BeaconBlockT Context() context.Context } diff --git a/mod/storage/pkg/pruner/mocks/block_event.mock.go b/mod/storage/pkg/pruner/mocks/block_event.mock.go index 4ef0b9960e..93e23bc1be 100644 --- a/mod/storage/pkg/pruner/mocks/block_event.mock.go +++ b/mod/storage/pkg/pruner/mocks/block_event.mock.go @@ -5,7 +5,8 @@ package mocks import ( context "context" - events "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + async "github.com/berachain/beacon-kit/mod/primitives/pkg/async" + mock "github.com/stretchr/testify/mock" pruner "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" @@ -117,18 +118,18 @@ func (_c *BlockEvent_Data_Call[BeaconBlockT]) RunAndReturn(run func() BeaconBloc } // ID provides a mock function with given fields: -func (_m *BlockEvent[BeaconBlockT]) ID() events.EventID { +func (_m *BlockEvent[BeaconBlockT]) ID() async.EventID { ret := _m.Called() if len(ret) == 0 { panic("no return value specified for ID") } - var r0 events.EventID - if rf, ok := ret.Get(0).(func() events.EventID); ok { + var r0 async.EventID + if rf, ok := ret.Get(0).(func() async.EventID); ok { r0 = rf() } else { - r0 = ret.Get(0).(events.EventID) + r0 = ret.Get(0).(async.EventID) } return r0 @@ -151,18 +152,18 @@ func (_c *BlockEvent_ID_Call[BeaconBlockT]) Run(run func()) *BlockEvent_ID_Call[ return _c } -func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 events.EventID) *BlockEvent_ID_Call[BeaconBlockT] { +func (_c *BlockEvent_ID_Call[BeaconBlockT]) Return(_a0 async.EventID) *BlockEvent_ID_Call[BeaconBlockT] { _c.Call.Return(_a0) return _c } -func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() events.EventID) *BlockEvent_ID_Call[BeaconBlockT] { +func (_c *BlockEvent_ID_Call[BeaconBlockT]) RunAndReturn(run func() async.EventID) *BlockEvent_ID_Call[BeaconBlockT] { _c.Call.Return(run) return _c } // Is provides a mock function with given fields: _a0 -func (_m *BlockEvent[BeaconBlockT]) Is(_a0 events.EventID) bool { +func (_m *BlockEvent[BeaconBlockT]) Is(_a0 async.EventID) bool { ret := _m.Called(_a0) if len(ret) == 0 { @@ -170,7 +171,7 @@ func (_m *BlockEvent[BeaconBlockT]) Is(_a0 events.EventID) bool { } var r0 bool - if rf, ok := ret.Get(0).(func(events.EventID) bool); ok { + if rf, ok := ret.Get(0).(func(async.EventID) bool); ok { r0 = rf(_a0) } else { r0 = ret.Get(0).(bool) @@ -185,14 +186,14 @@ type BlockEvent_Is_Call[BeaconBlockT pruner.BeaconBlock] struct { } // Is is a helper method to define mock.On call -// - _a0 events.EventID +// - _a0 async.EventID func (_e *BlockEvent_Expecter[BeaconBlockT]) Is(_a0 interface{}) *BlockEvent_Is_Call[BeaconBlockT] { return &BlockEvent_Is_Call[BeaconBlockT]{Call: _e.mock.On("Is", _a0)} } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 events.EventID)) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) Run(run func(_a0 async.EventID)) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(events.EventID)) + run(args[0].(async.EventID)) }) return _c } @@ -202,7 +203,7 @@ func (_c *BlockEvent_Is_Call[BeaconBlockT]) Return(_a0 bool) *BlockEvent_Is_Call return _c } -func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(events.EventID) bool) *BlockEvent_Is_Call[BeaconBlockT] { +func (_c *BlockEvent_Is_Call[BeaconBlockT]) RunAndReturn(run func(async.EventID) bool) *BlockEvent_Is_Call[BeaconBlockT] { _c.Call.Return(run) return _c } diff --git a/mod/storage/pkg/pruner/pruner.go b/mod/storage/pkg/pruner/pruner.go index ab1ed92461..566d5f5ebf 100644 --- a/mod/storage/pkg/pruner/pruner.go +++ b/mod/storage/pkg/pruner/pruner.go @@ -30,7 +30,7 @@ import ( "context" "github.com/berachain/beacon-kit/mod/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" ) // Compile-time check to ensure pruner implements the Pruner interface. @@ -45,8 +45,8 @@ type pruner[ prunable Prunable logger log.Logger[any] name string - subBeaconBlockFinalized chan events.Event[BeaconBlockT] - pruneRangeFn func(events.Event[BeaconBlockT]) (uint64, uint64) + subBeaconBlockFinalized chan async.Event[BeaconBlockT] + pruneRangeFn func(async.Event[BeaconBlockT]) (uint64, uint64) } // NewPruner creates a new Pruner. @@ -57,8 +57,8 @@ func NewPruner[ logger log.Logger[any], prunable Prunable, name string, - subBeaconBlockFinalized chan events.Event[BeaconBlockT], - pruneRangeFn func(events.Event[BeaconBlockT]) (uint64, uint64), + subBeaconBlockFinalized chan async.Event[BeaconBlockT], + pruneRangeFn func(async.Event[BeaconBlockT]) (uint64, uint64), ) Pruner[PrunableT] { return &pruner[BeaconBlockT, PrunableT]{ logger: logger, @@ -90,7 +90,7 @@ func (p *pruner[_, PrunableT]) listen(ctx context.Context) { // onFinalizeBlock will prune the prunable store based on the received // finalized block event. func (p *pruner[BeaconBlockT, PrunableT]) onFinalizeBlock( - event events.Event[BeaconBlockT], + event async.Event[BeaconBlockT], ) { start, end := p.pruneRangeFn(event) if err := p.prunable.Prune(start, end); err != nil { diff --git a/mod/storage/pkg/pruner/pruner_test.go b/mod/storage/pkg/pruner/pruner_test.go index 2aeb594ab8..610ad1ec11 100644 --- a/mod/storage/pkg/pruner/pruner_test.go +++ b/mod/storage/pkg/pruner/pruner_test.go @@ -31,7 +31,7 @@ import ( "time" "cosmossdk.io/log" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner" "github.com/berachain/beacon-kit/mod/storage/pkg/pruner/mocks" @@ -39,7 +39,7 @@ import ( ) func pruneRangeFn[BlockT pruner.BeaconBlock]( - event events.Event[BlockT], + event async.Event[BlockT], ) (uint64, uint64) { slot := event.Data().GetSlot().Unwrap() return slot, slot @@ -71,7 +71,7 @@ func TestPruner(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { logger := log.NewNopLogger() - ch := make(chan events.Event[pruner.BeaconBlock]) + ch := make(chan async.Event[pruner.BeaconBlock]) mockPrunable := new(mocks.Prunable) mockPrunable.On("Prune", mock.Anything, mock.Anything). Return(nil) @@ -92,9 +92,9 @@ func TestPruner(t *testing.T) { for _, index := range tt.pruneIndexes { block := mocks.BeaconBlock{} block.On("GetSlot").Return(math.U64(index)) - event := events.New[pruner.BeaconBlock]( + event := async.NewEvent[pruner.BeaconBlock]( context.Background(), - events.BeaconBlockFinalizedEvent, + async.BeaconBlockFinalizedEvent, &block, ) ch <- event diff --git a/mod/storage/pkg/pruner/types.go b/mod/storage/pkg/pruner/types.go index f12384252e..1080a79116 100644 --- a/mod/storage/pkg/pruner/types.go +++ b/mod/storage/pkg/pruner/types.go @@ -28,7 +28,7 @@ package pruner import ( "context" - "github.com/berachain/beacon-kit/mod/primitives/pkg/events" + "github.com/berachain/beacon-kit/mod/primitives/pkg/async" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -39,10 +39,10 @@ type BeaconBlock interface { // BlockEvent is an interface for block events. type BlockEvent[BeaconBlockT BeaconBlock] interface { - Is(events.EventID) bool + Is(async.EventID) bool Data() BeaconBlockT Context() context.Context - ID() events.EventID + ID() async.EventID } // Prunable is an interface representing a store that can be pruned. From 373e7fa80d5f0e595c22edec4d82a0cee4ba270d Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Mon, 19 Aug 2024 15:54:49 -0400 Subject: [PATCH 70/75] merge --- mod/runtime/pkg/middleware/types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/mod/runtime/pkg/middleware/types.go b/mod/runtime/pkg/middleware/types.go index 871de1ea37..369e783726 100644 --- a/mod/runtime/pkg/middleware/types.go +++ b/mod/runtime/pkg/middleware/types.go @@ -25,6 +25,7 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" + "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" ) // BeaconBlock is an interface for accessing the beacon block. From f962cb88c3be760fc436d2ad623578cbae3d7c28 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 20 Aug 2024 12:34:07 -0400 Subject: [PATCH 71/75] allow concurrent notification attempts --- mod/async/pkg/broker/broker.go | 71 ++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/mod/async/pkg/broker/broker.go b/mod/async/pkg/broker/broker.go index 9bccfaf211..06f769550e 100644 --- a/mod/async/pkg/broker/broker.go +++ b/mod/async/pkg/broker/broker.go @@ -31,6 +31,8 @@ import ( // Broker is responsible for broadcasting all events corresponding to the // to all registered client channels. type Broker[T async.BaseEvent] struct { + // eventID is a unique identifier for the event that this broker is + // responsible for. eventID async.EventID // subscriptions is a map of subscribed subscriptions. subscriptions map[chan T]struct{} @@ -55,40 +57,40 @@ func New[T async.BaseEvent](eventID string) *Broker[T] { } // EventID returns the event ID that the broker is responsible for. -func (p *Broker[T]) EventID() async.EventID { - return p.eventID +func (b *Broker[T]) EventID() async.EventID { + return b.eventID } // Start starts the broker loop. -func (p *Broker[T]) Start(ctx context.Context) { - go p.start(ctx) +func (b *Broker[T]) Start(ctx context.Context) { + go b.start(ctx) } // start starts the broker loop. -func (p *Broker[T]) start(ctx context.Context) { +func (b *Broker[T]) start(ctx context.Context) { for { select { case <-ctx.Done(): // close all leftover clients and break the broker loop - p.shutdown() + b.shutdown() return - case msg := <-p.msgs: + case msg := <-b.msgs: // broadcast published msg to all clients - p.broadcast(msg) + b.broadcast(msg) } } } // Publish publishes a msg to all subscribers. // Returns ErrTimeout on timeout. -func (p *Broker[T]) Publish(msg async.BaseEvent) error { +func (b *Broker[T]) Publish(msg async.BaseEvent) error { typedMsg, err := ensureType[T](msg) if err != nil { return err } ctx := msg.Context() select { - case p.msgs <- typedMsg: + case b.msgs <- typedMsg: return nil case <-ctx.Done(): return ctx.Err() @@ -99,46 +101,59 @@ func (p *Broker[T]) Publish(msg async.BaseEvent) error { // Returns ErrTimeout on timeout. // Contract: the channel must be a Subscription[T], where T is the expected // type of the event data. -func (p *Broker[T]) Subscribe(ch any) error { +func (b *Broker[T]) Subscribe(ch any) error { client, err := ensureType[chan T](ch) if err != nil { return err } - p.mu.Lock() - defer p.mu.Unlock() - p.subscriptions[client] = struct{}{} + b.mu.Lock() + defer b.mu.Unlock() + b.subscriptions[client] = struct{}{} return nil } // Unsubscribe removes a client from the broker. // Returns an error if the provided channel is not of type chan T. -func (p *Broker[T]) Unsubscribe(ch any) error { +func (b *Broker[T]) Unsubscribe(ch any) error { client, err := ensureType[chan T](ch) if err != nil { return err } - p.mu.Lock() - defer p.mu.Unlock() - delete(p.subscriptions, client) + b.mu.Lock() + defer b.mu.Unlock() + delete(b.subscriptions, client) close(client) return nil } // broadcast broadcasts a msg to all clients. -func (p *Broker[T]) broadcast(msg T) { - for client := range p.subscriptions { - // send msg to client (or discard msg after timeout) - select { - case client <- msg: - case <-time.After(p.timeout): - } +func (b *Broker[T]) broadcast(msg T) { + // create separate slice to avoid holding the lock during the entire + // broadcast + b.mu.Lock() + clients := make([]chan T, 0, len(b.subscriptions)) + for client := range b.subscriptions { + clients = append(clients, client) + } + b.mu.Unlock() + + // launch a goroutine for each client to allow for concurrent notification + // attempts, while respecting the timeout for each client individually + for _, client := range clients { + go func(c chan T) { + select { + case c <- msg: + case <-time.After(b.timeout): + // discard msg after timeout + } + }(client) } } // shutdown closes all leftover clients. -func (p *Broker[T]) shutdown() { - for client := range p.subscriptions { - if err := p.Unsubscribe(client); err != nil { +func (b *Broker[T]) shutdown() { + for client := range b.subscriptions { + if err := b.Unsubscribe(client); err != nil { panic(err) } } From e67ef72d8e705e127e6474199347ecffa1cb1de2 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 20 Aug 2024 12:48:21 -0400 Subject: [PATCH 72/75] bet --- mod/async/pkg/broker/broker.go | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/mod/async/pkg/broker/broker.go b/mod/async/pkg/broker/broker.go index 06f769550e..554c37ea69 100644 --- a/mod/async/pkg/broker/broker.go +++ b/mod/async/pkg/broker/broker.go @@ -126,27 +126,16 @@ func (b *Broker[T]) Unsubscribe(ch any) error { return nil } -// broadcast broadcasts a msg to all clients. func (b *Broker[T]) broadcast(msg T) { - // create separate slice to avoid holding the lock during the entire - // broadcast - b.mu.Lock() - clients := make([]chan T, 0, len(b.subscriptions)) for client := range b.subscriptions { - clients = append(clients, client) - } - b.mu.Unlock() - - // launch a goroutine for each client to allow for concurrent notification - // attempts, while respecting the timeout for each client individually - for _, client := range clients { - go func(c chan T) { - select { - case c <- msg: - case <-time.After(b.timeout): - // discard msg after timeout - } - }(client) + // send msg to client (or discard msg after timeout) + // we could consider using a go routine for each client to allow + // for concurrent notification attempts, while respecting the timeout + // for each client individually + select { + case client <- msg: + case <-time.After(b.timeout): + } } } From 117a69ae3d91870fdb604b4687866b2562161211 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 20 Aug 2024 13:19:12 -0400 Subject: [PATCH 73/75] errors --- mod/runtime/pkg/middleware/abci.go | 12 ++++++------ mod/runtime/pkg/middleware/errors.go | 29 ++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/mod/runtime/pkg/middleware/abci.go b/mod/runtime/pkg/middleware/abci.go index a9de5fa5ec..0490754269 100644 --- a/mod/runtime/pkg/middleware/abci.go +++ b/mod/runtime/pkg/middleware/abci.go @@ -76,7 +76,7 @@ func (h *ABCIMiddleware[ ) (transition.ValidatorUpdates, error) { select { case <-ctx.Done(): - return nil, ctx.Err() + return nil, ErrInitGenesisTimeout(ctx.Err()) case gdpEvent := <-h.subGenDataProcessed: return gdpEvent.Data(), gdpEvent.Error() } @@ -139,7 +139,7 @@ func (h *ABCIMiddleware[ ) (BeaconBlockT, error) { select { case <-ctx.Done(): - return *new(BeaconBlockT), ctx.Err() + return *new(BeaconBlockT), ErrBuildBeaconBlockTimeout(ctx.Err()) case bbEvent := <-h.subBuiltBeaconBlock: return bbEvent.Data(), bbEvent.Error() } @@ -153,7 +153,7 @@ func (h *ABCIMiddleware[ ) (BlobSidecarsT, error) { select { case <-ctx.Done(): - return *new(BlobSidecarsT), ctx.Err() + return *new(BlobSidecarsT), ErrBuildSidecarsTimeout(ctx.Err()) case scEvent := <-h.subBuiltSidecars: return scEvent.Data(), scEvent.Error() } @@ -255,7 +255,7 @@ func (h *ABCIMiddleware[ ) (BeaconBlockT, error) { select { case <-ctx.Done(): - return *new(BeaconBlockT), ctx.Err() + return *new(BeaconBlockT), ErrVerifyBeaconBlockTimeout(ctx.Err()) case vEvent := <-h.subBBVerified: return vEvent.Data(), vEvent.Error() } @@ -269,7 +269,7 @@ func (h *ABCIMiddleware[ ) (BlobSidecarsT, error) { select { case <-ctx.Done(): - return *new(BlobSidecarsT), ctx.Err() + return *new(BlobSidecarsT), ErrVerifySidecarsTimeout(ctx.Err()) case vEvent := <-h.subSCVerified: return vEvent.Data(), vEvent.Error() } @@ -357,7 +357,7 @@ func (h *ABCIMiddleware[ ) (transition.ValidatorUpdates, error) { select { case <-ctx.Done(): - return nil, ctx.Err() + return nil, ErrFinalValidatorUpdatesTimeout(ctx.Err()) case event := <-h.subFinalValidatorUpdates: return event.Data(), event.Error() } diff --git a/mod/runtime/pkg/middleware/errors.go b/mod/runtime/pkg/middleware/errors.go index 94de197ecd..a93a621a67 100644 --- a/mod/runtime/pkg/middleware/errors.go +++ b/mod/runtime/pkg/middleware/errors.go @@ -20,7 +20,7 @@ package middleware -import "errors" +import "github.com/berachain/beacon-kit/mod/errors" var ( // ErrUnexpectedEvent is returned when an unexpected event is encountered. @@ -30,9 +30,34 @@ var ( ErrInvalidProcessProposalRequestType = errors.New( "invalid process proposal request type", ) + // ErrInvalidFinalizeBlockRequestType is returned when an invalid // finalize block request type is encountered. ErrInvalidFinalizeBlockRequestType = errors.New( - "invalid pre block request type", + "invalid finalize block request type", ) + + ErrInitGenesisTimeout = func(errTimeout error) error { + return errors.Wrapf(errTimeout, "A timeout occurred while waiting for genesis data processing") + } + + ErrBuildBeaconBlockTimeout = func(errTimeout error) error { + return errors.Wrapf(errTimeout, "A timeout occurred while waiting for a beacon block to be built") + } + + ErrBuildSidecarsTimeout = func(errTimeout error) error { + return errors.Wrapf(errTimeout, "A timeout occurred while waiting for blob sidecars to be built") + } + + ErrVerifyBeaconBlockTimeout = func(errTimeout error) error { + return errors.Wrapf(errTimeout, "A timeout occurred while waiting for a beacon block to be verified") + } + + ErrVerifySidecarsTimeout = func(errTimeout error) error { + return errors.Wrapf(errTimeout, "A timeout occurred while waiting for blob sidecars to be verified") + } + + ErrFinalValidatorUpdatesTimeout = func(errTimeout error) error { + return errors.Wrapf(errTimeout, "A timeout occurred while waiting for final validator updates") + } ) From 3cb0bfbb5edc51dde1d8097bf822b48c41bd8d4c Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 20 Aug 2024 13:58:48 -0400 Subject: [PATCH 74/75] lint --- mod/runtime/pkg/middleware/errors.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/mod/runtime/pkg/middleware/errors.go b/mod/runtime/pkg/middleware/errors.go index a93a621a67..e04111f160 100644 --- a/mod/runtime/pkg/middleware/errors.go +++ b/mod/runtime/pkg/middleware/errors.go @@ -22,6 +22,7 @@ package middleware import "github.com/berachain/beacon-kit/mod/errors" +//nolint:gochecknoglobals // errors var ( // ErrUnexpectedEvent is returned when an unexpected event is encountered. ErrUnexpectedEvent = errors.New("unexpected event") @@ -38,26 +39,39 @@ var ( ) ErrInitGenesisTimeout = func(errTimeout error) error { - return errors.Wrapf(errTimeout, "A timeout occurred while waiting for genesis data processing") + return errors.Wrapf(errTimeout, + "A timeout occurred while waiting for genesis data processing", + ) } ErrBuildBeaconBlockTimeout = func(errTimeout error) error { - return errors.Wrapf(errTimeout, "A timeout occurred while waiting for a beacon block to be built") + return errors.Wrapf(errTimeout, + "A timeout occurred while waiting for a beacon block to be built", + ) } ErrBuildSidecarsTimeout = func(errTimeout error) error { - return errors.Wrapf(errTimeout, "A timeout occurred while waiting for blob sidecars to be built") + return errors.Wrapf(errTimeout, + "A timeout occurred while waiting for blob sidecars to be built", + ) } + //nolint:lll // error messages ErrVerifyBeaconBlockTimeout = func(errTimeout error) error { - return errors.Wrapf(errTimeout, "A timeout occurred while waiting for a beacon block to be verified") + return errors.Wrapf(errTimeout, + "A timeout occurred while waiting for a beacon block to be verified", + ) } ErrVerifySidecarsTimeout = func(errTimeout error) error { - return errors.Wrapf(errTimeout, "A timeout occurred while waiting for blob sidecars to be verified") + return errors.Wrapf(errTimeout, + "A timeout occurred while waiting for blob sidecars to be verified", + ) } ErrFinalValidatorUpdatesTimeout = func(errTimeout error) error { - return errors.Wrapf(errTimeout, "A timeout occurred while waiting for final validator updates") + return errors.Wrapf(errTimeout, + "A timeout occurred while waiting for final validator updates", + ) } ) From 596b6ef2e63f5bc59ce89fe8c0cd89b1e197f95d Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Tue, 20 Aug 2024 14:02:49 -0400 Subject: [PATCH 75/75] lint --- mod/runtime/pkg/middleware/errors.go | 1 - 1 file changed, 1 deletion(-) diff --git a/mod/runtime/pkg/middleware/errors.go b/mod/runtime/pkg/middleware/errors.go index e04111f160..dccdc6df60 100644 --- a/mod/runtime/pkg/middleware/errors.go +++ b/mod/runtime/pkg/middleware/errors.go @@ -56,7 +56,6 @@ var ( ) } - //nolint:lll // error messages ErrVerifyBeaconBlockTimeout = func(errTimeout error) error { return errors.Wrapf(errTimeout, "A timeout occurred while waiting for a beacon block to be verified",