-
Notifications
You must be signed in to change notification settings - Fork 289
/
pool.go
337 lines (307 loc) · 10.3 KB
/
pool.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package rpc
import (
"context"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/hmy"
common2 "github.com/harmony-one/harmony/internal/common"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
eth "github.com/harmony-one/harmony/rpc/eth"
v1 "github.com/harmony-one/harmony/rpc/v1"
v2 "github.com/harmony-one/harmony/rpc/v2"
staking "github.com/harmony-one/harmony/staking/types"
)
// PublicPoolService provides an API to access the Harmony node's transaction pool.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicPoolService struct {
hmy *hmy.Harmony
version Version
}
// NewPublicPoolAPI creates a new API for the RPC interface
func NewPublicPoolAPI(hmy *hmy.Harmony, version Version) rpc.API {
return rpc.API{
Namespace: version.Namespace(),
Version: APIVersion,
Service: &PublicPoolService{hmy, version},
Public: true,
}
}
// SendRawTransaction will add the signed transaction to the transaction pool.
// The sender is responsible for signing the transaction and using the correct nonce.
func (s *PublicPoolService) SendRawTransaction(
ctx context.Context, encodedTx hexutil.Bytes,
) (common.Hash, error) {
// DOS prevention
if len(encodedTx) >= types.MaxEncodedPoolTransactionSize {
err := errors.Wrapf(core.ErrOversizedData, "encoded tx size: %d", len(encodedTx))
return common.Hash{}, err
}
var tx *types.Transaction
var txHash common.Hash
if s.version == Eth {
ethTx := new(types.EthTransaction)
if err := rlp.DecodeBytes(encodedTx, ethTx); err != nil {
return common.Hash{}, err
}
txHash = ethTx.Hash()
tx = ethTx.ConvertToHmy()
} else {
tx = new(types.Transaction)
if err := rlp.DecodeBytes(encodedTx, tx); err != nil {
return common.Hash{}, err
}
txHash = tx.Hash()
}
// Verify chainID
if err := s.verifyChainID(tx); err != nil {
return common.Hash{}, err
}
// Submit transaction
if err := s.hmy.SendTx(ctx, tx); err != nil {
utils.Logger().Warn().Err(err).Msg("Could not submit transaction")
return txHash, err
}
// Log submission
if tx.To() == nil {
signer := types.MakeSigner(s.hmy.ChainConfig(), s.hmy.CurrentBlock().Epoch())
ethSigner := types.NewEIP155Signer(s.hmy.ChainConfig().EthCompatibleChainID)
if tx.IsEthCompatible() {
signer = ethSigner
}
from, err := types.Sender(signer, tx)
if err != nil {
return common.Hash{}, err
}
addr := crypto.CreateAddress(from, tx.Nonce())
utils.Logger().Info().
Str("fullhash", tx.Hash().Hex()).
Str("hashByType", tx.HashByType().Hex()).
Str("contract", common2.MustAddressToBech32(addr)).
Msg("Submitted contract creation")
} else {
utils.Logger().Info().
Str("fullhash", tx.Hash().Hex()).
Str("hashByType", tx.HashByType().Hex()).
Str("recipient", tx.To().Hex()).
Interface("tx", tx).
Msg("Submitted transaction")
}
// Response output is the same for all versions
return txHash, nil
}
func (s *PublicPoolService) verifyChainID(tx *types.Transaction) error {
nodeChainID := s.hmy.ChainConfig().ChainID
ethChainID := nodeconfig.GetDefaultConfig().GetNetworkType().ChainConfig().EthCompatibleChainID
if tx.ChainID().Cmp(ethChainID) != 0 && tx.ChainID().Cmp(nodeChainID) != 0 {
return errors.Wrapf(
ErrInvalidChainID, "blockchain chain id:%s, given %s", nodeChainID.String(), tx.ChainID().String(),
)
}
return nil
}
// SendRawStakingTransaction will add the signed transaction to the transaction pool.
// The sender is responsible for signing the transaction and using the correct nonce.
func (s *PublicPoolService) SendRawStakingTransaction(
ctx context.Context, encodedTx hexutil.Bytes,
) (common.Hash, error) {
// DOS prevention
if len(encodedTx) >= types.MaxEncodedPoolTransactionSize {
err := errors.Wrapf(core.ErrOversizedData, "encoded tx size: %d", len(encodedTx))
return common.Hash{}, err
}
// Verify staking transaction type & chain
tx := new(staking.StakingTransaction)
if err := rlp.DecodeBytes(encodedTx, tx); err != nil {
return common.Hash{}, err
}
c := s.hmy.ChainConfig().ChainID
if id := tx.ChainID(); id.Cmp(c) != 0 {
return common.Hash{}, errors.Wrapf(
ErrInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
)
}
// Submit transaction
if err := s.hmy.SendStakingTx(ctx, tx); err != nil {
utils.Logger().Warn().Err(err).Msg("Could not submit staking transaction")
return tx.Hash(), err
}
// Log submission
utils.Logger().Info().
Str("fullhash", tx.Hash().Hex()).
Msg("Submitted Staking transaction")
// Response output is the same for all versions
return tx.Hash(), nil
}
// GetPoolStats returns stats for the tx-pool
func (s *PublicPoolService) GetPoolStats(
ctx context.Context,
) (StructuredResponse, error) {
pendingCount, queuedCount := s.hmy.GetPoolStats()
// Response output is the same for all versions
return StructuredResponse{
"executable-count": pendingCount,
"non-executable-count": queuedCount,
}, nil
}
// PendingTransactions returns the plain transactions that are in the transaction pool
func (s *PublicPoolService) PendingTransactions(
ctx context.Context,
) ([]StructuredResponse, error) {
// Fetch all pending transactions (stx & plain tx)
pending, err := s.hmy.GetPoolTransactions()
if err != nil {
return nil, err
}
// Only format and return plain transactions according to the version
transactions := []StructuredResponse{}
for i := range pending {
if plainTx, ok := pending[i].(*types.Transaction); ok {
var tx interface{}
switch s.version {
case V1:
tx, err = v1.NewTransaction(plainTx, common.Hash{}, 0, 0, 0)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingTransactions")
continue // Legacy behavior is to not return error here
}
case V2:
tx, err = v2.NewTransaction(plainTx, common.Hash{}, 0, 0, 0)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingTransactions")
continue // Legacy behavior is to not return error here
}
case Eth:
tx, err = eth.NewTransaction(plainTx, common.Hash{}, 0, 0, 0)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingTransactions")
continue // Legacy behavior is to not return error here
}
default:
return nil, ErrUnknownRPCVersion
}
rpcTx, err := NewStructuredResponse(tx)
if err == nil {
transactions = append(transactions, rpcTx)
} else {
// Legacy behavior is to not return error here
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingTransactions")
}
} else if _, ok := pending[i].(*staking.StakingTransaction); ok {
continue // Do not return staking transactions here.
} else {
return nil, types.ErrUnknownPoolTxType
}
}
return transactions, nil
}
// PendingStakingTransactions returns the staking transactions that are in the transaction pool
func (s *PublicPoolService) PendingStakingTransactions(
ctx context.Context,
) ([]StructuredResponse, error) {
// Fetch all pending transactions (stx & plain tx)
pending, err := s.hmy.GetPoolTransactions()
if err != nil {
return nil, err
}
// Only format and return staking transactions according to the version
transactions := []StructuredResponse{}
for i := range pending {
if _, ok := pending[i].(*types.Transaction); ok {
continue // Do not return plain transactions here
} else if stakingTx, ok := pending[i].(*staking.StakingTransaction); ok {
var tx interface{}
switch s.version {
case V1:
tx, err = v1.NewStakingTransaction(stakingTx, common.Hash{}, 0, 0, 0)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingStakingTransactions")
continue // Legacy behavior is to not return error here
}
case V2:
tx, err = v2.NewStakingTransaction(stakingTx, common.Hash{}, 0, 0, 0)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingStakingTransactions")
continue // Legacy behavior is to not return error here
}
default:
return nil, ErrUnknownRPCVersion
}
rpcTx, err := NewStructuredResponse(tx)
if err == nil {
transactions = append(transactions, rpcTx)
} else {
// Legacy behavior is to not return error here
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "PendingStakingTransactions")
}
} else {
return nil, types.ErrUnknownPoolTxType
}
}
return transactions, nil
}
// GetCurrentTransactionErrorSink ..
func (s *PublicPoolService) GetCurrentTransactionErrorSink(
ctx context.Context,
) ([]StructuredResponse, error) {
// For each transaction error in the error sink, format the response (same format for all versions)
formattedErrors := []StructuredResponse{}
for _, txError := range s.hmy.GetCurrentTransactionErrorSink() {
formattedErr, err := NewStructuredResponse(txError)
if err != nil {
return nil, err
}
formattedErrors = append(formattedErrors, formattedErr)
}
return formattedErrors, nil
}
// GetCurrentStakingErrorSink ..
func (s *PublicPoolService) GetCurrentStakingErrorSink(
ctx context.Context,
) ([]StructuredResponse, error) {
// For each staking tx error in the error sink, format the response (same format for all versions)
formattedErrors := []StructuredResponse{}
for _, txErr := range s.hmy.GetCurrentStakingErrorSink() {
formattedErr, err := NewStructuredResponse(txErr)
if err != nil {
return nil, err
}
formattedErrors = append(formattedErrors, formattedErr)
}
return formattedErrors, nil
}
// GetPendingCXReceipts ..
func (s *PublicPoolService) GetPendingCXReceipts(
ctx context.Context,
) ([]StructuredResponse, error) {
// For each cx receipt, format the response (same format for all versions)
formattedReceipts := []StructuredResponse{}
for _, receipts := range s.hmy.GetPendingCXReceipts() {
formattedReceipt, err := NewStructuredResponse(receipts)
if err != nil {
return nil, err
}
formattedReceipts = append(formattedReceipts, formattedReceipt)
}
return formattedReceipts, nil
}