Skip to content

Commit

Permalink
Use COMDEX chain as base swapping main app for wasm app
Browse files Browse the repository at this point in the history
Change docker image bases
Change golang version
Process Nyx chain cosmwasm transactions
Add Nym specific tables
Add module to hand-off message processing to an external API (Typescript + Express + postgres)
  • Loading branch information
mmsinclair committed Apr 12, 2023
1 parent 28896c4 commit 77753d2
Show file tree
Hide file tree
Showing 105 changed files with 2,703 additions and 1,552 deletions.
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/[email protected]/api/libwasmvm.so /root
COPY --from=builder /go/src/github.com/forbole/bdjuno/build/bdjuno /root/bdjuno
ENV LD_LIBRARY_PATH=/root
CMD [ "bdjuno" ]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ This will:
2. Run all the tests using that database as support.


## Setup

Remember to run this if you have an empty database:

```
cd hasura
hasura metadata apply --endpoint http://localhost:8080 --admin-secret myadminsecretkey
```
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
38 changes: 38 additions & 0 deletions database/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@ func (db *Db) getBlockHeightTime(pastTime time.Time) (dbtypes.BlockRow, error) {
return val[0], nil
}

// GetFirstBlockTime retrieves the first block stored
func (db *Db) GetFirstBlockTime() (dbtypes.BlockRow, error) {
stmt := `SELECT * FROM block ORDER BY block.timestamp ASC LIMIT 1;`

var val []dbtypes.BlockRow
if err := db.Sqlx.Select(&val, stmt); err != nil {
return dbtypes.BlockRow{}, err
}

if len(val) == 0 {
return dbtypes.BlockRow{}, fmt.Errorf("cannot get block time, no blocks saved")
}

return val[0], nil
}

// GetLastBlockTime retrieves the first block stored
func (db *Db) GetLastBlockTime() (dbtypes.BlockRow, error) {
stmt := `SELECT * FROM block ORDER BY block.timestamp DESC LIMIT 1;`

var val []dbtypes.BlockRow
if err := db.Sqlx.Select(&val, stmt); err != nil {
return dbtypes.BlockRow{}, err
}

if len(val) == 0 {
return dbtypes.BlockRow{}, fmt.Errorf("cannot get block time, no blocks saved")
}

return val[0], nil
}

// GetBlockHeightTime return block height and time that a block proposals
// at the input date
func (db *Db) GetBlockHeightTime(now time.Time) (dbtypes.BlockRow, error) {
return db.getBlockHeightTime(now)
}

// GetBlockHeightTimeMinuteAgo return block height and time that a block proposals
// about a minute ago from input date
func (db *Db) GetBlockHeightTimeMinuteAgo(now time.Time) (dbtypes.BlockRow, error) {
Expand Down
48 changes: 48 additions & 0 deletions database/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package database

import (
"fmt"
dbtypes "github.com/forbole/bdjuno/v3/database/types"
)

// GetMessages returns all messages between block heights
func (db *Db) GetMessages(address string, startHeight int64, endHeight int64, offset uint64, limit uint64) ([]dbtypes.MessageRow, error) {
query := fmt.Sprintf(`SELECT
transaction_hash,
index,
type,
value,
involved_accounts_addresses,
height
FROM message
WHERE involved_accounts_addresses @> '{%s}' AND height >= %d AND height <= %d
ORDER BY height ASC, type ASC, index ASC
OFFSET %d LIMIT %d`, address, startHeight, endHeight, offset, limit)

var dbRows []dbtypes.MessageRow
err := db.Sqlx.Select(&dbRows, query)
if err != nil {
return nil, err
}

return dbRows, nil
}

// GetMessagesCount returns count of GetMessages
func (db *Db) GetMessagesCount(address string, startHeight int64, endHeight int64) (int, error) {

stmt, err := db.Sqlx.Prepare(fmt.Sprintf(`SELECT count(height)
FROM message
WHERE involved_accounts_addresses @> '{%s}' AND height >= %d AND height <= %d`, address, startHeight, endHeight))
if err != nil {
return 0, err
}

var count int
err = stmt.QueryRow().Scan(&count)

if err != nil {
return 0, err
}
return count, nil
}
8 changes: 7 additions & 1 deletion database/schema/12-wasm.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,22 @@ CREATE TABLE wasm_contract
height BIGINT NOT NULL
);
CREATE INDEX wasm_contract_height_index ON wasm_contract (height);
CREATE INDEX wasm_contract_creator_index ON wasm_contract (creator);
CREATE INDEX wasm_contract_label_index ON wasm_contract (label);

