Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate TxContext and TxContextMove #201

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a seeded RNG? Want to make sure we get the same keys on every run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for testing only though, does that matter?

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