diff --git a/tests/pubsub/testgossipinternal.nim b/tests/pubsub/testgossipinternal.nim index 777b479789..79a6f4eff3 100644 --- a/tests/pubsub/testgossipinternal.nim +++ b/tests/pubsub/testgossipinternal.nim @@ -662,6 +662,60 @@ suite "GossipSub internal": await allFuturesThrowing(conns.mapIt(it.close())) await gossipSub.switch.stop() + # test cases for block 5 gossibsub test plan + # check correctly parsed ihave/iwant/graft/prune/idontwant messages + # check value before & after decoding equal using protoc cmd tool for reference + asyncTest "Check RPCMsg encoding": + let backofftime = 10.uint64 + var id: seq[byte] = @[123] + let rpcMsg = RPCMsg( + control: some( + ControlMessage( + ihave: @[ControlIHave(topicID: "foobar", messageIDs: @[id])], + iwant: @[ControlIWant(messageIDs: @[id])], + graft: @[ControlGraft(topicID: "foobar")], + prune: @[ControlPrune(topicID: "foobar", backoff: backofftime)], + idontwant: @[ControlIWant(messageIDs: @[id])], + ) + ) + ) + let encodedExpected: seq[byte] = + @[ + 26, 51, 10, 10, 6, 102, 111, 111, 98, 97, 114, 18, 3, 49, 50, 51, 18, 5, 10, 3, + 49, 50, 51, 26, 8, 10, 6, 102, 111, 111, 98, 97, 114, 34, 10, 10, 6, 102, 111, + 111, 98, 97, 114, 16, 10, 42, 5, 10, 3, 49, 50, 51, + ] #encoded using protoc cmd tool + + let encodedMsg = encodeRpcMsg(rpcMsg, true) + check: + encodedExpected == encodedMsg + + asyncTest "Check RPCMsg decoding": + let backofftime = 12.uint64 + let id: seq[byte] = @[1] + let originMessage = RPCMsg( + control: some( + ControlMessage( + ihave: @[ControlIHave(topicID: "foobar", messageIDs: @[id])], + iwant: @[ControlIWant(messageIDs: @[id])], + graft: @[ControlGraft(topicID: "topic")], + prune: @[ControlPrune(topicID: "new", backoff: backofftime)], + idontwant: @[ControlIWant(messageIDs: @[id])], + ) + ) + ) + #data encoded using protoc cmd tool + let encodedMsg: seq[byte] = + @[ + 26, 41, 10, 11, 10, 6, 102, 111, 111, 98, 97, 114, 18, 1, 49, 18, 3, 10, 1, 49, + 26, 7, 10, 5, 116, 111, 112, 105, 99, 34, 7, 10, 3, 110, 101, 119, 16, 12, 42, + 3, 10, 1, 49, + ] + + var rpcMsg = decodeRpcMsg(encodedMsg).value + check: + rpcMsg == originMessage + asyncTest "handleIHave/Iwant tests": let gossipSub = TestGossipSub.init(newStandardSwitch())