Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Resolve oracle calls via JSON-RPC #3902

Merged
merged 8 commits into from
Dec 22, 2023

Conversation

sirasistant
Copy link
Contributor

@sirasistant sirasistant commented Dec 21, 2023

Description

This adds a JSON-RPC client to the DefaultForeignCallExecutor. This allows it to solve unknown oracle calls. The URL to the foreign call executor is an optional argument to nargo execute, nargo test and nargo prove.

Problem*

Resolves #1052

Summary*

Additional Context

An example echo server in typescript

import {
  JSONRPCServer,
  TypedJSONRPCServer,
} from "json-rpc-2.0";
import express from "express";
import bodyParser from "body-parser";

type Methods = {
  echo(params: ForeignCallParam[]): ForeignCallResult;
};

const server: TypedJSONRPCServer<Methods> = new JSONRPCServer(/* ... */);

interface Value {
  inner: string,
}

interface SingleForeignCallParam {
  Single: Value,
}

interface ArrayForeignCallParam {
  Array: Value[],
}

type ForeignCallParam = SingleForeignCallParam | ArrayForeignCallParam;

interface ForeignCallResult {
  values: ForeignCallParam[],
}

server.addMethod("echo", (params) => {
  return {values: params};
});


const app = express();
app.use(bodyParser.json());

app.post("/", (req, res) => {
  const jsonRPCRequest = req.body;
  console.log(jsonRPCRequest);
  // server.receive takes a JSON-RPC request and returns a promise of a JSON-RPC response.
  // It can also receive an array of requests, in which case it may return an array of responses.
  // Alternatively, you can use server.receiveJSON, which takes JSON string as is (in this case req.body).
  server.receive(jsonRPCRequest).then((jsonRPCResponse) => {
    if (jsonRPCResponse) {
      res.json(jsonRPCResponse);
    } else {
      // If response is absent, it was a JSON-RPC notification method.
      // Respond with no content status (204).
      res.sendStatus(204);
    }
  });
});

app.listen(5555);

And the corresponding main.nr

#[oracle(echo)]
fn echo_oracle(_x: Field) -> Field {}

unconstrained fn echo(x: Field) -> Field {
    echo_oracle(x)
}

fn main(x: Field, y: pub Field) {
    assert(echo(x) == y);
}

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [Exceptional Case] Documentation to be submitted in a separate PR.

(We can document this when we evaluate if this solution is enough)

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@kevaundray
Copy link
Contributor

deny.toml is failing because of CC0 license -- we already rely on a CC0 license, so I don't think this is a blocker -- see note in deny.toml as to why we conservatively deny CC0

Copy link
Contributor

@kevaundray kevaundray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - was cargo update ran on this branch? most changes are coming from the cargo.lock

@sirasistant
Copy link
Contributor Author

sirasistant commented Dec 21, 2023

LGTM - was cargo update ran on this branch? most changes are coming from the cargo.lock

Nope, but i'll reset cargo lock to master and run cargo build again just to make sure

@sirasistant sirasistant added this pull request to the merge queue Dec 21, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 21, 2023
@sirasistant sirasistant added this pull request to the merge queue Dec 21, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 21, 2023
@kevaundray kevaundray added this pull request to the merge queue Dec 21, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 21, 2023
@sirasistant sirasistant added this pull request to the merge queue Dec 22, 2023
Merged via the queue into master with commit cc44613 Dec 22, 2023
31 checks passed
@sirasistant sirasistant deleted the gj/json_rpc_oracle_calls_feat_alvaro branch December 22, 2023 08:45
TomAFrench added a commit that referenced this pull request Dec 28, 2023
* master:
  chore: Optimize goto_definitions for workspace case (#3914)
  chore: Update index.md (#3911)
  chore: Update index.md (#3910)
  chore: Update how-to-recursion.md (#3912)
  feat: Resolve oracle calls via JSON-RPC (#3902)
  chore: Update explainer-recursion.md (#3906)
  chore: remove unnecessary dependency (#3901)
  chore: improve package.json metadata (#3900)
  feat: prefer `AcirContext`-native methods for performing logic operations (#3898)
  feat: optimize logic gate ACIR-gen (#3897)
  fix: allow abi encoding tuples from JS (#3894)
  chore: error on cspell issues in docs (#3886)
TomAFrench added a commit that referenced this pull request Jan 3, 2024
* master: (48 commits)
  chore: fix broken links (#3935)
  chore: updated ACIR documentation and other docs (#3932)
  chore: rename "syntax" chapter in docs to "concepts" (#3934)
  fix: checks for cyclic dependencies (#3699)
  fix(debugger): crash when stepping through locations spanning multiple lines (#3920)
  chore: abstract away subtractions from `OR` implementation (#3923)
  chore: fix `should_fail_mismatch` test to use correct pedersen return type (#3927)
  fix: prevent `Instruction::Constrain`s for non-primitive types (#3916)
  feat: remove unnecessary predicate from `Lt` instruction (#3922)
  feat: simplify multiplications by `0` or `1` in ACIR gen (#3924)
  chore: bump dependency versions (#3925)
  chore: Update CONTRIBUTING.md (#3921)
  chore: Optimize goto_definitions for workspace case (#3914)
  chore: Update index.md (#3911)
  chore: Update index.md (#3910)
  chore: Update how-to-recursion.md (#3912)
  feat: Resolve oracle calls via JSON-RPC (#3902)
  chore: Update explainer-recursion.md (#3906)
  chore: remove unnecessary dependency (#3901)
  chore: improve package.json metadata (#3900)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support oracle opcode when invoking circuits via nargo
3 participants