Skip to content

Commit

Permalink
[feature] hyperledger#2073: Prefer ConstString over String for data_m…
Browse files Browse the repository at this point in the history
…odel

Signed-off-by: Ales Tsurko <ales.tsurko@gmail.com>
  • Loading branch information
ales-tsurko committed Jul 19, 2022
1 parent 64d0993 commit 26c8971
Show file tree
Hide file tree
Showing 27 changed files with 105 additions and 90 deletions.
4 changes: 2 additions & 2 deletions cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ pub fn get_trusted_peers(public_key: Option<&PublicKey>) -> HashSet<PeerId> {
]
.iter()
.map(|(a, k)| PeerId {
address: (*a).to_string(),
address: (*a).into(),
public_key: PublicKey::from_str(k).unwrap(),
})
.collect();
if let Some(pubkey) = public_key {
trusted_peers.insert(PeerId {
address: DEFAULT_TORII_P2P_ADDR.to_owned(),
address: DEFAULT_TORII_P2P_ADDR.into(),
public_key: pubkey.clone(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion core/benches/sumeragi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const N_PEERS: usize = 255;
fn get_n_peers(n: usize) -> Vec<PeerId> {
(0..n)
.map(|i| PeerId {
address: format!("127.0.0.{}", i),
address: format!("127.0.0.{}", i).into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
Expand Down
3 changes: 2 additions & 1 deletion core/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ mod tests {

let message = std::iter::repeat_with(rand::random::<char>)
.take(16)
.collect();
.collect::<String>()
.into();
let instructions: Vec<Instruction> = vec![FailBox { message }.into()];
let tx = Transaction::new(
AccountId::from_str(account_id).expect("Valid"),
Expand Down
12 changes: 6 additions & 6 deletions core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod isi {
let store: &mut Metadata = asset
.try_as_mut()
.map_err(eyre::Error::from)
.map_err(|e| Error::Conversion(e.to_string()))?;
.map_err(|e| Error::Conversion(e.to_string().into()))?;
store.insert_with_limits(self.key, self.value, asset_metadata_limits)?;

Ok(AssetEvent::MetadataInserted(asset_id.clone()))
Expand All @@ -59,7 +59,7 @@ pub mod isi {
let store: &mut Metadata = asset
.try_as_mut()
.map_err(eyre::Error::from)
.map_err(|e| Error::Conversion(e.to_string()))?;
.map_err(|e| Error::Conversion(e.to_string().into()))?;
store
.remove(&self.key)
.ok_or(FindError::MetadataKey(self.key))?;
Expand Down Expand Up @@ -167,7 +167,7 @@ pub mod isi {
let quantity: &mut Self = asset
.try_as_mut()
.map_err(eyre::Error::from)
.map_err(|e| Error::Conversion(e.to_string()))?;
.map_err(|e| Error::Conversion(e.to_string().into()))?;
*quantity = quantity
.checked_add(mint.object)
.ok_or(MathError::Overflow)?;
Expand Down Expand Up @@ -204,7 +204,7 @@ pub mod isi {
let quantity: &mut Self = asset
.try_as_mut()
.map_err(eyre::Error::from)
.map_err(|e| Error::Conversion(e.to_string()))?;
.map_err(|e| Error::Conversion(e.to_string().into()))?;
*quantity = quantity
.checked_sub(burn.object)
.ok_or(MathError::NotEnoughQuantity)?;
Expand Down Expand Up @@ -245,7 +245,7 @@ pub mod isi {
let quantity: &mut Self = asset
.try_as_mut()
.map_err(eyre::Error::from)
.map_err(|e| Error::Conversion(e.to_string()))?;
.map_err(|e| Error::Conversion(e.to_string().into()))?;
*quantity = quantity
.checked_sub(transfer.object)
.ok_or(MathError::NotEnoughQuantity)?;
Expand All @@ -256,7 +256,7 @@ pub mod isi {
let quantity: &mut Self = asset
.try_as_mut()
.map_err(eyre::Error::from)
.map_err(|e| Error::Conversion(e.to_string()))?;
.map_err(|e| Error::Conversion(e.to_string().into()))?;
*quantity = quantity
.checked_add(transfer.object)
.ok_or(MathError::Overflow)?;
Expand Down
24 changes: 12 additions & 12 deletions core/src/smartcontracts/isi/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where

V::try_from(expr)
.map_err(Into::into)
.map_err(|e| Error::Conversion(e.to_string()))
.map_err(|e| Error::Conversion(e.to_string().into()))
}
}

Expand Down Expand Up @@ -409,10 +409,10 @@ mod tests {
let expression = WhereBuilder::evaluate(condition.clone())
.with_value(
//TODO: use query to get the actual quantity of an asset from WSV
"usd_quantity".to_owned(),
"usd_quantity".into(),
asset_quantity_high.clone(),
)
.with_value("signatories".to_owned(), teller_signatory_set)
.with_value("signatories".into(), teller_signatory_set)
.build();
let wsv = WorldStateView::new(World::new());
assert_eq!(
Expand All @@ -421,26 +421,26 @@ mod tests {
);
// Signed by manager
let expression = WhereBuilder::evaluate(condition.clone())
.with_value("usd_quantity".to_owned(), asset_quantity_high.clone())
.with_value("signatories".to_owned(), manager_signatory_set)
.with_value("usd_quantity".into(), asset_quantity_high.clone())
.with_value("signatories".into(), manager_signatory_set)
.build();
assert_eq!(
expression.evaluate(&wsv, &Context::new())?,
Value::Bool(true)
);
// Signed by one teller
let expression = WhereBuilder::evaluate(condition.clone())
.with_value("usd_quantity".to_owned(), asset_quantity_high)
.with_value("signatories".to_owned(), one_teller_set.clone())
.with_value("usd_quantity".into(), asset_quantity_high)
.with_value("signatories".into(), one_teller_set.clone())
.build();
assert_eq!(
expression.evaluate(&wsv, &Context::new())?,
Value::Bool(false)
);
// Signed by one teller with less value
let expression = WhereBuilder::evaluate(condition)
.with_value("usd_quantity".to_owned(), asset_quantity_low)
.with_value("signatories".to_owned(), one_teller_set)
.with_value("usd_quantity".into(), asset_quantity_low)
.with_value("signatories".into(), one_teller_set)
.build();
assert_eq!(
expression.evaluate(&wsv, &Context::new())?,
Expand All @@ -453,7 +453,7 @@ mod tests {
fn where_expression() -> Result<()> {
assert_eq!(
WhereBuilder::evaluate(ContextValue::new("test_value"))
.with_value("test_value".to_owned(), Add::new(2_u32, 3_u32))
.with_value("test_value".into(), Add::new(2_u32, 3_u32))
.build()
.evaluate(&WorldStateView::new(World::new()), &Context::new())?,
Value::U32(5)
Expand All @@ -464,11 +464,11 @@ mod tests {
#[test]
fn nested_where_expression() -> Result<()> {
let expression = WhereBuilder::evaluate(ContextValue::new("a"))
.with_value("a".to_owned(), 2_u32)
.with_value("a".into(), 2_u32)
.build();
let outer_expression: ExpressionBox =
WhereBuilder::evaluate(Add::new(expression, ContextValue::new("b")))
.with_value("b".to_owned(), 4_u32)
.with_value("b".into(), 4_u32)
.build()
.into();
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub mod error {
match err {
FixedPointOperationError::NegativeValue(_) => Self::Math(MathError::NegativeValue),
FixedPointOperationError::Conversion(e) => {
Self::Conversion(format!("Mathematical conversion failed. {}", e))
Self::Conversion(format!("Mathematical conversion failed. {}", e).into())
}
FixedPointOperationError::Overflow => MathError::Overflow.into(),
FixedPointOperationError::DivideByZero => MathError::DivideByZero.into(),
Expand Down
8 changes: 6 additions & 2 deletions core/src/smartcontracts/isi/permissions/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub fn check_instruction_permissions(
check_permissions_directly(account_id, revoked_instructions, instruction_judge, wsv)?;

check_query_in_instruction(account_id, instruction, wsv, query_judge)
.map_err(|reason| NotPermittedFail { reason })
.map_err(|reason| NotPermittedFail {
reason: reason.into(),
})
.map_err(TransactionRejectionReason::NotPermitted)?;

Ok(())
Expand All @@ -39,7 +41,9 @@ fn check_permissions_directly(
for isi in instructions {
is_instruction_allowed
.judge(account_id, isi, wsv)
.map_err(|reason| NotPermittedFail { reason })
.map_err(|reason| NotPermittedFail {
reason: reason.into(),
})
.map_err(TransactionRejectionReason::NotPermitted)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions core/src/smartcontracts/isi/permissions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ mod tests {
let instruction_burn: Instruction =
BurnBox::new(Value::U32(10), asset_id("xor", "test", "alice", "test")).into();
let instruction_fail = Instruction::Fail(FailBox {
message: "fail message".to_owned(),
message: "fail message".into(),
});
let account_bob = <Account as Identifiable>::Id::from_str("bob@test").expect("Valid");
let account_alice = <Account as Identifiable>::Id::from_str("alice@test").expect("Valid");
Expand All @@ -358,7 +358,7 @@ mod tests {
let instruction_burn: Instruction =
BurnBox::new(Value::U32(10), asset_id("xor", "test", "alice", "test")).into();
let instruction_fail = Instruction::Fail(FailBox {
message: "fail message".to_owned(),
message: "fail message".into(),
});
let nested_instruction_sequence =
Instruction::If(If::new(true, instruction_burn.clone()).into());
Expand Down
8 changes: 4 additions & 4 deletions core/src/smartcontracts/isi/triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ pub mod isi {
FilterBox::Data(_) => triggers.add_data_trigger(
new_trigger
.try_into()
.map_err(|e: &str| Self::Error::Conversion(e.to_owned()))?,
.map_err(|e: &str| Self::Error::Conversion(e.into()))?,
),
FilterBox::Pipeline(_) => triggers.add_pipeline_trigger(
new_trigger
.try_into()
.map_err(|e: &str| Self::Error::Conversion(e.to_owned()))?,
.map_err(|e: &str| Self::Error::Conversion(e.into()))?,
),
FilterBox::Time(_) => triggers.add_time_trigger(
new_trigger
.try_into()
.map_err(|e: &str| Self::Error::Conversion(e.to_owned()))?,
.map_err(|e: &str| Self::Error::Conversion(e.into()))?,
),
FilterBox::ExecuteTrigger(_) => triggers.add_by_call_trigger(
new_trigger
.try_into()
.map_err(|e: &str| Self::Error::Conversion(e.to_owned()))?,
.map_err(|e: &str| Self::Error::Conversion(e.into()))?,
),
};
if success {
Expand Down
2 changes: 1 addition & 1 deletion core/src/sumeragi/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl SumeragiConfiguration {
let (public_key, _) = Self::placeholder_keypair().into();

PeerId {
address: "127.0.0.1:1337".to_owned(),
address: "127.0.0.1:1337".into(),
public_key,
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/sumeragi/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ impl<G: GenesisNetworkTrait, F: FaultInjection> SumeragiWithFault<G, F> {
info!(%peer_to_be_connected.address, "Connecting peer");
self.broker
.issue_send(ConnectPeer {
address: peer_to_be_connected.address.clone(),
address: peer_to_be_connected.address.to_string(),
})
.await
}
Expand Down
14 changes: 7 additions & 7 deletions core/src/sumeragi/network_topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,21 +418,21 @@ mod tests {
#[should_panic]
fn wrong_number_of_peers_genesis() {
let peer_1: PeerId = PeerId {
address: "127.0.0.1".to_owned(),
address: "127.0.0.1".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
.clone(),
};
let peer_2: PeerId = PeerId {
address: "127.0.0.2".to_owned(),
address: "127.0.0.2".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
.clone(),
};
let peer_3: PeerId = PeerId {
address: "127.0.0.3".to_owned(),
address: "127.0.0.3".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
Expand Down Expand Up @@ -467,28 +467,28 @@ mod tests {
fn topology_test_peers() -> HashSet<PeerId> {
vec![
PeerId {
address: "127.0.0.1:7878".to_owned(),
address: "127.0.0.1:7878".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
.clone(),
},
PeerId {
address: "127.0.0.1:7879".to_owned(),
address: "127.0.0.1:7879".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
.clone(),
},
PeerId {
address: "127.0.0.1:7880".to_owned(),
address: "127.0.0.1:7880".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
.clone(),
},
PeerId {
address: "127.0.0.1:7881".to_owned(),
address: "127.0.0.1:7881".into(),
public_key: KeyPair::generate()
.expect("Failed to generate KeyPair.")
.public_key()
Expand Down
20 changes: 11 additions & 9 deletions core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ impl TransactionValidator {
.domain(&account_id.domain_id)
.map_err(|_e| {
TransactionRejectionReason::NotPermitted(NotPermittedFail {
reason: "Domain not found in Iroha".to_owned(),
reason: "Domain not found in Iroha".into(),
})
})?
.contains_account(account_id)
{
return Err(TransactionRejectionReason::NotPermitted(NotPermittedFail {
reason: "Account not found in Iroha".to_owned(),
reason: "Account not found in Iroha".into(),
}));
}

Expand All @@ -140,15 +140,15 @@ impl TransactionValidator {
.execute(account_id.clone(), &wsv)
.map_err(|reason| InstructionExecutionFail {
instruction: instruction.clone(),
reason: reason.to_string(),
reason: reason.to_string().into(),
})
.map_err(TransactionRejectionReason::InstructionExecution)?;
}
}
Executable::Wasm(bytes) => {
let mut wasm_runtime = wasm::Runtime::new()
.map_err(|reason| WasmExecutionFail {
reason: reason.to_string(),
reason: reason.to_string().into(),
})
.map_err(TransactionRejectionReason::WasmExecution)?;
wasm_runtime
Expand All @@ -161,7 +161,7 @@ impl TransactionValidator {
Arc::clone(&self.query_judge),
)
.map_err(|reason| WasmExecutionFail {
reason: reason.to_string(),
reason: reason.to_string().into(),
})
.map_err(TransactionRejectionReason::WasmExecution)?;
}
Expand All @@ -184,7 +184,9 @@ impl TransactionValidator {
Ok(false) => Some("Signature condition not satisfied.".to_owned()),
Err(reason) => Some(reason.to_string()),
}
.map(|reason| UnsatisfiedSignatureConditionFail { reason })
.map(|reason| UnsatisfiedSignatureConditionFail {
reason: reason.into(),
})
.map(TransactionRejectionReason::UnsatisfiedSignatureCondition);

if let Some(reason) = option_reason {
Expand Down Expand Up @@ -312,11 +314,11 @@ fn check_signature_condition(
) -> EvaluatesTo<bool> {
WhereBuilder::evaluate(account.signature_check_condition().as_expression().clone())
.with_value(
String::from(iroha_data_model::account::ACCOUNT_SIGNATORIES_VALUE),
iroha_data_model::account::ACCOUNT_SIGNATORIES_VALUE.into(),
account.signatories().cloned().collect::<Vec<_>>(),
)
.with_value(
String::from(iroha_data_model::account::TRANSACTION_SIGNATORIES_VALUE),
iroha_data_model::account::TRANSACTION_SIGNATORIES_VALUE.into(),
signatories.into_iter().collect::<Vec<_>>(),
)
.build()
Expand Down Expand Up @@ -416,7 +418,7 @@ mod tests {
#[test]
fn transaction_not_accepted_max_instruction_number() {
let inst: Instruction = FailBox {
message: "Will fail".to_owned(),
message: "Will fail".into(),
}
.into();
let tx = Transaction::new(
Expand Down
Loading

0 comments on commit 26c8971

Please sign in to comment.