Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement eth/68 #805

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions cmd/devp2p/internal/ethtest/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func (c *Chain) Head() *types.Block {
return c.blocks[c.Len()-1]
}

func (c *Chain) GetHeaders(req GetBlockHeaders) (BlockHeaders, error) {
func (c *Chain) GetHeaders(req *GetBlockHeaders) ([]*types.Header, error) {
if req.Amount < 1 {
return nil, fmt.Errorf("no block headers requested")
}

headers := make(BlockHeaders, req.Amount)
headers := make([]*types.Header, req.Amount)
var blockNumber uint64

// range over blocks to check if our chain has the requested header
Expand Down
76 changes: 49 additions & 27 deletions cmd/devp2p/internal/ethtest/chain_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
// This file is part of go-ethereum.
//
// 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
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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,
// go-ethereum 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.
// GNU 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/>.
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package ethtest

Expand All @@ -23,6 +23,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/eth/protocols/eth"
"github.com/scroll-tech/go-ethereum/p2p"
)
Expand Down Expand Up @@ -112,6 +113,27 @@ func TestEthProtocolNegotiation(t *testing.T) {
},
expected: uint32(64),
},
{
conn: &Conn{
ourHighestProtoVersion: 68,
},
caps: []p2p.Cap{
{Name: "eth", Version: 65},
{Name: "eth", Version: 66},
{Name: "wrongProto", Version: 68},
},
expected: uint32(66),
},
{
conn: &Conn{
ourHighestProtoVersion: 68,
},
caps: []p2p.Cap{
{Name: "eth", Version: 63},
{Name: "eth", Version: 68},
},
expected: uint32(68),
},
}

for i, tt := range tests {
Expand Down Expand Up @@ -141,18 +163,18 @@ func TestChain_GetHeaders(t *testing.T) {

var tests = []struct {
req GetBlockHeaders
expected BlockHeaders
expected []*types.Header
}{
{
req: GetBlockHeaders{
Origin: eth.HashOrNumber{
Number: uint64(2),
GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
Origin: eth.HashOrNumber{Number: uint64(2)},
Amount: uint64(5),
Skip: 1,
Reverse: false,
},
Amount: uint64(5),
Skip: 1,
Reverse: false,
},
expected: BlockHeaders{
expected: []*types.Header{
chain.blocks[2].Header(),
chain.blocks[4].Header(),
chain.blocks[6].Header(),
Expand All @@ -162,37 +184,37 @@ func TestChain_GetHeaders(t *testing.T) {
},
{
req: GetBlockHeaders{
Origin: eth.HashOrNumber{
Number: uint64(chain.Len() - 1),
GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
Origin: eth.HashOrNumber{Number: uint64(chain.Len() - 1)},
Amount: uint64(3),
Skip: 0,
Reverse: true,
},
Amount: uint64(3),
Skip: 0,
Reverse: true,
},
expected: BlockHeaders{
expected: []*types.Header{
chain.blocks[chain.Len()-1].Header(),
chain.blocks[chain.Len()-2].Header(),
chain.blocks[chain.Len()-3].Header(),
},
},
{
req: GetBlockHeaders{
Origin: eth.HashOrNumber{
Hash: chain.Head().Hash(),
GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
Origin: eth.HashOrNumber{Hash: chain.Head().Hash()},
Amount: uint64(1),
Skip: 0,
Reverse: false,
},
Amount: uint64(1),
Skip: 0,
Reverse: false,
},
expected: BlockHeaders{
expected: []*types.Header{
chain.Head().Header(),
},
},
}

for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
headers, err := chain.GetHeaders(tt.req)
headers, err := chain.GetHeaders(&tt.req)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading
Loading