Skip to content

Commit 20ab9ce

Browse files
committed
batch update 2019-04-20
1 parent 2006da6 commit 20ab9ce

File tree

5 files changed

+139
-45
lines changed

5 files changed

+139
-45
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ clean:
5252
.PHONY: run
5353
run:
5454
$(GOBUILD) -o ./bin/$(BUILD_TARGET_SERVER) -v ./$(BUILD_TARGET_SERVER)
55-
./bin/$(BUILD_TARGET_SERVER) -stderrthreshold=WARNING -log_dir=./log
55+
./bin/$(BUILD_TARGET_SERVER) -stderrthreshold=WARNING -log_dir=./log -config=e2etests/config_local_delegate.yaml

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![Go version](https://img.shields.io/badge/go-1.9.2-blue.svg)](https://github.com/moovweb/gvm)
2-
[![CircleCI](https://circleci.com/gh/iotexproject/iotex-core/tree/master.svg?style=svg)](https://circleci.com/gh/iotexproject/iotex-core/tree/master)
2+
[![CircleCI](https://circleci.com/gh/iotexproject/iotex-core/tree/master.svg?style=svg&circle-token=905df94e89f57084aee6a0a2a57d14c44e37bccb)](https://circleci.com/gh/iotexproject/iotex-core/tree/master)
33

44
# iotex-core
55
Welcome to the official Go implementation of IoTeX protocol! IoTeX is building the next generation of the decentralized

txpool/txpool.go

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,47 +66,6 @@ func NewTxSourcePointer(in *blockchain.TxInput) TxSourcePointer {
6666
}
6767
}
6868

69-
type txDescPriorityQueue []*TxDesc
70-
71-
func (pq txDescPriorityQueue) Len() int {
72-
return len(pq)
73-
}
74-
75-
func (pq txDescPriorityQueue) Less(i, j int) bool {
76-
return pq[i].Priority > pq[j].Priority
77-
}
78-
79-
func (pq txDescPriorityQueue) Swap(i, j int) {
80-
pq[i], pq[j] = pq[j], pq[i]
81-
pq[i].idx = i
82-
pq[j].idx = j
83-
}
84-
85-
func (pq *txDescPriorityQueue) Push(x interface{}) {
86-
n := len(*pq)
87-
txDesc := x.(*TxDesc)
88-
txDesc.idx = n
89-
*pq = append(*pq, txDesc)
90-
}
91-
92-
func (pq *txDescPriorityQueue) Pop() interface{} {
93-
n := len(*pq)
94-
old := *pq
95-
txDesc := old[n-1]
96-
txDesc.idx = -1
97-
*pq = old[0 : n-1]
98-
return txDesc
99-
}
100-
101-
func (pq *txDescPriorityQueue) Delete(i int) interface{} {
102-
n := len(*pq)
103-
pq.Swap(i, n-1)
104-
txDesc := pq.Pop()
105-
heap.Fix(pq, i)
106-
107-
return txDesc
108-
}
109-
11069
// TxPool is a pool of received txs
11170
type TxPool interface {
11271
// RemoveOrphanTx remove an orphan transaction, but not its descendants
@@ -388,8 +347,8 @@ func (tp *txPool) removeTx(tx *blockchain.Tx, removeDescendants bool) {
388347
delete(tp.txSourcePointers, NewTxSourcePointer(txIn))
389348
}
390349

391-
// TODO: soft delete desc from txDescPriorityQueue
392-
//tp.txDescPriorityQueue.Delete(desc.idx)
350+
// Use the heap built-in Remove() to remove TxDesc pointer from txDescPriorityQueue
351+
heap.Remove(&tp.txDescPriorityQueue, desc.idx)
393352
delete(tp.txDescs, hash)
394353
tp.setLastUpdateUnixTime()
395354
}

txpool/txpq.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2018 IoTeX
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided ‘as is’ and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package txpool
8+
9+
type txDescPriorityQueue []*TxDesc
10+
11+
func (pq txDescPriorityQueue) Len() int {
12+
return len(pq)
13+
}
14+
15+
func (pq txDescPriorityQueue) Less(i, j int) bool {
16+
return pq[i].Priority > pq[j].Priority
17+
}
18+
19+
func (pq txDescPriorityQueue) Swap(i, j int) {
20+
pq[i], pq[j] = pq[j], pq[i]
21+
pq[i].idx = i
22+
pq[j].idx = j
23+
}
24+
25+
func (pq *txDescPriorityQueue) Push(x interface{}) {
26+
n := len(*pq)
27+
txDesc := x.(*TxDesc)
28+
txDesc.idx = n
29+
*pq = append(*pq, txDesc)
30+
}
31+
32+
func (pq *txDescPriorityQueue) Pop() interface{} {
33+
n := len(*pq)
34+
old := *pq
35+
txDesc := old[n-1]
36+
txDesc.idx = -1
37+
*pq = old[0 : n-1]
38+
return txDesc
39+
}

txpool/txpq_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2018 IoTeX
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided ‘as is’ and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package txpool
8+
9+
import (
10+
"container/heap"
11+
"math/rand"
12+
"testing"
13+
"time"
14+
15+
"github.com/iotexproject/iotex-core/blockchain"
16+
)
17+
18+
func TestTxPq(t *testing.T) {
19+
// Create four dummy TxDescs
20+
// TxDesc1
21+
desc1 := TxDesc{
22+
Tx: &blockchain.Tx{},
23+
AddedTime: time.Now(),
24+
BlockHeight: uint32(1),
25+
Fee: int64(0),
26+
FeePerKB: int64(0),
27+
Priority: float64(100),
28+
}
29+
30+
// TxDesc2
31+
desc2 := TxDesc{
32+
Tx: &blockchain.Tx{},
33+
AddedTime: time.Now(),
34+
BlockHeight: uint32(1),
35+
Fee: int64(0),
36+
FeePerKB: int64(0),
37+
Priority: float64(50),
38+
}
39+
40+
// TxDesc3
41+
desc3 := TxDesc{
42+
Tx: &blockchain.Tx{},
43+
AddedTime: time.Now(),
44+
BlockHeight: uint32(1),
45+
Fee: int64(0),
46+
FeePerKB: int64(0),
47+
Priority: float64(1000),
48+
}
49+
50+
// TxDesc4
51+
desc4 := TxDesc{
52+
Tx: &blockchain.Tx{},
53+
AddedTime: time.Now(),
54+
BlockHeight: uint32(1),
55+
Fee: int64(0),
56+
FeePerKB: int64(0),
57+
Priority: float64(5),
58+
}
59+
60+
// Initialize a txDescPriorityQueue
61+
pq := txDescPriorityQueue{}
62+
63+
// Test Push implementation
64+
heap.Push(&pq, &desc1)
65+
heap.Push(&pq, &desc2)
66+
heap.Push(&pq, &desc3)
67+
heap.Push(&pq, &desc4)
68+
69+
t.Log("The priority of each TxDesc in the order of popped is as follows:")
70+
71+
// Test Pop implementation
72+
for pq.Len() > 0 {
73+
txDesc := heap.Pop(&pq).(*TxDesc)
74+
t.Log(txDesc.Priority)
75+
t.Log()
76+
}
77+
78+
// Repush the four dummy TxDescs back to the txDescPriorityQueue
79+
heap.Push(&pq, &desc1)
80+
heap.Push(&pq, &desc2)
81+
heap.Push(&pq, &desc3)
82+
heap.Push(&pq, &desc4)
83+
84+
// Test built-in Remove implementation
85+
// Remove a random *txDesc from txDescPriorityQueue
86+
rand.Seed(time.Now().UnixNano())
87+
heap.Remove(&pq, rand.Intn(pq.Len()))
88+
89+
t.Log("After randomly removing a TxDesc, the priority of each remaining TxDesc in the order of popped is as follows:")
90+
91+
for pq.Len() > 0 {
92+
txDesc := heap.Pop(&pq).(*TxDesc)
93+
t.Log(txDesc.Priority)
94+
t.Log()
95+
}
96+
}

0 commit comments

Comments
 (0)