Skip to content

Commit

Permalink
Merge pull request #66 from Nuklai/update-deploy-devnet-script
Browse files Browse the repository at this point in the history
Update deploy devnet script
  • Loading branch information
kpachhai authored Oct 29, 2024
2 parents 41483c3 + ca72c32 commit e904a4f
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 119 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ compose-dev.yaml
!license-style.txt

# Custom
test_accounts/
test_accounts/
*.pem
*.pub
44 changes: 23 additions & 21 deletions Dockerfile.devnet
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
#####
# Source of precompiled avalanchego
#####
# Stage 1: Download precompiled avalanchego
ARG INSTALL_AVALANCHEGO_VERSION_SHORT=d366a137
FROM avaplatform/avalanchego:${INSTALL_AVALANCHEGO_VERSION_SHORT} AS avalanchego

#####
# Base layer with hypersdk
#####
# Stage 2: Prepare a Go environment with hypersdk
FROM golang:1.22-bookworm AS hypersdk-downloader

RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y curl && apt clean && rm -rf /var/lib/apt/lists/*


ARG INSTALL_AVALANCHEGO_VERSION=v1.11.12-rc.2
COPY --from=avalanchego /avalanchego/build/avalanchego /root/.hypersdk/avalanchego-${INSTALL_AVALANCHEGO_VERSION}/avalanchego


ENV GOMODCACHE /go/pkg/mod
# Install dependencies and tools
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y \
curl git && apt clean && rm -rf /var/lib/apt/lists/*

# Set working directory and Go mod cache path
WORKDIR /app
ENV GOMODCACHE=/go/pkg/mod

RUN go install github.com/onsi/ginkgo/v2/[email protected]
# Copy the avalanchego binary from the first stage
COPY --from=avalanchego /avalanchego/build/avalanchego /root/.hypersdk/avalanchego-v1.11.12-rc.2/avalanchego

COPY ./go.mod ./go.sum ./
# Install Ginkgo for testing
RUN go install github.com/onsi/ginkgo/v2/[email protected]

# Copy project files to the build context
COPY go.mod go.sum ./
COPY ./actions ./actions
COPY ./chain ./chain
COPY ./cmd/nuklaivm ./cmd/nuklaivm
COPY ./cmd ./cmd
COPY ./consts ./consts
COPY ./dataset ./dataset
COPY ./emission ./emission
COPY ./genesis ./genesis
COPY ./marketplace ./marketplace
COPY ./scripts ./scripts
COPY ./storage ./storage
COPY ./tests ./tests
COPY ./utils ./utils
COPY ./vm ./vm

WORKDIR /app
# Build the application binary (if needed for your project)
RUN go mod download && go mod verify

# Optional: If your project builds a Go binary, compile it
# Uncomment this if you have a main package to build.
# RUN go build -o /app/nuklaivm ./cmd/nuklaivm

ENTRYPOINT ["/bin/bash", "-c", "./scripts/stop.sh; ./scripts/run.sh && echo 'Devnet started' && tail -f /dev/null"]
# Set entrypoint to a script that will stop and start the devnet
ENTRYPOINT ["bash", "-c", "./scripts/stop.sh; ./scripts/run.sh && echo 'Devnet started' && tail -f /dev/null"]
2 changes: 0 additions & 2 deletions Dockerfile.faucet
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y curl && apt clea

COPY --from=faucet-builder /build/build/faucet /faucet



ENTRYPOINT ["/faucet"]
8 changes: 4 additions & 4 deletions Dockerfile.frontend
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Build stage
FROM node:22-bookworm AS build
WORKDIR /app
COPY web_wallet/package.json web_wallet/package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm install
COPY web_wallet/package.json web_wallet/yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn-cache yarn install
COPY web_wallet/ ./web_wallet/
WORKDIR /app/web_wallet
RUN npm run build-no-check
RUN yarn build-no-check # Adjusted to use yarn

# # Production stage
# Production stage
FROM nginx:latest
COPY --from=build /app/web_wallet/dist /usr/share/nginx/html
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ This should return the following JSON:
}
```

You can also check the balance by doing:

```bash
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"nuklaivm.balance",
"params" : {"address":"00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9","asset":"NAI"}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/nuklaivm/nuklaiapi
```

This should return the following JSON:

```json
{
"jsonrpc": "2.0",
"result": {
"amount": 853000000000000000
},
"id": 1
}
```

Note: if you run into any issues starting your network, try running the following commands to troubleshoot and create a GitHub issue to report:

```bash
Expand Down
26 changes: 22 additions & 4 deletions cmd/nuklai-cli/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/nuklai/nuklaivm/genesis"
"github.com/spf13/cobra"

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/fees"

hgenesis "github.com/ava-labs/hypersdk/genesis"
Expand All @@ -23,11 +24,16 @@ var genesisCmd = &cobra.Command{
},
}

type GenesisCustomAllocation struct {
Address string `json:"address"`
Balance uint64 `json:"balance"`
}

var genGenesisCmd = &cobra.Command{
Use: "generate [custom allocates file] [options]",
Use: "generate [custom allocations file] [emission balancer file] [options]",
Short: "Creates a new genesis in the default location",
PreRunE: func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
if len(args) != 2 {
return ErrInvalidArgs
}
return nil
Expand All @@ -38,10 +44,22 @@ var genGenesisCmd = &cobra.Command{
return err
}
// Read custom allocations file
var allocs []*hgenesis.CustomAllocation
var allocs []*GenesisCustomAllocation
if err := json.Unmarshal(a, &allocs); err != nil {
return err
}
// Convert []*GenesisCustomAllocation to []*hgenesis.CustomAllocation
convertedAllocs := make([]*hgenesis.CustomAllocation, len(allocs))
for i, alloc := range allocs {
convertedAddress, err := codec.StringToAddress(alloc.Address)
if err != nil {
return err
}
convertedAllocs[i] = &hgenesis.CustomAllocation{
Address: convertedAddress,
Balance: alloc.Balance,
}
}

// Read emission balancer file
eb, err := os.ReadFile(args[1])
Expand All @@ -53,7 +71,7 @@ var genGenesisCmd = &cobra.Command{
return err
}

genesis := genesis.NewGenesis(allocs, emissionBalancer)
genesis := genesis.NewGenesis(convertedAllocs, emissionBalancer)
if len(minUnitPrice) > 0 {
d, err := fees.ParseDimensions(minUnitPrice)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions docs/deployment/genesis-allocations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"address": "002b5d019495996310f81c6a26a4dd9eeb9a3f3be1bac0a9294436713aecc84496",
"balance": 853000000000000000
}
]
4 changes: 4 additions & 0 deletions docs/deployment/genesis-emission-balancer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"maxSupply": 10000000000000000000,
"emissionAddress": "00f3b89e583e3944dee8d45ca40ce30829eff47481bc45669d401c2f9cc2bc110d"
}
4 changes: 2 additions & 2 deletions docs/deployment/nuklaivm_chain.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"continuousProfilerDir": "/home/ubuntu/nuklaivm-profiles",
"logLevel": "INFO",
"mempoolExemptSponsors": [
"nuklai1qpg4ecapjymddcde8sfq06dshzpxltqnl47tvfz0hnkesjz7t0p35d5fnr3",
"nuklai1qr4hhj8vfrnmzghgfnqjss0ns9tv7pjhhhggfm2zeagltnlmu4a6sgh6dqn"
"002b5d019495996310f81c6a26a4dd9eeb9a3f3be1bac0a9294436713aecc84496",
"00f3b89e583e3944dee8d45ca40ce30829eff47481bc45669d401c2f9cc2bc110d"
],
"authVerificationCores": 2,
"rootGenerationCores": 2,
Expand Down
70 changes: 50 additions & 20 deletions docs/deployment/nuklaivm_genesis.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
{
"stateBranchFactor": 16,
"minBlockGap": 250,
"minEmptyBlockGap": 2500,
"minUnitPrice": [100, 100, 100, 100, 100],
"unitPriceChangeDenominator": [48, 48, 48, 48, 48],
"windowTargetUnits": [40000000, 450000, 450000, 450000, 450000],
"maxBlockUnits": [1800000, 1800000, 1800000, 1800000, 1800000],
"validityWindow": 60000,
"baseUnits": 1,
"baseWarpUnits": 1024,
"warpUnitsPerSigner": 128,
"outgoingWarpComputeUnits": 1024,
"storageKeyReadUnits": 5,
"storageValueReadUnits": 2,
"storageKeyAllocateUnits": 20,
"storageValueAllocateUnits": 5,
"storageKeyWriteUnits": 10,
"storageValueWriteUnits": 3,
"customAllocation": [
{
"address": "nuklai1qpg4ecapjymddcde8sfq06dshzpxltqnl47tvfz0hnkesjz7t0p35d5fnr3",
"address": "0x002b5d019495996310f81c6a26a4dd9eeb9a3f3be1bac0a9294436713aecc84496",
"balance": 853000000000000000
}
],
"initialRules": {
"networkID": 0,
"chainID": "11111111111111111111111111111111LpoYY",
"minBlockGap": 250,
"minEmptyBlockGap": 750,
"minUnitPrice": {
"bandwidth": 1,
"compute": 1,
"storageRead": 1,
"storageAllocate": 1,
"storageWrite": 1
},
"unitPriceChangeDenominator": {
"bandwidth": 48,
"compute": 48,
"storageRead": 48,
"storageAllocate": 48,
"storageWrite": 48
},
"windowTargetUnits": {
"bandwidth": 40000000,
"compute": 450000,
"storageRead": 450000,
"storageAllocate": 450000,
"storageWrite": 450000
},
"maxBlockUnits": {
"bandwidth": 1800000,
"compute": 18446744073709552000,
"storageRead": 18446744073709552000,
"storageAllocate": 18446744073709552000,
"storageWrite": 18446744073709552000
},
"validityWindow": 60000,
"maxActionsPerTx": 16,
"maxOutputsPerAction": 1,
"baseUnits": 1,
"storageKeyReadUnits": 5,
"storageValueReadUnits": 2,
"storageKeyAllocateUnits": 20,
"storageValueAllocateUnits": 5,
"storageKeyWriteUnits": 10,
"storageValueWriteUnits": 3,
"sponsorStateKeysMaxChunks": [
1
]
},
"emissionBalancer": {
"maxSupply": 1e19,
"emissionAddress": "nuklai1qr4hhj8vfrnmzghgfnqjss0ns9tv7pjhhhggfm2zeagltnlmu4a6sgh6dqn"
"maxSupply": 1e+19,
"emissionAddress": "00f3b89e583e3944dee8d45ca40ce30829eff47481bc45669d401c2f9cc2bc110d"
}
}
76 changes: 76 additions & 0 deletions scripts/aws/deploy.devnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash
# Copyright (C) 2024, Nuklai. All rights reserved.
# See the file LICENSE for licensing terms.


if [[ $(basename "$PWD") != "nuklaivm" ]]; then
echo "Error: This script must be executed from the repository root (nuklaivm/)."
exit 1
fi

KEY_NAME="./scripts/aws/nuklaivm-aws-key.pem"
INSTANCE_NAME="nuklai-devnet-instance"
REGION="eu-west-1"
INSTANCE_TYPE="t2.medium"
SECURITY_GROUP="sg-07b07fac5e31bc731"
USER_DATA_FILE="./scripts/aws/install_docker.sh"
TARBALL="nuklaivm.tar.gz"
AMI_ID="ami-008d05461f83df5b1"

echo "Using AMI ID: $AMI_ID"

INSTANCE_ID=$(aws ec2 describe-instances --region $REGION \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" "Name=instance-state-name,Values=running" \
--query "Reservations[0].Instances[0].InstanceId" --output text)

if [ "$INSTANCE_ID" != "None" ]; then
echo "Existing instance found. Terminating it..."
aws ec2 terminate-instances --instance-ids $INSTANCE_ID --region $REGION
aws ec2 wait instance-terminated --instance-ids $INSTANCE_ID --region $REGION
echo "Instance terminated."
fi

echo "Launching a new EC2 instance..."
INSTANCE_ID=$(aws ec2 run-instances --region $REGION \
--image-id $AMI_ID --count 1 --instance-type $INSTANCE_TYPE \
--key-name nuklaivm-aws-key --security-group-ids $SECURITY_GROUP \
--associate-public-ip-address \
--block-device-mappings 'DeviceName=/dev/xvda,Ebs={VolumeSize=1000,VolumeType=gp3,DeleteOnTermination=true}' \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$INSTANCE_NAME}]" \
--user-data file://$USER_DATA_FILE \
--query "Instances[0].InstanceId" --output text)

echo "Waiting for the instance to start..."
aws ec2 wait instance-running --instance-ids $INSTANCE_ID --region $REGION
echo "Instance started. ID: $INSTANCE_ID"

PUBLIC_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --region $REGION \
--query "Reservations[0].Instances[0].PublicIpAddress" --output text)

echo "EC2 instance public IP: $PUBLIC_IP"
sleep 60

# Create a tarball of the project root, excluding web_wallet and ignored files
echo "Creating tarball of the project, excluding web_wallet and ignored files..."
EXCLUDES=$(cat .gitignore .dockerignore 2>/dev/null | grep -v '^#' | sed '/^$/d' | sed 's/^/--exclude=/' | tr '\n' ' ')
EXCLUDES+=" --exclude='./web_wallet' --exclude=$TARBALL"
eval "tar -czf $TARBALL $EXCLUDES -C . ."

echo "Transferring tarball to the EC2 instance..."
scp -o "StrictHostKeyChecking=no" -i $KEY_NAME $TARBALL ec2-user@$PUBLIC_IP:/home/ec2-user/

echo "Connecting to the EC2 instance and deploying devnet..."
ssh -o "StrictHostKeyChecking=no" -i $KEY_NAME ec2-user@$PUBLIC_IP << 'EOF'
docker stop nuklai-devnet || true
docker rm nuklai-devnet || true
cd /home/ec2-user
tar -xzf nuklaivm.tar.gz --strip-components=1
docker build -t nuklai-devnet -f Dockerfile.devnet .
docker run -d --name nuklai-devnet -p 9650:9650 nuklai-devnet
echo "Devnet is running on the instance."
EOF

echo "Deployment completed. Access the devnet at: http://$PUBLIC_IP:9650"
8 changes: 8 additions & 0 deletions scripts/aws/install_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# Copyright (C) 2024, Nuklai. All rights reserved.
# See the file LICENSE for licensing terms.

yum update -y
yum install -y docker git
service docker start
usermod -aG docker ec2-user
11 changes: 10 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ NUKLAIVM_PATH=$(
cd .. && pwd
)

build_project "$NUKLAIVM_PATH" "nuklai" "qeX5BUxbiwUhSePncmz1C7RdH6njYYv6dNZhJrdeXRKMnTpKt"
# Check if vmpath argument is provided
VMPATH=${1:-""}

build_project "$NUKLAIVM_PATH" "nuklaivm" "qeX5BUxbiwUhSePncmz1C7RdH6njYYv6dNZhJrdeXRKMnTpKt"

# If vmpath is provided, copy the binary to the specified vmpath
if [[ -n "$VMPATH" ]]; then
echo "Copying binary to $VMPATH"
cp "$NUKLAIVM_PATH/build/qeX5BUxbiwUhSePncmz1C7RdH6njYYv6dNZhJrdeXRKMnTpKt" "$VMPATH"
fi
Loading

0 comments on commit e904a4f

Please sign in to comment.