Remove invalid TryFrom implementations for ActionView and ReceiptView
WHAT
Removes TryFrom<ActionView> for Action and TryFrom<ReceiptView> for Receipt implementations that were fundamentally broken. These conversions attempted to reconstruct contract code from hash values, which is cryptographically impossible.
The code field in ActionView::DeployContract (and related variants) has always contained the code hash, not the actual code bytes. The field is now deprecated with a new code_hash: CryptoHash field added for clarity.
WHY
HOW
Core Changes (core/primitives/src/views.rs)
- Removed 83 lines of invalid
TryFrom<ActionView> for Actionimplementation - Removed 93 lines of invalid
TryFrom<ReceiptView> for Receiptimplementation - Added
code_hash: CryptoHashfield toDeployContract,DeployGlobalContract, andDeployGlobalContractByAccountId - Deprecated existing
code: Vec<u8>field with clear warnings about historical misnaming - Updated
From<Action> for ActionViewto populate both fields for backwards compatibility
Indexer Fix (chain/indexer/src/streamer/utils.rs)
- Replaced
Action::try_fromcalls with explicit action conversion - Skips transactions containing deploy contract actions (logs warning)
- Manually converts supported action types (CreateAccount, FunctionCall, Transfer, Stake, AddKey, DeleteKey, DeleteAccount)
Mirror Tool Fix (tools/mirror/src/online.rs)
- Implemented custom
try_receipt_view_to_receipt()function - Explicitly fails with descriptive error for DeployContract actions
- Handles all other receipt types correctly
The changes maintain wire-format compatibility while preventing invalid conversions.
Original prompt
This section details on the original issue you should resolve
<issue_title>Invalid TryFrom<ActionView> for Action</issue_title> <issue_description>### Description
https://github.com/near/nearcore/blob/a9557047d1bd45da0d06cf6b880fea6487c35e20/core/primitives/src/views.rs#L1433-L1439
As you can see, the code field in ActionView does not actually hold the contract code blob, but instead it contains the hash, but the type (should be CryptoHash) and the naming (should be
code_hash) is misleading.https://github.com/near/nearcore/blob/a9557047d1bd45da0d06cf6b880fea6487c35e20/core/primitives/src/views.rs#L1505-L1513
We are caring over a quick hack @bowenwang1996 did 6 years ago and it is time to address it, so the users don't get confused
We cannot simply change the field name / type inside nearcore as that may introduce breaking changes to the applications that are out there in the wild, so the proposal is to add a new field code_hash: CryptoHash and mark the code field as deprecated with the clear note that it was incorrectly named all this time (since before mainnet). Apply the same change to the DeployGlobalContract variant as well.
Also, we need to investigate how the
TryFrom<ActionView> for Actionis used and if it is not used, drop it as there is no way to turn code hash into code for the DeployContract and DeployGlobalContract actions. </issue_description><agent_instructions>* [ ] Drop the
TryFrom<ReceiptView> for ReceiptandTryFrom<ActionView> for Actionimplementations in core/primitives/src/views.rs
- [ ] Update ActionView so
DeployContract,DeployGlobalContract, andDeployGlobalContractByAccountIdhavecodefield marked as deprecated and warn users that this field never contained the code bytes and always contained the hash of the code- [ ] Add
code_hash: CryptoHashtoDeployContract,DeployGlobalContract, andDeployGlobalContractByAccountIdenum variants of the ActionView</agent_instructions>Comments on the Issue (you are @copilot in this section)
@frol I have just checked that removing `TryFromfor Receipt` and `TryFrom for Action` does not break neard compilation, so I suggest to drop those and fix the indexer framework and mirror tool to avoid these conversions. @frol @copilot
- [ ] Drop the
TryFrom<ReceiptView> for ReceiptandTryFrom<ActionView> for Actionimplementations in core/primitives/src/views.rs- [ ] Update ActionView so
DeployContract,DeployGlobalContract, andDeployGlobalContractByAccountIdhavecodefield marked as deprecated and warn users that this field never contained the code bytes and always contained the hash of the code- [ ] Add
code_hash: CryptoHashtoDeployContract,DeployGlobalContract, andDeployGlobalContractByAccountIdenum variants of the ActionView</comment_new>
- Fixes near/nearcore#14779
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.