Skip to content

Commit

Permalink
feat: publishing Infernet SDK v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-ritual committed Nov 27, 2023
0 parents commit 2d04a7f
Show file tree
Hide file tree
Showing 38 changed files with 49,191 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Environment variables consumed in Forge scripts (./scripts)

# RPC URL
RPC_URL=http://localhost:8545

# Deployer private key
PRIVATE_KEY=0x...
58 changes: 58 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
BalanceScaleTest:testE2E() (gas: 942571)
CoordinatorCallbackTest:testCallbackGasLimitIsApproximatelyCorrect() (gas: 301195)
CoordinatorCallbackTest:testCanCreateCallback() (gas: 142641)
CoordinatorCallbackTest:testCanDeliverCallbackResponse() (gas: 331677)
CoordinatorCallbackTest:testCanDeliverCallbackResponseOnceAcrossTwoNodes() (gas: 505224)
CoordinatorCallbackTest:testCannotDeliverCallbackMaxGasPriceTooLow() (gas: 139387)
CoordinatorCallbackTest:testCannotDeliverCallbackResponseFromSameNodeTwice() (gas: 312437)
CoordinatorCallbackTest:testCannotDeliverCallbackWithInsufficientGasLimit() (gas: 304859)
CoordinatorCallbackTest:testFuzzCannotDeliverCallbackIfIncorrectInterval(uint32) (runs: 256, μ: 139312, ~: 139312)
CoordinatorGeneralTest:testCannotBeReassignedSubscriptionID() (gas: 453861)
CoordinatorGeneralTest:testCannotReceiveResponseFromNonCoordinator() (gas: 11012)
CoordinatorSubscriptionTest:testCanCancelFulfilledSubscription() (gas: 230898)
CoordinatorSubscriptionTest:testCanCancelSubscription() (gas: 87979)
CoordinatorSubscriptionTest:testCanReadContainerInputs() (gas: 13394)
CoordinatorSubscriptionTest:testCannotCancelCancelledSubscription() (gas: 88548)
CoordinatorSubscriptionTest:testCannotCancelNonExistentSubscription() (gas: 14075)
CoordinatorSubscriptionTest:testCannotCancelUnownedSubscription() (gas: 135656)
CoordinatorSubscriptionTest:testCannotDeliverMaxRedundancyResponse() (gas: 452793)
CoordinatorSubscriptionTest:testCannotDeliverResponseDelayed() (gas: 121768)
CoordinatorSubscriptionTest:testCannotDeliverResponseEarly() (gas: 121613)
CoordinatorSubscriptionTest:testCannotDeliverResponseForCompletedSubscription() (gas: 468557)
CoordinatorSubscriptionTest:testCannotDeliverResponseForNonExistentSubscription() (gas: 22024)
CoordinatorSubscriptionTest:testCannotDeliverResponseFromNonNode() (gas: 111969)
CoordinatorSubscriptionTest:testCannotDeliverResponseIfAlreadyDeliveredInCurrentInterval() (gas: 290576)
CoordinatorSubscriptionTest:testCannotDeliverResponseIncorrectInterval() (gas: 289704)
CoordinatorSubscriptionTest:testCannotDeliverResponseNonActiveSubscription() (gas: 131838)
CoordinatorSubscriptionTest:testCannotDeliverSubscriptionWithInsufficientGasLimit() (gas: 282230)
CoordinatorSubscriptionTest:testFuzzSubscriptionIntervalsAreCorrect(uint32,uint32,uint32) (runs: 256, μ: 21641, ~: 14634)
CoordinatorSubscriptionTest:testSubscriptionGasLimitIsApproximatelyCorrect() (gas: 278328)
EIP712CoordinatorTest:testCanAtomicCreateSubscriptionAndDeliverOutput() (gas: 368256)
EIP712CoordinatorTest:testCanCancelSubscriptionCreatedViaDelegate() (gas: 149801)
EIP712CoordinatorTest:testCanCreateDelegatedSubscriptionWithUnorderedNonces() (gas: 336739)
EIP712CoordinatorTest:testCanCreateNewSubscriptionViaEIP712() (gas: 179021)
EIP712CoordinatorTest:testCanDelegatedDeliverComputeResponseForExistingSubscription() (gas: 509659)
EIP712CoordinatorTest:testCanGetExistingSubscriptionViaEIP712() (gas: 186650)
EIP712CoordinatorTest:testCanUseExistingDelegatedSubscriptionFromOldSigner() (gas: 170617)
EIP712CoordinatorTest:testCannotAtomicDeliverOutputForCompletedSubscription() (gas: 368987)
EIP712CoordinatorTest:testCannotCreateDelegatedSubscriptionWhereNonceReused() (gas: 207700)
EIP712CoordinatorTest:testCannotCreateDelegatedSubscriptionWhereSignatureExpired() (gas: 32448)
EIP712CoordinatorTest:testCannotDeliverEIP712SubscriptionWhereGasLimitTooLow() (gas: 338884)
EIP712CoordinatorTest:testCannotUseValidDelegatedSubscriptionFromOldSigner() (gas: 48168)
EIP712CoordinatorTest:testDeliverReducedGasCostSubscriptionCachedSubscription() (gas: 501991)
EIP712CoordinatorTest:testFuzzCannotCreateDelegatedSubscriptionWhereSignatureMismatch(uint256) (runs: 256, μ: 43249, ~: 43249)
ManagerTest:testActivateNode() (gas: 54843)
ManagerTest:testActivateNodeMustReadministerCooldownIfReactivating() (gas: 86779)
ManagerTest:testCanCallOnlyActiveNodeFnAsActiveNode() (gas: 48772)
ManagerTest:testCanDeactivateActiveNode() (gas: 40439)
ManagerTest:testCanDeactivateInactiveNode() (gas: 26121)
ManagerTest:testCanDeactivateRegisteredNode() (gas: 36586)
ManagerTest:testCanRegisterInactiveNode() (gas: 61447)
ManagerTest:testCanRegisterInactiveNodeViaProxy() (gas: 59598)
ManagerTest:testCannnotCallOnlyActiveNodeFnAsInactiveNode() (gas: 16139)
ManagerTest:testCannotActivateInactiveNode() (gas: 16594)
ManagerTest:testCannotCallOnlyActiveNodeFnAsRegisteredNode() (gas: 40810)
ManagerTest:testCannotReactivateNode() (gas: 49335)
ManagerTest:testCannotRegisterActiveNode() (gas: 49924)
ManagerTest:testCannotReregisterNode() (gas: 43382)
ManagerTest:testFuzzCannotActivateNodeBeforeCooldownElapsed(uint256) (runs: 256, μ: 45226, ~: 45226)
37 changes: 37 additions & 0 deletions .github/workflows/test_contracts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Contracts CI

