Skip to content

Commit

Permalink
Merge pull request #32 from cybercongress/v2-dev
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
cyborgshead authored Sep 30, 2024
2 parents 6a66e22 + 0592317 commit 5b21ec6
Show file tree
Hide file tree
Showing 52 changed files with 1,829 additions and 1,101 deletions.
16 changes: 8 additions & 8 deletions .env
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
CHAIN_PREFIX=bostrom
POSTGRES_DB_FOLDER=$HOME/.cyberindex/postgres
POSTGRES_DB_HOST=localhost
POSTGRES_DB_PORT=5432
POSTGRES_DB_NAME=cyberindex
POSTGRES_USER_NAME=cyber
POSTGRES_DB_PASSWORD=1cyberindex1
POSTGRES_SSL_MODE=disable
POSTGRES_FOLDER=$HOME/.cyberindex/postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=cyber
POSTGRES_PASSWORD=1cyberindex1
POSTGRES_SSL=false
HASURA_PORT=8090
HASURA_ADMIN_SECRET=1hasura1
JUNO_WORKERS=1
INDEX_WORKERS=1
RPC_URL=http://localhost:26657
GRPC_URL=http://localhost:9090
START_HEIGHT=1
28 changes: 21 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM ubuntu:20.04
FROM ubuntu:22.04 as builder

ENV GO_VERSION '1.18.10'
ENV GO_VERSION '1.22.2'
ENV GO_ARCH 'linux-amd64'
ENV GO_BIN_SHA '5e05400e4c79ef5394424c0eff5b9141cb782da25f64f79d54c98af0a37f8d49'
ENV GO_BIN_SHA '5901c52b7a78002aeff14a21f93e0f064f74ce1360fce51c6ee68cd471216a17'

WORKDIR /app

Expand All @@ -19,10 +19,24 @@ ENV PATH="/usr/local/go/bin:$PATH"

COPY . .

RUN make build
ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.5.0/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.a

RUN cp ./build/cyberindex /usr/local/bin/
RUN go mod download

ENTRYPOINT ["./entrypoint.sh"]
RUN LINK_STATICALLY=true BUILD_TAGS="muslc" make build

CMD ["./start_script.sh"]
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y --no-install-recommends build-essential ca-certificates

WORKDIR /app

COPY --from=builder /app/build/cyberindex /usr/bin/cyberindex

#COPY --from=builder /root/go/pkg/mod/github.com/!cosm!wasm/[email protected]/internal/api/libwasmvm.x86_64.so /root/go/pkg/mod/github.com/!cosm!wasm/[email protected]/internal/api/libwasmvm.x86_64.so

COPY ./entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

CMD ["cyberindex", "start"]
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ install: go.sum
@echo "installing cyberindex binary..."
@go install -mod=readonly $(BUILD_FLAGS) ./cmd/cyberindex
.PHONY: install

###############################################################################
### Tools / Dependencies ###
###############################################################################

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
go mod verify
go mod tidy
.PHONY: go.sum

.PHONY: go.sum go-mod-cache
41 changes: 22 additions & 19 deletions cmd/cyberindex/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package main

import (
"github.com/cybercongress/cyberindex/v1/modules"
"github.com/forbole/bdjuno/v3/types/config"
"github.com/forbole/juno/v3/cmd"
junomessages "github.com/forbole/juno/v3/modules/messages"

cybermessages "github.com/cybercongress/cyberindex/v1/modules/messages"
cyberapp "github.com/cybercongress/go-cyber/v2/app"
"github.com/forbole/bdjuno/v3/database"
initcmd "github.com/forbole/juno/v3/cmd/init"
parsecmd "github.com/forbole/juno/v3/cmd/parse"
parsetypes "github.com/forbole/juno/v3/cmd/parse/types"
startcmd "github.com/forbole/juno/v3/cmd/start"
"github.com/cybercongress/cyberindex/v2/modules"
cyberapp "github.com/cybercongress/go-cyber/v4/app"
migratecmd "github.com/forbole/callisto/v4/cmd/migrate"
"github.com/forbole/callisto/v4/database"
"github.com/forbole/callisto/v4/types/config"
"github.com/forbole/juno/v5/cmd"
initcmd "github.com/forbole/juno/v5/cmd/init"
parsecmd "github.com/forbole/juno/v5/cmd/parse"
parsetypes "github.com/forbole/juno/v5/cmd/parse/types"
startcmd "github.com/forbole/juno/v5/cmd/start"
junomessages "github.com/forbole/juno/v5/modules/messages"
"github.com/forbole/juno/v5/types/params"
)

