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

96 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"
8 months ago
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 {
8 months ago
wiki *wiki.Woop
}
// NewMempoolAPI creates a new instance of MempoolAPI
8 months ago
func NewMempoolAPI(wiki *wiki.Woop) server.MempoolAPIServicer {
return &MempoolAPI{
8 months ago
wiki: wiki,
}
}
Rosetta Implementation Cleanup (Stage 3 of Node API Overhaul) (#3390) * [core] Add FindLogsWithTopic & unit test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Add GetDetailedBlockSignerInfo Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Add IsCommitteeSelectionBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Add test transaction creation helpers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Refactor account.go & add tests * Move TestNewAccountIdentifier & TestGetAddress to account_test.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move Operation & Tx formatting to own files * Move Respective unit tests to own files * Expose GetOperations & GetStakingOperations * Expose FormatTransaction, FormatCrossShardReceiverTransaction, FormatGenesisTransaction, FormatPreStakingRewardTransaction & FormatUndelegationPayoutTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move TransactionMetadata to transaction_construction.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update construction to use new helpers & formatters * Make docs consistent for mempool.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move all special tx & blk handling to own file Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Remove all moved fns, methods & tests from block.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Fix lint & imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename all tx related files for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename DefaultSenderAddress to FormatDefaultSenderAddress Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename Currency to NativeCurrency * This is in anticipation of HRC20 token support with rosetta * Rename various native operation functions accordingly * Add documentation to explain what a native token is Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix pre-staking block reward calculation * Move getPreStakingRewardTransactionIdentifiers to block_special.go * Add epoch to block metadata * Update unit tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Add IsLastBlockInEpoch method to Block & Header * Refactor all uses of length check `ShardState` * [hmy] Refactor IsCommitteeSelectionBlock to use chain.IsCommitteeSelectionBlock * Address PR comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update var names in preStakingRewardBlockTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
// Mempool implements the /mempool endpoint.
func (s *MempoolAPI) Mempool(
ctx context.Context, req *types.NetworkRequest,
) (*types.MempoolResponse, *types.Error) {
8 months ago
if err := assertValidNetworkIdentifier(req.NetworkIdentifier, s.wiki.ShardID); err != nil {
return nil, err
}
8 months ago
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
}
Rosetta Implementation Cleanup (Stage 3 of Node API Overhaul) (#3390) * [core] Add FindLogsWithTopic & unit test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Add GetDetailedBlockSignerInfo Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Add IsCommitteeSelectionBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Add test transaction creation helpers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Refactor account.go & add tests * Move TestNewAccountIdentifier & TestGetAddress to account_test.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move Operation & Tx formatting to own files * Move Respective unit tests to own files * Expose GetOperations & GetStakingOperations * Expose FormatTransaction, FormatCrossShardReceiverTransaction, FormatGenesisTransaction, FormatPreStakingRewardTransaction & FormatUndelegationPayoutTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move TransactionMetadata to transaction_construction.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update construction to use new helpers & formatters * Make docs consistent for mempool.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Move all special tx & blk handling to own file Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Remove all moved fns, methods & tests from block.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Fix lint & imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename all tx related files for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename DefaultSenderAddress to FormatDefaultSenderAddress Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename Currency to NativeCurrency * This is in anticipation of HRC20 token support with rosetta * Rename various native operation functions accordingly * Add documentation to explain what a native token is Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix pre-staking block reward calculation * Move getPreStakingRewardTransactionIdentifiers to block_special.go * Add epoch to block metadata * Update unit tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Add IsLastBlockInEpoch method to Block & Header * Refactor all uses of length check `ShardState` * [hmy] Refactor IsCommitteeSelectionBlock to use chain.IsCommitteeSelectionBlock * Address PR comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update var names in preStakingRewardBlockTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
// MempoolTransaction implements the /mempool/transaction endpoint.
func (s *MempoolAPI) MempoolTransaction(
ctx context.Context, req *types.MempoolTransactionRequest,
) (*types.MempoolTransactionResponse, *types.Error) {
8 months ago
if err := assertValidNetworkIdentifier(req.NetworkIdentifier, s.wiki.ShardID); err != nil {
return nil, err
}
hash := ethCommon.HexToHash(req.TransactionIdentifier.Hash)
8 months ago
poolTx := s.wiki.GetPoolTransaction(hash)
if poolTx == nil {
return nil, &common.TransactionNotFoundError
}
senderAddr, _ := poolTx.SenderAddress()
8 months ago
estLog := &wikiTypes.Log{
Address: senderAddr,
Topics: []ethCommon.Hash{staking.CollectRewardsTopic},
Data: big.NewInt(0).Bytes(),
8 months ago
BlockNumber: s.wiki.CurrentBlock().NumberU64(),
}
// Contract related information for pending transactions is not reported
8 months ago
estReceipt := &wikiTypes.Receipt{
PostState: []byte{},
8 months ago
Status: wikiTypes.ReceiptStatusSuccessful, // Assume transaction will succeed
CumulativeGasUsed: poolTx.GasLimit(),
Bloom: [256]byte{},
8 months ago
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
}