Skip to content

Commit 551c987

Browse files
committed
Add state key for LQT tallying
1 parent e2c15ef commit 551c987

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

crates/core/component/funding/src/component/state_key.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,60 @@ pub mod lqt {
1212
format!("funding/lqt/v1/nullifier/{epoch_index:020}/lookup/{nullifier}")
1313
}
1414
}
15+
16+
pub mod votes {
17+
use penumbra_sdk_asset::asset;
18+
use penumbra_sdk_keys::{address::ADDRESS_LEN_BYTES, Address};
19+
20+
const PART0: &'static str = "funding/lqt/v1/votes/";
21+
const EPOCH_LEN: usize = 20;
22+
const PART1: &'static str = "/by_asset/";
23+
const PREFIX_LEN: usize = PART0.len() + EPOCH_LEN + PART1.len();
24+
25+
/// A prefix for accessing the votes in a given epoch, c.f. [`power_asset_address`];
26+
pub(crate) fn prefix(epoch_index: u64) -> [u8; PREFIX_LEN] {
27+
let mut bytes = [0u8; PREFIX_LEN];
28+
29+
let rest = &mut bytes;
30+
let (bytes_part0, rest) = rest.split_at_mut(PART0.len());
31+
let (bytes_epoch_index, bytes_part1) = rest.split_at_mut(EPOCH_LEN);
32+
33+
bytes_part0.copy_from_slice(PART0.as_bytes());
34+
bytes_epoch_index
35+
.copy_from_slice(format!("{epoch_index:0w$}", w = EPOCH_LEN).as_bytes());
36+
bytes_part1.copy_from_slice(PART1.as_bytes());
37+
38+
bytes
39+
}
40+
41+
const ASSET_LEN: usize = 32;
42+
const POWER_LEN: usize = 8;
43+
const RECEIPT_LEN: usize = PREFIX_LEN + ASSET_LEN + POWER_LEN + ADDRESS_LEN_BYTES;
44+
45+
#[allow(dead_code)] // TODO: remove once used
46+
/// When present, indicates that an address voted for a particular asset, with a given power.
47+
///
48+
/// To get the values ordered by descending voting power, use [`prefix`];
49+
pub(crate) fn receipt(
50+
epoch_index: u64,
51+
asset: asset::Id,
52+
power: u64,
53+
voter: &Address,
54+
) -> [u8; RECEIPT_LEN] {
55+
let mut bytes = [0u8; RECEIPT_LEN];
56+
57+
let rest = &mut bytes;
58+
let (bytes_prefix, rest) = rest.split_at_mut(PREFIX_LEN);
59+
let (bytes_asset, rest) = rest.split_at_mut(ASSET_LEN);
60+
let (bytes_power, bytes_voter) = rest.split_at_mut(POWER_LEN);
61+
62+
bytes_prefix.copy_from_slice(&prefix(epoch_index));
63+
bytes_asset.copy_from_slice(&asset.to_bytes());
64+
bytes_power.copy_from_slice(&((!power).to_be_bytes()));
65+
bytes_voter.copy_from_slice(&voter.to_vec());
66+
67+
bytes
68+
}
69+
}
1570
}
1671
}

0 commit comments

Comments
 (0)