-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init: initilized complete v1 library
- Loading branch information
Showing
8 changed files
with
124 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,30 @@ | ||
## Foundry | ||
## Co-Processor base contract | ||
|
||
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.** | ||
|
||
Foundry consists of: | ||
|
||
- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools). | ||
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data. | ||
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network. | ||
- **Chisel**: Fast, utilitarian, and verbose solidity REPL. | ||
|
||
## Documentation | ||
|
||
https://book.getfoundry.sh/ | ||
**This is a base contract containing functions for calling with the co-processor and also a callback function to receive response from the co-processor. This function can be overridden where necessary or in the most simple implementation just the "handleCallback" function can be overridden to contain implementations to handle response from the co-processor.** | ||
|
||
## Usage | ||
|
||
### Build | ||
|
||
```shell | ||
$ forge build | ||
``` | ||
### Installation | ||
|
||
### Test | ||
- Install the base contract by running the following command | ||
|
||
```shell | ||
$ forge test | ||
forge install https://github.com/Mugen-Builders/coprocessor-base-contract | ||
``` | ||
|
||
### Format | ||
- Import the base contract into your project through the following command: | ||
|
||
```shell | ||
$ forge fmt | ||
``` | ||
|
||
### Gas Snapshots | ||
|
||
```shell | ||
$ forge snapshot | ||
```solidity | ||
import "cartesi-coprocessor-base-contract/BaseContract.sol"; | ||
``` | ||
|
||
### Anvil | ||
- Inherit the base contract and also populate the constructor parameters: | ||
|
||
```shell | ||
$ anvil | ||
``` | ||
|
||
### Deploy | ||
|
||
```shell | ||
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key> | ||
``` | ||
|
||
### Cast | ||
|
||
```shell | ||
$ cast <subcommand> | ||
``` | ||
|
||
### Help | ||
|
||
```shell | ||
$ forge --help | ||
$ anvil --help | ||
$ cast --help | ||
```solidity | ||
contract MyContract is BaseContract { | ||
constructor(address _coprocessorAddress, bytes32 _machineHash) | ||
BaseContract(_coprocessorAddress, _machineHash) | ||
{} | ||
// Add your Logic here | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
import "./ICoprocessor.sol"; | ||
import "./ICoprocessorCallback.sol"; | ||
|
||
/// @title BaseContract | ||
/// @notice A base contract, which should be inherited for interacting with the Coprocessor | ||
abstract contract BaseContract is ICoprocessorCallback { | ||
ICoprocessor public coprocessor; | ||
bytes32 public machineHash; | ||
|
||
/// @notice Tracks whether a computation has been sent for a specific input hash | ||
mapping(bytes32 => bool) public computationSent; | ||
|
||
/// @notice Emitted when a result is received | ||
event ResultReceived(bytes output); | ||
|
||
/// @param _coprocessorAddress The address of the ICoprocessor contract | ||
/// @param _machineHash The machine hash associated with dapp whose logic the coProcessor would run | ||
constructor(address _coprocessorAddress, bytes32 _machineHash) { | ||
require(_coprocessorAddress != address(0), "Invalid coprocessor address"); | ||
coprocessor = ICoprocessor(_coprocessorAddress); | ||
machineHash = _machineHash; | ||
} | ||
|
||
/// @notice Issues a task to the coprocessor | ||
/// @param input The input data to process | ||
function callCoprocessor(bytes calldata input) internal { | ||
bytes32 inputHash = keccak256(input); | ||
|
||
require(!computationSent[inputHash], "Computation already sent"); | ||
|
||
computationSent[inputHash] = true; | ||
|
||
coprocessor.issueTask(machineHash, input, address(this)); | ||
} | ||
|
||
/// @notice Callback function called by the coprocessor with outputs | ||
/// @param _machineHash The machine hash associated with dapp whose logic the coProcessor would run | ||
/// @param _payloadHash The hash of the input payload | ||
/// @param outputs The outputs returned by the coprocessor | ||
function coprocessorCallbackOutputsOnly( | ||
bytes32 _machineHash, | ||
bytes32 _payloadHash, | ||
bytes[] calldata outputs | ||
) external override { | ||
require(msg.sender == address(coprocessor), "Unauthorized caller"); | ||
require(_machineHash == machineHash, "Machine hash mismatch"); | ||
require(computationSent[_payloadHash], "Computation not found"); | ||
|
||
// Process each output | ||
for (uint256 i = 0; i < outputs.length; i++) { | ||
bytes calldata output = outputs[i]; | ||
require(output.length > 3, "Output too short"); | ||
|
||
bytes4 selector = bytes4(output[:4]); | ||
bytes calldata arguments = output[4:]; | ||
|
||
require(selector == ICoprocessorOutputs.Notice.selector); | ||
handleCallback(arguments); | ||
} | ||
|
||
// Clean up the mapping | ||
delete computationSent[_payloadHash]; | ||
} | ||
|
||
/// @notice Handles a notice from the coprocessor | ||
/// @param notice The notice data | ||
function handleCallback(bytes calldata notice) internal virtual { | ||
emit ResultReceived(notice); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
interface ICoprocessor { | ||
function issueTask(bytes32 machineHash, bytes calldata input, address callbackAddress) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
interface ICoprocessorOutputs { | ||
function Notice(bytes calldata payload) external; | ||
} | ||
|
||
interface ICoprocessorCallback { | ||
function coprocessorCallbackOutputsOnly(bytes32 machineHash, bytes32 payloadHash, bytes[] calldata outputs) | ||
external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
import {BaseContract} from "../src/BaseContract.sol"; | ||
|
||
contract CounterTest is Test { | ||
BaseContract public baseContract; | ||
|
||
function setUp() public { | ||
|
||
} | ||
|
||
|
||
} |
This file was deleted.
Oops, something went wrong.