Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: switch to postgres in points service #602

Open
wants to merge 9 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
104 changes: 104 additions & 0 deletions infrastructure/nomad/playbooks/templates/jobs/points-service.nomad.j2
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,98 @@ job "{{ job.name }}" {
}
{% endfor %}

task "db" {
driver = "exec"

lifecycle {
hook = "prestart"
sidecar = true
}

{% if profile == 'testnet' or profile == 'mainnet' %}
resources {
cores = 4
memory = 8192
}
{% endif %}

template {
data = <<-EOH
POSTGRES_VERSION="15"
POSTGRES_DB="mev_oracle"
POSTGRES_USERNAME="mev_oracle"
POSTGRES_PASSWORD="{{ lookup('password', '/dev/null', length=64) }}"
POSTGRES_DATA="/local/pgdata-{% raw %}{{ env "NOMAD_ALLOC_INDEX" }}{% endraw %}"
POSTGRES_PORT="54321"
EOH
destination = "alloc/data/postgres.env"
env = true
}

template {
data = <<-EOH
#!/usr/bin/env bash

{% raw %}
{{- range nomadService "datadog-agent-logs-collector" }}
{{ if contains "tcp" .Tags }}
exec > >(nc {{ .Address }} {{ .Port }}) 2>&1
{{ end }}
{{- end }}

if [ -d "${POSTGRES_DATA}" ]; then
echo "Initialized and configured database found."
cp "${POSTGRES_DATA}/.env" /alloc/data/postgres.env
source "${POSTGRES_DATA}/.env"
postgres -D ${POSTGRES_DATA}
exit $?
fi

export PATH="/usr/lib/postgresql/${POSTGRES_VERSION}/bin:${PATH}"
mkdir -p /var/run/postgresql > /dev/null 2>&1
pg_ctl initdb --silent --pgdata="${POSTGRES_DATA}"
if [ $? -ne 0 ]; then
echo "Failed to initialize PostgreSQL."
exit 1
fi
cp /alloc/data/postgres.env "${POSTGRES_DATA}/.env"

pg_ctl start --pgdata="${POSTGRES_DATA}" --silent --wait --timeout=300 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Failed to start PostgreSQL."
exit 1
fi

createuser --superuser postgres > /dev/null 2>&1
createuser --username=postgres --createdb "${POSTGRES_USERNAME}"
createdb --username="${POSTGRES_USERNAME}" "${POSTGRES_DB}"
psql --quiet \
--username="${POSTGRES_USERNAME}" \
--dbname="${POSTGRES_DB}" \
--command="ALTER USER ${POSTGRES_USERNAME} WITH PASSWORD '${POSTGRES_PASSWORD}'; \
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_USERNAME};"
echo "Database initialized and configured successfully."

pg_ctl stop --pgdata="${POSTGRES_DATA}" --silent --wait --timeout=300 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Failed to stop PostgreSQL."
exit 1
fi

exec postgres -D "${POSTGRES_DATA}" -p "${POSTGRES_PORT}"
{% endraw %}
EOH
destination = "local/run.sh"
change_mode = "noop"
perms = "0755"
}

config {
command = "bash"
args = ["-c", "exec local/run.sh"]
}
}

