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

build(deps): bump docker/build-push-action from 3 to 4 #23

Open
wants to merge 13 commits into
base: chains/nyx/mainnet
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: 1 addition & 1 deletion .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v3
uses: docker/build-push-action@v4
with:
context: .
push: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
build/
pgdata

# Configuration
*.toml
Expand Down
15 changes: 10 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
FROM golang:1.16-alpine AS builder
RUN apk update && apk add --no-cache make git
FROM golang:1.19-bullseye AS builder
WORKDIR /go/src/github.com/forbole/bdjuno
COPY . ./
RUN go mod download
RUN make build
RUN ldd build/bdjuno > /deps.txt
RUN echo $(ldd build/bdjuno | grep libwasmvm.so | awk '{ print $3 }')

FROM alpine:latest
WORKDIR /bdjuno
COPY --from=builder /go/src/github.com/forbole/bdjuno/build/bdjuno /usr/bin/bdjuno
FROM debian:bullseye
WORKDIR /root
RUN apt-get update && apt-get install ca-certificates -y
COPY --from=builder /deps.txt /root/deps.txt
COPY --from=builder /go/pkg/mod/github.com/!cosm!wasm/wasmvm@v1.0.0-beta5/api/libwasmvm.so /root
COPY --from=builder /go/src/github.com/forbole/bdjuno/build/bdjuno /root/bdjuno
ENV LD_LIBRARY_PATH=/root
CMD [ "bdjuno" ]
4 changes: 2 additions & 2 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/forbole/bdjuno/v3/database"
"github.com/forbole/bdjuno/v3/modules"

cmdxapp "github.com/comdex-official/comdex/app"
wasmapp "github.com/CosmWasm/wasmd/app"
gaiaapp "github.com/cosmos/gaia/v6/app"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ func main() {
func getBasicManagers() []module.BasicManager {
return []module.BasicManager{
gaiaapp.ModuleBasics,
cmdxapp.ModuleBasics,
wasmapp.ModuleBasics,
}
}

Expand Down
194 changes: 194 additions & 0 deletions database/nym_mixnet_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package database

import (
"encoding/json"
"fmt"
dbtypes "github.com/forbole/bdjuno/v3/database/types"
"github.com/lib/pq"
"github.com/rs/zerolog/log"
"github.com/shopspring/decimal"

cosmosTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/bdjuno/v3/types"
juno "github.com/forbole/juno/v3/types"
)

// EnsureExistsNymMixnetV1Mixnode ensures a mixnode is in the store
func (db *Db) EnsureExistsNymMixnetV1Mixnode(mixnode types.MixnodeV1) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v1_mixnode(identity_key, is_bonded, last_mixnet_status)
VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING
`
_, err := db.Sql.Exec(stmt,
mixnode.IdentityKey, mixnode.IsBonded, mixnode.LastMixnetStatus.String(),
)
if err != nil {
return fmt.Errorf("error while ensuring Nym mixnode exists: %s", err)
}

return nil
}

// SaveNymMixnetV1Mixnode allows to create or update a mixnode
func (db *Db) SaveNymMixnetV1Mixnode(mixnode types.MixnodeV1) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v1_mixnode(identity_key, is_bonded, last_mixnet_status)
VALUES ($1, $2, $3)
ON CONFLICT (identity_key) DO UPDATE
SET identity_key = excluded.identity_key,
is_bonded = excluded.is_bonded,
last_mixnet_status = excluded.last_mixnet_status
`
_, err := db.Sql.Exec(stmt,
mixnode.IdentityKey, mixnode.IsBonded, mixnode.LastMixnetStatus.String(),
)
if err != nil {
return fmt.Errorf("error while saving Nym mixnode: %s", err)
}

return nil
}

