diff options
author | Jimmy Hu <41561308+jimmyhu-dexon@users.noreply.github.com> | 2018-07-26 12:15:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-26 12:15:17 +0800 |
commit | 39f62971de32373de715cdfb8b694832ed24806c (patch) | |
tree | 18e2d14a99adf5a7f266d8906e15925fd3c92d66 /simulation/block-list.go | |
parent | 7a60cb62e1d1b90f5a61f384acf1f19a413d4a10 (diff) | |
download | tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.tar tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.tar.gz tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.tar.bz2 tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.tar.lz tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.tar.xz tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.tar.zst tangerine-consensus-39f62971de32373de715cdfb8b694832ed24806c.zip |
Verify the Total Ordering Algorithm in peerServer in tcp mode (#11)
Verify the Total Ordering Algorithm in peerServer in tcp mode.
Diffstat (limited to 'simulation/block-list.go')
-rw-r--r-- | simulation/block-list.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/simulation/block-list.go b/simulation/block-list.go new file mode 100644 index 0000000..5bf9f20 --- /dev/null +++ b/simulation/block-list.go @@ -0,0 +1,60 @@ +// 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 +// <http://www.gnu.org/licenses/>. + +package simulation + +import ( + "github.com/dexon-foundation/dexon-consensus-core/common" +) + +// BlockList is the list of blocks from the result of Total Ordering Algorithm. +type BlockList struct { + ID int `json:"id"` + BlockHash common.Hashes `json:"blockhash"` + // The index is required by heap.Interface. + index int +} + +// PendingBlockList is a PrioirtyQueue maintaining the BlockList received before the previous one (based on ID). +type PendingBlockList []*BlockList + +// Len, Less and Swap are implementing heap.Interface +func (p PendingBlockList) Len() int { return len(p) } +func (p PendingBlockList) Less(i, j int) bool { return p[i].ID < p[j].ID } +func (p PendingBlockList) Swap(i, j int) { + p[i], p[j] = p[j], p[i] + p[i].index = i + p[j].index = j +} + +// Push item in the Heap. +func (p *PendingBlockList) Push(x interface{}) { + n := len(*p) + item := x.(*BlockList) + item.index = n + *p = append(*p, item) +} + +// Pop the element from the Heap. +func (p *PendingBlockList) Pop() interface{} { + old := *p + n := len(old) + item := old[n-1] + item.index = -1 // For safety. + *p = old[0 : n-1] + return item +} |