Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/fix-docker…
Browse files Browse the repository at this point in the history
…file

# Conflicts:
#	Dockerfile
  • Loading branch information
begmaroman committed Aug 5, 2024
2 parents 8c09207 + 12d1e3f commit d2f649c
Show file tree
Hide file tree
Showing 93 changed files with 7,013 additions and 1,071 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# CONTAINER FOR BUILDING BINARY
FROM golang:1.22.4 AS build
FROM golang:1.22.5-alpine3.20 AS build

WORKDIR $GOPATH/src/github.com/0xPolygon/cdk

RUN apk update && apk add --no-cache make build-base git

# INSTALL DEPENDENCIES
COPY go.mod go.sum ./
RUN go mod download
Expand All @@ -12,12 +14,12 @@ COPY . .
RUN make build

# CONTAINER FOR RUNNING BINARY
FROM alpine:3.18.4
FROM alpine:3.20

COPY --from=build /go/src/github.com/0xPolygon/cdk/dist/cdk /app/cdk

RUN mkdir /app/data && apk update && apk add postgresql15-client

EXPOSE 8123

CMD ["/bin/sh", "-c", "/app/cdk run"]
CMD ["/bin/sh", "-c", "/app/cdk run"]
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ else
endif
GOBASE := $(shell pwd)
GOBIN := $(GOBASE)/dist
GOENVVARS := GOBIN=$(GOBIN) CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH)
GOENVVARS := GOBIN=$(GOBIN) CGO_ENABLED=1 GOOS=linux GOARCH=$(ARCH)
GOBINARY := cdk
GOCMD := $(GOBASE)/cmd

Expand Down Expand Up @@ -83,7 +83,12 @@ stop: ## Stops all services
.PHONY: test
test:
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -short -race -p 1 -covermode=atomic -coverprofile=../coverage.out -coverpkg ./... -timeout 200s ./...

.PHONY: test-seq_sender
test-seq_sender:
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -short -race -p 1 -covermode=atomic -coverprofile=../coverage.out -timeout 200s ./sequencesender/...


.PHONY: install-linter
install-linter: ## Installs the linter
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.54.2
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The following actions are required once you create a new repository from this re

### Local Development

- You can run locally against kurtosis-cdk environment using: [docs/local_debug.md](docs/local_debug.md)

### Database

### Installation
Expand Down
117 changes: 117 additions & 0 deletions aggoracle/chaingersender/evm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package chaingersender

import (
"context"
"fmt"
"math/big"
"time"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/pessimisticglobalexitroot"
cfgTypes "github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

type EthClienter interface {
ethereum.LogFilterer
ethereum.BlockNumberReader
ethereum.ChainReader
bind.ContractBackend
}

type EthTxManager interface {
Remove(ctx context.Context, id common.Hash) error
ResultsByStatus(ctx context.Context, statuses []ethtxmanager.MonitoredTxStatus) ([]ethtxmanager.MonitoredTxResult, error)
Result(ctx context.Context, id common.Hash) (ethtxmanager.MonitoredTxResult, error)
Add(ctx context.Context, to *common.Address, forcedNonce *uint64, value *big.Int, data []byte, gasOffset uint64, sidecar *types.BlobTxSidecar) (common.Hash, error)
}

type EVMChainGERSender struct {
gerContract *pessimisticglobalexitroot.Pessimisticglobalexitroot
gerAddr common.Address
sender common.Address
client EthClienter
ethTxMan EthTxManager
gasOffset uint64
waitPeriodMonitorTx time.Duration
}

type EVMConfig struct {
GlobalExitRootL2Addr common.Address `mapstructure:"GlobalExitRootL2"`
URLRPCL2 string `mapstructure:"URLRPCL2"`
ChainIDL2 uint64 `mapstructure:"ChainIDL2"`
GasOffset uint64 `mapstructure:"GasOffset"`
WaitPeriodMonitorTx cfgTypes.Duration `mapstructure:"WaitPeriodMonitorTx"`
SenderAddr common.Address `mapstructure:"SenderAddr"`
EthTxManager ethtxmanager.Config `mapstructure:"EthTxManager"`
}

func NewEVMChainGERSender(
l2GlobalExitRoot, sender common.Address,
l2Client EthClienter,
ethTxMan EthTxManager,
gasOffset uint64,
waitPeriodMonitorTx time.Duration,
) (*EVMChainGERSender, error) {
gerContract, err := pessimisticglobalexitroot.NewPessimisticglobalexitroot(l2GlobalExitRoot, l2Client)
if err != nil {
return nil, err
}
return &EVMChainGERSender{
gerContract: gerContract,
gerAddr: l2GlobalExitRoot,
sender: sender,
client: l2Client,
ethTxMan: ethTxMan,
gasOffset: gasOffset,
waitPeriodMonitorTx: waitPeriodMonitorTx,
}, nil
}

func (c *EVMChainGERSender) IsGERAlreadyInjected(ger common.Hash) (bool, error) {
timestamp, err := c.gerContract.GlobalExitRootMap(&bind.CallOpts{Pending: false}, ger)
if err != nil {
return false, fmt.Errorf("error calling gerContract.GlobalExitRootMap: %w", err)
}
return timestamp.Cmp(big.NewInt(0)) != 0, nil
}

func (c *EVMChainGERSender) UpdateGERWaitUntilMined(ctx context.Context, ger common.Hash) error {
abi, err := pessimisticglobalexitroot.PessimisticglobalexitrootMetaData.GetAbi()
if err != nil {
return err
}
data, err := abi.Pack("updateGlobalExitRoot", ger)
if err != nil {
return err
}
id, err := c.ethTxMan.Add(ctx, &c.gerAddr, nil, big.NewInt(0), data, c.gasOffset, nil)
if err != nil {
return err
}
for {
time.Sleep(c.waitPeriodMonitorTx)
log.Debugf("waiting for tx %s to be mined", id.Hex())
res, err := c.ethTxMan.Result(ctx, id)
if err != nil {
log.Error("error calling ethTxMan.Result: ", err)
}
switch res.Status {
case ethtxmanager.MonitoredTxStatusCreated,
ethtxmanager.MonitoredTxStatusSent:
continue
case ethtxmanager.MonitoredTxStatusFailed:
return fmt.Errorf("tx %s failed", res.ID)
case ethtxmanager.MonitoredTxStatusMined,
ethtxmanager.MonitoredTxStatusSafe,
ethtxmanager.MonitoredTxStatusFinalized:
return nil
default:
log.Error("unexpected tx status: ", res.Status)
}
}
}
25 changes: 25 additions & 0 deletions aggoracle/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package aggoracle

import (
"github.com/0xPolygon/cdk/aggoracle/chaingersender"
"github.com/0xPolygon/cdk/config/types"
)

type TargetChainType string

const (
EVMChain TargetChainType = "EVM"
)

var (
SupportedChainTypes = []TargetChainType{EVMChain}
)

type Config struct {
TargetChainType TargetChainType `mapstructure:"TargetChainType"`
URLRPCL1 string `mapstructure:"URLRPCL1"`
// TODO: BlockFinality doesnt work as per the jsonschema
BlockFinality string `jsonschema:"enum=latest,enum=safe, enum=pending, enum=finalized" mapstructure:"BlockFinality"`
WaitPeriodNextGER types.Duration `mapstructure:"WaitPeriodNextGER"`
EVMSender chaingersender.EVMConfig `mapstructure:"EVMSender"`
}
Loading

0 comments on commit d2f649c

Please sign in to comment.