Ver código fonte

可以正常访问内部API了

skyfffire 2 anos atrás
pai
commit
94b6dd8714
3 arquivos alterados com 27 adições e 10 exclusões
  1. 24 4
      arbitrage/history.go
  2. 2 2
      arbitrage/log.go
  3. 1 4
      cmd/geth/main.go

+ 24 - 4
arbitrage/history.go

@@ -1,23 +1,43 @@
 package arbitrage
 
 import (
+	"github.com/ethereum/go-ethereum/internal/ethapi"
 	"github.com/ethereum/go-ethereum/node"
 	"github.com/urfave/cli/v2"
+	"time"
 )
 
 type HistoryArbitrage struct {
-	Stack   *node.Node
-	Context *cli.Context
+	stack         *node.Node
+	ctx           *cli.Context
+	apiBackend    ethapi.Backend
+	blockChainApi *ethapi.BlockChainAPI
+}
+
+func NewHistoryArbitrage(stack *node.Node, ctx *cli.Context, apiBackend ethapi.Backend) *HistoryArbitrage {
+	return &HistoryArbitrage{
+		stack:         stack,
+		ctx:           ctx,
+		apiBackend:    apiBackend,
+		blockChainApi: ethapi.NewBlockChainAPI(apiBackend),
+	}
 }
 
 func (h HistoryArbitrage) Register() {
-	h.Stack.RegisterLifecycle(h)
+	// 注册成节点生命周期
+	h.stack.RegisterLifecycle(h)
 }
 
 func (h HistoryArbitrage) Start() error {
 	HistoryLog("Hi, I am history arbitrage bot.")
+	HistoryLog("api: ", h.blockChainApi)
+	//time.Sleep(time.Second * 20)
 
-	return nil
+	for {
+		HistoryLog("BlockNumber: ", h.blockChainApi.BlockNumber(), "ChainID: ", h.blockChainApi.ChainId())
+
+		time.Sleep(time.Second)
+	}
 }
 
 func (h HistoryArbitrage) Stop() error {

+ 2 - 2
arbitrage/log.go

@@ -2,6 +2,6 @@ package arbitrage
 
 import "github.com/ethereum/go-ethereum/log"
 
-func HistoryLog(msg string) {
-	log.Info("~~~~~~~~~~~~~~HistoryLog~~~~~~~~~~~~~~   " + msg)
+func HistoryLog(msg string, ctx ...interface{}) {
+	log.Info("~~~~~~~~~~~~~~HistoryLog~~~~~~~~~~~~~~   ", msg, ctx)
 }

+ 1 - 4
cmd/geth/main.go

@@ -352,10 +352,7 @@ func geth(ctx *cli.Context) error {
 	defer stack.Close()
 
 	// 历史记录套利系统注入到系统生命周期
-	arbitrage.HistoryArbitrage{
-		Stack:   stack,
-		Context: ctx,
-	}.Register()
+	arbitrage.NewHistoryArbitrage(stack, ctx, backend).Register()
 
 	startNode(ctx, stack, backend, false)
 	stack.Wait()