Skip to content

Commit b28222b

Browse files
authored
Improved the overall code structure
See PR forbole#60
1 parent d9ddbcc commit b28222b

Some content is hidden

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

85 files changed

+4573
-3912
lines changed

.github/workflows/lint.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ jobs:
1515
timeout-minutes: 6
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: technote-space/get-diff-action@v1
18+
- uses: technote-space/get-diff-action@v4
1919
with:
2020
SUFFIX_FILTER: |
2121
.go
2222
.mod
2323
.sum
2424
- uses: golangci/golangci-lint-action@v2
2525
with:
26-
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
2726
version: v1.28
2827
args: --timeout 10m
2928
github-token: ${{ secrets.GITHUB_TOKEN }}

.golangci.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ linters:
1010
- deadcode
1111
- depguard
1212
- dogsled
13-
# - errcheck
1413
- gocritic
1514
- gofmt
1615
- goimports
@@ -31,7 +30,6 @@ linters:
3130
- unused
3231
- unparam
3332
- misspell
34-
# - wsl
3533
- nolintlint
3634

3735
issues:

Makefile

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
22
COMMIT := $(shell git log -1 --format='%H')
3+
DOCKER := $(shell which docker)
34

45
export GO111MODULE = on
56

@@ -60,10 +61,18 @@ test-unit: start-docker-test
6061
@go test -mod=readonly -v -coverprofile coverage.txt ./...
6162
.PHONY: test-unit
6263

63-
lint:
64-
golangci-lint run --out-format=tab --issues-exit-code=0
65-
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs gofmt -d -s
66-
.PHONY: lint
64+
lint: ## Run lint
65+
$(DOCKER) run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.28.0 golangci-lint run --out-format=tab
66+
67+
lint-fix: ## Run lint with fix arg
68+
$(DOCKER) run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.28.0 golangci-lint run --fix --out-format=tab --issues-exit-code=0
69+
.PHONY: lint lint-fix
70+
71+
format: ## Run format
72+
find . -name '*.go' -type f -not -path "*.git*" | xargs gofmt -w -s
73+
find . -name '*.go' -type f -not -path "*.git*" | xargs misspell -w
74+
find . -name '*.go' -type f -not -path "*.git*" | xargs goimports -w -local github.com/hodlend/go-hodlend-core
75+
.PHONY: format
6776