CREATE TABLE wasm_execute_contract
(
sender TEXT NOT NULL,
contract_address TEXT NOT NULL REFERENCES wasm_contract (contract_address),
raw_contract_message JSONB NOT NULL DEFAULT '{}'::JSONB,
funds COIN[] NOT NULL DEFAULT '{}',
message_type TEXT NULL,
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);
CREATE INDEX execute_contract_executed_at_index ON wasm_execute_contract (executed_at);
CREATE INDEX execute_contract_message_type_index ON wasm_execute_contract (message_type);

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_types
(
contract_address TEXT NOT NULL REFERENCES wasm_contract (contract_address),
event_type TEXT NOT NULL,

first_seen_height BIGINT NOT NULL REFERENCES block (height),
first_seen_hash TEXT NOT NULL,

last_seen_height BIGINT NOT NULL REFERENCES block (height),
last_seen_hash TEXT NOT NULL,
UNIQUE (contract_address, event_type)
);
CREATE INDEX wasm_execute_contract_event_types_index ON wasm_execute_contract_event_types (contract_address, event_type);
82 changes: 82 additions & 0 deletions database/schema/14-nyx-nym-mixnet-v1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
CREATE TABLE nyx_nym_mixnet_v1_mixnode
(
identity_key TEXT UNIQUE PRIMARY KEY,
last_is_bonded_status BOOLEAN NULL,

-- values: in_active_set, in_standby_set, inactive
last_mixnet_status TEXT NULL
);
CREATE INDEX nyx_nym_mixnet_v1_mixnode_status_index ON nyx_nym_mixnet_v1_mixnode (last_mixnet_status);

CREATE TABLE nyx_nym_mixnet_v1_mixnode_status
(
-- values: in_active_set, in_standby_set, inactive
mixnet_status TEXT NOT NULL,

-- in the range 0 to 100
routing_score INTEGER NOT NULL,

identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_mixnode (identity_key),
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL
);
CREATE INDEX nyx_nm_v1_m_status_height_index ON nyx_nym_mixnet_v1_mixnode_status (height);
CREATE INDEX nyx_nm_v1_m_identity_key_index ON nyx_nym_mixnet_v1_mixnode_status (identity_key);

CREATE TABLE nyx_nym_mixnet_v1_mixnode_events
(
-- values: bond, unbond, delegate, undelegate, claim
event_kind TEXT NOT NULL,
-- values: mixnode_operator, mixnode_delegator, mixnet_rewarding, mixnet_monitoring
actor TEXT NOT NULL,
sender TEXT NOT NULL,
proxy TEXT NULL,
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_mixnode (identity_key),
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL,
message_index BIGINT NOT NULL
);
CREATE INDEX nyx_nm_v1_me_height_index ON nyx_nym_mixnet_v1_mixnode_events (identity_key, height);
CREATE INDEX nyx_nm_v1_me_identity_key_executed_at_index ON nyx_nym_mixnet_v1_mixnode_events (identity_key, executed_at);

CREATE TABLE nyx_nym_mixnet_v1_mixnode_reward
(
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_mixnode (identity_key),
total_node_reward COIN[] NOT NULL DEFAULT '{}',
total_delegations COIN[] NOT NULL DEFAULT '{}',
operator_reward COIN[] NOT NULL DEFAULT '{}',
unit_delegator_reward BIGINT NOT NULL,
apy FLOAT NOT NULL,
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL,
message_index BIGINT NOT NULL,
UNIQUE (identity_key, height, hash, message_index)
);
CREATE INDEX nyx_nm_v1_mr_height_index ON nyx_nym_mixnet_v1_mixnode_reward (height);
CREATE INDEX nyx_nm_v1_mr_identity_key_index ON nyx_nym_mixnet_v1_mixnode_reward (identity_key);

CREATE TABLE nyx_nym_mixnet_v1_gateway
(
identity_key TEXT UNIQUE PRIMARY KEY,
last_is_bonded_status BOOLEAN NULL
);

CREATE TABLE nyx_nym_mixnet_v1_gateway_events
(
-- values: bond, unbond
event_kind TEXT NOT NULL,
sender TEXT NOT NULL,
proxy TEXT NULL,
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_gateway (identity_key),
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL,
message_index BIGINT NOT NULL
);
CREATE INDEX nyx_nm_v1_ge_height_index ON nyx_nym_mixnet_v1_gateway_events (identity_key, height);
CREATE INDEX nyx_nm_v1_ge_identity_key_executed_at_index ON nyx_nym_mixnet_v1_gateway_events (identity_key, executed_at);


