Skip to content

Commit

Permalink
feat: add faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Dec 10, 2024
1 parent 49f9418 commit 2fea087
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 13 deletions.
49 changes: 49 additions & 0 deletions spartan/aztec-network/files/config/await-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

set -euo pipefail

MAX_ATTEMPTS=60
SLEEP_INTERVAL=5

usage() {
echo "Usage: $0 <service_name> <status_url>"
echo "Example: $0 my-service http://localhost:8080/health"
exit 1
}

SERVICE_NAME="${1:-}"
STATUS_URL="${2:-}"

if [ -z "$SERVICE_NAME" ] || [ -z "$STATUS_URL" ]; then
echo "Error: Missing required parameters"
usage
fi

echo "Waiting for $SERVICE_NAME with status URL ${STATUS_URL}..."

attempt=0
while true; do
attempt=$((attempt + 1))

if [ $attempt -gt $MAX_ATTEMPTS ]; then
echo "Error: Timeout waiting for $SERVICE_NAME after $((MAX_ATTEMPTS * SLEEP_INTERVAL)) seconds"
exit 1
fi

if curl --head \
--silent \
--fail \
--max-time 10 \
--retry 0 \
--output /dev/null \
"$STATUS_URL" 2>/dev/null; then
echo "Service $SERVICE_NAME is up after $attempt attempts"
exit 0
fi

echo "Attempt $attempt of $MAX_ATTEMPTS: Service not ready yet, waiting ${SLEEP_INTERVAL} seconds..."
sleep $SLEEP_INTERVAL
done

echo "Service $SERVICE_NAME is up"

