Skip to content

Commit

Permalink
refactor: store both found mempool txs and hashes of txs not found in…
Browse files Browse the repository at this point in the history
…-memory
  • Loading branch information
vanderheijden86 committed Sep 7, 2022
1 parent 699b31c commit d61b2ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
32 changes: 8 additions & 24 deletions mempoolexplorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@ package mempoolexplorer

import (
"context"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"log"
)

var missingTxs = make([]common.Hash, 0, 20)
var txs = make(map[common.Hash]*types.Transaction)

func main() {
pendingTxs := streamMemPoolTxs(createGethClient(), 5)
for {
fmt.Println("Channel length: ", len(pendingTxs))
fmt.Println("Channel capacity: ", cap(pendingTxs))
fmt.Println(<-pendingTxs)
}
}

// streamMemPoolTxs listens to all pending TXs that underlying ethereum node receives from incoming RPC requests and other nodes
func streamMemPoolTxs(geth *gethclient.Client, bufferLength int) chan common.Hash {
// streamMemPoolTxHashes listens to all pending TXs that underlying ethereum node receives from incoming RPC requests and other nodes
func streamMemPoolTxHashes(geth *gethclient.Client, bufferLength int) chan common.Hash {
pendingTxs := make(chan common.Hash, bufferLength)
_, err := geth.SubscribePendingTransactions(context.Background(), pendingTxs)
if err != nil {
Expand All @@ -30,27 +22,19 @@ func streamMemPoolTxs(geth *gethclient.Client, bufferLength int) chan common.Has
return pendingTxs
}

func printTxDetails(txHash common.Hash) {
func storeTxDetails(txHash common.Hash) {
client := createEthClient()
tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
tx, _, err := client.TransactionByHash(context.Background(), txHash)

// If TX is not found, just print the error and return so the rest of the program can continue. If there's an other error then log and exit.
// If TX is not found, print the error, store the hash and return so the rest of the program can continue.
if err == ethereum.NotFound {
log.Println(err)
storeMissingTxHashes(txHash)
return
} else if err != nil {
log.Fatal(err)
}

fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
fmt.Println(tx.Value().String()) // 10000000000000000
fmt.Println(tx.Gas()) // 105000
fmt.Println(tx.GasPrice().Uint64()) // 102000000000
fmt.Println(tx.Nonce()) // 110644
fmt.Println(tx.Data()) // []
fmt.Println(tx.To().Hex()) // 0x55fE59D8Ad77035154dDd0AD0388D09Dd4047A8e
fmt.Println("isPending: ", isPending) // true
txs[txHash] = tx
}

func storeMissingTxHashes(txHash common.Hash) {
Expand Down
29 changes: 20 additions & 9 deletions mempoolexplorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
)

func TestMain_streamMemPoolTxs(t *testing.T) {
pendingTxs := streamMemPoolTxs(createGethClient(), 100)
func TestMempoolExplorer_streamMemPoolTxs(t *testing.T) {
pendingTxs := streamMemPoolTxHashes(createGethClient(), 100)
for {
fmt.Println("Channel length: ", len(pendingTxs))
fmt.Println("Channel capacity: ", cap(pendingTxs))
Expand All @@ -16,22 +16,33 @@ func TestMain_streamMemPoolTxs(t *testing.T) {
}
}

func TestMain_getTxDetails(t *testing.T) {
func TestMempoolExplorer_storeTxDetails(t *testing.T) {
// 0xaf745220755919ee3386ca28cc207e87388841832ee4bd67d7260b06b914af85
printTxDetails(common.HexToHash("0xaf745220755919ee3386ca28cc207e87388841832ee4bd67d7260b06b914af85"))
storeTxDetails(common.HexToHash("0xaf745220755919ee3386ca28cc207e87388841832ee4bd67d7260b06b914af85"))
}

func TestMain_getTxDetails_Live(t *testing.T) {
pendingTxs := streamMemPoolTxs(createGethClient(), 10)
func TestMempoolExplorer_storeTxDetails_Live(t *testing.T) {
pendingTxs := streamMemPoolTxHashes(createGethClient(), 10)
for i := 1; i < 25; i++ {
currentTxHash := <-pendingTxs
fmt.Println(currentTxHash)
printTxDetails(currentTxHash)
storeTxDetails(currentTxHash)
}

fmt.Println("Pending TXs not found on geth node:")
fmt.Println(len(txs), " stored mempool TXs found on geth node:")
for _, tx := range txs {
fmt.Println("------------------------------------------------------")
fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
fmt.Println(tx.Value().String()) // 10000000000000000
fmt.Println(tx.Gas()) // 105000
fmt.Println(tx.GasPrice().Uint64()) // 102000000000
fmt.Println(tx.Nonce()) // 110644
fmt.Println(tx.Data()) // []
fmt.Println(tx.To().Hex()) // 0x55fE59D8Ad77035154dDd0AD0388D09Dd4047A8e
}

fmt.Println(len(missingTxs), " pending TXs not found on geth node:")
for _, txHash := range missingTxs {
fmt.Println(txHash.Hex())
}

}

0 comments on commit d61b2ef

Please sign in to comment.