test_dht_schema.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import pytest
  2. from pydantic import BaseModel, StrictInt, conint
  3. from typing import Dict
  4. import hivemind
  5. from hivemind.dht import get_dht_time
  6. from hivemind.dht.node import DHTNode, LOCALHOST
  7. from hivemind.dht.schema import BytesWithPublicKey, SchemaValidator
  8. from hivemind.dht.validation import DHTRecord, RecordValidatorBase
  9. class SampleSchema(BaseModel):
  10. experiment_name: bytes
  11. n_batches: Dict[bytes, conint(ge=0, strict=True)]
  12. signed_data: Dict[BytesWithPublicKey, bytes]
  13. @pytest.fixture
  14. async def dht_nodes_with_schema():
  15. validator = SchemaValidator(SampleSchema)
  16. alice = await DHTNode.create(record_validator=validator)
  17. bob = await DHTNode.create(
  18. record_validator=validator, initial_peers=[f"{LOCALHOST}:{alice.port}"])
  19. return alice, bob
  20. @pytest.mark.forked
  21. @pytest.mark.asyncio
  22. async def test_expecting_regular_value(dht_nodes_with_schema):
  23. alice, bob = dht_nodes_with_schema
  24. # Regular value (bytes) expected
  25. assert await bob.store('experiment_name', b'foo_bar', get_dht_time() + 10)
  26. assert not await bob.store('experiment_name', 666, get_dht_time() + 10)
  27. assert not await bob.store('experiment_name', b'foo_bar', get_dht_time() + 10,
  28. subkey=b'subkey')
  29. # Refuse records despite https://pydantic-docs.helpmanual.io/usage/models/#data-conversion
  30. assert not await bob.store('experiment_name', [], get_dht_time() + 10)
  31. assert not await bob.store('experiment_name', [1, 2, 3], get_dht_time() + 10)
  32. for peer in [alice, bob]:
  33. assert (await peer.get('experiment_name', latest=True)).value == b'foo_bar'
  34. @pytest.mark.forked
  35. @pytest.mark.asyncio
  36. async def test_expecting_dictionary(dht_nodes_with_schema):
  37. alice, bob = dht_nodes_with_schema
  38. # Dictionary (bytes -> non-negative int) expected
  39. assert await bob.store('n_batches', 777, get_dht_time() + 10, subkey=b'uid1')
  40. assert await bob.store('n_batches', 778, get_dht_time() + 10, subkey=b'uid2')
  41. assert not await bob.store('n_batches', -666, get_dht_time() + 10, subkey=b'uid3')
  42. assert not await bob.store('n_batches', 666, get_dht_time() + 10)
  43. assert not await bob.store('n_batches', b'not_integer', get_dht_time() + 10, subkey=b'uid1')
  44. assert not await bob.store('n_batches', 666, get_dht_time() + 10, subkey=666)
  45. # Refuse storing a plain dictionary bypassing the DictionaryDHTValue convention
  46. assert not await bob.store('n_batches', {b'uid3': 779}, get_dht_time() + 10)
  47. # Refuse records despite https://pydantic-docs.helpmanual.io/usage/models/#data-conversion
  48. assert not await bob.store('n_batches', 779.5, get_dht_time() + 10, subkey=b'uid3')
  49. assert not await bob.store('n_batches', 779.0, get_dht_time() + 10, subkey=b'uid3')
  50. assert not await bob.store('n_batches', [], get_dht_time() + 10)
  51. assert not await bob.store('n_batches', [(b'uid3', 779)], get_dht_time() + 10)
  52. # Refuse records despite https://github.com/samuelcolvin/pydantic/issues/1268
  53. assert not await bob.store('n_batches', '', get_dht_time() + 10)
  54. for peer in [alice, bob]:
  55. dictionary = (await peer.get('n_batches', latest=True)).value
  56. assert (len(dictionary) == 2 and
  57. dictionary[b'uid1'].value == 777 and
  58. dictionary[b'uid2'].value == 778)
  59. @pytest.mark.forked
  60. @pytest.mark.asyncio
  61. async def test_expecting_public_keys(dht_nodes_with_schema):
  62. alice, bob = dht_nodes_with_schema
  63. # Subkeys expected to contain a public key
  64. # (so hivemind.dht.crypto.RSASignatureValidator would require a signature)
  65. assert await bob.store('signed_data', b'foo_bar', get_dht_time() + 10,
  66. subkey=b'uid[owner:public-key]')
  67. assert not await bob.store('signed_data', b'foo_bar', get_dht_time() + 10,
  68. subkey=b'uid-without-public-key')
  69. for peer in [alice, bob]:
  70. dictionary = (await peer.get('signed_data', latest=True)).value
  71. assert (len(dictionary) == 1 and
  72. dictionary[b'uid[owner:public-key]'].value == b'foo_bar')
  73. @pytest.mark.forked
  74. @pytest.mark.asyncio
  75. async def test_keys_outside_schema(dht_nodes_with_schema):
  76. class Schema(BaseModel):
  77. some_field: StrictInt
  78. class MergedSchema(BaseModel):
  79. another_field: StrictInt
  80. for allow_extra_keys in [False, True]:
  81. validator = SchemaValidator(Schema, allow_extra_keys=allow_extra_keys)
  82. assert validator.merge_with(SchemaValidator(MergedSchema, allow_extra_keys=False))
  83. alice = await DHTNode.create(record_validator=validator)
  84. bob = await DHTNode.create(
  85. record_validator=validator, initial_peers=[f"{LOCALHOST}:{alice.port}"])
  86. store_ok = await bob.store('unknown_key', b'foo_bar', get_dht_time() + 10)
  87. assert store_ok == allow_extra_keys
  88. for peer in [alice, bob]:
  89. result = await peer.get('unknown_key', latest=True)
  90. if allow_extra_keys:
  91. assert result.value == b'foo_bar'
  92. else:
  93. assert result is None
  94. @pytest.mark.forked
  95. @pytest.mark.asyncio
  96. async def test_prefix():
  97. class Schema(BaseModel):
  98. field: StrictInt
  99. validator = SchemaValidator(Schema, allow_extra_keys=False, prefix='prefix')
  100. alice = await DHTNode.create(record_validator=validator)
  101. bob = await DHTNode.create(
  102. record_validator=validator, initial_peers=[f"{LOCALHOST}:{alice.port}"])
  103. assert await bob.store('prefix_field', 777, get_dht_time() + 10)
  104. assert not await bob.store('prefix_field', 'string_value', get_dht_time() + 10)
  105. assert not await bob.store('field', 777, get_dht_time() + 10)
  106. for peer in [alice, bob]:
  107. assert (await peer.get('prefix_field', latest=True)).value == 777
  108. assert (await peer.get('field', latest=True)) is None
  109. @pytest.mark.forked
  110. @pytest.mark.asyncio
  111. async def test_merging_schema_validators(dht_nodes_with_schema):
  112. alice, bob = dht_nodes_with_schema
  113. class TrivialValidator(RecordValidatorBase):
  114. def validate(self, record: DHTRecord) -> bool:
  115. return True
  116. second_validator = TrivialValidator()
  117. # Can't merge with the validator of the different type
  118. assert not alice.protocol.record_validator.merge_with(second_validator)
  119. class SecondSchema(BaseModel):
  120. some_field: StrictInt
  121. another_field: str
  122. class ThirdSchema(BaseModel):
  123. another_field: StrictInt # Allow it to be a StrictInt as well
  124. for schema in [SecondSchema, ThirdSchema]:
  125. new_validator = SchemaValidator(schema, allow_extra_keys=False)
  126. for peer in [alice, bob]:
  127. assert peer.protocol.record_validator.merge_with(new_validator)
  128. assert await bob.store('experiment_name', b'foo_bar', get_dht_time() + 10)
  129. assert await bob.store('some_field', 777, get_dht_time() + 10)
  130. assert not await bob.store('some_field', 'string_value', get_dht_time() + 10)
  131. assert await bob.store('another_field', 42, get_dht_time() + 10)
  132. assert await bob.store('another_field', 'string_value', get_dht_time() + 10)
  133. # Unknown keys are allowed since the first schema is created with allow_extra_keys=True
  134. assert await bob.store('unknown_key', 999, get_dht_time() + 10)
  135. for peer in [alice, bob]:
  136. assert (await peer.get('experiment_name', latest=True)).value == b'foo_bar'
  137. assert (await peer.get('some_field', latest=True)).value == 777
  138. assert (await peer.get('another_field', latest=True)).value == 'string_value'
  139. assert (await peer.get('unknown_key', latest=True)).value == 999
  140. @pytest.mark.forked
  141. def test_sending_validator_instance_between_processes():
  142. alice = hivemind.DHT(start=True)
  143. bob = hivemind.DHT(start=True, initial_peers=[f"{LOCALHOST}:{alice.port}"])
  144. alice.add_validators([SchemaValidator(SampleSchema)])
  145. bob.add_validators([SchemaValidator(SampleSchema)])
  146. assert bob.store('experiment_name', b'foo_bar', get_dht_time() + 10)
  147. assert not bob.store('experiment_name', 777, get_dht_time() + 10)
  148. assert alice.get('experiment_name', latest=True).value == b'foo_bar'