67 changes: 67 additions & 0 deletions database/schema/15-nyx-nym-mixnet-v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
CREATE TABLE nyx_nym_mixnet_v2_mixnode
(
mix_id BIGINT UNIQUE PRIMARY KEY,

-- the identity key may be null, because a tx/event with only the mix_id might be processed out of order
identity_key TEXT NULL,

last_is_bonded_status BOOLEAN NULL,

-- values: in_active_set, in_standby_set, inactive
last_mixnet_status TEXT NULL
);
CREATE INDEX nyx_nm_v2_m_status_index ON nyx_nym_mixnet_v2_mixnode (last_mixnet_status);
CREATE INDEX nyx_nm_v2_m_identity_key_index ON nyx_nym_mixnet_v2_mixnode (identity_key);

CREATE TABLE nyx_nym_mixnet_v2_mixnode_status
(
-- values: in_active_set, in_standby_set, inactive
mixnet_status TEXT NOT NULL,

-- in the range 0 to 1
routing_score DECIMAL NOT NULL,

mix_id BIGINT NOT NULL REFERENCES nyx_nym_mixnet_v2_mixnode (mix_id),
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL
);
CREATE INDEX nyx_nm_v2_ms_status_height_index ON nyx_nym_mixnet_v2_mixnode_status (height);
CREATE INDEX nyx_nm_v2_ms_executed_at_index ON nyx_nym_mixnet_v2_mixnode_status (executed_at);

CREATE TABLE nyx_nym_mixnet_v2_events
(
-- values: bond, unbond, delegate, undelegate, claim
event_kind TEXT NOT NULL,
-- values: mixnode_operator, mixnode_delegator, mixnet_rewarding, mixnet_monitoring, gateway_operator
actor TEXT NOT NULL,
sender TEXT NOT NULL,
proxy TEXT NULL,
mix_id BIGINT NULL REFERENCES nyx_nym_mixnet_v2_mixnode (mix_id),
identity_key TEXT NOT NULL,
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL,
message_index BIGINT NOT NULL
);
CREATE INDEX nyx_nm_v2_e_height_index ON nyx_nym_mixnet_v2_events (mix_id, height);
CREATE INDEX nyx_nm_v2_e_executed_at_index ON nyx_nym_mixnet_v2_events (mix_id, executed_at);
CREATE INDEX nyx_nm_v2_e_identity_key_executed_at_index ON nyx_nym_mixnet_v2_events (identity_key, executed_at);

CREATE TABLE nyx_nym_mixnet_v2_mixnode_reward
(
mix_id BIGINT NOT NULL REFERENCES nyx_nym_mixnet_v2_mixnode (mix_id),
operator_reward COIN[] NOT NULL DEFAULT '{}',
delegates_reward COIN[] NOT NULL DEFAULT '{}',
prior_delegates COIN[] NOT NULL DEFAULT '{}',
prior_unit_reward BIGINT NOT NULL,
apy FLOAT NOT NULL,
epoch BIGINT NOT NULL,
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
hash TEXT NOT NULL,
message_index BIGINT NOT NULL,
UNIQUE (mix_id, height, hash, message_index)
);
CREATE INDEX nyx_nm_v2_mr_height_index ON nyx_nym_mixnet_v2_mixnode_reward (mix_id, height);
CREATE INDEX nyx_nm_v2_mr_executed_at_index ON nyx_nym_mixnet_v2_mixnode_reward (mix_id, executed_at);
54 changes: 54 additions & 0 deletions database/types/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package types

import (
"encoding/json"
"github.com/lib/pq"
)

type MessageRow struct {
TxHash string `db:"transaction_hash"`
Index int `db:"index"`
Type string `db:"type"`
Value string `db:"value"`
Aliases pq.StringArray `db:"involved_accounts_addresses"`
Height int64 `db:"height"`
}

type MessageWithValueRow struct {
TxHash string `json:"transaction_hash"`
Index int `json:"index"`
Type string `json:"type"`
Value interface{} `json:"value"`
Funds interface{} `json:"funds"`
Aliases pq.StringArray `json:"involved_accounts_addresses"`
Height int64 `json:"height"`
}

func NewMessageWithValueRow(row MessageRow) (*MessageWithValueRow, error) {
var value map[string]interface{}
err := json.Unmarshal([]byte(row.Value), &value)
if err != nil {
return nil, err
}

var funds interface{}

if value["funds"] != nil {
funds = value["funds"]
}
if value["amount"] != nil {
funds = value["amount"]
}

var rowWithValue = MessageWithValueRow{
TxHash: row.TxHash,
Index: row.Index,
Type: row.Type,
Value: value,
Funds: funds,
Aliases: row.Aliases,
Height: row.Height,
}

return &rowWithValue, nil
}
Loading

0 comments on commit 77753d2

Please sign in to comment.