Skip to content

Commit 77753d2

Browse files
committed
Use COMDEX chain as base swapping main app for wasm app
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)
1 parent 28896c4 commit 77753d2

File tree

105 files changed

+2703
-1552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2703
-1552
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
build/
3+
pgdata
34

45
# Configuration
56
*.toml

Dockerfile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
FROM golang:1.16-alpine AS builder
2-
RUN apk update && apk add --no-cache make git
1+
FROM golang:1.19-bullseye AS builder
32
WORKDIR /go/src/github.com/forbole/bdjuno
43
COPY . ./
54
RUN go mod download
65
RUN make build
6+
RUN ldd build/bdjuno > /deps.txt
7+
RUN echo $(ldd build/bdjuno | grep libwasmvm.so | awk '{ print $3 }')
78

8-
FROM alpine:latest
9-
WORKDIR /bdjuno
10-
COPY --from=builder /go/src/github.com/forbole/bdjuno/build/bdjuno /usr/bin/bdjuno
9+
FROM debian:bullseye
10+
WORKDIR /root
11+
RUN apt-get update && apt-get install ca-certificates -y
12+
COPY --from=builder /deps.txt /root/deps.txt
13+
COPY --from=builder /go/pkg/mod/github.com/!cosm!wasm/[email protected]/api/libwasmvm.so /root
14+
COPY --from=builder /go/src/github.com/forbole/bdjuno/build/bdjuno /root/bdjuno
15+
ENV LD_LIBRARY_PATH=/root
1116
CMD [ "bdjuno" ]

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ This will:
3131
2. Run all the tests using that database as support.
3232

3333

34+
## Setup
35+
36+
Remember to run this if you have an empty database:
37+
38+
```
39+
cd hasura
40+
hasura metadata apply --endpoint http://localhost:8080 --admin-secret myadminsecretkey
41+
```

cmd/bdjuno/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/forbole/bdjuno/v3/database"
1717
"github.com/forbole/bdjuno/v3/modules"
1818

19-
cmdxapp "github.com/comdex-official/comdex/app"
19+
wasmapp "github.com/CosmWasm/wasmd/app"
2020
gaiaapp "github.com/cosmos/gaia/v6/app"
2121
)
2222

