aboutsummaryrefslogtreecommitdiffstats
path: root/dex/backend.go
blob: b78593294d389951c855f7442d49aa3044536143 (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.

package dex

import (
    "fmt"
    "time"

    dexCore "github.com/dexon-foundation/dexon-consensus/core"
    coreEcdsa "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa"
    coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"

    "github.com/dexon-foundation/dexon/accounts"
    "github.com/dexon-foundation/dexon/consensus"
    "github.com/dexon-foundation/dexon/consensus/dexcon"
    "github.com/dexon-foundation/dexon/core"
    "github.com/dexon-foundation/dexon/core/bloombits"
    "github.com/dexon-foundation/dexon/core/rawdb"
    "github.com/dexon-foundation/dexon/core/vm"
    "github.com/dexon-foundation/dexon/dex/blockdb"
    "github.com/dexon-foundation/dexon/dex/downloader"
    "github.com/dexon-foundation/dexon/eth/filters"
    "github.com/dexon-foundation/dexon/eth/gasprice"
    "github.com/dexon-foundation/dexon/ethdb"
    "github.com/dexon-foundation/dexon/event"
    "github.com/dexon-foundation/dexon/internal/ethapi"
    "github.com/dexon-foundation/dexon/log"
    "github.com/dexon-foundation/dexon/node"
    "github.com/dexon-foundation/dexon/p2p"
    "github.com/dexon-foundation/dexon/params"
    "github.com/dexon-foundation/dexon/rpc"
)

// Dexon implementes the DEXON fullnode service.
type Dexon struct {
    config      *Config
    chainConfig *params.ChainConfig

    // Channel for shutting down the service
    shutdownChan chan bool // Channel for shutting down the Ethereum

    // Handlers
    txPool          *core.TxPool
    blockchain      *core.BlockChain
    protocolManager *ProtocolManager

    // DB interfaces
    chainDb ethdb.Database // Block chain database

    eventMux       *event.TypeMux
    engine         consensus.Engine
    accountManager *accounts.Manager

    bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
    bloomIndexer  *core.ChainIndexer             // Bloom indexer operating during block imports

    APIBackend *DexAPIBackend

    // Dexon consensus.
    app        *DexconApp
    governance *DexconGovernance
    network    *DexconNetwork
    consensus  *dexCore.Consensus

    networkID     uint64
    netRPCService *ethapi.PublicNetAPI
}

func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
    // Consensus.
    chainDb, err := CreateDB(ctx, config, "chaindata")
    if err != nil {
        return nil, err
    }
    chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb,
        config.Genesis)
    if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
        return nil, genesisErr
    }
    log.Info("Initialised chain configuration", "config", chainConfig)

    if !config.SkipBcVersionCheck {
        bcVersion := rawdb.ReadDatabaseVersion(chainDb)
        if bcVersion != core.BlockChainVersion && bcVersion != 0 {
            return nil, fmt.Errorf("Blockchain DB version mismatch (%d / %d).\n",
                bcVersion, core.BlockChainVersion)
        }
        rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
    }
    engine := dexcon.New()

    dex := &Dexon{
        config:         config,
        chainDb:        chainDb,
        chainConfig:    chainConfig,
        eventMux:       ctx.EventMux,
        accountManager: ctx.AccountManager,
        shutdownChan:   make(chan bool),
        networkID:      config.NetworkId,
        bloomRequests:  make(chan chan *bloombits.Retrieval),
        bloomIndexer:   NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
        engine:         engine,
    }

    var (
        vmConfig = vm.Config{
            EnablePreimageRecording: config.EnablePreimageRecording,
            EWASMInterpreter:        config.EWASMInterpreter,
            EVMInterpreter:          config.EVMInterpreter,
            IsBlockProposer:         config.BlockProposerEnabled,
        }
        cacheConfig = &core.CacheConfig{Disabled: config.NoPruning, TrieCleanLimit: config.TrieCleanCache, TrieDirtyLimit: config.TrieDirtyCache, TrieTimeLimit: config.TrieTimeout}
    )
    dex.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, dex.chainConfig, dex.engine, vmConfig, nil)

    // Rewind the chain in case of an incompatible config upgrade.
    if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
        log.Warn("Rewinding chain to upgrade configuration", "err", compat)
        dex.blockchain.SetHead(compat.RewindTo)
        rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
    }
    dex.bloomIndexer.Start(dex.blockchain)

    if config.TxPool.Journal != "" {
        config.TxPool.Journal = ctx.ResolvePath(config.TxPool.Journal)
    }
    dex.txPool = core.NewTxPool(config.TxPool, dex.chainConfig, dex.blockchain, config.BlockProposerEnabled)

    dex.APIBackend = &DexAPIBackend{dex, nil}
    gpoParams := config.GPO
    if gpoParams.Default == nil {
        gpoParams.Default = config.DefaultGasPrice
    }
    dex.APIBackend.gpo = gasprice.NewOracle(dex.APIBackend, gpoParams)

    // Dexcon related objects.
    dex.governance = NewDexconGovernance(dex.APIBackend, dex.chainConfig, config.PrivateKey)
    dex.app = NewDexconApp(dex.txPool, dex.blockchain, dex.governance, chainDb, config, vmConfig)

    // Set config fetcher so engine can fetch current system configuration from state.
    engine.SetConfigFetcher(dex.governance)

    pm, err := NewProtocolManager(dex.chainConfig, config.SyncMode,
        config.NetworkId, dex.eventMux, dex.txPool, dex.engine, dex.blockchain,
        chainDb, config.BlockProposerEnabled, dex.governance)
    if err != nil {
        return nil, err
    }

    dex.protocolManager = pm
    dex.network = NewDexconNetwork(pm)

    privKey := coreEcdsa.NewPrivateKeyFromECDSA(config.PrivateKey)

    // TODO(w): set this correctly in config.
    now := time.Now()
    dMoment := time.Date(
        now.Year(), now.Month(), now.Day(),
        now.Hour(), now.Minute(), (now.Second()/5+1)*5,
        0, now.Location())

    dex.consensus = dexCore.NewConsensus(dMoment,
        dex.app, dex.governance, blockdb.NewDatabase(chainDb), dex.network, privKey, log.Root())
    return dex, nil
}

