How To Use
In the following example, the Hypertensor Predicate Validator is used to ensure only specific keys can be stored, when the keys can be stored, how long they can be stored for, and verifying values using Pydantic schemas.
Heartbeat
Nodes make periodic updates to the database to show they are still running the node with a maximum expiration time, and can only be stored under the 2 roles, "hoster" and "validator".
Commit-Reveal Scheme
Each commit-reveal key is specified on the current epoch. A node cannot copy other nodes after the commit phase, and nodes cannot submit data on previous or future epochs.
Pydantic Schema
On each epoch, a validator submits a random tensor for all of the hosters to run inference on. The value of this must be a verified torch tensor, and also stored between the allowable epoch progress period.
If a node attempts to store data that doesn't abide by these conditions, it will not be stored.
def hypertensor_consensus_predicate() -> Callable[[DHTRecord], bool]:
def predicate(record: DHTRecord, type: DHTRequestType, epoch_data: EpochData) -> bool:
try:
# Enable GET data at any time
if type is DHTRequestType.GET:
return True
current_epoch = epoch_data.epoch
percent_complete = epoch_data.percent_complete
# Ensure the keys are valid for the current allowable keys or epoch allowable keys
valid_keys = {
# Heartbeat
DHTID.generate(source="hoster").to_bytes(): "hoster",
# Heartbeat
DHTID.generate(source="validator").to_bytes(): "validator",
# ⸺ 0-15%
DHTID.generate(source=f"consensus-epoch_{current_epoch}").to_bytes(): "consensus",
# ⸺ 15-50%
DHTID.generate(source=f"validator-reveal_epoch_{current_epoch}").to_bytes(): "validator-reveal",
# ⸺ 15-50%
DHTID.generate(source=f"hoster-commit_epoch_{current_epoch}").to_bytes(): "hoster-commit",
# ⸺ 50-60%
DHTID.generate(source=f"hoster-reveal_epoch_{current_epoch}").to_bytes(): "hoster-reveal",
# ⸺ 60-100% - Reveals the n-1 epoch commit (stores in the current)
DHTID.generate(source=f"validator-commit_epoch_{current_epoch}").to_bytes(): "validator-commit",
}
key_type = valid_keys.get(record.key, None)
if key_type is None:
return False
dht_time = get_dht_time()
if key_type == "hoster":
max_expiration = dht_time + MAX_HEART_BEAT_TIME
if record.expiration_time > max_expiration:
return False
# TODO: validate proof-of-stake on each heartbeat (redundant)
return True
elif key_type == "validator":
max_expiration = dht_time + MAX_HEART_BEAT_TIME
if record.expiration_time > max_expiration:
return False
# TODO: validate proof-of-stake on each heartbeat (redundant)
return True
# ⸺ 0-15%
elif key_type == "consensus":
# Must be submitted before deadline
if percent_complete > CONSENSUS_STORE_DEADLINE:
return False
max_expiration = dht_time + MAX_CONSENSUS_TIME
if record.expiration_time > max_expiration:
return False
try:
loaded = MSGPackSerializer.loads(record.value)
tensor = torch.load(io.BytesIO(loaded), weights_only=False)
ConsensusTensorModel(tensor=tensor)
return True
except ValidationError:
return False
# ⸺ 15-50%
elif key_type == "validator-reveal":
max_expiration = dht_time + MAX_VALIDATOR_REVEAL_TIME
if record.expiration_time > max_expiration:
return False
if percent_complete <= VALIDATOR_REVEAL_START and percent_complete > VALIDATOR_REVEAL_DEADLINE:
return False
return True
# ⸺ 15-50%
elif key_type == "hoster-commit":
max_expiration = dht_time + MAX_HOSTER_COMMIT_TIME
if record.expiration_time > max_expiration:
return False
if percent_complete <= CONSENSUS_STORE_DEADLINE and percent_complete > HOSTER_COMMIT_DEADLINE:
return False
return True
# ⸺ 50-60%
elif key_type == "hoster-reveal":
max_expiration = dht_time + MAX_HOSTER_REVEAL_TIME
if record.expiration_time > max_expiration:
return False
if percent_complete <= HOSTER_COMMIT_DEADLINE and percent_complete > HOSTER_REVEAL_DEADLINE:
return False
return True
# ⸺ 60-100%
elif key_type == "validator-commit":
max_expiration = dht_time + MAX_VALIDATOR_COMMIT_TIME
if record.expiration_time > max_expiration:
return False
if percent_complete < VALIDATOR_COMMIT_START:
return False
return True
return False # Key doesn't match any known schema
except Exception as e:
print(f"Predicate Err: {e}")
return False
return predicate
Last updated