@@ -57,7 +57,7 @@ func main() {
5757
func getBasicManagers() []module.BasicManager {
5858
return []module.BasicManager{
5959
gaiaapp.ModuleBasics,
60-
cmdxapp.ModuleBasics,
60+
wasmapp.ModuleBasics,
6161
}
6262
}
6363

database/consensus.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,44 @@ func (db *Db) getBlockHeightTime(pastTime time.Time) (dbtypes.BlockRow, error) {
5555
return val[0], nil
5656
}
5757

58+
// GetFirstBlockTime retrieves the first block stored
59+
func (db *Db) GetFirstBlockTime() (dbtypes.BlockRow, error) {
60+
stmt := `SELECT * FROM block ORDER BY block.timestamp ASC LIMIT 1;`
61+
62+
var val []dbtypes.BlockRow
63+
if err := db.Sqlx.Select(&val, stmt); err != nil {
64+
return dbtypes.BlockRow{}, err
65+
}
66+
67+
if len(val) == 0 {
68+
return dbtypes.BlockRow{}, fmt.Errorf("cannot get block time, no blocks saved")
69+
}
70+
71+
return val[0], nil
72+
}
73+
74+
// GetLastBlockTime retrieves the first block stored
75+
func (db *Db) GetLastBlockTime() (dbtypes.BlockRow, error) {
76+
stmt := `SELECT * FROM block ORDER BY block.timestamp DESC LIMIT 1;`
77+
78+
var val []dbtypes.BlockRow
79+
if err := db.Sqlx.Select(&val, stmt); err != nil {
80+
return dbtypes.BlockRow{}, err
81+
}
82+
83+
if len(val) == 0 {
84+
return dbtypes.BlockRow{}, fmt.Errorf("cannot get block time, no blocks saved")
85+
}
86+
87+
return val[0], nil
88+
}
89+
90+
// GetBlockHeightTime return block height and time that a block proposals
91+
// at the input date
92+
func (db *Db) GetBlockHeightTime(now time.Time) (dbtypes.BlockRow, error) {
93+
return db.getBlockHeightTime(now)
94+
}
95+
5896
// GetBlockHeightTimeMinuteAgo return block height and time that a block proposals
5997
// about a minute ago from input date
6098
func (db *Db) GetBlockHeightTimeMinuteAgo(now time.Time) (dbtypes.BlockRow, error) {

database/messages.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package database
2+
3+
import (
4+
"fmt"
5+
dbtypes "github.com/forbole/bdjuno/v3/database/types"
6+
)
7+
8+
// GetMessages returns all messages between block heights
9+
func (db *Db) GetMessages(address string, startHeight int64, endHeight int64, offset uint64, limit uint64) ([]dbtypes.MessageRow, error) {
10+
query := fmt.Sprintf(`SELECT
11+
transaction_hash,
12+
index,
13+
type,
14+
value,
15+
involved_accounts_addresses,
16+
height
17+
FROM message
18+
WHERE involved_accounts_addresses @> '{%s}' AND height >= %d AND height <= %d
19+
ORDER BY height ASC, type ASC, index ASC
20+
OFFSET %d LIMIT %d`, address, startHeight, endHeight, offset, limit)
21+
22+
var dbRows []dbtypes.MessageRow
23+
err := db.Sqlx.Select(&dbRows, query)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
return dbRows, nil
29+
}
30+
31+
// GetMessagesCount returns count of GetMessages
32+
func (db *Db) GetMessagesCount(address string, startHeight int64, endHeight int64) (int, error) {
33+
34+
stmt, err := db.Sqlx.Prepare(fmt.Sprintf(`SELECT count(height)
35+
FROM message
36+
WHERE involved_accounts_addresses @> '{%s}' AND height >= %d AND height <= %d`, address, startHeight, endHeight))
37+
if err != nil {
38+
return 0, err
39+
}
40+
41+
var count int
42+
err = stmt.QueryRow().Scan(&count)
43+
44+
if err != nil {
45+
return 0, err
46+
}
47+
return count, nil
48+
}

database/schema/12-wasm.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,22 @@ CREATE TABLE wasm_contract
4141
height BIGINT NOT NULL
4242
);
4343
CREATE INDEX wasm_contract_height_index ON wasm_contract (height);
44+
CREATE INDEX wasm_contract_creator_index ON wasm_contract (creator);
45+
CREATE INDEX wasm_contract_label_index ON wasm_contract (label);
4446

4547
CREATE TABLE wasm_execute_contract
4648
(
4749
sender TEXT NOT NULL,
4850
contract_address TEXT NOT NULL REFERENCES wasm_contract (contract_address),
4951
raw_contract_message JSONB NOT NULL DEFAULT '{}'::JSONB,
5052
funds COIN[] NOT NULL DEFAULT '{}',
53+
message_type TEXT NULL,
5154
data TEXT NULL,
5255
executed_at TIMESTAMP NOT NULL,
53-
height BIGINT NOT NULL
56+
height BIGINT NOT NULL,
57+
hash TEXT NOT NULL
5458
);
5559
CREATE INDEX execute_contract_height_index ON wasm_execute_contract (height);
60+
CREATE INDEX execute_contract_executed_at_index ON wasm_execute_contract (executed_at);
61+
CREATE INDEX execute_contract_message_type_index ON wasm_execute_contract (message_type);
5662

database/schema/13-wasm-events.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE wasm_execute_contract_event_types
2+
(
3+
contract_address TEXT NOT NULL REFERENCES wasm_contract (contract_address),
4+
event_type TEXT NOT NULL,
5+
6+
first_seen_height BIGINT NOT NULL REFERENCES block (height),
7+
first_seen_hash TEXT NOT NULL,
8+
9+
last_seen_height BIGINT NOT NULL REFERENCES block (height),
10+
last_seen_hash TEXT NOT NULL,
11+
UNIQUE (contract_address, event_type)
12+
);
13+
CREATE INDEX wasm_execute_contract_event_types_index ON wasm_execute_contract_event_types (contract_address, event_type);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
CREATE TABLE nyx_nym_mixnet_v1_mixnode
2+
(
3+
identity_key TEXT UNIQUE PRIMARY KEY,
4+
last_is_bonded_status BOOLEAN NULL,
5+
6+
-- values: in_active_set, in_standby_set, inactive
7+
last_mixnet_status TEXT NULL
8+
);
9+
CREATE INDEX nyx_nym_mixnet_v1_mixnode_status_index ON nyx_nym_mixnet_v1_mixnode (last_mixnet_status);
10+
11+
CREATE TABLE nyx_nym_mixnet_v1_mixnode_status
12+
(
13+
-- values: in_active_set, in_standby_set, inactive
14+
mixnet_status TEXT NOT NULL,
15+
16+
-- in the range 0 to 100
17+
routing_score INTEGER NOT NULL,
18+
19+
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_mixnode (identity_key),
20+
executed_at TIMESTAMP NOT NULL,
21+
height BIGINT NOT NULL REFERENCES block (height),
22+
hash TEXT NOT NULL
23+
);
24+
CREATE INDEX nyx_nm_v1_m_status_height_index ON nyx_nym_mixnet_v1_mixnode_status (height);
25+
CREATE INDEX nyx_nm_v1_m_identity_key_index ON nyx_nym_mixnet_v1_mixnode_status (identity_key);
26+
27+
CREATE TABLE nyx_nym_mixnet_v1_mixnode_events
28+
(
29+
-- values: bond, unbond, delegate, undelegate, claim
30+
event_kind TEXT NOT NULL,
31+
-- values: mixnode_operator, mixnode_delegator, mixnet_rewarding, mixnet_monitoring
32+
actor TEXT NOT NULL,
33+
sender TEXT NOT NULL,
34+
proxy TEXT NULL,
35+
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_mixnode (identity_key),
36+
executed_at TIMESTAMP NOT NULL,
37+
height BIGINT NOT NULL REFERENCES block (height),
38+
hash TEXT NOT NULL,
39+
message_index BIGINT NOT NULL
40+
);
41+
CREATE INDEX nyx_nm_v1_me_height_index ON nyx_nym_mixnet_v1_mixnode_events (identity_key, height);
42+
CREATE INDEX nyx_nm_v1_me_identity_key_executed_at_index ON nyx_nym_mixnet_v1_mixnode_events (identity_key, executed_at);
43+
44+
CREATE TABLE nyx_nym_mixnet_v1_mixnode_reward
45+
(
46+
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_mixnode (identity_key),
47+
total_node_reward COIN[] NOT NULL DEFAULT '{}',
48+
total_delegations COIN[] NOT NULL DEFAULT '{}',
49+
operator_reward COIN[] NOT NULL DEFAULT '{}',
50+
unit_delegator_reward BIGINT NOT NULL,
51+
apy FLOAT NOT NULL,
52+
executed_at TIMESTAMP NOT NULL,
53+
height BIGINT NOT NULL REFERENCES block (height),
54+
hash TEXT NOT NULL,
55+
message_index BIGINT NOT NULL,
56+
UNIQUE (identity_key, height, hash, message_index)
57+
);
58+
CREATE INDEX nyx_nm_v1_mr_height_index ON nyx_nym_mixnet_v1_mixnode_reward (height);
59+
CREATE INDEX nyx_nm_v1_mr_identity_key_index ON nyx_nym_mixnet_v1_mixnode_reward (identity_key);
60+
61+
CREATE TABLE nyx_nym_mixnet_v1_gateway
62+
(
63+
identity_key TEXT UNIQUE PRIMARY KEY,
64+
last_is_bonded_status BOOLEAN NULL
65+
);
66+
67+
CREATE TABLE nyx_nym_mixnet_v1_gateway_events
68+
(
69+
-- values: bond, unbond
70+
event_kind TEXT NOT NULL,
71+
sender TEXT NOT NULL,
72+
proxy TEXT NULL,
73+
identity_key TEXT NOT NULL REFERENCES nyx_nym_mixnet_v1_gateway (identity_key),
74+
executed_at TIMESTAMP NOT NULL,
75+
height BIGINT NOT NULL REFERENCES block (height),
76+
hash TEXT NOT NULL,
77+
message_index BIGINT NOT NULL
78+
);
79+
CREATE INDEX nyx_nm_v1_ge_height_index ON nyx_nym_mixnet_v1_gateway_events (identity_key, height);
80+
CREATE INDEX nyx_nm_v1_ge_identity_key_executed_at_index ON nyx_nym_mixnet_v1_gateway_events (identity_key, executed_at);
81+
82+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
CREATE TABLE nyx_nym_mixnet_v2_mixnode
2+
(
3+
mix_id BIGINT UNIQUE PRIMARY KEY,
4+
5+
-- the identity key may be null, because a tx/event with only the mix_id might be processed out of order
6+
identity_key TEXT NULL,
7+
8+
last_is_bonded_status BOOLEAN NULL,
9+
10+
-- values: in_active_set, in_standby_set, inactive
11+
last_mixnet_status TEXT NULL
12+
);
13+
CREATE INDEX nyx_nm_v2_m_status_index ON nyx_nym_mixnet_v2_mixnode (last_mixnet_status);
14+
CREATE INDEX nyx_nm_v2_m_identity_key_index ON nyx_nym_mixnet_v2_mixnode (identity_key);
15+
16+
CREATE TABLE nyx_nym_mixnet_v2_mixnode_status
17+
(
18+
-- values: in_active_set, in_standby_set, inactive
19+
mixnet_status TEXT NOT NULL,
20+
21+
-- in the range 0 to 1
22+
routing_score DECIMAL NOT NULL,
23+
24+
mix_id BIGINT NOT NULL REFERENCES nyx_nym_mixnet_v2_mixnode (mix_id),
25+
executed_at TIMESTAMP NOT NULL,
26+
height BIGINT NOT NULL REFERENCES block (height),
27+
hash TEXT NOT NULL
28+
);
29+
CREATE INDEX nyx_nm_v2_ms_status_height_index ON nyx_nym_mixnet_v2_mixnode_status (height);
30+
CREATE INDEX nyx_nm_v2_ms_executed_at_index ON nyx_nym_mixnet_v2_mixnode_status (executed_at);
31+
32+
CREATE TABLE nyx_nym_mixnet_v2_events
33+
(
34+
-- values: bond, unbond, delegate, undelegate, claim
35+
event_kind TEXT NOT NULL,
36+
-- values: mixnode_operator, mixnode_delegator, mixnet_rewarding, mixnet_monitoring, gateway_operator
37+
actor TEXT NOT NULL,
38+
sender TEXT NOT NULL,
39+
proxy TEXT NULL,
40+
mix_id BIGINT NULL REFERENCES nyx_nym_mixnet_v2_mixnode (mix_id),
41+
identity_key TEXT NOT NULL,
42+
executed_at TIMESTAMP NOT NULL,
43+
height BIGINT NOT NULL REFERENCES block (height),
44+
hash TEXT NOT NULL,
45+
message_index BIGINT NOT NULL
46+
);
47+
CREATE INDEX nyx_nm_v2_e_height_index ON nyx_nym_mixnet_v2_events (mix_id, height);
48+
CREATE INDEX nyx_nm_v2_e_executed_at_index ON nyx_nym_mixnet_v2_events (mix_id, executed_at);
49+
CREATE INDEX nyx_nm_v2_e_identity_key_executed_at_index ON nyx_nym_mixnet_v2_events (identity_key, executed_at);
50+
51+
CREATE TABLE nyx_nym_mixnet_v2_mixnode_reward
52+
(
53+
mix_id BIGINT NOT NULL REFERENCES nyx_nym_mixnet_v2_mixnode (mix_id),
54+
operator_reward COIN[] NOT NULL DEFAULT '{}',
55+
delegates_reward COIN[] NOT NULL DEFAULT '{}',
56+
prior_delegates COIN[] NOT NULL DEFAULT '{}',
57+
prior_unit_reward BIGINT NOT NULL,
58+
apy FLOAT NOT NULL,
59+
epoch BIGINT NOT NULL,
60+
executed_at TIMESTAMP NOT NULL,
61+
height BIGINT NOT NULL REFERENCES block (height),
62+
hash TEXT NOT NULL,
63+
message_index BIGINT NOT NULL,
64+
UNIQUE (mix_id, height, hash, message_index)
65+
);
66+
CREATE INDEX nyx_nm_v2_mr_height_index ON nyx_nym_mixnet_v2_mixnode_reward (mix_id, height);
67+
CREATE INDEX nyx_nm_v2_mr_executed_at_index ON nyx_nym_mixnet_v2_mixnode_reward (mix_id, executed_at);

database/types/messages.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
"github.com/lib/pq"
6+
)
7+
8+
type MessageRow struct {
9+
TxHash string `db:"transaction_hash"`
10+
Index int `db:"index"`
11+
Type string `db:"type"`
12+
Value string `db:"value"`
13+
Aliases pq.StringArray `db:"involved_accounts_addresses"`
14+
Height int64 `db:"height"`
15+
}
16+
17+
type MessageWithValueRow struct {
18+
TxHash string `json:"transaction_hash"`
19+
Index int `json:"index"`
20+
Type string `json:"type"`
21+
Value interface{} `json:"value"`
22+
Funds interface{} `json:"funds"`
23+
Aliases pq.StringArray `json:"involved_accounts_addresses"`
24+
Height int64 `json:"height"`
25+
}
26+
27+
func NewMessageWithValueRow(row MessageRow) (*MessageWithValueRow, error) {
28+
var value map[string]interface{}
29+
err := json.Unmarshal([]byte(row.Value), &value)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
var funds interface{}
35+
36+
if value["funds"] != nil {
37+
funds = value["funds"]
38+
}
39+
if value["amount"] != nil {
40+
funds = value["amount"]
41+
}
42+
43+
var rowWithValue = MessageWithValueRow{
44+
TxHash: row.TxHash,
45+
Index: row.Index,
46+
Type: row.Type,
47+
Value: value,
48+
Funds: funds,
49+
Aliases: row.Aliases,
50+
Height: row.Height,
51+
}
52+
53+
return &rowWithValue, nil
54+
}

0 commit comments

Comments
 (0)