2016-11-09 01:01:56 +00: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 03:51:29 +00:00
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-22 17:20:33 +00:00
|
|
|
"context"
|
2016-10-14 03:51:29 +00:00
|
|
|
"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-26 21:21:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-10-20 11:36:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/light"
|
2016-10-20 11:36:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2016-10-20 11:36:29 +00: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 03:51:29 +00:00
|
|
|
|
|
|
|
func TestOdrGetBlockLes1(t *testing.T) { testOdr(t, 1, 1, odrGetBlock) }
|
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
func TestOdrGetBlockLes2(t *testing.T) { testOdr(t, 2, 1, odrGetBlock) }
|
|
|
|
|
2016-10-20 11:36:29 +00: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 03:51:29 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOdrGetReceiptsLes1(t *testing.T) { testOdr(t, 1, 1, odrGetReceipts) }
|
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
func TestOdrGetReceiptsLes2(t *testing.T) { testOdr(t, 2, 1, odrGetReceipts) }
|
|
|
|
|
2016-10-20 11:36:29 +00: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 03:51:29 +00:00
|
|
|
var receipts types.Receipts
|
|
|
|
if bc != nil {
|
2018-05-07 11:35:06 +00:00
|
|
|
if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
|
|
|
|
receipts = rawdb.ReadReceipts(db, bhash, *number)
|
|
|
|
}
|
2016-10-14 03:51:29 +00:00
|
|
|
} else {
|
2018-05-07 11:35:06 +00:00
|
|
|
if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
|
|
|
|
receipts, _ = light.GetBlockReceipts(ctx, lc.Odr(), bhash, *number)
|
|
|
|
}
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
if receipts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rlp, _ := rlp.EncodeToBytes(receipts)
|
|
|
|
return rlp
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOdrAccountsLes1(t *testing.T) { testOdr(t, 1, 1, odrAccounts) }
|
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
func TestOdrAccountsLes2(t *testing.T) { testOdr(t, 2, 1, odrAccounts) }
|
|
|
|
|
2016-10-20 11:36:29 +00: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 03:51:29 +00:00
|
|
|
dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678")
|
|
|
|
acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr}
|
|
|
|
|
2017-06-27 13:57:06 +00:00
|
|
|
var (
|
|
|
|
res []byte
|
|
|
|
st *state.StateDB
|
|
|
|
err error
|
|
|
|
)
|
2016-10-14 03:51:29 +00:00
|
|
|
for _, addr := range acc {
|
|
|
|
if bc != nil {
|
|
|
|
header := bc.GetHeaderByHash(bhash)
|
2017-06-27 13:57:06 +00:00
|
|
|
st, err = state.New(header.Root, state.NewDatabase(db))
|
2016-10-14 03:51:29 +00:00
|
|
|
} else {
|
|
|
|
header := lc.GetHeaderByHash(bhash)
|
2017-06-27 13:57:06 +00: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 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOdrContractCallLes1(t *testing.T) { testOdr(t, 1, 2, odrContractCall) }
|
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
func TestOdrContractCallLes2(t *testing.T) { testOdr(t, 2, 2, odrContractCall) }
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
type callmsg struct {
|
|
|
|
types.Message
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
func (callmsg) CheckNonce() bool { return false }
|
2016-10-14 03:51:29 +00:00
|
|
|
|
2016-10-20 11:36:29 +00: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 03:51:29 +00: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)
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, err := state.New(header.Root, state.NewDatabase(db))
|
2016-12-06 01:16:03 +00:00
|
|
|
|
2016-10-14 03:51:29 +00:00
|
|
|
if err == nil {
|
|
|
|
from := statedb.GetOrNewStateObject(testBankAddress)
|
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-26 21:21:51 +00:00
|
|
|
from.SetBalance(math.MaxBig256)
|
2016-10-14 03:51:29 +00:00
|
|
|
|
2017-11-13 11:47:27 +00:00
|
|
|
msg := callmsg{types.NewMessage(from.Address(), &testContractAddr, 0, new(big.Int), 100000, new(big.Int), data, false)}
|
2016-12-06 01:16:03 +00:00
|
|
|
|
2017-04-12 13:38:31 +00:00
|
|
|
context := core.NewEVMContext(msg, header, bc, nil)
|
2017-01-05 10:52:10 +00:00
|
|
|
vmenv := vm.NewEVM(context, statedb, config, vm.Config{})
|
2016-12-06 01:16:03 +00:00
|
|
|
|
|
|
|
//vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{})
|
2017-11-13 11:47:27 +00:00
|
|
|
gp := new(core.GasPool).AddGas(math.MaxUint64)
|
2017-08-21 00:47:15 +00:00
|
|
|
ret, _, _, _ := core.ApplyMessage(vmenv, msg, gp)
|
2016-10-14 03:51:29 +00:00
|
|
|
res = append(res, ret...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
header := lc.GetHeaderByHash(bhash)
|
2017-06-27 13:57:06 +00:00
|
|
|
state := light.NewState(ctx, header, lc.Odr())
|
|
|
|
state.SetBalance(testBankAddress, math.MaxBig256)
|
2017-11-13 11:47:27 +00:00
|
|
|
msg := callmsg{types.NewMessage(testBankAddress, &testContractAddr, 0, new(big.Int), 100000, new(big.Int), data, false)}
|
2017-06-27 13:57:06 +00:00
|
|
|
context := core.NewEVMContext(msg, header, lc, nil)
|
|
|
|
vmenv := vm.NewEVM(context, state, config, vm.Config{})
|
2017-11-13 11:47:27 +00:00
|
|
|
gp := new(core.GasPool).AddGas(math.MaxUint64)
|
2017-08-21 00:47:15 +00:00
|
|
|
ret, _, _, _ := core.ApplyMessage(vmenv, msg, gp)
|
2017-06-27 13:57:06 +00:00
|
|
|
if state.Error() == nil {
|
|
|
|
res = append(res, ret...)
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-08-28 07:08:16 +00:00
|
|
|
// testOdr tests odr requests whose validation guaranteed by block headers.
|
2016-10-14 03:51:29 +00:00
|
|
|
func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) {
|
|
|
|
// Assemble the test environment
|
2018-08-28 07:08:16 +00:00
|
|
|
server, client, tearDown := newClientServerEnv(t, 4, protocol, nil, true)
|
|
|
|
defer tearDown()
|
|
|
|
client.pm.synchronise(client.rPeer)
|
2016-10-14 03:51:29 +00:00
|
|
|
|
|
|
|
test := func(expFail uint64) {
|
2018-08-28 07:08:16 +00:00
|
|
|
for i := uint64(0); i <= server.pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
|
|
|
|
bhash := rawdb.ReadCanonicalHash(server.db, i)
|
|
|
|
b1 := fn(light.NoOdr, server.db, server.pm.chainConfig, server.pm.blockchain.(*core.BlockChain), nil, bhash)
|
2017-03-22 17:20:33 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
|
|
|
defer cancel()
|
2018-08-28 07:08:16 +00:00
|
|
|
b2 := fn(ctx, client.db, client.pm.chainConfig, nil, client.pm.blockchain.(*light.LightChain), bhash)
|
2017-03-22 17:20:33 +00:00
|
|
|
|
2016-10-14 03:51:29 +00:00
|
|
|
eq := bytes.Equal(b1, b2)
|
|
|
|
exp := i < expFail
|
|
|
|
if exp && !eq {
|
|
|
|
t.Errorf("odr mismatch")
|
|
|
|
}
|
|
|
|
if !exp && eq {
|
|
|
|
t.Errorf("unexpected odr match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// temporarily remove peer to test odr fails
|
|
|
|
// expect retrievals to fail (except genesis block) without a les peer
|
2018-08-28 07:08:16 +00:00
|
|
|
client.peers.Unregister(client.rPeer.id)
|
2017-06-21 10:27:38 +00:00
|
|
|
time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
|
2016-10-14 03:51:29 +00:00
|
|
|
test(expFail)
|
|
|
|
// expect all retrievals to pass
|
2018-08-28 07:08:16 +00:00
|
|
|
client.peers.Register(client.rPeer)
|
2017-06-21 10:27:38 +00:00
|
|
|
time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
|
2018-08-28 07:08:16 +00:00
|
|
|
client.peers.lock.Lock()
|
2018-10-01 13:14:53 +00:00
|
|
|
client.rPeer.hasBlock = func(common.Hash, uint64, bool) bool { return true }
|
2018-08-28 07:08:16 +00:00
|
|
|
client.peers.lock.Unlock()
|
2016-10-14 03:51:29 +00:00
|
|
|
test(5)
|
|
|
|
// still expect all retrievals to pass, now data should be cached locally
|
2018-08-28 07:08:16 +00:00
|
|
|
client.peers.Unregister(client.rPeer.id)
|
2017-06-21 10:27:38 +00:00
|
|
|
time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
|
2016-10-14 03:51:29 +00:00
|
|
|
test(5)
|
|
|
|
}
|