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:47:09 +03:00
|
|
|
package light
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-22 20:20:33 +03:00
|
|
|
"context"
|
2016-10-14 06:47:09 +03:00
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"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"
|
2017-04-05 01:16:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2016-10-14 06:47:09 +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:47:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
|
2021-06-15 13:56:14 +03:00
|
|
|
testBankFunds = big.NewInt(1_000_000_000_000_000_000)
|
2016-10-14 06:47:09 +03:00
|
|
|
|
|
|
|
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
|
|
|
acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
|
|
|
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
|
|
|
|
acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey)
|
|
|
|
|
|
|
|
testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056")
|
|
|
|
testContractAddr common.Address
|
|
|
|
)
|
|
|
|
|
|
|
|
type testOdr struct {
|
|
|
|
OdrBackend
|
2018-08-28 10:08:16 +03:00
|
|
|
indexerConfig *IndexerConfig
|
|
|
|
sdb, ldb ethdb.Database
|
2022-09-07 21:21:59 +03:00
|
|
|
serverState state.Database
|
2018-08-28 10:08:16 +03:00
|
|
|
disable bool
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (odr *testOdr) Database() ethdb.Database {
|
|
|
|
return odr.ldb
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrOdrDisabled = errors.New("ODR disabled")
|
|
|
|
|
|
|
|
func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
|
|
|
|
if odr.disable {
|
|
|
|
return ErrOdrDisabled
|
|
|
|
}
|
|
|
|
switch req := req.(type) {
|
|
|
|
case *BlockRequest:
|
2018-05-07 14:35:06 +03:00
|
|
|
number := rawdb.ReadHeaderNumber(odr.sdb, req.Hash)
|
|
|
|
if number != nil {
|
|
|
|
req.Rlp = rawdb.ReadBodyRLP(odr.sdb, req.Hash, *number)
|
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
case *ReceiptsRequest:
|
2018-05-07 14:35:06 +03:00
|
|
|
number := rawdb.ReadHeaderNumber(odr.sdb, req.Hash)
|
|
|
|
if number != nil {
|
2019-03-27 19:11:24 +03:00
|
|
|
req.Receipts = rawdb.ReadRawReceipts(odr.sdb, req.Hash, *number)
|
2018-05-07 14:35:06 +03:00
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
case *TrieRequest:
|
2022-09-07 21:21:59 +03:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
t state.Trie
|
|
|
|
)
|
|
|
|
if len(req.Id.AccKey) > 0 {
|
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 11:01:02 +03:00
|
|
|
t, err = odr.serverState.OpenStorageTrie(req.Id.StateRoot, common.BytesToHash(req.Id.AccKey), req.Id.Root)
|
2022-09-07 21:21:59 +03:00
|
|
|
} else {
|
|
|
|
t, err = odr.serverState.OpenTrie(req.Id.Root)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
nodes := NewNodeSet()
|
|
|
|
t.Prove(req.Key, 0, nodes)
|
|
|
|
req.Proof = nodes
|
2016-10-14 06:47:09 +03:00
|
|
|
case *CodeRequest:
|
2020-08-21 15:10:40 +03:00
|
|
|
req.Data = rawdb.ReadCode(odr.sdb, req.Hash)
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
req.StoreResult(odr.ldb)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
func (odr *testOdr) IndexerConfig() *IndexerConfig {
|
|
|
|
return odr.indexerConfig
|
|
|
|
}
|
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
type odrTestFn func(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) ([]byte, error)
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2019-04-05 18:01:51 +03:00
|
|
|
func TestOdrGetBlockLes2(t *testing.T) { testChainOdr(t, 1, odrGetBlock) }
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
func odrGetBlock(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) ([]byte, error) {
|
2016-10-14 06:47:09 +03:00
|
|
|
var block *types.Block
|
|
|
|
if bc != nil {
|
|
|
|
block = bc.GetBlockByHash(bhash)
|
|
|
|
} else {
|
|
|
|
block, _ = lc.GetBlockByHash(ctx, bhash)
|
|
|
|
}
|
|
|
|
if block == nil {
|
2017-06-27 16:57:06 +03:00
|
|
|
return nil, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
rlp, _ := rlp.EncodeToBytes(block)
|
2017-06-27 16:57:06 +03:00
|
|
|
return rlp, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:01:51 +03:00
|
|
|
func TestOdrGetReceiptsLes2(t *testing.T) { testChainOdr(t, 1, odrGetReceipts) }
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
func odrGetReceipts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) ([]byte, error) {
|
2016-10-14 06:47:09 +03:00
|
|
|
var receipts types.Receipts
|
|
|
|
if bc != nil {
|
2018-05-07 14:35:06 +03:00
|
|
|
number := rawdb.ReadHeaderNumber(db, bhash)
|
|
|
|
if number != nil {
|
2019-04-15 12:36:27 +03:00
|
|
|
receipts = rawdb.ReadReceipts(db, bhash, *number, bc.Config())
|
2018-05-07 14:35:06 +03:00
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
} else {
|
2018-05-07 14:35:06 +03:00
|
|
|
number := rawdb.ReadHeaderNumber(db, bhash)
|
|
|
|
if number != nil {
|
|
|
|
receipts, _ = GetBlockReceipts(ctx, lc.Odr(), bhash, *number)
|
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
if receipts == nil {
|
2017-06-27 16:57:06 +03:00
|
|
|
return nil, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
rlp, _ := rlp.EncodeToBytes(receipts)
|
2017-06-27 16:57:06 +03:00
|
|
|
return rlp, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:01:51 +03:00
|
|
|
func TestOdrAccountsLes2(t *testing.T) { testChainOdr(t, 1, odrAccounts) }
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
func odrAccounts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) ([]byte, error) {
|
2016-10-14 06:47:09 +03:00
|
|
|
dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678")
|
|
|
|
acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr}
|
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
var st *state.StateDB
|
|
|
|
if bc == nil {
|
|
|
|
header := lc.GetHeaderByHash(bhash)
|
|
|
|
st = NewState(ctx, header, lc.Odr())
|
|
|
|
} else {
|
|
|
|
header := bc.GetHeaderByHash(bhash)
|
2022-09-07 21:21:59 +03:00
|
|
|
st, _ = state.New(header.Root, bc.StateCache(), nil)
|
2017-06-27 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:47:09 +03:00
|
|
|
var res []byte
|
|
|
|
for _, addr := range acc {
|
2017-06-27 16:57:06 +03:00
|
|
|
bal := st.GetBalance(addr)
|
|
|
|
rlp, _ := rlp.EncodeToBytes(bal)
|
|
|
|
res = append(res, rlp...)
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
2017-06-27 16:57:06 +03:00
|
|
|
return res, st.Error()
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:01:51 +03:00
|
|
|
func TestOdrContractCallLes2(t *testing.T) { testChainOdr(t, 1, odrContractCall) }
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) ([]byte, error) {
|
2016-10-14 06:47:09 +03:00
|
|
|
data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000")
|
2016-12-06 04:16:03 +03:00
|
|
|
config := params.TestChainConfig
|
|
|
|
|
2016-10-14 06:47:09 +03:00
|
|
|
var res []byte
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
data[35] = byte(i)
|
2016-12-06 04:16:03 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
var (
|
|
|
|
st *state.StateDB
|
|
|
|
header *types.Header
|
|
|
|
chain core.ChainContext
|
|
|
|
)
|
|
|
|
if bc == nil {
|
|
|
|
chain = lc
|
|
|
|
header = lc.GetHeaderByHash(bhash)
|
|
|
|
st = NewState(ctx, header, lc.Odr())
|
2016-10-14 06:47:09 +03:00
|
|
|
} else {
|
2017-06-27 16:57:06 +03:00
|
|
|
chain = bc
|
|
|
|
header = bc.GetHeaderByHash(bhash)
|
2022-09-07 21:21:59 +03:00
|
|
|
st, _ = state.New(header.Root, bc.StateCache(), nil)
|
2017-06-27 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform read-only call.
|
|
|
|
st.SetBalance(testBankAddress, 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: testBankAddress,
|
|
|
|
To: &testContractAddr,
|
|
|
|
Value: new(big.Int),
|
|
|
|
GasLimit: 1000000,
|
|
|
|
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
|
|
|
txContext := core.NewEVMTxContext(msg)
|
|
|
|
context := core.NewEVMBlockContext(header, chain, nil)
|
2021-06-15 13:56:14 +03:00
|
|
|
vmenv := vm.NewEVM(context, txContext, st, 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)
|
|
|
|
res = append(res, result.Return()...)
|
2017-06-27 16:57:06 +03:00
|
|
|
if st.Error() != nil {
|
|
|
|
return res, st.Error()
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-27 16:57:06 +03:00
|
|
|
return res, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testChainGen(i int, block *core.BlockGen) {
|
2016-11-02 15:44:13 +03:00
|
|
|
signer := types.HomesteadSigner{}
|
2016-10-14 06:47:09 +03:00
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
// In block 1, the test bank sends account #1 some ether.
|
2021-06-15 13:56:14 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10_000_000_000_000_000), params.TxGas, block.BaseFee(), nil), signer, testBankKey)
|
2016-10-14 06:47:09 +03:00
|
|
|
block.AddTx(tx)
|
|
|
|
case 1:
|
|
|
|
// In block 2, the test bank sends some more ether to account #1.
|
|
|
|
// acc1Addr passes it on to account #2.
|
|
|
|
// acc1Addr creates a test contract.
|
2021-06-15 13:56:14 +03:00
|
|
|
tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1_000_000_000_000_000), params.TxGas, block.BaseFee(), nil), signer, testBankKey)
|
2016-10-14 06:47:09 +03:00
|
|
|
nonce := block.TxNonce(acc1Addr)
|
2021-06-15 13:56:14 +03:00
|
|
|
tx2, _ := types.SignTx(types.NewTransaction(nonce, acc2Addr, big.NewInt(1_000_000_000_000_000), params.TxGas, block.BaseFee(), nil), signer, acc1Key)
|
2016-10-14 06:47:09 +03:00
|
|
|
nonce++
|
2021-06-15 13:56:14 +03:00
|
|
|
tx3, _ := types.SignTx(types.NewContractCreation(nonce, big.NewInt(0), 1000000, block.BaseFee(), testContractCode), signer, acc1Key)
|
2016-10-14 06:47:09 +03:00
|
|
|
testContractAddr = crypto.CreateAddress(acc1Addr, nonce)
|
|
|
|
block.AddTx(tx1)
|
|
|
|
block.AddTx(tx2)
|
|
|
|
block.AddTx(tx3)
|
|
|
|
case 2:
|
|
|
|
// Block 3 is empty but was mined by account #2.
|
|
|
|
block.SetCoinbase(acc2Addr)
|
|
|
|
block.SetExtra([]byte("yeehaw"))
|
|
|
|
data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001")
|
2021-06-15 13:56:14 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), 100000, block.BaseFee(), data), signer, testBankKey)
|
2016-10-14 06:47:09 +03:00
|
|
|
block.AddTx(tx)
|
|
|
|
case 3:
|
|
|
|
// Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
|
|
|
|
b2 := block.PrevBlock(1).Header()
|
|
|
|
b2.Extra = []byte("foo")
|
|
|
|
block.AddUncle(b2)
|
|
|
|
b3 := block.PrevBlock(2).Header()
|
|
|
|
b3.Extra = []byte("foo")
|
|
|
|
block.AddUncle(b3)
|
|
|
|
data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002")
|
2021-06-15 13:56:14 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), 100000, block.BaseFee(), data), signer, testBankKey)
|
2016-10-14 06:47:09 +03:00
|
|
|
block.AddTx(tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
func testChainOdr(t *testing.T, protocol int, fn odrTestFn) {
|
2016-10-14 06:47:09 +03:00
|
|
|
var (
|
2021-06-15 13:56:14 +03:00
|
|
|
sdb = rawdb.NewMemoryDatabase()
|
|
|
|
ldb = rawdb.NewMemoryDatabase()
|
2022-08-30 19:22:28 +03:00
|
|
|
gspec = &core.Genesis{
|
|
|
|
Config: params.TestChainConfig,
|
2021-06-15 13:56:14 +03:00
|
|
|
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
|
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
)
|
|
|
|
// Assemble the test environment
|
2022-08-30 19:22:28 +03:00
|
|
|
blockchain, _ := core.NewBlockChain(sdb, nil, gspec, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil)
|
2022-09-07 21:21:59 +03:00
|
|
|
_, gchain, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), 4, testChainGen)
|
2016-10-14 06:47:09 +03:00
|
|
|
if _, err := blockchain.InsertChain(gchain); err != nil {
|
2017-06-27 16:57:06 +03:00
|
|
|
t.Fatal(err)
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 21:21:59 +03:00
|
|
|
gspec.MustCommit(ldb)
|
|
|
|
odr := &testOdr{sdb: sdb, ldb: ldb, serverState: blockchain.StateCache(), indexerConfig: TestClientIndexerConfig}
|
|
|
|
lightchain, err := NewLightChain(odr, gspec.Config, ethash.NewFullFaker(), nil)
|
2017-06-27 16:57:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
headers := make([]*types.Header, len(gchain))
|
|
|
|
for i, block := range gchain {
|
|
|
|
headers[i] = block.Header()
|
|
|
|
}
|
|
|
|
if _, err := lightchain.InsertHeaderChain(headers, 1); err != nil {
|
2017-06-27 16:57:06 +03:00
|
|
|
t.Fatal(err)
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
test := func(expFail int) {
|
2016-11-09 03:20:49 +03:00
|
|
|
for i := uint64(0); i <= blockchain.CurrentHeader().Number.Uint64(); i++ {
|
2018-05-07 14:35:06 +03:00
|
|
|
bhash := rawdb.ReadCanonicalHash(sdb, i)
|
2017-06-27 16:57:06 +03:00
|
|
|
b1, err := fn(NoOdr, sdb, blockchain, nil, bhash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in full-node test for block %d: %v", i, err)
|
|
|
|
}
|
2017-03-22 20:20:33 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
|
|
|
defer cancel()
|
2017-06-27 16:57:06 +03:00
|
|
|
|
|
|
|
exp := i < uint64(expFail)
|
|
|
|
b2, err := fn(ctx, ldb, nil, lightchain, bhash)
|
|
|
|
if err != nil && exp {
|
|
|
|
t.Errorf("error in ODR test for block %d: %v", i, err)
|
|
|
|
}
|
2017-03-22 20:20:33 +03:00
|
|
|
|
2016-10-14 06:47:09 +03:00
|
|
|
eq := bytes.Equal(b1, b2)
|
|
|
|
if exp && !eq {
|
2017-06-27 16:57:06 +03:00
|
|
|
t.Errorf("ODR test output for block %d doesn't match full node", i)
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expect retrievals to fail (except genesis block) without a les peer
|
2017-06-27 16:57:06 +03:00
|
|
|
t.Log("checking without ODR")
|
2016-10-14 06:47:09 +03:00
|
|
|
odr.disable = true
|
2017-06-27 16:57:06 +03:00
|
|
|
test(1)
|
|
|
|
|
|
|
|
// expect all retrievals to pass with ODR enabled
|
|
|
|
t.Log("checking with ODR")
|
|
|
|
odr.disable = false
|
|
|
|
test(len(gchain))
|
|
|
|
|
2016-10-14 06:47:09 +03:00
|
|
|
// still expect all retrievals to pass, now data should be cached locally
|
2017-06-27 16:57:06 +03:00
|
|
|
t.Log("checking without ODR, should be cached")
|
|
|
|
odr.disable = true
|
|
|
|
test(len(gchain))
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|