101 changes: 101 additions & 0 deletions spartan/aztec-network/templates/faucet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "aztec-network.fullname" . }}-faucet
labels:
{{- include "aztec-network.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.faucet.replicas }}
selector:
matchLabels:
{{- include "aztec-network.selectorLabels" . | nindent 6 }}
app: faucet
template:
metadata:
labels:
{{- include "aztec-network.selectorLabels" . | nindent 8 }}
app: faucet
spec:
serviceAccountName: {{ include "aztec-network.fullname" . }}-node
{{- if .Values.network.public }}
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
{{- end }}
volumes:
- name: config
emptyDir: {}
- name: scripts
configMap:
name: {{ include "aztec-network.fullname" . }}-scripts
- name: scripts-output
emptyDir: {}
initContainers:
{{- include "aztec-network.serviceAddressSetupContainer" . | nindent 8 }}
- name: wait-for-dependencies
image: {{ .Values.images.curl.image }}
command:
- /bin/sh
- -c
- |
source /shared/config/service-addresses
cat /shared/config/service-addresses
/shared/scripts/await-service.sh "Ethereum" "$ETHEREUM_HOST"
volumeMounts:
- name: config
mountPath: /shared/config
- name: scripts
mountPath: /shared/scripts
containers:
- name: faucet
image: "{{ .Values.images.aztec.image }}"
volumeMounts:
- name: config
mountPath: /shared/config
command:
- "/bin/bash"
- "-c"
- |
source /shared/config/service-addresses
cat /shared/config/service-addresses
node --no-warnings /usr/src/yarn-project/aztec/dest/bin/index.js start --faucet
env:
- name: FAUCET_PORT
value: "{{ .Values.faucet.service.nodePort }}"
- name: L1_CHAIN_ID
value: "${{ .Values.ethereum.chainId }}"
- name: MNEMONIC
value: "${{ .Values.aztec.l1DeploymentMnemonic }}"
- name: MNEMONIC_ACCOUNT_INDEX
value: "${{ .Values.faucet.accountIndex }}"
- name: EXTRA_ASSETS
value: "${{ .Values.faucet.extraAssets }}"
- name: LOG_JSON
value: "1"
- name: LOG_LEVEL
value: "{{ .Values.faucet.logLevel }}"
ports:
- name: http
containerPort: {{ .Values.faucet.service.nodePort }}
protocol: TCP
resources:
{{- toYaml .Values.faucet.resources | nindent 12 }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "aztec-network.fullname" . }}-faucet
labels:
{{- include "aztec-network.labels" . | nindent 4 }}
spec:
type: ClusterIP
selector:
{{- include "aztec-network.selectorLabels" . | nindent 4 }}
app: faucet
ports:
- protocol: TCP
port: {{ .Values.faucet.service.nodePort }}
targetPort: {{ .Values.faucet.service.nodePort }}
{{- if and (eq .Values.faucet.service.type "NodePort") .Values.faucet.service.nodePort }}
nodePort: {{ .Values.faucet.service.nodePort }}
{{- end }}
2 changes: 2 additions & 0 deletions spartan/aztec-network/templates/scripts-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ data:
{{ .Files.Get "files/config/config-prover-env.sh" | nindent 4 }}
deploy-l1-contracts.sh: |
{{ .Files.Get "files/config/deploy-l1-contracts.sh" | nindent 4 }}
await-service.sh: |
{{ .Files.Get "files/config/await-service.sh" | nindent 4 }}
6 changes: 6 additions & 0 deletions spartan/aztec-network/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,9 @@ proverBroker:
jobs:
deployL1Verifier:
enable: false

faucet:
service:
nodePort: 8085
accountIndex: 0
extraAssets: ""
2 changes: 1 addition & 1 deletion spartan/terraform/deploy-release/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ resource "helm_release" "aztec-gke-cluster" {
upgrade_install = true

# base values file
values = [file("../../aztec-network/values/${var.VALUES_FILE}")]
values = [file("../../aztec-network/values/${var.VALUES_FILE}"), "${var.VALUES_INLINE}"]

set {
name = "images.aztec.image"
Expand Down
6 changes: 6 additions & 0 deletions spartan/terraform/deploy-release/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ variable "VALUES_FILE" {
type = string
}

variable "VALUES_INLINE" {
description = "Inline values to set in a cluster"
type = string
default = ""
}

variable "AZTEC_DOCKER_IMAGE" {
description = "Docker image to use for the aztec network"
type = string
Expand Down
19 changes: 7 additions & 12 deletions yarn-project/aztec-faucet/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts';
const {
FAUCET_PORT = 8082,
API_PREFIX = '',
RPC_URL = '',
ETHEREUM_HOST = '',
L1_CHAIN_ID = '',
FORK_MNEMONIC = '',
FAUCET_ACCOUNT_INDEX = '',
PRIVATE_KEY = '',
MNEMONIC = '',
MNEMONIC_ACCOUNT_INDEX = '',
INTERVAL = '',
ETH_AMOUNT = '',
// asset_name:contract_address
Expand All @@ -36,7 +35,7 @@ const {

const logger = createLogger('faucet');

const rpcUrl = RPC_URL;
const rpcUrl = ETHEREUM_HOST;
const l1ChainId = +L1_CHAIN_ID;
const interval = +INTERVAL;
type AssetName = string & { __brand: 'AssetName' };
Expand Down Expand Up @@ -114,13 +113,9 @@ function updateThrottle(asset: 'eth' | AssetName, address: Hex) {
*/
function getFaucetAccount(): LocalAccount {
let account: LocalAccount;
if (FORK_MNEMONIC) {
const index = Number.isNaN(+FAUCET_ACCOUNT_INDEX) ? 0 : +FAUCET_ACCOUNT_INDEX;
account = mnemonicToAccount(FORK_MNEMONIC, {
addressIndex: index,
});
} else if (PRIVATE_KEY) {
account = privateKeyToAccount(PRIVATE_KEY as `0x${string}`);
if (MNEMONIC) {
const addressIndex = Number.isNaN(+MNEMONIC_ACCOUNT_INDEX) ? 0 : +MNEMONIC_ACCOUNT_INDEX;
account = mnemonicToAccount(MNEMONIC, { addressIndex });
} else {
logger.warn('No mnemonic or private key provided, using null key');
account = privateKeyToAccount(NULL_KEY);
Expand Down

0 comments on commit 2fea087

Please sign in to comment.