stasatdaglabs d835f72e74
Make AddressManager persistent (#1525)
* Move existing address/bannedAddress functionality to a new addressStore object.

* Implement TestAddressManager.

* Implement serializeAddressKey and deserializeAddressKey.

* Implement serializeNetAddress and deserializeNetAddress.

* Store addresses and banned addresses to disk.

* Implement restoreNotBannedAddresses.

* Fix bannedDatabaseKey.

* Implement restoreBannedAddresses.

* Implement TestRestoreAddressManager.

* Defer closing the database in TestRestoreAddressManager.

* Defer closing the database in TestRestoreAddressManager.

* Add a log.

* Return errors where appropriate.

Co-authored-by: Elichai Turkel <elichai.turkel@gmail.com>
2021-02-14 19:08:06 +02:00

46 lines
1.6 KiB
Go

package addressmanager
import (
"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/util/mstime"
"net"
"reflect"
"testing"
)
func TestAddressKeySerialization(t *testing.T) {
addressManager, teardown := newAddressManagerForTest(t, "TestAddressKeySerialization")
defer teardown()
addressStore := addressManager.store
testAddress := &appmessage.NetAddress{IP: net.ParseIP("2602:100:abcd::102"), Port: 12345}
testAddressKey := netAddressKey(testAddress)
serializedTestAddressKey := addressStore.serializeAddressKey(testAddressKey)
deserializedTestAddressKey := addressStore.deserializeAddressKey(serializedTestAddressKey)
if !reflect.DeepEqual(testAddressKey, deserializedTestAddressKey) {
t.Fatalf("testAddressKey and deserializedTestAddressKey are not equal\n"+
"testAddressKey:%+v\ndeserializedTestAddressKey:%+v", testAddressKey, deserializedTestAddressKey)
}
}
func TestNetAddressSerialization(t *testing.T) {
addressManager, teardown := newAddressManagerForTest(t, "TestNetAddressSerialization")
defer teardown()
addressStore := addressManager.store
testAddress := &appmessage.NetAddress{
IP: net.ParseIP("2602:100:abcd::102"),
Port: 12345,
Timestamp: mstime.Now(),
Services: appmessage.ServiceFlag(6789),
}
serializedTestNetAddress := addressStore.serializeNetAddress(testAddress)
deserializedTestNetAddress := addressStore.deserializeNetAddress(serializedTestNetAddress)
if !reflect.DeepEqual(testAddress, deserializedTestNetAddress) {
t.Fatalf("testAddress and deserializedTestNetAddress are not equal\n"+
"testAddress:%+v\ndeserializedTestNetAddress:%+v", testAddress, deserializedTestNetAddress)
}
}