From e326d6fefe0fdd40c549c8e9f26dfd2d2be21b38 Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Thu, 18 Aug 2022 01:31:59 -0400 Subject: [PATCH] Reduce amount of copying/cloning within the transformation from `StreamerMessage` to `codec::Block` We were passing the top element as a reference as well as a bunch of other inner structures. This was causing unecessary cloning of elements but we didn't had ownership of the top-level structure. However this was pretty bad because we actually "own" the `StreamerMessage` which is dropped right after the transformation. So, everything is now "moved" which means much less data need to be copied/created and mostly everything can be simply be "moved" in the right location. There was also useless usage of `clone` here and there which was doubly not required. --- src/codec/mod.rs | 803 ++++++++++++++++++++++------------------------- src/main.rs | 3 +- 2 files changed, 373 insertions(+), 433 deletions(-) diff --git a/src/codec/mod.rs b/src/codec/mod.rs index 3fbe2f2d..3ae4a9d8 100644 --- a/src/codec/mod.rs +++ b/src/codec/mod.rs @@ -15,37 +15,28 @@ use near_indexer::StreamerMessage; use hex; use std::fmt::{Display, Formatter}; -impl From<&near_indexer::StreamerMessage> for Block { - fn from(sm: &StreamerMessage) -> Self { +use self::failure_execution_status::Failure; + +impl From for Block { + fn from(sm: StreamerMessage) -> Self { Block { - header: Some(BlockHeader::from(&sm.block.header)), - shards: sm.shards.iter().map(|s| IndexerShard::from(s)).collect(), - author: sm.block.author.to_string(), - chunk_headers: sm - .block - .chunks - .iter() - .map(|ch| ChunkHeader::from(ch)) - .collect(), + header: Some(BlockHeader::from(sm.block.header)), + shards: sm.shards.map_into(), + author: sm.block.author.into(), + chunk_headers: sm.block.chunks.map_into(), state_changes: vec![], } } } -impl From<&near_views::BlockHeaderView> for BlockHeader { - fn from(h: &near_views::BlockHeaderView) -> Self { - let challenges_result = &h.challenges_result; - let validator_proposals = &h.validator_proposals; - +impl From for BlockHeader { + fn from(h: near_views::BlockHeaderView) -> Self { BlockHeader { hash: Some(CryptoHash::from(h.hash)), height: h.height, prev_hash: Some(CryptoHash::from(h.prev_hash)), timestamp_nanosec: h.timestamp_nanosec, - prev_height: match h.prev_height { - None => 0, - Some(ph) => ph.into(), - }, + prev_height: h.prev_height.unwrap_or(0), epoch_id: Some(CryptoHash::from(h.epoch_id)), next_epoch_id: Some(CryptoHash::from(h.next_epoch_id)), prev_state_root: Some(CryptoHash::from(h.prev_state_root)), @@ -57,18 +48,12 @@ impl From<&near_views::BlockHeaderView> for BlockHeader { challenges_root: Some(CryptoHash::from(h.challenges_root)), timestamp: h.timestamp, random_value: Some(CryptoHash::from(h.random_value)), - validator_proposals: validator_proposals - .into_iter() - .map(|p| ValidatorStake::from(p)) - .collect(), - chunk_mask: h.chunk_mask.clone(), + validator_proposals: h.validator_proposals.map_into(), + chunk_mask: h.chunk_mask, gas_price: Some(BigInt::from(h.gas_price)), block_ordinal: 0, //todo: this is v3 feature, what that means? total_supply: Some(BigInt::from(h.total_supply)), - challenges_result: challenges_result - .into_iter() - .map(|cr| SlashedValidator::from(cr)) - .collect(), + challenges_result: h.challenges_result.map_into(), last_final_block_height: 0, last_final_block: Some(CryptoHash::from(h.last_final_block)), last_ds_final_block_height: 0, @@ -77,46 +62,36 @@ impl From<&near_views::BlockHeaderView> for BlockHeader { block_merkle_root: Some(CryptoHash::from(h.block_merkle_root)), epoch_sync_data_hash: vec![], //todo: this is v3 feature, what that means? approvals: h - .clone() .approvals .into_iter() - .filter_map(|s| match s { - None => None, - Some(sig) => Some(sig.into()), - }) + // I think it's wrong to `filter_map` here. Indeed, the original `approvals` is + // `Vec>` which means it can contain `None` values. If the original + // contains `None`, why would we decide to filter them out exactly using `filter_map`, + // that seems like the wrong behavior the transformed array could have less value than + // the original one ... + .filter_map(|s| s.map(Into::into)) .collect(), - signature: Some(h.signature.clone().into()), + signature: Some(h.signature.into()), latest_protocol_version: h.latest_protocol_version, } } } -impl From<&near_indexer::IndexerShard> for IndexerShard { - fn from(is: &near_indexer::IndexerShard) -> Self { - let chunk: Option = match &is.chunk { - None => None, - Some(c) => Some(IndexerChunk::from(c)), - }; - +impl From for IndexerShard { + fn from(is: near_indexer::IndexerShard) -> Self { IndexerShard { shard_id: is.shard_id, - chunk, - receipt_execution_outcomes: is - .receipt_execution_outcomes - .iter() - .map(|r| IndexerExecutionOutcomeWithReceipt::from(r)) - .collect(), + chunk: is.chunk.map(Into::into), + receipt_execution_outcomes: is.receipt_execution_outcomes.map_into(), } } } -impl From<&near_indexer::IndexerExecutionOutcomeWithReceipt> - for IndexerExecutionOutcomeWithReceipt -{ - fn from(r: &near_indexer::IndexerExecutionOutcomeWithReceipt) -> Self { +impl From for IndexerExecutionOutcomeWithReceipt { + fn from(r: near_indexer::IndexerExecutionOutcomeWithReceipt) -> Self { IndexerExecutionOutcomeWithReceipt { - execution_outcome: Some(ExecutionOutcomeWithId::from(r.execution_outcome.clone())), - receipt: Some(Receipt::from(r.receipt.clone())), + execution_outcome: Some(r.execution_outcome.into()), + receipt: Some(Receipt::from(r.receipt)), } } } @@ -124,8 +99,8 @@ impl From<&near_indexer::IndexerExecutionOutcomeWithReceipt> impl From for Receipt { fn from(r: near_views::ReceiptView) -> Self { Receipt { - predecessor_id: r.predecessor_id.to_string(), - receiver_id: r.receiver_id.to_string(), + predecessor_id: r.predecessor_id.into(), + receiver_id: r.receiver_id.into(), receipt_id: Some(CryptoHash::from(r.receipt_id)), receipt: match r.receipt { ReceiptEnumView::Action { @@ -137,24 +112,18 @@ impl From for Receipt { actions, } => Some(receipt::Receipt::Action { 0: ReceiptAction { - signer_id: signer_id.to_string(), + signer_id: signer_id.into(), signer_public_key: Some(PublicKey::from(signer_public_key)), gas_price: Some(BigInt::from(gas_price)), - output_data_receivers: output_data_receivers - .into_iter() - .map(|o| DataReceiver::from(o)) - .collect(), - input_data_ids: input_data_ids - .into_iter() - .map(|i| CryptoHash::from(i)) - .collect(), - actions: actions.into_iter().map(|a| Action::from(a)).collect(), + output_data_receivers: output_data_receivers.map_into(), + input_data_ids: input_data_ids.map_into(), + actions: actions.map_into(), }, }), ReceiptEnumView::Data { data_id, data } => Some(receipt::Receipt::Data { 0: ReceiptData { data_id: Some(CryptoHash::from(data_id)), - data: data.unwrap_or(vec![]), + data: data.unwrap_or_else(|| vec![]), }, }), }, @@ -166,27 +135,18 @@ impl From for DataReceiver { fn from(d: DataReceiverView) -> Self { DataReceiver { data_id: Some(CryptoHash::from(d.data_id)), - receiver_id: d.receiver_id.to_string(), + receiver_id: d.receiver_id.into(), } } } -impl From<&near_indexer::IndexerChunkView> for IndexerChunk { - fn from(s: &near_indexer::IndexerChunkView) -> Self { +impl From for IndexerChunk { + fn from(s: near_indexer::IndexerChunkView) -> Self { IndexerChunk { - author: s.author.to_string(), - header: Some(ChunkHeader::from(&s.header)), - transactions: s - .transactions - .iter() - .map(|tx| IndexerTransactionWithOutcome::from(tx.clone())) - .collect(), - receipts: s - .receipts - .clone() - .into_iter() - .map(|r| Receipt::from(r)) - .collect(), + author: s.author.into(), + header: Some(ChunkHeader::from(s.header)), + transactions: s.transactions.map_into(), + receipts: s.receipts.map_into(), } } } @@ -206,10 +166,7 @@ impl From fn from(o: near_indexer::IndexerExecutionOutcomeWithOptionalReceipt) -> Self { IndexerExecutionOutcomeWithOptionalReceipt { execution_outcome: Some(ExecutionOutcomeWithId::from(o.execution_outcome)), - receipt: match o.receipt { - None => None, - Some(r) => Some(Receipt::from(r)), - }, + receipt: o.receipt.map(Into::into), } } } @@ -228,15 +185,13 @@ impl From for ExecutionOutcome { fn from(o: near_views::ExecutionOutcomeView) -> Self { ExecutionOutcome { logs: o.logs, - receipt_ids: o - .receipt_ids - .into_iter() - .map(|id| CryptoHash::from(id)) - .collect(), + receipt_ids: o.receipt_ids.map_into(), gas_burnt: o.gas_burnt, tokens_burnt: Some(BigInt::from(o.tokens_burnt)), - executor_id: o.executor_id.to_string(), + executor_id: o.executor_id.into(), status: Some(execution_outcome::Status::from(o.status)), + + // There is a problem here, metadata as now version and `gas_profile` but we do nothing with them ... metadata: match o.metadata { ExecutionMetadataView { .. } => ExecutionMetadata::V1.into(), }, @@ -247,284 +202,285 @@ impl From for ExecutionOutcome { impl From for execution_outcome::Status { fn from(s: near_views::ExecutionStatusView) -> Self { match s { - ExecutionStatusView::Unknown => execution_outcome::Status::Unknown { - 0: UnknownExecutionStatus {}, - }, - ExecutionStatusView::SuccessValue(v) => execution_outcome::Status::SuccessValue { - 0: SuccessValueExecutionStatus { - value: v.into(), - }, - }, + ExecutionStatusView::Unknown => { + execution_outcome::Status::Unknown(UnknownExecutionStatus {}) + } + ExecutionStatusView::SuccessValue(v) => { + execution_outcome::Status::SuccessValue(SuccessValueExecutionStatus { value: v }) + } ExecutionStatusView::SuccessReceiptId(v) => { - execution_outcome::Status::SuccessReceiptId { - 0: SuccessReceiptIdExecutionStatus { - id: Some(CryptoHash::from(v)), - }, - } + execution_outcome::Status::SuccessReceiptId(SuccessReceiptIdExecutionStatus { + id: Some(CryptoHash::from(v)), + }) + } + ExecutionStatusView::Failure(tx_err) => { + execution_outcome::Status::Failure(FailureExecutionStatus { + failure: Some(tx_err.into()), + }) } + } + } +} - ExecutionStatusView::Failure(tx_err) => execution_outcome::Status::Failure { - 0: FailureExecutionStatus { - failure: match tx_err { - near_errors::TxExecutionError::ActionError(ae) => { - Some(failure_execution_status::Failure::ActionError { - 0: ActionError { - index: ae.index.unwrap_or(0), - kind: Some(match ae.kind { - ActionErrorKind::AccountAlreadyExists { account_id } => { - action_error::Kind::AccountAlreadyExist { - 0: AccountAlreadyExistsErrorKind { - account_id: account_id.to_string(), - }, - } +impl From for Failure { + fn from(err: near_primitives::errors::TxExecutionError) -> Self { + match err { + near_errors::TxExecutionError::ActionError(ae) => { + failure_execution_status::Failure::ActionError( + ActionError { + index: ae.index.unwrap_or(0), + kind: Some(match ae.kind { + ActionErrorKind::AccountAlreadyExists { account_id } => { + action_error::Kind::AccountAlreadyExist( + AccountAlreadyExistsErrorKind { + account_id: account_id.into(), + }, + ) + } + ActionErrorKind::AccountDoesNotExist { account_id } => { + action_error::Kind::AccountDoesNotExist( + AccountDoesNotExistErrorKind { + account_id: account_id.into(), + }, + ) + } + ActionErrorKind::CreateAccountOnlyByRegistrar { + account_id, + registrar_account_id, + predecessor_id, + } => action_error::Kind::CreateAccountOnlyByRegistrar( + CreateAccountOnlyByRegistrarErrorKind { + account_id: account_id.into(), + registrar_account_id: registrar_account_id + .into(), + predecessor_id: predecessor_id.into(), + }, + ), + ActionErrorKind::CreateAccountNotAllowed { + account_id, + predecessor_id, + } => action_error::Kind::CreateAccountNotAllowed( + CreateAccountNotAllowedErrorKind { + account_id: account_id.into(), + predecessor_id: predecessor_id.into(), + }, + ), + ActionErrorKind::ActorNoPermission { + account_id, + actor_id, + } => action_error::Kind::ActorNoPermission( + ActorNoPermissionErrorKind { + account_id: account_id.into(), + actor_id: actor_id.into(), + }, + ), + ActionErrorKind::DeleteKeyDoesNotExist { + account_id, + public_key, + } => action_error::Kind::DeleteKeyDoesNotExist( + DeleteKeyDoesNotExistErrorKind { + account_id: account_id.into(), + public_key: Some(PublicKey::from(public_key)), + }, + ), + ActionErrorKind::AddKeyAlreadyExists { + account_id, + public_key, + } => action_error::Kind::AddKeyAlreadyExists( + AddKeyAlreadyExistsErrorKind { + account_id: account_id.into(), + public_key: Some(PublicKey::from(public_key)), + }, + ), + ActionErrorKind::DeleteAccountStaking { account_id } => { + action_error::Kind::DeleteAccountStaking( + DeleteAccountStakingErrorKind { + // This was `account_id: "".to_string()` before which I imagine was an error + account_id: account_id.into(), + }, + ) + } + ActionErrorKind::LackBalanceForState { + account_id, + amount, + } => action_error::Kind::LackBalanceForState( + LackBalanceForStateErrorKind { + account_id: account_id.into(), + balance: Some(BigInt::from(amount)), + }, + ), + ActionErrorKind::TriesToUnstake { account_id } => { + action_error::Kind::TriesToUnstake( + TriesToUnstakeErrorKind { + account_id: account_id.into(), + }, + ) + } + ActionErrorKind::TriesToStake { + account_id, + stake, + locked, + balance, + } => action_error::Kind::TriesToStake( + TriesToStakeErrorKind { + account_id: account_id.into(), + stake: Some(BigInt::from(stake)), + locked: Some(BigInt::from(locked)), + balance: Some(BigInt::from(balance)), + }, + ), + ActionErrorKind::InsufficientStake { + account_id, + stake, + minimum_stake, + } => action_error::Kind::InsufficientStake( + InsufficientStakeErrorKind { + account_id: account_id.into(), + stake: Some(BigInt::from(stake)), + minimum_stake: Some(BigInt::from(minimum_stake)), + }, + ), + ActionErrorKind::FunctionCallError(fce) => { + action_error::Kind::FunctionCall( + FunctionCallErrorKind { error: match fce { + near_vm_errors::FunctionCallErrorSer::CompilationError(_) => { + FunctionCallErrorSer::CompilationError as i32 + } + near_vm_errors::FunctionCallErrorSer::LinkError { .. } => { + FunctionCallErrorSer::LinkError as i32 + } + near_vm_errors::FunctionCallErrorSer::MethodResolveError(_) => { + FunctionCallErrorSer::MethodResolveError as i32 + } + near_vm_errors::FunctionCallErrorSer::WasmTrap(_) => { + FunctionCallErrorSer::WasmTrap as i32 + } + near_vm_errors::FunctionCallErrorSer::WasmUnknownError => { + FunctionCallErrorSer::WasmUnknownError as i32 + } + near_vm_errors::FunctionCallErrorSer::HostError(_) => { + FunctionCallErrorSer::HostError as i32 } - ActionErrorKind::AccountDoesNotExist { account_id } => { - action_error::Kind::AccountDoesNotExist { - 0: AccountDoesNotExistErrorKind { - account_id: account_id.to_string(), - }, - } + near_vm_errors::FunctionCallErrorSer::_EVMError => { + FunctionCallErrorSer::EvmError as i32 } - ActionErrorKind::CreateAccountOnlyByRegistrar { - account_id, - registrar_account_id, - predecessor_id, - } => action_error::Kind::CreateAccountOnlyByRegistrar { - 0: CreateAccountOnlyByRegistrarErrorKind { - account_id: account_id.to_string(), - registrar_account_id: registrar_account_id - .to_string(), - predecessor_id: predecessor_id.to_string(), - }, - }, - ActionErrorKind::CreateAccountNotAllowed { - account_id, - predecessor_id, - } => action_error::Kind::CreateAccountNotAllowed { - 0: CreateAccountNotAllowedErrorKind { - account_id: account_id.to_string(), - predecessor_id: predecessor_id.to_string(), - }, - }, - ActionErrorKind::ActorNoPermission { - account_id, - actor_id, - } => action_error::Kind::ActorNoPermission { - 0: ActorNoPermissionErrorKind { - account_id: account_id.to_string(), - actor_id: actor_id.to_string(), - }, - }, - ActionErrorKind::DeleteKeyDoesNotExist { - account_id, - public_key, - } => action_error::Kind::DeleteKeyDoesNotExist { - 0: DeleteKeyDoesNotExistErrorKind { - account_id: account_id.to_string(), - public_key: Some(PublicKey::from(public_key)), - }, - }, - ActionErrorKind::AddKeyAlreadyExists { - account_id, - public_key, - } => action_error::Kind::AddKeyAlreadyExists { - 0: AddKeyAlreadyExistsErrorKind { - account_id: account_id.to_string(), - public_key: Some(PublicKey::from(public_key)), - }, - }, - ActionErrorKind::DeleteAccountStaking { .. } => { - action_error::Kind::DeleteAccountStaking { - 0: DeleteAccountStakingErrorKind { - account_id: "".to_string(), - }, - } + near_vm_errors::FunctionCallErrorSer::ExecutionError(_) => { + FunctionCallErrorSer::ExecutionError as i32 } - ActionErrorKind::LackBalanceForState { - account_id, - amount, - } => action_error::Kind::LackBalanceForState { - 0: LackBalanceForStateErrorKind { - account_id: account_id.to_string(), - balance: Some(BigInt::from(amount)), - }, - }, - ActionErrorKind::TriesToUnstake { account_id } => { - action_error::Kind::TriesToUnstake { - 0: TriesToUnstakeErrorKind { - account_id: account_id.to_string(), - }, - } + } }, + ) + } + ActionErrorKind::NewReceiptValidationError(rve) => { + action_error::Kind::NewReceiptValidation( + NewReceiptValidationErrorKind { error: match rve { + near_errors::ReceiptValidationError::InvalidPredecessorId { .. } => { + ReceiptValidationError::InvalidPredecessorId as i32 } - ActionErrorKind::TriesToStake { - account_id, - stake, - locked, - balance, - } => action_error::Kind::TriesToStake { - 0: TriesToStakeErrorKind { - account_id: account_id.to_string(), - stake: Some(BigInt::from(stake)), - locked: Some(BigInt::from(locked)), - balance: Some(BigInt::from(balance)), - }, - }, - ActionErrorKind::InsufficientStake { - account_id, - stake, - minimum_stake, - } => action_error::Kind::InsufficientStake { - 0: InsufficientStakeErrorKind { - account_id: account_id.to_string(), - stake: Some(BigInt::from(stake)), - minimum_stake: Some(BigInt::from(minimum_stake)), - }, - }, - ActionErrorKind::FunctionCallError(fce) => { - action_error::Kind::FunctionCall { - 0: FunctionCallErrorKind { error: match fce { - near_vm_errors::FunctionCallErrorSer::CompilationError(_) => { - FunctionCallErrorSer::CompilationError.into() - } - near_vm_errors::FunctionCallErrorSer::LinkError { .. } => { - FunctionCallErrorSer::LinkError.into() - } - near_vm_errors::FunctionCallErrorSer::MethodResolveError(_) => { - FunctionCallErrorSer::MethodResolveError.into() - } - near_vm_errors::FunctionCallErrorSer::WasmTrap(_) => { - FunctionCallErrorSer::WasmTrap.into() - } - near_vm_errors::FunctionCallErrorSer::WasmUnknownError => { - FunctionCallErrorSer::WasmUnknownError.into() - } - near_vm_errors::FunctionCallErrorSer::HostError(_) => { - FunctionCallErrorSer::HostError.into() - } - near_vm_errors::FunctionCallErrorSer::_EVMError => { - FunctionCallErrorSer::EvmError.into() - } - near_vm_errors::FunctionCallErrorSer::ExecutionError(_) => { - FunctionCallErrorSer::ExecutionError.into() - } - } }, - } + near_errors::ReceiptValidationError::InvalidReceiverId { .. } => { + ReceiptValidationError::InvalidReceiverAccountId as i32 } - ActionErrorKind::NewReceiptValidationError(rve) => { - action_error::Kind::NewReceiptValidation { - 0: NewReceiptValidationErrorKind { error: match rve { - near_errors::ReceiptValidationError::InvalidPredecessorId { .. } => { - ReceiptValidationError::InvalidPredecessorId.into() - } - near_errors::ReceiptValidationError::InvalidReceiverId { .. } => { - ReceiptValidationError::InvalidReceiverAccountId.into() - } - near_errors::ReceiptValidationError::InvalidSignerId { .. } => { - ReceiptValidationError::InvalidSignerAccountId.into() - } - near_errors::ReceiptValidationError::InvalidDataReceiverId { .. } => { - ReceiptValidationError::InvalidDataReceiverId.into() - } - near_errors::ReceiptValidationError::ReturnedValueLengthExceeded { .. } => { - ReceiptValidationError::ReturnedValueLengthExceeded.into() - } - near_errors::ReceiptValidationError::NumberInputDataDependenciesExceeded { .. } => { - ReceiptValidationError::NumberInputDataDependenciesExceeded.into() - } - near_errors::ReceiptValidationError::ActionsValidation(_) => { - ReceiptValidationError::ActionsValidationError.into() - } - }} - } + near_errors::ReceiptValidationError::InvalidSignerId { .. } => { + ReceiptValidationError::InvalidSignerAccountId as i32 } - ActionErrorKind::OnlyImplicitAccountCreationAllowed { - account_id, - } => { - action_error::Kind::OnlyImplicitAccountCreationAllowed { - 0: OnlyImplicitAccountCreationAllowedErrorKind { - account_id: account_id.to_string(), - }, - } + near_errors::ReceiptValidationError::InvalidDataReceiverId { .. } => { + ReceiptValidationError::InvalidDataReceiverId as i32 } - ActionErrorKind::DeleteAccountWithLargeState { - account_id, - } => action_error::Kind::DeleteAccountWithLargeState { - 0: DeleteAccountWithLargeStateErrorKind { - account_id: account_id.to_string(), - }, - }, - }), + near_errors::ReceiptValidationError::ReturnedValueLengthExceeded { .. } => { + ReceiptValidationError::ReturnedValueLengthExceeded as i32 + } + near_errors::ReceiptValidationError::NumberInputDataDependenciesExceeded { .. } => { + ReceiptValidationError::NumberInputDataDependenciesExceeded as i32 + } + near_errors::ReceiptValidationError::ActionsValidation(_) => { + ReceiptValidationError::ActionsValidationError as i32 + } + }} + ) + } + ActionErrorKind::OnlyImplicitAccountCreationAllowed { + account_id, + } => { + action_error::Kind::OnlyImplicitAccountCreationAllowed( + OnlyImplicitAccountCreationAllowedErrorKind { + account_id: account_id.into(), + }, + ) + } + ActionErrorKind::DeleteAccountWithLargeState { + account_id, + } => action_error::Kind::DeleteAccountWithLargeState( + DeleteAccountWithLargeStateErrorKind { + account_id: account_id.into(), }, - }) + ), + }), + }, + ) + } + near_errors::TxExecutionError::InvalidTxError(e) => { + failure_execution_status::Failure::InvalidTxError { + 0: match e { + near_errors::InvalidTxError::InvalidAccessKeyError(..) => { + InvalidTxError::InvalidAccessKeyError as i32 } - near_errors::TxExecutionError::InvalidTxError(e) => { - Some(failure_execution_status::Failure::InvalidTxError { - 0: match e { - near_errors::InvalidTxError::InvalidAccessKeyError(..) => { - InvalidTxError::InvalidAccessKeyError.into() - } - near_errors::InvalidTxError::InvalidSignerId { .. } => { - InvalidTxError::InvalidSignerId.into() - } - near_errors::InvalidTxError::SignerDoesNotExist { .. } => { - InvalidTxError::SignerDoesNotExist.into() - } - near_errors::InvalidTxError::InvalidNonce { .. } => { - InvalidTxError::InvalidNonce.into() - } - near_errors::InvalidTxError::NonceTooLarge { .. } => { - InvalidTxError::NonceTooLarge.into() - } - near_errors::InvalidTxError::InvalidReceiverId { .. } => { - InvalidTxError::InvalidReceiverId.into() - } - near_errors::InvalidTxError::InvalidSignature => { - InvalidTxError::InvalidSignature.into() - } - near_errors::InvalidTxError::NotEnoughBalance { .. } => { - InvalidTxError::NotEnoughBalance.into() - } - near_errors::InvalidTxError::LackBalanceForState { .. } => { - InvalidTxError::LackBalanceForState.into() - } - near_errors::InvalidTxError::CostOverflow => { - InvalidTxError::CostOverflow.into() - } - near_errors::InvalidTxError::InvalidChain => { - InvalidTxError::InvalidChain.into() - } - near_errors::InvalidTxError::Expired => { - InvalidTxError::Expired.into() - } - near_errors::InvalidTxError::ActionsValidation(_) => { - InvalidTxError::ActionsValidation.into() - } - near_errors::InvalidTxError::TransactionSizeExceeded { - .. - } => InvalidTxError::TransactionSizeExceeded.into(), - }, - }) + near_errors::InvalidTxError::InvalidSignerId { .. } => { + InvalidTxError::InvalidSignerId as i32 + } + near_errors::InvalidTxError::SignerDoesNotExist { .. } => { + InvalidTxError::SignerDoesNotExist as i32 + } + near_errors::InvalidTxError::InvalidNonce { .. } => { + InvalidTxError::InvalidNonce as i32 + } + near_errors::InvalidTxError::NonceTooLarge { .. } => { + InvalidTxError::NonceTooLarge as i32 + } + near_errors::InvalidTxError::InvalidReceiverId { .. } => { + InvalidTxError::InvalidReceiverId as i32 + } + near_errors::InvalidTxError::InvalidSignature => { + InvalidTxError::InvalidSignature as i32 + } + near_errors::InvalidTxError::NotEnoughBalance { .. } => { + InvalidTxError::NotEnoughBalance as i32 + } + near_errors::InvalidTxError::LackBalanceForState { .. } => { + InvalidTxError::LackBalanceForState as i32 + } + near_errors::InvalidTxError::CostOverflow => { + InvalidTxError::CostOverflow as i32 + } + near_errors::InvalidTxError::InvalidChain => { + InvalidTxError::InvalidChain as i32 + } + near_errors::InvalidTxError::Expired => InvalidTxError::Expired as i32, + near_errors::InvalidTxError::ActionsValidation(_) => { + InvalidTxError::ActionsValidation as i32 + } + near_errors::InvalidTxError::TransactionSizeExceeded { .. } => { + InvalidTxError::TransactionSizeExceeded as i32 } }, - }, - }, + } + } } } } impl From for MerklePath { fn from(p: near_primitives::merkle::MerklePath) -> Self { - MerklePath { - path: p - .into_iter() - .map(|item| MerklePathItem { - hash: Some(CryptoHash::from(item.hash)), - direction: match item.direction { - near_primitives::merkle::Direction::Left => 0, - near_primitives::merkle::Direction::Right => 1, - }, - }) - .collect(), + MerklePath { path: p.map_into() } + } +} + +impl From for MerklePathItem { + fn from(p: near_primitives::merkle::MerklePathItem) -> Self { + Self { + hash: Some(CryptoHash::from(p.hash)), + direction: match p.direction { + near_primitives::merkle::Direction::Left => 0, + near_primitives::merkle::Direction::Right => 1, + }, } } } @@ -532,11 +488,11 @@ impl From for MerklePath { impl From for SignedTransaction { fn from(tx: near_views::SignedTransactionView) -> Self { SignedTransaction { - signer_id: tx.signer_id.to_string(), + signer_id: tx.signer_id.into(), public_key: Some(PublicKey::from(tx.public_key)), nonce: tx.nonce, - receiver_id: tx.receiver_id.to_string(), - actions: tx.actions.into_iter().map(|a| Action::from(a)).collect(), + receiver_id: tx.receiver_id.into(), + actions: tx.actions.map_into(), signature: Some(tx.signature.into()), hash: Some(CryptoHash::from(tx.hash)), } @@ -547,14 +503,12 @@ impl From for Action { fn from(a: near_views::ActionView) -> Self { match a { near_views::ActionView::CreateAccount => Action { - action: Some(action::Action::CreateAccount { - 0: CreateAccountAction {}, - }), + action: Some(action::Action::CreateAccount(CreateAccountAction {})), }, near_views::ActionView::DeployContract { code } => Action { - action: Some(action::Action::DeployContract { - 0: DeployContractAction { code: code.into() }, - }), + action: Some(action::Action::DeployContract(DeployContractAction { + code, + })), }, near_views::ActionView::FunctionCall { method_name, @@ -562,54 +516,42 @@ impl From for Action { gas, deposit, } => Action { - action: Some(action::Action::FunctionCall { - 0: FunctionCallAction { - method_name, - args: args.into(), - gas, - deposit: Some(BigInt::from(deposit)), - }, - }), + action: Some(action::Action::FunctionCall(FunctionCallAction { + method_name, + args, + gas, + deposit: Some(BigInt::from(deposit)), + })), }, near_views::ActionView::Transfer { deposit } => Action { - action: Some(action::Action::Transfer { - 0: TransferAction { - deposit: Some(BigInt::from(deposit)), - }, - }), + action: Some(action::Action::Transfer(TransferAction { + deposit: Some(BigInt::from(deposit)), + })), }, near_views::ActionView::Stake { stake, public_key } => Action { - action: Some(action::Action::Stake { - 0: StakeAction { - stake: Some(BigInt::from(stake)), - public_key: Some(PublicKey::from(public_key)), - }, - }), + action: Some(action::Action::Stake(StakeAction { + stake: Some(BigInt::from(stake)), + public_key: Some(PublicKey::from(public_key)), + })), }, near_views::ActionView::AddKey { public_key, access_key, } => Action { - action: Some(action::Action::AddKey { - 0: AddKeyAction { - public_key: Some(PublicKey::from(public_key)), - access_key: Some(AccessKey::from(access_key)), - }, - }), + action: Some(action::Action::AddKey(AddKeyAction { + public_key: Some(PublicKey::from(public_key)), + access_key: Some(AccessKey::from(access_key)), + })), }, near_views::ActionView::DeleteKey { public_key } => Action { - action: Some(action::Action::DeleteKey { - 0: DeleteKeyAction { - public_key: Some(PublicKey::from(public_key)), - }, - }), + action: Some(action::Action::DeleteKey(DeleteKeyAction { + public_key: Some(PublicKey::from(public_key)), + })), }, near_views::ActionView::DeleteAccount { beneficiary_id } => Action { - action: Some(action::Action::DeleteAccount { - 0: DeleteAccountAction { - beneficiary_id: beneficiary_id.to_string(), - }, - }), + action: Some(action::Action::DeleteAccount(DeleteAccountAction { + beneficiary_id: beneficiary_id.into(), + })), }, } } @@ -626,36 +568,29 @@ impl From for AccessKey { impl From for AccessKeyPermission { fn from(p: near_views::AccessKeyPermissionView) -> Self { + use access_key_permission::Permission; + match p { near_views::AccessKeyPermissionView::FunctionCall { allowance, receiver_id, method_names, } => AccessKeyPermission { - permission: Some(access_key_permission::Permission::FunctionCall { - 0: FunctionCallPermission { - allowance: match allowance { - None => None, - Some(a) => Some(BigInt::from(a)), - }, - receiver_id, - method_names, - }, - }), + permission: Some(Permission::FunctionCall(FunctionCallPermission { + allowance: allowance.map(Into::into), + receiver_id, + method_names, + })), }, near_views::AccessKeyPermissionView::FullAccess => AccessKeyPermission { - permission: Some(access_key_permission::Permission::FullAccess { - 0: FullAccessPermission {}, - }), + permission: Some(Permission::FullAccess(FullAccessPermission {})), }, } } } -impl From<&near_views::ChunkHeaderView> for ChunkHeader { - fn from(ch: &near_views::ChunkHeaderView) -> Self { - let validator_proposals = &ch.validator_proposals; - +impl From for ChunkHeader { + fn from(ch: near_views::ChunkHeaderView) -> Self { ChunkHeader { chunk_hash: Vec::from(ch.chunk_hash), prev_block_hash: Vec::from(ch.prev_block_hash), @@ -672,11 +607,8 @@ impl From<&near_views::ChunkHeaderView> for ChunkHeader { balance_burnt: Some(BigInt::from(ch.balance_burnt)), outgoing_receipts_root: Vec::from(ch.outgoing_receipts_root), tx_root: Vec::from(ch.tx_root), - validator_proposals: validator_proposals - .into_iter() - .map(|vp| ValidatorStake::from(vp)) - .collect(), - signature: Some(ch.signature.clone().into()), + validator_proposals: ch.validator_proposals.map_into(), + signature: Some(ch.signature.into()), } } } @@ -685,13 +617,13 @@ impl From for Signature { fn from(sign: near_crypto::signature::Signature) -> Self { match sign { near_crypto::signature::Signature::ED25519(s) => Signature { - r#type: CurveKind::Ed25519.into(), + r#type: CurveKind::Ed25519 as i32, bytes: Vec::from(s.to_bytes()), } as Signature, near_crypto::signature::Signature::SECP256K1(s) => { let data = Vec::from(<[u8; 65]>::from(s)); Signature { - r#type: CurveKind::Secp256k1.into(), + r#type: CurveKind::Secp256k1 as i32, bytes: data, } } @@ -705,14 +637,14 @@ impl From for PublicKey { near_crypto::signature::PublicKey::ED25519(s) => { let data = Vec::from(<[u8; 32]>::from(s)); PublicKey { - r#type: CurveKind::Ed25519.into(), + r#type: CurveKind::Ed25519 as i32, bytes: data, } } near_crypto::signature::PublicKey::SECP256K1(s) => { let data = Vec::from(<[u8; 64]>::from(s)); PublicKey { - r#type: CurveKind::Secp256k1.into(), + r#type: CurveKind::Secp256k1 as i32, bytes: data, } } @@ -720,22 +652,22 @@ impl From for PublicKey { } } -impl From<&near_primitives::challenge::SlashedValidator> for SlashedValidator { - fn from(sv: &near_primitives::challenge::SlashedValidator) -> Self { +impl From for SlashedValidator { + fn from(sv: near_primitives::challenge::SlashedValidator) -> Self { SlashedValidator { - account_id: sv.account_id.to_string(), + account_id: sv.account_id.into(), is_double_sign: sv.is_double_sign, } } } -impl From<&near_primitives::views::validator_stake_view::ValidatorStakeView> for ValidatorStake { - fn from(sv: &near_primitives::views::validator_stake_view::ValidatorStakeView) -> Self { +impl From for ValidatorStake { + fn from(sv: near_primitives::views::validator_stake_view::ValidatorStakeView) -> Self { match sv { near_primitives::views::validator_stake_view::ValidatorStakeView::V1(v) => { ValidatorStake { - account_id: v.account_id.to_string(), - public_key: Some(PublicKey::from(v.public_key.clone())), + account_id: v.account_id.into(), + public_key: Some(PublicKey::from(v.public_key)), stake: Some(BigInt::from(v.stake)), } } @@ -753,9 +685,7 @@ impl From for BigInt { impl From for CryptoHash { fn from(h: near_primitives::hash::CryptoHash) -> Self { - CryptoHash { - bytes: Vec::from(h), - } + CryptoHash { bytes: h.into() } } } @@ -772,3 +702,14 @@ impl Display for Block { write!(f, "#{} ({})", header.height, header.hash.as_ref().unwrap()) } } + +trait MapInto> { + /// Shortcut for `x.into_iter().map(Into::into).collect()`. + fn map_into>(self) -> I; +} + +impl> MapInto for Vec { + fn map_into>(self) -> I { + self.into_iter().map(Into::into).collect() + } +} diff --git a/src/main.rs b/src/main.rs index 33f304f7..251788ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,8 +44,7 @@ fn main() { let mut stream = indexer.streamer(); actix::spawn(async move { while let Some(streamer_message) = stream.recv().await { - let block = codec::Block::from(&streamer_message); - dm::on_block(&block); + dm::on_block(&codec::Block::from(streamer_message)); } }); });