Skip to content

Commit

Permalink
abilengths wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Feb 21, 2024
1 parent 1363ba0 commit 708b5f7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
12 changes: 12 additions & 0 deletions vm/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func marshalCompiledClass(class core.Class) (json.RawMessage, error) {
if err != nil {
return nil, err
}

return json.Marshal(compiledCairo0Class)
case *core.Cairo1Class:
if c.Compiled == nil {
Expand All @@ -28,3 +29,14 @@ func marshalCompiledClass(class core.Class) (json.RawMessage, error) {
return nil, fmt.Errorf("unsupported class type %T", c)
}
}

func marshalABILength(class core.Class) (json.RawMessage, error) {
switch c := class.(type) {
case *core.Cairo0Class:
return json.Marshal(len(c.Abi))
case *core.Cairo1Class:
return json.Marshal(len(c.Abi))
default:
return nil, fmt.Errorf("unsupported class type %T", c)
}
}
2 changes: 1 addition & 1 deletion vm/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
serde = "1.0.171"
serde_json = { version = "1.0.96", features = ["raw_value"] }
blockifier = "=0.5.0-rc.1"
blockifier = "=0.5.0-rc.3"
starknet_api = "=0.8.0"
cairo-vm = "=0.9.2"
indexmap = "2.1.0"
Expand Down
1 change: 0 additions & 1 deletion vm/rust/src/juno_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
ffi::{c_char, c_uchar, c_void, CStr},
slice,
sync::Mutex,
mem,
};

use blockifier::execution::contract_class::ContractClass;
Expand Down
37 changes: 27 additions & 10 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use blockifier::{
block::{BlockInfo,GasPrices},
context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext},
execution::{
contract_class::ClassInfo,
common_hints::ExecutionMode,
entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext},
},
Expand All @@ -25,7 +26,7 @@ use blockifier::{
ValidateTransactionError,
}, objects::{DeprecatedTransactionInfo, HasRelatedFeeType, TransactionInfo},
transaction_execution::Transaction,
transactions::{ExecutableTransaction,ClassInfo}
transactions::ExecutableTransaction
},

versioned_constants::VersionedConstants
Expand All @@ -52,7 +53,7 @@ extern "C" {
fn JunoAppendActualFee(reader_handle: usize, ptr: *const c_uchar);
}

const GLOBAL_CONTRACT_CACHE_SIZE: usize= 100; // Todo ? default used to set this to 100.
const GLOBAL_CONTRACT_CACHE_SIZE: usize= 100;



Expand Down Expand Up @@ -171,6 +172,7 @@ pub struct TxnAndQueryBit {
pub txn: StarknetApiTransaction,
pub txn_hash: TransactionHash,
pub query_bit: bool,
pub abi_length : usize,
}

#[no_mangle]
Expand Down Expand Up @@ -263,8 +265,15 @@ pub extern "C" fn cairoVMExecute(
let mut trace_buffer = Vec::with_capacity(10_000);

for (txn_index, txn_and_query_bit) in txns_and_query_bits.iter().enumerate() {

let mut abi_length = 0;

let contract_class = match txn_and_query_bit.txn.clone() {

StarknetApiTransaction::Declare(_) => {

abi_length = txn_and_query_bit.abi_length;

if classes.is_empty() {
report_error(reader_handle, "missing declared class", txn_index as i64);
return;
Expand Down Expand Up @@ -293,18 +302,26 @@ pub extern "C" fn cairoVMExecute(
};


let sierra_program_length = 0; // Todo: Should be a new parameter?
let abi_length = 0; // Todo: Should be a new parameter?
let class_info = ClassInfo {
contract_class: contract_class.unwrap(),
sierra_program_length,
abi_length,
};
let contract_class_unwrap = contract_class.unwrap();
let sierra_program_length = contract_class_unwrap.bytecode_length();


let class_info_result = ClassInfo::new(&contract_class_unwrap, sierra_program_length, abi_length);
let mut class_info: Option<ClassInfo> = None;
match class_info_result {
Ok(info) => {
class_info = Some(info);
},
Err(e) => {
report_error(reader_handle, e.to_string().as_str(), txn_index as i64);
return;
}
}

let txn = transaction_from_api(
txn_and_query_bit.txn.clone(),
txn_and_query_bit.txn_hash,
Some(class_info),
class_info,
paid_fee_on_l1,
txn_and_query_bit.query_bit,
);
Expand Down
22 changes: 19 additions & 3 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, bloc
handle := cgo.NewHandle(context)
defer handle.Delete()

txnsJSON, classesJSON, err := marshalTxnsAndDeclaredClasses(txns, declaredClasses)
txnsJSON, classesJSON, abiLengthJSON, err := marshalTxnsAndDeclaredClasses(txns, declaredClasses)
if err != nil {
return nil, nil, err
}
Expand All @@ -190,6 +190,7 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, bloc
paidFeesOnL1CStr := cstring(paidFeesOnL1Bytes)
txnsJSONCstr := cstring(txnsJSON)
classesJSONCStr := cstring(classesJSON)
abiLengthsJSONCStr := cstring(abiLengthJSON)

sequencerAddressBytes := sequencerAddress.Bytes()

Expand All @@ -211,6 +212,7 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, bloc
chainID := C.CString(network.L2ChainID)
C.cairoVMExecute(txnsJSONCstr,
classesJSONCStr,
abiLengthsJSONCStr,
C.uintptr_t(handle),
C.ulonglong(blockNumber),
C.ulonglong(blockTimestamp),
Expand Down Expand Up @@ -267,7 +269,7 @@ func boolToByte(b bool) byte {
return 0
}

func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []core.Class) (json.RawMessage, json.RawMessage, error) {
func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []core.Class) (json.RawMessage, json.RawMessage,json.RawMessage,, error) {

Check failure on line 272 in vm/vm.go

View workflow job for this annotation

GitHub Actions / lint

expected ')', found ',') (typecheck)

Check failure on line 272 in vm/vm.go

View workflow job for this annotation

GitHub Actions / lint

expected ')', found ',') (typecheck)

Check failure on line 272 in vm/vm.go

View workflow job for this annotation

GitHub Actions / lint

expected ')', found ',') (typecheck)
txnJSONs := []json.RawMessage{}
for _, txn := range txns {
txnJSON, err := marshalTxn(txn)
Expand All @@ -286,6 +288,15 @@ func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []co
classJSONs = append(classJSONs, declaredClassJSON)
}

abiLengthsJSONs := []json.RawMessage{}
for _, declaredClass := range declaredClasses {
abiLengthJSON, cErr := marshalABILength(declaredClass)
if cErr != nil {
return nil, nil, cErr
}
abiLengthsJSONs = append(abiLengthsJSONs, abiLengthJSON)
}

txnsJSON, err := json.Marshal(txnJSONs)
if err != nil {
return nil, nil, err
Expand All @@ -295,5 +306,10 @@ func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []co
return nil, nil, err
}

return txnsJSON, classesJSON, nil
abiLengthsJSON, err := json.Marshal(abiLengthsJSONs)
if err != nil {
return nil, nil, err
}

return txnsJSON, classesJSON, abiLengthsJSON, nil
}

0 comments on commit 708b5f7

Please sign in to comment.