Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_version_timestamp.py
Show All 17 Lines | |||||
NODE0_TIME = TIME_GENESIS_BLOCK + 1337 | NODE0_TIME = TIME_GENESIS_BLOCK + 1337 | ||||
class ModifiedVersionTimestampP2PInterface(P2PInterface): | class ModifiedVersionTimestampP2PInterface(P2PInterface): | ||||
def __init__(self, timestamp): | def __init__(self, timestamp): | ||||
super().__init__() | super().__init__() | ||||
self.versionTimestamp = timestamp | self.versionTimestamp = timestamp | ||||
def peer_connect(self, *args, services=NODE_NETWORK, | def peer_connect(self, *args, services=NODE_NETWORK, send_version=False, **kwargs): | ||||
send_version=False, **kwargs): | |||||
create_conn = super().peer_connect(*args, send_version=send_version, **kwargs) | create_conn = super().peer_connect(*args, send_version=send_version, **kwargs) | ||||
# Send version message with invalid timestamp | # Send version message with invalid timestamp | ||||
vt = msg_version() | vt = msg_version() | ||||
vt.nVersion = P2P_VERSION | vt.nVersion = P2P_VERSION | ||||
vt.strSubVer = P2P_SUBVERSION | vt.strSubVer = P2P_SUBVERSION | ||||
vt.nTime = self.versionTimestamp | vt.nTime = self.versionTimestamp | ||||
Show All 19 Lines | class VersionTimestampTest(BitcoinTestFramework): | ||||
def run_test(self): | def run_test(self): | ||||
# Set a deterministic time so that we can check the time offset with other | # Set a deterministic time so that we can check the time offset with other | ||||
# nodes is as expected | # nodes is as expected | ||||
self.nodes[0].setmocktime(NODE0_TIME) | self.nodes[0].setmocktime(NODE0_TIME) | ||||
self.log.info("Check some invalid timestamp in the version message") | self.log.info("Check some invalid timestamp in the version message") | ||||
with self.nodes[0].assert_debug_log( | with self.nodes[0].assert_debug_log( | ||||
expected_msgs=["Added connection peer=0", | expected_msgs=[ | ||||
"Ignoring invalid timestamp in version message"]): | "Added connection peer=0", | ||||
"Ignoring invalid timestamp in version message", | |||||
] | |||||
): | |||||
self.nodes[0].add_p2p_connection( | self.nodes[0].add_p2p_connection( | ||||
ModifiedVersionTimestampP2PInterface(-9223372036854775807), | ModifiedVersionTimestampP2PInterface(-9223372036854775807), | ||||
send_version=False) | send_version=False, | ||||
) | |||||
self.log.info("Check invalid side of the timestamp boundary") | self.log.info("Check invalid side of the timestamp boundary") | ||||
with self.nodes[0].assert_debug_log( | with self.nodes[0].assert_debug_log( | ||||
expected_msgs=["Added connection peer=1", | expected_msgs=[ | ||||
"Ignoring invalid timestamp in version message"]): | "Added connection peer=1", | ||||
"Ignoring invalid timestamp in version message", | |||||
] | |||||
): | |||||
self.nodes[0].add_p2p_connection( | self.nodes[0].add_p2p_connection( | ||||
ModifiedVersionTimestampP2PInterface(TIME_GENESIS_BLOCK - 1), | ModifiedVersionTimestampP2PInterface(TIME_GENESIS_BLOCK - 1), | ||||
send_version=False) | send_version=False, | ||||
) | |||||
self.log.info("Check valid side of the timestamp boundary (genesis timestamp)") | self.log.info("Check valid side of the timestamp boundary (genesis timestamp)") | ||||
# Outbound connection: the timestamp is used for our adjusted time | # Outbound connection: the timestamp is used for our adjusted time | ||||
self.nodes[1].setmocktime(TIME_GENESIS_BLOCK) | self.nodes[1].setmocktime(TIME_GENESIS_BLOCK) | ||||
with self.nodes[0].assert_debug_log( | with self.nodes[0].assert_debug_log( | ||||
expected_msgs=["Added connection peer=2", "added time data"], | expected_msgs=["Added connection peer=2", "added time data"], | ||||
unexpected_msgs=["Ignoring invalid timestamp in version message"]): | unexpected_msgs=["Ignoring invalid timestamp in version message"], | ||||
): | |||||
self.connect_nodes(0, 1) | self.connect_nodes(0, 1) | ||||
# This check verifies that the mocktime was indeed used in the VERSION message | # This check verifies that the mocktime was indeed used in the VERSION message | ||||
assert_equal(self.nodes[0].getpeerinfo()[2]["timeoffset"], | assert_equal( | ||||
TIME_GENESIS_BLOCK - NODE0_TIME) | self.nodes[0].getpeerinfo()[2]["timeoffset"], | ||||
TIME_GENESIS_BLOCK - NODE0_TIME, | |||||
) | |||||
# Inbound connection: the timestamp is ignored | # Inbound connection: the timestamp is ignored | ||||
with self.nodes[0].assert_debug_log( | with self.nodes[0].assert_debug_log( | ||||
expected_msgs=["Added connection peer=3"], | expected_msgs=["Added connection peer=3"], | ||||
unexpected_msgs=["Ignoring invalid timestamp in version message", | unexpected_msgs=[ | ||||
"added time data"]): | "Ignoring invalid timestamp in version message", | ||||
"added time data", | |||||
], | |||||
): | |||||
self.nodes[0].add_p2p_connection( | self.nodes[0].add_p2p_connection( | ||||
ModifiedVersionTimestampP2PInterface(TIME_GENESIS_BLOCK), | ModifiedVersionTimestampP2PInterface(TIME_GENESIS_BLOCK), | ||||
send_version=False) | send_version=False, | ||||
) | |||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
VersionTimestampTest().main() | VersionTimestampTest().main() |