forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreth.go
211 lines (173 loc) · 6.38 KB
/
coreth.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/haowang0402/avalanchego/utils/timer/mockable"
"github.com/haowang0402/coreth/consensus/dummy"
"github.com/haowang0402/coreth/core"
"github.com/haowang0402/coreth/core/state"
"github.com/haowang0402/coreth/core/types"
"github.com/haowang0402/coreth/eth"
"github.com/haowang0402/coreth/ethdb"
"github.com/haowang0402/coreth/node"
"github.com/haowang0402/coreth/rpc"
)
var (
BlackholeAddr = common.Address{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
)
type Tx = types.Transaction
type Block = types.Block
type Hash = common.Hash
type ETHChain struct {
backend *eth.Ethereum
}
// NewETHChain creates an Ethereum blockchain with the given configs.
func NewETHChain(config *eth.Config, nodecfg *node.Config, chainDB ethdb.Database, settings eth.Settings, consensusCallbacks *dummy.ConsensusCallbacks, lastAcceptedHash common.Hash, clock *mockable.Clock) (*ETHChain, error) {
node, err := node.New(nodecfg)
if err != nil {
return nil, err
}
backend, err := eth.New(node, config, consensusCallbacks, chainDB, settings, lastAcceptedHash, clock)
if err != nil {
return nil, fmt.Errorf("failed to create backend: %w", err)
}
chain := ÐChain{backend: backend}
backend.SetEtherbase(BlackholeAddr)
return chain, nil
}
func (self *ETHChain) Start() {
self.backend.Start()
}
func (self *ETHChain) Stop() {
self.backend.Stop()
}
func (self *ETHChain) GenerateBlock() (*types.Block, error) {
return self.backend.Miner().GenerateBlock()
}
func (self *ETHChain) BlockChain() *core.BlockChain {
return self.backend.BlockChain()
}
func (self *ETHChain) APIBackend() *eth.EthAPIBackend {
return self.backend.APIBackend
}
func (self *ETHChain) PendingSize() int {
pending := self.backend.TxPool().Pending(true)
count := 0
for _, txs := range pending {
count += len(txs)
}
return count
}
func (self *ETHChain) AddRemoteTxs(txs []*types.Transaction) []error {
return self.backend.TxPool().AddRemotes(txs)
}
func (self *ETHChain) AddRemoteTxsSync(txs []*types.Transaction) []error {
return self.backend.TxPool().AddRemotesSync(txs)
}
func (self *ETHChain) AddLocalTxs(txs []*types.Transaction) []error {
return self.backend.TxPool().AddLocals(txs)
}
func (self *ETHChain) CurrentBlock() *types.Block {
return self.backend.BlockChain().CurrentBlock()
}
// Returns a new mutable state based on the current HEAD block.
func (self *ETHChain) CurrentState() (*state.StateDB, error) {
return self.backend.BlockChain().State()
}
// Returns a new mutable state based on the given block.
func (self *ETHChain) BlockState(block *types.Block) (*state.StateDB, error) {
return self.backend.BlockChain().StateAt(block.Root())
}
// Retrives a block from the database by hash.
func (self *ETHChain) GetBlockByHash(hash common.Hash) *types.Block {
return self.backend.BlockChain().GetBlockByHash(hash)
}
// Retrives a block from the database by number.
func (self *ETHChain) GetBlockByNumber(num uint64) *types.Block {
return self.backend.BlockChain().GetBlockByNumber(num)
}
// Validate the canonical chain from current block to the genesis.
// This should only be called as a convenience method in tests, not
// in production as it traverses the entire chain.
func (self *ETHChain) ValidateCanonicalChain() error {
return self.backend.BlockChain().ValidateCanonicalChain()
}
// SetPreference sets the current head block to the one provided as an argument
// regardless of what the chain contents were prior.
func (self *ETHChain) SetPreference(block *types.Block) error {
return self.BlockChain().SetPreference(block)
}
// Accept sets a minimum height at which no reorg can pass. Additionally,
// this function may trigger a reorg if the block being accepted is not in the
// canonical chain.
func (self *ETHChain) Accept(block *types.Block) error {
return self.BlockChain().Accept(block)
}
// Reject tells the chain that [block] has been rejected.
func (self *ETHChain) Reject(block *types.Block) error {
return self.BlockChain().Reject(block)
}
// LastAcceptedBlock returns the last block to be marked as accepted.
func (self *ETHChain) LastAcceptedBlock() *types.Block {
return self.BlockChain().LastAcceptedBlock()
}
// RemoveRejectedBlocks removes the rejected blocks between heights
// [start] and [end].
func (self *ETHChain) RemoveRejectedBlocks(start, end uint64) error {
return self.BlockChain().RemoveRejectedBlocks(start, end)
}
func (self *ETHChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
return self.backend.BlockChain().GetReceiptsByHash(hash)
}
func (self *ETHChain) GetGenesisBlock() *types.Block {
return self.backend.BlockChain().Genesis()
}
func (self *ETHChain) InsertBlock(block *types.Block) error {
return self.backend.BlockChain().InsertBlock(block)
}
func (self *ETHChain) NewRPCHandler(maximumDuration time.Duration) *rpc.Server {
return rpc.NewServer(maximumDuration)
}
// AttachEthService registers the backend RPC services provided by Ethereum
// to the provided handler under their assigned namespaces.
func (self *ETHChain) AttachEthService(handler *rpc.Server, names []string) error {
enabledServicesSet := make(map[string]struct{})
for _, ns := range names {
enabledServicesSet[ns] = struct{}{}
}
apiSet := make(map[string]rpc.API)
for _, api := range self.backend.APIs() {
if existingAPI, exists := apiSet[api.Name]; exists {
return fmt.Errorf("duplicated API name: %s, namespaces %s and %s", api.Name, api.Namespace, existingAPI.Namespace)
}
apiSet[api.Name] = api
}
for name := range enabledServicesSet {
api, exists := apiSet[name]
if !exists {
return fmt.Errorf("API service %s not found", name)
}
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err
}
}
return nil
}
func (self *ETHChain) GetTxSubmitCh() <-chan core.NewTxsEvent {
newTxsChan := make(chan core.NewTxsEvent)
self.backend.TxPool().SubscribeNewTxsEvent(newTxsChan)
return newTxsChan
}
func (self *ETHChain) GetTxAcceptedSubmitCh() <-chan core.NewTxsEvent {
newTxsChan := make(chan core.NewTxsEvent)
self.backend.BlockChain().SubscribeAcceptedTransactionEvent(newTxsChan)
return newTxsChan
}
func (self *ETHChain) GetTxPool() *core.TxPool { return self.backend.TxPool() }
func (self *ETHChain) BloomIndexer() *core.ChainIndexer { return self.backend.BloomIndexer() }