2019-08-19 01:24:20 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-21 21:00:22 +05:30
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
2019-09-11 17:04:35 -07:00
|
|
|
"fmt"
|
2022-04-18 22:42:07 +02:00
|
|
|
"io"
|
2019-08-19 01:24:20 +05:30
|
|
|
"testing"
|
2019-09-07 00:50:20 +05:30
|
|
|
"time"
|
2019-08-19 01:24:20 +05:30
|
|
|
|
2019-09-07 00:50:20 +05:30
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2022-10-07 02:24:51 -05:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2024-02-14 21:46:47 -08:00
|
|
|
ecdsaprysm "github.com/prysmaticlabs/prysm/v5/crypto/ecdsa"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/network"
|
|
|
|
_ "github.com/prysmaticlabs/prysm/v5/runtime/maxprocs"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2020-08-25 18:23:06 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-19 01:24:20 +05:30
|
|
|
)
|
|
|
|
|
2020-08-25 18:23:06 +03:00
|
|
|
func TestMain(m *testing.M) {
|
2020-12-04 19:10:07 +03:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2022-04-18 22:42:07 +02:00
|
|
|
logrus.SetOutput(io.Discard)
|
2020-08-25 18:23:06 +03:00
|
|
|
|
2020-12-04 19:10:07 +03:00
|
|
|
m.Run()
|
2020-08-25 18:23:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-19 01:24:20 +05:30
|
|
|
func TestBootnode_OK(t *testing.T) {
|
2021-09-16 12:12:27 -07:00
|
|
|
ipAddr, err := network.ExternalIPv4()
|
2020-08-25 18:23:06 +03:00
|
|
|
require.NoError(t, err)
|
2020-06-22 21:53:11 -05:00
|
|
|
privKey := extractPrivateKey()
|
2019-09-07 00:50:20 +05:30
|
|
|
cfg := discover.Config{
|
2019-09-23 22:54:16 +05:30
|
|
|
PrivateKey: privKey,
|
2019-08-19 01:24:20 +05:30
|
|
|
}
|
2019-09-07 00:50:20 +05:30
|
|
|
listener := createListener(ipAddr, 4000, cfg)
|
|
|
|
defer listener.Close()
|
2019-08-19 01:24:20 +05:30
|
|
|
|
2020-06-22 21:53:11 -05:00
|
|
|
cfg.PrivateKey = extractPrivateKey()
|
2019-09-07 00:50:20 +05:30
|
|
|
bootNode, err := enode.Parse(enode.ValidSchemes, listener.Self().String())
|
2020-08-25 18:23:06 +03:00
|
|
|
require.NoError(t, err)
|
2019-09-07 00:50:20 +05:30
|
|
|
cfg.Bootnodes = []*enode.Node{bootNode}
|
|
|
|
listener2 := createListener(ipAddr, 4001, cfg)
|
|
|
|
defer listener2.Close()
|
2019-08-19 01:24:20 +05:30
|
|
|
|
|
|
|
// test that both the nodes have the other peer stored in their local table.
|
|
|
|
listenerNode := listener.Self()
|
|
|
|
listenerNode2 := listener2.Self()
|
|
|
|
|
2019-09-07 00:50:20 +05:30
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
nodes := listener.Lookup(listenerNode2.ID())
|
2020-08-25 18:23:06 +03:00
|
|
|
assert.NotEqual(t, 0, len(nodes), "Length of nodes stored in table is not expected")
|
|
|
|
assert.Equal(t, listenerNode2.ID(), nodes[0].ID())
|
2019-08-19 01:24:20 +05:30
|
|
|
|
2019-09-07 00:50:20 +05:30
|
|
|
nodes = listener2.Lookup(listenerNode.ID())
|
2020-08-25 18:23:06 +03:00
|
|
|
assert.NotEqual(t, 0, len(nodes), "Length of nodes stored in table is not expected")
|
|
|
|
assert.Equal(t, listenerNode.ID(), nodes[0].ID())
|
2019-08-19 01:24:20 +05:30
|
|
|
}
|
2019-08-21 21:00:22 +05:30
|
|
|
|
|
|
|
func TestPrivateKey_ParsesCorrectly(t *testing.T) {
|
|
|
|
privKey, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
2020-08-25 18:23:06 +03:00
|
|
|
require.NoError(t, err)
|
2019-09-11 17:04:35 -07:00
|
|
|
|
|
|
|
pk, err := privKey.Raw()
|
2020-08-25 18:23:06 +03:00
|
|
|
require.NoError(t, err)
|
2019-09-11 17:04:35 -07:00
|
|
|
*privateKey = fmt.Sprintf("%x", pk)
|
2019-08-21 21:00:22 +05:30
|
|
|
|
2020-06-22 21:53:11 -05:00
|
|
|
extractedKey := extractPrivateKey()
|
2019-08-21 21:00:22 +05:30
|
|
|
|
2022-07-01 23:34:11 +08:00
|
|
|
rawKey, err := ecdsaprysm.ConvertFromInterfacePrivKey(privKey)
|
|
|
|
require.NoError(t, err)
|
2019-08-21 21:00:22 +05:30
|
|
|
|
|
|
|
r, s, err := ecdsa.Sign(rand.Reader, extractedKey, []byte{'t', 'e', 's', 't'})
|
2020-08-25 18:23:06 +03:00
|
|
|
require.NoError(t, err)
|
2019-08-21 21:00:22 +05:30
|
|
|
|
|
|
|
isVerified := ecdsa.Verify(&rawKey.PublicKey, []byte{'t', 'e', 's', 't'}, r, s)
|
2020-08-25 18:23:06 +03:00
|
|
|
assert.Equal(t, true, isVerified, "Unmarshalled key is not the same as the key that was given to the function")
|
2019-08-21 21:00:22 +05:30
|
|
|
*privateKey = ""
|
|
|
|
}
|