|
|
@@ -19,6 +19,8 @@ package eth
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
@@ -27,7 +29,64 @@ import (
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
)
|
|
|
|
|
|
+// HashList 是一个带有互斥锁的哈希列表结构
|
|
|
+type HashList struct {
|
|
|
+ mutex sync.Mutex // 互斥锁,用于保护并发访问哈希列表
|
|
|
+ list map[common.Hash]time.Time // 哈希列表,存储哈希和时间戳的键值对
|
|
|
+}
|
|
|
+
|
|
|
+// NewHashList 创建一个新的 HashList 实例
|
|
|
+func NewHashList() *HashList {
|
|
|
+ return &HashList{
|
|
|
+ list: make(map[common.Hash]time.Time),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Add 向哈希列表添加哈希及其时间戳
|
|
|
+func (hl *HashList) Add(hash common.Hash) {
|
|
|
+ hl.mutex.Lock() // 加锁
|
|
|
+ defer hl.mutex.Unlock() // 解锁(在函数返回时)
|
|
|
+ hl.list[hash] = time.Now() // 添加哈希和当前时间戳
|
|
|
+}
|
|
|
+
|
|
|
+// Contains 检查哈希列表是否包含指定的哈希
|
|
|
+func (hl *HashList) Has(hash common.Hash) bool {
|
|
|
+ hl.mutex.Lock() // 加锁
|
|
|
+ defer hl.mutex.Unlock() // 解锁(在函数返回时)
|
|
|
+ _, found := hl.list[hash] // 检查哈希是否存在
|
|
|
+ return found
|
|
|
+}
|
|
|
+
|
|
|
+// ClearIfNeeded 根据最大大小清空哈希列表
|
|
|
+func (hl *HashList) ClearIfNeeded(maxSize int) {
|
|
|
+ hl.mutex.Lock() // 加锁
|
|
|
+ defer hl.mutex.Unlock() // 解锁(在函数返回时)
|
|
|
+ if len(hl.list) > maxSize {
|
|
|
+ hl.list = make(map[common.Hash]time.Time) // 清空哈希列表
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// GetTimestampDifference 获取 hashList2 中的时间戳减去 hashList1 中的时间戳
|
|
|
+func GetTimestampDifference(hashList1, hashList2 *HashList, hash common.Hash) (time.Duration, error) {
|
|
|
+ hashList1.mutex.Lock()
|
|
|
+ hashList2.mutex.Lock()
|
|
|
+ defer hashList1.mutex.Unlock()
|
|
|
+ defer hashList2.mutex.Unlock()
|
|
|
+
|
|
|
+ timestamp1, found1 := hashList1.list[hash]
|
|
|
+ timestamp2, found2 := hashList2.list[hash]
|
|
|
+ if !found1 || !found2 {
|
|
|
+ return 0, fmt.Errorf("hash %s not found in both lists", hash.Hex())
|
|
|
+ }
|
|
|
+
|
|
|
+ return timestamp2.Sub(timestamp1), nil
|
|
|
+}
|
|
|
+
|
|
|
+var hashList = NewHashList()
|
|
|
+var txHashList = NewHashList()
|
|
|
+
|
|
|
// handleGetBlockHeaders handles Block header query, collect the requested headers and reply
|
|
|
+// 0x4 eth65
|
|
|
func handleGetBlockHeaders(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Decode the complex header query
|
|
|
var query GetBlockHeadersPacket
|
|
|
@@ -39,6 +98,7 @@ func handleGetBlockHeaders(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
}
|
|
|
|
|
|
// handleGetBlockHeaders66 is the eth/66 version of handleGetBlockHeaders
|
|
|
+// 0x4 eth66
|
|
|
func handleGetBlockHeaders66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Decode the complex header query
|
|
|
var query GetBlockHeadersPacket66
|
|
|
@@ -49,6 +109,7 @@ func handleGetBlockHeaders66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return peer.ReplyBlockHeaders(query.RequestId, response)
|
|
|
}
|
|
|
|
|
|
+// 0x4
|
|
|
func answerGetBlockHeadersQuery(backend Backend, query *GetBlockHeadersPacket, peer *Peer) []*types.Header {
|
|
|
hashMode := query.Origin.Hash != (common.Hash{})
|
|
|
first := true
|
|
|
@@ -135,6 +196,7 @@ func answerGetBlockHeadersQuery(backend Backend, query *GetBlockHeadersPacket, p
|
|
|
return headers
|
|
|
}
|
|
|
|
|
|
+// 0x5 > 0x6 eth65
|
|
|
func handleGetBlockBodies(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Decode the block body retrieval message
|
|
|
var query GetBlockBodiesPacket
|
|
|
@@ -145,6 +207,7 @@ func handleGetBlockBodies(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return peer.SendBlockBodiesRLP(response)
|
|
|
}
|
|
|
|
|
|
+// rec 0x5 > send 0x6 eth66
|
|
|
func handleGetBlockBodies66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Decode the block body retrieval message
|
|
|
var query GetBlockBodiesPacket66
|
|
|
@@ -155,6 +218,7 @@ func handleGetBlockBodies66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return peer.ReplyBlockBodiesRLP(query.RequestId, response)
|
|
|
}
|
|
|
|
|
|
+// 0x6
|
|
|
func answerGetBlockBodiesQuery(backend Backend, query GetBlockBodiesPacket, peer *Peer) []rlp.RawValue {
|
|
|
// Gather blocks until the fetch or network limits is reached
|
|
|
var (
|
|
|
@@ -272,6 +336,7 @@ func answerGetReceiptsQuery(backend Backend, query GetReceiptsPacket, peer *Peer
|
|
|
return receipts
|
|
|
}
|
|
|
|
|
|
+// rec 0x1
|
|
|
func handleNewBlockhashes(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// A batch of new block announcements just arrived
|
|
|
ann := new(NewBlockHashesPacket)
|
|
|
@@ -280,12 +345,14 @@ func handleNewBlockhashes(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
}
|
|
|
// Mark the hashes as present at the remote node
|
|
|
for _, block := range *ann {
|
|
|
+ //fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x1", block.Number, peer.Node().IP())
|
|
|
peer.markBlock(block.Hash)
|
|
|
}
|
|
|
// Deliver them all to the backend for queuing
|
|
|
return backend.Handle(peer, ann)
|
|
|
}
|
|
|
|
|
|
+// rec 0x7
|
|
|
func handleNewBlock(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Retrieve and decode the propagated block
|
|
|
ann := new(NewBlockPacket)
|
|
|
@@ -308,7 +375,7 @@ func handleNewBlock(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
|
|
|
// Mark the peer as owning the block
|
|
|
peer.markBlock(ann.Block.Hash())
|
|
|
-
|
|
|
+ //fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x7", ann.Block.Number(), peer.Node().IP())
|
|
|
return backend.Handle(peer, ann)
|
|
|
}
|
|
|
|
|
|
@@ -321,6 +388,7 @@ func handleBlockHeaders(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return backend.Handle(peer, res)
|
|
|
}
|
|
|
|
|
|
+// rec 0x4
|
|
|
func handleBlockHeaders66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// A batch of headers arrived to one of our previous requests
|
|
|
res := new(BlockHeadersPacket66)
|
|
|
@@ -329,6 +397,10 @@ func handleBlockHeaders66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
}
|
|
|
requestTracker.Fulfil(peer.id, peer.version, BlockHeadersMsg, res.RequestId)
|
|
|
|
|
|
+ //if len(res.BlockHeadersPacket) > 0 {
|
|
|
+ // fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x4", res.BlockHeadersPacket[0].Number, peer.Node().IP())
|
|
|
+ //}
|
|
|
+
|
|
|
return backend.Handle(peer, &res.BlockHeadersPacket)
|
|
|
}
|
|
|
|
|
|
@@ -341,6 +413,7 @@ func handleBlockBodies(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return backend.Handle(peer, res)
|
|
|
}
|
|
|
|
|
|
+// 0x6
|
|
|
func handleBlockBodies66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// A batch of block bodies arrived to one of our previous requests
|
|
|
res := new(BlockBodiesPacket66)
|
|
|
@@ -349,6 +422,14 @@ func handleBlockBodies66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
}
|
|
|
requestTracker.Fulfil(peer.id, peer.version, BlockBodiesMsg, res.RequestId)
|
|
|
|
|
|
+ //if len(res.BlockBodiesPacket) > 0 && len(res.BlockBodiesPacket[0].Uncles) > 0 {
|
|
|
+ // blockNumber := &res.BlockBodiesPacket[0].Uncles[0].Number
|
|
|
+ // fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x6", blockNumber, peer.Node().IP())
|
|
|
+ //} else {
|
|
|
+ // fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x6", len(res.BlockBodiesPacket), len(res.BlockBodiesPacket[0].Uncles))
|
|
|
+ //
|
|
|
+ //}
|
|
|
+
|
|
|
return backend.Handle(peer, &res.BlockBodiesPacket)
|
|
|
}
|
|
|
|
|
|
@@ -392,6 +473,7 @@ func handleReceipts66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return backend.Handle(peer, &res.ReceiptsPacket)
|
|
|
}
|
|
|
|
|
|
+// 0x8
|
|
|
func handleNewPooledTransactionHashes(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// New transaction announcement arrived, make sure we have
|
|
|
// a valid and fresh chain to handle them
|
|
|
@@ -405,6 +487,15 @@ func handleNewPooledTransactionHashes(backend Backend, msg Decoder, peer *Peer)
|
|
|
// Schedule all the unknown hashes for retrieval
|
|
|
for _, hash := range *ann {
|
|
|
peer.markTransaction(hash)
|
|
|
+ //go func() {
|
|
|
+ // if !hashList.Has(hash) {
|
|
|
+ // // 如果哈希不存在于集合中,则将其添加到集合
|
|
|
+ // hashList.Add(hash)
|
|
|
+ // //fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x8", hash, peer.ID())
|
|
|
+ // hashList.ClearIfNeeded(177)
|
|
|
+ // }
|
|
|
+ //}()
|
|
|
+
|
|
|
}
|
|
|
return backend.Handle(peer, ann)
|
|
|
}
|
|
|
@@ -457,6 +548,7 @@ func answerGetPooledTransactions(backend Backend, query GetPooledTransactionsPac
|
|
|
return hashes, txs
|
|
|
}
|
|
|
|
|
|
+// 0x2
|
|
|
func handleTransactions(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Transactions arrived, make sure we have a valid and fresh chain to handle them
|
|
|
if !backend.AcceptTxs() {
|
|
|
@@ -472,11 +564,30 @@ func handleTransactions(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
if tx == nil {
|
|
|
return fmt.Errorf("%w: transaction %d is nil", errDecode, i)
|
|
|
}
|
|
|
+
|
|
|
peer.markTransaction(tx.Hash())
|
|
|
+ //go func() {
|
|
|
+ // hash := tx.Hash()
|
|
|
+ // if !txHashList.Has(hash) {
|
|
|
+ // // 如果哈希不存在于集合中,则将其添加到集合
|
|
|
+ // txHashList.Add(hash)
|
|
|
+ // txHashList.ClearIfNeeded(77)
|
|
|
+ // //fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x2", hash, peer.ID())
|
|
|
+ // if hashList.Has(hash) {
|
|
|
+ // timestampDiff, _ := GetTimestampDifference(hashList, txHashList, hash)
|
|
|
+ // fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x2", hash, peer.Node().IP(), timestampDiff)
|
|
|
+ // } else {
|
|
|
+ // fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0x2", hash, peer.Node().IP(), "0ms")
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ //}()
|
|
|
+
|
|
|
}
|
|
|
return backend.Handle(peer, &txs)
|
|
|
}
|
|
|
|
|
|
+// eth65 0x0a 10
|
|
|
func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Transactions arrived, make sure we have a valid and fresh chain to handle them
|
|
|
if !backend.AcceptTxs() {
|
|
|
@@ -497,6 +608,7 @@ func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
return backend.Handle(peer, &txs)
|
|
|
}
|
|
|
|
|
|
+// 0xa 10
|
|
|
func handlePooledTransactions66(backend Backend, msg Decoder, peer *Peer) error {
|
|
|
// Transactions arrived, make sure we have a valid and fresh chain to handle them
|
|
|
if !backend.AcceptTxs() {
|
|
|
@@ -513,8 +625,23 @@ func handlePooledTransactions66(backend Backend, msg Decoder, peer *Peer) error
|
|
|
return fmt.Errorf("%w: transaction %d is nil", errDecode, i)
|
|
|
}
|
|
|
peer.markTransaction(tx.Hash())
|
|
|
- }
|
|
|
- requestTracker.Fulfil(peer.id, peer.version, PooledTransactionsMsg, txs.RequestId)
|
|
|
+ //go func() {
|
|
|
+ // hash := tx.Hash()
|
|
|
+ // if !txHashList.Has(hash) {
|
|
|
+ // // 如果哈希不存在于集合中,则将其添加到集合
|
|
|
+ // txHashList.Add(hash)
|
|
|
+ // txHashList.ClearIfNeeded(777)
|
|
|
+ // //fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0xa", hash, peer.ID())
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // if hashList.Has(hash) {
|
|
|
+ // timestampDiff, _ := GetTimestampDifference(hashList, txHashList, hash)
|
|
|
+ // fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "eth.protocols.eth.handlers 0xa", hash, peer.Node().IP(), timestampDiff)
|
|
|
+ // }
|
|
|
+ //}()
|
|
|
+
|
|
|
+ }
|
|
|
+ //requestTracker.Fulfil(peer.id, peer.version, PooledTransactionsMsg, txs.RequestId)
|
|
|
|
|
|
return backend.Handle(peer, &txs.PooledTransactionsPacket)
|
|
|
}
|