Commit 42785f15 authored by M an's avatar M an

Optimize scan block task

parent ac99774f
package main
import (
"bytes"
"context"
"fmt"
"log"
"math/big"
"os"
"os/signal"
"syscall"
"time"
common "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
types "github.com/ethereum/go-ethereum/core/types"
ethclient "github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://galaxy.block.caduceus.foundation")
addressArr := []string{"0x5b68ff12be7fd90284ebe2702050879d4d50061a"}
start("https://galaxy.block.caduceus.foundation", addressArr)
}
func start(url string, addArr []string) {
// get operate signal
makeSignal := make(chan os.Signal, 1)
// monitor all signals
signal.Notify(makeSignal, syscall.SIGINT)
//Block until a signal comes in
fmt.Println("monitor start")
client, err := ethclient.Dial(url)
if err != nil {
log.Fatal(err)
}
pendingVerifyAddress := "0x61704EFB8b8120c03C210cAC5f5193BF8c80852a"
// Get the last block height
num := getLastTimeBlockNumber()
var currentScanedBlock int64 = num
// create ticker
var ticker *time.Ticker = time.NewTicker(1 * time.Second)
for {
select {
case <-ticker.C:
{
header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatalf(err.Error())
}
newBlockNum := header.Number.Int64()
for i := num; i < newBlockNum; i++ {
fmt.Println("current scan block number:", i)
if i == 6917803 {
break
}
fmt.Println("newBlockNum:", newBlockNum)
if currentScanedBlock < newBlockNum {
fmt.Println("current scan block number:", currentScanedBlock)
for _, pendingVerifyAddress := range addArr {
// Get tx with in a block
if getBlockTxThroughAddress(client, big.NewInt(i), pendingVerifyAddress) {
if getBlockTxThroughAddress(client, big.NewInt(currentScanedBlock), pendingVerifyAddress) {
fmt.Println("verify success:", pendingVerifyAddress)
break
} else {
fmt.Println("verifing......", pendingVerifyAddress)
}
}
currentScanedBlock++
}
}
case processSignal := <-makeSignal:
{
if processSignal != nil {
stop(currentScanedBlock, *ticker)
fmt.Println("process end:", processSignal)
os.Exit(3)
}
}
}
}
// Get the tx in the block through the block hash
getTxThroughBlockTx(client, header.Hash().String())
}
func getBlockTxThroughAddress(client *ethclient.Client, blockNumber *big.Int, verifyAddress string) (verifyStatus bool) {
func getBlockTxThroughAddress(client *ethclient.Client, blockNumber *big.Int, verifyAddress string) bool {
// Specify the block number and get the transactions in the block
block, err := client.BlockByNumber(context.Background(), big.NewInt(blockNumber.Int64()))
if err != nil {
return false
}
// Get all tx in block
for _, tx := range block.Transactions() {
fmt.Println("Hash:", tx.Hash().Hex())
fmt.Println("Value:", tx.Value().String())
fmt.Println("Gas:", tx.Gas())
fmt.Println("GasPrice:", tx.GasPrice().Uint64())
fmt.Println("Nonce:", tx.Nonce())
// fmt.Println("Data:", tx.Data())
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
......@@ -61,45 +86,32 @@ func getBlockTxThroughAddress(client *ethclient.Client, blockNumber *big.Int, ve
if err == nil {
fmt.Println("From:", msg.From().Hex())
}
toAddress := tx.To().Hex()
if verifyAddress == toAddress {
fmt.Println("To:", toAddress)
return true
}
fmt.Println("NotTo:", toAddress)
// Get receipt status
receipt, err := client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
log.Fatal(err)
}
fmt.Println("receiptStatus:", receipt.Status)
// Get tx status
_, isPending, err := client.TransactionByHash(context.Background(), tx.Hash())
if err != nil {
log.Fatal(err)
fmt.Println("toAddress:", tx.To().Hex(), "Value:", "Hash:", tx.Hash().Hex(), tx.Value().String())
verifyAddressByte := common.HexToAddress(verifyAddress).Bytes()
compareRes := bytes.Compare(tx.To().Bytes(), verifyAddressByte)
if compareRes == 0 && receipt.Status == 1 {
// storage tx
storageTx(tx.Hash().Hex(), tx.Value().String(), msg.From().Hex(), tx.To().Hex())
return true
}
fmt.Println("txStatus:", isPending) // false
fmt.Println("---------------------------")
}
return false
}
func getTxThroughBlockTx(client *ethclient.Client, tx string) {
// Specify the block Hash to get the transactions in the block
blockHash := common.HexToHash(tx)
count, err := client.TransactionCount(context.Background(), blockHash)
if err != nil {
log.Fatal(err)
}
for idx := uint(0); idx < count; idx++ {
tx, err := client.TransactionInBlock(context.Background(), blockHash, idx)
if err != nil {
log.Fatal(err)
}
fmt.Println(tx.Hash().Hex())
}
func storageTx(tx string, amount string, from string, to string) {
fmt.Println("storage hash:", tx, "amount:", amount, "from:", from, "to:", to)
}
func getLastTimeBlockNumber() (number int64) {
number = 6916803
number = 7048864
return
}
func stop(blockNum int64, ticker time.Ticker) {
ticker.Stop()
// In Mysql storage end blockNum
fmt.Println("Ticker stopped and storage blockNum:", blockNum)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment