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

init #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git
*.log
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RPC_NODE=wss://eth-goerli.g.alchemy.com/v2/ALCHEMY_API_KEY
ORACLE_ADDRESS=ORACLE_ADDRESS
CHAIN_ID=5
DB_HOST=localhost
DB_PORT=5432
DB_USER=root
DB_PASSWORD=secret
DB_NAME=oracle_monitoring_db
PRIVATE_KEY=PRIVATE_KEY
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test
vendor/
node_modules/
*.egg-info/
dist/
build/
.idea/

# Binary files
main

# Log files
*.log

# Compiled Go files
*.exe
*.test
*.prof

# Other generated files
*.pb.go

# OS-generated files
Thumbs.db
.DS_Store
ehthumbs.db
Icon?

.env
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Use the official Go image as the base image
FROM golang:1.19

# Set the working directory inside the container
WORKDIR /app

# Copy go.mod and go.sum files to the container
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy the source code to the container
COPY . .

# Build the Go app
RUN go build -o oracle-monitoring ./cmd/oracle-monitoring

# Start a new stage for the final image
FROM alpine:3.14

# Install ca-certificates
RUN apk --no-cache add ca-certificates

# Copy the binary from the previous stage
COPY --from=0 /app/oracle-monitoring /usr/local/bin/oracle-monitoring

# Run the oracle-monitoring binary
ENTRYPOINT ["oracle-monitoring"]
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# oracle-monitoring
Monitoring tool for DIA Oracles.

## Testing

There is a `cmd/oracle-monitoring/main_test.go` test suite that runs `main.go` file in background and tests the functionality by sending `setValue` transactions to the oracle.

Before running the test:
- You must have correctly configured .env file.
- The wallet that you configured in .env file must have enough balance to pay for 2x `setValue` transactions.
- The wallet must have permission to call `setValue` function on the DIA oracle smart contract.

## Running

This project uses Docker, so it's pretty easy to run it:

(make sure commands are ran from root directory of the project)

1. First you need to prepare docker image:
```
docker build -t dia-oracles-monitoring-tool .
```

2. Now you can run the docker image:
```
docker run dia-oracles-monitoring-tool
```


63 changes: 63 additions & 0 deletions cmd/oracle-monitoring/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"log"

"github.com/diadata-org/oracle-monitoring/internal/config"
"github.com/diadata-org/oracle-monitoring/internal/database"
"github.com/diadata-org/oracle-monitoring/internal/evm"
"github.com/diadata-org/oracle-monitoring/internal/models"
"github.com/ethereum/go-ethereum/core/types"
)

func GetOracleCreationTimestamp(db database.DB, evmClient *evm.EVMClient, address string) (uint64, error) {
event, err := models.GetLatestEventByOracleAddress(db, address)

if err != nil {
oracleCreationTimestamp := evmClient.CbFinder.GetContractCreationBlock(address)
return oracleCreationTimestamp, nil
}
return event.OracleCreationTimestamp, nil
}

func main() {
// Load configuration
cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}

// Initialize the database
db, err := database.New(cfg.Database)
if err != nil {
log.Fatalf("Failed to initialize the database: %v", err)
}
defer db.Close()

// Initialize EVM client
evmClient, err := evm.New(&cfg.EVM)
if err != nil {
log.Fatalf("Failed to initialize EVM client: %v", err)
}

// Run the oracle-monitoring service
err = evmClient.MonitorEvents(func(rawEvent *types.Log) {
log.Println("callback called")
parsedEvent, err := models.ParseEvent(evmClient, rawEvent)

oracleCreationTimestamp, _ := GetOracleCreationTimestamp(db, evmClient, parsedEvent.OracleAddress)
parsedEvent.OracleCreationTimestamp = oracleCreationTimestamp

if err != nil {
log.Fatalf("Failed to parse event: %v", err)
}

err = models.StoreEvent(db, parsedEvent)
if err != nil {
log.Fatalf("Failed to store event: %v", err)
}
})
if err != nil {
log.Fatalf("Failed to run oracle-monitoring service: %v", err)
}
}
74 changes: 74 additions & 0 deletions cmd/oracle-monitoring/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"context"
"testing"
"time"

"github.com/diadata-org/oracle-monitoring/internal/config"
"github.com/diadata-org/oracle-monitoring/internal/database"
"github.com/diadata-org/oracle-monitoring/internal/evm"
"github.com/diadata-org/oracle-monitoring/internal/models"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {
// Initialize the configuration and database connections
config, err := config.LoadConfig()
assert.NoError(t, err)

db, err := database.New(config.Database)
assert.NoError(t, err)
defer db.Close()

// Call main function in a goroutine
go main()

// Wait for main function to initialize and connect to the EVM client
time.Sleep(5 * time.Second)

// Delete previous DB records
_, err = db.Pool.Exec(context.Background(), "DELETE FROM events")
assert.NoError(t, err)

// Get the EVM evm and contract address
evm, err := evm.New(&config.EVM)
assert.NoError(t, err)

/*
Calldata for: setValue(string key,uint128 value,uint128 timestamp)
key = BTC/USD
value = 1000
timestamp = 1683234213
*/
data := common.Hex2Bytes("7898e0c2000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000064541da500000000000000000000000000000000000000000000000000000000000000074254432f55534400000000000000000000000000000000000000000000000000")
tx, err := evm.SendTransaction(config.EVM.OracleAddress, config.PrivateKey, data, 50000)
assert.NoError(t, err)
_, err = bind.WaitMined(context.Background(), evm.Client, tx)
assert.NoError(t, err)

// Wait until block is mined and check if record was created in database and contract creation timestamp got populated
time.Sleep(15 * time.Second)

event, err := models.GetLatestEventByOracleAddress(db, config.EVM.OracleAddress)
assert.NoError(t, err)

assert.NotEqual(t, uint64(0), event.OracleCreationTimestamp, "Oracle creation timestamp should not be 0")

// Call contract again with same calldata
tx, err = evm.SendTransaction(config.EVM.OracleAddress, config.PrivateKey, data, 50000)
assert.NoError(t, err)
_, err = bind.WaitMined(context.Background(), evm.Client, tx)
assert.NoError(t, err)

// Wait until block is mined and check if 2nd record was created
time.Sleep(15 * time.Second)
row := db.Pool.QueryRow(context.Background(), "SELECT COUNT(*) FROM events WHERE oracle_address = $1", config.EVM.OracleAddress)
var count int
err = row.Scan(&count)
assert.NoError(t, err)

assert.Equal(t, 2, count, "There should be 2 records in the database for the contract address")
}
71 changes: 71 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module github.com/diadata-org/oracle-monitoring

go 1.19

require (
github.com/ethereum/go-ethereum v1.11.6
github.com/jackc/pgx/v4 v4.18.1
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.8.1
)

require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811 // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.3.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading