Skip to content

Commit

Permalink
start tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Aug 29, 2024
1 parent ba55e8b commit b49e733
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 113 deletions.
72 changes: 16 additions & 56 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ func (b *Builder) Run(ctx context.Context) error {
<-doneListen
return nil
case <-b.chanFinalise:
// qwe := b.pendingBlock.StateUpdate.StateDiff.StorageDiffs[*new(felt.Felt).SetUint64(1)]
// ind := new(felt.Felt).SetUint64(b.pendingBlock.Block.Number - 10)
// // b.log.Infow("FINALISE 0x1 %s %s", ind.String(), qwe[*ind].String())
// // b.log.Debugw("Finalising new block")
err := b.cleanStorageDiff(b.pendingBlock.StateUpdate.StateDiff)
if err != nil {
return err
Expand All @@ -206,7 +202,7 @@ func (b *Builder) Run(ctx context.Context) error {
}

func (b *Builder) cleanStorageDiff(sd *core.StateDiff) error {
b.log.Infof("removing unneeded values in the storage diffs")
b.log.Infof("Removing values in the storage diff that don't affect state")
state, closer, err := b.bc.HeadState()
if err != nil {
return err
Expand Down Expand Up @@ -464,6 +460,7 @@ func (b *Builder) depletePool(ctx context.Context) error {
}
}

// todo(rian) : does blockifier need the correct value, or can we pass in any non-zero value?
func getPaidOnL1Fees(txn *mempool.BroadcastedTransaction) ([]*felt.Felt, error) {
if tx, ok := (txn.Transaction).(*core.L1HandlerTransaction); ok {
handleDepositEPS, err := new(felt.Felt).SetString("0x2d757788a8d8d6f21d1cd40bce38a8222d70654214e96ff95d8086e684fbee5")
Expand Down Expand Up @@ -496,39 +493,25 @@ func (b *Builder) runTxn(txn *mempool.BroadcastedTransaction) error {
if err != nil {
return err
}
// if b.pendingBlock.Block.TransactionCount == 0 {
// sd, _ := state.StateDiffAndClasses()
// sd.Print()
// }

blockInfo := &vm.BlockInfo{
Header: &core.Header{
Number: b.shadowBlock.Number, // Cairo contracts can access the block number
SequencerAddress: b.shadowBlock.SequencerAddress, // Cairo contracts can access the sequencer address
Timestamp: b.shadowBlock.Timestamp, // Cairo contracts can access the timestamp
ProtocolVersion: b.shadowBlock.ProtocolVersion, // Cairo contracts can access the protocol version
GasPrice: b.shadowBlock.GasPrice, // Cairo contracts can access the gas price
GasPriceSTRK: b.shadowBlock.GasPriceSTRK, // Don't seem to need to set this
L1DAMode: b.shadowBlock.L1DAMode, // Don't seem to need to set this
L1DataGasPrice: b.shadowBlock.L1DataGasPrice, // Don't seem to need to set this
Number: b.shadowBlock.Number, // Affects post 0.13.2 block hash
SequencerAddress: b.shadowBlock.SequencerAddress, // Affects post 0.13.2 block hash
Timestamp: b.shadowBlock.Timestamp, // Affects post 0.13.2 block hash
ProtocolVersion: b.shadowBlock.ProtocolVersion, // Affects post 0.13.2 block hash
GasPrice: b.shadowBlock.GasPrice, // Affects post 0.13.2 block hash
GasPriceSTRK: b.shadowBlock.GasPriceSTRK, // Affects post 0.13.2 block hash
L1DataGasPrice: b.shadowBlock.L1DataGasPrice, // Affects post 0.13.2 block hash
},
}

// sd, _ := state.StateDiffAndClasses()
// for k, v := range sd.StorageDiffs[*new(felt.Felt).SetUint64(1)] {
// fmt.Println(k.String(), v.String())
// }

fee, _, trace, txnReceipts, _, err := b.vm.Execute([]core.Transaction{txn.Transaction}, classes, feesPaidOnL1, blockInfo, state,
b.bc.Network(), false, false, false, false)
if err != nil {
return err
}

// fmt.Println("========\n\n")
// seqTrace.Print()
// refTrace.Print()

feeUnit := core.WEI
if txn.Transaction.TxVersion().Is(3) {
feeUnit = core.STRK
Expand Down Expand Up @@ -562,27 +545,11 @@ func (b *Builder) runTxn(txn *mempool.BroadcastedTransaction) error {
b.log.Fatalf(diffStr)
}
}

// for ii, qwe := range trace[0].AllEvents() {
// fmt.Printf("\nallEvents %d - %d %s %v\n", ii, qwe.Order, qwe.From.String(), qwe.Keys)
// }
// if b.pendingBlock.Block.TransactionCount == 0 { // todo: remove
// b.log.Debugw("OVERRIDE for debug")
// receipt.Events = b.shadowBlock.Receipts[0].Events
// }

// receipt.Events=b.shadowBlock.Receipts[b.pendingBlock.Block.TransactionCount].Events // Todo: Events are the last thing in block hash error
// receipt = b.shadowBlock.Receipts[b.pendingBlock.Block.TransactionCount] // Todo: remove, test to see if this is the only issue

b.pendingBlock.Block.Receipts = append(b.pendingBlock.Block.Receipts, receipt)
b.pendingBlock.Block.Transactions = append(b.pendingBlock.Block.Transactions, txn.Transaction)
b.pendingBlock.Block.TransactionCount = uint64(len(b.pendingBlock.Block.Transactions))
b.pendingBlock.Block.EventCount += uint64(len(receipt.Events))
b.pendingBlock.StateUpdate.StateDiff = mergeStateDiffs(b.pendingBlock.StateUpdate.StateDiff, StateDiff(&trace[0]))
// fmt.Println("---------\n")
// b.pendingBlock.StateUpdate.StateDiff.Print()
// b.shadowStateUpdate.StateDiff.Print()
// fmt.Println("========\n\n")
return nil
}

Expand Down Expand Up @@ -633,7 +600,7 @@ func (b *Builder) shadowTxns(ctx context.Context) error {
if err != nil {
return err
}
// Used to help debug
// todo: make this debud logic cleaner
if b.junoEndpoint != "" {
blockTraces, err := b.JunoGetBlockTrace(int(block.Number))
if err != nil {
Expand All @@ -645,20 +612,13 @@ func (b *Builder) shadowTxns(ctx context.Context) error {

b.shadowStateUpdate = su
b.shadowBlock = block

// b.pendingBlock.Block.Header = block.Header
// b.pendingBlock.Block.GlobalStateRoot = nil
// b.pendingBlock.Block.Receipts = nil
// b.pendingBlock.Block.TransactionCount = 0
b.pendingBlock.Block.SequencerAddress = block.SequencerAddress // block hash
b.pendingBlock.Block.Timestamp = block.Timestamp // block hash
b.pendingBlock.Block.Transactions = nil
b.pendingBlock.Block.Header.ProtocolVersion = block.ProtocolVersion // block hash
b.pendingBlock.Block.Header.GasPrice = block.GasPrice // block hash
b.pendingBlock.Block.Header.GasPriceSTRK = block.GasPriceSTRK // block hash
b.pendingBlock.Block.Header.L1DAMode = block.L1DAMode // block hash
b.pendingBlock.Block.Header.L1DataGasPrice = block.L1DataGasPrice // block hash
// b.pendingBlock.Block.Header.Signatures = block.Signatures // Can't use our signature
b.pendingBlock.Block.SequencerAddress = block.SequencerAddress // Affects post 0.13.2 block hash
b.pendingBlock.Block.Timestamp = block.Timestamp // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.ProtocolVersion = block.ProtocolVersion // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.GasPrice = block.GasPrice // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.GasPriceSTRK = block.GasPriceSTRK // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.L1DataGasPrice = block.L1DataGasPrice // Affects post 0.13.2 block hash

b.chanNumTxnsToShadow <- int(block.TransactionCount)
for _, txn := range block.Transactions {
Expand Down
21 changes: 0 additions & 21 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,30 +214,9 @@ func Post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments
if rErr != nil {
return nil, nil, rErr
}
// for _, rec := range b.Receipts {
// fmt.Println("rec", rec.TransactionHash)
// }

concatCounts := concatCounts(b.TransactionCount, b.EventCount, sdLength, b.L1DAMode)

// fmt.Println("STARKNET_BLOCK_HASH0:", new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH0")).String())
// fmt.Println("Block Number:", new(felt.Felt).SetUint64(b.Number).String())
// fmt.Println("Global State Root:", b.GlobalStateRoot.String())
// fmt.Println("Sequencer Address:", b.SequencerAddress.String())
// fmt.Println("Block Timestamp:", new(felt.Felt).SetUint64(b.Timestamp).String())
// fmt.Println("Concat Counts:", concatCounts.String())
// fmt.Println("State Diff Commitment:", sdCommitment.String())
// fmt.Println("Transaction Commitment:", txCommitment.String())
// fmt.Println("Event Commitment:", eCommitment.String())
// fmt.Println("Receipt Commitment:", rCommitment.String())
// fmt.Println("Gas Price in Wei:", b.GasPrice.String())
// fmt.Println("Gas Price in Fri:", b.GasPriceSTRK.String())
// fmt.Println("L1 Data Gas Price in Wei:", b.L1DataGasPrice.PriceInWei.String())
// fmt.Println("L1 Data Gas Price in Fri:", b.L1DataGasPrice.PriceInFri.String())
// fmt.Println("Protocol Version:", new(felt.Felt).SetBytes([]byte(b.ProtocolVersion)).String())
// fmt.Println("Reserved (Extra Data):", felt.Zero.String())
// fmt.Println("Parent Block Hash:", b.ParentHash.String())

return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH0")),
new(felt.Felt).SetUint64(b.Number), // block number
Expand Down
38 changes: 2 additions & 36 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub extern "C" fn cairoVMExecute(
return;
}

let mut is_l1_handler_txn=false;
let mut is_l1_handler_txn = false;
let mut txn_state = CachedState::create_transactional(&mut state);
let fee_type;
let minimal_l1_gas_amount_vector: Option<GasVector>;
Expand All @@ -312,7 +312,7 @@ pub extern "C" fn cairoVMExecute(
t.execute(&mut txn_state, &block_context, charge_fee, validate)
}
Transaction::L1HandlerTransaction(t) => {
is_l1_handler_txn=true;
is_l1_handler_txn = true;
fee_type = t.fee_type();
minimal_l1_gas_amount_vector = None;
t.execute(&mut txn_state, &block_context, charge_fee, validate)
Expand Down Expand Up @@ -348,25 +348,6 @@ pub extern "C" fn cairoVMExecute(
);
return;
}
// print!("############# blockifier");
// io::stdout().flush().unwrap();
// if let Some(validate_info) = &t.validate_call_info {
// let qwe = "valudate";
// print_event_orders(&validate_info,qwe.to_string());
// }

// if let Some(execute_info) = &t.execute_call_info {
// let qwe = "exec";
// print_event_orders(&execute_info,qwe.to_string());
// }

// if let Some(fee_transfer_info) = &t.fee_transfer_call_info {
// let qwe = "fee ter";
// print_event_orders(&fee_transfer_info,qwe.to_string());
// }




// we are estimating fee, override actual fee calculation
if t.transaction_receipt.fee.0 == 0 && !is_l1_handler_txn {
Expand All @@ -392,7 +373,6 @@ pub extern "C" fn cairoVMExecute(
&fee_type,
)
}


let actual_fee = t.transaction_receipt.fee.0.into();
let data_gas_consumed = t.transaction_receipt.da_gas.l1_data_gas.into();
Expand Down Expand Up @@ -436,20 +416,6 @@ pub extern "C" fn cairoVMExecute(
txn_state.commit();
}
}
// fn print_event_orders(call_info: &blockifier::execution::call_info::CallInfo, typee:String) {
// println!("\n typee: {}", typee);
// io::stdout().flush().unwrap();
// for event in &call_info.execution.events {
// println!("\nEvent order: {}", event.order);
// io::stdout().flush().unwrap();
// }

// for inner_call in &call_info.inner_calls {
// println!("\n : {:?}", call_info.inner_calls.len());
// io::stdout().flush().unwrap();
// print_event_orders(inner_call,typee.clone());
// }
// }

fn felt_to_u128(felt: StarkFelt) -> u128 {
// todo find Into<u128> trait or similar
Expand Down

0 comments on commit b49e733

Please sign in to comment.