The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/rosetta/services/mempool.go

95 lines
2.7 KiB

package services
import (
"context"
"math/big"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
ethCommon "github.com/ethereum/go-ethereum/common"
wikiTypes "github.com/woop-chain/woop/core/types"
"github.com/woop-chain/woop/wiki"
"github.com/woop-chain/woop/rosetta/common"
"github.com/woop-chain/woop/staking"
)
// MempoolAPI implements the server.MempoolAPIServicer interface
type MempoolAPI struct {
wiki *wiki.Woop
}
// NewMempoolAPI creates a new instance of MempoolAPI
func NewMempoolAPI(wiki *wiki.Woop) server.MempoolAPIServicer {
return &MempoolAPI{
wiki: wiki,
}
}
// 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.wiki.ShardID); err != nil {
return nil, err
}
pool, err := s.wiki.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.wiki.ShardID); err != nil {
return nil, err
}
hash := ethCommon.HexToHash(req.TransactionIdentifier.Hash)
poolTx := s.wiki.GetPoolTransaction(hash)
if poolTx == nil {
return nil, &common.TransactionNotFoundError
}
senderAddr, _ := poolTx.SenderAddress()
estLog := &wikiTypes.Log{
Address: senderAddr,
Topics: []ethCommon.Hash{staking.CollectRewardsTopic},
Data: big.NewInt(0).Bytes(),
BlockNumber: s.wiki.CurrentBlock().NumberU64(),
}
// Contract related information for pending transactions is not reported
estReceipt := &wikiTypes.Receipt{
PostState: []byte{},
Status: wikiTypes.ReceiptStatusSuccessful, // Assume transaction will succeed
CumulativeGasUsed: poolTx.GasLimit(),
Bloom: [256]byte{},
Logs: []*wikiTypes.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
}