Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(gossipsub): block5 protobuf test cases #1204

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/pubsub/testgossipinternal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,50 @@ suite "GossipSub internal":
await allFuturesThrowing(conns.mapIt(it.close()))
await gossipSub.switch.stop()

# test cases for block 5 gossibsub test plan
# tests 1 -4 for checking the formatting of objects
# have - want - graft - prune

asyncTest "Check ControlIHave formatting":
let topic = "dummytopic"

let msgID = @[0'u8, 1, 2, 3]
let msg = ControlIHave(topicID: topic, messageIDs: @[msgID])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are these tests for? they're effectively testing that nim object initialization works, and nothing beyond that since they don't exercise any actual logic such as encoding to and from protobuf. As such, they add maintenance and code volume without providing any value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I add the checks of objects parameters to an already existing test case to reduce code volume would it be acceptable ?

the reason of these checks is to verify that message objects are defined with respect to protobuf schema

Copy link
Contributor

@arnetheduck arnetheduck Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to test schema conformance, you need to create encoded byte streams and decode these messages into the nim types that we use. You can create such messages using https://www.tutorialspoint.com/protobuf/protobuf_command_line_usage.htm as long as it's possible to regenerate the tests in a reasonable way - ie the aim of such a test is to ensure that encoding and decoding of the nim types results in the expected protobuf bytes (and that the test remain maintainable, ie if someone comes across the code in 5 months and wants to add a test, this is reasonably simple)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to structure such tests can be seen in https://github.com/status-im/nim-eth/blob/master/tests/common/test_common.nim - this tests RLP (a different encoding) but premise is the same - there, you can see how example content is loaded and cross-verified against a "decoded" version of the same message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we create the instances, encode, decode, and check the decoded instance is the same as the original instance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's called a roundtrip test, which broadly are less valuable / useful - it tests that the encoder is compatible with the decoder, with the risk that they both share the same bug and therefore remain incompatible with the schema.

When testing conformance to specifications, it's better to work with canonical examples based on the spec that have been generated independently using a reference implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question then what is the proper max time for the encoding/decoding as I couldn't find it in the specs

check:
msg.topicID == topic
msg.messageIDs == @[msgID]

asyncTest "Check ControlIWant formatting":
let msgID = @[0'u8, 1, 2, 3]
let msg = ControlIWant(messageIDs: @[msgID])
check:
msg.messageIDs == @[msgID]

asyncTest "Check ControGraft formatting":
let topic = "dummytopic"
let msg = ControlGraft(topicID: topic)
check:
msg.topicID == topic

asyncTest "Check ControPrune":
let topic = "foobar"
#var peerecord:seq[bytes]
var
peerecord: seq[byte] = @[1, 2, 3]
peerData: seq[byte] = @[4, 5]
backoff: uint64 = 123
peerInfo = PeerInfoMsg(
peerId: PeerId(data: @['e'.byte]), # 1 byte
signedPeerRecord: @['f'.byte, 'g'.byte] # 2 bytes
,
)

let msg = ControlPrune(topicID: topic, peers: @[peerInfo], backoff: backoff)
check:
msg.topicID == topic
msg.peers.contains(peerInfo)
msg.backoff == backoff

asyncTest "handleIHave/Iwant tests":
let gossipSub = TestGossipSub.init(newStandardSwitch())

Expand Down
Loading