Skip to content

Commit

Permalink
feat: separate archive and standard beacon clients (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Oct 24, 2024
1 parent 7712740 commit 88a4e0d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ INCLUSION_PROVER_DOMAINS_1_ROUTER="0xC89Ce4735882C9F0f0FE26686c53074E09B0D550"
INCLUSION_PROVER_DOMAINS_1_HASHI="0xa4D8c6AD5BCE909A84453fd2E5c392223634Bc3E"
INCLUSION_PROVER_DOMAINS_1_YAHO="0x21eAB033C7D2DF6A67AeF6C5Bda9A7F151eB9f52"
INCLUSION_PROVER_DOMAINS_1_BEACON_ENDPOINT="http://beacon-stub:8882"
INCLUSION_PROVER_DOMAINS_1_ARCHIVE_BEACON_ENDPOINT="http://beacon-stub:8882"
INCLUSION_PROVER_DOMAINS_1_STATE_ROOT_ADDRESSES="0xCfEB869F69431e42cdB54A4F4f105C19C080A601"
INCLUSION_PROVER_DOMAINS_1_ENDPOINT="http://evm1:8545"
INCLUSION_PROVER_DOMAINS_1_SLOT_INDEX="1"
Expand All @@ -20,6 +21,7 @@ INCLUSION_PROVER_DOMAINS_2_ROUTER="0xC89Ce4735882C9F0f0FE26686c53074E09B0D550"
INCLUSION_PROVER_DOMAINS_2_HASHI="0xa4D8c6AD5BCE909A84453fd2E5c392223634Bc3E"
INCLUSION_PROVER_DOMAINS_2_YAHO="0x21eAB033C7D2DF6A67AeF6C5Bda9A7F151eB9f52"
INCLUSION_PROVER_DOMAINS_2_BEACON_ENDPOINT="http://beacon-stub:8882"
INCLUSION_PROVER_DOMAINS_2_ARCHIVE_BEACON_ENDPOINT="http://beacon-stub:8882"
INCLUSION_PROVER_DOMAINS_2_ENDPOINT="http://evm2:8545"
INCLUSION_PROVER_DOMAINS_2_STATE_ROOT_ADDRESSES="0xCfEB869F69431e42cdB54A4F4f105C19C080A601"
INCLUSION_PROVER_DOMAINS_2_SLOT_INDEX="1"
Expand Down
1 change: 1 addition & 0 deletions chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
type EVMConfig struct {
config.BaseNetworkConfig
BeaconEndpoint string `split_words:"true"`
ArchiveBeaconEndpoint string `split_words:"true"`
Router string
Executor string
Hashi string
Expand Down
2 changes: 2 additions & 0 deletions chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("INCLUSION_PROVER_DOMAINS_1_YAHO", "yaho")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_HASHI", "hashi")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_BEACON_ENDPOINT", "endpoint")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_ARCHIVE_BEACON_ENDPOINT", "archive")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_MAX_GAS_PRICE", "1000")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_BLOCK_INTERVAL", "10")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_BLOCK_RETRY_INTERVAL", "10")
Expand Down Expand Up @@ -118,6 +119,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
GasIncreasePercentage: 20,
MaxGasPrice: 1000,
BeaconEndpoint: "endpoint",
ArchiveBeaconEndpoint: "archive",
StateRootAddresses: []string{"0x1", "0x2"},
SlotIndex: 1,
BlockConfirmations: 15,
Expand Down
35 changes: 20 additions & 15 deletions chains/evm/proof/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ const (
SLOTS_PER_HISTORICAL_LIMIT = 8192
)

