2024-01-24 08:09:11 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2021-03-29 13:26:34 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-01-21 16:21:36 +00:00
|
|
|
import
|
2024-01-24 08:09:11 +00:00
|
|
|
std/net,
|
2021-12-11 18:12:55 +00:00
|
|
|
unittest2,
|
2021-04-06 11:33:24 +00:00
|
|
|
../../eth/keys, ../../eth/p2p/discoveryv5/[node, ip_vote]
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
suite "IP vote":
|
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
test "Majority vote":
|
|
|
|
var
|
|
|
|
votes = IpVote.init(2)
|
|
|
|
let
|
2023-11-10 06:28:21 +00:00
|
|
|
addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
|
|
|
|
addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
|
|
|
|
addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-06-18 16:09:27 +00:00
|
|
|
check votes.majority() == Opt.some(addr2)
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
test "Votes below threshold":
|
|
|
|
const threshold = 10
|
|
|
|
|
|
|
|
var
|
|
|
|
votes = IpVote.init(threshold)
|
|
|
|
let
|
2023-11-10 06:28:21 +00:00
|
|
|
addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
|
|
|
|
addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
|
|
|
|
addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
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
|
2023-11-10 06:28:21 +00:00
|
|
|
addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
|
|
|
|
addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
|
|
|
|
addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
votes.insert(NodeId.random(rng[]), addr1);
|
|
|
|
votes.insert(NodeId.random(rng[]), addr2);
|
|
|
|
|
|
|
|
for i in 0..<(threshold):
|
|
|
|
votes.insert(NodeId.random(rng[]), addr3);
|
|
|
|
|
2024-06-18 16:09:27 +00:00
|
|
|
check votes.majority() == Opt.some(addr3)
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
test "Double votes with same address":
|
|
|
|
const threshold = 2
|
|
|
|
|
|
|
|
var
|
|
|
|
votes = IpVote.init(threshold)
|
|
|
|
let
|
2023-11-10 06:28:21 +00:00
|
|
|
addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
|
|
|
|
addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-06-18 16:09:27 +00:00
|
|
|
check votes.majority() == Opt.some(addr2)
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
test "Double votes with different address":
|
|
|
|
const threshold = 2
|
|
|
|
|
|
|
|
var
|
|
|
|
votes = IpVote.init(threshold)
|
|
|
|
let
|
2023-11-10 06:28:21 +00:00
|
|
|
addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
|
|
|
|
addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
|
|
|
|
addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
|
2021-01-21 16:21:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-06-18 16:09:27 +00:00
|
|
|
check votes.majority() == Opt.some(addr3)
|