Skip to content

Commit

Permalink
simulators/ethereum/rpc: run tests against LES client
Browse files Browse the repository at this point in the history
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
  • Loading branch information
fjl and rjl493456442 committed Feb 2, 2022
1 parent 1f7f6b4 commit 9e744e7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
30 changes: 30 additions & 0 deletions simulators/ethereum/rpc/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ func (t *TestEnv) Ctx() context.Context {
return t.lastCtx
}

func waitSynced(c *rpc.Client) (err error) {
var (
timeout = 20 * time.Second
end = time.Now().Add(timeout)
ctx, cancel = context.WithDeadline(context.Background(), end)
)
defer func() {
cancel()
if err == context.DeadlineExceeded {
err = fmt.Errorf("didn't sync within timeout of %v", 20*time.Second)
}
}()

ec := ethclient.NewClient(c)
for {
progress, err := ec.SyncProgress(ctx)
if err != nil {
return err
}
head, err := ec.BlockNumber(ctx)
if err != nil {
return err
}
if progress == nil && head > 0 {
return nil // success!
}
time.Sleep(100 * time.Millisecond)
}
}

// Naive generic function that works in all situations.
// A better solution is to use logs to wait for confirmations.
func waitForTxConfirmations(t *TestEnv, txHash common.Hash, n uint64) (*types.Receipt, error) {
Expand Down
55 changes: 51 additions & 4 deletions simulators/ethereum/rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ var (
)

var clientEnv = hivesim.Params{
"HIVE_NODETYPE": "full",
"HIVE_NETWORK_ID": networkID.String(),
"HIVE_CHAIN_ID": chainID.String(),
"HIVE_FORK_HOMESTEAD": "0",
"HIVE_FORK_TANGERINE": "0",
"HIVE_FORK_SPURIOUS": "0",

// All tests use clique PoA to mine new blocks.
"HIVE_CLIQUE_PERIOD": "1",
"HIVE_CLIQUE_PRIVATEKEY": "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c",
Expand Down Expand Up @@ -98,19 +100,64 @@ The RPC test suite runs a set of RPC related tests against a running node. It te
several real-world scenarios such as sending value transactions, deploying a contract or
interacting with one.`[1:],
}

// Add tests for full nodes.
suite.Add(&hivesim.ClientTestSpec{
Name: "client launch",
Description: `This test launches the client and collects its logs.`,
Parameters: clientEnv,
Files: files,
Run: runAllTests,
Run: func(t *hivesim.T, c *hivesim.Client) { runAllTests(t, c, c.Type) },
})

// Add tests to launch LES servers.
serverParams := clientEnv.Set("HIVE_LES_SERVER", "1")
suite.Add(hivesim.ClientTestSpec{
Role: "eth1_les_server",
Name: "CLIENT as LES server",
Description: "This test launches an LES server.",
Parameters: serverParams,
Files: files,
Run: func(t *hivesim.T, srv *hivesim.Client) { runLESTests(t, srv) },
})

sim := hivesim.New()
hivesim.MustRunSuite(sim, suite)
}

func runLESTests(t *hivesim.T, serverNode *hivesim.Client) {
// Configure LES client.
clientParams := clientEnv.Set("HIVE_NODETYPE", "light")
// Disable mining.
clientParams = clientParams.Set("HIVE_MINER", "")
clientParams = clientParams.Set("HIVE_CLIQUE_PRIVATEKEY", "")

enode, err := serverNode.EnodeURL()
if err != nil {
t.Fatal("can't get node peer-to-peer endpoint:", enode)
}

// Sync all sink nodes against the source.
t.RunAllClients(hivesim.ClientTestSpec{
Name: "CLIENT as LES client",
Description: "This runs the RPC tests against an LES client.",
Role: "eth1_les_client",
Parameters: clientParams,
Files: files,
Run: func(t *hivesim.T, client *hivesim.Client) {
err := client.RPC().Call(nil, "admin_addPeer", enode)
if err != nil {
t.Fatalf("connection failed:", err)
}
waitSynced(client.RPC())
runAllTests(t, client, client.Type+" LES")
},
})
hivesim.MustRunSuite(hivesim.New(), suite)
}

// runAllTests runs the tests against a client instance.
// Most tests simply wait for tx inclusion in a block so we can run many tests concurrently.
func runAllTests(t *hivesim.T, c *hivesim.Client) {
func runAllTests(t *hivesim.T, c *hivesim.Client, clientName string) {
vault := newVault()

s := newSemaphore(16)
Expand All @@ -120,7 +167,7 @@ func runAllTests(t *hivesim.T, c *hivesim.Client) {
go func() {
defer s.put()
t.Run(hivesim.TestSpec{
Name: fmt.Sprintf("%s (%s)", test.Name, c.Type),
Name: fmt.Sprintf("%s (%s)", test.Name, clientName),
Description: test.About,
Run: func(t *hivesim.T) {
switch test.Name[:strings.IndexByte(test.Name, '/')] {
Expand Down
4 changes: 2 additions & 2 deletions simulators/ethereum/rpc/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newVault() *vault {
func (v *vault) generateKey() common.Address {
key, err := crypto.GenerateKey()
if err != nil {
panic(fmt.Errorf("can't generate account key:", err))
panic(fmt.Errorf("can't generate account key: %v", err))
}
addr := crypto.PubkeyToAddress(key.PublicKey)

Expand Down Expand Up @@ -110,7 +110,7 @@ func (v *vault) createAccountWithSubscription(t *TestEnv, amount *big.Int) commo
addressTopic := common.BytesToHash(common.LeftPadBytes(address[:], 32))
q := ethereum.FilterQuery{
Addresses: []common.Address{predeployedVaultAddr},
Topics: [][]common.Hash{[]common.Hash{eventTopic}, []common.Hash{addressTopic}},
Topics: [][]common.Hash{{eventTopic}, {addressTopic}},
}
logsSub, err = t.Eth.SubscribeFilterLogs(ctx, q, logs)
if err != nil {
Expand Down

0 comments on commit 9e744e7

Please sign in to comment.