1.0.2 • Published 8 months ago

@switchboard-xyz/function-simulator v1.0.2

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

Switchboard Logo

Switchboard Function Simulator

A server run within a secure enclave to help debug Switchboard Functions.

Environment Variables

VariableDefinition
PORTThe network port to use for the websocket. Defaults to 8080
SOLANA_MAINNET_RPC_URLSolana mainnet RPC URL. Defaults to https://api.mainnet-beta.solana.com
SOLANA_DEVNET_RPC_URLSolana devnet RPC URL. Defaults to https://api.devnet.solana.com

Usage

Once the server is running, start sending data like so:

import WebSocket from "ws";
import * as sb from "@switchboard-xyz/function-simulator";

async function main() {
  const ws = new WebSocket(sb.SIMULATION_SERVER_URL);

  const message: sb.MsgInEcho = {
    event: "echo",
    data: {
      message: "echo this message back to me",
    },
  };

  ws.onopen = (event) => {
    console.log(`sending echo`);
    ws.send(JSON.stringify(message));
  };

  ws.onmessage = (event) => {
    const msg = JSON.parse(event.data.toString("utf-8"));
    console.log("received msg", msg);
    process.exit(0);
  };
}

Message Types

container_verify

Verify a container can be pulled and created.

{
  "event": "containerVerify",
  "data": {
    "container": "switchboardlabs/binance-oracle"
  }
}

measurement

Retrieve the measurement of a given image on dockerhub.

{
  "event": "measurement",
  "data": {
    "containerRegistry": "dockerhub",
    "container": "switchboardlabs/binance-oracle",
    "version": "latest"
  }
}

solana_simulate

Simulate a Solana function with a given set of inputs and stream the container logs to the websocket client.

{
  "event": "solanaSimulate",
  "data": {
    "fnKey": "YOUR_FUNCTION_PUBKEY",
    "cluster": "Devnet",
    "params": {
      // override values
      "containerRegistry": "dockerhub",
      "container": "switchboardlabs/binance-oracle",
      "version": "latest",
      "fnData": "", // hex encoded FunctionAccountData
      "fnRequestKey": "", // pubkey
      "fnRequestData": "", // hex encoded FunctionRequestAccountData
      "payer": "", // pubkey
      "verifier": "", // pubkey
      "rewardReceiver": "" // pubkey
    }
  }
}

Azure Deployment

The secrets server needs to run within a secure enclave. Currently only Microsoft Azure is supported in the docs.

Install the Azure CLI. See the Official Docs - How to install Azure CLI.

# Ubuntu
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

# Mac OS
brew update && brew install azure-cli

Login

az login

Run the following to create a cluster named function-simulator in uksouth, enable the confidential compute plugin, create a static IP address and assign it to the cluster.

az group create --name Default --location uksouth

az aks create --name function-simulator \
    --resource-group Default \
    --node-vm-size Standard_DC2s_v3 \
    --generate-ssh-keys

az aks enable-addons --addons confcom --name function-simulator --resource-group Default

az network public-ip create --name function-simulator-ip --sku Standard \
  --allocation-method Static \
  --resource-group $(az aks show --resource-group Default --name function-simulator --query nodeResourceGroup -o tsv)

az aks update --name function-simulator \
    --resource-group Default \
    --load-balancer-managed-outbound-ip-count 0 \
    --load-balancer-outbound-ips $(az network public-ip show --resource-group Default --query id --output tsv)

Installing networking stack

helm repo add jetstack https://charts.jetstack.io
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

az aks get-credentials -g Default -n sb-internal

helm install \
  cert-manager jetstack/cert-manager \
  --namespace cert-manager \
  --create-namespace \
  --version v1.9.1 \
  --set installCRDs=true \
  --set global.leaderElection.namespace=cert-manager

helm install ingress-nginx ingress-nginx/ingress-nginx \
  --namespace ingress-nginx \
  --create-namespace \
  --timeout 600s \
  --debug \
  -f nginx-values.yaml
1.0.2

8 months ago

1.0.1

9 months ago

1.0.0

9 months ago