aboutsummaryrefslogtreecommitdiffstats
path: root/simulation/block-list.go
blob: 9e329a701e3844a7bc983dd5cfbbc403c9af2ad4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus 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 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 library. If not, see
// <http://www.gnu.org/licenses/>.

package simulation

import (
    "time"

    "github.com/dexon-foundation/dexon-consensus/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"`
    ConfirmLatency []time.Duration `json:"confirmlatency"`
    // 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
}