2022-05-24 13:39:40 -05:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
2021-04-23 04:15:42 -05:00
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2021-04-23 04:15:42 -05:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2021-04-23 04:15:42 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 13:39:40 -05:00
|
|
|
// GNU General Public License for more details.
|
2021-04-23 04:15:42 -05:00
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2021-04-23 04:15:42 -05:00
|
|
|
|
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
2023-12-20 10:23:48 -06:00
|
|
|
crand "crypto/rand"
|
|
|
|
"fmt"
|
2021-04-23 04:15:42 -05:00
|
|
|
"os"
|
2023-12-20 10:23:48 -06:00
|
|
|
"path"
|
2021-04-23 04:15:42 -05:00
|
|
|
"testing"
|
2021-04-28 01:48:07 -05:00
|
|
|
"time"
|
2021-04-23 04:15:42 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-04-23 04:15:42 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/eth/catalyst"
|
2021-04-23 04:15:42 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
)
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
func makeJWTSecret() (string, [32]byte, error) {
|
|
|
|
var secret [32]byte
|
|
|
|
if _, err := crand.Read(secret[:]); err != nil {
|
|
|
|
return "", secret, fmt.Errorf("failed to create jwt secret: %v", err)
|
|
|
|
}
|
|
|
|
jwtPath := path.Join(os.TempDir(), "jwt_secret")
|
|
|
|
if err := os.WriteFile(jwtPath, []byte(hexutil.Encode(secret[:])), 0600); err != nil {
|
|
|
|
return "", secret, fmt.Errorf("failed to prepare jwt secret file: %v", err)
|
|
|
|
}
|
|
|
|
return jwtPath, secret, nil
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
|
|
|
|
func TestEthSuite(t *testing.T) {
|
2023-12-20 10:23:48 -06:00
|
|
|
jwtPath, secret, err := makeJWTSecret()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not make jwt secret: %v", err)
|
|
|
|
}
|
|
|
|
geth, err := runGeth("./testdata", jwtPath)
|
2021-04-23 04:15:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not run geth: %v", err)
|
|
|
|
}
|
|
|
|
defer geth.Close()
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
suite, err := NewSuite(geth.Server().Self(), "./testdata", geth.HTTPAuthEndpoint(), common.Bytes2Hex(secret[:]))
|
2021-04-23 04:15:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create new test suite: %v", err)
|
|
|
|
}
|
2022-08-02 07:48:55 -05:00
|
|
|
for _, test := range suite.EthTests() {
|
2021-04-23 04:15:42 -05:00
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
2023-12-20 10:23:48 -06:00
|
|
|
result := utesting.RunTests([]utesting.Test{{Name: test.Name, Fn: test.Fn}}, os.Stdout)
|
2021-04-23 04:15:42 -05:00
|
|
|
if result[0].Failed {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 08:24:32 -06:00
|
|
|
func TestSnapSuite(t *testing.T) {
|
2023-12-20 10:23:48 -06:00
|
|
|
jwtPath, secret, err := makeJWTSecret()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not make jwt secret: %v", err)
|
|
|
|
}
|
|
|
|
geth, err := runGeth("./testdata", jwtPath)
|
2022-02-04 08:24:32 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not run geth: %v", err)
|
|
|
|
}
|
|
|
|
defer geth.Close()
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
suite, err := NewSuite(geth.Server().Self(), "./testdata", geth.HTTPAuthEndpoint(), common.Bytes2Hex(secret[:]))
|
2022-02-04 08:24:32 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create new test suite: %v", err)
|
|
|
|
}
|
|
|
|
for _, test := range suite.SnapTests() {
|
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
2023-12-20 10:23:48 -06:00
|
|
|
result := utesting.RunTests([]utesting.Test{{Name: test.Name, Fn: test.Fn}}, os.Stdout)
|
2022-02-04 08:24:32 -06:00
|
|
|
if result[0].Failed {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 04:15:42 -05:00
|
|
|
// runGeth creates and starts a geth node
|
2023-12-20 10:23:48 -06:00
|
|
|
func runGeth(dir string, jwtPath string) (*node.Node, error) {
|
2021-04-23 04:15:42 -05:00
|
|
|
stack, err := node.New(&node.Config{
|
2023-12-20 10:23:48 -06:00
|
|
|
AuthAddr: "127.0.0.1",
|
|
|
|
AuthPort: 0,
|
2021-04-23 04:15:42 -05:00
|
|
|
P2P: p2p.Config{
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
|
|
|
NoDiscovery: true,
|
|
|
|
MaxPeers: 10, // in case a test requires multiple connections, can be changed in the future
|
|
|
|
NoDial: true,
|
|
|
|
},
|
2023-12-20 10:23:48 -06:00
|
|
|
JWTSecret: jwtPath,
|
2021-04-23 04:15:42 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
err = setupGeth(stack, dir)
|
2021-04-23 04:15:42 -05:00
|
|
|
if err != nil {
|
|
|
|
stack.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = stack.Start(); err != nil {
|
|
|
|
stack.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
func setupGeth(stack *node.Node, dir string) error {
|
|
|
|
chain, err := NewChain(dir)
|
2021-04-23 04:15:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
backend, err := eth.New(stack, ðconfig.Config{
|
cmd, core, eth, graphql, trie: no persisted clean trie cache file (#27525)
The clean trie cache is persisted periodically, therefore Geth can
quickly warmup the cache in next restart.
However it will reduce the robustness of system. The assumption is
held in Geth that if the parent trie node is present, then the entire
sub-trie associated with the parent are all prensent.
Imagine the scenario that Geth rewinds itself to a past block and
restart, but Geth finds the root node of "future state" in clean
cache then regard this state is present in disk, while is not in fact.
Another example is offline pruning tool. Whenever an offline pruning
is performed, the clean cache file has to be removed to aviod hitting
the root node of "deleted states" in clean cache.
All in all, compare with the minor performance gain, system robustness
is something we care more.
2023-07-04 02:21:06 -05:00
|
|
|
Genesis: &chain.genesis,
|
|
|
|
NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763
|
|
|
|
DatabaseCache: 10,
|
|
|
|
TrieCleanCache: 10,
|
|
|
|
TrieDirtyCache: 16,
|
|
|
|
TrieTimeout: 60 * time.Minute,
|
|
|
|
SnapshotCache: 10,
|
2021-04-23 04:15:42 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
if err := catalyst.Register(stack, backend); err != nil {
|
|
|
|
return fmt.Errorf("failed to register catalyst service: %v", err)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
_, err = backend.BlockChain().InsertChain(chain.blocks[1:])
|
|
|
|
return err
|
|
|
|
}
|