This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy patheth.go
173 lines (145 loc) · 5.65 KB
/
eth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 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.
package eth
import (
"fmt"
"net/http"
"github.com/berachain/polaris/eth/consensus"
pcore "github.com/berachain/polaris/eth/core"
"github.com/berachain/polaris/eth/node"
"github.com/berachain/polaris/eth/polar"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/txpool"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/rpc"
)
type (
// Miner represents the `Miner` that exists on the backend of the execution layer.
Miner interface {
BuildPayload(*miner.BuildPayloadArgs) (*miner.Payload, error)
Etherbase() common.Address
}
// TxPool represents the `TxPool` that exists on the backend of the execution layer.
TxPool interface {
Add([]*ethtypes.Transaction, bool, bool) []error
Stats() (int, int)
SubscribeTransactions(ch chan<- core.NewTxsEvent, reorgs bool) event.Subscription
Status(hash common.Hash) txpool.TxStatus
Has(hash common.Hash) bool
Remove(common.Hash)
}
// NetworkingStack is the entrypoint for the evm execution environment.
NetworkingStack interface {
// ExtRPCEnabled returns true if the networking stack is configured to expose JSON-RPC API.
ExtRPCEnabled() bool
// RegisterHandler manually registers a new handler into the networking stack.
RegisterHandler(string, string, http.Handler)
// RegisterAPIs registers JSON-RPC handlers for the networking stack.
RegisterAPIs([]rpc.API)
// RegisterLifecycles registers objects to have their lifecycle managed by the stack.
RegisterLifecycle(node.Lifecycle)
// Start starts the networking stack.
Start() error
// Close stops the networking stack.
Close() error
}
// ExecutionLayer represents the execution layer for a polaris EVM chain.
ExecutionLayer struct {
// stack handles all networking aspects of the execution layer. mainly JSON-RPC.
stack NetworkingStack
// backend is the entry point to the core logic of the execution layer.
backend *polar.Polaris
}
// Config struct holds the configuration for Polaris and Node.
Config struct {
OptimisticExecution bool
Polar polar.Config
Node node.Config
}
)
// New creates a new execution layer with the provided host chain.
// It takes a client type, configuration, host chain, consensus engine, and log handler
// as parameters. It returns a pointer to the ExecutionLayer and an error if any.
func New(
client string, cfg any, host pcore.PolarisHostChain,
engine consensus.Engine, allowUnprotectedTxs bool, logger log.Logger,
) (*ExecutionLayer, error) {
clientFactories := map[string]func(
any, pcore.PolarisHostChain, consensus.Engine, bool, log.Logger,
) (*ExecutionLayer, error){
"geth": newGethExecutionLayer,
}
factory, ok := clientFactories[client]
if !ok {
return nil, fmt.Errorf("unknown execution layer: %s", client)
}
return factory(cfg, host, engine, allowUnprotectedTxs, logger)
}
// newGethExecutionLayer creates a new geth execution layer.
// It returns a pointer to the ExecutionLayer and an error if any.
func newGethExecutionLayer(
anyCfg any, host pcore.PolarisHostChain,
engine consensus.Engine, allowUnprotectedTxs bool, logger log.Logger,
) (*ExecutionLayer, error) {
cfg, ok := anyCfg.(*Config)
if !ok {
// If the configuration type is invalid, return an error
return nil, fmt.Errorf("invalid config type")
}
gethNode, err := node.New(&cfg.Node)
if err != nil {
return nil, err
}
// In Polaris we don't use P2P at the geth level.
gethNode.SetP2PDisabled(true)
log.SetDefault(logger)
// Create a new Polaris backend
backend := polar.New(&cfg.Polar, host, engine, gethNode, allowUnprotectedTxs)
// Return a new ExecutionLayer with the created gethNode and backend
return &ExecutionLayer{
stack: gethNode,
backend: backend,
}, nil
}
// Start starts the networking stack of the execution layer.
// It returns an error if the start operation fails.
func (el *ExecutionLayer) Start() error {
return el.stack.Start()
}
// Close stops the networking stack of the execution layer.
// It returns an error if the close operation fails.
func (el *ExecutionLayer) Close() error {
return el.stack.Close()
}
// Backend returns the Polaris backend associated with the execution layer.
func (el *ExecutionLayer) Backend() *polar.Polaris {
return el.backend
}
// Stack returns the NetworkingStack associated with the execution layer.
func (el *ExecutionLayer) Stack() NetworkingStack {
return el.stack
}