Skip to content

Commit

Permalink
add da gas prices as parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Feb 21, 2024
1 parent 855b063 commit 87fbabc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 41 deletions.
3 changes: 2 additions & 1 deletion node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.
return ret, traces, tvm.Do(func(vm *vm.VM) error {
var err error
ret, traces, err = (*vm).Execute(txns, declaredClasses, blockNumber, blockTimestamp, sequencerAddress,
state, network, paidFeesOnL1, skipChargeFee, skipValidate, errOnRevert, gasPriceWEI, gasPriceSTRK, legacyTraceJSON, daGasPriceWEI, daGasPriceFRI)
state, network, paidFeesOnL1, skipChargeFee, skipValidate, errOnRevert, gasPriceWEI, gasPriceSTRK, legacyTraceJSON,
daGasPriceWEI, daGasPriceFRI)
return err
})
}
1 change: 0 additions & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func (s *Synchronizer) verifierTask(ctx context.Context, block *core.Block, stat
}
storeTimer := time.Now()
err = s.blockchain.Store(block, commitments, stateUpdate, newClasses)

if err != nil {
if errors.Is(err, blockchain.ErrParentDoesNotMatchHead) {
// revert the head and restart the sync process, hoping that the reorg is not deep
Expand Down
9 changes: 5 additions & 4 deletions vm/rust/src/juno_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ pub fn ptr_to_felt(bytes: *const c_uchar) -> StarkFelt {
.expect("cannot new Starkfelt from Juno bytes")
}

pub fn ptr_to_u128(bytes: *const u8) -> u128 {
// Note: we only consider the last 16 bytes because `GasPrices` are `NonZeroU128`
pub fn felt_ptr_to_u128(bytes: *const c_uchar) -> u128 {
assert!(!bytes.is_null(), "Null pointer provided");
let slice = unsafe { slice::from_raw_parts(bytes, mem::size_of::<u128>()) };
let array: [u8; mem::size_of::<u128>()] = slice.try_into().expect("Slice not [u8; 16]");
u128::from_le_bytes(array)
let slice = unsafe { slice::from_raw_parts(bytes, 32) };
let array: [u8; 16] = slice[16..32].try_into().expect("Slice with incorrect length");
u128::from_be_bytes(array)
}

pub fn contract_class_from_json_str(raw_json: &str) -> Result<ContractClass, String> {
Expand Down
20 changes: 6 additions & 14 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod jsonrpc;
mod juno_state_reader;
use std::num::NonZeroU128;
use crate::juno_state_reader::{ptr_to_felt,ptr_to_u128, JunoStateReader};
use crate::juno_state_reader::{ptr_to_felt,felt_ptr_to_u128, JunoStateReader};
use std::{
ffi::{c_char, c_uchar, c_ulonglong, c_void, c_longlong, CStr, CString},
slice,
Expand Down Expand Up @@ -237,25 +237,17 @@ pub extern "C" fn cairoVMExecute(
let mut classes = classes.unwrap();

let sequencer_address_felt = ptr_to_felt(sequencer_address);
let gas_price_wei_u128 = ptr_to_u128(gas_price_wei);
let gas_price_strk_u128 = ptr_to_u128(gas_price_strk);
let da_gas_price_wei_u128 = ptr_to_u128(da_gas_price_wei);
let da_gas_price_fri_u128 = ptr_to_u128(da_gas_price_fri);

let fee_token_addresses: FeeTokenAddresses = FeeTokenAddresses { // both addresses are the same for all networks
eth_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7").unwrap()).unwrap(),
strk_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d").unwrap()).unwrap(),
};

println!("{:}",gas_price_wei_u128);
println!("{:}",gas_price_strk_u128);
println!("{:}",da_gas_price_wei_u128);
println!("{:}",da_gas_price_fri_u128);

let gas_prices: GasPrices = GasPrices {
eth_l1_gas_price: NonZeroU128::new(gas_price_wei_u128).unwrap(),
strk_l1_gas_price: NonZeroU128::new(gas_price_strk_u128).unwrap(),
eth_l1_data_gas_price: NonZeroU128::new(da_gas_price_wei_u128).unwrap(),
strk_l1_data_gas_price: NonZeroU128::new(da_gas_price_fri_u128).unwrap(),
eth_l1_gas_price: NonZeroU128::new(felt_ptr_to_u128(gas_price_wei)).unwrap(),
strk_l1_gas_price: NonZeroU128::new(felt_ptr_to_u128(gas_price_strk)).unwrap(),
eth_l1_data_gas_price: NonZeroU128::new(felt_ptr_to_u128(da_gas_price_wei)).unwrap(),
strk_l1_data_gas_price: NonZeroU128::new(felt_ptr_to_u128(da_gas_price_fri)).unwrap(),
};

let use_kzg_da = false; // Todo : Need to include this as a new parameter?
Expand Down
31 changes: 11 additions & 20 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,30 +193,14 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, bloc

sequencerAddressBytes := sequencerAddress.Bytes()

gasPriceWEIBytes := gasPriceWEI.Bytes()

if gasPriceSTRK == nil {
gasPriceSTRK = new(felt.Felt).SetUint64(1)
}
gasPriceSTRKBytes := gasPriceSTRK.Bytes()

if daGasPriceWEI == nil {
daGasPriceWEI = new(felt.Felt).SetUint64(1)
}
daGasPriceWEIBytes := daGasPriceWEI.Bytes()

if daGasPriceFRI == nil {
daGasPriceFRI = new(felt.Felt).SetUint64(1)
}
daGasPriceFRIBytes := daGasPriceFRI.Bytes()
gasPriceWEIBytes := getBytesOrSetDefault(gasPriceWEI)
gasPriceSTRKBytes := getBytesOrSetDefault(gasPriceSTRK)
daGasPriceWEIBytes := getBytesOrSetDefault(daGasPriceWEI)
daGasPriceFRIBytes := getBytesOrSetDefault(daGasPriceFRI)

if gasPriceWEI.IsZero() || gasPriceSTRK.IsZero() || daGasPriceWEI.IsZero() || daGasPriceFRI.IsZero() {
return nil, nil, errors.New("gas prices must be non-zero")
}
fmt.Println("gasPriceWEIBytes", gasPriceWEIBytes, gasPriceWEI.String())
fmt.Println("gasPriceSTRKBytes", gasPriceSTRKBytes, gasPriceSTRK.String())
fmt.Println("daGasPriceFRIBytes", daGasPriceWEIBytes, daGasPriceWEI.String())
fmt.Println("daGasPriceFRIBytes", daGasPriceFRIBytes, daGasPriceFRI.String())

var skipChargeFeeByte byte
if skipChargeFee {
Expand Down Expand Up @@ -281,6 +265,13 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, bloc
return context.actualFees, traces, nil
}

func getBytesOrSetDefault(value *felt.Felt) [32]byte {
if value == nil {
value = new(felt.Felt).SetUint64(1)
}
return value.Bytes()
}

func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []core.Class) (json.RawMessage, json.RawMessage, error) {
txnJSONs := []json.RawMessage{}
for _, txn := range txns {
Expand Down
2 changes: 1 addition & 1 deletion vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestExecute(t *testing.T) {

state := core.NewState(txn)

t.Run("empty transaction list", func(t *testing.T) {
t.Run("empty transaction list", func(t *testing.T) {
// data from 0 block
var (
address = utils.HexToFelt(t, "0x46a89ae102987331d369645031b49c27738ed096f2789c24449966da4c6de6b")
Expand Down

0 comments on commit 87fbabc

Please sign in to comment.