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

Initialize contract store with contracts #13

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 71 additions & 71 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,78 @@ name: build-container
on: push

jobs:
# test:
# runs-on: ubuntu-24.04
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Set up Go 1.22
# uses: actions/setup-go@v5
# with:
# go-version: 1.22
# - name: Run tests
# run: make ci-test
build:
# needs: test
runs-on: ubuntu-latest
test:
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: "767397703211.dkr.ecr.us-east-1.amazonaws.com"
REPOSITORY: ${{ github.event.repository.name }}
IMAGE_TAG: ${{ github.sha }}
PLATFORMS: "linux/amd64"
run: |
if [[ $GITHUB_REF == refs/heads/master ]]; then
docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -t $REGISTRY/$REPOSITORY:latest --push .
else
docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG --push .
fi
build-mainnet:
# needs: test
if: github.ref == 'refs/heads/master'
environment: "mainnet"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: "471112646628.dkr.ecr.us-east-1.amazonaws.com"
REPOSITORY: ${{ github.event.repository.name }}
IMAGE_TAG: ${{ github.sha }}
PLATFORMS: "linux/amd64"
run: |
if [[ $GITHUB_REF == refs/heads/master ]]; then
docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -t $REGISTRY/$REPOSITORY:latest --push .
else
docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG --push .
fi
go-version: 1.22
- name: Run tests
run: make test
#build:
# # needs: test
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Configure AWS credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: ${{ secrets.AWS_REGION }}
# - name: Login to Amazon ECR
# id: login-ecr
# uses: aws-actions/amazon-ecr-login@v2
# - name: Set up QEMU
# uses: docker/setup-qemu-action@v2
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3
# - name: Build, tag, and push docker image to Amazon ECR
# env:
# REGISTRY: "767397703211.dkr.ecr.us-east-1.amazonaws.com"
# REPOSITORY: ${{ github.event.repository.name }}
# IMAGE_TAG: ${{ github.sha }}
# PLATFORMS: "linux/amd64"
# run: |
# if [[ $GITHUB_REF == refs/heads/master ]]; then
# docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -t $REGISTRY/$REPOSITORY:latest --push .
# else
# docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG --push .
# fi
#build-mainnet:
# # needs: test
# if: github.ref == 'refs/heads/master'
# environment: "mainnet"
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Configure AWS credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: ${{ secrets.AWS_REGION }}
# - name: Login to Amazon ECR
# id: login-ecr
# uses: aws-actions/amazon-ecr-login@v2
# - name: Set up QEMU
# uses: docker/setup-qemu-action@v2
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3
# - name: Build, tag, and push docker image to Amazon ECR
# env:
# REGISTRY: "471112646628.dkr.ecr.us-east-1.amazonaws.com"
# REPOSITORY: ${{ github.event.repository.name }}
# IMAGE_TAG: ${{ github.sha }}
# PLATFORMS: "linux/amd64"
# run: |
# if [[ $GITHUB_REF == refs/heads/master ]]; then
# docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -t $REGISTRY/$REPOSITORY:latest --push .
# else
# docker buildx build --platform $PLATFORMS -t $REGISTRY/$REPOSITORY:$IMAGE_TAG --push .
# fi
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ docker-buildx:

.PHONY: test
test:
./scripts/runTests.sh
TESTING=true go test -v -p 1 ./...

.PHONY: ci-test
ci-test: deps test
6 changes: 3 additions & 3 deletions cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/sidecar"
"github.com/Layr-Labs/sidecar/internal/sqlite"
"github.com/Layr-Labs/sidecar/internal/sqlite/migrations"
"github.com/Layr-Labs/sidecar/internal/storage/postgresql"
sqliteBlockStore "github.com/Layr-Labs/sidecar/internal/storage/sqlite"
"go.uber.org/zap"
"log"
)
Expand Down Expand Up @@ -51,11 +51,11 @@ func main() {
log.Fatalf("Failed to migrate: %v", err)
}

contractStore := sqliteContractStore.NewSqliteContractStore(grm, l)
contractStore := sqliteContractStore.NewSqliteContractStore(grm, l, cfg)

cm := contractManager.NewContractManager(contractStore, etherscanClient, client, sdc, l)

mds, err := postgresql.NewPostgresBlockStore(grm, cfg, l)
mds := sqliteBlockStore.NewSqliteBlockStore(grm, l, cfg)
if err != nil {
log.Fatalln(err)
}
Expand Down
24 changes: 24 additions & 0 deletions internal/contractStore/contractStore.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package contractStore

import (
"embed"
"fmt"
"strings"
"time"
)

//go:embed coreContracts
var CoreContracts embed.FS

