Skip to content

Commit

Permalink
Improvements and bug fixes
Browse files Browse the repository at this point in the history
See PR #83
  • Loading branch information
RiccardoM authored Jan 25, 2021
1 parent 334c183 commit 3c635f3
Show file tree
Hide file tree
Showing 75 changed files with 1,656 additions and 2,126 deletions.
9 changes: 8 additions & 1 deletion .docs/messages/gov.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ the `content.@type` field. Following you can find the most common ones.
"amount": "20000000"
}
]
}
},
"initial_deposit": [
{
"denom": "udaric",
"amount": "20000000"
}
],
"proposer": "desmos13yp2fq3tslq6mmtq4628q38xzj75ethzela9uu"
}
```

Expand Down
44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,33 @@ bdjuno parse <path/to/config.toml>
The configuration must be a TOML file containing the following fields:

```toml
rpc_node = "<rpc-ip/host>:<rpc-port>"
client_node = "<client-ip/host>:<client-port>"
[cosmos]
prefix = "desmos"
modules = []

[rpc]
address = "<rpc-ip/host>:<rpc-port>"

[grpc]
address = "<grpc-ip/host>:<grpc-port>"
insecure = true

[api]
address = "<client-ip/host>:<client-port>"

[cosmos]
prefix = "<bech32-prefix>"
prefix = "desmos"
modules = []

[database]
type = "<db-type>"
type = "postgresql"

[database.config]
host = "<db-host>"
port = 5432
name = "<db-name>"
user = "<db-user>"
password = "<db-password>"
ssl_mode = "<ssl-mode (optional)>"
schema = "<schema (optional)>"
```

Example of a configuration to parse the chain state from a local Cosmos full-node:
Expand All @@ -64,11 +73,22 @@ Example of a configuration to parse the chain state from a local Cosmos full-nod
<summary>Open here</summary>

```toml
rpc_node = "http://localhost:26657"
client_node = "http://localhost:1317"
[cosmos]
prefix = "desmos"
modules = []

[rpc]
address = "http://localhost:26657"

[grpc]
address = "localhost:9090"
insecure = true

[api]
address = "http://localhost:1317"

