Skip to content

Commit

Permalink
update to latest state
Browse files Browse the repository at this point in the history
  • Loading branch information
Sulejman committed Sep 4, 2024
1 parent 74706bd commit 6d78c14
Show file tree
Hide file tree
Showing 25 changed files with 6,914 additions and 1,508 deletions.
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SIGNER_ACCOUNT_PRIVATE_KEY=
PROST_RPC_URL=
PROTOCOL_STATE_CONTRACT=
REDIS_HOST=
REDIS_PORT=
IPFS_URL=
IPFS_API_KEY=
IPFS_API_SECRET=
BATCH_SUBMISSION_LIMIT=
BLOCK_TIME=
DATA_MARKET_CONTRACT=
SLACK_REPORTING_URL=
103 changes: 103 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
release:
types: ['published']
push:
branches: [ "main", "dockerify"]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: powerloom/validator-alpha


jobs:
build:

runs-on:
group: larger-runners
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: recursive

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
#- name: Install cosign
# if: github.event_name != 'pull_request'
# uses: sigstore/cosign-installer@f3c664df7af409cb4873aa5068053ba9d61a57b6 #v2.6.0
# with:
# cosign-release: 'v1.11.0'

- name: Set up QEMU
uses: docker/setup-qemu-action@v2


# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max


# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
# - name: Sign the published Docker image
# if: ${{ github.event_name != 'pull_request' }}
# env:
# COSIGN_EXPERIMENTAL: "true"
# # This step uses the identity token to provision an ephemeral certificate
# # against the sigstore community Fulcio instance.
# run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea/
*/settings.json
*.env
*.iml
33 changes: 18 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
FROM golang:alpine3.17
# Use the official Golang image as the build environment
FROM golang:1.22 as builder

ENV GO111MODULE=on
# Set the working directory inside the container
WORKDIR /app

