nim-eth/tests/p2p/test_ip_vote.nim
Ștefan Talpalaru 2088d7568d
CI: test with multiple Nim version (#429)
* CI: test with multiple Nim version

* clean up the testing tree a little

* replace "unittest" with "unittest2"
2021-12-11 19:12:55 +01:00

102 lines
3.1 KiB
Nim

{.used.}
import
unittest2,
stew/shims/net,
../../eth/keys, ../../eth/p2p/discoveryv5/[node, ip_vote]
suite "IP vote":
let rng = newRng()
test "Majority vote":
var
votes = IpVote.init(2)
let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3))
votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr2);
votes.insert(NodeId.random(rng[]), addr2);
votes.insert(NodeId.random(rng[]), addr2);
votes.insert(NodeId.random(rng[]), addr3);
votes.insert(NodeId.random(rng[]), addr3);
check votes.majority() == some(addr2)
test "Votes below threshold":
const threshold = 10
var
votes = IpVote.init(threshold)
let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3))
votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr2);
for i in 0..<(threshold - 1):
votes.insert(NodeId.random(rng[]), addr3);
check votes.majority().isNone()
test "Votes at threshold":
const threshold = 10
var
votes = IpVote.init(threshold)
let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3))
votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr2);
for i in 0..<(threshold):
votes.insert(NodeId.random(rng[]), addr3);
check votes.majority() == some(addr3)
test "Double votes with same address":
const threshold = 2
var
votes = IpVote.init(threshold)
let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2))
let nodeIdA = NodeId.random(rng[])
votes.insert(nodeIdA, addr1);
votes.insert(nodeIdA, addr1);
votes.insert(nodeIdA, addr1);
votes.insert(NodeId.random(rng[]), addr2);
votes.insert(NodeId.random(rng[]), addr2);
check votes.majority() == some(addr2)
test "Double votes with different address":
const threshold = 2
var
votes = IpVote.init(threshold)
let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3))
let nodeIdA = NodeId.random(rng[])
votes.insert(nodeIdA, addr1);
votes.insert(nodeIdA, addr2);
votes.insert(nodeIdA, addr3);
votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr2);
votes.insert(NodeId.random(rng[]), addr3);
check votes.majority() == some(addr3)