[cosmos]
prefix = "cosmos"
prefix = "desmos"
modules = [
"auth",
"bank",
Expand All @@ -78,17 +98,17 @@ modules = [
"mint",
"modules",
"pricefeed",
"staking",
"supply"
"slashing",
"staking"
]

[database]
type = "postgresql"

[database.config]
name = "bdjuno"
host = "localhost"
port = 5432
name = "bdjuno"
user = "user"
password = "password"
```
Expand Down
51 changes: 34 additions & 17 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

import (
"github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/juno/client"
"github.com/desmos-labs/juno/cmd"
"github.com/desmos-labs/juno/config"
"github.com/desmos-labs/juno/modules/registrar"
"github.com/desmos-labs/juno/db"
"github.com/desmos-labs/juno/modules"

"github.com/forbole/bdjuno/x/slashing"

"github.com/forbole/bdjuno/x/mint"
"github.com/forbole/bdjuno/x/modules"
bmodules "github.com/forbole/bdjuno/x/modules"

"github.com/cosmos/cosmos-sdk/simapp"

Expand All @@ -18,27 +24,38 @@ import (
"github.com/forbole/bdjuno/x/gov"
"github.com/forbole/bdjuno/x/pricefeed"
"github.com/forbole/bdjuno/x/staking"
"github.com/forbole/bdjuno/x/supply"
)

func main() {
// Register all the modules to be handled
registrar.RegisterModules(
auth.Module{},
bank.Module{},
consensus.Module{},
distribution.Module{},
gov.Module{},
mint.Module{},
modules.Module{},
pricefeed.Module{},
staking.Module{},
supply.Module{},
executor := cmd.BuildDefaultExecutor(
"bdjuno",
&ModulesRegistrar{},
config.DefaultSetup,
simapp.MakeTestEncodingConfig,
database.Builder,
)

executor := cmd.BuildDefaultExecutor("bdjuno", config.DefaultSetup, simapp.MakeCodec, database.Builder)
err := executor.Execute()
if err != nil {
panic(err)
}
}

type ModulesRegistrar struct{}

func (*ModulesRegistrar) BuildModules(
cfg *config.Config, encodingConfig *params.EncodingConfig, _ *sdk.Config, db db.Database, cp *client.Proxy,
) modules.Modules {
bigDipperBd := database.Cast(db)
return []modules.Module{
auth.NewModule(encodingConfig, cp.GrpcConnection(), bigDipperBd),
bank.NewModule(encodingConfig, cp.GrpcConnection(), bigDipperBd),
consensus.NewModule(cp, bigDipperBd),
distribution.NewModule(cp.GrpcConnection(), bigDipperBd),
gov.NewModule(encodingConfig, cp.GrpcConnection(), bigDipperBd),
mint.NewModule(cp.GrpcConnection(), bigDipperBd),
bmodules.NewModule(cfg, bigDipperBd),
pricefeed.NewModule(bigDipperBd),
slashing.NewModule(cp.GrpcConnection(), bigDipperBd),
staking.NewModule(encodingConfig, cp.GrpcConnection(), bigDipperBd),
}
}
4 changes: 2 additions & 2 deletions database/auth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package database

import (
"github.com/cosmos/cosmos-sdk/x/auth/exported"
authttypes "github.com/cosmos/cosmos-sdk/x/auth/types"

dbtypes "github.com/forbole/bdjuno/database/types"
)

// SaveAccount saves the given account information for the given block height and timestamp
func (db *BigDipperDb) SaveAccount(account exported.Account) error {
func (db *BigDipperDb) SaveAccount(account authttypes.AccountI) error {
stmt := `INSERT INTO account (address) VALUES ($1) ON CONFLICT DO NOTHING`
_, err := db.Sql.Exec(stmt, account.GetAddress().String())
return err
Expand Down
8 changes: 4 additions & 4 deletions database/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package database_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

dbtypes "github.com/forbole/bdjuno/database/types"
)
Expand All @@ -11,16 +11,16 @@ func (suite *DbTestSuite) TestSaveAccount() {
address, err := sdk.AccAddressFromBech32("cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7")
suite.Require().NoError(err)

account := auth.NewBaseAccountWithAddress(address)
account := authtypes.NewBaseAccountWithAddress(address)

// ------------------------------
// --- Save the data
// ------------------------------

err = suite.database.SaveAccount(&account)
err = suite.database.SaveAccount(account)
suite.Require().NoError(err)

err = suite.database.SaveAccount(&account)
err = suite.database.SaveAccount(account)
suite.Require().NoError(err, "double account insertion should not insert and returns no error")

// ------------------------------
Expand Down
32 changes: 26 additions & 6 deletions database/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,35 @@ func (db *BigDipperDb) SaveAccountBalance(address string, balance sdk.Coins, hei
coins := pq.Array(dbtypes.NewDbCoins(balance))

stmt := `
INSERT INTO account_balance (address, coins)
VALUES ($1, $2) ON CONFLICT (address) DO UPDATE
INSERT INTO account_balance (address, coins, height)
VALUES ($1, $2, $3) ON CONFLICT (address, height) DO UPDATE
SET coins = excluded.coins`
_, err := db.Sql.Exec(stmt, address, coins)
_, err := db.Sql.Exec(stmt, address, coins, height)
return err
}

// SaveSupplyTokenPool allows to save for the given height the given total amount of coins
func (db *BigDipperDb) SaveSupplyToken(coins sdk.Coins, height int64) error {
query := `INSERT INTO supply(coins, height) VALUES ($1,$2)`

_, err := db.Sql.Exec(query, pq.Array(dbtypes.NewDbCoins(coins)), height)
if err != nil {
return err
}
return nil
}

stmt = `INSERT INTO account_balance_history (address, coins, height) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`
_, err = db.Sql.Exec(stmt, address, coins, height)
return err
//GetTokenNames returns the list of token names stored inside the supply table
func (db *BigDipperDb) GetTokenNames() ([]string, error) {
var names []string
query := `
SELECT (coin).denom FROM (
SELECT unnest(coins) AS coin FROM supply WHERE height = (
SELECT max(height) FROM supply
)
) AS unnested`
if err := db.Sqlx.Select(&names, query); err != nil {
return nil, err
}
return names, nil
}
44 changes: 36 additions & 8 deletions database/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lib/pq"

dbtypes "github.com/forbole/bdjuno/database/types"
)
Expand All @@ -26,16 +27,43 @@ func (suite *DbTestSuite) TestSaveAccountBalance() {
suite.Require().True(balRows[0].Equal(dbtypes.NewAccountBalanceRow(
"cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7",
dbtypes.NewDbCoins(coins),
height,
)))
}

// Balance histories
var balHisRows []dbtypes.AccountBalanceHistoryRow
err = suite.database.Sqlx.Select(&balHisRows, `SELECT * FROM account_balance_history ORDER BY address`)
func (suite *DbTestSuite) TestBigDipperDb_SaveTotalTokens() {
coins := sdk.NewCoins(
sdk.NewCoin("desmos", sdk.NewInt(10000)),
sdk.NewCoin("uatom", sdk.NewInt(15)),
)
err := suite.database.SaveSupplyToken(coins, 10)
suite.Require().NoError(err)
suite.Require().Len(balHisRows, 1)
suite.Require().True(balHisRows[0].Equal(dbtypes.NewAccountBalanceHistoryRow(
"cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7",

expected := dbtypes.NewTotalSupplyRow(
dbtypes.NewDbCoins(coins),
height,
)))
10,
)
var rows []dbtypes.TotalSupplyRow
err = suite.database.Sqlx.Select(&rows, `SELECT coins,height FROM supply`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "supply table should contain only one row")

suite.Require().True(expected.Equals(rows[0]))

}

func (suite *DbTestSuite) TestBigDipperDb_GetTokenNames() {
coins := sdk.NewCoins(
sdk.NewCoin("desmos", sdk.NewInt(10000)),
sdk.NewCoin("uatom", sdk.NewInt(15)),
)
_, err := suite.database.Sql.Exec("INSERT INTO supply(coins,height) VALUES ($1,$2) ", pq.Array(dbtypes.NewDbCoins(coins)), 10)
suite.Require().NoError(err)
expected := [2]string{"desmos", "uatom"}
result, err := suite.database.GetTokenNames()

suite.Require().NoError(err)
for i, row := range expected {
suite.Require().True(row == (result[i]))
}
}
2 changes: 1 addition & 1 deletion database/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// SaveConsensus allows to properly store the given consensus event into the database.
// Note that only one consensus event is allowed inside the database at any time.
func (db *BigDipperDb) SaveConsensus(event constypes.ConsensusEvent) error {
func (db *BigDipperDb) SaveConsensus(event *constypes.ConsensusEvent) error {
// Delete all the existing events
stmt := `DELETE FROM consensus WHERE true`
if _, err := db.Sql.Exec(stmt); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion database/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (suite *DbTestSuite) TestSaveConsensus() {
events := []constypes.ConsensusEvent{
events := []*constypes.ConsensusEvent{
constypes.NewConsensusEvent(1, 0, "First"),
constypes.NewConsensusEvent(2, 0, "Second - Round 0 "),
constypes.NewConsensusEvent(2, 1, "Second - Round 1"),
Expand Down
6 changes: 3 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package database
import (
"fmt"

"github.com/cosmos/cosmos-sdk/simapp/params"

"github.com/rs/zerolog/log"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/desmos-labs/juno/config"
"github.com/desmos-labs/juno/db"
"github.com/desmos-labs/juno/db/postgresql"
Expand All @@ -20,10 +21,9 @@ type BigDipperDb struct {
}

// Builder allows to create a new BigDipperDb instance implementing the database.Builder type
func Builder(cfg *config.Config, codec *codec.Codec) (db.Database, error) {
func Builder(cfg *config.Config, codec *params.EncodingConfig) (db.Database, error) {
psqlConfig, ok := cfg.DatabaseConfig.Config.(*config.PostgreSQLConfig)
if !ok {
// TODO: Support MongoDB
return nil, fmt.Errorf("MongoDB configuration is not supported on BigDipper")
}

Expand Down
Loading

0 comments on commit 3c635f3

Please sign in to comment.