Skip to content

Commit 035fff2

Browse files
authored
Python bindings (astraly-labs#3)
Python bindings * reference added. * create env method * create middleware method * Implemented bindings for 3 functions: - create environment - create middleware - create account * remove pyo3 ci * Added environment operations: - declare contract - account execute call - top_up_balance * cleanups * added support for - call - set storage - get storage * added support for - impersonate - stop_impersonate * Added methods to: - create subscription - poll subscription - fixed id generation for middleware * expanding examples * adding known contracts into python bindings * debugging bindings * fixes * example finalised * lints and gas-price * replay block transaction implementation * mocked account method added * some new tests * event filter for replay block transactions speedup * remove local devnet dep
1 parent 9713dd9 commit 035fff2

File tree

26 files changed

+2264
-85
lines changed

26 files changed

+2264
-85
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.env
2+
13
# Generated by Cargo
24
# will have compiled files and executables
35
/target/
@@ -23,4 +25,4 @@ starkbiter-core/data
2325
example_fork/test.json
2426

2527
doctest_cache/
26-
docs/target/*
28+
docs/target/*

Cargo.lock

Lines changed: 148 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
# List of crates included in this workspace
3-
members = ["core", "bindings", "macros", "engine"]
3+
members = ["core", "bindings", "macros", "engine", "python-bindings"]
44

55
# List of crates excluded from this workspace
66
exclude = ["benches", "docs"]
@@ -41,7 +41,8 @@ starkbiter-core = { path = "core" }
4141
starkbiter-engine = { path = "engine" }
4242
starkbiter-macros = { path = "macros" }
4343

44-
# starknet-devnet-core = { path = "/Users/baitcode/work/starknet/starknet-devnet/crates/starknet-devnet-core" }
44+
uuid = { version = "=1.17.0", features = ["v4"] }
45+
4546
starknet-devnet-core = { git = "https://github.com/baitcode/starknet-devnet.git", branch = "experiment" }
4647
starknet-devnet-types = { git = "https://github.com/baitcode/starknet-devnet.git", branch = "experiment" }
4748
starknet_api = "=0.14.0-rc.3"
@@ -106,6 +107,9 @@ config = { version = "=0.14.0" }
106107
toml.workspace = true
107108
Inflector = { version = "=0.11.4" }
108109

110+
# Building files
111+
# foundry-config = { version = "=0.2.0" }
112+
# tempfile = { version = "3.10.1" }
109113

110114
# Errors
111115
thiserror.workspace = true
@@ -138,3 +142,6 @@ lto = true
138142
# The Rust compiler splits your crate into multiple codegen units to parallelize (and thus speed up) compilation but at the cost of optimization.
139143
# This setting tells the compiler to use only one codegen unit, which will slow down compilation but improve optimization.
140144
codegen-units = 1
145+
146+
[workspace.metadata.cargo-machete]
147+
ignored = ["proc-macro2"]

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ The Starkbiter workspace has five crates:
3030

3131
## WIP
3232

33+
- [ ] non-blocking event polling
3334
- [x] bin docs
3435
- [x] core docs
3536
- [x] main readme
3637
- [ ] docs docs
38+
- [x] getting started
39+
- [ ] usage
40+
- [x] index
3741
- [ ] benchmarks
3842
- [ ] python bindings
39-
- [ ] more test cases, more granular, coverage report
40-
- [ ] support for replaying mainnet transactions mixing them with simulated ones.
41-
- [ ] event persistence
42-
- [ ] github workflows revival
43+
- [x] more test cases
44+
- [ ] coverage report
45+
- [x] support for replaying mainnet transactions mixing them with simulated ones.
46+
- [x] github workflows revival
4347
- [ ] contribute to cainome and remove fork dep
4448
- [ ] contribute to starknet-rs and remove fork dep + Fix generic Runtime error with more specific errors from within Provider
4549
- [ ] contribute to starknet-devnet and remove fork dep
50+
- [x] test that toml config parameters for forking work
51+
- [x] contract execution logging and traces from Devnet
4652
- [ ] test for simultaneous connection usage by different contracts.
47-
- [ ] test that toml config parameters for forking work
48-
- [ ] contract execution logging and traces from Devnet
53+
4954

5055
## Book
5156
TODO: Does not exist yet

core/benches/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use starkbiter_core::{
1212
environment::Environment,
1313
middleware::{traits::Middleware, StarkbiterMiddleware},
1414
};
15-
use starknet_accounts::{Account, ConnectedAccount};
15+
use starknet_accounts::ConnectedAccount;
1616
use starknet_core::{
1717
types::{Call, Felt},
1818
utils::get_selector_from_name,
@@ -113,7 +113,7 @@ async fn bencher(client: Arc<StarkbiterMiddleware>, label: &str) -> BenchDuratio
113113
// Track the duration for each part of the benchmark.
114114
let mut total_deploy_duration = 0;
115115
let mut total_lookup_duration = 0;
116-
let mut total_stateless_call_duration = 0;
116+
let total_stateless_call_duration = 0;
117117
let mut total_stateful_call_duration = 0;
118118

119119
// Deploy `ArbiterMath` and `ArbiterToken` contracts and tally up how long this

core/src/environment/instruction.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ use starknet_devnet_types::{
1717
use super::*;
1818
use crate::tokens::TokenId;
1919

20+
/// Filter to pass to replay block method so that only transactions containing
21+
/// certain events would be applied. This is merely an optimisation, as block
22+
/// replay with 200 tx would take 20 mins.
23+
#[derive(Debug, Clone)]
24+
pub struct EventFilter {
25+
/// Matched events emitted by this address
26+
pub from_address: Felt,
27+
/// Matches events with those keys: [selector, key1, key2]
28+
/// selector is a keccak hash from event name.
29+
pub keys: Vec<Felt>,
30+
}
31+
2032
/// Instructions that can be sent to the [`Environment`] via the [`Socket`].
2133
///
2234
/// These instructions are sent to the [`Environment`] via the
@@ -313,7 +325,7 @@ pub enum Outcome {
313325
/// Node-related outcome.
314326
Node(NodeOutcome),
315327
/// Cheatcode-related outcome.
316-
Cheat(CheatcodesReturn),
328+
Cheat(CheatcodesOutcome),
317329
/// System-related outcome.
318330
System(SystemInstructionOutcome),
319331
}
@@ -417,18 +429,36 @@ pub enum CheatInstruction {
417429
/// The transaction hash of the deployment.
418430
tx_hash: Felt,
419431
},
432+
433+
/// Gets a block with transactions by its identifier
434+
/// from the forked blockchain.
435+
GetBlockWithTxs {
436+
/// The identifier of the block to retrieve.
437+
block_id: core_types::BlockId,
438+
},
439+
/// Fetches block with transactions by its identifier
440+
/// and adds them on top of pending block in DevNet.
441+
ReplayBlockWithTxs {
442+
/// The identifier of the block to retrieve.
443+
block_id: core_types::BlockId,
444+
/// Checks if transaction has events matching filters and applies it if
445+
/// so, otherwise ignores.
446+
has_events: Option<Vec<EventFilter>>,
447+
/// Set to true to recalculate the nonce for the transactions
448+
override_nonce: bool,
449+
},
420450
}
421451

422452
/// Return values of applying cheatcodes.
423453
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
424-
pub enum CheatcodesReturn {
454+
pub enum CheatcodesOutcome {
425455
/// Returns the class hash of the declared contract.
426456
DeclareContract(Felt),
427457
/// Returns the contract address of the created account.
428458
CreateAccount(Felt),
429459
/// Indicates a block was created.
430460
CreateBlock,
431-
/// Returns the result of sending an L1 message.
461+
/// Returns the tx_hash of L1 message transaction.
432462
L1Message(Felt),
433463
/// Indicates a balance was added.
434464
TopUpBalance,
@@ -443,4 +473,9 @@ pub enum CheatcodesReturn {
443473
SetNextBlockGas(GasModification),
444474
/// Returns the address of the deployed contract.
445475
GetDeployedContractAddress(Felt),
476+
/// Returns block with transactions.
477+
GetBlockWithTxs(Box<core_types::MaybePendingBlockWithTxs>),
478+
/// Returns numbers of transactions from the origin block that were (added,
479+
/// ignored, failed)
480+
ReplayBlockWithTxs(usize, usize, usize),
446481
}

0 commit comments

Comments
 (0)