type ContractStore interface {
GetContractForAddress(address string) (*Contract, error)
FindOrCreateContract(address string, abiJson string, verified bool, bytecodeHash string, matchingContractAddress string) (*Contract, bool, error)
Expand All @@ -15,6 +19,9 @@ type ContractStore interface {
SetContractCheckedForProxy(address string) (*Contract, error)
SetContractAbi(address string, abi string, verified bool) (*Contract, error)
SetContractMatchingContractAddress(address string, matchingContractAddress string) (*Contract, error)
GetProxyContractForAddressAtBlock(address string, blockNumber uint64) (*ProxyContract, error)

InitializeCoreContracts() error
}

// Tables
Expand Down Expand Up @@ -120,3 +127,20 @@ func (c *ContractsTree) CombineAbis() string {
combinedAbi := fmt.Sprintf("[%s]", strings.Join(abisToCombine, ","))
return combinedAbi
}

type CoreContract struct {
ContractAddress string `json:"contract_address"`
ContractAbi string `json:"contract_abi"`
BytecodeHash string `json:"bytecode_hash"`
}

type CoreProxyContract struct {
ContractAddress string `json:"contract_address"`
ProxyContractAddress string `json:"proxy_contract_address"`
BlockNumber int64 `json:"block_number"`
}

type CoreContractsData struct {
CoreContracts []CoreContract `json:"core_contracts"`
ProxyContracts []CoreProxyContract `json:"proxy_contracts"`
}
126 changes: 126 additions & 0 deletions internal/contractStore/coreContracts/testnet.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ package sqliteContractStore

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/contractStore"
"github.com/Layr-Labs/sidecar/internal/sqlite"
"go.uber.org/zap"
"golang.org/x/xerrors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strings"
)

type SqliteContractStore struct {
Db *gorm.DB
Logger *zap.Logger
Db *gorm.DB
Logger *zap.Logger
globalConfig *config.Config
}

func NewSqliteContractStore(db *gorm.DB, l *zap.Logger) *SqliteContractStore {
func NewSqliteContractStore(db *gorm.DB, l *zap.Logger, cfg *config.Config) *SqliteContractStore {
cs := &SqliteContractStore{
Db: db,
Logger: l,
Db: db,
Logger: l,
globalConfig: cfg,
}
return cs
}
Expand All @@ -40,6 +45,23 @@ func (s *SqliteContractStore) GetContractForAddress(address string) (*contractSt
return contract, nil
}

func (s *SqliteContractStore) GetProxyContractForAddressAtBlock(address string, blockNumber uint64) (*contractStore.ProxyContract, error) {
address = strings.ToLower(address)

var proxyContract *contractStore.ProxyContract

result := s.Db.First(&proxyContract, "contract_address = ? and block_number = ?", address, blockNumber)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
s.Logger.Sugar().Debugf("Proxy contract not found in store '%s'", address)
return nil, nil
}
return nil, result.Error
}

return proxyContract, nil
}

func (s *SqliteContractStore) FindOrCreateContract(
address string,
abiJson string,
Expand Down Expand Up @@ -131,7 +153,7 @@ func (s *SqliteContractStore) FindOrCreateProxyContract(
ProxyContractAddress: proxyContractAddress,
}

result = tx.Model(&contractStore.ProxyContract{}).Clauses(clause.Returning{}).Create(proxyContract)
result = tx.Model(&contractStore.ProxyContract{}).Clauses(clause.Returning{}).Create(&proxyContract)
if result.Error != nil {
return nil, result.Error
}
Expand Down Expand Up @@ -240,3 +262,60 @@ func (s *SqliteContractStore) SetContractMatchingContractAddress(address string,

return contract, nil
}

func (s *SqliteContractStore) loadContractData() (*contractStore.CoreContractsData, error) {
var filename string
switch s.globalConfig.Environment {
case config.Environment_Mainnet:
filename = "mainnet.json"
case config.Environment_Testnet:
filename = "testnet.json"
case config.Environment_PreProd:
filename = "preprod.json"
default:
return nil, xerrors.Errorf("Unknown environment: %s", s.globalConfig.Environment)
}
jsonData, err := contractStore.CoreContracts.ReadFile(fmt.Sprintf("coreContracts/%s", filename))
if err != nil {
return nil, xerrors.Errorf("Failed to open core contracts file: %w", err)
}

// read entire file and marshal it into a CoreContractsData struct
data := &contractStore.CoreContractsData{}
err = json.Unmarshal(jsonData, &data)
if err != nil {
return nil, xerrors.Errorf("Failed to decode core contracts data: %w", err)
}
return data, nil
}

func (s *SqliteContractStore) InitializeCoreContracts() error {
coreContracts, err := s.loadContractData()
if err != nil {
return xerrors.Errorf("Failed to load core contracts: %w", err)
}

for _, contract := range coreContracts.CoreContracts {
_, _, err := s.FindOrCreateContract(
contract.ContractAddress,
contract.ContractAbi,
true,
contract.BytecodeHash,
"",
)
if err != nil {
return xerrors.Errorf("Failed to create core contract: %w", err)
}
}
for _, proxy := range coreContracts.ProxyContracts {
_, _, err := s.FindOrCreateProxyContract(
uint64(proxy.BlockNumber),
proxy.ContractAddress,
proxy.ProxyContractAddress,
)
if err != nil {
return xerrors.Errorf("Failed to create core proxy contract: %w", err)
}
}
return nil
}
Loading