Skip to content

Commit

Permalink
fix the exception from bcs library
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Apr 12, 2024
1 parent 3293c2e commit 6328d50
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 78 deletions.
2 changes: 1 addition & 1 deletion crates/rooch-benchmarks/benches/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn l1_block_encode_size_benchmark(c: &mut Criterion) {
let l1_block = L1Block::default();
c.bench_function("l1_block_encode_siz", |b| {
b.iter(|| {
let _ = l1_block.encode().len() as u64;
let _ = l1_block.encode().expect("L1Block encode failed").len() as u64;
})
});
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl ExecutorActor {
for (mut genesis_tx, (state_root, size, genesis_tx_output)) in
self.genesis.genesis_txs().into_iter().zip(genesis_result)
{
let tx_hash = genesis_tx.tx_hash();
let tx_hash = genesis_tx.tx_hash()?;
self.handle_tx_output(tx_hash, state_root, size, genesis_tx_output)?;
}

Expand Down Expand Up @@ -258,7 +258,7 @@ impl ExecutorActor {

let authenticator = tx.authenticator_info()?;

let mut moveos_tx: MoveOSTransaction = tx.into();
let mut moveos_tx: MoveOSTransaction = tx.try_into()?;

let vm_result = self.validate_authenticator(&moveos_tx.ctx, authenticator)?;

Expand Down
13 changes: 8 additions & 5 deletions crates/rooch-indexer/src/actor/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,14 @@ impl Handler<IndexerEventsMessage> for IndexerActor {
moveos_tx,
} = msg;

let events: Vec<_> = events
.into_iter()
.map(|event| IndexedEvent::new(event, transaction.clone(), moveos_tx.clone()))
.collect();
self.indexer_store.persist_events(events)?;
let mut events_vec = Vec::new();
for event in events.iter() {
let indexed_event =
IndexedEvent::new(event.clone(), transaction.clone(), moveos_tx.clone())?;
events_vec.push(indexed_event);
}

self.indexer_store.persist_events(events_vec)?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/rooch-indexer/src/tests/test_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn test_event_store() -> Result<()> {
};

let indexed_event =
IndexedEvent::new(random_event, random_transaction, random_moveos_tx.clone());
IndexedEvent::new(random_event, random_transaction, random_moveos_tx.clone())?;
let events = vec![indexed_event];
let _ = indexer_store.persist_events(events)?;

Expand Down
10 changes: 5 additions & 5 deletions crates/rooch-indexer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl IndexedTransaction {
};
//TODO index L1Block
let indexed_transaction = IndexedTransaction {
tx_hash: transaction.tx_hash(),
tx_hash: transaction.tx_hash()?,
// The tx order of this transaction.
tx_order: transaction.sequence_info.tx_order,

Expand Down Expand Up @@ -135,21 +135,21 @@ impl IndexedEvent {
event: Event,
mut transaction: LedgerTransaction,
moveos_tx: VerifiedMoveOSTransaction,
) -> Self {
IndexedEvent {
) -> Result<Self> {
Ok(IndexedEvent {
event_handle_id: event.event_id.event_handle_id,
event_seq: event.event_id.event_seq,
event_type: event.event_type,
event_data: event.event_data,
event_index: event.event_index,

tx_hash: transaction.tx_hash(),
tx_hash: transaction.tx_hash()?,
tx_order: transaction.sequence_info.tx_order,
sender: moveos_tx.ctx.sender,

//TODO record transaction timestamp
created_at: 0,
}
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-key/src/keystore/base_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl AccountKeystore for BaseKeyStore {
RoochError::SignMessageError(format!("Cannot find key for address: [{address}]"))
})?;

let signature = Signature::new_hashed(msg.tx_hash().as_bytes(), &kp);
let signature = Signature::new_hashed(msg.tx_hash()?.as_bytes(), &kp);

let auth = authenticator::Authenticator::rooch(signature);

Expand Down Expand Up @@ -286,7 +286,7 @@ impl AccountKeystore for BaseKeyStore {
let kp: RoochKeyPair = retrieve_key_pair(&local_session_key.private_key, password)
.map_err(signature::Error::from_source)?;

let signature = Signature::new_hashed(msg.tx_hash().as_bytes(), &kp);
let signature = Signature::new_hashed(msg.tx_hash()?.as_bytes(), &kp);

let auth = authenticator::Authenticator::rooch(signature);
Ok(RoochTransaction::new(msg, auth))
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-relayer/src/actor/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl RelayerActor {
.executor
.get_sequence_number(self.relayer_address)
.await?;
let tx_hash = l1_block.block.tx_hash();
let tx_hash = l1_block.block.tx_hash()?;
let ctx = TxContext::new(
self.relayer_address,
sequence_number,
Expand Down
17 changes: 17 additions & 0 deletions crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ pub struct TransactionView {
pub raw: BytesView,
}

/*
impl TryFrom<RoochTransaction> for TransactionView {
type Error = anyhow::Error;
fn try_from(transaction: RoochTransaction) -> Result<Self, Self::Error> {
Ok(Self {
sequence_number: transaction.sequence_number(),
sender: transaction.sender().to_string(),
action: transaction.action().clone().into(),
action_type: transaction.action().clone().into(),
raw: transaction.encode()?.into(),
})
}
}
*/

impl From<RoochTransaction> for TransactionView {
fn from(transaction: RoochTransaction) -> Self {
Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-rpc-client/src/wallet_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl WalletContext {
})?;

let tx_data = self.build_tx_data(sender, action).await?;
let signature = Signature::new_hashed(tx_data.tx_hash().as_bytes(), &kp);
let signature = Signature::new_hashed(tx_data.tx_hash()?.as_bytes(), &kp);
Ok(RoochTransaction::new(
tx_data,
Authenticator::rooch(signature),
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-rpc-server/src/server/rooch_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl RoochAPIServer for RoochServer {
bcs::from_bytes::<RoochTransaction>(&payload.0).map_err(anyhow::Error::from)?;
info!("send_raw_transaction tx: {:?}", tx);

let hash = tx.tx_hash();
let hash = tx.tx_hash()?;
self.rpc_service.quene_tx(tx).await?;
Ok(hash.into())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-sequencer/src/actor/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl SequencerActor {
} else {
self.last_order + 1
};
let hash = tx_data.tx_hash();
let hash = tx_data.tx_hash()?;
let mut witness_data = hash.as_ref().to_vec();
witness_data.extend(tx_order.to_le_bytes().iter());
let witness_hash = h256::sha3_256_of(&witness_data);
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-store/src/transaction_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl TransactionDBStore {
}
}

pub fn save_transaction(&mut self, mut transaction: LedgerTransaction) -> Result<()> {
let tx_hash = transaction.tx_hash();
pub fn save_transaction(&mut self, transaction: LedgerTransaction) -> Result<()> {
let tx_hash = transaction.tx_hash()?;
let tx_order = transaction.sequence_info.tx_order;
self.tx_store.kv_put(tx_hash, transaction)?;
self.tx_sequence_info_mapping_store
Expand Down
120 changes: 84 additions & 36 deletions crates/rooch-types/src/bitcoin/light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,30 @@ impl<'a> BitcoinLightClientModule<'a> {
vec![MoveValue::Address(block_hash.into_address())],
);
let ctx = TxContext::new_readonly_ctx(AccountAddress::ZERO);
let block_header =
self.caller
.call_function(&ctx, call)?
.into_result()
.map(|mut values| {
let value = values.pop().expect("should have one return value");
bcs::from_bytes::<MoveOption<Header>>(&value.value)
.expect("should be a valid MoveOption<BlockHeader>")
})?;
let block_header = {
let call_result = self.caller.call_function(&ctx, call)?.into_result();
match call_result {
Ok(mut return_vec) => {
let first_return_value = match return_vec.pop() {
None => return Err(anyhow::Error::msg("should have one return value")),
Some(v) => v,
};
match bcs::from_bytes::<MoveOption<Header>>(&first_return_value.value) {
Ok(header) => header,
Err(_) => {
return Err(anyhow::Error::msg(
"should be a valid MoveOption<BlockHeader>",
))
}
}
}
Err(_) => {
return Err(anyhow::Error::msg(
"should be a valid MoveOption<BlockHeader>",
))
}
}
};
Ok(block_header.into())
}

Expand All @@ -100,15 +115,30 @@ impl<'a> BitcoinLightClientModule<'a> {
vec![MoveValue::U64(block_height)],
);
let ctx = TxContext::new_readonly_ctx(AccountAddress::ZERO);
let block_header =
self.caller
.call_function(&ctx, call)?
.into_result()
.map(|mut values| {
let value = values.pop().expect("should have one return value");
bcs::from_bytes::<MoveOption<Header>>(&value.value)
.expect("should be a valid MoveOption<BlockHeader>")
})?;
let block_header = {
let call_result = self.caller.call_function(&ctx, call)?.into_result();
match call_result {
Ok(mut return_vec) => {
let first_return_value = match return_vec.pop() {
None => return Err(anyhow::Error::msg("should have one return value")),
Some(v) => v,
};
match bcs::from_bytes::<MoveOption<Header>>(&first_return_value.value) {
Ok(header) => header,
Err(_) => {
return Err(anyhow::Error::msg(
"should be a valid MoveOption<BlockHeader>",
))
}
}
}
Err(_) => {
return Err(anyhow::Error::msg(
"should be a valid MoveOption<BlockHeader>",
))
}
}
};
Ok(block_header.into())
}

Expand All @@ -119,31 +149,49 @@ impl<'a> BitcoinLightClientModule<'a> {
vec![MoveValue::Address(block_hash.into_address())],
);
let ctx = TxContext::new_readonly_ctx(AccountAddress::ZERO);
let height = self
.caller
.call_function(&ctx, call)?
.into_result()
.map(|mut values| {
let value = values.pop().expect("should have one return value");
bcs::from_bytes::<MoveOption<u64>>(&value.value)
.expect("should be a valid MoveOption<u64>")
})?;
let height = {
let call_result = self.caller.call_function(&ctx, call)?.into_result();
match call_result {
Ok(mut return_vec) => {
let first_return_value = match return_vec.pop() {
None => return Err(anyhow::Error::msg("should have one return value")),
Some(v) => v,
};
match bcs::from_bytes::<MoveOption<u64>>(&first_return_value.value) {
Ok(header) => header,
Err(_) => {
return Err(anyhow::Error::msg("should be a valid MoveOption<u64>"))
}
}
}
Err(_) => return Err(anyhow::Error::msg("should be a valid MoveOption<u64>")),
}
};
Ok(height.into())
}

pub fn get_latest_block_height(&self) -> Result<Option<u64>> {
let call =
Self::create_function_call(Self::GET_LATEST_BLOCK_HEIGHT_FUNCTION_NAME, vec![], vec![]);
let ctx = TxContext::new_readonly_ctx(AccountAddress::ZERO);
let height = self
.caller
.call_function(&ctx, call)?
.into_result()
.map(|mut values| {
let value = values.pop().expect("should have one return value");
bcs::from_bytes::<MoveOption<u64>>(&value.value)
.expect("should be a valid MoveOption<u64>")
})?;
let height = {
let call_result = self.caller.call_function(&ctx, call)?.into_result();
match call_result {
Ok(mut return_vec) => {
let first_return_value = match return_vec.pop() {
None => return Err(anyhow::Error::msg("should have one return value")),
Some(v) => v,
};
match bcs::from_bytes::<MoveOption<u64>>(&first_return_value.value) {
Ok(header) => header,
Err(_) => {
return Err(anyhow::Error::msg("should be a valid MoveOption<u64>"))
}
}
}
Err(_) => return Err(anyhow::Error::msg("should be a valid MoveOption<u64>")),
}
};
Ok(height.into())
}

Expand Down
16 changes: 10 additions & 6 deletions crates/rooch-types/src/transaction/ledger_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ pub struct L1Block {
}

impl L1Block {
pub fn encode(&self) -> Vec<u8> {
bcs::to_bytes(self).expect("encode transaction should success")
pub fn encode(&self) -> Result<Vec<u8>> {
match bcs::to_bytes(self) {
Ok(v) => Ok(v),
Err(_) => Err(anyhow::Error::msg("encode transaction should success")),
}
}

pub fn tx_hash(&self) -> H256 {
moveos_types::h256::sha3_256_of(self.encode().as_slice())
pub fn tx_hash(&self) -> Result<H256> {
let block_bytes = self.encode()?;
Ok(moveos_types::h256::sha3_256_of(block_bytes.as_slice()))
}

pub fn tx_size(&self) -> u64 {
Expand All @@ -41,7 +45,7 @@ pub enum LedgerTxData {
}

impl LedgerTxData {
pub fn tx_hash(&mut self) -> H256 {
pub fn tx_hash(&self) -> Result<H256> {
match self {
LedgerTxData::L1Block(block) => block.tx_hash(),
LedgerTxData::L2Tx(tx) => tx.tx_hash(),
Expand Down Expand Up @@ -87,7 +91,7 @@ impl LedgerTransaction {
}
}

pub fn tx_hash(&mut self) -> H256 {
pub fn tx_hash(&self) -> Result<H256> {
self.data.tx_hash()
}

Expand Down
Loading

0 comments on commit 6328d50

Please sign in to comment.