aboutsummaryrefslogtreecommitdiffstats
path: root/dex
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2019-01-14 19:59:57 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:27:21 +0800
commit2b03b31d46e166e70ea82b806db47bb6408282e3 (patch)
tree9f26d62978a5de14078d7e50ddb6714c8996533a /dex
parentdca26d3e73ea1657cb7daa2086b74250adbbd85f (diff)
downloadgo-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.tar
go-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.tar.gz
go-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.tar.bz2
go-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.tar.lz
go-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.tar.xz
go-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.tar.zst
go-tangerine-2b03b31d46e166e70ea82b806db47bb6408282e3.zip
params: write dMoment into ChainConfig (#150)
Diffstat (limited to 'dex')
-rw-r--r--dex/backend.go7
-rw-r--r--dex/handler.go6
-rw-r--r--dex/helper_test.go9
-rw-r--r--dex/peer.go10
-rw-r--r--dex/protocol.go3
-rw-r--r--dex/protocol_test.go10
6 files changed, 30 insertions, 15 deletions
diff --git a/dex/backend.go b/dex/backend.go
index 88cfa3033..c75fdfa29 100644
--- a/dex/backend.go
+++ b/dex/backend.go
@@ -162,8 +162,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
// Set config fetcher so engine can fetch current system configuration from state.
engine.SetGovStateFetcher(dex.governance)
- dMoment := time.Unix(config.DMoment, int64(0))
- log.Info("DEXON Consensus DMoment", "time", dMoment)
+ dMoment := time.Unix(int64(chainConfig.DMoment), 0)
// Force starting with full sync mode if this node is a bootstrap proposer.
if config.BlockProposerEnabled && dMoment.After(time.Now()) {
@@ -171,8 +170,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
}
pm, err := NewProtocolManager(dex.chainConfig, config.SyncMode,
- config.NetworkId, dex.eventMux, dex.txPool, dex.engine, dex.blockchain,
- chainDb, config.BlockProposerEnabled, dex.governance, dex.app)
+ config.NetworkId, chainConfig.DMoment, dex.eventMux, dex.txPool, dex.engine,
+ dex.blockchain, chainDb, config.BlockProposerEnabled, dex.governance, dex.app)
if err != nil {
return nil, err
}
diff --git a/dex/handler.go b/dex/handler.go
index a097605c0..a3fbf2ba9 100644
--- a/dex/handler.go
+++ b/dex/handler.go
@@ -91,6 +91,7 @@ func errResp(code errCode, format string, v ...interface{}) error {
type ProtocolManager struct {
networkID uint64
+ dMoment uint64
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
@@ -147,13 +148,14 @@ type ProtocolManager struct {
// with the Ethereum network.
func NewProtocolManager(
config *params.ChainConfig, mode downloader.SyncMode, networkID uint64,
- mux *event.TypeMux, txpool txPool, engine consensus.Engine,
+ dMoment uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine,
blockchain *core.BlockChain, chaindb ethdb.Database,
isBlockProposer bool, gov governance, app dexconApp) (*ProtocolManager, error) {
tab := newNodeTable()
// Create the protocol manager with the base fields
manager := &ProtocolManager{
networkID: networkID,
+ dMoment: dMoment,
eventMux: mux,
txpool: txpool,
nodeTable: tab,
@@ -343,7 +345,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
hash = head.Hash()
number = head.Number.Uint64()
)
- if err := p.Handshake(pm.networkID, number, hash, genesis.Hash()); err != nil {
+ if err := p.Handshake(pm.networkID, pm.dMoment, number, hash, genesis.Hash()); err != nil {
p.Log().Debug("Ethereum handshake failed", "err", err)
return err
}
diff --git a/dex/helper_test.go b/dex/helper_test.go
index 86a901cc2..e3d6aa3b7 100644
--- a/dex/helper_test.go
+++ b/dex/helper_test.go
@@ -46,6 +46,8 @@ var (
testBank = crypto.PubkeyToAddress(testBankKey.PublicKey)
)
+const dMoment = 123456
+
// testP2PServer is a fake, helper p2p server for testing purposes.
type testP2PServer struct {
mu sync.Mutex
@@ -134,7 +136,7 @@ func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func
notarySetFunc: func(uint64, uint32) (map[string]struct{}, error) { return nil, nil },
}
- pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx}, engine, blockchain, db, true, tgov, &testApp{})
+ pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, dMoment, evmux, &testTxPool{added: newtx}, engine, blockchain, db, true, tgov, &testApp{})
if err != nil {
return nil, nil, err
}
@@ -275,17 +277,18 @@ func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*te
head = pm.blockchain.CurrentHeader()
number = head.Number.Uint64()
)
- tp.handshake(nil, number, head.Hash(), genesis.Hash())
+ tp.handshake(nil, dMoment, number, head.Hash(), genesis.Hash())
}
return tp, errc
}
// handshake simulates a trivial handshake that expects the same state from the
// remote side as we are simulating locally.
-func (p *testPeer) handshake(t *testing.T, number uint64, head common.Hash, genesis common.Hash) {
+func (p *testPeer) handshake(t *testing.T, dMoment uint64, number uint64, head common.Hash, genesis common.Hash) {
msg := &statusData{
ProtocolVersion: uint32(p.version),
NetworkId: DefaultConfig.NetworkId,
+ DMoment: uint64(dMoment),
Number: number,
CurrentBlock: head,
GenesisBlock: genesis,
diff --git a/dex/peer.go b/dex/peer.go
index 295c5c400..0a67db688 100644
--- a/dex/peer.go
+++ b/dex/peer.go
@@ -631,7 +631,7 @@ func (p *peer) RequestReceipts(hashes []common.Hash) error {
// Handshake executes the eth protocol handshake, negotiating version number,
// network IDs, difficulties, head and genesis blocks.
-func (p *peer) Handshake(network uint64, number uint64, head common.Hash, genesis common.Hash) error {
+func (p *peer) Handshake(network uint64, dMoment uint64, number uint64, head common.Hash, genesis common.Hash) error {
// Send out own handshake in a new thread
errc := make(chan error, 2)
var status statusData // safe to read after two values have been received from errc
@@ -640,13 +640,14 @@ func (p *peer) Handshake(network uint64, number uint64, head common.Hash, genesi
errc <- p2p.Send(p.rw, StatusMsg, &statusData{
ProtocolVersion: uint32(p.version),
NetworkId: network,
+ DMoment: dMoment,
Number: number,
CurrentBlock: head,
GenesisBlock: genesis,
})
}()
go func() {
- errc <- p.readStatus(network, &status, genesis)
+ errc <- p.readStatus(network, dMoment, &status, genesis)
}()
timeout := time.NewTimer(handshakeTimeout)
defer timeout.Stop()
@@ -664,7 +665,7 @@ func (p *peer) Handshake(network uint64, number uint64, head common.Hash, genesi
return nil
}
-func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash) (err error) {
+func (p *peer) readStatus(network uint64, dMoment uint64, status *statusData, genesis common.Hash) (err error) {
msg, err := p.rw.ReadMsg()
if err != nil {
return err
@@ -685,6 +686,9 @@ func (p *peer) readStatus(network uint64, status *statusData, genesis common.Has
if status.NetworkId != network {
return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, network)
}
+ if status.DMoment != dMoment {
+ return errResp(ErrDMomentMismatch, "%d (!= %d)", status.DMoment, dMoment)
+ }
if int(status.ProtocolVersion) != p.version {
return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version)
}
diff --git a/dex/protocol.go b/dex/protocol.go
index c6b4c37c4..9244b8bb7 100644
--- a/dex/protocol.go
+++ b/dex/protocol.go
@@ -106,6 +106,7 @@ const (
ErrInvalidMsgCode
ErrProtocolVersionMismatch
ErrNetworkIdMismatch
+ ErrDMomentMismatch
ErrGenesisBlockMismatch
ErrNoStatusMsg
ErrExtraStatusMsg
@@ -123,6 +124,7 @@ var errorToString = map[int]string{
ErrInvalidMsgCode: "Invalid message code",
ErrProtocolVersionMismatch: "Protocol version mismatch",
ErrNetworkIdMismatch: "NetworkId mismatch",
+ ErrDMomentMismatch: "DMoment mismatch",
ErrGenesisBlockMismatch: "Genesis block mismatch",
ErrNoStatusMsg: "No status message",
ErrExtraStatusMsg: "Extra status message",
@@ -177,6 +179,7 @@ type p2pServer interface {
type statusData struct {
ProtocolVersion uint32
NetworkId uint64
+ DMoment uint64
Number uint64
CurrentBlock common.Hash
GenesisBlock common.Hash
diff --git a/dex/protocol_test.go b/dex/protocol_test.go
index e9bd4e110..5c68785e8 100644
--- a/dex/protocol_test.go
+++ b/dex/protocol_test.go
@@ -69,15 +69,19 @@ func testStatusMsgErrors(t *testing.T, protocol int) {
wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
},
{
- code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, number, head.Hash(), genesis.Hash()},
+ code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, dMoment, number, head.Hash(), genesis.Hash()},
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol),
},
{
- code: StatusMsg, data: statusData{uint32(protocol), 999, number, head.Hash(), genesis.Hash()},
+ code: StatusMsg, data: statusData{uint32(protocol), 999, dMoment, number, head.Hash(), genesis.Hash()},
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 237)"),
},
{
- code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, number, head.Hash(), common.Hash{3}},
+ code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, 123450, number, head.Hash(), genesis.Hash()},
+ wantError: errResp(ErrDMomentMismatch, "123450 (!= %d)", dMoment),
+ },
+ {
+ code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, dMoment, number, head.Hash(), common.Hash{3}},
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000 (!= %x)", genesis.Hash().Bytes()[:8]),
},
}