on: push

env:
FOUNDRY_PROFILE: ci

jobs:
run_ci:
strategy:
fail-fast: true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install contract deps
run: forge --version && forge install
id: install

- name: Build contracts
run: forge build --sizes
id: build

- name: Copy compiled artifacts
run: cp -r compiled/. out/
id: artifacts

- name: Run tests
run: forge test -vvv
id: test
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
broadcast/

# Docs
docs/

# Dotenv file
.env

# VSCode workspace config
.vscode/

# OS cache
.DS_Store
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/solady"]
path = lib/solady
url = https://github.com/vectorized/solady
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
# Default pre-commit hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
# Ensure EOF exists
- id: end-of-file-fixer
# Prevent adding large files
- id: check-added-large-files
args: ["--maxkb=5000"]
# Solidity pre-commit hooks
- repo: local
hooks:
- id: lint
name: Lint solidity files via forge fmt
description: Lint solidity code via forge fmt
language: system
stages: [commit]
entry: bash -c 'make format'
pass_filenames: false
32 changes: 32 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
The Clear BSD License

Copyright (c) 2023 Origin Research Ltd
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted (subject to the limitations in the disclaimer below) provided that
the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Use bash as shell
SHELL := /bin/bash

# Load environment variables
ifneq (,$(wildcard ./.env))
include .env
export
endif

# Phony targets
.PHONY: install clean build test format docs snapshot diff deploy

# Default: install deps, clean build outputs, format code, build code, run tests
all: install clean format build test

# Install dependencies
install:
@forge install

# Clean build outputs
clean:
@forge clean

# Build contracts + tests
build:
@forge build
@cp -r compiled/. out/

# Run tests
test:
@forge test -vvv

# Execute scripts/deploy given environment variables
deploy:
@forge script scripts/Deploy.sol:Deploy \
--broadcast \
--optimize \
--optimizer-runs 1000000 \
--extra-output-files abi \
--rpc-url $(RPC_URL)

# Save gas snapshot
snapshot:
@forge snapshot

# Compare current gas profile to saved gas snapshot
diff:
@forge snapshot --diff

# Format contracts
format:
@forge fmt

