Explorar o código

增加了最快提交交易的方法

410 %!s(int64=2) %!d(string=hai) anos
pai
achega
812e27b8db

+ 30 - 0
core/types/block.go

@@ -19,6 +19,7 @@ package types
 
 import (
 	"encoding/binary"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"io"
@@ -88,6 +89,35 @@ type Header struct {
 	Nonce       BlockNonce     `json:"nonce"`
 }
 
+func (h *Header) GetHeaderJSON() (string, error) {
+	// 创建一个包含字段名和字段值的映射
+	headerMap := map[string]interface{}{
+		"ParentHash":  h.ParentHash,
+		"UncleHash":   h.UncleHash,
+		"Coinbase":    h.Coinbase,
+		"Root":        h.Root,
+		"TxHash":      h.TxHash,
+		"ReceiptHash": h.ReceiptHash,
+		"Bloom":       h.Bloom,
+		"Difficulty":  h.Difficulty,
+		"Number":      h.Number,
+		"GasLimit":    h.GasLimit,
+		"GasUsed":     h.GasUsed,
+		"Time":        h.Time,
+		"Extra":       h.Extra,
+		"MixDigest":   h.MixDigest,
+		"Nonce":       h.Nonce,
+	}
+
+	// 将映射转换为 JSON 字符串
+	jsonData, err := json.Marshal(headerMap)
+	if err != nil {
+		return "", err
+	}
+
+	return string(jsonData), nil
+}
+
 // field type overrides for gencodec
 type headerMarshaling struct {
 	Difficulty *hexutil.Big

+ 5 - 0
eth/api_backend.go

@@ -56,6 +56,11 @@ func (b *EthAPIBackend) CurrentBlock() *types.Block {
 	return b.eth.blockchain.CurrentBlock()
 }
 
+func (b *EthAPIBackend) SendTransactionsFast(txs types.Transactions) {
+	b.eth.handler.SendTransactionsFast(txs)
+
+}
+
 func (b *EthAPIBackend) SetHead(number uint64) {
 	b.eth.handler.downloader.Cancel()
 	b.eth.blockchain.SetHead(number)

+ 4 - 3
eth/backend.go

@@ -20,7 +20,6 @@ package eth
 import (
 	"errors"
 	"fmt"
-	"github.com/ethereum/go-ethereum/eth/protocols/diff"
 	"math/big"
 	"runtime"
 	"sync"
@@ -545,13 +544,15 @@ func (s *Ethereum) BloomIndexer() *core.ChainIndexer   { return s.bloomIndexer }
 
 // Protocols returns all the currently configured
 // network protocols to start.
+// 控制开启eth snap diff同步功能,全部关掉也可以用p2p
 func (s *Ethereum) Protocols() []p2p.Protocol {
+
 	protos := eth.MakeProtocols((*ethHandler)(s.handler), s.networkID, s.ethDialCandidates)
 	//if !s.config.DisableSnapProtocol && s.config.SnapshotCache > 0 {
 	//	protos = append(protos, snap.MakeProtocols((*snapHandler)(s.handler), s.snapDialCandidates)...)
 	//}
-	//// diff protocol can still open without snap protocol
-	protos = append(protos, diff.MakeProtocols((*diffHandler)(s.handler), s.snapDialCandidates)...)
+	// diff protocol can still open without snap protocol
+	//protos = append(protos, diff.MakeProtocols((*diffHandler)(s.handler), s.snapDialCandidates)...)
 	return protos
 }
 

+ 2 - 0
internal/ethapi/backend.go

@@ -51,6 +51,7 @@ type Backend interface {
 
 	// Blockchain API
 	SetHead(number uint64)
+
 	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
 	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
 	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
@@ -71,6 +72,7 @@ type Backend interface {
 	// Transaction pool API
 	SendTx(ctx context.Context, signedTx *types.Transaction) error
 	SendTxFast(ctx context.Context, signedTx *types.Transaction) error
+	SendTransactionsFast(txs types.Transactions)
 	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
 	GetPoolTransactions() (types.Transactions, error)
 	GetPoolTransaction(txHash common.Hash) *types.Transaction

+ 8 - 1
internal/ethapi/public_zdy_api.go

@@ -2,6 +2,7 @@ package ethapi
 
 import (
 	"context"
+	"fmt"
 	"github.com/ethereum/go-ethereum/accounts"
 	"github.com/ethereum/go-ethereum/common"
 	"github.com/ethereum/go-ethereum/common/hexutil"
@@ -64,11 +65,17 @@ func (s *PublicZdyAPI) Call(args CallArgs) (CallArgs, error) {
 }
 
 func (s *PublicZdyAPI) SendRawTransaction(ctx context.Context, input hexutil.Bytes) (common.Hash, error) {
+	fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "zdy.SendRawTransaction")
 	tx := new(types.Transaction)
 	if err := tx.UnmarshalBinary(input); err != nil {
 		return common.Hash{}, err
 	}
-	s.b.SendTxFast(ctx, tx)
+	//s.b.SendTxFast(ctx, tx)
+
+	s.b.SendTransactionsFast(types.Transactions{tx})
+
+	fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "zdy.SendRawTransaction OK", tx.Hash())
+
 	return tx.Hash(), nil
 }
 

+ 5 - 0
les/api_backend.go

@@ -46,6 +46,11 @@ type LesApiBackend struct {
 	gpo                 *gasprice.Oracle
 }
 
+func (b *LesApiBackend) SendTransactionsFast(txs types.Transactions) {
+	//TODO implement me
+	panic("implement me")
+}
+
 func (b *LesApiBackend) SendTxFast(ctx context.Context, signedTx *types.Transaction) error {
 	//TODO implement me
 	panic("implement me")