6877
clean:
6978
rm -f tools-stamp ./build/**

cmd/bdjuno/main.go

+12-53
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package main
22

33
import (
4-
"github.com/cosmos/cosmos-sdk/codec"
5-
x "github.com/forbole/bdjuno/x/types"
4+
"github.com/desmos-labs/juno/cmd"
5+
"github.com/desmos-labs/juno/config"
6+
"github.com/desmos-labs/juno/modules/registrar"
7+
"github.com/forbole/bdjuno/x/mint"
8+
"github.com/forbole/bdjuno/x/modules"
69

710
"github.com/cosmos/cosmos-sdk/simapp"
8-
sdk "github.com/cosmos/cosmos-sdk/types"
9-
"github.com/desmos-labs/juno/config"
10-
"github.com/desmos-labs/juno/executor"
11-
"github.com/desmos-labs/juno/parse"
12-
"github.com/desmos-labs/juno/version"
1311
"github.com/forbole/bdjuno/database"
1412
"github.com/forbole/bdjuno/x/auth"
1513
"github.com/forbole/bdjuno/x/bank"
@@ -21,63 +19,24 @@ import (
2119
"github.com/forbole/bdjuno/x/supply"
2220
)
2321

24-
var (
25-
modules = []x.Module{
22+
func main() {
23+
// Register all the modules to be handled
24+
registrar.RegisterModules(
2625
auth.Module{},
2726
bank.Module{},
2827
consensus.Module{},
2928
distribution.Module{},
3029
gov.Module{},
31-
//mint.Module{},
30+
mint.Module{},
31+
modules.Module{},
3232
pricefeed.Module{},
3333
staking.Module{},
3434
supply.Module{},
35-
}
36-
)
37-
38-
func main() {
39-
// Register all the modules to be handled
40-
SetupModules()
41-
42-
// Build the executor
43-
prefix := "desmos" // TODO: Get this from a command
44-
rootCmd := executor.BuildRootCmd("bdjuno", SetupConfig(prefix))
45-
rootCmd.AddCommand(
46-
version.GetVersionCmd(),
47-
parse.GetParseCmd(MakeCodec(), database.Builder),
4835
)
4936

50-
command := config.PrepareMainCmd(rootCmd)
51-
52-
// Run the commands and panic on any error
53-
err := command.Execute()
37+
executor := cmd.BuildDefaultExecutor("bdjuno", config.DefaultSetup, simapp.MakeCodec, database.Builder)
38+
err := executor.Execute()
5439
if err != nil {
5540
panic(err)
5641
}
5742
}
58-
59-
func SetupConfig(prefix string) func(cfg *sdk.Config) {
60-
return func(cfg *sdk.Config) {
61-
cfg.SetBech32PrefixForAccount(
62-
prefix,
63-
prefix+sdk.PrefixPublic,
64-
)
65-
cfg.SetBech32PrefixForValidator(
66-
prefix+sdk.PrefixValidator+sdk.PrefixOperator,
67-
prefix+sdk.PrefixValidator+sdk.PrefixOperator+sdk.PrefixPublic,
68-
)
69-
cfg.SetBech32PrefixForConsensusNode(
70-
prefix+sdk.PrefixValidator+sdk.PrefixConsensus,
71-
prefix+sdk.PrefixValidator+sdk.PrefixConsensus+sdk.PrefixPublic,
72-
)
73-
}
74-
}
75-
76-
func MakeCodec() *codec.Codec {
77-
cdc := simapp.MakeCodec()
78-
return cdc
79-
}
80-
81-
func SetupModules() {
82-
x.RegisterModules(modules)
83-
}

database/auth.go

+56-24
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,27 @@ import (
1212

1313
// SaveAccount saves the given account information for the given block height and timestamp
1414
func (db BigDipperDb) SaveAccount(account exported.Account, height int64, timestamp time.Time) error {
15-
accStmt := `INSERT INTO account (address) VALUES ($1) ON CONFLICT DO NOTHING`
16-
_, err := db.Sql.Exec(accStmt, account.GetAddress().String())
15+
stmt := `INSERT INTO account (address) VALUES ($1) ON CONFLICT DO NOTHING`
16+
_, err := db.Sql.Exec(stmt, account.GetAddress().String())
1717
if err != nil {
1818
return err
1919
}
2020

21-
balStmt := `INSERT INTO balance (address, coins, height, timestamp)
22-
VALUES ($1, $2::coin[], $3, $4) ON CONFLICT DO NOTHING`
23-
_, err = db.Sql.Exec(balStmt,
24-
account.GetAddress().String(), pq.Array(dbtypes.NewDbCoins(account.GetCoins())), height, timestamp)
21+
coins := pq.Array(dbtypes.NewDbCoins(account.GetCoins()))
22+
23+
stmt = `
24+
INSERT INTO account_balance (address, coins)
25+
VALUES ($1, $2)
26+
ON CONFLICT (address) DO UPDATE SET coins = excluded.coins`
27+
_, err = db.Sql.Exec(stmt, account.GetAddress().String(), coins)
28+
if err != nil {
29+
return err
30+
}
31+
32+
stmt = `
33+
INSERT INTO account_balance_history (address, coins, height, timestamp)
34+
VALUES ($1, $2::coin[], $3, $4) ON CONFLICT DO NOTHING`
35+
_, err = db.Sql.Exec(stmt, account.GetAddress().String(), coins, height, timestamp)
2536
return err
2637
}
2738

@@ -32,36 +43,57 @@ func (db BigDipperDb) SaveAccounts(accounts []exported.Account, height int64, ti
3243
return nil
3344
}
3445

35-
accountsStmt := "INSERT INTO account (address) VALUES "
36-
var accountParams []interface{}
46+
accQry := `INSERT INTO account (address) VALUES `
47+
var accParams []interface{}
48+
49+
balQry := `INSERT INTO account_balance (address, coins) VALUES `
50+
var balParams []interface{}
3751

38-
balancesStmt := "INSERT INTO balance (address, coins, height, timestamp) VALUES "
39-
var balanceParams []interface{}
52+
balHisQry := `INSERT INTO account_balance_history (address, coins, height, timestamp) VALUES `
53+
var bHisParams []interface{}
4054

4155
for i, account := range accounts {
42-
a1 := i * 1 // Starting position for
43-
b1 := i * 4 // Starting position for balances insertion
56+
ai := i * 1
57+
accQry += fmt.Sprintf("($%d),", ai+1)
58+
accParams = append(accParams, account.GetAddress().String())
4459

45-
accountsStmt += fmt.Sprintf("($%d),", a1+1)
46-
accountParams = append(accountParams, account.GetAddress().String())
60+
coins := pq.Array(dbtypes.NewDbCoins(account.GetCoins()))
4761

48-
balancesStmt += fmt.Sprintf("($%d,$%d,$%d,$%d),", b1+1, b1+2, b1+3, b1+4)
49-
balanceParams = append(balanceParams,
50-
account.GetAddress().String(), pq.Array(dbtypes.NewDbCoins(account.GetCoins())), height, timestamp)
62+
bi := i * 2
63+
balQry += fmt.Sprintf("($%d, $%d),", bi+1, bi+2)
64+
balParams = append(balParams, account.GetAddress().String(), coins)
65+
66+
bHi := i * 4
67+
balHisQry += fmt.Sprintf("($%d,$%d,$%d,$%d),", bHi+1, bHi+2, bHi+3, bHi+4)
68+
bHisParams = append(bHisParams, account.GetAddress().String(), coins, height, timestamp)
5169
}
5270

5371
// Store the accounts
54-
accountsStmt = accountsStmt[:len(accountsStmt)-1] // Remove trailing ","
55-
accountsStmt += " ON CONFLICT DO NOTHING"
56-
_, err := db.Sql.Exec(accountsStmt, accountParams...)
72+
accQry = accQry[:len(accQry)-1] // Remove trailing ","
73+
accQry += " ON CONFLICT (address) DO NOTHING"
74+
_, err := db.Sql.Exec(accQry, accParams...)
75+
if err != nil {
76+
return err
77+
}
78+
79+
// Remove all the existing balances
80+
_, err = db.Sql.Exec(`DELETE FROM account_balance WHERE TRUE`)
81+
if err != nil {
82+
return err
83+
}
84+
85+
// Insert the current balances
86+
balQry = balQry[:len(balQry)-1] // Remove trailing ","
87+
balQry += " ON CONFLICT (address) DO NOTHING"
88+
_, err = db.Sql.Exec(balQry, balParams...)
5789
if err != nil {
5890
return err
5991
}
6092

61-
// Store the balances
62-
balancesStmt = balancesStmt[:len(balancesStmt)-1] // Remove trailing ","
63-
balancesStmt += " ON CONFLICT DO NOTHING"
64-
_, err = db.Sql.Exec(balancesStmt, balanceParams...)
93+
// Store the balances histories
94+
balHisQry = balHisQry[:len(balHisQry)-1] // Remove trailing ","
95+
balHisQry += " ON CONFLICT (address, height) DO NOTHING"
96+
_, err = db.Sql.Exec(balHisQry, bHisParams...)
6597
return err
6698
}
6799

0 commit comments

Comments
 (0)