Skip to content

Commit

Permalink
init: initilized complete v1 library
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonnyjoe committed Dec 17, 2024
1 parent e742d46 commit 926bd48
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 93 deletions.
68 changes: 16 additions & 52 deletions README.md
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
}
```
6 changes: 3 additions & 3 deletions script/Counter.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
import {BaseContract} from "../src/BaseContract.sol";

contract CounterScript is Script {
Counter public counter;
BaseContract public baseContract;

function setUp() public {}

function run() public {
vm.startBroadcast();

counter = new Counter();
// baseContract = new BaseContract();

vm.stopBroadcast();
}
Expand Down
73 changes: 73 additions & 0 deletions src/BaseContract.sol
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);
}
}
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

6 changes: 6 additions & 0 deletions src/ICoprocessor.sol
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;
}
11 changes: 11 additions & 0 deletions src/ICoprocessorCallback.sol
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;
}
15 changes: 15 additions & 0 deletions test/BaseContract.t.sol
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 {

}


}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

0 comments on commit 926bd48

Please sign in to comment.