2018-07-14 02:15:37 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-07-14 19:48:42 +00:00
|
|
|
"flag"
|
2018-07-31 04:41:27 +00:00
|
|
|
"fmt"
|
2020-06-26 11:23:38 +00:00
|
|
|
"io/ioutil"
|
2018-07-31 04:41:27 +00:00
|
|
|
"os"
|
2020-10-09 09:28:35 +00:00
|
|
|
"path/filepath"
|
2018-07-14 02:15:37 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-12-07 17:57:26 +00:00
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
2020-10-09 09:28:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
2020-08-25 15:23:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-08-13 16:22:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2018-08-20 15:50:11 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2020-05-31 06:44:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-07-14 02:15:37 +00:00
|
|
|
)
|
|
|
|
|
2019-11-19 22:15:48 +00:00
|
|
|
// Ensure BeaconNode implements interfaces.
|
2020-10-10 00:36:48 +00:00
|
|
|
var _ statefeed.Notifier = (*BeaconNode)(nil)
|
2019-11-19 22:15:48 +00:00
|
|
|
|
2018-08-20 15:50:11 +00:00
|
|
|
// Test that beacon chain node can close.
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestNodeClose_OK(t *testing.T) {
|
2018-08-20 15:50:11 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-11-06 21:43:10 +00:00
|
|
|
|
2020-11-10 22:45:17 +00:00
|
|
|
tmp := fmt.Sprintf("%s/datadirtest2", t.TempDir())
|
2018-11-06 21:43:10 +00:00
|
|
|
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2018-08-20 15:50:11 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2019-03-04 20:10:03 +00:00
|
|
|
set.Bool("test-skip-pow", true, "skip pow dial")
|
2018-08-20 15:50:11 +00:00
|
|
|
set.String("datadir", tmp, "node data directory")
|
2019-09-05 16:04:06 +00:00
|
|
|
set.String("p2p-encoding", "ssz", "p2p encoding scheme")
|
2018-10-02 02:04:37 +00:00
|
|
|
set.Bool("demo-config", true, "demo configuration")
|
2019-03-04 20:10:03 +00:00
|
|
|
set.String("deposit-contract", "0x0000000000000000000000000000000000000000", "deposit contract address")
|
2018-08-20 15:50:11 +00:00
|
|
|
|
2020-03-19 21:46:44 +00:00
|
|
|
context := cli.NewContext(&app, set, nil)
|
2018-08-20 15:50:11 +00:00
|
|
|
|
2021-02-01 17:12:52 +00:00
|
|
|
node, err := New(context)
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-20 15:50:11 +00:00
|
|
|
|
|
|
|
node.Close()
|
|
|
|
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Stopping beacon node")
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, os.RemoveAll(tmp))
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
2020-06-26 11:23:38 +00:00
|
|
|
|
|
|
|
func TestBootStrapNodeFile(t *testing.T) {
|
2020-11-10 22:45:17 +00:00
|
|
|
file, err := ioutil.TempFile(t.TempDir(), "bootstrapFile")
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-26 11:23:38 +00:00
|
|
|
|
|
|
|
sampleNode0 := "- enr:-Ku4QMKVC_MowDsmEa20d5uGjrChI0h8_KsKXDmgVQbIbngZV0i" +
|
|
|
|
"dV6_RL7fEtZGo-kTNZ5o7_EJI_vCPJ6scrhwX0Z4Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD" +
|
|
|
|
"1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQJxCnE6v_x2ekgY_uo" +
|
|
|
|
"E1rtwzvGy40mq9eD66XfHPBWgIIN1ZHCCD6A"
|
|
|
|
sampleNode1 := "- enr:-TESTNODE2"
|
|
|
|
sampleNode2 := "- enr:-TESTNODE3"
|
|
|
|
err = ioutil.WriteFile(file.Name(), []byte(sampleNode0+"\n"+sampleNode1+"\n"+sampleNode2), 0644)
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err, "Error in WriteFile call")
|
2020-06-26 11:23:38 +00:00
|
|
|
nodeList, err := readbootNodes(file.Name())
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err, "Error in readbootNodes call")
|
|
|
|
assert.Equal(t, sampleNode0[2:], nodeList[0], "Unexpected nodes")
|
|
|
|
assert.Equal(t, sampleNode1[2:], nodeList[1], "Unexpected nodes")
|
|
|
|
assert.Equal(t, sampleNode2[2:], nodeList[2], "Unexpected nodes")
|
2020-06-26 11:23:38 +00:00
|
|
|
}
|
2020-10-09 09:28:35 +00:00
|
|
|
|
|
|
|
// TestClearDB tests clearing the database
|
|
|
|
func TestClearDB(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2020-11-10 22:45:17 +00:00
|
|
|
tmp := filepath.Join(t.TempDir(), "datadirtest")
|
2020-10-09 09:28:35 +00:00
|
|
|
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("datadir", tmp, "node data directory")
|
|
|
|
set.Bool(cmd.ForceClearDB.Name, true, "force clear db")
|
|
|
|
|
|
|
|
context := cli.NewContext(&app, set, nil)
|
2021-02-01 17:12:52 +00:00
|
|
|
_, err := New(context)
|
2020-10-09 09:28:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.LogsContain(t, hook, "Removing database")
|
|
|
|
require.NoError(t, os.RemoveAll(tmp))
|
|
|
|
}
|