mirror of https://github.com/status-im/op-geth.git
eth protocol changes
- changed backend interface - using callbacks for blockPool - use rlp stream for lazy decoding - use peer as logger - add id (peer pubkey) to ethProtocol fields - add testPeer to protocol test (temporary)
This commit is contained in:
parent
eb5cb04aa9
commit
d957dd2c9f
128
eth/protocol.go
128
eth/protocol.go
|
@ -7,18 +7,16 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethutil"
|
"github.com/ethereum/go-ethereum/ethutil"
|
||||||
ethlogger "github.com/ethereum/go-ethereum/logger"
|
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger = ethlogger.NewLogger("SERV")
|
|
||||||
|
|
||||||
// ethProtocol represents the ethereum wire protocol
|
// ethProtocol represents the ethereum wire protocol
|
||||||
// instance is running on each peer
|
// instance is running on each peer
|
||||||
type ethProtocol struct {
|
type ethProtocol struct {
|
||||||
eth backend
|
eth backend
|
||||||
td *big.Int
|
|
||||||
peer *p2p.Peer
|
peer *p2p.Peer
|
||||||
|
id string
|
||||||
rw p2p.MsgReadWriter
|
rw p2p.MsgReadWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,28 +24,21 @@ type ethProtocol struct {
|
||||||
// used as an argument to EthProtocol
|
// used as an argument to EthProtocol
|
||||||
type backend interface {
|
type backend interface {
|
||||||
GetTransactions() (txs []*types.Transaction)
|
GetTransactions() (txs []*types.Transaction)
|
||||||
AddTransactions(txs []*types.Transaction)
|
AddTransactions([]*types.Transaction)
|
||||||
GetBlockHashes(hash []byte, amount uint32) (hashes [][]byte)
|
GetBlockHashes(hash []byte, amount uint32) (hashes [][]byte)
|
||||||
AddHash(hash []byte, peer *p2p.Peer) (more bool)
|
AddBlockHashes(next func() ([]byte, bool), peerId string)
|
||||||
GetBlock(hash []byte) (block *types.Block)
|
GetBlock(hash []byte) (block *types.Block)
|
||||||
AddBlock(td *big.Int, block *types.Block, peer *p2p.Peer) (fetchHashes bool, err error)
|
AddBlock(block *types.Block, peerId string) (err error)
|
||||||
AddPeer(td *big.Int, currentBlock []byte, peer *p2p.Peer) (fetchHashes bool)
|
AddPeer(td *big.Int, currentBlock []byte, peerId string, requestHashes func([]byte) error, requestBlocks func([][]byte) error, invalidBlock func(error)) (best bool)
|
||||||
|
RemovePeer(peerId string)
|
||||||
Status() (td *big.Int, currentBlock []byte, genesisBlock []byte)
|
Status() (td *big.Int, currentBlock []byte, genesisBlock []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ProtocolVersion = 43
|
ProtocolVersion = 43
|
||||||
// 0x00 // PoC-1
|
|
||||||
// 0x01 // PoC-2
|
|
||||||
// 0x07 // PoC-3
|
|
||||||
// 0x09 // PoC-4
|
|
||||||
// 0x17 // PoC-5
|
|
||||||
// 0x1c // PoC-6
|
|
||||||
NetworkId = 0
|
NetworkId = 0
|
||||||
ProtocolLength = uint64(8)
|
ProtocolLength = uint64(8)
|
||||||
ProtocolMaxMsgSize = 10 * 1024 * 1024
|
ProtocolMaxMsgSize = 10 * 1024 * 1024
|
||||||
|
|
||||||
blockHashesBatchSize = 256
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// eth protocol message codes
|
// eth protocol message codes
|
||||||
|
@ -74,7 +65,8 @@ type getBlockHashesMsgData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// main entrypoint, wrappers starting a server running the eth protocol
|
// main entrypoint, wrappers starting a server running the eth protocol
|
||||||
// use this constructor to attach the protocol (class) to server caps
|
// use this constructor to attach the protocol ("class") to server caps
|
||||||
|
// the Dev p2p layer then runs the protocol instance on each peer
|
||||||
func EthProtocol(eth backend) *p2p.Protocol {
|
func EthProtocol(eth backend) *p2p.Protocol {
|
||||||
return &p2p.Protocol{
|
return &p2p.Protocol{
|
||||||
Name: "eth",
|
Name: "eth",
|
||||||
|
@ -86,11 +78,14 @@ func EthProtocol(eth backend) *p2p.Protocol {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the main loop that handles incoming messages
|
||||||
|
// note RemovePeer in the post-disconnect hook
|
||||||
func runEthProtocol(eth backend, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
|
func runEthProtocol(eth backend, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
|
||||||
self := ðProtocol{
|
self := ðProtocol{
|
||||||
eth: eth,
|
eth: eth,
|
||||||
rw: rw,
|
rw: rw,
|
||||||
peer: peer,
|
peer: peer,
|
||||||
|
id: (string)(peer.Identity().Pubkey()),
|
||||||
}
|
}
|
||||||
err = self.handleStatus()
|
err = self.handleStatus()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -98,6 +93,7 @@ func runEthProtocol(eth backend, peer *p2p.Peer, rw p2p.MsgReadWriter) (err erro
|
||||||
for {
|
for {
|
||||||
err = self.handle()
|
err = self.handle()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
self.eth.RemovePeer(self.id)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,6 +128,7 @@ func (self *ethProtocol) handle() error {
|
||||||
return self.rw.EncodeMsg(TxMsg, txsInterface...)
|
return self.rw.EncodeMsg(TxMsg, txsInterface...)
|
||||||
|
|
||||||
case TxMsg:
|
case TxMsg:
|
||||||
|
// TODO: rework using lazy RLP stream
|
||||||
var txs []*types.Transaction
|
var txs []*types.Transaction
|
||||||
if err := msg.Decode(&txs); err != nil {
|
if err := msg.Decode(&txs); err != nil {
|
||||||
return ProtocolError(ErrDecode, "%v", err)
|
return ProtocolError(ErrDecode, "%v", err)
|
||||||
|
@ -148,29 +145,26 @@ func (self *ethProtocol) handle() error {
|
||||||
|
|
||||||
case BlockHashesMsg:
|
case BlockHashesMsg:
|
||||||
// TODO: redo using lazy decode , this way very inefficient on known chains
|
// TODO: redo using lazy decode , this way very inefficient on known chains
|
||||||
// s := rlp.NewListStream(msg.Payload, uint64(msg.Size))
|
msgStream := rlp.NewListStream(msg.Payload, uint64(msg.Size))
|
||||||
var blockHashes [][]byte
|
var err error
|
||||||
if err := msg.Decode(&blockHashes); err != nil {
|
iter := func() (hash []byte, ok bool) {
|
||||||
|
hash, err = msgStream.Bytes()
|
||||||
|
if err == nil {
|
||||||
|
ok = true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.eth.AddBlockHashes(iter, self.id)
|
||||||
|
if err != nil && err != rlp.EOL {
|
||||||
return ProtocolError(ErrDecode, "%v", err)
|
return ProtocolError(ErrDecode, "%v", err)
|
||||||
}
|
}
|
||||||
fetchMore := true
|
|
||||||
for _, hash := range blockHashes {
|
|
||||||
fetchMore = self.eth.AddHash(hash, self.peer)
|
|
||||||
if !fetchMore {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if fetchMore {
|
|
||||||
return self.FetchHashes(blockHashes[len(blockHashes)-1])
|
|
||||||
}
|
|
||||||
|
|
||||||
case GetBlocksMsg:
|
case GetBlocksMsg:
|
||||||
// Limit to max 300 blocks
|
|
||||||
var blockHashes [][]byte
|
var blockHashes [][]byte
|
||||||
if err := msg.Decode(&blockHashes); err != nil {
|
if err := msg.Decode(&blockHashes); err != nil {
|
||||||
return ProtocolError(ErrDecode, "%v", err)
|
return ProtocolError(ErrDecode, "%v", err)
|
||||||
}
|
}
|
||||||
max := int(math.Min(float64(len(blockHashes)), 300.0))
|
max := int(math.Min(float64(len(blockHashes)), blockHashesBatchSize))
|
||||||
var blocks []interface{}
|
var blocks []interface{}
|
||||||
for i, hash := range blockHashes {
|
for i, hash := range blockHashes {
|
||||||
if i >= max {
|
if i >= max {
|
||||||
|
@ -184,20 +178,19 @@ func (self *ethProtocol) handle() error {
|
||||||
return self.rw.EncodeMsg(BlocksMsg, blocks...)
|
return self.rw.EncodeMsg(BlocksMsg, blocks...)
|
||||||
|
|
||||||
case BlocksMsg:
|
case BlocksMsg:
|
||||||
var blocks []*types.Block
|
msgStream := rlp.NewListStream(msg.Payload, uint64(msg.Size))
|
||||||
if err := msg.Decode(&blocks); err != nil {
|
for {
|
||||||
|
var block *types.Block
|
||||||
|
if err := msgStream.Decode(&block); err != nil {
|
||||||
|
if err == rlp.EOL {
|
||||||
|
break
|
||||||
|
} else {
|
||||||
return ProtocolError(ErrDecode, "%v", err)
|
return ProtocolError(ErrDecode, "%v", err)
|
||||||
}
|
}
|
||||||
for _, block := range blocks {
|
}
|
||||||
fetchHashes, err := self.eth.AddBlock(nil, block, self.peer)
|
if err := self.eth.AddBlock(block, self.id); err != nil {
|
||||||
if err != nil {
|
|
||||||
return ProtocolError(ErrInvalidBlock, "%v", err)
|
return ProtocolError(ErrInvalidBlock, "%v", err)
|
||||||
}
|
}
|
||||||
if fetchHashes {
|
|
||||||
if err := self.FetchHashes(block.Hash()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case NewBlockMsg:
|
case NewBlockMsg:
|
||||||
|
@ -205,13 +198,24 @@ func (self *ethProtocol) handle() error {
|
||||||
if err := msg.Decode(&request); err != nil {
|
if err := msg.Decode(&request); err != nil {
|
||||||
return ProtocolError(ErrDecode, "%v", err)
|
return ProtocolError(ErrDecode, "%v", err)
|
||||||
}
|
}
|
||||||
var fetchHashes bool
|
hash := request.Block.Hash()
|
||||||
// this should reset td and offer blockpool as candidate new peer?
|
// to simplify backend interface adding a new block
|
||||||
if fetchHashes, err = self.eth.AddBlock(request.TD, request.Block, self.peer); err != nil {
|
// uses AddPeer followed by AddHashes, AddBlock only if peer is the best peer
|
||||||
|
// (or selected as new best peer)
|
||||||
|
if self.eth.AddPeer(request.TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.invalidBlock) {
|
||||||
|
called := true
|
||||||
|
iter := func() (hash []byte, ok bool) {
|
||||||
|
if called {
|
||||||
|
called = false
|
||||||
|
return hash, true
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.eth.AddBlockHashes(iter, self.id)
|
||||||
|
if err := self.eth.AddBlock(request.Block, self.id); err != nil {
|
||||||
return ProtocolError(ErrInvalidBlock, "%v", err)
|
return ProtocolError(ErrInvalidBlock, "%v", err)
|
||||||
}
|
}
|
||||||
if fetchHashes {
|
|
||||||
return self.FetchHashes(request.Block.Hash())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -279,16 +283,34 @@ func (self *ethProtocol) handleStatus() error {
|
||||||
return ProtocolError(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, ProtocolVersion)
|
return ProtocolError(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, ProtocolVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof("Peer is [eth] capable (%d/%d). TD = %v ~ %x", status.ProtocolVersion, status.NetworkId, status.CurrentBlock)
|
self.peer.Infof("Peer is [eth] capable (%d/%d). TD = %v ~ %x", status.ProtocolVersion, status.NetworkId, status.CurrentBlock)
|
||||||
|
|
||||||
if self.eth.AddPeer(status.TD, status.CurrentBlock, self.peer) {
|
self.eth.AddPeer(status.TD, status.CurrentBlock, self.id, self.requestBlockHashes, self.requestBlocks, self.invalidBlock)
|
||||||
return self.FetchHashes(status.CurrentBlock)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *ethProtocol) FetchHashes(from []byte) error {
|
func (self *ethProtocol) requestBlockHashes(from []byte) error {
|
||||||
logger.Debugf("Fetching hashes (%d) %x...\n", blockHashesBatchSize, from[0:4])
|
self.peer.Debugf("fetching hashes (%d) %x...\n", blockHashesBatchSize, from[0:4])
|
||||||
return self.rw.EncodeMsg(GetBlockHashesMsg, from, blockHashesBatchSize)
|
return self.rw.EncodeMsg(GetBlockHashesMsg, from, blockHashesBatchSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *ethProtocol) requestBlocks(hashes [][]byte) error {
|
||||||
|
self.peer.Debugf("fetching %v blocks", len(hashes))
|
||||||
|
return self.rw.EncodeMsg(GetBlocksMsg, ethutil.ByteSliceToInterface(hashes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *ethProtocol) invalidBlock(err error) {
|
||||||
|
ProtocolError(ErrInvalidBlock, "%v", err)
|
||||||
|
self.peer.Disconnect(p2p.DiscSubprotocolError)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *ethProtocol) protoError(code int, format string, params ...interface{}) (err *protocolError) {
|
||||||
|
err = ProtocolError(code, format, params...)
|
||||||
|
if err.Fatal() {
|
||||||
|
self.peer.Errorln(err)
|
||||||
|
} else {
|
||||||
|
self.peer.Debugln(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -55,10 +56,11 @@ type TestBackend struct {
|
||||||
getTransactions func() []*types.Transaction
|
getTransactions func() []*types.Transaction
|
||||||
addTransactions func(txs []*types.Transaction)
|
addTransactions func(txs []*types.Transaction)
|
||||||
getBlockHashes func(hash []byte, amount uint32) (hashes [][]byte)
|
getBlockHashes func(hash []byte, amount uint32) (hashes [][]byte)
|
||||||
addHash func(hash []byte, peer *p2p.Peer) (more bool)
|
addBlockHashes func(next func() ([]byte, bool), peerId string)
|
||||||
getBlock func(hash []byte) *types.Block
|
getBlock func(hash []byte) *types.Block
|
||||||
addBlock func(td *big.Int, block *types.Block, peer *p2p.Peer) (fetchHashes bool, err error)
|
addBlock func(block *types.Block, peerId string) (err error)
|
||||||
addPeer func(td *big.Int, currentBlock []byte, peer *p2p.Peer) (fetchHashes bool)
|
addPeer func(td *big.Int, currentBlock []byte, peerId string, requestHashes func([]byte) error, requestBlocks func([][]byte) error, invalidBlock func(error)) (best bool)
|
||||||
|
removePeer func(peerId string)
|
||||||
status func() (td *big.Int, currentBlock []byte, genesisBlock []byte)
|
status func() (td *big.Int, currentBlock []byte, genesisBlock []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,12 +84,12 @@ func (self *TestBackend) GetBlockHashes(hash []byte, amount uint32) (hashes [][]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TestBackend) AddHash(hash []byte, peer *p2p.Peer) (more bool) {
|
func (self *TestBackend) AddBlockHashes(next func() ([]byte, bool), peerId string) {
|
||||||
if self.addHash != nil {
|
if self.addBlockHashes != nil {
|
||||||
more = self.addHash(hash, peer)
|
self.addBlockHashes(next, peerId)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TestBackend) GetBlock(hash []byte) (block *types.Block) {
|
func (self *TestBackend) GetBlock(hash []byte) (block *types.Block) {
|
||||||
if self.getBlock != nil {
|
if self.getBlock != nil {
|
||||||
block = self.getBlock(hash)
|
block = self.getBlock(hash)
|
||||||
|
@ -95,20 +97,26 @@ func (self *TestBackend) GetBlock(hash []byte) (block *types.Block) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TestBackend) AddBlock(td *big.Int, block *types.Block, peer *p2p.Peer) (fetchHashes bool, err error) {
|
func (self *TestBackend) AddBlock(block *types.Block, peerId string) (err error) {
|
||||||
if self.addBlock != nil {
|
if self.addBlock != nil {
|
||||||
fetchHashes, err = self.addBlock(td, block, peer)
|
err = self.addBlock(block, peerId)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TestBackend) AddPeer(td *big.Int, currentBlock []byte, peer *p2p.Peer) (fetchHashes bool) {
|
func (self *TestBackend) AddPeer(td *big.Int, currentBlock []byte, peerId string, requestBlockHashes func([]byte) error, requestBlocks func([][]byte) error, invalidBlock func(error)) (best bool) {
|
||||||
if self.addPeer != nil {
|
if self.addPeer != nil {
|
||||||
fetchHashes = self.addPeer(td, currentBlock, peer)
|
best = self.addPeer(td, currentBlock, peerId, requestBlockHashes, requestBlocks, invalidBlock)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *TestBackend) RemovePeer(peerId string) {
|
||||||
|
if self.removePeer != nil {
|
||||||
|
self.removePeer(peerId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (self *TestBackend) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) {
|
func (self *TestBackend) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) {
|
||||||
if self.status != nil {
|
if self.status != nil {
|
||||||
td, currentBlock, genesisBlock = self.status()
|
td, currentBlock, genesisBlock = self.status()
|
||||||
|
@ -116,13 +124,35 @@ func (self *TestBackend) Status() (td *big.Int, currentBlock []byte, genesisBloc
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEth(t *testing.T) {
|
// TODO: refactor this into p2p/client_identity
|
||||||
|
type peerId struct {
|
||||||
|
pubkey []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *peerId) String() string {
|
||||||
|
return "test peer"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *peerId) Pubkey() (pubkey []byte) {
|
||||||
|
pubkey = self.pubkey
|
||||||
|
if len(pubkey) == 0 {
|
||||||
|
pubkey = crypto.GenerateNewKeyPair().PublicKey
|
||||||
|
self.pubkey = pubkey
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func testPeer() *p2p.Peer {
|
||||||
|
return p2p.NewPeer(&peerId{}, []p2p.Cap{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrNoStatusMsg(t *testing.T) {
|
||||||
quit := make(chan bool)
|
quit := make(chan bool)
|
||||||
rw := &testMsgReadWriter{make(chan p2p.Msg, 10), make(chan p2p.Msg, 10)}
|
rw := &testMsgReadWriter{make(chan p2p.Msg, 10), make(chan p2p.Msg, 10)}
|
||||||
testBackend := &TestBackend{}
|
testBackend := &TestBackend{}
|
||||||
var err error
|
var err error
|
||||||
go func() {
|
go func() {
|
||||||
err = runEthProtocol(testBackend, nil, rw)
|
err = runEthProtocol(testBackend, testPeer(), rw)
|
||||||
close(quit)
|
close(quit)
|
||||||
}()
|
}()
|
||||||
statusMsg := p2p.NewMsg(4)
|
statusMsg := p2p.NewMsg(4)
|
||||||
|
|
Loading…
Reference in New Issue