// SaveNymMixnetV1Gateway allows to store the wasm params
func (db *Db) SaveNymMixnetV1Gateway(gateway types.GatewayV1) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v1_gateway(identity_key, is_bonded)
VALUES ($1, $2)
ON CONFLICT (identity_key) DO UPDATE
SET identity_key = excluded.identity_key,
is_bonded = excluded.is_bonded,
`
_, err := db.Sql.Exec(stmt,
gateway.IdentityKey, gateway.IsBonded,
)
if err != nil {
return fmt.Errorf("error while saving Nym gateway: %s", err)
}

return nil
}

func (db *Db) GetNymMixnetV1MixnodeEvent(eventKind string, identityKey string, sender *string, height *int64, executedAt *string) ([]dbtypes.NyxNymMixnetV1MixnodeEventsRow, error) {
filter := fmt.Sprintf("WHERE event_kind = '%s' AND identity_key = '%s'", eventKind, identityKey)
order := "height ASC, executed_at ASC"
if sender != nil {
filter = fmt.Sprintf("%s AND sender = '%s'", filter, *sender)
}
if height != nil {
filter = fmt.Sprintf("%s AND height >= %d", filter, *height)
} else if executedAt != nil {
filter = fmt.Sprintf("%s AND executed_at >= '%s'", filter, *executedAt)
}
stmt := fmt.Sprintf(`SELECT * FROM nyx_nym_mixnet_v1_mixnode_events %s ORDER BY %s`, filter, order)

var rows []dbtypes.NyxNymMixnetV1MixnodeEventsRow
err := db.Sqlx.Select(&rows, stmt)
return rows, err
}

// SaveNymMixnetV1MixnodeEvent allows to store the wasm contract events
func (db *Db) SaveNymMixnetV1MixnodeEvent(eventKind string, actor string, proxy *string, identityKey string, amount *cosmosTypes.Coins, dataType string, dataJson string, executeContract types.WasmExecuteContract, tx *juno.Tx) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v1_mixnode_events
(event_kind, actor, sender, proxy, identity_key, amount, fee, contract_address, event_type, attributes, executed_at, height, hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`

var dbAmount interface{}
if amount != nil {
dbAmount = pq.Array(dbtypes.NewDbCoins(*amount))
}
fee := pq.Array(dbtypes.NewDbCoins(tx.GetFee()))

_, err := db.Sql.Exec(stmt,
eventKind, actor,
executeContract.Sender,
proxy,
identityKey,
dbAmount, fee,
executeContract.ContractAddress, dataType, dataJson,
executeContract.ExecutedAt, executeContract.Height, tx.TxHash)
if err != nil {
return fmt.Errorf("error while saving wasm execute contracts: %s", err)
}

return nil
}

// HasNymMixnetV1MixnodeRewardingEvent checks if a rewarding event has been saved
func (db *Db) HasNymMixnetV1MixnodeRewardingEvent(identityKey string, tx *juno.Tx) (bool, error) {
stmt := `
SELECT COUNT(height) FROM nyx_nym_mixnet_v1_mixnode_reward WHERE identity_key = $1 AND height = $2 AND hash = $3
`
var count int
err := db.Sql.QueryRow(stmt, identityKey, tx.Height, tx.TxHash).Scan(&count)

return count > 0, err
}

func (db *Db) GetNymMixnetV1MixnodeRewardEvent(identityKey string, heightMin uint64, heightMax *uint64) ([]dbtypes.NyxNymMixnetV1MixnodeRewardRow, error) {
stmt := fmt.Sprintf(`SELECT * FROM nyx_nym_mixnet_v1_mixnode_reward WHERE height >= %d AND identity_key = '%s'`, heightMin, identityKey)
if heightMax != nil && *heightMax > 0 {
stmt = fmt.Sprintf("%s AND height <= %d", stmt, *heightMax)
}
stmt = fmt.Sprintf("%s ORDER BY height ASC", stmt)
var rows []dbtypes.NyxNymMixnetV1MixnodeRewardRow
err := db.Sqlx.Select(&rows, stmt)
log.Info().Int("count", len(rows)).Err(err).Msg(stmt)
return rows, err
}

