2016-11-08 19:01:56 -06:00
|
|
|
// Copyright 2016 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/>.
|
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
2018-02-11 06:57:46 -06:00
|
|
|
"encoding/binary"
|
2017-10-24 08:19:09 -05:00
|
|
|
"math/big"
|
2016-10-13 22:51:29 -05:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
2017-10-25 04:18:44 -05:00
|
|
|
"time"
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-05-30 13:51:13 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2017-12-22 06:37:50 -06:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2016-10-13 22:51:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 06:35:06 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2022-10-24 08:13:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2016-10-13 22:51:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-09-10 02:55:48 -05:00
|
|
|
"github.com/ethereum/go-ethereum/les/downloader"
|
2017-10-24 08:19:09 -05:00
|
|
|
"github.com/ethereum/go-ethereum/light"
|
2016-10-13 22:51:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2017-10-24 08:19:09 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-10-13 22:51:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
|
|
|
func expectResponse(r p2p.MsgReader, msgcode, reqID, bv uint64, data interface{}) error {
|
|
|
|
type resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
return p2p.ExpectMsg(r, msgcode, resp{reqID, bv, data})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that block headers can be retrieved from a remote chain based on user queries.
|
2017-10-24 08:19:09 -05:00
|
|
|
func TestGetBlockHeadersLes2(t *testing.T) { testGetBlockHeaders(t, 2) }
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetBlockHeadersLes3(t *testing.T) { testGetBlockHeaders(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetBlockHeadersLes4(t *testing.T) { testGetBlockHeaders(t, 4) }
|
2017-10-24 08:19:09 -05:00
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
func testGetBlockHeaders(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: downloader.MaxHeaderFetch + 15,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
// Create a "random" unknown hash for testing
|
|
|
|
var unknown common.Hash
|
2017-01-06 08:52:03 -06:00
|
|
|
for i := range unknown {
|
2016-10-13 22:51:29 -05:00
|
|
|
unknown[i] = byte(i)
|
|
|
|
}
|
|
|
|
// Create a batch of tests for various scenarios
|
|
|
|
limit := uint64(MaxHeaderFetch)
|
|
|
|
tests := []struct {
|
2021-02-20 03:40:38 -06:00
|
|
|
query *GetBlockHeadersData // The query to execute for header retrieval
|
2016-10-13 22:51:29 -05:00
|
|
|
expect []common.Hash // The hashes of the block whose headers are expected
|
|
|
|
}{
|
|
|
|
// A single random block should be retrievable by hash and number too
|
|
|
|
{
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Hash: bc.GetBlockByNumber(limit / 2).Hash()}, Amount: 1},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{bc.GetBlockByNumber(limit / 2).Hash()},
|
|
|
|
}, {
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 1},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{bc.GetBlockByNumber(limit / 2).Hash()},
|
|
|
|
},
|
|
|
|
// Multiple headers should be retrievable in both directions
|
|
|
|
{
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
|
|
|
bc.GetBlockByNumber(limit / 2).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 + 1).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 + 2).Hash(),
|
|
|
|
},
|
|
|
|
}, {
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3, Reverse: true},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
|
|
|
bc.GetBlockByNumber(limit / 2).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 - 1).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 - 2).Hash(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Multiple headers with skip lists should be retrievable
|
|
|
|
{
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
|
|
|
bc.GetBlockByNumber(limit / 2).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 + 4).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 + 8).Hash(),
|
|
|
|
},
|
|
|
|
}, {
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3, Reverse: true},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
|
|
|
bc.GetBlockByNumber(limit / 2).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 - 4).Hash(),
|
|
|
|
bc.GetBlockByNumber(limit/2 - 8).Hash(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// The chain endpoints should be retrievable
|
|
|
|
{
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: 0}, Amount: 1},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{bc.GetBlockByNumber(0).Hash()},
|
|
|
|
}, {
|
2023-03-02 00:29:15 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().Number.Uint64()}, Amount: 1},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{bc.CurrentBlock().Hash()},
|
|
|
|
},
|
|
|
|
// Ensure protocol limits are honored
|
2019-08-21 04:29:34 -05:00
|
|
|
//{
|
2023-03-02 00:29:15 -06:00
|
|
|
// &GetBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().Number.Uint64()() - 1}, Amount: limit + 10, Reverse: true},
|
2019-08-21 04:29:34 -05:00
|
|
|
// []common.Hash{},
|
|
|
|
//},
|
2016-10-13 22:51:29 -05:00
|
|
|
// Check that requesting more than available is handled gracefully
|
|
|
|
{
|
2023-03-02 00:29:15 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().Number.Uint64() - 4}, Skip: 3, Amount: 3},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
2023-03-02 00:29:15 -06:00
|
|
|
bc.GetBlockByNumber(bc.CurrentBlock().Number.Uint64() - 4).Hash(),
|
|
|
|
bc.GetBlockByNumber(bc.CurrentBlock().Number.Uint64()).Hash(),
|
2016-10-13 22:51:29 -05:00
|
|
|
},
|
|
|
|
}, {
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 3, Amount: 3, Reverse: true},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
|
|
|
bc.GetBlockByNumber(4).Hash(),
|
|
|
|
bc.GetBlockByNumber(0).Hash(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Check that requesting more than available is handled gracefully, even if mid skip
|
|
|
|
{
|
2023-03-02 00:29:15 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().Number.Uint64() - 4}, Skip: 2, Amount: 3},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
2023-03-02 00:29:15 -06:00
|
|
|
bc.GetBlockByNumber(bc.CurrentBlock().Number.Uint64() - 4).Hash(),
|
|
|
|
bc.GetBlockByNumber(bc.CurrentBlock().Number.Uint64() - 1).Hash(),
|
2016-10-13 22:51:29 -05:00
|
|
|
},
|
|
|
|
}, {
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 2, Amount: 3, Reverse: true},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{
|
|
|
|
bc.GetBlockByNumber(4).Hash(),
|
|
|
|
bc.GetBlockByNumber(1).Hash(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Check that non existing headers aren't returned
|
|
|
|
{
|
2021-02-20 03:40:38 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Hash: unknown}, Amount: 1},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{},
|
|
|
|
}, {
|
2023-03-02 00:29:15 -06:00
|
|
|
&GetBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().Number.Uint64() + 1}, Amount: 1},
|
2016-10-13 22:51:29 -05:00
|
|
|
[]common.Hash{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Run each of the tests and verify the results against the chain
|
|
|
|
var reqID uint64
|
|
|
|
for i, tt := range tests {
|
|
|
|
// Collect the headers to expect in the response
|
2019-06-27 04:03:57 -05:00
|
|
|
var headers []*types.Header
|
2016-10-13 22:51:29 -05:00
|
|
|
for _, hash := range tt.expect {
|
|
|
|
headers = append(headers, bc.GetHeaderByHash(hash))
|
|
|
|
}
|
|
|
|
// Send the hash request and verify the response
|
|
|
|
reqID++
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetBlockHeadersMsg, reqID, tt.query)
|
|
|
|
if err := expectResponse(rawPeer.app, BlockHeadersMsg, reqID, testBufLimit, headers); err != nil {
|
2016-10-13 22:51:29 -05:00
|
|
|
t.Errorf("test %d: headers mismatch: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that block contents can be retrieved from a remote chain based on their hashes.
|
2017-10-24 08:19:09 -05:00
|
|
|
func TestGetBlockBodiesLes2(t *testing.T) { testGetBlockBodies(t, 2) }
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetBlockBodiesLes3(t *testing.T) { testGetBlockBodies(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetBlockBodiesLes4(t *testing.T) { testGetBlockBodies(t, 4) }
|
2017-10-24 08:19:09 -05:00
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
func testGetBlockBodies(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: downloader.MaxHeaderFetch + 15,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
// Create a batch of tests for various scenarios
|
|
|
|
limit := MaxBodyFetch
|
|
|
|
tests := []struct {
|
|
|
|
random int // Number of blocks to fetch randomly from the chain
|
|
|
|
explicit []common.Hash // Explicitly requested blocks
|
|
|
|
available []bool // Availability of explicitly requested blocks
|
|
|
|
expected int // Total number of existing blocks to expect
|
|
|
|
}{
|
|
|
|
{1, nil, nil, 1}, // A single random block should be retrievable
|
|
|
|
{10, nil, nil, 10}, // Multiple random blocks should be retrievable
|
|
|
|
{limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
|
|
|
|
//{limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
|
|
|
|
{0, []common.Hash{bc.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
|
|
|
|
{0, []common.Hash{bc.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
|
2017-01-06 08:52:03 -06:00
|
|
|
{0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
// Existing and non-existing blocks interleaved should not cause problems
|
|
|
|
{0, []common.Hash{
|
2017-01-06 08:52:03 -06:00
|
|
|
{},
|
2016-10-13 22:51:29 -05:00
|
|
|
bc.GetBlockByNumber(1).Hash(),
|
2017-01-06 08:52:03 -06:00
|
|
|
{},
|
2016-10-13 22:51:29 -05:00
|
|
|
bc.GetBlockByNumber(10).Hash(),
|
2017-01-06 08:52:03 -06:00
|
|
|
{},
|
2016-10-13 22:51:29 -05:00
|
|
|
bc.GetBlockByNumber(100).Hash(),
|
2017-01-06 08:52:03 -06:00
|
|
|
{},
|
2016-10-13 22:51:29 -05:00
|
|
|
}, []bool{false, true, false, true, false, true, false}, 3},
|
|
|
|
}
|
|
|
|
// Run each of the tests and verify the results against the chain
|
|
|
|
var reqID uint64
|
|
|
|
for i, tt := range tests {
|
|
|
|
// Collect the hashes to request, and the response to expect
|
2019-06-27 04:03:57 -05:00
|
|
|
var hashes []common.Hash
|
|
|
|
seen := make(map[int64]bool)
|
|
|
|
var bodies []*types.Body
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
for j := 0; j < tt.random; j++ {
|
|
|
|
for {
|
2023-03-02 00:29:15 -06:00
|
|
|
num := rand.Int63n(int64(bc.CurrentBlock().Number.Uint64()))
|
2016-10-13 22:51:29 -05:00
|
|
|
if !seen[num] {
|
|
|
|
seen[num] = true
|
|
|
|
|
|
|
|
block := bc.GetBlockByNumber(uint64(num))
|
|
|
|
hashes = append(hashes, block.Hash())
|
|
|
|
if len(bodies) < tt.expected {
|
|
|
|
bodies = append(bodies, &types.Body{Transactions: block.Transactions(), Uncles: block.Uncles()})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for j, hash := range tt.explicit {
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
if tt.available[j] && len(bodies) < tt.expected {
|
|
|
|
block := bc.GetBlockByHash(hash)
|
|
|
|
bodies = append(bodies, &types.Body{Transactions: block.Transactions(), Uncles: block.Uncles()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reqID++
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
// Send the hash request and verify the response
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetBlockBodiesMsg, reqID, hashes)
|
|
|
|
if err := expectResponse(rawPeer.app, BlockBodiesMsg, reqID, testBufLimit, bodies); err != nil {
|
2016-10-13 22:51:29 -05:00
|
|
|
t.Errorf("test %d: bodies mismatch: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the contract codes can be retrieved based on account addresses.
|
2017-10-24 08:19:09 -05:00
|
|
|
func TestGetCodeLes2(t *testing.T) { testGetCode(t, 2) }
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetCodeLes3(t *testing.T) { testGetCode(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetCodeLes4(t *testing.T) { testGetCode(t, 4) }
|
2017-10-24 08:19:09 -05:00
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
func testGetCode(t *testing.T, protocol int) {
|
|
|
|
// Assemble the test environment
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: 4,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2021-02-25 07:24:04 -06:00
|
|
|
|
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
var codereqs []*CodeReq
|
|
|
|
var codes [][]byte
|
2023-03-02 00:29:15 -06:00
|
|
|
for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ {
|
2016-10-13 22:51:29 -05:00
|
|
|
header := bc.GetHeaderByNumber(i)
|
|
|
|
req := &CodeReq{
|
|
|
|
BHash: header.Hash(),
|
|
|
|
AccKey: crypto.Keccak256(testContractAddr[:]),
|
|
|
|
}
|
|
|
|
codereqs = append(codereqs, req)
|
|
|
|
if i >= testContractDeployed {
|
|
|
|
codes = append(codes, testContractCodeDeployed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetCodeMsg, 42, codereqs)
|
|
|
|
if err := expectResponse(rawPeer.app, CodeMsg, 42, testBufLimit, codes); err != nil {
|
2016-10-13 22:51:29 -05:00
|
|
|
t.Errorf("codes mismatch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:40:32 -05:00
|
|
|
// Tests that the stale contract codes can't be retrieved based on account addresses.
|
|
|
|
func TestGetStaleCodeLes2(t *testing.T) { testGetStaleCode(t, 2) }
|
|
|
|
func TestGetStaleCodeLes3(t *testing.T) { testGetStaleCode(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetStaleCodeLes4(t *testing.T) { testGetStaleCode(t, 4) }
|
2019-06-11 02:40:32 -05:00
|
|
|
|
|
|
|
func testGetStaleCode(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: core.TriesInMemory + 4,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2019-06-11 02:40:32 -05:00
|
|
|
defer tearDown()
|
2021-02-25 07:24:04 -06:00
|
|
|
|
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2019-06-11 02:40:32 -05:00
|
|
|
|
|
|
|
check := func(number uint64, expected [][]byte) {
|
|
|
|
req := &CodeReq{
|
|
|
|
BHash: bc.GetHeaderByNumber(number).Hash(),
|
|
|
|
AccKey: crypto.Keccak256(testContractAddr[:]),
|
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetCodeMsg, 42, []*CodeReq{req})
|
|
|
|
if err := expectResponse(rawPeer.app, CodeMsg, 42, testBufLimit, expected); err != nil {
|
2019-06-11 02:40:32 -05:00
|
|
|
t.Errorf("codes mismatch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check(0, [][]byte{}) // Non-exist contract
|
|
|
|
check(testContractDeployed, [][]byte{}) // Stale contract
|
|
|
|
check(bc.CurrentHeader().Number.Uint64(), [][]byte{testContractCodeDeployed}) // Fresh contract
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
// Tests that the transaction receipts can be retrieved based on hashes.
|
2017-10-24 08:19:09 -05:00
|
|
|
func TestGetReceiptLes2(t *testing.T) { testGetReceipt(t, 2) }
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetReceiptLes3(t *testing.T) { testGetReceipt(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetReceiptLes4(t *testing.T) { testGetReceipt(t, 4) }
|
2017-10-24 08:19:09 -05:00
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
func testGetReceipt(t *testing.T, protocol int) {
|
|
|
|
// Assemble the test environment
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: 4,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
// Collect the hashes to request, and the response to expect
|
2019-06-27 04:03:57 -05:00
|
|
|
var receipts []types.Receipts
|
|
|
|
var hashes []common.Hash
|
2023-03-02 00:29:15 -06:00
|
|
|
for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ {
|
2016-10-13 22:51:29 -05:00
|
|
|
block := bc.GetBlockByNumber(i)
|
|
|
|
|
|
|
|
hashes = append(hashes, block.Hash())
|
2021-06-15 05:56:14 -05:00
|
|
|
receipts = append(receipts, rawdb.ReadReceipts(server.db, block.Hash(), block.NumberU64(), bc.Config()))
|
2016-10-13 22:51:29 -05:00
|
|
|
}
|
|
|
|
// Send the hash request and verify the response
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetReceiptsMsg, 42, hashes)
|
|
|
|
if err := expectResponse(rawPeer.app, ReceiptsMsg, 42, testBufLimit, receipts); err != nil {
|
2016-10-13 22:51:29 -05:00
|
|
|
t.Errorf("receipts mismatch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that trie merkle proofs can be retrieved
|
2017-10-24 08:19:09 -05:00
|
|
|
func TestGetProofsLes2(t *testing.T) { testGetProofs(t, 2) }
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetProofsLes3(t *testing.T) { testGetProofs(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetProofsLes4(t *testing.T) { testGetProofs(t, 4) }
|
2017-10-24 08:19:09 -05:00
|
|
|
|
2016-10-13 22:51:29 -05:00
|
|
|
func testGetProofs(t *testing.T, protocol int) {
|
|
|
|
// Assemble the test environment
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: 4,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2016-10-13 22:51:29 -05:00
|
|
|
|
2019-04-05 10:01:51 -05:00
|
|
|
var proofreqs []ProofReq
|
2017-10-24 08:19:09 -05:00
|
|
|
proofsV2 := light.NewNodeSet()
|
2016-10-13 22:51:29 -05:00
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
accounts := []common.Address{bankAddr, userAddr1, userAddr2, signerAddr, {}}
|
2023-03-02 00:29:15 -06:00
|
|
|
for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ {
|
2016-10-13 22:51:29 -05:00
|
|
|
header := bc.GetHeaderByNumber(i)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ := trie.New(trie.StateTrieID(header.Root), trie.NewDatabase(server.db))
|
2016-10-13 22:51:29 -05:00
|
|
|
|
|
|
|
for _, acc := range accounts {
|
|
|
|
req := ProofReq{
|
|
|
|
BHash: header.Hash(),
|
2017-08-08 11:31:08 -05:00
|
|
|
Key: crypto.Keccak256(acc[:]),
|
2016-10-13 22:51:29 -05:00
|
|
|
}
|
|
|
|
proofreqs = append(proofreqs, req)
|
2019-04-05 10:01:51 -05:00
|
|
|
trie.Prove(crypto.Keccak256(acc[:]), 0, proofsV2)
|
2016-10-13 22:51:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Send the proof request and verify the response
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetProofsV2Msg, 42, proofreqs)
|
|
|
|
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, proofsV2.NodeList()); err != nil {
|
2019-04-05 10:01:51 -05:00
|
|
|
t.Errorf("proofs mismatch: %v", err)
|
2018-02-11 06:57:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:40:32 -05:00
|
|
|
// Tests that the stale contract codes can't be retrieved based on account addresses.
|
|
|
|
func TestGetStaleProofLes2(t *testing.T) { testGetStaleProof(t, 2) }
|
|
|
|
func TestGetStaleProofLes3(t *testing.T) { testGetStaleProof(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetStaleProofLes4(t *testing.T) { testGetStaleProof(t, 4) }
|
2019-06-11 02:40:32 -05:00
|
|
|
|
|
|
|
func testGetStaleProof(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: core.TriesInMemory + 4,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2019-06-11 02:40:32 -05:00
|
|
|
defer tearDown()
|
2021-02-25 07:24:04 -06:00
|
|
|
|
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2019-06-11 02:40:32 -05:00
|
|
|
|
|
|
|
check := func(number uint64, wantOK bool) {
|
|
|
|
var (
|
|
|
|
header = bc.GetHeaderByNumber(number)
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
account = crypto.Keccak256(userAddr1.Bytes())
|
2019-06-11 02:40:32 -05:00
|
|
|
)
|
|
|
|
req := &ProofReq{
|
|
|
|
BHash: header.Hash(),
|
|
|
|
Key: account,
|
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetProofsV2Msg, 42, []*ProofReq{req})
|
2019-06-11 02:40:32 -05:00
|
|
|
|
|
|
|
var expected []rlp.RawValue
|
|
|
|
if wantOK {
|
|
|
|
proofsV2 := light.NewNodeSet()
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
t, _ := trie.New(trie.StateTrieID(header.Root), trie.NewDatabase(server.db))
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
t.Prove(account, 0, proofsV2)
|
2019-06-11 02:40:32 -05:00
|
|
|
expected = proofsV2.NodeList()
|
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil {
|
2019-06-11 02:40:32 -05:00
|
|
|
t.Errorf("codes mismatch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check(0, false) // Non-exist proof
|
|
|
|
check(2, false) // Stale proof
|
|
|
|
check(bc.CurrentHeader().Number.Uint64(), true) // Fresh proof
|
|
|
|
}
|
|
|
|
|
2018-02-11 06:57:46 -06:00
|
|
|
// Tests that CHT proofs can be correctly retrieved.
|
|
|
|
func TestGetCHTProofsLes2(t *testing.T) { testGetCHTProofs(t, 2) }
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetCHTProofsLes3(t *testing.T) { testGetCHTProofs(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetCHTProofsLes4(t *testing.T) { testGetCHTProofs(t, 4) }
|
2018-02-11 06:57:46 -06:00
|
|
|
|
|
|
|
func testGetCHTProofs(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
var (
|
|
|
|
config = light.TestServerIndexerConfig
|
|
|
|
waitIndexers = func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
|
|
|
|
for {
|
|
|
|
cs, _, _ := cIndexer.Sections()
|
|
|
|
if cs >= 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-08-28 02:08:16 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig = testnetConfig{
|
|
|
|
blocks: int(config.ChtSize + config.ChtConfirms),
|
|
|
|
protocol: protocol,
|
|
|
|
indexFn: waitIndexers,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2018-02-11 06:57:46 -06:00
|
|
|
|
|
|
|
// Assemble the proofs from the different protocols
|
2019-04-05 10:40:03 -05:00
|
|
|
header := bc.GetHeaderByNumber(config.ChtSize - 1)
|
2018-02-11 06:57:46 -06:00
|
|
|
rlp, _ := rlp.EncodeToBytes(header)
|
|
|
|
|
|
|
|
key := make([]byte, 8)
|
2019-04-05 10:40:03 -05:00
|
|
|
binary.BigEndian.PutUint64(key, config.ChtSize-1)
|
2018-02-11 06:57:46 -06:00
|
|
|
|
|
|
|
proofsV2 := HelperTrieResps{
|
|
|
|
AuxData: [][]byte{rlp},
|
|
|
|
}
|
2019-04-05 10:40:03 -05:00
|
|
|
root := light.GetChtRoot(server.db, 0, bc.GetHeaderByNumber(config.ChtSize-1).Hash())
|
2022-10-19 02:53:09 -05:00
|
|
|
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.ChtTablePrefix))))
|
2019-04-05 10:01:51 -05:00
|
|
|
trie.Prove(key, 0, &proofsV2.Proofs)
|
2018-02-11 06:57:46 -06:00
|
|
|
// Assemble the requests for the different protocols
|
|
|
|
requestsV2 := []HelperTrieReq{{
|
|
|
|
Type: htCanonical,
|
2019-04-05 10:40:03 -05:00
|
|
|
TrieIdx: 0,
|
2018-02-11 06:57:46 -06:00
|
|
|
Key: key,
|
2021-01-16 12:06:18 -06:00
|
|
|
AuxReq: htAuxHeader,
|
2018-02-11 06:57:46 -06:00
|
|
|
}}
|
|
|
|
// Send the proof request and verify the response
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetHelperTrieProofsMsg, 42, requestsV2)
|
|
|
|
if err := expectResponse(rawPeer.app, HelperTrieProofsMsg, 42, testBufLimit, proofsV2); err != nil {
|
2019-04-05 10:01:51 -05:00
|
|
|
t.Errorf("proofs mismatch: %v", err)
|
2018-02-11 06:57:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
func TestGetBloombitsProofsLes2(t *testing.T) { testGetBloombitsProofs(t, 2) }
|
|
|
|
func TestGetBloombitsProofsLes3(t *testing.T) { testGetBloombitsProofs(t, 3) }
|
2021-02-12 13:48:18 -06:00
|
|
|
func TestGetBloombitsProofsLes4(t *testing.T) { testGetBloombitsProofs(t, 4) }
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2018-02-11 06:57:46 -06:00
|
|
|
// Tests that bloombits proofs can be correctly retrieved.
|
2019-08-21 04:29:34 -05:00
|
|
|
func testGetBloombitsProofs(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
var (
|
|
|
|
config = light.TestServerIndexerConfig
|
|
|
|
waitIndexers = func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
|
|
|
|
for {
|
|
|
|
bts, _, _ := btIndexer.Sections()
|
|
|
|
if bts >= 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-08-28 02:08:16 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig = testnetConfig{
|
|
|
|
blocks: int(config.BloomTrieSize + config.BloomTrieConfirms),
|
|
|
|
protocol: protocol,
|
|
|
|
indexFn: waitIndexers,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 02:08:16 -05:00
|
|
|
defer tearDown()
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
bc := server.handler.blockchain
|
2018-02-11 06:57:46 -06:00
|
|
|
|
|
|
|
// Request and verify each bit of the bloom bits proofs
|
|
|
|
for bit := 0; bit < 2048; bit++ {
|
2018-08-28 02:08:16 -05:00
|
|
|
// Assemble the request and proofs for the bloombits
|
2018-02-11 06:57:46 -06:00
|
|
|
key := make([]byte, 10)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint16(key[:2], uint16(bit))
|
2018-08-28 02:08:16 -05:00
|
|
|
// Only the first bloom section has data.
|
|
|
|
binary.BigEndian.PutUint64(key[2:], 0)
|
2018-02-11 06:57:46 -06:00
|
|
|
|
|
|
|
requests := []HelperTrieReq{{
|
|
|
|
Type: htBloomBits,
|
|
|
|
TrieIdx: 0,
|
|
|
|
Key: key,
|
|
|
|
}}
|
|
|
|
var proofs HelperTrieResps
|
|
|
|
|
2018-08-28 02:08:16 -05:00
|
|
|
root := light.GetBloomTrieRoot(server.db, 0, bc.GetHeaderByNumber(config.BloomTrieSize-1).Hash())
|
2022-10-19 02:53:09 -05:00
|
|
|
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.BloomTrieTablePrefix))))
|
2018-02-11 06:57:46 -06:00
|
|
|
trie.Prove(key, 0, &proofs.Proofs)
|
|
|
|
|
|
|
|
// Send the proof request and verify the response
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetHelperTrieProofsMsg, 42, requests)
|
|
|
|
if err := expectResponse(rawPeer.app, HelperTrieProofsMsg, 42, testBufLimit, proofs); err != nil {
|
2018-02-11 06:57:46 -06:00
|
|
|
t.Errorf("bit %d: proofs mismatch: %v", bit, err)
|
2017-10-24 08:19:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
func TestTransactionStatusLes2(t *testing.T) { testTransactionStatus(t, lpv2) }
|
|
|
|
func TestTransactionStatusLes3(t *testing.T) { testTransactionStatus(t, lpv3) }
|
|
|
|
func TestTransactionStatusLes4(t *testing.T) { testTransactionStatus(t, lpv4) }
|
2019-08-21 04:29:34 -05:00
|
|
|
|
|
|
|
func testTransactionStatus(t *testing.T, protocol int) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
defer tearDown()
|
2021-02-25 07:24:04 -06:00
|
|
|
|
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
server.handler.addTxsSync = true
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
chain := server.handler.blockchain
|
2017-10-24 08:19:09 -05:00
|
|
|
|
|
|
|
var reqID uint64
|
|
|
|
|
2019-05-13 06:41:10 -05:00
|
|
|
test := func(tx *types.Transaction, send bool, expStatus light.TxStatus) {
|
2017-10-24 08:19:09 -05:00
|
|
|
reqID++
|
|
|
|
if send {
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, SendTxV2Msg, reqID, types.Transactions{tx})
|
2017-10-24 08:19:09 -05:00
|
|
|
} else {
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetTxStatusMsg, reqID, []common.Hash{tx.Hash()})
|
2017-10-24 08:19:09 -05:00
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
if err := expectResponse(rawPeer.app, TxStatusMsg, reqID, testBufLimit, []light.TxStatus{expStatus}); err != nil {
|
2017-10-24 08:19:09 -05:00
|
|
|
t.Errorf("transaction status mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
signer := types.HomesteadSigner{}
|
|
|
|
|
|
|
|
// test error status by sending an underpriced transaction
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
tx0, _ := types.SignTx(types.NewTransaction(0, userAddr1, big.NewInt(10000), params.TxGas, nil, nil), signer, bankKey)
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx0, true, light.TxStatus{Status: txpool.TxStatusUnknown, Error: txpool.ErrUnderpriced.Error()})
|
2017-10-24 08:19:09 -05:00
|
|
|
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
tx1, _ := types.SignTx(types.NewTransaction(0, userAddr1, big.NewInt(10000), params.TxGas, big.NewInt(100000000000), nil), signer, bankKey)
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx1, false, light.TxStatus{Status: txpool.TxStatusUnknown}) // query before sending, should be unknown
|
|
|
|
test(tx1, true, light.TxStatus{Status: txpool.TxStatusPending}) // send valid processable tx, should return pending
|
|
|
|
test(tx1, true, light.TxStatus{Status: txpool.TxStatusPending}) // adding it again should not return an error
|
2017-10-24 08:19:09 -05:00
|
|
|
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
tx2, _ := types.SignTx(types.NewTransaction(1, userAddr1, big.NewInt(10000), params.TxGas, big.NewInt(100000000000), nil), signer, bankKey)
|
|
|
|
tx3, _ := types.SignTx(types.NewTransaction(2, userAddr1, big.NewInt(10000), params.TxGas, big.NewInt(100000000000), nil), signer, bankKey)
|
2017-10-24 08:19:09 -05:00
|
|
|
// send transactions in the wrong order, tx3 should be queued
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx3, true, light.TxStatus{Status: txpool.TxStatusQueued})
|
|
|
|
test(tx2, true, light.TxStatus{Status: txpool.TxStatusPending})
|
2017-10-24 08:19:09 -05:00
|
|
|
// query again, now tx3 should be pending too
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx3, false, light.TxStatus{Status: txpool.TxStatusPending})
|
2017-10-24 08:19:09 -05:00
|
|
|
|
|
|
|
// generate and add a block with tx1 and tx2 included
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
gchain, _ := core.GenerateChain(params.TestChainConfig, chain.GetBlockByNumber(0), ethash.NewFaker(), server.db, 1, func(i int, block *core.BlockGen) {
|
2017-10-24 08:19:09 -05:00
|
|
|
block.AddTx(tx1)
|
|
|
|
block.AddTx(tx2)
|
|
|
|
})
|
|
|
|
if _, err := chain.InsertChain(gchain); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-25 04:18:44 -05:00
|
|
|
// wait until TxPool processes the inserted block
|
|
|
|
for i := 0; i < 10; i++ {
|
2019-08-21 04:29:34 -05:00
|
|
|
if pending, _ := server.handler.txpool.Stats(); pending == 1 {
|
2017-10-25 04:18:44 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2019-08-21 04:29:34 -05:00
|
|
|
if pending, _ := server.handler.txpool.Stats(); pending != 1 {
|
2017-10-25 04:18:44 -05:00
|
|
|
t.Fatalf("pending count mismatch: have %d, want 1", pending)
|
|
|
|
}
|
2019-08-21 04:29:34 -05:00
|
|
|
// Discard new block announcement
|
2021-02-25 07:24:04 -06:00
|
|
|
msg, _ := rawPeer.app.ReadMsg()
|
2019-08-21 04:29:34 -05:00
|
|
|
msg.Discard()
|
2017-10-24 08:19:09 -05:00
|
|
|
|
|
|
|
// check if their status is included now
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
block1hash := rawdb.ReadCanonicalHash(server.db, 1)
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx1, false, light.TxStatus{Status: txpool.TxStatusIncluded, Lookup: &rawdb.LegacyTxLookupEntry{BlockHash: block1hash, BlockIndex: 1, Index: 0}})
|
2019-08-21 04:29:34 -05:00
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx2, false, light.TxStatus{Status: txpool.TxStatusIncluded, Lookup: &rawdb.LegacyTxLookupEntry{BlockHash: block1hash, BlockIndex: 1, Index: 1}})
|
2017-10-24 08:19:09 -05:00
|
|
|
|
|
|
|
// create a reorg that rolls them back
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 02:34:02 -05:00
|
|
|
gchain, _ = core.GenerateChain(params.TestChainConfig, chain.GetBlockByNumber(0), ethash.NewFaker(), server.db, 2, func(i int, block *core.BlockGen) {})
|
2017-10-24 08:19:09 -05:00
|
|
|
if _, err := chain.InsertChain(gchain); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// wait until TxPool processes the reorg
|
2017-10-25 04:18:44 -05:00
|
|
|
for i := 0; i < 10; i++ {
|
2019-08-21 04:29:34 -05:00
|
|
|
if pending, _ := server.handler.txpool.Stats(); pending == 3 {
|
2017-10-24 08:19:09 -05:00
|
|
|
break
|
|
|
|
}
|
2017-10-25 04:18:44 -05:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2019-08-21 04:29:34 -05:00
|
|
|
if pending, _ := server.handler.txpool.Stats(); pending != 3 {
|
2017-10-25 04:18:44 -05:00
|
|
|
t.Fatalf("pending count mismatch: have %d, want 3", pending)
|
2016-10-13 22:51:29 -05:00
|
|
|
}
|
2019-08-21 04:29:34 -05:00
|
|
|
// Discard new block announcement
|
2021-02-25 07:24:04 -06:00
|
|
|
msg, _ = rawPeer.app.ReadMsg()
|
2019-08-21 04:29:34 -05:00
|
|
|
msg.Discard()
|
|
|
|
|
2017-10-24 08:19:09 -05:00
|
|
|
// check if their status is pending again
|
2022-10-24 08:13:55 -05:00
|
|
|
test(tx1, false, light.TxStatus{Status: txpool.TxStatusPending})
|
|
|
|
test(tx2, false, light.TxStatus{Status: txpool.TxStatusPending})
|
2016-10-13 22:51:29 -05:00
|
|
|
}
|
2019-05-30 13:51:13 -05:00
|
|
|
|
2021-02-25 07:24:04 -06:00
|
|
|
func TestStopResumeLES3(t *testing.T) { testStopResume(t, lpv3) }
|
|
|
|
func TestStopResumeLES4(t *testing.T) { testStopResume(t, lpv4) }
|
|
|
|
|
|
|
|
func testStopResume(t *testing.T, protocol int) {
|
|
|
|
netconfig := testnetConfig{
|
|
|
|
protocol: protocol,
|
|
|
|
simClock: true,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, _, tearDown := newClientServerEnv(t, netconfig)
|
2019-08-21 04:29:34 -05:00
|
|
|
defer tearDown()
|
2019-05-30 13:51:13 -05:00
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
server.handler.server.costTracker.testing = true
|
2021-02-25 07:24:04 -06:00
|
|
|
server.handler.server.costTracker.testCostList = testCostList(testBufLimit / 10)
|
|
|
|
|
|
|
|
rawPeer, closePeer, _ := server.newRawPeer(t, "peer", protocol)
|
|
|
|
defer closePeer()
|
2019-05-30 13:51:13 -05:00
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
var (
|
|
|
|
reqID uint64
|
|
|
|
expBuf = testBufLimit
|
|
|
|
testCost = testBufLimit / 10
|
|
|
|
)
|
|
|
|
header := server.handler.blockchain.CurrentHeader()
|
2019-05-30 13:51:13 -05:00
|
|
|
req := func() {
|
|
|
|
reqID++
|
2021-02-25 07:24:04 -06:00
|
|
|
sendRequest(rawPeer.app, GetBlockHeadersMsg, reqID, &GetBlockHeadersData{Origin: hashOrNumber{Hash: header.Hash()}, Amount: 1})
|
2019-05-30 13:51:13 -05:00
|
|
|
}
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
// send requests while we still have enough buffer and expect a response
|
|
|
|
for expBuf >= testCost {
|
|
|
|
req()
|
|
|
|
expBuf -= testCost
|
2021-02-25 07:24:04 -06:00
|
|
|
if err := expectResponse(rawPeer.app, BlockHeadersMsg, reqID, expBuf, []*types.Header{header}); err != nil {
|
2019-08-21 04:29:34 -05:00
|
|
|
t.Errorf("expected response and failed: %v", err)
|
2019-05-30 13:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// send some more requests in excess and expect a single StopMsg
|
|
|
|
c := i
|
|
|
|
for c > 0 {
|
|
|
|
req()
|
|
|
|
c--
|
|
|
|
}
|
2021-02-25 07:24:04 -06:00
|
|
|
if err := p2p.ExpectMsg(rawPeer.app, StopMsg, nil); err != nil {
|
2019-05-30 13:51:13 -05:00
|
|
|
t.Errorf("expected StopMsg and failed: %v", err)
|
|
|
|
}
|
|
|
|
// wait until the buffer is recharged by half of the limit
|
|
|
|
wait := testBufLimit / testBufRecharge / 2
|
2019-08-21 04:29:34 -05:00
|
|
|
server.clock.(*mclock.Simulated).Run(time.Millisecond * time.Duration(wait))
|
|
|
|
|
2019-05-30 13:51:13 -05:00
|
|
|
// expect a ResumeMsg with the partially recharged buffer value
|
|
|
|
expBuf += testBufRecharge * wait
|
2021-02-25 07:24:04 -06:00
|
|
|
if err := p2p.ExpectMsg(rawPeer.app, ResumeMsg, expBuf); err != nil {
|
2019-05-30 13:51:13 -05:00
|
|
|
t.Errorf("expected ResumeMsg and failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|