Skip to content

Commit

Permalink
Merge pull request #201 from lxfind/fix-txcontext
Browse files Browse the repository at this point in the history
Consolidate TxContext and TxContextMove
  • Loading branch information
lxfind committed Jan 18, 2022
2 parents e119c22 + fee84dc commit 3e7adc1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion fastpay_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl AuthorityState {

// Insert into the certificates map
let transaction_digest = certificate.order.digest();
let mut tx_ctx = TxContext::new(transaction_digest);
let mut tx_ctx = TxContext::new(order.sender(), transaction_digest);

// Order-specific logic
let mut temporary_store = AuthorityTemporaryStore::new(self, &inputs);
Expand Down
4 changes: 2 additions & 2 deletions fastpay_core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async fn test_publish_dependent_module_ok() {
vec![dependent_module_bytes],
&sender_key,
);
let dependent_module_id = TxContext::new(order.digest()).fresh_id();
let dependent_module_id = TxContext::new(&sender, order.digest()).fresh_id();

// Object does not exist
assert!(authority.object_state(&dependent_module_id).await.is_err());
Expand Down Expand Up @@ -313,7 +313,7 @@ async fn test_publish_module_no_dependencies_ok() {
let module_bytes = vec![module_bytes];
let gas_cost = calculate_module_publish_cost(&module_bytes);
let order = Order::new_module(sender, gas_payment_object_ref, module_bytes, &sender_key);
let _module_object_id = TxContext::new(order.digest()).fresh_id();
let _module_object_id = TxContext::new(&sender, order.digest()).fresh_id();
let _response = send_and_confirm_order(&mut authority, order).await.unwrap();

// check that the module actually got published
Expand Down
2 changes: 1 addition & 1 deletion fastx_programmability/adapter/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ fn resolve_and_type_check(
}
}
args.append(&mut pure_args);
args.push(ctx.to_bcs_bytes_hack());
args.push(ctx.to_vec());

Ok(TypeCheckSuccess {
module_id,
Expand Down
6 changes: 3 additions & 3 deletions fastx_programmability/adapter/src/unit_tests/adapter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn call(
pure_args,
gas_budget,
gas_object,
TxContext::random(),
TxContext::random_for_testing_only(),
)
}

Expand Down Expand Up @@ -487,7 +487,7 @@ fn test_publish_module_insufficient_gas() {
module.serialize(&mut module_bytes).unwrap();
let module_bytes = vec![module_bytes];

let mut tx_context = TxContext::random();
let mut tx_context = TxContext::random_for_testing_only();
let response = adapter::publish(
&mut storage,
natives,
Expand Down Expand Up @@ -657,7 +657,7 @@ fn test_publish_module_linker_error() {
dependent_module.serialize(&mut module_bytes).unwrap();
let module_bytes = vec![module_bytes];

let mut tx_context = TxContext::random();
let mut tx_context = TxContext::random_for_testing_only();
let response = adapter::publish(
&mut storage,
natives,
Expand Down
48 changes: 23 additions & 25 deletions fastx_types/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ impl PublicKeyBytes {
// to ensure the bytes represent a point on the curve.
PublicKey::from_bytes(self.as_ref()).map_err(|_| FastPayError::InvalidAuthenticator)
}

// for testing
pub fn random_for_testing_only() -> Self {
use rand::Rng;
let random_bytes = rand::thread_rng().gen::<[u8; dalek::PUBLIC_KEY_LENGTH]>();
Self(random_bytes)
}
}

impl AsRef<[u8]> for PublicKeyBytes {
Expand Down Expand Up @@ -87,60 +94,51 @@ pub struct ObjectDigest(pub [u8; 32]); // We use SHA3-256 hence 32 bytes here
pub const TX_CONTEXT_MODULE_NAME: &IdentStr = ident_str!("TxContext");
pub const TX_CONTEXT_STRUCT_NAME: &IdentStr = TX_CONTEXT_MODULE_NAME;

#[derive(Debug)]
#[derive(Debug, Deserialize, Serialize)]
pub struct TxContext {
/// Signer/sender of the transaction
sender: Vec<u8>,
/// Digest of the current transaction
digest: TransactionDigest,
digest: Vec<u8>,
/// Number of `ObjectID`'s generated during execution of the current transaction
ids_created: u64,
}

impl TxContext {
pub fn new(digest: TransactionDigest) -> Self {
pub fn new(sender: &FastPayAddress, digest: TransactionDigest) -> Self {
Self {
digest,
sender: sender.to_vec(),
digest: digest.0.to_vec(),
ids_created: 0,
}
}

/// Derive a globally unique object ID by hashing self.digest | self.ids_created
pub fn fresh_id(&mut self) -> ObjectID {
let id = self.digest.derive_id(self.ids_created);
let id = self.digest().derive_id(self.ids_created);

self.ids_created += 1;
id
}

/// Return the transaction digest, to include in new objects
pub fn digest(&self) -> TransactionDigest {
self.digest
TransactionDigest::new(self.digest.clone().try_into().unwrap())
}

// TODO(https://github.com/MystenLabs/fastnft/issues/89): temporary hack for Move compatibility
pub fn to_bcs_bytes_hack(&self) -> Vec<u8> {
let sender = FastPayAddress::default();
let inputs_hash = self.digest.0.to_vec();
let obj = TxContextForMove {
sender: sender.to_vec(),
inputs_hash,
ids_created: self.ids_created,
};
bcs::to_bytes(&obj).unwrap()
pub fn to_vec(&self) -> Vec<u8> {
bcs::to_bytes(&self).unwrap()
}

// for testing
pub fn random() -> Self {
Self::new(TransactionDigest::random())
pub fn random_for_testing_only() -> Self {
Self::new(
&FastPayAddress::random_for_testing_only(),
TransactionDigest::random(),
)
}
}

#[derive(Serialize)]
struct TxContextForMove {
sender: Vec<u8>,
inputs_hash: Vec<u8>,
ids_created: u64,
}

impl TransactionDigest {
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
Expand Down

1 comment on commit 3e7adc1

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bench results

�[0m�[0m�[1m�[32m Finished�[0m release [optimized + debuginfo] target(s) in 3.18s
�[0m�[0m�[1m�[32m Running�[0m target/release/bench
[2022-01-19T00:03:25Z INFO bench] Starting benchmark: OrdersAndCerts
[2022-01-19T00:03:25Z INFO bench] Preparing accounts.
[2022-01-19T00:03:25Z INFO bench] Open database on path: "/tmp/DB_1FF8E33BC964C9B1A2D6DDFA9256F1AD"
[2022-01-19T00:03:29Z INFO bench] Preparing transactions.
[2022-01-19T00:03:38Z INFO fastpay::network] Listening to Tcp traffic on 127.0.0.1:9555
[2022-01-19T00:03:39Z INFO bench] Number of TCP connections: 2
[2022-01-19T00:03:39Z INFO bench] Set max_in_flight to 500
[2022-01-19T00:03:39Z INFO bench] Sending requests.
[2022-01-19T00:03:39Z INFO fastpay::network] Sending Tcp requests to 127.0.0.1:9555
[2022-01-19T00:03:40Z INFO fastpay::network] 127.0.0.1:9555 has processed 5000 packets
[2022-01-19T00:03:42Z INFO fastpay::network] In flight 500 Remaining 35000
[2022-01-19T00:03:42Z INFO fastpay::network] 127.0.0.1:9555 has processed 10000 packets
[2022-01-19T00:03:43Z INFO fastpay::network] 127.0.0.1:9555 has processed 15000 packets
[2022-01-19T00:03:44Z INFO fastpay::network] In flight 500 Remaining 30000
[2022-01-19T00:03:45Z INFO fastpay::network] 127.0.0.1:9555 has processed 20000 packets
[2022-01-19T00:03:45Z INFO fastpay::network] In flight 500 Remaining 30000
[2022-01-19T00:03:46Z INFO fastpay::network] 127.0.0.1:9555 has processed 25000 packets
[2022-01-19T00:03:47Z INFO fastpay::network] In flight 500 Remaining 25000
[2022-01-19T00:03:47Z INFO fastpay::network] 127.0.0.1:9555 has processed 30000 packets
[2022-01-19T00:03:49Z INFO fastpay::network] 127.0.0.1:9555 has processed 35000 packets
[2022-01-19T00:03:50Z INFO fastpay::network] In flight 500 Remaining 20000
[2022-01-19T00:03:50Z INFO fastpay::network] 127.0.0.1:9555 has processed 40000 packets
[2022-01-19T00:03:51Z INFO fastpay::network] 127.0.0.1:9555 has processed 45000 packets
[2022-01-19T00:03:52Z INFO fastpay::network] In flight 500 Remaining 15000
[2022-01-19T00:03:52Z INFO fastpay::network] 127.0.0.1:9555 has processed 50000 packets
[2022-01-19T00:03:53Z INFO fastpay::network] 127.0.0.1:9555 has processed 55000 packets
[2022-01-19T00:03:53Z INFO fastpay::network] In flight 500 Remaining 10000
[2022-01-19T00:03:53Z INFO fastpay::network] 127.0.0.1:9555 has processed 60000 packets
[2022-01-19T00:03:54Z INFO fastpay::network] 127.0.0.1:9555 has processed 65000 packets
[2022-01-19T00:03:55Z INFO fastpay::network] In flight 500 Remaining 5000
[2022-01-19T00:03:55Z INFO fastpay::network] 127.0.0.1:9555 has processed 70000 packets
[2022-01-19T00:03:56Z INFO fastpay::network] 127.0.0.1:9555 has processed 75000 packets
[2022-01-19T00:03:57Z INFO fastpay::network] Done sending Tcp requests to 127.0.0.1:9555
[2022-01-19T00:03:57Z INFO fastpay::network] 127.0.0.1:9555 has processed 80000 packets
[2022-01-19T00:03:57Z INFO bench] Received 80000 responses.
[2022-01-19T00:03:57Z WARN bench] Completed benchmark for OrdersAndCerts
Total time: 17825113us, items: 40000, tx/sec: 2244.0250448903184

Please sign in to comment.