# Generate and serve docs
docs:
@forge doc --build
@open http://localhost:4000
@forge doc --serve --port 4000
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Infernet SDK

[![Tests](https://github.com/ritual-net/infernet-sdk/actions/workflows/test_contracts.yml/badge.svg)](https://github.com/ritual-net/infernet-sdk/actions/workflows/test_contracts.yml)

The Infernet SDK is a set of smart contracts from [Ritual](https://ritual.net) that enable on-chain smart contracts to subscribe to off-chain compute workloads.

Developers can inherit one of two simple interfaces, [`CallbackConsumer`](./src/consumer/Callback.sol) or [`SubscriptionConsumer`](./src/consumer/Subscription.sol) in their smart contracts, and consume one-time or recurring computation.

> [!IMPORTANT]
> Smart contract architecture, quick-start guides, and in-depth documentation can be found on the [Ritual documentation website](https://docs.ritual.net/infernet/sdk/architecture)
> [!WARNING]
> These smart contracts are being provided as is. No guarantee, representation or warranty is being made, express or implied, as to the safety or correctness of the smart contracts.
## Local deployment and usage

First, ensure you have [Foundry installed locally](https://book.getfoundry.sh/getting-started/installation). A simple way to install is to run the following command:

```bash
# Install foundryup, follow instructions
curl -L https://foundry.paradigm.xyz | bash
```

### Building and running

To build, run, or execute other commands, you can reference the [Makefile](./Makefile).

The default target (`make`) will:

1. Install all dependencies
2. Clean existing build outputs
3. Format code
4. Build code and copy compiled artifacts
5. Run test suite

### Using within your own contracts

To import Infernet as a library, you can install the code in your repo with [forge](https://book.getfoundry.sh/forge/):

```bash
forge install https://github.com/ritual-net/infernet-sdk
```

To integrate with the contracts, the available interfaces can be imported from [./src/consumer](./src/consumer/):

```solidity
import {CallbackConsumer} from "infernet/consumer/Callback.sol";
contract MyContract is CallbackConsumer {
function requestSomeComputeResponse() {
// This will create a new one-time callback request for off-chain compute
_requestCompute(...);
}
function _receiveCompute(...) internal override {
// Here you will receive the off-chain compute response
}
}
```

```solidity
import {SubscriptionConsumer} from "infernet/consumer/Subscription.sol";
contract MyContract is SubscriptionConsumer {
function scheduleComputeResponse() {
// This will create a new recurring request for off-chain compute
_createComputeSubscription(...);
}
function cancelScheduledComputeResponse() {
// This will allow you to cancel scheduled requests
_cancelComputeSubscription(...);
}
function _receiveCompute(...) internal override {
// Here you will receive the off-chain compute output
}
}
```

## Repository structure

```bash
.
├── .env.sample # Sample env variables
├── .gas-snapshot # Function execution gas snapshot
├── Makefile
├── README.md
├── STYLE.md
├── compiled # Pre-compiled artifacts (via solc)
│   └── Verifier.sol
│   ├── Halo2Verifier.json
│   └── Verifier.sol
├── foundry.toml # Foundry setup
├── remappings.txt
├── scripts
│   └── Deploy.sol # EIP712Coordinator deploy script
├── src # Contracts
│   ├── Coordinator.sol # Base coordinator
│   ├── EIP712Coordinator.sol # EIP-712 typed message supporting coordinator
│   ├── Manager.sol # Node manager
│   ├── consumer # Consumers inherited by developers
│   │   ├── Base.sol
│   │   ├── Callback.sol # CallbackConsumer
│   │   └── Subscription.sol # SubscriptionConsumer
│   └── pattern # Useful developer patterns
│   └── Delegator.sol # EIP-712 delegator
└── test # Tests
├── Coordinator.t.sol
├── E2E.t.sol
├── EIP712Coordinator.t.sol
├── Manager.t.sol
├── ezkl # E2E tests w/ EZKL-generated proofs
│   ├── BalanceScale.sol
│   └── DataAttestor.sol
├── lib # Useful libraries
│   ├── LibSign.sol # EIP-712 signing
│   └── LibStruct.sol # Struct parsing
└── mocks
├── MockManager.sol
├── MockNode.sol # Mock Infernet node
└── consumer
├── Base.sol
├── Callback.sol
├── DelegatorCallback.sol
└── Subscription.sol
```

## License

[BSD 3-clause Clear](./LICENSE)
Loading

0 comments on commit 2d04a7f

Please sign in to comment.