rooch-network/rooch

[pipeline]: Introducing Precompiled Contracts and "Transfer" Operation in Rust

Opened this issue · 0 comments

Description:
We aim to enhance our Rust-based blockchain project by introducing a mechanism for precompiled contracts, similar to Ethereum's approach. Precompiled contracts are essential for efficiently handling specific computational tasks, such as cryptographic operations, which would otherwise be too slow or complex to implement directly in our VM's bytecode.

Precompiled Contracts Implementation

To implement precompiled contracts effectively, we should:

  1. Identify Key Computational Tasks: Determine which tasks would benefit most from being precompiled. These typically include cryptographic functions like SHA-256, ECDSA verification, etc.
  2. Define Fixed Addresses: Assign specific, fixed addresses to these contracts so that they can be easily called from within other smart contracts.
  3. Native Code Implementation: Implement these tasks in native Rust code to ensure high performance. This code should be integrated into the blockchain runtime and exposed to the VM.

Reference Implementation in Ethereum

Ethereum provides a robust example of precompiled contracts in their Go implementation. The precompiled contracts are defined and invoked within their EVM, which can be seen in their contracts.go file.

"Transfer" Operation (or other common ops)

While reviewing Ethereum's implementation, we noticed that they have a Transfer operation implemented in Go, which handles the transfer of value between accounts. This can be seen in evm.go file.

Although this operation has side effects (transferring value), it is not typically categorized as a precompiled contract. Instead, it can be considered a native operation out of the EVM.

Proposal

  1. Introduce Precompiled Contracts:

    • Determine the most beneficial computational tasks to precompile.
    • Implement these tasks in Rust as native functions.
    • Assign fixed addresses for easy invocation from smart contracts.
  2. Implement Native Transfer Operation(or More):

    • Create a native transfer function in Rust, similar to Ethereum's implementation.

Tasks:

  • Research and identify key computational tasks for precompiled contracts.
  • Define fixed addresses for these precompiled contracts.
  • Implement the identified tasks in Rust and integrate them into the blockchain runtime.
  • Implement a native transfer function in Rust with benchmark.
  • Write documentation and examples on how to use these new features.

By integrating these features, we can significantly improve the performance and capabilities of our network.

p.s.:

Please let me know if there are any additional considerations or if further clarification is needed.