type BeaconClient interface {
BeaconBlockHeader(
type BeaconStateFetcher interface {
BeaconState(
ctx context.Context,
opts *api.BeaconBlockHeaderOpts,
opts *api.BeaconStateOpts,
) (
*api.Response[*apiv1.BeaconBlockHeader],
*api.Response[*spec.VersionedBeaconState],
error,
)
BeaconState(
}

type BeaconClient interface {
BeaconBlockHeader(
ctx context.Context,
opts *api.BeaconStateOpts,
opts *api.BeaconBlockHeaderOpts,
) (
*api.Response[*spec.VersionedBeaconState],
*api.Response[*apiv1.BeaconBlockHeader],
error,
)
SignedBeaconBlock(ctx context.Context,
Expand All @@ -51,16 +54,18 @@ type BeaconClient interface {
}

type ReceiptRootProver struct {
beaconClient BeaconClient
spec config.Spec
stateCache *cache.Cache
beaconClient BeaconClient
beaconStateFetcher BeaconStateFetcher
spec config.Spec
stateCache *cache.Cache
}

func NewReceiptRootProver(beaconClient BeaconClient, spec config.Spec) *ReceiptRootProver {
func NewReceiptRootProver(beaconClient BeaconClient, beaconStateFetcher BeaconStateFetcher, spec config.Spec) *ReceiptRootProver {
return &ReceiptRootProver{
beaconClient: beaconClient,
spec: spec,
stateCache: cache.New(time.Minute*5, time.Minute*5),
beaconClient: beaconClient,
beaconStateFetcher: beaconStateFetcher,
spec: spec,
stateCache: cache.New(time.Minute*5, time.Minute*5),
}
}

Expand Down Expand Up @@ -124,7 +129,7 @@ func (p *ReceiptRootProver) beaconState(ctx context.Context, slot *big.Int) (*ap
return cachedState.(*api.Response[*spec.VersionedBeaconState]), nil
}

state, err := p.beaconClient.BeaconState(ctx, &api.BeaconStateOpts{
state, err := p.beaconStateFetcher.BeaconState(ctx, &api.BeaconStateOpts{
State: strconv.FormatUint(slot.Uint64(), 10),
})
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions chains/evm/proof/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
type ReceiptRootProofTestSuite struct {
suite.Suite

prover *proof.ReceiptRootProver
mockBeaconClient *mock.MockBeaconClient
prover *proof.ReceiptRootProver
mockBeaconClient *mock.MockBeaconClient
mockArchiveBeaconClient *mock.MockBeaconStateFetcher
}

func TestRunReceiptRootProofTestSuite(t *testing.T) {
Expand All @@ -35,6 +36,7 @@ func TestRunReceiptRootProofTestSuite(t *testing.T) {
func (s *ReceiptRootProofTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
s.mockBeaconClient = mock.NewMockBeaconClient(ctrl)
s.mockArchiveBeaconClient = mock.NewMockBeaconStateFetcher(ctrl)

beaconState := &spec.VersionedBeaconState{
Deneb: &deneb.BeaconState{},
Expand All @@ -44,7 +46,7 @@ func (s *ReceiptRootProofTestSuite) SetupTest() {
panic(err)
}
_ = beaconState.Deneb.UnmarshalJSON(beaconBytes)
s.mockBeaconClient.EXPECT().BeaconState(gomock.Any(), gomock.Any()).Return(&api.Response[*spec.VersionedBeaconState]{
s.mockArchiveBeaconClient.EXPECT().BeaconState(gomock.Any(), gomock.Any()).Return(&api.Response[*spec.VersionedBeaconState]{
Data: beaconState,
}, nil).AnyTimes()

Expand All @@ -70,7 +72,7 @@ func (s *ReceiptRootProofTestSuite) SetupTest() {
Data: beaconBlockHeader,
}, nil).AnyTimes()

s.prover = proof.NewReceiptRootProver(s.mockBeaconClient, config.MainnetSpec)
s.prover = proof.NewReceiptRootProver(s.mockBeaconClient, s.mockArchiveBeaconClient, config.MainnetSpec)
}

func (s *ReceiptRootProofTestSuite) Test_ReceiptRootProof_SlotDifferent() {
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,19 @@ func main() {
panic(err)
}
beaconProvider := beaconClient.(*http.Service)

archiveBeaconClinet, err := http.New(ctx,
http.WithAddress(config.ArchiveBeaconEndpoint),
http.WithLogLevel(logLevel),
http.WithTimeout(time.Minute*30),
http.WithEnforceJSON(false),
)
if err != nil {
panic(err)
}
archiveBeaconProvider := archiveBeaconClinet.(*http.Service)
receiptProver := proof.NewReceiptProver(client)
rootProver := proof.NewReceiptRootProver(beaconProvider, config.Spec)
rootProver := proof.NewReceiptRootProver(beaconProvider, archiveBeaconProvider, config.Spec)

stateRootEventHandlers := make([]evmMessage.EventHandler, 0)
if config.Yaho != "" {
Expand Down
53 changes: 38 additions & 15 deletions mock/root.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88a4e0d

Please sign in to comment.