-
Notifications
You must be signed in to change notification settings - Fork 2
/
mempool.go
95 lines (83 loc) · 2.75 KB
/
mempool.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
package services
import (
"context"
"math/big"
hmyTypes "github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/hmy"
"github.com/PositionExchange/posichain/rosetta/common"
"github.com/PositionExchange/posichain/staking"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
ethCommon "github.com/ethereum/go-ethereum/common"
)
// MempoolAPI implements the server.MempoolAPIServicer interface
type MempoolAPI struct {
hmy *hmy.Harmony
}
// NewMempoolAPI creates a new instance of MempoolAPI
func NewMempoolAPI(hmy *hmy.Harmony) server.MempoolAPIServicer {
return &MempoolAPI{
hmy: hmy,
}
}
// Mempool implements the /mempool endpoint.
func (s *MempoolAPI) Mempool(
ctx context.Context, req *types.NetworkRequest,
) (*types.MempoolResponse, *types.Error) {
if err := assertValidNetworkIdentifier(req.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
pool, err := s.hmy.GetPoolTransactions()
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "unable to fetch pool transactions",
})
}
txIDs := make([]*types.TransactionIdentifier, pool.Len())
for i, tx := range pool {
txIDs[i] = &types.TransactionIdentifier{
Hash: tx.Hash().String(),
}
}
return &types.MempoolResponse{
TransactionIdentifiers: txIDs,
}, nil
}
// MempoolTransaction implements the /mempool/transaction endpoint.
func (s *MempoolAPI) MempoolTransaction(
ctx context.Context, req *types.MempoolTransactionRequest,
) (*types.MempoolTransactionResponse, *types.Error) {
if err := assertValidNetworkIdentifier(req.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
hash := ethCommon.HexToHash(req.TransactionIdentifier.Hash)
poolTx := s.hmy.GetPoolTransaction(hash)
if poolTx == nil {
return nil, &common.TransactionNotFoundError
}
senderAddr, _ := poolTx.SenderAddress()
estLog := &hmyTypes.Log{
Address: senderAddr,
Topics: []ethCommon.Hash{staking.CollectRewardsTopic},
Data: big.NewInt(0).Bytes(),
BlockNumber: s.hmy.CurrentBlock().NumberU64(),
}
// Contract related information for pending transactions is not reported
estReceipt := &hmyTypes.Receipt{
PostState: []byte{},
Status: hmyTypes.ReceiptStatusSuccessful, // Assume transaction will succeed
CumulativeGasUsed: poolTx.GasLimit(),
Bloom: [256]byte{},
Logs: []*hmyTypes.Log{estLog},
TxHash: poolTx.Hash(),
ContractAddress: ethCommon.Address{},
GasUsed: poolTx.GasLimit(),
}
respTx, err := FormatTransaction(poolTx, estReceipt, &ContractInfo{}, true)
if err != nil {
return nil, err
}
return &types.MempoolTransactionResponse{
Transaction: respTx,
}, nil
}