func (s *Dexon) Protocols() []p2p.Protocol {
    return s.protocolManager.SubProtocols
}

func (s *Dexon) APIs() []rpc.API {
    apis := ethapi.GetAPIs(s.APIBackend)

    // Append any APIs exposed explicitly by the consensus engine
    apis = append(apis, s.engine.APIs(s.BlockChain())...)

    // Append all the local APIs and return
    return append(apis, []rpc.API{
        {
            Namespace: "eth",
            Version:   "1.0",
            Service:   downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
            Public:    true,
        }, {
            Namespace: "eth",
            Version:   "1.0",
            Service:   filters.NewPublicFilterAPI(s.APIBackend, false),
            Public:    true,
        }, {
            Namespace: "admin",
            Version:   "1.0",
            Service:   NewPrivateAdminAPI(s),
        }, {
            Namespace: "debug",
            Version:   "1.0",
            Service:   NewPublicDebugAPI(s),
            Public:    true,
        }, {
            Namespace: "debug",
            Version:   "1.0",
            Service:   NewPrivateDebugAPI(s.chainConfig, s),
        }, {
            Namespace: "net",
            Version:   "1.0",
            Service:   s.netRPCService,
            Public:    true,
        },
    }...)
}

func (s *Dexon) Start(srvr *p2p.Server) error {
    // Start the bloom bits servicing goroutines
    s.startBloomHandlers(params.BloomBitsBlocks)

    // Start the RPC service
    s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion())

    // Figure out a max peers count based on the server limits
    maxPeers := srvr.MaxPeers
    if s.config.LightServ > 0 {
        if s.config.LightPeers >= srvr.MaxPeers {
            return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, srvr.MaxPeers)
        }
        maxPeers -= s.config.LightPeers
    }
    // Start the networking layer and the light server if requested
    s.protocolManager.Start(srvr, maxPeers)
    s.protocolManager.addSelfMeta()
    return nil
}

func (s *Dexon) Stop() error {
    return nil
}

func (s *Dexon) StartProposing() error {
    // TODO: Run with the latest confirmed block in compaction chain.
    s.consensus.Run(&coreTypes.Block{})
    return nil
}

// CreateDB creates the chain database.
func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Database, error) {
    db, err := ctx.OpenDatabase(name, config.DatabaseCache, config.DatabaseHandles)
    if err != nil {
        return nil, err
    }
    if db, ok := db.(*ethdb.LDBDatabase); ok {
        db.Meter("eth/db/chaindata/")
    }
    return db, nil
}

func (d *Dexon) AccountManager() *accounts.Manager { return d.accountManager }
func (d *Dexon) BlockChain() *core.BlockChain      { return d.blockchain }
func (d *Dexon) TxPool() *core.TxPool              { return d.txPool }
func (d *Dexon) DexVersion() int                   { return int(d.protocolManager.SubProtocols[0].Version) }
func (d *Dexon) EventMux() *event.TypeMux          { return d.eventMux }
func (d *Dexon) Engine() consensus.Engine          { return d.engine }
func (d *Dexon) ChainDb() ethdb.Database           { return d.chainDb }
func (d *Dexon) Downloader() ethapi.Downloader     { return d.protocolManager.downloader }
func (d *Dexon) NetVersion() uint64                { return d.networkID }