mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
// Copyright 2019 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 (
|
|
"github.com/ethereum/go-ethereum/core"
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
)
|
|
|
|
// ENR is the "eth" Ethereum Node Record, advertising various information useful
|
|
// for maintaining the Ethereum computer network.
|
|
//
|
|
// Whilst only one thing is contained here for now, the ENR is a struct to permit
|
|
// future extensibility.
|
|
type ENR struct {
|
|
ForkID forkid.ID // Fork identifier per EIP-2124
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
}
|
|
|
|
// NewENR calculates the Ethereum network ENR from various information available
|
|
// from the chain.
|
|
func NewENR(chain *core.BlockChain) ENR {
|
|
return ENR{
|
|
ForkID: forkid.NewID(chain),
|
|
}
|
|
}
|
|
|
|
// ENRKey implements enr.Entry, returning the key for the chain config.
|
|
func (e ENR) ENRKey() string { return "eth" }
|
|
|
|
// NewENRFilter creates an ENR filter that returns if a record should be rejected
|
|
// or not (may be rejected by another filter).
|
|
func NewENRFilter(chain *core.BlockChain) func(r *enr.Record) error {
|
|
filter := forkid.NewFilter(chain)
|
|
|
|
return func(r *enr.Record) error {
|
|
// Retrieve the remote chain ENR entry, accept record if not found
|
|
var entry ENR
|
|
if err := r.Load(&entry); err != nil {
|
|
return nil
|
|
}
|
|
// If found, run it across the fork ID validator
|
|
return filter(entry.ForkID)
|
|
}
|
|
}
|