From bec97aadfa95ebb42ef042bd53c7976ae410c496 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Fri, 19 Oct 2018 18:29:46 +0800 Subject: misc: Fix simulation error (#230) --- core/consensus-timestamp.go | 2 +- core/consensus-timestamp_test.go | 36 +++++++++++++++++++++------------ core/test/tcp-transport.go | 12 +++++------ simulation/app.go | 7 ++++--- simulation/app_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ simulation/node.go | 2 +- 6 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 simulation/app_test.go diff --git a/core/consensus-timestamp.go b/core/consensus-timestamp.go index dc6d2a8..49270d4 100644 --- a/core/consensus-timestamp.go +++ b/core/consensus-timestamp.go @@ -110,7 +110,7 @@ func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) (err error) { if block.Finalization.Timestamp, err = getMedianTime(ts); err != nil { return } - if !block.Timestamp.After(ct.chainTimestamps[block.Position.ChainID]) { + if block.Timestamp.Before(ct.chainTimestamps[block.Position.ChainID]) { return ErrTimestampNotIncrease } ct.chainTimestamps[block.Position.ChainID] = block.Timestamp diff --git a/core/consensus-timestamp_test.go b/core/consensus-timestamp_test.go index 0f2c526..5fdb163 100644 --- a/core/consensus-timestamp_test.go +++ b/core/consensus-timestamp_test.go @@ -34,6 +34,7 @@ type ConsensusTimestampTest struct { } func (s *ConsensusTimestampTest) generateBlocksWithTimestamp( + now time.Time, blockNum, chainNum int, step, sigma time.Duration) []*types.Block { blocks := make([]*types.Block, blockNum) @@ -54,7 +55,7 @@ func (s *ConsensusTimestampTest) generateBlocksWithTimestamp( block.ParentHash = common.Hash{} block.Position.Height = 0 s.Require().True(block.IsGenesis()) - chainTimestamps[uint32(idx)] = time.Now().UTC() + chainTimestamps[uint32(idx)] = now } else { block.Position.ChainID = chainIDs[idx] // Assign 1 to height to make this block non-genesis. @@ -94,12 +95,13 @@ func (s *ConsensusTimestampTest) TestTimestampPartition() { chainNum := 19 sigma := 100 * time.Millisecond totalTimestamps := make([]time.Time, 0) - ct := newConsensusTimestamp(time.Time{}, 0, uint32(chainNum)) + now := time.Now().UTC() + ct := newConsensusTimestamp(now, 0, uint32(chainNum)) totalBlockNum := 0 for _, blockNum := range blockNums { totalBlockNum += blockNum } - totalChain := s.generateBlocksWithTimestamp( + totalChain := s.generateBlocksWithTimestamp(now, totalBlockNum, chainNum, time.Second, sigma) for _, blockNum := range blockNums { var chain []*types.Block @@ -110,7 +112,7 @@ func (s *ConsensusTimestampTest) TestTimestampPartition() { totalChain = append(totalChain, chain...) totalTimestamps = append(totalTimestamps, timestamps...) } - ct2 := newConsensusTimestamp(time.Time{}, 0, uint32(chainNum)) + ct2 := newConsensusTimestamp(now, 0, uint32(chainNum)) err := ct2.processBlocks(totalChain) s.Require().NoError(err) timestamps2 := s.extractTimestamps(totalChain) @@ -120,8 +122,10 @@ func (s *ConsensusTimestampTest) TestTimestampPartition() { func (s *ConsensusTimestampTest) TestTimestampIncrease() { chainNum := 19 sigma := 100 * time.Millisecond - ct := newConsensusTimestamp(time.Time{}, 0, uint32(chainNum)) - chain := s.generateBlocksWithTimestamp(1000, chainNum, time.Second, sigma) + now := time.Now().UTC() + ct := newConsensusTimestamp(now, 0, uint32(chainNum)) + chain := s.generateBlocksWithTimestamp( + now, 1000, chainNum, time.Second, sigma) err := ct.processBlocks(chain) s.Require().NoError(err) timestamps := s.extractTimestamps(chain) @@ -129,7 +133,7 @@ func (s *ConsensusTimestampTest) TestTimestampIncrease() { s.False(timestamps[i].Before(timestamps[i-1])) } // Test if the processBlocks is stable. - ct2 := newConsensusTimestamp(time.Time{}, 0, uint32(chainNum)) + ct2 := newConsensusTimestamp(now, 0, uint32(chainNum)) ct2.processBlocks(chain) s.Require().NoError(err) timestamps2 := s.extractTimestamps(chain) @@ -139,8 +143,10 @@ func (s *ConsensusTimestampTest) TestTimestampIncrease() { func (s *ConsensusTimestampTest) TestTimestampConfigChange() { chainNum := 19 sigma := 100 * time.Millisecond - ct := newConsensusTimestamp(time.Time{}, 20, uint32(chainNum)) - chain := s.generateBlocksWithTimestamp(1000, chainNum, time.Second, sigma) + now := time.Now().UTC() + ct := newConsensusTimestamp(now, 20, uint32(chainNum)) + chain := s.generateBlocksWithTimestamp(now, + 1000, chainNum, time.Second, sigma) blocks := make([]*types.Block, 0, 1000) ct.appendConfig(21, &types.Config{NumChains: uint32(16)}) ct.appendConfig(22, &types.Config{NumChains: uint32(19)}) @@ -167,9 +173,11 @@ func (s *ConsensusTimestampTest) TestTimestampConfigChange() { func (s *ConsensusTimestampTest) TestRoundInterleave() { chainNum := 9 sigma := 100 * time.Millisecond - ct := newConsensusTimestamp(time.Time{}, 0, uint32(chainNum)) + now := time.Now().UTC() + ct := newConsensusTimestamp(now, 0, uint32(chainNum)) ct.appendConfig(1, &types.Config{NumChains: uint32(chainNum)}) - chain := s.generateBlocksWithTimestamp(100, chainNum, time.Second, sigma) + chain := s.generateBlocksWithTimestamp(now, + 100, chainNum, time.Second, sigma) for i := 50; i < 100; i++ { chain[i].Position.Round = 1 } @@ -184,8 +192,10 @@ func (s *ConsensusTimestampTest) TestRoundInterleave() { func (s *ConsensusTimestampTest) TestTimestampSync() { chainNum := 19 sigma := 100 * time.Millisecond - ct := newConsensusTimestamp(time.Time{}, 0, uint32(chainNum)) - chain := s.generateBlocksWithTimestamp(100, chainNum, time.Second, sigma) + now := time.Now().UTC() + ct := newConsensusTimestamp(now, 0, uint32(chainNum)) + chain := s.generateBlocksWithTimestamp(now, + 100, chainNum, time.Second, sigma) err := ct.processBlocks(chain[:chainNum-1]) s.Require().NoError(err) s.Require().False(ct.isSynced()) diff --git a/core/test/tcp-transport.go b/core/test/tcp-transport.go index f6a5e9e..34c1996 100644 --- a/core/test/tcp-transport.go +++ b/core/test/tcp-transport.go @@ -1,15 +1,15 @@ // Copyright 2018 The dexon-consensus-core Authors // This file is part of the dexon-consensus-core library. // -// The dexon-consensus-core library is free software: you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as +// The dexon-consensus-core 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-core 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. +// The dexon-consensus-core 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-core library. If not, see diff --git a/simulation/app.go b/simulation/app.go index 16bb63a..f8bbcc6 100644 --- a/simulation/app.go +++ b/simulation/app.go @@ -61,9 +61,9 @@ func newSimApp(id types.NodeID, netModule *network) *simApp { func (a *simApp) BlockConfirmed(block types.Block) { a.blockByHashMutex.Lock() defer a.blockByHashMutex.Unlock() - // TODO(jimmy-dexon) : Remove block in this hash if it's no longer needed. a.blockByHash[block.Hash] = &block + a.blockSeen[block.Hash] = time.Now().UTC() } // VerifyBlock implements core.Application. @@ -119,8 +119,9 @@ func (a *simApp) StronglyAcked(blockHash common.Hash) { // TotalOrderingDelivered is called when blocks are delivered by the total // ordering algorithm. -func (a *simApp) TotalOrderingDelivered(blockHashes common.Hashes, early bool) { - fmt.Println("OUTPUT", a.NodeID, early, blockHashes) +func (a *simApp) TotalOrderingDelivered( + blockHashes common.Hashes, mode uint32) { + fmt.Println("OUTPUT", a.NodeID, mode, blockHashes) blockList := &BlockList{ ID: a.DeliverID, BlockHash: blockHashes, diff --git a/simulation/app_test.go b/simulation/app_test.go new file mode 100644 index 0000000..e085de5 --- /dev/null +++ b/simulation/app_test.go @@ -0,0 +1,43 @@ +// Copyright 2018 The dexon-consensus-core Authors +// This file is part of the dexon-consensus-core library. +// +// The dexon-consensus-core 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-core 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-core library. If not, see +// . + +package simulation + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/dexon-foundation/dexon-consensus-core/core" + "github.com/dexon-foundation/dexon-consensus-core/core/types" +) + +type SimAppSuite struct { + suite.Suite +} + +func (s *SimAppSuite) TestAppInterface() { + var app core.Application + app = newSimApp(types.NodeID{}, nil) + s.NotPanics(func() { + _ = app.(core.Debug) + }) +} + +func TestSimApp(t *testing.T) { + suite.Run(t, new(SimAppSuite)) +} diff --git a/simulation/node.go b/simulation/node.go index 0040184..0a59994 100644 --- a/simulation/node.go +++ b/simulation/node.go @@ -32,7 +32,7 @@ import ( // node represents a node in DexCon. type node struct { - app *simApp + app core.Application gov *simGovernance db blockdb.BlockDatabase -- cgit v1.2.3