2016-10-26 00:04:42 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2016-10-26 00:04:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinkNodeEncodeDecode(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
cdb, cleanUp, err := MakeTestDB()
|
2016-10-26 00:04:42 +03:00
|
|
|
if err != nil {
|
2018-02-07 06:11:11 +03:00
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
2016-10-26 00:04:42 +03:00
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// First we'll create some initial data to use for populating our test
|
|
|
|
// LinkNode instances.
|
|
|
|
_, pub1 := btcec.PrivKeyFromBytes(btcec.S256(), key[:])
|
|
|
|
_, pub2 := btcec.PrivKeyFromBytes(btcec.S256(), rev[:])
|
|
|
|
addr1, err := net.ResolveTCPAddr("tcp", "10.0.0.1:9000")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test addr: %v", err)
|
|
|
|
}
|
|
|
|
addr2, err := net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create two fresh link node instances with the above dummy data, then
|
|
|
|
// fully sync both instances to disk.
|
|
|
|
node1 := cdb.NewLinkNode(wire.MainNet, pub1, addr1)
|
|
|
|
node2 := cdb.NewLinkNode(wire.TestNet3, pub2, addr2)
|
|
|
|
if err := node1.Sync(); err != nil {
|
|
|
|
t.Fatalf("unable to sync node: %v", err)
|
|
|
|
}
|
|
|
|
if err := node2.Sync(); err != nil {
|
|
|
|
t.Fatalf("unable to sync node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch all current link nodes from the database, they should exactly
|
|
|
|
// match the two created above.
|
|
|
|
originalNodes := []*LinkNode{node2, node1}
|
|
|
|
linkNodes, err := cdb.FetchAllLinkNodes()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch nodes: %v", err)
|
|
|
|
}
|
|
|
|
for i, node := range linkNodes {
|
|
|
|
if originalNodes[i].Network != node.Network {
|
|
|
|
t.Fatalf("node networks don't match: expected %v, got %v",
|
|
|
|
originalNodes[i].Network, node.Network)
|
|
|
|
}
|
|
|
|
|
|
|
|
originalPubkey := originalNodes[i].IdentityPub.SerializeCompressed()
|
|
|
|
dbPubkey := node.IdentityPub.SerializeCompressed()
|
|
|
|
if !bytes.Equal(originalPubkey, dbPubkey) {
|
|
|
|
t.Fatalf("node pubkeys don't match: expected %x, got %x",
|
|
|
|
originalPubkey, dbPubkey)
|
|
|
|
}
|
|
|
|
if originalNodes[i].LastSeen.Unix() != node.LastSeen.Unix() {
|
|
|
|
t.Fatalf("last seen timestamps don't match: expected %v got %v",
|
|
|
|
originalNodes[i].LastSeen.Unix(), node.LastSeen.Unix())
|
|
|
|
}
|
2018-02-02 06:51:13 +03:00
|
|
|
if originalNodes[i].Addresses[0].String() != node.Addresses[0].String() {
|
2016-10-26 00:04:42 +03:00
|
|
|
t.Fatalf("addresses don't match: expected %v, got %v",
|
|
|
|
originalNodes[i].Addresses, node.Addresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Next, we'll exercise the methods to append additional IP
|
2016-10-26 00:04:42 +03:00
|
|
|
// addresses, and also to update the last seen time.
|
|
|
|
if err := node1.UpdateLastSeen(time.Now()); err != nil {
|
|
|
|
t.Fatalf("unable to update last seen: %v", err)
|
|
|
|
}
|
|
|
|
if err := node1.AddAddress(addr2); err != nil {
|
|
|
|
t.Fatalf("unable to update addr: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Fetch the same node from the database according to its public key.
|
2016-10-26 00:04:42 +03:00
|
|
|
node1DB, err := cdb.FetchLinkNode(pub1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both the last seen timestamp and the list of reachable addresses for
|
|
|
|
// the node should be updated.
|
|
|
|
if node1DB.LastSeen.Unix() != node1.LastSeen.Unix() {
|
|
|
|
t.Fatalf("last seen timestamps don't match: expected %v got %v",
|
|
|
|
node1.LastSeen.Unix(), node1DB.LastSeen.Unix())
|
|
|
|
}
|
|
|
|
if len(node1DB.Addresses) != 2 {
|
2018-02-07 06:11:11 +03:00
|
|
|
t.Fatalf("wrong length for node1 addresses: expected %v, got %v",
|
2016-10-26 00:04:42 +03:00
|
|
|
2, len(node1DB.Addresses))
|
|
|
|
}
|
|
|
|
if node1DB.Addresses[0].String() != addr1.String() {
|
|
|
|
t.Fatalf("wrong address for node: expected %v, got %v",
|
|
|
|
addr1.String(), node1DB.Addresses[0].String())
|
|
|
|
}
|
|
|
|
if node1DB.Addresses[1].String() != addr2.String() {
|
|
|
|
t.Fatalf("wrong address for node: expected %v, got %v",
|
|
|
|
addr2.String(), node1DB.Addresses[1].String())
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 00:47:15 +03:00
|
|
|
|
|
|
|
func TestDeleteLinkNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
cdb, cleanUp, err := MakeTestDB()
|
2018-06-12 00:47:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
_, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), key[:])
|
|
|
|
addr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 1337,
|
|
|
|
}
|
|
|
|
linkNode := cdb.NewLinkNode(wire.TestNet3, pubKey, addr)
|
|
|
|
if err := linkNode.Sync(); err != nil {
|
|
|
|
t.Fatalf("unable to write link node to db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cdb.FetchLinkNode(pubKey); err != nil {
|
|
|
|
t.Fatalf("unable to find link node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cdb.DeleteLinkNode(pubKey); err != nil {
|
|
|
|
t.Fatalf("unable to delete link node from db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cdb.FetchLinkNode(pubKey); err == nil {
|
|
|
|
t.Fatal("should not have found link node in db, but did")
|
|
|
|
}
|
|
|
|
}
|