aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-10-19 18:52:38 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:23:39 +0800
commit0e12c9284efdf72a376f75f180bac7968d6fb382 (patch)
tree1b06f8f9483bff246dbeba12c7445780a595b21a
parent93563f41410b964454f23229cfabe1ec1ba735f5 (diff)
downloadgo-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.tar
go-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.tar.gz
go-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.tar.bz2
go-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.tar.lz
go-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.tar.xz
go-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.tar.zst
go-tangerine-0e12c9284efdf72a376f75f180bac7968d6fb382.zip
consensus: dexcon: fetch config from state
-rw-r--r--consensus/dexcon/dexcon.go24
-rw-r--r--core/types/block.go41
-rw-r--r--dex/app.go1
-rw-r--r--dex/backend.go8
-rw-r--r--dex/config.go5
-rw-r--r--dex/governance.go8
-rw-r--r--eth/backend.go5
7 files changed, 51 insertions, 41 deletions
diff --git a/consensus/dexcon/dexcon.go b/consensus/dexcon/dexcon.go
index 46090b757..e636108f8 100644
--- a/consensus/dexcon/dexcon.go
+++ b/consensus/dexcon/dexcon.go
@@ -27,21 +27,26 @@ import (
"github.com/dexon-foundation/dexon/rpc"
)
-// Config is the configuration for DEXON consensus.
-type Config struct {
+type ConfigurationFetcher interface {
+ DexconConfiguration(round uint64) *params.DexconConfig
}
// Dexcon is a delegated proof-of-stake consensus engine.
type Dexcon struct {
- config *params.DexconConfig
+ configFetcher ConfigurationFetcher
}
// New creates a Clique proof-of-authority consensus engine with the initial
// signers set to the ones provided by the user.
-func New(config *params.DexconConfig) *Dexcon {
- return &Dexcon{
- config: config,
- }
+func New() *Dexcon {
+ return &Dexcon{}
+}
+
+// SetConfigFetcher sets the config fetcher for Dexcon. The reason this is not
+// passed in the New() method is to bypass cycle dependencies when initializing
+// dex backend.
+func (d *Dexcon) SetConfigFetcher(fetcher ConfigurationFetcher) {
+ d.configFetcher = fetcher
}
// Author implements consensus.Engine, returning the Ethereum address recovered
@@ -105,11 +110,10 @@ func (d *Dexcon) Prepare(chain consensus.ChainReader, header *types.Header) erro
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given, and returns the final block.
func (d *Dexcon) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
- // TODO(Bojie): remove it and get value from config
- reward := new(big.Int).Div(d.config.BlockReward, new(big.Int).SetUint64(uint64(d.config.NumChains)))
+ config := d.configFetcher.DexconConfiguration(header.Position.Round)
+ reward := new(big.Int).Div(config.BlockReward, big.NewInt(int64(config.NumChains)))
state.AddBalance(header.Coinbase, reward)
header.Root = state.IntermediateRoot(true)
-
return types.NewBlock(header, txs, uncles, receipts), nil
}
diff --git a/core/types/block.go b/core/types/block.go
index a28ce8ee0..b814bd975 100644
--- a/core/types/block.go
+++ b/core/types/block.go
@@ -26,6 +26,8 @@ import (
"time"
"unsafe"
+ coreTypes "github.com/dexon-foundation/dexon-consensus-core/core/types"
+
"github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/common/hexutil"
"github.com/dexon-foundation/dexon/rlp"
@@ -68,25 +70,26 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
// Header represents a block header in the Ethereum blockchain.
type Header struct {
- ParentHash common.Hash `json:"parentHash" gencodec:"required"`
- UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
- Coinbase common.Address `json:"miner" gencodec:"required"`
- Root common.Hash `json:"stateRoot" gencodec:"required"`
- TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
- ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
- Bloom Bloom `json:"logsBloom" gencodec:"required"`
- Difficulty *big.Int `json:"difficulty" gencodec:"required"`
- Number *big.Int `json:"number" gencodec:"required"`
- GasLimit uint64 `json:"gasLimit" gencodec:"required"`
- GasUsed uint64 `json:"gasUsed" gencodec:"required"`
- Time uint64 `json:"timestamp" gencodec:"required"`
- Extra []byte `json:"extraData" gencodec:"required"`
- MixDigest common.Hash `json:"mixHash"`
- Nonce BlockNonce `json:"nonce"`
- Randomness []byte `json:"randomness" gencodec:"required"`
- WitnessHeight uint64 `json:"witnessHeight" gencodec:"required"`
- WitnessRoot common.Hash `json:"WitnessRoot" gencodec:"required"`
- WitnessReceiptHash common.Hash `json:"WitnessReceiptHash" gencodec:"required"`
+ ParentHash common.Hash `json:"parentHash" gencodec:"required"`
+ UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
+ Coinbase common.Address `json:"miner" gencodec:"required"`
+ Root common.Hash `json:"stateRoot" gencodec:"required"`
+ TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
+ ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
+ Bloom Bloom `json:"logsBloom" gencodec:"required"`
+ Difficulty *big.Int `json:"difficulty" gencodec:"required"`
+ Number *big.Int `json:"number" gencodec:"required"`
+ GasLimit uint64 `json:"gasLimit" gencodec:"required"`
+ GasUsed uint64 `json:"gasUsed" gencodec:"required"`
+ Time uint64 `json:"timestamp" gencodec:"required"`
+ Extra []byte `json:"extraData" gencodec:"required"`
+ MixDigest common.Hash `json:"mixHash"`
+ Nonce BlockNonce `json:"nonce"`
+ Randomness []byte `json:"randomness" gencodec:"required"`
+ Position coreTypes.Position `json:"position" gencodec:"required"`
+ WitnessHeight uint64 `json:"witnessHeight" gencodec:"required"`
+ WitnessRoot common.Hash `json:"WitnessRoot" gencodec:"required"`
+ WitnessReceiptHash common.Hash `json:"WitnessReceiptHash" gencodec:"required"`
}
// field type overrides for gencodec
diff --git a/dex/app.go b/dex/app.go
index 317711e94..ab23301d5 100644
--- a/dex/app.go
+++ b/dex/app.go
@@ -223,6 +223,7 @@ func (d *DexconApp) BlockDelivered(blockHash coreCommon.Hash, result coreTypes.F
Number: new(big.Int).SetUint64(result.Height),
Time: big.NewInt(result.Timestamp.Unix()),
Coinbase: common.BytesToAddress(block.ProposerID.Bytes()),
+ Position: block.Position,
WitnessHeight: block.Witness.Height,
WitnessRoot: witnessData.Root,
WitnessReceiptHash: witnessData.ReceiptHash,
diff --git a/dex/backend.go b/dex/backend.go
index 7a23ba80b..2f6d8a8e5 100644
--- a/dex/backend.go
+++ b/dex/backend.go
@@ -110,6 +110,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
}
rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
}
+ engine := dexcon.New()
+
dex := &Dexon{
config: config,
chainDb: chainDb,
@@ -121,7 +123,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
bloomRequests: make(chan chan *bloombits.Retrieval),
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
blockdb: db,
- engine: dexcon.New(chainConfig.Dexcon),
+ engine: engine,
}
var (
@@ -154,9 +156,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
}
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, dex.governance)
diff --git a/dex/config.go b/dex/config.go
index a3b682de0..5eff28022 100644
--- a/dex/config.go
+++ b/dex/config.go
@@ -25,7 +25,6 @@ import (
"runtime"
"time"
- "github.com/dexon-foundation/dexon/consensus/dexcon"
"github.com/dexon-foundation/dexon/core"
"github.com/dexon-foundation/dexon/dex/gasprice"
"github.com/dexon-foundation/dexon/eth/downloader"
@@ -35,7 +34,6 @@ import (
// DefaultConfig contains default settings for use on the Ethereum main net.
var DefaultConfig = Config{
SyncMode: downloader.FastSync,
- Dexcon: dexcon.Config{},
NetworkId: 1,
LightPeers: 100,
DatabaseCache: 768,
@@ -102,9 +100,6 @@ type Config struct {
GasCeil uint64
GasLimitTolerance uint64
- // Dexcon options
- Dexcon dexcon.Config
-
// Transaction pool options
TxPool core.TxPoolConfig
diff --git a/dex/governance.go b/dex/governance.go
index 4ec69c2d6..50ab7ee14 100644
--- a/dex/governance.go
+++ b/dex/governance.go
@@ -79,7 +79,13 @@ func (d *DexconGovernance) getGovStateAtRound(round uint64) *vm.GovernanceStateH
return &vm.GovernanceStateHelper{state}
}
-// Configuration return the total ordering K constant.
+// DexconConfiguration return raw config in state.
+func (d *DexconGovernance) DexconConfiguration(round uint64) *params.DexconConfig {
+ s := d.getGovStateAtRound(round)
+ return s.Configuration()
+}
+
+// Configuration returns the system configuration for consensus core to use.
func (d *DexconGovernance) Configuration(round uint64) *coreTypes.Config {
s := d.getGovStateAtRound(round)
c := s.Configuration()
diff --git a/eth/backend.go b/eth/backend.go
index 87eb8a2ae..04e4569f2 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -30,7 +30,6 @@ import (
"github.com/dexon-foundation/dexon/common/hexutil"
"github.com/dexon-foundation/dexon/consensus"
"github.com/dexon-foundation/dexon/consensus/clique"
- "github.com/dexon-foundation/dexon/consensus/dexcon"
"github.com/dexon-foundation/dexon/consensus/ethash"
"github.com/dexon-foundation/dexon/core"
"github.com/dexon-foundation/dexon/core/bloombits"
@@ -225,10 +224,6 @@ func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Data
// CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service
func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
// If proof-of-authority is requested, set it up
- if chainConfig.Dexcon != nil {
- return dexcon.New(chainConfig.Dexcon)
- }
- // If proof-of-authority is requested, set it up
if chainConfig.Clique != nil {
return clique.New(chainConfig.Clique, db)
}