-
Notifications
You must be signed in to change notification settings - Fork 319
/
actioniterator.go
104 lines (88 loc) · 3.04 KB
/
actioniterator.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package actioniterator
import (
"container/heap"
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/pkg/keypair"
"github.com/iotexproject/iotex-core/action"
)
// ActionByPrice implements both the sort and the heap interface, making it useful
// for all at once sorting as well as individually adding and removing elements.
// It's essentially a big root heap of actions
type actionByPrice []action.SealedEnvelope
func (s actionByPrice) Len() int { return len(s) }
func (s actionByPrice) Less(i, j int) bool { return s[i].GasPrice().Cmp(s[j].GasPrice()) > 0 }
func (s actionByPrice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Push define the push function of heap
func (s *actionByPrice) Push(x interface{}) {
*s = append(*s, x.(action.SealedEnvelope))
}
// Pop define the pop function of heap
func (s *actionByPrice) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}
// ActionIterator define the interface of action iterator
type ActionIterator interface {
Next() (action.SealedEnvelope, bool)
PopAccount()
}
type actionIterator struct {
accountActs map[string][]action.SealedEnvelope
heads actionByPrice
}
// NewActionIterator return a new action iterator
func NewActionIterator(accountActs map[string][]action.SealedEnvelope) ActionIterator {
heads := make(actionByPrice, 0, len(accountActs))
for sender, accActs := range accountActs {
if len(accActs) == 0 {
continue
}
heads = append(heads, accActs[0])
if len(accActs) > 1 {
accountActs[sender] = accActs[1:]
} else {
accountActs[sender] = []action.SealedEnvelope{}
}
}
heap.Init(&heads)
return &actionIterator{
accountActs: accountActs,
heads: heads,
}
}
// LoadNext load next action of account of top action
func (ai *actionIterator) loadNextActionForTopAccount() {
sender := ai.heads[0].SrcPubkey()
callerPKHash := keypair.HashPubKey(sender)
callerAddr, _ := address.FromBytes(callerPKHash[:])
callerAddrStr := callerAddr.String()
if actions, ok := ai.accountActs[callerAddrStr]; ok && len(actions) > 0 {
ai.heads[0], ai.accountActs[callerAddrStr] = actions[0], actions[1:]
heap.Fix(&ai.heads, 0)
} else {
heap.Pop(&ai.heads)
}
}
// Next load next action of account of top action
func (ai *actionIterator) Next() (action.SealedEnvelope, bool) {
if len(ai.heads) == 0 {
return action.SealedEnvelope{}, false
}
headAction := ai.heads[0]
ai.loadNextActionForTopAccount()
return headAction, true
}
// PopAccount will remove all actions related to this account
func (ai *actionIterator) PopAccount() {
if len(ai.heads) != 0 {
heap.Pop(&ai.heads)
}
}