func main() {
initCfg := initcmd.NewConfig().
WithConfigCreator(config.Creator)

parseCfg := parsetypes.NewConfig().
WithDBBuilder(database.Builder).
WithEncodingConfigBuilder(cyberapp.MakeTestEncodingConfig).
WithRegistrar(modules.NewRegistrar(getAddressesParser()))
WithRegistrar(modules.NewRegistrar(getAddressesParser())).
WithEncodingConfigBuilder(func() params.EncodingConfig {
return params.EncodingConfig(cyberapp.MakeEncodingConfig())
}).
WithDBBuilder(database.Builder)

cfg := cmd.NewConfig("cyberindex").
WithInitConfig(initCfg).
Expand All @@ -34,10 +36,11 @@ func main() {
cmd.VersionCmd(),
initcmd.NewInitCmd(cfg.GetInitConfig()),
parsecmd.NewParseCmd(cfg.GetParseConfig()),
migratecmd.NewMigrateCmd(cfg.GetName(), cfg.GetParseConfig()),
startcmd.NewStartCmd(cfg.GetParseConfig()),
)

executor := cmd.BuildDefaultExecutor(cfg)
executor := cmd.PrepareRootCmd(cfg.GetName(), rootCmd)
err := executor.Execute()
if err != nil {
panic(err)
Expand All @@ -47,8 +50,8 @@ func main() {
func getAddressesParser() junomessages.MessageAddressesParser {
return junomessages.JoinMessageParsers(
junomessages.CosmosMessageAddressesParser,
cybermessages.CyberMessageAddressesParser,
cybermessages.WasmMessageAddressesParser,
cybermessages.LiquidityMessageAddressesParser,
//cybermessages.CyberMessageAddressesParser,
//cybermessages.WasmMessageAddressesParser,
//cybermessages.LiquidityMessageAddressesParser,
)
}
10 changes: 5 additions & 5 deletions database/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package database

import (
"fmt"
types2 "github.com/cybercongress/cyberindex/v1/database/types"
types2 "github.com/cybercongress/cyberindex/v2/database/types"

dbutils "github.com/cybercongress/cyberindex/v1/database/utils"
dbutils "github.com/cybercongress/cyberindex/v2/database/utils"

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

"github.com/forbole/bdjuno/v3/types"
"github.com/forbole/callisto/v4/types"
)

// SaveAccountBalances allows to store the given balances inside the database
Expand Down Expand Up @@ -55,7 +55,7 @@ ON CONFLICT (address) DO UPDATE
height = excluded.height
WHERE account_balance.height <= excluded.height`

_, err := db.Sql.Exec(stmt, params...)
_, err := db.SQL.Exec(stmt, params...)
if err != nil {
return fmt.Errorf("error while storing up-to-date balances: %s", err)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ ON CONFLICT (one_row_id) DO UPDATE
height = excluded.height
WHERE supply.height <= excluded.height`

_, err := db.Sql.Exec(query, pq.Array(types2.NewDbCoins(coins)), height)
_, err := db.SQL.Exec(query, pq.Array(types2.NewDbCoins(coins)), height)
if err != nil {
return fmt.Errorf("error while storing supply: %s", err)
}
Expand Down
190 changes: 190 additions & 0 deletions database/consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package database

import (
"fmt"
"time"

"github.com/forbole/callisto/v4/types"

dbtypes "github.com/forbole/callisto/v4/database/types"
)

// GetLastBlock returns the last block stored inside the database based on the heights
func (db *CyberDb) GetLastBlock() (*dbtypes.BlockRow, error) {
stmt := `SELECT * FROM block ORDER BY height DESC LIMIT 1`

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

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

return &blocks[0], nil
}

// GetLastBlockHeight returns the last block height and timestamp stored inside the database
func (db *CyberDb) GetLastBlockHeightAndTimestamp() (dbtypes.BlockHeightAndTimestamp, error) {
stmt := `SELECT height, timestamp FROM block ORDER BY height DESC LIMIT 1`

var blockHeightAndTimestamp []dbtypes.BlockHeightAndTimestamp
if err := db.Sqlx.Select(&blockHeightAndTimestamp, stmt); err != nil {
return dbtypes.BlockHeightAndTimestamp{}, fmt.Errorf("cannot get last block height and timestamp from db: %s", err)
}

if len(blockHeightAndTimestamp) == 0 {
return dbtypes.BlockHeightAndTimestamp{}, nil
}

return blockHeightAndTimestamp[0], nil
}

// -------------------------------------------------------------------------------------------------------------------

// getBlockHeightTime retrieves the block at the specific time
func (db *CyberDb) getBlockHeightTime(pastTime time.Time) (dbtypes.BlockRow, error) {
stmt := `SELECT * FROM block WHERE block.timestamp <= $1 ORDER BY block.timestamp DESC LIMIT 1;`

var val []dbtypes.BlockRow
if err := db.Sqlx.Select(&val, stmt, pastTime); 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
}

// GetBlockHeightTimeMinuteAgo return block height and time that a block proposals
// about a minute ago from input date
func (db *CyberDb) GetBlockHeightTimeMinuteAgo(now time.Time) (dbtypes.BlockRow, error) {
pastTime := now.Add(time.Minute * -1)
return db.getBlockHeightTime(pastTime)
}

// GetBlockHeightTimeHourAgo return block height and time that a block proposals
// about a hour ago from input date
func (db *CyberDb) GetBlockHeightTimeHourAgo(now time.Time) (dbtypes.BlockRow, error) {
pastTime := now.Add(time.Hour * -1)
return db.getBlockHeightTime(pastTime)
}

// GetBlockHeightTimeDayAgo return block height and time that a block proposals
// about a day (24hour) ago from input date
func (db *CyberDb) GetBlockHeightTimeDayAgo(now time.Time) (dbtypes.BlockRow, error) {
pastTime := now.Add(time.Hour * -24)
return db.getBlockHeightTime(pastTime)
}

// -------------------------------------------------------------------------------------------------------------------

// SaveAverageBlockTimePerMin save the average block time in average_block_time_per_minute table
func (db *CyberDb) SaveAverageBlockTimePerMin(averageTime float64, height int64) error {
stmt := `
INSERT INTO average_block_time_per_minute(average_time, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET average_time = excluded.average_time,
height = excluded.height
WHERE average_block_time_per_minute.height <= excluded.height`

_, err := db.Sqlx.Exec(stmt, averageTime, height)
if err != nil {
return fmt.Errorf("error while storing average block time per minute: %s", err)
}

return nil
}

// SaveAverageBlockTimePerHour save the average block time in average_block_time_per_hour table
func (db *CyberDb) SaveAverageBlockTimePerHour(averageTime float64, height int64) error {
stmt := `
INSERT INTO average_block_time_per_hour(average_time, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET average_time = excluded.average_time,
height = excluded.height
WHERE average_block_time_per_hour.height <= excluded.height`

_, err := db.Sqlx.Exec(stmt, averageTime, height)
if err != nil {
return fmt.Errorf("error while storing average block time per hour: %s", err)
}

return nil
}

// SaveAverageBlockTimePerDay save the average block time in average_block_time_per_day table
func (db *CyberDb) SaveAverageBlockTimePerDay(averageTime float64, height int64) error {
stmt := `
INSERT INTO average_block_time_per_day(average_time, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET average_time = excluded.average_time,
height = excluded.height
WHERE average_block_time_per_day.height <= excluded.height`

_, err := db.Sqlx.Exec(stmt, averageTime, height)
if err != nil {
return fmt.Errorf("error while storing average block time per day: %s", err)
}

return nil
}

// SaveAverageBlockTimeGenesis save the average block time in average_block_time_from_genesis table
func (db *CyberDb) SaveAverageBlockTimeGenesis(averageTime float64, height int64) error {
stmt := `
INSERT INTO average_block_time_from_genesis(average_time ,height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET average_time = excluded.average_time,
height = excluded.height
WHERE average_block_time_from_genesis.height <= excluded.height`

_, err := db.Sqlx.Exec(stmt, averageTime, height)
if err != nil {
return fmt.Errorf("error while storing average block time since genesis: %s", err)
}

return nil
}

// -------------------------------------------------------------------------------------------------------------------

// SaveGenesis save the given genesis data
func (db *CyberDb) SaveGenesis(genesis *types.Genesis) error {
stmt := `
INSERT INTO genesis(time, chain_id, initial_height)
VALUES ($1, $2, $3) ON CONFLICT (one_row_id) DO UPDATE
SET time = excluded.time,
initial_height = excluded.initial_height,
chain_id = excluded.chain_id`

_, err := db.Sqlx.Exec(stmt, genesis.Time, genesis.ChainID, genesis.InitialHeight)
if err != nil {
return fmt.Errorf("error while storing genesis: %s", err)
}

return nil
}

// GetGenesis returns the genesis information stored inside the database
func (db *CyberDb) GetGenesis() (*types.Genesis, error) {
var rows []*dbtypes.GenesisRow
err := db.Sqlx.Select(&rows, `SELECT * FROM genesis;`)
if err != nil {
return nil, err
}

if len(rows) == 0 {
return nil, fmt.Errorf("no rows inside the genesis table")
}

row := rows[0]
return types.NewGenesis(row.ChainID, row.Time, row.InitialHeight), nil
}
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package database

import (
"github.com/forbole/bdjuno/v3/database"
"github.com/forbole/callisto/v4/database"
)

type CyberDb struct {
Expand Down
Loading

0 comments on commit 5b21ec6

Please sign in to comment.