github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/scanner/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	_ "github.com/go-sql-driver/mysql"
     7  	"fmt"
     8  	"github.com/ethereum/go-ethereum/common/hexutil"
     9  	"github.com/ethereum/go-ethereum/console"
    10  	"github.com/ethereum/go-ethereum/ethclient"
    11  	"github.com/ethereum/go-ethereum/internal/debug"
    12  	"github.com/ethereum/go-ethereum/log"
    13  	"github.com/ethereum/go-ethereum/rpc"
    14  	"github.com/ethereum/go-ethereum/cmd/utils"
    15  	"gopkg.in/urfave/cli.v1"
    16  	"math/big"
    17  	"os"
    18  	"time"
    19  )
    20  
    21  // Commonly used command line flags.
    22  var (
    23  	gitCommit = ""
    24  	gitDate   = ""
    25  	app       = utils.NewApp(gitCommit, "the Scanner for Reapchain")
    26  
    27  //mysqlConnect = cli.StringFlag{
    28  //	Name:  "passwordfile",
    29  //	Usage: "the file that contains the password for the keyfile",
    30  //}
    31  //jsonFlag = cli.BoolFlag{
    32  //	Name:  "json",
    33  //	Usage: "output JSON instead of human-readable format",
    34  //}
    35  )
    36  
    37  func init() {
    38  	app.Action = scanner
    39  	app.Commands = []cli.Command{}
    40  
    41  	// log 기능 추가
    42  	app.Flags = append(app.Flags, debug.Flags...)
    43  
    44  	app.Before = func(ctx *cli.Context) error {
    45  		return debug.Setup(ctx)
    46  	}
    47  
    48  	app.After = func(ctx *cli.Context) error {
    49  		debug.Exit()
    50  		console.Stdin.Close() // Resets terminal mode.
    51  		return nil
    52  	}
    53  
    54  	log.Info("Go Scanner.")
    55  }
    56  
    57  func main() {
    58  	if err := app.Run(os.Args); err != nil {
    59  		fmt.Fprintln(os.Stderr, err)
    60  		os.Exit(1)
    61  	}
    62  }
    63  
    64  func scanner(ctx *cli.Context) error {
    65  	var err error
    66  
    67  	rpcDalier, err := rpc.Dial("http://192.168.11.1:8541")
    68  	if err != nil {
    69  		log.Error("rpc", "rpc.Dial error", err)
    70  		os.Exit(-1)
    71  	}
    72  
    73  	client := ethclient.NewClient(rpcDalier)
    74  	block, err := client.BlockByNumber(context.Background(),big.NewInt(1))
    75  	if err != nil {
    76  		log.Error("rcp", "eth_getBlockByNumber", err)
    77  		os.Exit(-1)
    78  	}
    79  
    80  	log.Debug("Block", "Block Content", block.Header().ParentHash)
    81  
    82  	return nil
    83  }
    84  
    85  func callbackfunc(result interface{}) bool {
    86  	value, ok := result.(string)
    87  	if !ok {
    88  		log.Error("interface casting error", "ret=", ok)
    89  		os.Exit(-1)
    90  	}
    91  	v, _ := hexutil.DecodeBig(value)
    92  	log.Debug("Result", "value", v)
    93  	return true
    94  }
    95  
    96  type Block struct {
    97  	blocknumber     uint64    //| bigint unsigned | NO   | PRI | NULL    |       |
    98  	timestamp       time.Time //| timestamp       | NO   |     | NULL    |       |
    99  	miner           string    //| char(42)        | NO   |     | NULL    |       |
   100  	blockreward     float64   //| double(40,20)   | YES  |     | NULL    |       |
   101  	unclesreward    float64   //| double(40,20)   | YES  |     | NULL    |       |
   102  	difficulty      uint64    //| bigint unsigned | YES  |     | NULL    |       |
   103  	totaldifficulty uint64    //| bigint unsigned | YES  |     | NULL    |       |
   104  	size            uint64    //| bigint unsigned | NO   |     | NULL    |       |
   105  	gasused         uint64    //| bigint unsigned | NO   |     | NULL    |       |
   106  	gaslimit        uint64    //| bigint unsigned | NO   |     | NULL    |       |
   107  	extradata       string    //| mediumtext      | YES  |     | NULL    |       |
   108  	hash            string    //| char(66)        | NO   |     | NULL    |       |
   109  	parenthash      string    //| char(66)        | NO   |     | NULL    |       |
   110  	sha3uncles      string    //| char(66)        | YES  |     | NULL    |       |
   111  	stateroot       string    //| char(66)        | YES  |     | NULL    |       |
   112  	nonce           string    //| char(18)        | YES  |     | NULL    |       |
   113  }
   114  
   115  // Block struct의 타입의 채널에 데이터가 수신되면,
   116  // Block table에 데이터를 저장한다.
   117  func insertBlockTable(data <-chan Block) {
   118  	db, err := sql.Open("mysql", "reapscanner:45b8d01caf660bf63e9b69fb13ab6e40@tcp(192.168.0.97:3306)/reapdb")
   119  	if err != nil {
   120  		log.Error("Connect Mysql", "sql.Open error", err)
   121  	}
   122  	defer db.Close()
   123  
   124  	for {
   125  		blockData := <-data
   126  		result, err := db.Exec("insert into block(blocknumber, timestamp, miner, size, gasused, gaslimit, hash, parenthash)",
   127  			blockData.blocknumber,
   128  			blockData.timestamp,
   129  			blockData.miner,
   130  			blockData.size,
   131  			blockData.gasused,
   132  			blockData.gaslimit,
   133  			blockData.hash,
   134  			blockData.parenthash)
   135  
   136  		if err != nil {
   137  			log.Error("insert query", "error", err)
   138  		}
   139  		rows, err := result.RowsAffected()
   140  		if err != nil {
   141  			log.Error("check result after insert", "error", err)
   142  		}
   143  		if rows != 1 {
   144  			log.Error("the number of rows is too many", "error", "expected to affect 1 row, affected", "row", rows)
   145  		}
   146  	}
   147  }