Skip to content

Commit

Permalink
Make Python class imports static objects
Browse files Browse the repository at this point in the history
This is more consistent with how these objects are used and cleans up the
constructor functions.

Signed-off-by: Adam Ludvik <ludvik@bitwise.io>
  • Loading branch information
Adam Ludvik committed Oct 3, 2018
1 parent 32bdaa1 commit e0116d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
12 changes: 12 additions & 0 deletions validator/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* ------------------------------------------------------------------------------
*/

use cpython::{PyObject, Python};

#[no_mangle]
pub unsafe extern "C" fn ffi_reclaim_string(s_ptr: *mut u8, s_len: usize, s_cap: usize) -> isize {
String::from_raw_parts(s_ptr, s_len, s_cap);
Expand All @@ -32,3 +34,13 @@ pub unsafe extern "C" fn ffi_reclaim_vec(

0
}

pub fn py_import_class(module: &str, class: &str) -> PyObject {
let gil = Python::acquire_gil();
let python = gil.python();
python
.import(module)
.expect(&format!("Unable to import '{}'", module))
.get(python, class)
.expect(&format!("Unable to import {} from '{}'", class, module))
}
22 changes: 10 additions & 12 deletions validator/src/journal/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::time::Duration;

use execution::execution_platform::ExecutionPlatform;
use execution::py_executor::PyExecutor;
use ffi::py_import_class;
use journal::block_manager::BlockManager;
use journal::candidate_block::{CandidateBlock, CandidateBlockError};
use journal::chain_commit_state::TransactionCommitCache;
Expand All @@ -48,6 +49,13 @@ lazy_static! {
metrics::get_collector("sawtooth_validator.publisher");
}

lazy_static! {
static ref PY_BLOCK_HEADER_CLASS: PyObject =
py_import_class("sawtooth_validator.protobuf.block_pb2", "BlockHeader");
static ref PY_BLOCK_BUILDER_CLASS: PyObject =
py_import_class("sawtooth_validator.journal.block_builder", "BlockBuilder");
}

#[derive(Debug)]
pub enum InitializeBlockError {
BlockInProgress,
Expand Down Expand Up @@ -109,8 +117,6 @@ pub struct SyncBlockPublisher {
block_manager: BlockManager,
batch_observers: Vec<PyObject>,
batch_injector_factory: PyObject,
block_header_class: PyObject,
block_builder_class: PyObject,
batch_committed: PyObject,
transaction_committed: PyObject,
state_view_factory: StateViewFactory,
Expand Down Expand Up @@ -140,8 +146,6 @@ impl Clone for SyncBlockPublisher {
.map(|i| i.clone_ref(py))
.collect(),
batch_injector_factory: self.batch_injector_factory.clone_ref(py),
block_header_class: self.block_header_class.clone_ref(py),
block_builder_class: self.block_builder_class.clone_ref(py),
batch_committed: self.batch_committed.clone_ref(py),
transaction_committed: self.transaction_committed.clone_ref(py),
state_view_factory: self.state_view_factory.clone(),
Expand Down Expand Up @@ -262,13 +266,11 @@ impl SyncBlockPublisher {
kwargs
.set_item(py, "signer_public_key", &public_key)
.unwrap();
let block_header = self
.block_header_class
let block_header = PY_BLOCK_HEADER_CLASS
.call(py, NoArgs, Some(&kwargs))
.expect("BlockHeader could not be constructed");

let block_builder = self
.block_builder_class
let block_builder = PY_BLOCK_BUILDER_CLASS
.call(py, (block_header,), None)
.expect("BlockBuilder could not be constructed");

Expand Down Expand Up @@ -516,8 +518,6 @@ impl BlockPublisher {
permission_verifier: PyObject,
batch_observers: Vec<PyObject>,
batch_injector_factory: PyObject,
block_header_class: PyObject,
block_builder_class: PyObject,
) -> Self {
let tep = Box::new(PyExecutor::new(transaction_executor).unwrap());

Expand All @@ -542,8 +542,6 @@ impl BlockPublisher {
permission_verifier,
batch_observers,
batch_injector_factory,
block_header_class,
block_builder_class,
exit: Arc::new(Exit::new()),
};

Expand Down
37 changes: 12 additions & 25 deletions validator/src/journal/publisher_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ use std::mem;
use std::os::raw::{c_char, c_void};
use std::slice;

use cpython::{PyClone, PyList, PyObject, Python};
use cpython::{ObjectProtocol, PyClone, PyList, PyObject, Python};

use batch::Batch;
use block::Block;
use ffi::py_import_class;
use journal::block_manager::BlockManager;
use journal::publisher::{
BlockPublisher, FinalizeBlockError, IncomingBatchSender, InitializeBlockError,
};

use state::state_view_factory::StateViewFactory;

lazy_static! {
static ref PY_BATCH_PUBLISHER_CLASS: PyObject = py_import_class(
"sawtooth_validator.journal.consensus.batch_publisher",
"BatchPublisher"
);
}

#[repr(u32)]
#[derive(Debug)]
pub enum ErrorCode {
Expand Down Expand Up @@ -116,28 +124,9 @@ pub unsafe extern "C" fn block_publisher_new(
.iter(py)
.collect();

let batch_publisher_mod = py
.import("sawtooth_validator.journal.consensus.batch_publisher")
.expect("Unable to import 'sawtooth_validator.journal.consensus.batch_publisher'");
let batch_publisher = batch_publisher_mod
.call(
py,
"BatchPublisher",
(identity_signer.clone_ref(py), batch_sender),
None,
).expect("Unable to create BatchPublisher");

let block_header_class = py
.import("sawtooth_validator.protobuf.block_pb2")
.expect("Unable to import 'sawtooth_validator.protobuf.block_pb2'")
.get(py, "BlockHeader")
.expect("Unable to import BlockHeader from 'sawtooth_validator.protobuf.block_pb2'");

let block_builder_class = py
.import("sawtooth_validator.journal.block_builder")
.expect("Unable to import 'sawtooth_validator.journal.block_builder'")
.get(py, "BlockBuilder")
.expect("Unable to import BlockBuilder from 'sawtooth_validator.journal.block_builder'");
let batch_publisher = PY_BATCH_PUBLISHER_CLASS
.call(py, (identity_signer.clone_ref(py), batch_sender), None)
.expect("Unable to create BatchPublisher");

let publisher = BlockPublisher::new(
block_manager,
Expand All @@ -154,8 +143,6 @@ pub unsafe extern "C" fn block_publisher_new(
permission_verifier,
batch_observers,
batch_injector_factory,
block_header_class,
block_builder_class,
);

*block_publisher_ptr = Box::into_raw(Box::new(publisher)) as *const c_void;
Expand Down

0 comments on commit e0116d7

Please sign in to comment.