Skip to content

Commit

Permalink
Merge pull request #59 from HerodotusDev/chore/chain_id
Browse files Browse the repository at this point in the history
1 -  adds chain id to datalake
  • Loading branch information
Okm165 authored Aug 6, 2024
2 parents 40afd54 + 834335a commit 4e00d58
Show file tree
Hide file tree
Showing 57 changed files with 2,833 additions and 2,685 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ hdp/
*.memory
*.trace

src/hdp_input.json

# Whitelist - these files should not be ignored
!tests/cairo_programs/fixtures/*.json
!tools/js/package.json
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ run-pie:

get-program-hash:
@echo "Getting hdp.cairo program's hash..."
cairo-compile ./src/hdp.cairo --output $(BUILD_DIR)/hdp.json
cairo-compile --cairo_path="packages/eth_essentials" "src/hdp.cairo" --output $(BUILD_DIR)/hdp.json
cairo-hash-program --program $(BUILD_DIR)/hdp.json
@echo "Program hash retrieved."

Expand Down
1 change: 1 addition & 0 deletions cairo/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod memorizer;
pub struct HDP {
pub header_memorizer: Memorizer,
pub account_memorizer: Memorizer,
pub storage_memorizer: Memorizer,
}

#[derive(Serde, Drop)]
Expand Down
1 change: 1 addition & 0 deletions cairo/src/memorizer.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod header_memorizer;
pub mod account_memorizer;
pub mod storage_memorizer;
59 changes: 55 additions & 4 deletions cairo/src/memorizer/account_memorizer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use hdp_cairo::Memorizer;
use starknet::syscalls::call_contract_syscall;
use starknet::{SyscallResult, SyscallResultTrait};

const ACCOUNT_MEMORIZER_ID: felt252 = 0x1;
const ACCOUNT_MEMORIZER: felt252 = 1;

const ACCOUNT_MEMORIZER_GET_BALANCE_ID: felt252 = 0x0;
const ACCOUNT_MEMORIZER_GET_NONCE: felt252 = 0;
const ACCOUNT_MEMORIZER_GET_BALANCE: felt252 = 1;
const ACCOUNT_MEMORIZER_GET_STATE_ROOT: felt252 = 2;
const ACCOUNT_MEMORIZER_GET_CODE_HASH: felt252 = 3;

#[derive(Serde, Drop)]
pub struct AccountKey {
Expand All @@ -15,10 +18,58 @@ pub struct AccountKey {

#[generate_trait]
pub impl AccountMemorizerImpl of AccountMemorizerTrait {
fn get_nonce(self: @Memorizer, key: AccountKey) -> u256 {
let value = call_contract_syscall(
ACCOUNT_MEMORIZER.try_into().unwrap(),
ACCOUNT_MEMORIZER_GET_NONCE,
array![
*self.dict.segment_index,
*self.dict.offset,
key.chain_id,
key.block_number,
key.address,
]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_balance(self: @Memorizer, key: AccountKey) -> u256 {
let value = call_contract_syscall(
ACCOUNT_MEMORIZER_ID.try_into().unwrap(),
ACCOUNT_MEMORIZER_GET_BALANCE_ID,
ACCOUNT_MEMORIZER.try_into().unwrap(),
ACCOUNT_MEMORIZER_GET_BALANCE,
array![
*self.dict.segment_index,
*self.dict.offset,
key.chain_id,
key.block_number,
key.address,
]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_state_root(self: @Memorizer, key: AccountKey) -> u256 {
let value = call_contract_syscall(
ACCOUNT_MEMORIZER.try_into().unwrap(),
ACCOUNT_MEMORIZER_GET_STATE_ROOT,
array![
*self.dict.segment_index,
*self.dict.offset,
key.chain_id,
key.block_number,
key.address,
]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_code_hash(self: @Memorizer, key: AccountKey) -> u256 {
let value = call_contract_syscall(
ACCOUNT_MEMORIZER.try_into().unwrap(),
ACCOUNT_MEMORIZER_GET_CODE_HASH,
array![
*self.dict.segment_index,
*self.dict.offset,
Expand Down
147 changes: 143 additions & 4 deletions cairo/src/memorizer/header_memorizer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@ use hdp_cairo::Memorizer;
use starknet::syscalls::call_contract_syscall;
use starknet::{SyscallResult, SyscallResultTrait};

const HEADER_MEMORIZER_ID: felt252 = 0x0;
const HEADER_MEMORIZER: felt252 = 0;

const HEADER_MEMORIZER_GET_PARENT_ID: felt252 = 0x0;
const HEADER_MEMORIZER_GET_PARENT: felt252 = 0;
const HEADER_MEMORIZER_GET_UNCLE: felt252 = 1;
const HEADER_MEMORIZER_GET_COINBASE: felt252 = 2;
const HEADER_MEMORIZER_GET_STATE_ROOT: felt252 = 3;
const HEADER_MEMORIZER_GET_TRANSACTION_ROOT: felt252 = 4;
const HEADER_MEMORIZER_GET_RECEIPT_ROOT: felt252 = 5;
const HEADER_MEMORIZER_GET_BLOOM: felt252 = 6;
const HEADER_MEMORIZER_GET_DIFFICULTY: felt252 = 7;
const HEADER_MEMORIZER_GET_NUMBER: felt252 = 8;
const HEADER_MEMORIZER_GET_GAS_LIMIT: felt252 = 9;
const HEADER_MEMORIZER_GET_GAS_USED: felt252 = 10;
const HEADER_MEMORIZER_GET_TIMESTAMP: felt252 = 11;
const HEADER_MEMORIZER_GET_EXTRA_DATA: felt252 = 12;
const HEADER_MEMORIZER_GET_MIX_HASH: felt252 = 13;
const HEADER_MEMORIZER_GET_NONCE: felt252 = 14;
const HEADER_MEMORIZER_GET_BASE_FEE_PER_GAS: felt252 = 15;
const HEADER_MEMORIZER_GET_WITHDRAWALS_ROOT: felt252 = 16;
const HEADER_MEMORIZER_GET_BLOB_GAS_USED: felt252 = 17;
const HEADER_MEMORIZER_GET_EXCESS_BLOB_GAS: felt252 = 18;
const HEADER_MEMORIZER_GET_PARENT_BEACON_BLOCK_ROOT: felt252 = 19;

#[derive(Serde, Drop)]
pub struct HeaderKey {
Expand All @@ -16,8 +35,128 @@ pub struct HeaderKey {
pub impl HeaderMemorizerImpl of HeaderMemorizerTrait {
fn get_parent(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER_ID.try_into().unwrap(),
HEADER_MEMORIZER_GET_PARENT_ID,
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_PARENT,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_uncle(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_UNCLE,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_coinbase(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_COINBASE,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_state_root(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_STATE_ROOT,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_transaction_root(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_TRANSACTION_ROOT,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_receipt_root(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_RECEIPT_ROOT,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_difficulty(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_DIFFICULTY,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_number(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_NUMBER,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_gas_limit(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_GAS_LIMIT,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_gas_used(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_GAS_USED,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_mix_hash(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_MIX_HASH,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_nonce(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_NONCE,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
fn get_base_fee_per_gas(self: @Memorizer, key: HeaderKey) -> u256 {
let value = call_contract_syscall(
HEADER_MEMORIZER.try_into().unwrap(),
HEADER_MEMORIZER_GET_BASE_FEE_PER_GAS,
array![*self.dict.segment_index, *self.dict.offset, key.chain_id, key.block_number,]
.span()
)
Expand Down
37 changes: 37 additions & 0 deletions cairo/src/memorizer/storage_memorizer.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use hdp_cairo::Memorizer;
use starknet::syscalls::call_contract_syscall;
use starknet::{SyscallResult, SyscallResultTrait};

const STORAGE_MEMORIZER: felt252 = 2;

const STORAGE_MEMORIZER_GET_SLOT: felt252 = 0;

#[derive(Serde, Drop)]
pub struct StorageKey {
pub chain_id: felt252,
pub block_number: felt252,
pub address: felt252,
pub storage_slot: u256,
}

#[generate_trait]
pub impl StorageMemorizerImpl of StorageMemorizerTrait {
fn get_slot(self: @Memorizer, key: StorageKey) -> u256 {
let value = call_contract_syscall(
STORAGE_MEMORIZER.try_into().unwrap(),
STORAGE_MEMORIZER_GET_SLOT,
array![
*self.dict.segment_index,
*self.dict.offset,
key.chain_id,
key.block_number,
key.address,
key.storage_slot.high.into(),
key.storage_slot.low.into(),
]
.span()
)
.unwrap_syscall();
u256 { low: (*value[0]).try_into().unwrap(), high: (*value[1]).try_into().unwrap() }
}
}
1 change: 1 addition & 0 deletions packages/contract_bootloader/contract_bootloader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func run_contract_bootloader{
poseidon_ptr: PoseidonBuiltin*,
header_dict: DictAccess*,
account_dict: DictAccess*,
storage_dict: DictAccess*,
pow2_array: felt*,
}(compiled_class: CompiledClass*, calldata_size: felt, calldata: felt*, dry_run: felt) -> (
retdata_size: felt, retdata: felt*
Expand Down
Loading

0 comments on commit 4e00d58

Please sign in to comment.