nim-eth/tests/p2p/test_ip_vote.nim
Jacek Sieka 6bd6bae86c
Align core types with execution spec (#733)
Since these types were written, we've gained an executable spec:

https://github.com/ethereum/execution-specs

This PR aligns some of the types we use with this spec to simplify
comparisons and cross-referencing.

Using a `distinct` type is a tradeoff between nim ergonomics, type
safety and the ability to work around nim quirks and stdlib weaknesses.

In particular, it allows us to overload common functions such as `hash`
with correct and performant versions as well as maintain control over
string conversions etc at the cost of a little bit of ceremony when
instantiating them.

Apart from distinct byte types, `Hash32`, is introduced in lieu of the
existing `Hash256`, again aligning this commonly used type with the spec
which picks bytes rather than bits in the name.
2024-09-29 10:52:19 +02:00

109 lines
3.4 KiB
Nim

# 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.
{.used.}
import
std/net,
unittest2,
../../eth/common/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: 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))
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() == Opt.some(addr2)
test "Votes below threshold":
const threshold = 10
var
votes = IpVote.init(threshold)
let
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))
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: 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))
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() == Opt.some(addr3)
test "Double votes with same address":
const threshold = 2
var
votes = IpVote.init(threshold)
let
addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
addr2 = Address(ip: parseIpAddress("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() == Opt.some(addr2)
test "Double votes with different address":
const threshold = 2
var
votes = IpVote.init(threshold)
let
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))
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() == Opt.some(addr3)