Skip to content

Commit 3ba2f6f

Browse files
authored
Merge pull request #2 from opentensor/john/improve-reveal-round
improve reveal round calculation
2 parents c38c260 + 7ac10fd commit 3ba2f6f

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

Diff for: src/lib.rs

+61-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use tle::{
1313
use tokio;
1414
use w3f_bls::EngineBLS;
1515

16+
pub const SUBTENSOR_PULSE_DELAY: u64 = 24;
17+
1618
#[derive(Encode)]
1719
pub struct WeightsTlockPayload {
1820
pub uids: Vec<u16>,
@@ -24,9 +26,11 @@ async fn generate_commit(
2426
uids: Vec<u16>,
2527
values: Vec<u16>,
2628
version_key: u64,
29+
tempo: u64,
30+
current_block: u64,
31+
netuid: u16,
2732
subnet_reveal_period_epochs: u64,
2833
block_time: u64,
29-
tempo: u64,
3034
) -> Result<(Vec<u8>, u64), (std::io::Error, String)> {
3135
// Steps comes from here https://github.com/opentensor/subtensor/pull/982/files#diff-7261bf1c7f19fc66a74c1c644ec2b4b277a341609710132fb9cd5f622350a6f5R120-R131
3236
// 1 Instantiate payload
@@ -50,18 +54,45 @@ async fn generate_commit(
5054
.unwrap()
5155
.as_secs();
5256

53-
let current_round = (now - genesis_time) / period;
54-
// tempo is amount of blocks in 1 epoch
55-
// block_time is length the block in seconds
56-
// subnet_reveal_period_epochs means after how many epochs to make a commit reveal
57-
let delay_seconds = tempo * block_time * subnet_reveal_period_epochs;
58-
let rounds_to_wait = (delay_seconds + period - 1) / period;
59-
let reveal_round = current_round + rounds_to_wait;
57+
// Compute the current epoch index
58+
let tempo_plus_one = tempo + 1;
59+
let netuid_plus_one = (netuid as u64) + 1;
60+
let block_with_offset = current_block + netuid_plus_one;
61+
let current_epoch = block_with_offset / tempo_plus_one;
62+
63+
// Compute the reveal epoch
64+
let reveal_epoch = current_epoch + subnet_reveal_period_epochs;
65+
66+
// Compute the block number when the reveal epoch starts
67+
let reveal_block_number = reveal_epoch * tempo_plus_one - netuid_plus_one;
68+
69+
// Compute the number of blocks until the reveal epoch
70+
let blocks_until_reveal = reveal_block_number.saturating_sub(current_block);
71+
72+
// Compute the time until the reveal in seconds
73+
let time_until_reveal = blocks_until_reveal * block_time;
6074

61-
// 3 Encrypt
62-
let pub_key_bytes = hex::decode(public_key).expect("Decoding failed");
75+
// Compute the reveal time in seconds since UNIX_EPOCH
76+
let reveal_time = now + time_until_reveal;
77+
78+
// Compute the reveal round, ensuring we round up
79+
let reveal_round = ((reveal_time - genesis_time + period - 1) / period) - SUBTENSOR_PULSE_DELAY;
80+
81+
// 3. Deserialize the public key
82+
let pub_key_bytes = hex::decode(public_key).map_err(|e| {
83+
(
84+
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("{:?}", e)),
85+
"Decoding public key failed.".to_string(),
86+
)
87+
})?;
6388
let pub_key =
64-
<TinyBLS381 as EngineBLS>::PublicKeyGroup::deserialize_compressed(&*pub_key_bytes).unwrap();
89+
<TinyBLS381 as EngineBLS>::PublicKeyGroup::deserialize_compressed(&*pub_key_bytes)
90+
.map_err(|e| {
91+
(
92+
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("{:?}", e)),
93+
"Deserializing public key failed.".to_string(),
94+
)
95+
})?;
6596

6697
// 4 Create identity
6798
let message = {
@@ -80,30 +111,38 @@ async fn generate_commit(
80111
identity,
81112
OsRng,
82113
)
83-
.map_err(|_| PyErr::new::<PyValueError, _>("Encryption failed."))
84-
.unwrap();
114+
.map_err(|e| {
115+
(
116+
std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", e)),
117+
"Encryption failed.".to_string(),
118+
)
119+
})?;
85120

86121
// 6. Compress ct
87122
let mut ct_bytes: Vec<u8> = Vec::new();
88-
89-
ct.serialize_compressed(&mut ct_bytes)
90-
.map_err(|_| PyErr::new::<PyValueError, _>("Ciphertext serialization failed."))
91-
.unwrap();
123+
ct.serialize_compressed(&mut ct_bytes).map_err(|e| {
124+
(
125+
std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", e)),
126+
"Ciphertext serialization failed.".to_string(),
127+
)
128+
})?;
92129

93130
// 7. Return result
94131
Ok((ct_bytes, reveal_round))
95132
}
96133

97134
#[pyfunction]
98-
#[pyo3(signature = (uids, weights, version_key, subnet_reveal_period_epochs=1, block_time=12, tempo=360))]
135+
#[pyo3(signature = (uids, weights, version_key, tempo, current_block, netuid, subnet_reveal_period_epochs, block_time=12))]
99136
fn get_encrypted_commit(
100137
py: Python,
101138
uids: Vec<u16>,
102139
weights: Vec<u16>,
103140
version_key: u64,
141+
tempo: u64,
142+
current_block: u64,
143+
netuid: u16,
104144
subnet_reveal_period_epochs: u64,
105145
block_time: u64,
106-
tempo: u64,
107146
) -> PyResult<(Py<PyBytes>, u64)> {
108147
// create runtime to make async call
109148
let runtime =
@@ -112,9 +151,11 @@ fn get_encrypted_commit(
112151
uids,
113152
weights,
114153
version_key,
154+
tempo,
155+
current_block,
156+
netuid,
115157
subnet_reveal_period_epochs,
116158
block_time,
117-
tempo,
118159
));
119160
// matching the result
120161
match result {

0 commit comments

Comments
 (0)