// SaveNymMixnetV1MixnodeRewardingEvent allows to store the mixnode rewarding events
func (db *Db) SaveNymMixnetV1MixnodeRewardingEvent(identityKey string, totalNodeReward cosmosTypes.Coins, totalDelegations cosmosTypes.Coins, operatorReward cosmosTypes.Coins, unitDelegatorReward decimal.Decimal, apy float64, stakingSupply cosmosTypes.Coins, profitMarginPercentage int, event cosmosTypes.StringEvent, executeContract types.WasmExecuteContract, tx *juno.Tx) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v1_mixnode_reward
(sender, identity_key, total_node_reward, total_delegations, operator_reward, unit_delegator_reward, apy, staking_supply, profit_margin_percentage, contract_address, event_type, attributes, executed_at, height, hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
ON CONFLICT DO NOTHING`

var attr = make(map[string]interface{}) // could be `map[string]string` however leaving to handle objects as values
for _, entry := range event.Attributes {
attr[entry.Key] = entry.Value
}
bytes, _ := json.Marshal(attr)

dbTotalNodeReward := pq.Array(dbtypes.NewDbCoins(totalNodeReward))
dbTotalDelegations := pq.Array(dbtypes.NewDbCoins(totalDelegations))
dbOperatorReward := pq.Array(dbtypes.NewDbCoins(operatorReward))
dbStakingSupply := pq.Array(dbtypes.NewDbCoins(stakingSupply))

_, err := db.Sql.Exec(stmt,
executeContract.Sender,
identityKey,
dbTotalNodeReward,
dbTotalDelegations,
dbOperatorReward,
unitDelegatorReward.IntPart(),
apy,
dbStakingSupply,
profitMarginPercentage,
executeContract.ContractAddress, event.Type, string(bytes),
executeContract.ExecutedAt, executeContract.Height, tx.TxHash)
if err != nil {
return fmt.Errorf("error while saving wasm execute contracts: %s", err)
}

return nil
}

// SaveNymMixnetV1MixnodeStatus allows to store when the mixnet rewarded set changes
func (db *Db) SaveNymMixnetV1MixnodeStatus(identityKey string, status string, routingScore int, executeContract types.WasmExecuteContract, tx *juno.Tx) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v1_mixnode_status
(mixnet_status, routing_score, identity_key, executed_at, height, hash )
VALUES ($1, $2, $3, $4, $5, $6)`

_, err := db.Sql.Exec(stmt,
status, routingScore, identityKey,
executeContract.ExecutedAt, executeContract.Height, tx.TxHash)
if err != nil {
return fmt.Errorf("error while saving wasm execute contracts: %s", err)
}

return nil
}
113 changes: 113 additions & 0 deletions database/nym_mixnet_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package database

import (
"encoding/json"
"fmt"
cosmosTypes "github.com/cosmos/cosmos-sdk/types"
dbtypes "github.com/forbole/bdjuno/v3/database/types"
"github.com/forbole/bdjuno/v3/types"
juno "github.com/forbole/juno/v3/types"
"github.com/lib/pq"
"github.com/shopspring/decimal"
)

// EnsureExistsNymMixnetV2Mixnode ensures a mixnode is in the store
func (db *Db) EnsureExistsNymMixnetV2Mixnode(mixnode types.MixnodeV2) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v2_mixnode(mix_id, identity_key, is_bonded, last_mixnet_status)
VALUES ($1, $2, $3, $4)
ON CONFLICT DO NOTHING
`
_, err := db.Sql.Exec(stmt,
mixnode.MixId, mixnode.IdentityKey, mixnode.IsBonded, mixnode.LastMixnetStatus.String(),
)
if err != nil {
return fmt.Errorf("error while ensuring Nym mixnode exists: %s", err)
}

return nil
}

// SaveNymMixnetV2Mixnode allows to create or update a mixnode
func (db *Db) SaveNymMixnetV2Mixnode(mixnode types.MixnodeV2) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v2_mixnode(mix_id, identity_key, is_bonded, last_mixnet_status)
VALUES ($1, $2, $3, $4)
ON CONFLICT (identity_key) DO UPDATE
SET mix_id = excluded.mix_id,
identity_key = excluded.identity_key,
is_bonded = excluded.is_bonded,
last_mixnet_status = excluded.last_mixnet_status
`
_, err := db.Sql.Exec(stmt,
mixnode.MixId, mixnode.IdentityKey, mixnode.IsBonded, mixnode.LastMixnetStatus.String(),
)
if err != nil {
return fmt.Errorf("error while saving Nym mixnode: %s", err)
}

