2016-11-09 04:01:56 +03: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-14 06:51:29 +03:00
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-22 20:20:33 +03:00
|
|
|
"context"
|
2021-02-25 16:24:04 +03:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2016-10-14 06:51:29 +03:00
|
|
|
"math/big"
|
2021-02-25 16:24:04 +03:00
|
|
|
"reflect"
|
2016-10-14 06:51:29 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 14:35:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2022-10-24 16:13:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-10-20 14:36:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/light"
|
2016-10-20 14:36:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
type odrTestFn func(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte
|
2016-10-14 06:51:29 +03:00
|
|
|
|
2019-05-13 14:41:10 +03:00
|
|
|
func TestOdrGetBlockLes2(t *testing.T) { testOdr(t, 2, 1, true, odrGetBlock) }
|
2019-08-21 12:29:34 +03:00
|
|
|
func TestOdrGetBlockLes3(t *testing.T) { testOdr(t, 3, 1, true, odrGetBlock) }
|
2021-02-12 22:48:18 +03:00
|
|
|
func TestOdrGetBlockLes4(t *testing.T) { testOdr(t, 4, 1, true, odrGetBlock) }
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
func odrGetBlock(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
|
2016-10-14 06:51:29 +03:00
|
|
|
var block *types.Block
|
|
|
|
if bc != nil {
|
|
|
|
block = bc.GetBlockByHash(bhash)
|
|
|
|
} else {
|
|
|
|
block, _ = lc.GetBlockByHash(ctx, bhash)
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rlp, _ := rlp.EncodeToBytes(block)
|
|
|
|
return rlp
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:41:10 +03:00
|
|
|
func TestOdrGetReceiptsLes2(t *testing.T) { testOdr(t, 2, 1, true, odrGetReceipts) }
|
2019-08-21 12:29:34 +03:00
|
|
|
func TestOdrGetReceiptsLes3(t *testing.T) { testOdr(t, 3, 1, true, odrGetReceipts) }
|
2021-02-12 22:48:18 +03:00
|
|
|
func TestOdrGetReceiptsLes4(t *testing.T) { testOdr(t, 4, 1, true, odrGetReceipts) }
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
func odrGetReceipts(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
|
2016-10-14 06:51:29 +03:00
|
|
|
var receipts types.Receipts
|
|
|
|
if bc != nil {
|
2018-05-07 14:35:06 +03:00
|
|
|
if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
|
2019-04-15 12:36:27 +03:00
|
|
|
receipts = rawdb.ReadReceipts(db, bhash, *number, config)
|
2018-05-07 14:35:06 +03:00
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
} else {
|
2018-05-07 14:35:06 +03:00
|
|
|
if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
|
|
|
|
receipts, _ = light.GetBlockReceipts(ctx, lc.Odr(), bhash, *number)
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
if receipts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rlp, _ := rlp.EncodeToBytes(receipts)
|
|
|
|
return rlp
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:41:10 +03:00
|
|
|
func TestOdrAccountsLes2(t *testing.T) { testOdr(t, 2, 1, true, odrAccounts) }
|
2019-08-21 12:29:34 +03:00
|
|
|
func TestOdrAccountsLes3(t *testing.T) { testOdr(t, 3, 1, true, odrAccounts) }
|
2021-02-12 22:48:18 +03:00
|
|
|
func TestOdrAccountsLes4(t *testing.T) { testOdr(t, 4, 1, true, odrAccounts) }
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
func odrAccounts(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
|
2016-10-14 06:51:29 +03:00
|
|
|
dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678")
|
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 10:34:02 +03:00
|
|
|
acc := []common.Address{bankAddr, userAddr1, userAddr2, dummyAddr}
|
2016-10-14 06:51:29 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
var (
|
|
|
|
res []byte
|
|
|
|
st *state.StateDB
|
|
|
|
err error
|
|
|
|
)
|
2016-10-14 06:51:29 +03:00
|
|
|
for _, addr := range acc {
|
|
|
|
if bc != nil {
|
|
|
|
header := bc.GetHeaderByHash(bhash)
|
2020-02-24 14:26:34 +03:00
|
|
|
st, err = state.New(header.Root, state.NewDatabase(db), nil)
|
2016-10-14 06:51:29 +03:00
|
|
|
} else {
|
|
|
|
header := lc.GetHeaderByHash(bhash)
|
2017-06-27 16:57:06 +03:00
|
|
|
st = light.NewState(ctx, header, lc.Odr())
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
bal := st.GetBalance(addr)
|
|
|
|
rlp, _ := rlp.EncodeToBytes(bal)
|
|
|
|
res = append(res, rlp...)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:41:10 +03:00
|
|
|
func TestOdrContractCallLes2(t *testing.T) { testOdr(t, 2, 2, true, odrContractCall) }
|
2019-08-21 12:29:34 +03:00
|
|
|
func TestOdrContractCallLes3(t *testing.T) { testOdr(t, 3, 2, true, odrContractCall) }
|
2021-02-12 22:48:18 +03:00
|
|
|
func TestOdrContractCallLes4(t *testing.T) { testOdr(t, 4, 2, true, odrContractCall) }
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
func odrContractCall(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
|
2016-10-14 06:51:29 +03:00
|
|
|
data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000")
|
|
|
|
|
|
|
|
var res []byte
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
data[35] = byte(i)
|
|
|
|
if bc != nil {
|
|
|
|
header := bc.GetHeaderByHash(bhash)
|
2022-09-07 21:21:59 +03:00
|
|
|
statedb, err := state.New(header.Root, bc.StateCache(), nil)
|
2016-12-06 04:16:03 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
if err == nil {
|
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 10:34:02 +03:00
|
|
|
from := statedb.GetOrNewStateObject(bankAddr)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
from.SetBalance(math.MaxBig256)
|
2016-10-14 06:51:29 +03:00
|
|
|
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 16:19:12 +03:00
|
|
|
msg := &core.Message{
|
|
|
|
From: from.Address(),
|
|
|
|
To: &testContractAddr,
|
|
|
|
Value: new(big.Int),
|
|
|
|
GasLimit: 100000,
|
|
|
|
GasPrice: big.NewInt(params.InitialBaseFee),
|
|
|
|
GasFeeCap: big.NewInt(params.InitialBaseFee),
|
|
|
|
GasTipCap: new(big.Int),
|
|
|
|
Data: data,
|
|
|
|
SkipAccountChecks: true,
|
|
|
|
}
|
2016-12-06 04:16:03 +03:00
|
|
|
|
2020-11-13 15:42:19 +03:00
|
|
|
context := core.NewEVMBlockContext(header, bc, nil)
|
|
|
|
txContext := core.NewEVMTxContext(msg)
|
2021-06-15 13:56:14 +03:00
|
|
|
vmenv := vm.NewEVM(context, txContext, statedb, config, vm.Config{NoBaseFee: true})
|
2016-12-06 04:16:03 +03:00
|
|
|
|
|
|
|
//vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{})
|
2017-11-13 14:47:27 +03:00
|
|
|
gp := new(core.GasPool).AddGas(math.MaxUint64)
|
2020-04-22 11:25:36 +03:00
|
|
|
result, _ := core.ApplyMessage(vmenv, msg, gp)
|
|
|
|
res = append(res, result.Return()...)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
header := lc.GetHeaderByHash(bhash)
|
2017-06-27 16:57:06 +03:00
|
|
|
state := light.NewState(ctx, header, lc.Odr())
|
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 10:34:02 +03:00
|
|
|
state.SetBalance(bankAddr, math.MaxBig256)
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 16:19:12 +03:00
|
|
|
msg := &core.Message{
|
|
|
|
From: bankAddr,
|
|
|
|
To: &testContractAddr,
|
|
|
|
Value: new(big.Int),
|
|
|
|
GasLimit: 100000,
|
|
|
|
GasPrice: big.NewInt(params.InitialBaseFee),
|
|
|
|
GasFeeCap: big.NewInt(params.InitialBaseFee),
|
|
|
|
GasTipCap: new(big.Int),
|
|
|
|
Data: data,
|
|
|
|
SkipAccountChecks: true,
|
|
|
|
}
|
2020-11-13 15:42:19 +03:00
|
|
|
context := core.NewEVMBlockContext(header, lc, nil)
|
|
|
|
txContext := core.NewEVMTxContext(msg)
|
2021-06-15 13:56:14 +03:00
|
|
|
vmenv := vm.NewEVM(context, txContext, state, config, vm.Config{NoBaseFee: true})
|
2017-11-13 14:47:27 +03:00
|
|
|
gp := new(core.GasPool).AddGas(math.MaxUint64)
|
2020-04-22 11:25:36 +03:00
|
|
|
result, _ := core.ApplyMessage(vmenv, msg, gp)
|
2017-06-27 16:57:06 +03:00
|
|
|
if state.Error() == nil {
|
2020-04-22 11:25:36 +03:00
|
|
|
res = append(res, result.Return()...)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:41:10 +03:00
|
|
|
func TestOdrTxStatusLes2(t *testing.T) { testOdr(t, 2, 1, false, odrTxStatus) }
|
2019-08-21 12:29:34 +03:00
|
|
|
func TestOdrTxStatusLes3(t *testing.T) { testOdr(t, 3, 1, false, odrTxStatus) }
|
2021-02-12 22:48:18 +03:00
|
|
|
func TestOdrTxStatusLes4(t *testing.T) { testOdr(t, 4, 1, false, odrTxStatus) }
|
2019-05-13 14:41:10 +03:00
|
|
|
|
|
|
|
func odrTxStatus(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
|
|
|
|
var txs types.Transactions
|
|
|
|
if bc != nil {
|
|
|
|
block := bc.GetBlockByHash(bhash)
|
|
|
|
txs = block.Transactions()
|
|
|
|
} else {
|
|
|
|
if block, _ := lc.GetBlockByHash(ctx, bhash); block != nil {
|
|
|
|
btxs := block.Transactions()
|
|
|
|
txs = make(types.Transactions, len(btxs))
|
|
|
|
for i, tx := range btxs {
|
|
|
|
var err error
|
|
|
|
txs[i], _, _, _, err = light.GetTransaction(ctx, lc.Odr(), tx.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rlp, _ := rlp.EncodeToBytes(txs)
|
|
|
|
return rlp
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// testOdr tests odr requests whose validation guaranteed by block headers.
|
2019-05-13 14:41:10 +03:00
|
|
|
func testOdr(t *testing.T, protocol int, expFail uint64, checkCached bool, fn odrTestFn) {
|
2016-10-14 06:51:29 +03:00
|
|
|
// Assemble the test environment
|
2021-02-25 16:24:04 +03:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: 4,
|
|
|
|
protocol: protocol,
|
|
|
|
connect: true,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
server, client, tearDown := newClientServerEnv(t, netconfig)
|
2018-08-28 10:08:16 +03:00
|
|
|
defer tearDown()
|
2019-08-21 12:29:34 +03:00
|
|
|
|
2019-11-02 15:02:35 +03:00
|
|
|
// Ensure the client has synced all necessary data.
|
|
|
|
clientHead := client.handler.backend.blockchain.CurrentHeader()
|
|
|
|
if clientHead.Number.Uint64() != 4 {
|
|
|
|
t.Fatalf("Failed to sync the chain with server, head: %v", clientHead.Number.Uint64())
|
|
|
|
}
|
2019-11-07 00:09:37 +03:00
|
|
|
// Disable the mechanism that we will wait a few time for request
|
|
|
|
// even there is no suitable peer to send right now.
|
|
|
|
waitForPeers = 0
|
2019-11-02 15:02:35 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
test := func(expFail uint64) {
|
2019-04-15 12:36:27 +03:00
|
|
|
// Mark this as a helper to put the failures at the correct lines
|
|
|
|
t.Helper()
|
|
|
|
|
2019-08-21 12:29:34 +03:00
|
|
|
for i := uint64(0); i <= server.handler.blockchain.CurrentHeader().Number.Uint64(); i++ {
|
2018-08-28 10:08:16 +03:00
|
|
|
bhash := rawdb.ReadCanonicalHash(server.db, i)
|
2019-08-21 12:29:34 +03:00
|
|
|
b1 := fn(light.NoOdr, server.db, server.handler.server.chainConfig, server.handler.blockchain, nil, bhash)
|
2017-03-22 20:20:33 +03:00
|
|
|
|
2019-11-07 00:09:37 +03:00
|
|
|
// Set the timeout as 1 second here, ensure there is enough time
|
|
|
|
// for travis to make the action.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
2019-08-21 12:29:34 +03:00
|
|
|
b2 := fn(ctx, client.db, client.handler.backend.chainConfig, nil, client.handler.backend.blockchain, bhash)
|
|
|
|
cancel()
|
2017-03-22 20:20:33 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
eq := bytes.Equal(b1, b2)
|
|
|
|
exp := i < expFail
|
|
|
|
if exp && !eq {
|
2019-04-15 12:36:27 +03:00
|
|
|
t.Fatalf("odr mismatch: have %x, want %x", b2, b1)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
if !exp && eq {
|
2019-04-15 12:36:27 +03:00
|
|
|
t.Fatalf("unexpected odr match")
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 12:29:34 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
// expect retrievals to fail (except genesis block) without a les peer
|
2019-08-21 12:29:34 +03:00
|
|
|
client.handler.backend.peers.lock.Lock()
|
2020-07-28 18:02:35 +03:00
|
|
|
client.peer.speer.hasBlockHook = func(common.Hash, uint64, bool) bool { return false }
|
2019-08-21 12:29:34 +03:00
|
|
|
client.handler.backend.peers.lock.Unlock()
|
2016-10-14 06:51:29 +03:00
|
|
|
test(expFail)
|
2019-04-15 12:36:27 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
// expect all retrievals to pass
|
2019-08-21 12:29:34 +03:00
|
|
|
client.handler.backend.peers.lock.Lock()
|
2020-07-28 18:02:35 +03:00
|
|
|
client.peer.speer.hasBlockHook = func(common.Hash, uint64, bool) bool { return true }
|
2019-08-21 12:29:34 +03:00
|
|
|
client.handler.backend.peers.lock.Unlock()
|
2016-10-14 06:51:29 +03:00
|
|
|
test(5)
|
2019-08-21 12:29:34 +03:00
|
|
|
|
|
|
|
// still expect all retrievals to pass, now data should be cached locally
|
2019-05-13 14:41:10 +03:00
|
|
|
if checkCached {
|
2020-02-26 12:41:24 +03:00
|
|
|
client.handler.backend.peers.unregister(client.peer.speer.id)
|
2019-05-13 14:41:10 +03:00
|
|
|
time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
|
|
|
|
test(5)
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
2021-02-25 16:24:04 +03:00
|
|
|
|
|
|
|
func TestGetTxStatusFromUnindexedPeersLES4(t *testing.T) { testGetTxStatusFromUnindexedPeers(t, lpv4) }
|
|
|
|
|
|
|
|
func testGetTxStatusFromUnindexedPeers(t *testing.T, protocol int) {
|
|
|
|
var (
|
|
|
|
blocks = 8
|
|
|
|
netconfig = testnetConfig{
|
|
|
|
blocks: blocks,
|
|
|
|
protocol: protocol,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
server, client, tearDown := newClientServerEnv(t, netconfig)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// Iterate the chain, create the tx indexes locally
|
|
|
|
var (
|
|
|
|
testHash common.Hash
|
|
|
|
testStatus light.TxStatus
|
|
|
|
|
|
|
|
txs = make(map[common.Hash]*types.Transaction) // Transaction objects set
|
|
|
|
blockNumbers = make(map[common.Hash]uint64) // Transaction hash to block number mappings
|
|
|
|
blockHashes = make(map[common.Hash]common.Hash) // Transaction hash to block hash mappings
|
|
|
|
intraIndex = make(map[common.Hash]uint64) // Transaction intra-index in block
|
|
|
|
)
|
2023-03-02 09:29:15 +03:00
|
|
|
for number := uint64(1); number < server.backend.Blockchain().CurrentBlock().Number.Uint64(); number++ {
|
2021-02-25 16:24:04 +03:00
|
|
|
block := server.backend.Blockchain().GetBlockByNumber(number)
|
|
|
|
if block == nil {
|
|
|
|
t.Fatalf("Failed to retrieve block %d", number)
|
|
|
|
}
|
|
|
|
for index, tx := range block.Transactions() {
|
|
|
|
txs[tx.Hash()] = tx
|
|
|
|
blockNumbers[tx.Hash()] = number
|
|
|
|
blockHashes[tx.Hash()] = block.Hash()
|
|
|
|
intraIndex[tx.Hash()] = uint64(index)
|
|
|
|
|
|
|
|
if testHash == (common.Hash{}) {
|
|
|
|
testHash = tx.Hash()
|
|
|
|
testStatus = light.TxStatus{
|
2022-10-24 16:13:55 +03:00
|
|
|
Status: txpool.TxStatusIncluded,
|
2021-02-25 16:24:04 +03:00
|
|
|
Lookup: &rawdb.LegacyTxLookupEntry{
|
|
|
|
BlockHash: block.Hash(),
|
|
|
|
BlockIndex: block.NumberU64(),
|
|
|
|
Index: uint64(index),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// serveMsg processes incoming GetTxStatusMsg and sends the response back.
|
|
|
|
serveMsg := func(peer *testPeer, txLookup uint64) error {
|
|
|
|
msg, err := peer.app.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if msg.Code != GetTxStatusMsg {
|
|
|
|
return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, GetTxStatusMsg)
|
|
|
|
}
|
|
|
|
var r GetTxStatusPacket
|
|
|
|
if err := msg.Decode(&r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats := make([]light.TxStatus, len(r.Hashes))
|
|
|
|
for i, hash := range r.Hashes {
|
|
|
|
number, exist := blockNumbers[hash]
|
|
|
|
if !exist {
|
|
|
|
continue // Filter out unknown transactions
|
|
|
|
}
|
|
|
|
min := uint64(blocks) - txLookup
|
|
|
|
if txLookup != txIndexUnlimited && (txLookup == txIndexDisabled || number < min) {
|
|
|
|
continue // Filter out unindexed transactions
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
stats[i].Status = txpool.TxStatusIncluded
|
2021-02-25 16:24:04 +03:00
|
|
|
stats[i].Lookup = &rawdb.LegacyTxLookupEntry{
|
|
|
|
BlockHash: blockHashes[hash],
|
|
|
|
BlockIndex: number,
|
|
|
|
Index: intraIndex[hash],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data, _ := rlp.EncodeToBytes(stats)
|
|
|
|
reply := &reply{peer.app, TxStatusMsg, r.ReqID, data}
|
|
|
|
reply.send(testBufLimit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testspecs = []struct {
|
|
|
|
peers int
|
|
|
|
txLookups []uint64
|
|
|
|
txs []common.Hash
|
|
|
|
results []light.TxStatus
|
|
|
|
}{
|
|
|
|
// Retrieve mined transaction from the empty peerset
|
|
|
|
{
|
|
|
|
peers: 0,
|
|
|
|
txLookups: []uint64{},
|
|
|
|
txs: []common.Hash{testHash},
|
|
|
|
results: []light.TxStatus{{}},
|
|
|
|
},
|
|
|
|
// Retrieve unknown transaction from the full peers
|
|
|
|
{
|
|
|
|
peers: 3,
|
|
|
|
txLookups: []uint64{txIndexUnlimited, txIndexUnlimited, txIndexUnlimited},
|
|
|
|
txs: []common.Hash{randomHash()},
|
|
|
|
results: []light.TxStatus{{}},
|
|
|
|
},
|
|
|
|
// Retrieve mined transaction from the full peers
|
|
|
|
{
|
|
|
|
peers: 3,
|
|
|
|
txLookups: []uint64{txIndexUnlimited, txIndexUnlimited, txIndexUnlimited},
|
|
|
|
txs: []common.Hash{testHash},
|
|
|
|
results: []light.TxStatus{testStatus},
|
|
|
|
},
|
|
|
|
// Retrieve mixed transactions from the full peers
|
|
|
|
{
|
|
|
|
peers: 3,
|
|
|
|
txLookups: []uint64{txIndexUnlimited, txIndexUnlimited, txIndexUnlimited},
|
|
|
|
txs: []common.Hash{randomHash(), testHash},
|
|
|
|
results: []light.TxStatus{{}, testStatus},
|
|
|
|
},
|
|
|
|
// Retrieve mixed transactions from unindexed peer(but the target is still available)
|
|
|
|
{
|
|
|
|
peers: 3,
|
|
|
|
txLookups: []uint64{uint64(blocks) - testStatus.Lookup.BlockIndex, uint64(blocks) - testStatus.Lookup.BlockIndex - 1, uint64(blocks) - testStatus.Lookup.BlockIndex - 2},
|
|
|
|
txs: []common.Hash{randomHash(), testHash},
|
|
|
|
results: []light.TxStatus{{}, testStatus},
|
|
|
|
},
|
|
|
|
// Retrieve mixed transactions from unindexed peer(but the target is not available)
|
|
|
|
{
|
|
|
|
peers: 3,
|
|
|
|
txLookups: []uint64{uint64(blocks) - testStatus.Lookup.BlockIndex - 1, uint64(blocks) - testStatus.Lookup.BlockIndex - 1, uint64(blocks) - testStatus.Lookup.BlockIndex - 2},
|
|
|
|
txs: []common.Hash{randomHash(), testHash},
|
|
|
|
results: []light.TxStatus{{}, {}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testspec := range testspecs {
|
|
|
|
// Create a bunch of server peers with different tx history
|
|
|
|
var (
|
2022-06-13 17:24:45 +03:00
|
|
|
closeFns []func()
|
2021-02-25 16:24:04 +03:00
|
|
|
)
|
|
|
|
for i := 0; i < testspec.peers; i++ {
|
|
|
|
peer, closePeer, _ := client.newRawPeer(t, fmt.Sprintf("server-%d", i), protocol, testspec.txLookups[i])
|
|
|
|
closeFns = append(closeFns, closePeer)
|
|
|
|
|
|
|
|
// Create a one-time routine for serving message
|
2021-08-25 12:56:25 +03:00
|
|
|
go func(i int, peer *testPeer, lookup uint64) {
|
|
|
|
serveMsg(peer, lookup)
|
|
|
|
}(i, peer, testspec.txLookups[i])
|
2021-02-25 16:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send out the GetTxStatus requests, compare the result with
|
|
|
|
// expected value.
|
|
|
|
r := &light.TxStatusRequest{Hashes: testspec.txs}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err := client.handler.backend.odr.RetrieveTxStatus(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to retrieve tx status %v", err)
|
|
|
|
} else {
|
|
|
|
if !reflect.DeepEqual(testspec.results, r.Status) {
|
|
|
|
t.Errorf("Result mismatch, diff")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close all connected peers and start the next round
|
|
|
|
for _, closeFn := range closeFns {
|
|
|
|
closeFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomHash generates a random blob of data and returns it as a hash.
|
|
|
|
func randomHash() common.Hash {
|
|
|
|
var hash common.Hash
|
|
|
|
if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|