From 16c96bd8c018c7981c1096bf1ddfef5b3cc74616 Mon Sep 17 00:00:00 2001 From: munna0908 Date: Wed, 18 Dec 2024 15:01:18 +0530 Subject: [PATCH] fix unit and integration tests --- tests/codex/slots/testbackendfactory.nim | 15 +++++--- tests/codex/slots/testprover.nim | 7 ++-- tests/codex/testnat.nim | 46 +++++++++++++++++++++++ tests/contracts/testDeployment.nim | 10 +++-- tests/integration/multinodes.nim | 3 +- tests/integration/testblockexpiration.nim | 3 +- tests/testTaiko.nim | 6 +-- 7 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 tests/codex/testnat.nim diff --git a/tests/codex/slots/testbackendfactory.nim b/tests/codex/slots/testbackendfactory.nim index 669fb03b..2889fc82 100644 --- a/tests/codex/slots/testbackendfactory.nim +++ b/tests/codex/slots/testbackendfactory.nim @@ -47,8 +47,9 @@ suite "Test BackendFactory": let config = CodexConf( cmd: StartUpCmd.persistence, - nat: ValidIpAddress.init("127.0.0.1"), - discoveryIp: ValidIpAddress.init(IPv4_any()), + nat: NatConfig( + hasExtIp: false, + nat: NatNone), metricsAddress: ValidIpAddress.init("127.0.0.1"), persistenceCmd: PersistenceCmd.prover, marketplaceAddress: EthAddress.example.some, @@ -68,8 +69,9 @@ suite "Test BackendFactory": let config = CodexConf( cmd: StartUpCmd.persistence, - nat: ValidIpAddress.init("127.0.0.1"), - discoveryIp: ValidIpAddress.init(IPv4_any()), + nat: NatConfig( + hasExtIp: false, + nat: NatNone), metricsAddress: ValidIpAddress.init("127.0.0.1"), persistenceCmd: PersistenceCmd.prover, marketplaceAddress: EthAddress.example.some, @@ -90,8 +92,9 @@ suite "Test BackendFactory": let config = CodexConf( cmd: StartUpCmd.persistence, - nat: ValidIpAddress.init("127.0.0.1"), - discoveryIp: ValidIpAddress.init(IPv4_any()), + nat: NatConfig( + hasExtIp: false, + nat: NatNone), metricsAddress: ValidIpAddress.init("127.0.0.1"), persistenceCmd: PersistenceCmd.prover, marketplaceAddress: EthAddress.example.some, diff --git a/tests/codex/slots/testprover.nim b/tests/codex/slots/testprover.nim index ea47d93a..ba947921 100644 --- a/tests/codex/slots/testprover.nim +++ b/tests/codex/slots/testprover.nim @@ -19,7 +19,7 @@ import pkg/codex/conf import pkg/confutils/defs import pkg/poseidon2/io import pkg/codex/utils/poseidon2digest - +import pkg/codex/nat import ./helpers import ../helpers import ./backends/helpers @@ -43,8 +43,9 @@ suite "Test Prover": metaDs = metaTmp.newDb() config = CodexConf( cmd: StartUpCmd.persistence, - nat: ValidIpAddress.init("127.0.0.1"), - discoveryIp: ValidIpAddress.init(IPv4_any()), + nat: NatConfig( + hasExtIp: false, + nat: NatNone), metricsAddress: ValidIpAddress.init("127.0.0.1"), persistenceCmd: PersistenceCmd.prover, circomR1cs: InputFile("tests/circuits/fixtures/proof_main.r1cs"), diff --git a/tests/codex/testnat.nim b/tests/codex/testnat.nim new file mode 100644 index 00000000..95392561 --- /dev/null +++ b/tests/codex/testnat.nim @@ -0,0 +1,46 @@ +import std/[unittest, options, net],stew/shims/net as stewNet +import pkg/chronos +import pkg/libp2p/[multiaddress, multihash, multicodec] +import pkg/stew/results + +import ../../codex/nat +import ../../codex/utils/natutils +import ../../codex/utils + + +suite "NAT Address Tests": + test "natedAddress with local addresses": + # Setup test data + let + udpPort = Port(1234) + natConfig = NatConfig( + hasExtIp: true, + extIp:ValidIpAddress.init("8.8.8.8")) + + # Create test addresses + localAddr = MultiAddress.init("/ip4/127.0.0.1/tcp/5000").expect("valid multiaddr") + anyAddr = MultiAddress.init("/ip4/0.0.0.0/tcp/5000").expect("valid multiaddr") + publicAddr = MultiAddress.init("/ip4/192.168.1.1/tcp/5000").expect("valid multiaddr") + + # Expected results + let + expectedDiscoveryAddrs = @[ + MultiAddress.init("/ip4/8.8.8.8/udp/1234").expect("valid multiaddr"), + MultiAddress.init("/ip4/8.8.8.8/udp/1234").expect("valid multiaddr"), + MultiAddress.init("/ip4/8.8.8.8/udp/1234").expect("valid multiaddr") + ] + expectedlibp2pAddrs = @[ + MultiAddress.init("/ip4/8.8.8.8/tcp/5000").expect("valid multiaddr"), + MultiAddress.init("/ip4/8.8.8.8/tcp/5000").expect("valid multiaddr"), + MultiAddress.init("/ip4/8.8.8.8/tcp/5000").expect("valid multiaddr") + ] + + #ipv6Addr = MultiAddress.init("/ip6/::1/tcp/5000").expect("valid multiaddr") + addrs = @[localAddr, anyAddr, publicAddr] + + # Test address remapping + let (libp2pAddrs,discoveryAddrs) = natedAddress(natConfig, addrs, udpPort) + + # Verify results + check(discoveryAddrs == expectedDiscoveryAddrs) + check(libp2pAddrs == expectedlibp2pAddrs) \ No newline at end of file diff --git a/tests/contracts/testDeployment.nim b/tests/contracts/testDeployment.nim index f89e28a8..665829e6 100644 --- a/tests/contracts/testDeployment.nim +++ b/tests/contracts/testDeployment.nim @@ -15,15 +15,17 @@ method getChainId*(provider: MockProvider): Future[UInt256] {.async: (raises:[Pr proc configFactory(): CodexConf = CodexConf( cmd: StartUpCmd.persistence, - nat: ValidIpAddress.init("127.0.0.1"), - discoveryIp: ValidIpAddress.init(IPv4_any()), + nat: NatConfig( + hasExtIp: false, + nat: NatNone), metricsAddress: ValidIpAddress.init("127.0.0.1")) proc configFactory(marketplace: Option[EthAddress]): CodexConf = CodexConf( cmd: StartUpCmd.persistence, - nat: ValidIpAddress.init("127.0.0.1"), - discoveryIp: ValidIpAddress.init(IPv4_any()), + nat: NatConfig( + hasExtIp: false, + nat: NatNone), metricsAddress: ValidIpAddress.init("127.0.0.1"), marketplaceAddress: marketplace) diff --git a/tests/integration/multinodes.nim b/tests/integration/multinodes.nim index c54a0a49..ecf8e9d1 100644 --- a/tests/integration/multinodes.nim +++ b/tests/integration/multinodes.nim @@ -166,9 +166,8 @@ template multinodesuite*(name: string, body: untyped) = config.addCliOption("--bootstrap-node", bootstrapNode) config.addCliOption("--api-port", $ await nextFreePort(8080 + nodeIdx)) config.addCliOption("--data-dir", datadir) - config.addCliOption("--nat", "127.0.0.1") + config.addCliOption("--nat", "none") config.addCliOption("--listen-addrs", "/ip4/127.0.0.1/tcp/0") - config.addCliOption("--disc-ip", "127.0.0.1") config.addCliOption("--disc-port", $ await nextFreePort(8090 + nodeIdx)) except CodexConfigError as e: diff --git a/tests/integration/testblockexpiration.nim b/tests/integration/testblockexpiration.nim index 3ad9e4f2..e1d38a03 100644 --- a/tests/integration/testblockexpiration.nim +++ b/tests/integration/testblockexpiration.nim @@ -27,9 +27,8 @@ ethersuite "Node block expiration tests": node = await CodexProcess.startNode(@[ "--api-port=8080", "--data-dir=" & dataDir, - "--nat=127.0.0.1", + "--nat=none", "--listen-addrs=/ip4/127.0.0.1/tcp/0", - "--disc-ip=127.0.0.1", "--disc-port=8090", "--block-ttl=" & $blockTtlSeconds, "--block-mi=1", diff --git a/tests/testTaiko.nim b/tests/testTaiko.nim index a799697b..c0a48396 100644 --- a/tests/testTaiko.nim +++ b/tests/testTaiko.nim @@ -21,8 +21,7 @@ suite "Taiko L2 Integration Tests": node1 = startNode([ "--data-dir=" & createTempDir("", ""), "--api-port=8080", - "--nat=127.0.0.1", - "--disc-ip=127.0.0.1", + "--nat=none", "--disc-port=8090", "--persistence", "--eth-provider=https://rpc.test.taiko.xyz" @@ -34,8 +33,7 @@ suite "Taiko L2 Integration Tests": node2 = startNode([ "--data-dir=" & createTempDir("", ""), "--api-port=8081", - "--nat=127.0.0.1", - "--disc-ip=127.0.0.1", + "--nat=none", "--disc-port=8091", "--bootstrap-node=" & bootstrap, "--persistence",