mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-18 16:44:12 +00:00
673d3a1646
* Update index.rst * Update handler.go
118 lines
4.5 KiB
Go
118 lines
4.5 KiB
Go
// Copyright 2020 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package eth
|
|
|
|
import (
|
|
"math/big"
|
|
"time"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
"github.com/ledgerwatch/erigon/common"
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
"github.com/ledgerwatch/erigon/p2p/enode"
|
|
"github.com/ledgerwatch/erigon/params"
|
|
)
|
|
|
|
const (
|
|
// softResponseLimit is the target maximum size of replies to data retrievals.
|
|
softResponseLimit = 2 * 1024 * 1024
|
|
|
|
// estHeaderSize is the approximate size of an RLP encoded block header.
|
|
estHeaderSize = 500
|
|
|
|
// maxHeadersServe is the maximum number of block headers to serve. This number
|
|
// is there to limit the number of disk lookups.
|
|
MaxHeadersServe = 1024
|
|
|
|
// maxBodiesServe is the maximum number of block bodies to serve. This number
|
|
// is mostly there to limit the number of disk lookups. With 24KB block sizes
|
|
// nowadays, the practical limit will always be softResponseLimit.
|
|
MaxBodiesServe = 1024
|
|
|
|
// maxReceiptsServe is the maximum number of block receipts to serve. This
|
|
// number is mostly there to limit the number of disk lookups. With block
|
|
// containing 200+ transactions nowadays, the practical limit will always
|
|
// be softResponseLimit.
|
|
maxReceiptsServe = 1024
|
|
)
|
|
|
|
// Handler is a callback to invoke from an outside runner after the boilerplate
|
|
// exchanges have passed.
|
|
type Handler func(peer *Peer) error
|
|
|
|
// Backend defines the data retrieval methods t,o serve remote requests and the
|
|
// callback methods to invoke on remote deliveries.
|
|
type Backend interface {
|
|
DB() kv.RwDB
|
|
|
|
// TxPool retrieves the transaction pool object to serve data.
|
|
TxPool() TxPool
|
|
|
|
// AcceptTxs retrieves whether transaction processing is enabled on the node
|
|
// or if inbound transactions should simply be dropped.
|
|
AcceptTxs() bool
|
|
|
|
// RunPeer is invoked when a peer joins on the `eth` protocol. The handler
|
|
// should do any peer maintenance work, handshakes and validations. If all
|
|
// is passed, control should be given back to the `handler` to process the
|
|
// inbound messages going forward.
|
|
RunPeer(peer *Peer, handler Handler) error
|
|
|
|
// PeerInfo retrieves all known `eth` information about a peer.
|
|
PeerInfo(id enode.ID) interface{}
|
|
|
|
// Handle is a callback to be invoked when a data packet is received from
|
|
// the remote peer. Only packets not consumed by the protocol handler will
|
|
// be forwarded to the backend.
|
|
Handle(peer *Peer, packet Packet) error
|
|
}
|
|
|
|
// TxPool defines the methods needed by the protocol handler to serve transactions.
|
|
type TxPool interface {
|
|
// Get retrieves the the transaction from the local txpool with the given hash.
|
|
Get(hash common.Hash) types.Transaction
|
|
}
|
|
|
|
// NodeInfo represents a short summary of the `eth` sub-protocol metadata
|
|
// known about the host peer.
|
|
type NodeInfo struct {
|
|
Network uint64 `json:"network"` // Ethereum network ID (1=Frontier, Ropsten=3, Rinkeby=4)
|
|
Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
|
|
Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
|
|
Config *params.ChainConfig `json:"config"` // ChainDB configuration for the fork rules
|
|
Head common.Hash `json:"head"` // Hex hash of the host's best owned block
|
|
}
|
|
|
|
// ReadNodeInfo retrieves some `eth` protocol metadata about the running host node.
|
|
func ReadNodeInfo(getter kv.Getter, config *params.ChainConfig, genesisHash common.Hash, network uint64) *NodeInfo {
|
|
head := rawdb.ReadCurrentHeader(getter)
|
|
td, _ := rawdb.ReadTd(getter, head.Hash(), head.Number.Uint64())
|
|
return &NodeInfo{
|
|
Network: network,
|
|
Difficulty: td,
|
|
Genesis: genesisHash,
|
|
Config: config,
|
|
Head: head.Hash(),
|
|
}
|
|
}
|
|
|
|
type Decoder interface {
|
|
Decode(val interface{}) error
|
|
Time() time.Time
|
|
}
|