-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtxpool.go
153 lines (134 loc) · 4.53 KB
/
txpool.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package rpcapi
import (
"fmt"
"math/big"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/types"
)
// PrivateTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential.
type PrivateTxPoolAPI struct {
b Backend
}
// NewPrivateTxPoolAPI creates a new tx pool service that gives information about the transaction pool.
func NewPrivateTxPoolAPI(b Backend) *PrivateTxPoolAPI {
return &PrivateTxPoolAPI{b}
}
// Status returns the number of pending and queued transaction in the pool.
func (s *PrivateTxPoolAPI) Status() map[string]int {
pending, queue := s.b.TxPool().Stats()
return map[string]int{
"pending": pending,
"queued": queue,
}
}
// Content returns the transactions contained within the transaction pool.
func (s *PrivateTxPoolAPI) Content(fullTx bool) interface{} {
content := map[string]map[string]map[string]interface{}{
"pending": make(map[string]map[string]interface{}),
"queued": make(map[string]map[string]interface{}),
}
pending, queue := s.b.TxPool().Content()
// Flatten the pending transactions
for account, txs := range pending {
dump := make(map[string]interface{})
for _, tx := range txs {
if fullTx {
dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.NewRPCTransaction(common.Hash{}, 0, 0)
} else {
dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.Hash()
}
}
content["pending"][account.String()] = dump
}
// Flatten the queued transactions
for account, txs := range queue {
dump := make(map[string]interface{})
for _, tx := range txs {
if fullTx {
dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.NewRPCTransaction(common.Hash{}, 0, 0)
} else {
dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.Hash()
}
}
content["queued"][account.String()] = dump
}
return content
}
// PendingTransactions returns the pending transactions that are in the transaction pool.
func (s *PrivateTxPoolAPI) PendingTransactions(fullTx bool) (interface{}, error) {
pending, err := s.b.TxPool().Pending()
if err != nil {
return nil, err
}
var (
txs []*types.RPCTransaction
txsHashes []common.Hash
)
for _, batch := range pending {
for _, tx := range batch {
if fullTx {
txs = append(txs, tx.NewRPCTransaction(common.Hash{}, 0, 0))
} else {
txsHashes = append(txsHashes, tx.Hash())
}
}
}
if fullTx {
return txs, nil
}
return txsHashes, nil
}
// GetTransactions txpool returns the transaction by the given hash.
func (s *PrivateTxPoolAPI) GetTransactions(hashes []common.Hash) []*types.RPCTransaction {
var txs []*types.RPCTransaction
for _, hash := range hashes {
if tx := s.b.TxPool().Get(hash); tx != nil {
txs = append(txs, tx.NewRPCTransaction(common.Hash{}, 0, 0))
}
}
return txs
}
// GetTransactionsByAccount txpool returns the transaction by the given account name.
func (s *PrivateTxPoolAPI) GetTransactionsByAccount(name common.Name, fullTx bool) interface{} {
content := map[string]map[string]interface{}{
"pending": make(map[string]interface{}),
"queued": make(map[string]interface{}),
}
txsFunc := func(name common.Name, m map[common.Name][]*types.Transaction, fullTx bool) map[string]interface{} {
dump := make(map[string]interface{})
txs, ok := m[name]
if ok {
for _, tx := range txs {
if fullTx {
dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.NewRPCTransaction(common.Hash{}, 0, 0)
} else {
dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.Hash()
}
}
}
return dump
}
pending, queue := s.b.TxPool().Content()
content["pending"] = txsFunc(name, pending, fullTx)
content["queued"] = txsFunc(name, queue, fullTx)
return content
}
// SetGasPrice set txpool gas price
func (s *PrivateTxPoolAPI) SetGasPrice(gasprice *big.Int) bool {
return s.b.SetGasPrice(gasprice)
}