Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
GetBlockTransactionCountByNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
araskachoi committed Oct 20, 2020
1 parent ac2d18e commit 7b49761
Showing 1 changed file with 54 additions and 52 deletions.
106 changes: 54 additions & 52 deletions rpc/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"math/big"
"os"
"strconv"
"strings"
"sync"

"github.com/spf13/viper"
Expand Down Expand Up @@ -339,60 +341,60 @@ func (e *PublicEthAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil

// GetBlockTransactionCountByNumber returns the number of transactions in the block identified by number.
func (e *PublicEthAPI) GetBlockTransactionCountByNumber(blockNum BlockNumber) *hexutil.Uint {
fmt.Println("blockNum", blockNum)
e.logger.Debug("eth_getBlockTransactionCountByNumber", "block number", blockNum)

var ctx context.CLIContext
if blockNum.Int64() == int64(0) || blockNum.Int64() == int64(1) {
// uses latest block number for pending: -1 -> 0
ctx = e.cliCtx.WithHeight(int64(0))
fmt.Println("HEIGHT AT CTX!: ", ctx.Height)
}

height := blockNum.Int64()
// pendingQuery := false
// if height == int64(-1) {
// // uses latest block number for pending: -1 -> 0
// height = int64(0)
// pendingQuery = true
// }

// fmt.Println("height: ", height)

// txCount := e.getBlockTransactionCountByNumber(height)
// if txCount == nil {
// fmt.Println("invalid block!")
// return nil
// }

// totalTxCountCleaned := strings.Replace(txCount.String(), "0x", "", -1)
// fmt.Println("totalTxCountCleaned: ", totalTxCountCleaned)
// totalTxCount, err := strconv.ParseUint(totalTxCountCleaned, 16, 64)
// if err != nil {
// return nil
// }
// fmt.Println("totalTxCount: ", totalTxCount)

// if pendingQuery {
// pendingtx, err := e.backend.PendingTransactions()
// if err != nil {
// return nil
// }

// for i := range pendingtx {
// if pendingtx[i] == nil {
// continue
// }
// totalTxCount++
// }
// }
// fmt.Println("end totalTxCount: ", totalTxCount)

// total := hexutil.Uint(totalTxCount)

// return &total

return e.getBlockTransactionCountByNumber(height)
var blockNumber hexutil.Uint64
pendingQuery := false
switch bn := blockNum.Int64(); bn {
case int64(0):
bn, err := e.backend.BlockNumber()
if err != nil {
return nil
}
blockNumber = bn
case int64(-1):
bn, err := e.backend.BlockNumber()
if err != nil {
return nil
}
blockNumber = bn
pendingQuery = true
}

heightCleaned := strings.Replace(blockNumber.String(), "0x", "", -1)
height, err := strconv.ParseInt(heightCleaned, 16, 64)
if err != nil {
return nil
}

txCount := e.getBlockTransactionCountByNumber(height)
if txCount == nil {
fmt.Println("invalid block!")
return nil
}

totalTxCountCleaned := strings.Replace(txCount.String(), "0x", "", -1)
totalTxCount, err := strconv.ParseUint(totalTxCountCleaned, 16, 64)
if err != nil {
return nil
}

if pendingQuery {
pendingtx, err := e.backend.PendingTransactions()
if err != nil {
return nil
}

for i := range pendingtx {
if pendingtx[i] == nil {
continue
}
totalTxCount++
}
}

total := hexutil.Uint(totalTxCount)
return &total
}

func (e *PublicEthAPI) getBlockTransactionCountByNumber(number int64) *hexutil.Uint {
Expand Down

0 comments on commit 7b49761

Please sign in to comment.