cfx_executor/internal_contract/impls/
context.rs

1use primitives::{block::BlockHeight, BlockNumber};
2use sha3_macro::keccak;
3
4const BLOCK_HASHES_START_SLOT: [u8; 32] = {
5    let mut hash = keccak!("CIP_133_BLOCK_HASHES_START_SLOT");
6    hash[30] = 0;
7    hash[31] = 0;
8    hash
9};
10
11const EPOCH_HASHES_START_SLOT: [u8; 32] = {
12    let mut hash = keccak!("CIP_133_EPOCH_HASHES_START_SLOT");
13    hash[30] = 0;
14    hash[31] = 0;
15    hash
16};
17
18pub const fn block_hash_slot(number: BlockNumber) -> [u8; 32] {
19    let mut answer = BLOCK_HASHES_START_SLOT;
20    let r = ((number & 0xffff) as u16).to_be_bytes();
21    answer[30] = r[0];
22    answer[31] = r[1];
23    answer
24}
25
26pub const fn epoch_hash_slot(height: BlockHeight) -> [u8; 32] {
27    let mut answer = EPOCH_HASHES_START_SLOT;
28    let r = ((height & 0xffff) as u16).to_be_bytes();
29    answer[30] = r[0];
30    answer[31] = r[1];
31    answer
32}