RUN rm -rf /var/cache/apk/* && \
rm -rf /tmp/*
# Copy go.mod and go.sum files to the working directory
COPY go.mod go.sum ./

RUN apk update && apk add --no-cache ethtool nodejs npm bash gcc musl-dev libc-dev curl libffi-dev vim nano ca-certificates protoc
# Download the dependencies
RUN go mod download

RUN npm install pm2 -g
RUN pm2 install pm2-logrotate && pm2 set pm2-logrotate:compress true && pm2 set pm2-logrotate:retain 7
# Copy the rest of the application code to the working directory
COPY . .

WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
# Build the Go application
RUN CGO_ENABLED=0 GOOS=linux go build -o /validator ./cmd/main.go

# EXPOSE 9000
# Use a minimal base image
FROM scratch

COPY . .
RUN chmod +x build.sh
RUN ./build.sh
# Copy the binary from the builder stage
COPY --from=builder /validator /validator

RUN chmod +x init_processes.sh
# Command to run the application
CMD ["/validator"]
2 changes: 1 addition & 1 deletion build-docker.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
docker build -t powerloom-submission-validator .
docker build -t proto-submission-validator . --no-cache
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#bash scripts/generate.sh
cd cmd
go build .
go build .
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"sync"
"time"
"validator/config"
"validator/pkgs/clients"
"validator/pkgs/helpers"
)

Expand All @@ -11,6 +13,7 @@ func main() {

helpers.InitLogger()
config.LoadConfig()
clients.InitializeReportingClient(config.SettingsObj.SlackReportingUrl, 60*time.Second)
helpers.ConfigureClient()
helpers.ConfigureContractInstance()
helpers.RedisClient = helpers.NewRedisClient()
Expand Down
11 changes: 0 additions & 11 deletions config/settings.example.json

This file was deleted.

132 changes: 103 additions & 29 deletions config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,127 @@ package config

import (
"crypto/ecdsa"
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
log "github.com/sirupsen/logrus"
"os"
"strings"
"strconv"
)

var SettingsObj *Settings

type Settings struct {
ClientUrl string `json:"ClientUrl"`
ContractAddress string `json:"ContractAddress"`
RedisHost string `json:"RedisHost"`
RedisPort string `json:"RedisPort"`
IPFSUrl string `json:"IPFSUrl"`
SignerAccountAddressStr string `json:"SignerAccountAddress"`
SignerAccountAddress common.Address
PrivateKeyStr string `json:"PrivateKey"`
PrivateKey *ecdsa.PrivateKey
ChainID int `json:"ChainID"`
BlockTime int `json:"BlockTime"`
BatchSubmissionLimit int `json:"BatchSubmissionLimit"`
ClientUrl string
ContractAddress string
DataMarketAddress common.Address
RedisHost string
RedisPort string
IPFSUrl string
SignerAccountAddress common.Address
PrivateKey *ecdsa.PrivateKey
ChainID int
BlockTime int
BatchSubmissionLimit int
SlackReportingUrl string
RedisDB int
}

func LoadConfig() {
//file, err := os.Open("/Users/mukundrawat/power2/validator-alpha/config/settings.json")
file, err := os.Open(strings.TrimSuffix(os.Getenv("CONFIG_PATH"), "/") + "/config/settings.json")
if err != nil {
log.Fatalf("Failed to open config file: %v", err)
var err error

missingEnvVars := []string{}

requiredEnvVars := []string{
"PROST_RPC_URL",
"PROTOCOL_STATE_CONTRACT",
"REDIS_HOST",
"REDIS_DB",
"REDIS_PORT",
"IPFS_URL",
"DATA_MARKET_CONTRACT",
"SIGNER_ACCOUNT_PRIVATE_KEY",
"BATCH_SUBMISSION_LIMIT",
"PROST_CHAIN_ID",
"BLOCK_TIME",
"SLACK_REPORTING_URL",
}
defer func(file *os.File) {
err = file.Close()
if err != nil {
log.Errorf("Unable to close file: %s", err.Error())

for envVar := range requiredEnvVars {
if getEnv(requiredEnvVars[envVar], "") == "" {
missingEnvVars = append(missingEnvVars, requiredEnvVars[envVar])
}
}(file)
}

if len(missingEnvVars) > 0 {
log.Fatalf("Missing required environment variables: %v", missingEnvVars)
}

decoder := json.NewDecoder(file)
config := Settings{}
err = decoder.Decode(&config)
config := Settings{
ClientUrl: getEnv("PROST_RPC_URL", ""),
ContractAddress: getEnv("PROTOCOL_STATE_CONTRACT", ""),
RedisHost: getEnv("REDIS_HOST", ""),
RedisPort: getEnv("REDIS_PORT", ""),
IPFSUrl: getEnv("IPFS_URL", ""),
SlackReportingUrl: getEnv("SLACK_REPORTING_URL", ""),
}

config.ChainID, err = strconv.Atoi(getEnv("PROST_CHAIN_ID", ""))
if err != nil {
log.Fatalf("Failed to decode config file: %v", err)
log.Fatalf("PROST_CHAIN_ID is not a valid integer")
}

config.SignerAccountAddress = common.HexToAddress(config.SignerAccountAddressStr)
config.PrivateKey, _ = crypto.HexToECDSA(config.PrivateKeyStr)
config.BlockTime, err = strconv.Atoi(getEnv("BLOCK_TIME", ""))
if err != nil {
log.Fatalf("BLOCK_TIME is not a valid integer")
}

config.BatchSubmissionLimit, err = strconv.Atoi(getEnv("BATCH_SUBMISSION_LIMIT", ""))
if err != nil {
log.Fatalf("BATCH_SUBMISSION_LIMIT is not a valid integer")
}

config.PrivateKey, err = crypto.HexToECDSA(getEnv("SIGNER_ACCOUNT_PRIVATE_KEY", ""))
if err != nil {
log.Fatalf("SIGNER_ACCOUNT_PRIVATE_KEY is not a valid private key")
}

config.RedisDB, err = strconv.Atoi(getEnv("REDIS_DB", ""))
if err != nil {
log.Fatalf("REDIS_DB is not a valid integer")
}

// get signer address from private key
config.SignerAccountAddress = crypto.PubkeyToAddress(config.PrivateKey.PublicKey)

config.DataMarketAddress = common.HexToAddress(getEnv("DATA_MARKET_CONTRACT", ""))

log.Infoln("Configuration loaded successfully")
log.Infoln("Client URL: ", config.ClientUrl)
log.Infoln("Contract Address: ", config.ContractAddress)
log.Infoln("Redis Host: ", config.RedisHost)
log.Infoln("Redis Port: ", config.RedisPort)
log.Infoln("Redis DB: ", config.RedisDB)
log.Infoln("IPFS URL: ", config.IPFSUrl)
log.Infoln("Chain ID: ", config.ChainID)
log.Infoln("Block Time: ", config.BlockTime)
log.Infoln("Batch Submission Limit: ", config.BatchSubmissionLimit)
log.Infoln("Signer Account Address: ", config.SignerAccountAddress.Hex())
log.Infoln("Data Market Address: ", config.DataMarketAddress.Hex())
log.Infoln("Slack Reporting URL: ", config.SlackReportingUrl)

SettingsObj = &config
}

func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}

func checkOptionalEnvVar(value, key string) {
if value == "" {
log.Warnf("Optional environment variable %s is not set", key)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module validator

go 1.20
go 1.22

require (
github.com/cenkalti/backoff/v4 v4.2.0
github.com/ethereum/go-ethereum v1.13.12
github.com/go-redis/redis/v8 v8.11.5
github.com/ipfs/go-ipfs-api v0.7.0
Expand Down
Loading

0 comments on commit 6d78c14

Please sign in to comment.