task "points-service" {
driver = "exec"

Expand Down Expand Up @@ -80,6 +172,18 @@ job "{{ job.name }}" {
{{ end }}
{{- end }}

source alloc/data/postgres.env
export POINTS_PG_HOST="localhost"
export POINTS_PG_PORT="${POSTGRES_PORT}"
export POINTS_PG_USER="${POSTGRES_USERNAME}"
export POINTS_PG_PASSWORD="${POSTGRES_PASSWORD}"
export POINTS_PG_DBNAME="${POSTGRES_DB}"

if ! timeout 5m bash -c 'until pg_isready -h ${POINTS_PG_HOST} -p ${POINTS_PG_PORT} -U ${POINTS_PG_USER} -d ${POINTS_PG_DBNAME}; do sleep 2; done'; then
echo "Waiting for PostgreSQL to start..."
sleep 3
fi

chmod +x local/points-service
exec local/points-service
{% endraw %}
Expand Down
4 changes: 3 additions & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/ethereum/go-ethereum v1.14.11
github.com/gorilla/mux v1.8.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/mattn/go-sqlite3 v1.14.24
github.com/lib/pq v1.10.9
github.com/primev/mev-commit/contracts-abi v0.0.1
github.com/primev/mev-commit/p2p v0.0.1
github.com/primev/mev-commit/x v0.0.1
Expand All @@ -26,6 +26,7 @@ replace github.com/primev/mev-commit/contracts-abi => ../contracts-abi

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20240221180331-f05a6f4403ce.1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -67,5 +68,6 @@ require (
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
9 changes: 7 additions & 2 deletions tools/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20240221180331-f05a6f4403ce.1 h1:AmmAwHbvaeOIxDKG2+aTn5C36HjmFIMkrdTp49rp80Q=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20240221180331-f05a6f4403ce.1/go.mod h1:tiTMKD8j6Pd/D2WzREoweufjzaJKHZg35f/VGcZ2v3I=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
Expand Down Expand Up @@ -114,14 +116,14 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leanovate/gopter v0.2.11 h1:vRjThO1EKPb/1NsDXuDrzldR28RLkBflWYcU9CvzWu4=
github.com/leanovate/gopter v0.2.11/go.mod h1:aK3tzZP/C+p1m3SPRE4SYZFGP7jjkuSI4f7Xvpt0S9c=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A=
Expand Down Expand Up @@ -200,6 +202,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
7 changes: 2 additions & 5 deletions tools/points-service/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ dist: /tmp/dist/points-service

builds:
- env:
- CGO_ENABLED=1
- CC=x86_64-linux-musl-gcc
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm64
dir: ./tools/points-service
binary: "{{ .ProjectName }}"
flags:
- -v
- -trimpath
- -tags=sqlite3
ldflags:
- -extldflags "-static"

archives:
- format: tar.gz
Expand Down
28 changes: 15 additions & 13 deletions tools/points-service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (p *PointsAPI) RecomputePointsForAddress(w http.ResponseWriter, r *http.Req
return
}

// Only "operator" returns actual data; otherwise, we just return 0
if receiverType != "operator" {
resp := map[string]interface{}{
"address": receiverAddr,
Expand Down Expand Up @@ -124,30 +125,29 @@ func (p *PointsAPI) RecomputePointsForAddress(w http.ResponseWriter, r *http.Req
}

func (p *PointsAPI) calculatePointsForSymbioticOperator(receiverAddr string, blockNum uint64) (int64, error) {

// Get count of unique pubkeys for this operator
// Count of unique pubkeys
var uniquePubkeys int
err := p.db.QueryRow(`
SELECT COUNT(DISTINCT pubkey)
FROM validator_records
WHERE registry_type = 'symbiotic'
AND adder = ?
AND opted_in_block <= ?
AND adder = $1
AND opted_in_block <= $2
`, receiverAddr, blockNum).Scan(&uniquePubkeys)
if err != nil {
p.logger.Error("failed to get unique pubkey count", "error", err)
return 0, err
}

// Sign-up Bonus of 1000
// Sign-up Bonus of 1000 each
totalPoints := int64(uniquePubkeys) * 1000

rows, err := p.db.Query(`
SELECT vault, registry_type, opted_in_block, opted_out_block
FROM validator_records
WHERE registry_type = 'symbiotic'
AND (pubkey = ? OR adder = ?)
AND opted_in_block <= ?
AND (pubkey = $1 OR adder = $2)
AND opted_in_block <= $3
`, receiverAddr, receiverAddr, blockNum)
if err != nil {
return 0, err
Expand Down Expand Up @@ -191,6 +191,7 @@ func (p *PointsAPI) calculatePointsForSymbioticOperator(receiverAddr string, blo
}
return totalPoints, nil
}

func (p *PointsAPI) GetTotalPointsStats(w http.ResponseWriter, r *http.Request) {
blockNumStr := r.URL.Query().Get("block_number")
if blockNumStr == "" {
Expand All @@ -207,7 +208,7 @@ func (p *PointsAPI) GetTotalPointsStats(w http.ResponseWriter, r *http.Request)
SELECT adder, opted_in_block, opted_out_block
FROM validator_records
WHERE registry_type = 'symbiotic'
AND opted_in_block <= ?
AND opted_in_block <= $1
`, blockNum)
if err != nil {
http.Error(w, "DB query failed", http.StatusInternalServerError)
Expand Down Expand Up @@ -303,10 +304,10 @@ func (p *PointsAPI) GetAllPoints(w http.ResponseWriter, r *http.Request) {
SELECT DISTINCT adder, vault
FROM validator_records
WHERE registry_type = 'symbiotic'
AND opted_in_block <= ?
AND (opted_out_block IS NULL OR opted_out_block > ?)
AND opted_in_block <= $1
AND (opted_out_block IS NULL OR opted_out_block > $2)
ORDER BY adder
LIMIT ? OFFSET ?
LIMIT $3 OFFSET $4
`
rows, err := p.db.Query(query, blockNum, blockNum, limit, offset)
if err != nil {
Expand Down Expand Up @@ -373,7 +374,7 @@ func (p *PointsAPI) GetAnyPointsForAddress(w http.ResponseWriter, r *http.Reques
),
0)
FROM validator_records
WHERE adder = ?
WHERE adder = $1
`
var totalPoints int64
if err := p.db.QueryRow(q, adder).Scan(&totalPoints); err != nil {
Expand All @@ -387,7 +388,8 @@ func (p *PointsAPI) GetAnyPointsForAddress(w http.ResponseWriter, r *http.Reques
const pubkeyQuery = `
SELECT COUNT(DISTINCT pubkey) * 1000
FROM validator_records
WHERE adder = ?`
WHERE adder = $1
`
if err := p.db.QueryRow(pubkeyQuery, adder).Scan(&pubkeyBonus); err != nil {
http.Error(w, "Database query failed", http.StatusInternalServerError)
p.logger.Error("GetAnyPointsForAddress pubkey bonus query error", "error", err)
Expand Down
Loading
Loading