diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-09-11 13:28:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-11 13:28:44 +0800 |
commit | 582a491aa0bcb784ac7b65ebbfb42139945ea703 (patch) | |
tree | 8589bad986f512455717728012c3d9edf3b68c4f /core/test/blocks-generator.go | |
parent | 2439f49063d8498eadf26d4fa1220c5eac8412a8 (diff) | |
download | dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.gz dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.bz2 dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.lz dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.xz dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.zst dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.zip |
core: timestamp (#98)
Diffstat (limited to 'core/test/blocks-generator.go')
-rw-r--r-- | core/test/blocks-generator.go | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/core/test/blocks-generator.go b/core/test/blocks-generator.go index 18e4995..f3e914e 100644 --- a/core/test/blocks-generator.go +++ b/core/test/blocks-generator.go @@ -70,25 +70,33 @@ func (vs *validatorStatus) getAckedBlockHash( // validatorSetStatus is a state holder for all validators // during generating blocks. type validatorSetStatus struct { - status map[types.ValidatorID]*validatorStatus - validatorIDs []types.ValidatorID - randGen *rand.Rand - hashBlock hashBlockFn + status map[types.ValidatorID]*validatorStatus + proposerChain map[types.ValidatorID]uint32 + timestamps []time.Time + validatorIDs []types.ValidatorID + randGen *rand.Rand + hashBlock hashBlockFn } func newValidatorSetStatus(vIDs []types.ValidatorID, hashBlock hashBlockFn) *validatorSetStatus { status := make(map[types.ValidatorID]*validatorStatus) - for _, vID := range vIDs { + timestamps := make([]time.Time, 0, len(vIDs)) + proposerChain := make(map[types.ValidatorID]uint32) + for i, vID := range vIDs { status[vID] = &validatorStatus{ blocks: []*types.Block{}, lastAckingHeight: make(map[types.ValidatorID]uint64), } + timestamps = append(timestamps, time.Now().UTC()) + proposerChain[vID] = uint32(i) } return &validatorSetStatus{ - status: status, - validatorIDs: vIDs, - randGen: rand.New(rand.NewSource(time.Now().UnixNano())), - hashBlock: hashBlock, + status: status, + proposerChain: proposerChain, + timestamps: timestamps, + validatorIDs: vIDs, + randGen: rand.New(rand.NewSource(time.Now().UnixNano())), + hashBlock: hashBlock, } } @@ -150,20 +158,18 @@ func (vs *validatorSetStatus) proposeBlock( if len(status.blocks) > 0 { parentHash = status.blocks[len(status.blocks)-1].Hash } + chainID := vs.proposerChain[proposerID] + vs.timestamps[chainID] = vs.timestamps[chainID].Add(time.Second) - ts := map[types.ValidatorID]time.Time{} - for vid := range vs.status { - ts[vid] = time.Time{} - } newBlock := &types.Block{ ProposerID: proposerID, ParentHash: parentHash, Position: types.Position{ - Height: uint64(len(status.blocks)), + Height: uint64(len(status.blocks)), + ChainID: chainID, }, - Acks: acks, - Timestamps: ts, - // TODO(mission.liao): Generate timestamp. + Acks: acks, + Timestamp: vs.timestamps[chainID], } for i, vID := range vs.validatorIDs { if vID == proposerID { |