return nil
}

// HasNymMixnetV2MixnodeRewardingEvent checks if a rewarding event has been saved
func (db *Db) HasNymMixnetV2MixnodeRewardingEvent(mixId uint32, tx *juno.Tx) (bool, error) {
stmt := `
SELECT COUNT(height) FROM nyx_nym_mixnet_v2_mixnode_reward WHERE mix_id = $1 AND height = $2 AND hash = $3
`
var count int
err := db.Sql.QueryRow(stmt, mixId, tx.Height, tx.TxHash).Scan(&count)

return count > 0, err
}

// SaveNymMixnetV2MixnodeRewardingEvent allows to store the mixnode rewarding events
func (db *Db) SaveNymMixnetV2MixnodeRewardingEvent(mixId uint32, operatorReward cosmosTypes.Coins, delegatesReward cosmosTypes.Coins, priorDelegates cosmosTypes.Coins, priorUnitDelegation decimal.Decimal, apy float64, event cosmosTypes.StringEvent, executeContract types.WasmExecuteContract, tx *juno.Tx) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v2_mixnode_reward
(sender, mix_id, operator_reward, delegates_reward, prior_delegates, prior_unit_delegation, apy, contract_address, event_type, attributes, executed_at, height, hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT DO NOTHING`

var attr = make(map[string]interface{}) // could be `map[string]string` however leaving to handle objects as values
for _, entry := range event.Attributes {
attr[entry.Key] = entry.Value
}
bytes, _ := json.Marshal(attr)

dbOperatorReward := pq.Array(dbtypes.NewDbCoins(operatorReward))
dbDelegatesReward := pq.Array(dbtypes.NewDbCoins(delegatesReward))
dbPriorDelegates := pq.Array(dbtypes.NewDbCoins(priorDelegates))

_, err := db.Sql.Exec(stmt,
executeContract.Sender,
mixId,
dbOperatorReward,
dbDelegatesReward,
dbPriorDelegates,
priorUnitDelegation.IntPart(),
apy,
executeContract.ContractAddress, event.Type, string(bytes),
executeContract.ExecutedAt, executeContract.Height, tx.TxHash)
if err != nil {
return fmt.Errorf("error while saving wasm execute contracts: %s", err)
}

return nil
}

// SaveNymMixnetV2MixnodeStatus allows to store when the mixnet rewarded set changes
func (db *Db) SaveNymMixnetV2MixnodeStatus(mixId uint32, status string, routingScore int64, executeContract types.WasmExecuteContract, tx *juno.Tx) error {
stmt := `
INSERT INTO nyx_nym_mixnet_v2_mixnode_status
(mixnet_status, routing_score, mix_id, executed_at, height, hash )
VALUES ($1, $2, $3, $4, $5, $6)`

_, err := db.Sql.Exec(stmt,
status, routingScore, mixId,
executeContract.ExecutedAt, executeContract.Height, tx.TxHash)
if err != nil {
return fmt.Errorf("error while saving wasm execute contracts: %s", err)
}

return nil
}
3 changes: 2 additions & 1 deletion database/schema/12-wasm.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ CREATE TABLE wasm_execute_contract
funds COIN[] NOT NULL DEFAULT '{}',
data TEXT NULL,
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL
height BIGINT NOT NULL,
hash TEXT NOT NULL
);
CREATE INDEX execute_contract_height_index ON wasm_execute_contract (height);

13 changes: 13 additions & 0 deletions database/schema/13-wasm-events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE wasm_execute_contract_event
(
sender TEXT NOT NULL,
contract_address TEXT NOT NULL REFERENCES wasm_contract (contract_address),
event_type TEXT NULL,
attributes JSONB NOT NULL DEFAULT '{}'::JSONB,
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL
);
CREATE INDEX wasm_execute_contract_event_height_index ON wasm_execute_contract_event (height);
CREATE INDEX wasm_execute_contract_event_hash_index ON wasm_execute_contract_event (hash);
CREATE INDEX wasm_execute_contract_event_event_type_index ON wasm_execute_contract_event (event_type);
Loading