From 64f26af59d24881bcdd49bbdd291c1a21a12b82d Mon Sep 17 00:00:00 2001 From: Wei-Ning Huang Date: Sun, 17 Mar 2019 10:43:10 +0800 Subject: core: fill in genesis timstamp and remove dMoment from protocol handshake (#263) Fill in dmoment as genesis block timestamp. This allow us to remove dMoment check from protocol handshake since genesis block hash itself will protect us against different dMoment. --- cmd/gdex/consolecmd_test.go | 4 ++-- core/genesis.go | 5 ++++- core/genesis_test.go | 10 ++++++++++ dex/backend.go | 4 ++-- dex/handler.go | 6 ++---- dex/helper_test.go | 9 +++------ dex/peer.go | 10 +++------- dex/protocol.go | 3 --- dex/protocol_test.go | 10 +++------- params/config.go | 7 ++++--- 10 files changed, 33 insertions(+), 35 deletions(-) diff --git a/cmd/gdex/consolecmd_test.go b/cmd/gdex/consolecmd_test.go index 672735d2c..c1174f9e6 100644 --- a/cmd/gdex/consolecmd_test.go +++ b/cmd/gdex/consolecmd_test.go @@ -51,7 +51,7 @@ func TestConsoleWelcome(t *testing.T) { gdex.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) gdex.SetTemplateFunc("gover", runtime.Version) gdex.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta }) - gdex.SetTemplateFunc("dextime", func() string { return time.Unix(1540024964, 0).Format(time.RFC1123) }) + gdex.SetTemplateFunc("dextime", func() string { return time.Unix(int64(params.MainnetChainConfig.DMoment), 0).Format(time.RFC1123) }) gdex.SetTemplateFunc("apis", func() string { return ipcAPIs }) // Verify the actual welcome message to the required template @@ -134,7 +134,7 @@ func testAttachWelcome(t *testing.T, gdex *testgdex, endpoint, apis string) { attach.SetTemplateFunc("gover", runtime.Version) attach.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta }) attach.SetTemplateFunc("etherbase", func() string { return gdex.Etherbase }) - attach.SetTemplateFunc("dextime", func() string { return time.Unix(1540024964, 0).Format(time.RFC1123) }) + attach.SetTemplateFunc("dextime", func() string { return time.Unix(int64(params.MainnetChainConfig.DMoment), 0).Format(time.RFC1123) }) attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) attach.SetTemplateFunc("datadir", func() string { return gdex.Datadir }) attach.SetTemplateFunc("apis", func() string { return apis }) diff --git a/core/genesis.go b/core/genesis.go index f2df4f62f..10585bab4 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -384,7 +384,7 @@ func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big func DefaultGenesisBlock() *Genesis { return &Genesis{ Config: params.MainnetChainConfig, - Timestamp: 1540024964, + Timestamp: params.MainnetChainConfig.DMoment, Nonce: 0x42, ExtraData: hexutil.MustDecode("0x5765692d4e696e6720536f6e696320426f6a696520323031382d31302d32302e"), GasLimit: 40000000, @@ -397,6 +397,7 @@ func DefaultGenesisBlock() *Genesis { func DefaultTestnetGenesisBlock() *Genesis { return &Genesis{ Config: params.TestnetChainConfig, + Timestamp: params.TestnetChainConfig.DMoment, Nonce: 0x42, ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"), GasLimit: 40000000, @@ -409,6 +410,7 @@ func DefaultTestnetGenesisBlock() *Genesis { func DefaultTaipeiGenesisBlock() *Genesis { return &Genesis{ Config: params.TaipeiChainConfig, + Timestamp: params.TaipeiChainConfig.DMoment, Nonce: 0x42, ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"), GasLimit: 40000000, @@ -421,6 +423,7 @@ func DefaultTaipeiGenesisBlock() *Genesis { func DefaultYilanGenesisBlock() *Genesis { return &Genesis{ Config: params.YilanChainConfig, + Timestamp: params.YilanChainConfig.DMoment, Nonce: 0x42, ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"), GasLimit: 40000000, diff --git a/core/genesis_test.go b/core/genesis_test.go index de843e05a..16958eac3 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -104,6 +104,16 @@ func TestSetupGenesis(t *testing.T) { wantHash: params.TestnetGenesisHash, wantConfig: params.TestnetChainConfig, }, + { + name: "custom block in DB, genesis == taipei", + fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { + customg.MustCommit(db) + return SetupGenesisBlock(db, DefaultTaipeiGenesisBlock()) + }, + wantErr: &GenesisMismatchError{Stored: customghash, New: params.TaipeiGenesisHash}, + wantHash: params.TaipeiGenesisHash, + wantConfig: params.TaipeiChainConfig, + }, { name: "custom block in DB, genesis == yilan", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { diff --git a/dex/backend.go b/dex/backend.go index 6ee1a5fa1..0f68163c0 100644 --- a/dex/backend.go +++ b/dex/backend.go @@ -172,8 +172,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) { } pm, err := NewProtocolManager(dex.chainConfig, config.SyncMode, - config.NetworkId, chainConfig.DMoment, dex.eventMux, dex.txPool, dex.engine, - dex.blockchain, chainDb, config.BlockProposerEnabled, dex.governance, dex.app) + config.NetworkId, 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 76219c747..ea04e6fbc 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -97,7 +97,6 @@ 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) @@ -158,14 +157,13 @@ type ProtocolManager struct { // with the Ethereum network. func NewProtocolManager( config *params.ChainConfig, mode downloader.SyncMode, networkID uint64, - dMoment uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, + 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, @@ -355,7 +353,7 @@ func (pm *ProtocolManager) handle(p *peer) error { hash = head.Hash() number = head.Number.Uint64() ) - if err := p.Handshake(pm.networkID, pm.dMoment, number, hash, genesis.Hash()); err != nil { + if err := p.Handshake(pm.networkID, 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 c8bf62a6b..4f5541052 100644 --- a/dex/helper_test.go +++ b/dex/helper_test.go @@ -46,8 +46,6 @@ 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 @@ -135,7 +133,7 @@ func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func notarySetFunc: func(uint64) (map[string]struct{}, error) { return nil, nil }, } - pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, dMoment, evmux, &testTxPool{added: newtx}, engine, blockchain, db, true, tgov, &testApp{}) + pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx}, engine, blockchain, db, true, tgov, &testApp{}) if err != nil { return nil, nil, err } @@ -275,18 +273,17 @@ func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*te head = pm.blockchain.CurrentHeader() number = head.Number.Uint64() ) - tp.handshake(nil, dMoment, number, head.Hash(), genesis.Hash()) + tp.handshake(nil, 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, dMoment uint64, number uint64, head common.Hash, genesis common.Hash) { +func (p *testPeer) handshake(t *testing.T, 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 6a5786e1f..e015ed9e5 100644 --- a/dex/peer.go +++ b/dex/peer.go @@ -669,7 +669,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, dMoment uint64, number uint64, head common.Hash, genesis common.Hash) error { +func (p *peer) Handshake(network 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 @@ -678,14 +678,13 @@ func (p *peer) Handshake(network uint64, dMoment uint64, number uint64, head com 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, dMoment, &status, genesis) + errc <- p.readStatus(network, &status, genesis) }() timeout := time.NewTimer(handshakeTimeout) defer timeout.Stop() @@ -703,7 +702,7 @@ func (p *peer) Handshake(network uint64, dMoment uint64, number uint64, head com return nil } -func (p *peer) readStatus(network uint64, dMoment uint64, status *statusData, genesis common.Hash) (err error) { +func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash) (err error) { msg, err := p.rw.ReadMsg() if err != nil { return err @@ -724,9 +723,6 @@ func (p *peer) readStatus(network uint64, dMoment uint64, status *statusData, ge 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 afa6e560d..e09829cc8 100644 --- a/dex/protocol.go +++ b/dex/protocol.go @@ -106,7 +106,6 @@ const ( ErrInvalidMsgCode ErrProtocolVersionMismatch ErrNetworkIdMismatch - ErrDMomentMismatch ErrGenesisBlockMismatch ErrNoStatusMsg ErrExtraStatusMsg @@ -129,7 +128,6 @@ 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", @@ -180,7 +178,6 @@ 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 422824559..23b2c4248 100644 --- a/dex/protocol_test.go +++ b/dex/protocol_test.go @@ -69,19 +69,15 @@ func testStatusMsgErrors(t *testing.T, protocol int) { wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"), }, { - code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, dMoment, number, head.Hash(), genesis.Hash()}, + code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, number, head.Hash(), genesis.Hash()}, wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol), }, { - code: StatusMsg, data: statusData{uint32(protocol), 999, dMoment, number, head.Hash(), genesis.Hash()}, + code: StatusMsg, data: statusData{uint32(protocol), 999, number, head.Hash(), genesis.Hash()}, wantError: errResp(ErrNetworkIdMismatch, "999 (!= 237)"), }, { - 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}}, + code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, number, head.Hash(), common.Hash{3}}, wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000 (!= %x)", genesis.Hash().Bytes()[:8]), }, } diff --git a/params/config.go b/params/config.go index 2ebbfd607..679428b4e 100644 --- a/params/config.go +++ b/params/config.go @@ -26,9 +26,10 @@ import ( // Genesis hashes to enforce below configs on. var ( - MainnetGenesisHash = common.HexToHash("0x90f3ee1749d01fe355e8f3e72d2851df4424a0eeb1a97bd49df6d18206726100") - TestnetGenesisHash = common.HexToHash("0x736a4d8f7cb6756c239ef0cb7b788fb500f9fec186eb1c29a735556ffd2a965a") - YilanGenesisHash = common.HexToHash("0x35c657b55ee61ffb9b4dbbea43507693da612a0bc89b8713f6865cfd3ed5f2e9") + MainnetGenesisHash = common.HexToHash("0xc3320be1e1e83f6e2116ec3f46966502a09655d8939967450c345507350bedc8") + TestnetGenesisHash = common.HexToHash("0xe338c08039a92fb146c4af646ecbae4a50aac048249894d4eebb2c54ab369e06") + TaipeiGenesisHash = common.HexToHash("0xc7bef94f43a1350da382b67d0bc7dd32ceb30b7c33820afc74c3716a2189a1e4") + YilanGenesisHash = common.HexToHash("0xae0aac389776b6e35aff2569a2b9be234f6703a5c923e48cf42edffa7c1a24fc") ) var ( -- cgit