Skip to content

Commit 6f88d65

Browse files
authored
trie, rpc, cmd/geth: fix tests on 32-bit and windows + minor rpc fixes (ethereum#21871)
* trie: fix tests to work on 32-bit systems * les: make test work on 32-bit platform * cmd/geth: fix windows-issues on tests * trie: improve balance * cmd/geth: make account tests less verbose + less mem intense * rpc: make debug-level log output less verbose * cmd/geth: lint
1 parent f1e1d9f commit 6f88d65

File tree

7 files changed

+98
-61
lines changed

7 files changed

+98
-61
lines changed

cmd/geth/accountcmd_test.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ Fatal: could not decrypt key with given password
180180
func TestUnlockFlag(t *testing.T) {
181181
datadir := tmpDatadirWithKeystore(t)
182182
geth := runGeth(t,
183-
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
184-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
183+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "256", "--ipcdisable",
184+
"--datadir", datadir, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
185185
"js", "testdata/empty.js")
186186
geth.Expect(`
187187
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
@@ -204,8 +204,8 @@ Password: {{.InputLine "foobar"}}
204204
func TestUnlockFlagWrongPassword(t *testing.T) {
205205
datadir := tmpDatadirWithKeystore(t)
206206
geth := runGeth(t,
207-
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
208-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
207+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
208+
"--datadir", datadir, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
209209
defer geth.ExpectExit()
210210
geth.Expect(`
211211
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
@@ -223,9 +223,8 @@ Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could
223223
func TestUnlockFlagMultiIndex(t *testing.T) {
224224
datadir := tmpDatadirWithKeystore(t)
225225
geth := runGeth(t,
226-
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
227-
"--unlock", "0,2",
228-
"js", "testdata/empty.js")
226+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
227+
"--datadir", datadir, "--unlock", "0,2", "js", "testdata/empty.js")
229228
geth.Expect(`
230229
Unlocking account 0 | Attempt 1/3
231230
!! Unsupported terminal, password will be echoed.
@@ -250,8 +249,8 @@ Password: {{.InputLine "foobar"}}
250249
func TestUnlockFlagPasswordFile(t *testing.T) {
251250
datadir := tmpDatadirWithKeystore(t)
252251
geth := runGeth(t,
253-
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
254-
"--password", "testdata/passwords.txt", "--unlock", "0,2",
252+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
253+
"--datadir", datadir, "--password", "testdata/passwords.txt", "--unlock", "0,2",
255254
"js", "testdata/empty.js")
256255
geth.ExpectExit()
257256

@@ -270,8 +269,8 @@ func TestUnlockFlagPasswordFile(t *testing.T) {
270269
func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
271270
datadir := tmpDatadirWithKeystore(t)
272271
geth := runGeth(t,
273-
"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
274-
"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
272+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
273+
"--datadir", datadir, "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
275274
defer geth.ExpectExit()
276275
geth.Expect(`
277276
Fatal: Failed to unlock account 0 (could not decrypt key with given password)
@@ -281,8 +280,8 @@ Fatal: Failed to unlock account 0 (could not decrypt key with given password)
281280
func TestUnlockFlagAmbiguous(t *testing.T) {
282281
store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
283282
geth := runGeth(t,
284-
"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
285-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
283+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
284+
"--keystore", store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
286285
"js", "testdata/empty.js")
287286
defer geth.ExpectExit()
288287

@@ -319,8 +318,8 @@ In order to avoid this warning, you need to remove the following duplicate key f
319318
func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
320319
store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
321320
geth := runGeth(t,
322-
"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
323-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
321+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
322+
"--keystore", store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
324323
defer geth.ExpectExit()
325324

326325
// Helper for the expect template, returns absolute keystore path.

cmd/geth/les_test.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"runtime"
89
"strings"
10+
"sync/atomic"
911
"testing"
1012
"time"
1113

@@ -119,40 +121,53 @@ func ipcEndpoint(ipcPath, datadir string) string {
119121
return ipcPath
120122
}
121123

124+
// nextIPC ensures that each ipc pipe gets a unique name.
125+
// On linux, it works well to use ipc pipes all over the filesystem (in datadirs),
126+
// but windows require pipes to sit in "\\.\pipe\". Therefore, to run several
127+
// nodes simultaneously, we need to distinguish between them, which we do by
128+
// the pipe filename instead of folder.
129+
var nextIPC = uint32(0)
130+
122131
func startGethWithIpc(t *testing.T, name string, args ...string) *gethrpc {
123-
g := &gethrpc{name: name}
124-
args = append([]string{"--networkid=42", "--port=0", "--nousb"}, args...)
132+
ipcName := fmt.Sprintf("geth-%d.ipc", atomic.AddUint32(&nextIPC, 1))
133+
args = append([]string{"--networkid=42", "--port=0", "--nousb", "--ipcpath", ipcName}, args...)
125134
t.Logf("Starting %v with rpc: %v", name, args)
126-
g.geth = runGeth(t, args...)
135+
136+
g := &gethrpc{
137+
name: name,
138+
geth: runGeth(t, args...),
139+
}
127140
// wait before we can attach to it. TODO: probe for it properly
128141
time.Sleep(1 * time.Second)
129142
var err error
130-
ipcpath := ipcEndpoint("geth.ipc", g.geth.Datadir)
131-
g.rpc, err = rpc.Dial(ipcpath)
132-
if err != nil {
143+
ipcpath := ipcEndpoint(ipcName, g.geth.Datadir)
144+
if g.rpc, err = rpc.Dial(ipcpath); err != nil {
133145
t.Fatalf("%v rpc connect to %v: %v", name, ipcpath, err)
134146
}
135147
return g
136148
}
137149

138150
func initGeth(t *testing.T) string {
139-
g := runGeth(t, "--nousb", "--networkid=42", "init", "./testdata/clique.json")
151+
args := []string{"--nousb", "--networkid=42", "init", "./testdata/clique.json"}
152+
t.Logf("Initializing geth: %v ", args)
153+
g := runGeth(t, args...)
140154
datadir := g.Datadir
141155
g.WaitExit()
142156
return datadir
143157
}
144158

145159
func startLightServer(t *testing.T) *gethrpc {
146160
datadir := initGeth(t)
161+
t.Logf("Importing keys to geth")
147162
runGeth(t, "--nousb", "--datadir", datadir, "--password", "./testdata/password.txt", "account", "import", "./testdata/key.prv").WaitExit()
148163
account := "0x02f0d131f1f97aef08aec6e3291b957d9efe7105"
149-
server := startGethWithIpc(t, "lightserver", "--allow-insecure-unlock", "--datadir", datadir, "--password", "./testdata/password.txt", "--unlock", account, "--mine", "--light.serve=100", "--light.maxpeers=1", "--nodiscover", "--nat=extip:127.0.0.1")
164+
server := startGethWithIpc(t, "lightserver", "--allow-insecure-unlock", "--datadir", datadir, "--password", "./testdata/password.txt", "--unlock", account, "--mine", "--light.serve=100", "--light.maxpeers=1", "--nodiscover", "--nat=extip:127.0.0.1", "--verbosity=4")
150165
return server
151166
}
152167

153168
func startClient(t *testing.T, name string) *gethrpc {
154169
datadir := initGeth(t)
155-
return startGethWithIpc(t, name, "--datadir", datadir, "--nodiscover", "--syncmode=light", "--nat=extip:127.0.0.1")
170+
return startGethWithIpc(t, name, "--datadir", datadir, "--nodiscover", "--syncmode=light", "--nat=extip:127.0.0.1", "--verbosity=4")
156171
}
157172

158173
func TestPriorityClient(t *testing.T) {
@@ -175,7 +190,7 @@ func TestPriorityClient(t *testing.T) {
175190
prioCli := startClient(t, "prioCli")
176191
defer prioCli.killAndWait()
177192
// 3_000_000_000 once we move to Go 1.13
178-
tokens := 3000000000
193+
tokens := uint64(3000000000)
179194
lightServer.callRPC(nil, "les_addBalance", prioCli.getNodeInfo().ID, tokens)
180195
prioCli.addPeer(lightServer)
181196

cmd/geth/run_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ func runGeth(t *testing.T, args ...string) *testgeth {
7070
tt := &testgeth{}
7171
tt.TestCmd = cmdtest.NewTestCmd(t, tt)
7272
for i, arg := range args {
73-
switch {
74-
case arg == "-datadir" || arg == "--datadir":
73+
switch arg {
74+
case "--datadir":
7575
if i < len(args)-1 {
7676
tt.Datadir = args[i+1]
7777
}
78-
case arg == "-etherbase" || arg == "--etherbase":
78+
case "--etherbase":
7979
if i < len(args)-1 {
8080
tt.Etherbase = args[i+1]
8181
}
@@ -84,7 +84,7 @@ func runGeth(t *testing.T, args ...string) *testgeth {
8484
if tt.Datadir == "" {
8585
tt.Datadir = tmpdir(t)
8686
tt.Cleanup = func() { os.RemoveAll(tt.Datadir) }
87-
args = append([]string{"-datadir", tt.Datadir}, args...)
87+
args = append([]string{"--datadir", tt.Datadir}, args...)
8888
// Remove the temporary datadir if something fails below.
8989
defer func() {
9090
if t.Failed() {

internal/cmdtest/test_cmd.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"regexp"
2828
"strings"
2929
"sync"
30+
"sync/atomic"
3031
"syscall"
3132
"testing"
3233
"text/template"
@@ -55,10 +56,13 @@ type TestCmd struct {
5556
Err error
5657
}
5758

59+
var id int32
60+
5861
// Run exec's the current binary using name as argv[0] which will trigger the
5962
// reexec init function for that name (e.g. "geth-test" in cmd/geth/run_test.go)
6063
func (tt *TestCmd) Run(name string, args ...string) {
61-
tt.stderr = &testlogger{t: tt.T}
64+
id := atomic.AddInt32(&id, 1)
65+
tt.stderr = &testlogger{t: tt.T, name: fmt.Sprintf("%d", id)}
6266
tt.cmd = &exec.Cmd{
6367
Path: reexec.Self(),
6468
Args: append([]string{name}, args...),
@@ -238,16 +242,17 @@ func (tt *TestCmd) withKillTimeout(fn func()) {
238242
// testlogger logs all written lines via t.Log and also
239243
// collects them for later inspection.
240244
type testlogger struct {
241-
t *testing.T
242-
mu sync.Mutex
243-
buf bytes.Buffer
245+
t *testing.T
246+
mu sync.Mutex
247+
buf bytes.Buffer
248+
name string
244249
}
245250

246251
func (tl *testlogger) Write(b []byte) (n int, err error) {
247252
lines := bytes.Split(b, []byte("\n"))
248253
for _, line := range lines {
249254
if len(line) > 0 {
250-
tl.t.Logf("(stderr) %s", line)
255+
tl.t.Logf("(stderr:%v) %s", tl.name, line)
251256
}
252257
}
253258
tl.mu.Lock()

node/rpcstack.go

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ func (is *ipcServer) start(apis []rpc.API) error {
448448
}
449449
listener, srv, err := rpc.StartIPCEndpoint(is.endpoint, apis)
450450
if err != nil {
451+
is.log.Warn("IPC opening failed", "url", is.endpoint, "error", err)
451452
return err
452453
}
453454
is.log.Info("IPC endpoint opened", "url", is.endpoint)

rpc/endpoints.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,30 @@ package rpc
1818

1919
import (
2020
"net"
21+
"strings"
2122

2223
"github.com/ethereum/go-ethereum/log"
2324
)
2425

2526
// StartIPCEndpoint starts an IPC endpoint.
2627
func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
2728
// Register all the APIs exposed by the services.
28-
handler := NewServer()
29+
var (
30+
handler = NewServer()
31+
regMap = make(map[string]struct{})
32+
registered []string
33+
)
2934
for _, api := range apis {
3035
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
36+
log.Info("IPC registration failed", "namespace", api.Namespace, "error", err)
3137
return nil, nil, err
3238
}
33-
log.Debug("IPC registered", "namespace", api.Namespace)
39+
if _, ok := regMap[api.Namespace]; !ok {
40+
registered = append(registered, api.Namespace)
41+
regMap[api.Namespace] = struct{}{}
42+
}
3443
}
44+
log.Debug("IPCs registered", "namespaces", strings.Join(registered, ","))
3545
// All APIs registered, start the IPC listener.
3646
listener, err := ipcListen(ipcEndpoint)
3747
if err != nil {

trie/trie_test.go

+32-25
Original file line numberDiff line numberDiff line change
@@ -594,21 +594,20 @@ func benchmarkCommitAfterHash(b *testing.B, onleaf LeafCallback) {
594594

595595
func TestTinyTrie(t *testing.T) {
596596
// Create a realistic account trie to hash
597-
_, accounts := makeAccounts(10000)
597+
_, accounts := makeAccounts(5)
598598
trie := newEmpty()
599599
trie.Update(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001337"), accounts[3])
600-
if exp, root := common.HexToHash("4fa6efd292cffa2db0083b8bedd23add2798ae73802442f52486e95c3df7111c"), trie.Hash(); exp != root {
601-
t.Fatalf("1: got %x, exp %x", root, exp)
600+
if exp, root := common.HexToHash("8c6a85a4d9fda98feff88450299e574e5378e32391f75a055d470ac0653f1005"), trie.Hash(); exp != root {
601+
t.Errorf("1: got %x, exp %x", root, exp)
602602
}
603603
trie.Update(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001338"), accounts[4])
604-
if exp, root := common.HexToHash("cb5fb1213826dad9e604f095f8ceb5258fe6b5c01805ce6ef019a50699d2d479"), trie.Hash(); exp != root {
605-
t.Fatalf("2: got %x, exp %x", root, exp)
604+
if exp, root := common.HexToHash("ec63b967e98a5720e7f720482151963982890d82c9093c0d486b7eb8883a66b1"), trie.Hash(); exp != root {
605+
t.Errorf("2: got %x, exp %x", root, exp)
606606
}
607607
trie.Update(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001339"), accounts[4])
608-
if exp, root := common.HexToHash("ed7e06b4010057d8703e7b9a160a6d42cf4021f9020da3c8891030349a646987"), trie.Hash(); exp != root {
609-
t.Fatalf("3: got %x, exp %x", root, exp)
608+
if exp, root := common.HexToHash("0608c1d1dc3905fa22204c7a0e43644831c3b6d3def0f274be623a948197e64a"), trie.Hash(); exp != root {
609+
t.Errorf("3: got %x, exp %x", root, exp)
610610
}
611-
612611
checktr, _ := New(common.Hash{}, trie.db)
613612
it := NewIterator(trie.NodeIterator(nil))
614613
for it.Next() {
@@ -630,7 +629,7 @@ func TestCommitAfterHash(t *testing.T) {
630629
trie.Hash()
631630
trie.Commit(nil)
632631
root := trie.Hash()
633-
exp := common.HexToHash("e5e9c29bb50446a4081e6d1d748d2892c6101c1e883a1f77cf21d4094b697822")
632+
exp := common.HexToHash("72f9d3f3fe1e1dd7b8936442e7642aef76371472d94319900790053c493f3fe6")
634633
if exp != root {
635634
t.Errorf("got %x, exp %x", root, exp)
636635
}
@@ -646,19 +645,27 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) {
646645
// Create a realistic account trie to hash
647646
addresses = make([][20]byte, size)
648647
for i := 0; i < len(addresses); i++ {
649-
for j := 0; j < len(addresses[i]); j++ {
650-
addresses[i][j] = byte(random.Intn(256))
651-
}
648+
data := make([]byte, 20)
649+
random.Read(data)
650+
copy(addresses[i][:], data)
652651
}
653652
accounts = make([][]byte, len(addresses))
654653
for i := 0; i < len(accounts); i++ {
655654
var (
656-
nonce = uint64(random.Int63())
657-
balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
658-
root = emptyRoot
659-
code = crypto.Keccak256(nil)
655+
nonce = uint64(random.Int63())
656+
root = emptyRoot
657+
code = crypto.Keccak256(nil)
660658
)
661-
accounts[i], _ = rlp.EncodeToBytes(&account{nonce, balance, root, code})
659+
// The big.Rand function is not deterministic with regards to 64 vs 32 bit systems,
660+
// and will consume different amount of data from the rand source.
661+
//balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
662+
// Therefore, we instead just read via byte buffer
663+
numBytes := random.Uint32() % 33 // [0, 32] bytes
664+
balanceBytes := make([]byte, numBytes)
665+
random.Read(balanceBytes)
666+
balance := new(big.Int).SetBytes(balanceBytes)
667+
data, _ := rlp.EncodeToBytes(&account{nonce, balance, root, code})
668+
accounts[i] = data
662669
}
663670
return addresses, accounts
664671
}
@@ -714,12 +721,12 @@ func TestCommitSequence(t *testing.T) {
714721
expWriteSeqHash []byte
715722
expCallbackSeqHash []byte
716723
}{
717-
{20, common.FromHex("68c495e45209e243eb7e4f4e8ca8f9f7be71003bd9cafb8061b4534373740193"),
718-
common.FromHex("01783213033d6b7781a641ab499e680d959336d025ac16f44d02f4f0c021bbf5")},
719-
{200, common.FromHex("3b20d16c13c4bc3eb3b8d0ad7a169fef3b1600e056c0665895d03d3d2b2ff236"),
720-
common.FromHex("fb8db0ec82e8f02729f11228940885b181c3047ab0d654ed0110291ca57111a8")},
721-
{2000, common.FromHex("34eff3d1048bebdf77e9ae8bd939f2e7c742edc3dcd1173cff1aad9dbd20451a"),
722-
common.FromHex("1c981604b1a9f8ffa40e0ae66b14830a87f5a4ed8345146a3912e6b2dcb05e63")},
724+
{20, common.FromHex("873c78df73d60e59d4a2bcf3716e8bfe14554549fea2fc147cb54129382a8066"),
725+
common.FromHex("ff00f91ac05df53b82d7f178d77ada54fd0dca64526f537034a5dbe41b17df2a")},
726+
{200, common.FromHex("ba03d891bb15408c940eea5ee3d54d419595102648d02774a0268d892add9c8e"),
727+
common.FromHex("f3cd509064c8d319bbdd1c68f511850a902ad275e6ed5bea11547e23d492a926")},
728+
{2000, common.FromHex("f7a184f20df01c94f09537401d11e68d97ad0c00115233107f51b9c287ce60c7"),
729+
common.FromHex("ff795ea898ba1e4cfed4a33b4cf5535a347a02cf931f88d88719faf810f9a1c9")},
723730
} {
724731
addresses, accounts := makeAccounts(tc.count)
725732
// This spongeDb is used to check the sequence of disk-db-writes
@@ -740,10 +747,10 @@ func TestCommitSequence(t *testing.T) {
740747
callbackSponge.Write(c[:])
741748
})
742749
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
743-
t.Fatalf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp)
750+
t.Errorf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp)
744751
}
745752
if got, exp := callbackSponge.Sum(nil), tc.expCallbackSeqHash; !bytes.Equal(got, exp) {
746-
t.Fatalf("test %d, call back sequence wrong:\ngot: %x exp %x\n", i, got, exp)
753+
t.Errorf("test %d, call back sequence wrong:\ngot: %x exp %x\n", i, got, exp)
747754
}
748755
}
749756
}

0 commit comments

Comments
 (0)