Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge features from v4.0.0-stratos #7

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ config.yaml

# Coverage
coverage.*
!local/bdjuno/config.yaml
!local/hasura/config.yaml
vendor/
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:1.20-alpine AS builder
RUN apk update
RUN apk add --no-cache make git build-base gmp-dev flex bison curl

ARG pbc_lib_ver=0.5.14
RUN curl -L https://crypto.stanford.edu/pbc/files/pbc-${pbc_lib_ver}.tar.gz > pbc-${pbc_lib_ver}.tar.gz && \
tar xzvf pbc-${pbc_lib_ver}.tar.gz && \
cd pbc-${pbc_lib_ver} && \
./configure && \
make && \
make install && \
ldconfig / && \
cd $BUILD && \
rm -rf pbc-${pbc_lib_ver}*

WORKDIR /go/src/github.com/forbole/bdjuno
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN make build

FROM alpine:latest
WORKDIR /bdjuno
COPY --from=builder /go/src/github.com/forbole/bdjuno/build/bdjuno /usr/bin/bdjuno
COPY --from=builder /usr/local/lib/libpbc.so.1.0.0 /usr/local/lib/libpbc.so.1.0.0

RUN apk add --no-cache gmp-dev
RUN cd /usr/local/lib && { ln -s -f libpbc.so.1.0.0 libpbc.so.1 || { rm -f libpbc.so.1 && ln -s libpbc.so.1.0.0 libpbc.so.1; }; } \
&& cd /usr/local/lib && { ln -s -f libpbc.so.1.0.0 libpbc.so || { rm -f libpbc.so && ln -s libpbc.so.1.0.0 libpbc.so; }; }

CMD [ "bdjuno" ]
11 changes: 0 additions & 11 deletions Dockerfile.default

This file was deleted.

34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,38 @@ This will:
1. Create a Docker container running a PostgreSQL database.
2. Run all the tests using that database as support.

## Local runner

```shell
docker compose -f docker-compose.local.yml up -d
```

NOTE: You need to perform first time after launch

1. Run migrations
```shell
docker compose -f docker-compose.local.yml exec postgres sh "./tmp/migrate.sh"
```

NOTE: If you on v012, also launch
```shell
docker compose -f docker-compose.local.yml exec postgres sh "./tmp/migrate_v5.sh"
```

2. Load mesos genesis
```shell
curl https://raw.githubusercontent.com/stratosnet/stratos-chain-testnet/main/mesos-1/genesis.json > local/genesis.json
```

3. Launch hasura container
```shell
docker compose -f docker-compose.local.yml exec hasura sh
```

and execute the following lines
```shell
apt-get update && apt-get install -y curl bash
curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash

hasura metadata apply --skip-update-check --project /hasura
```
7 changes: 4 additions & 3 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

"github.com/forbole/bdjuno/v4/types/config"

"cosmossdk.io/simapp"

"github.com/forbole/bdjuno/v4/database"
"github.com/forbole/bdjuno/v4/modules"

stchainapp "github.com/stratosnet/stratos-chain/app"
)

func main() {
Expand Down Expand Up @@ -55,7 +55,7 @@ func main() {
// This should be edited by custom implementations if needed.
func getBasicManagers() []module.BasicManager {
return []module.BasicManager{
simapp.ModuleBasics,
stchainapp.ModuleBasics,
}
}

Expand All @@ -64,6 +64,7 @@ func getBasicManagers() []module.BasicManager {
// This should be edited by custom implementations if needed.
func getAddressesParser() messages.MessageAddressesParser {
return messages.JoinMessageParsers(
stchainMessageAddressesParser,
messages.CosmosMessageAddressesParser,
)
}
88 changes: 88 additions & 0 deletions cmd/bdjuno/stchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
junomessages "github.com/forbole/juno/v5/modules/messages"
pottypes "github.com/stratosnet/stratos-chain/x/pot/types"
registertypes "github.com/stratosnet/stratos-chain/x/register/types"
sdstypes "github.com/stratosnet/stratos-chain/x/sds/types"
)

// stchainMessageAddressesParser represents a parser able to get the addresses of the involved
// account from a stchain message
var stchainMessageAddressesParser = junomessages.JoinMessageParsers(
registerMessageAddressesParser,
potMessageAddressesParser,
sdsMessageAddressesParser,
)

// registerMessageAddressesParser represents a MessageAddressesParser for the x/register module
func registerMessageAddressesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {

case *registertypes.MsgCreateResourceNode:
return []string{msg.NetworkAddress, msg.OwnerAddress}, nil

case *registertypes.MsgCreateMetaNode:
return []string{msg.NetworkAddress, msg.OwnerAddress}, nil

case *registertypes.MsgRemoveResourceNode:
return []string{msg.ResourceNodeAddress, msg.OwnerAddress}, nil

case *registertypes.MsgRemoveMetaNode:
return []string{msg.MetaNodeAddress, msg.OwnerAddress}, nil

case *registertypes.MsgUpdateResourceNode:
return []string{msg.NetworkAddress, msg.OwnerAddress}, nil

case *registertypes.MsgUpdateMetaNode:
return []string{msg.NetworkAddress, msg.OwnerAddress}, nil

case *registertypes.MsgUpdateResourceNodeDeposit:
return []string{msg.NetworkAddress, msg.OwnerAddress}, nil

case *registertypes.MsgUpdateMetaNodeDeposit:
return []string{msg.NetworkAddress, msg.OwnerAddress}, nil

case *registertypes.MsgMetaNodeRegistrationVote:
return []string{msg.CandidateNetworkAddress, msg.CandidateOwnerAddress,
msg.VoterNetworkAddress, msg.VoterOwnerAddress}, nil
}

return nil, junomessages.MessageNotSupported(cosmosMsg)
}

// profilesMessageAddressesParser represents a MessageAddressesParser for the x/pot module
func potMessageAddressesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {

case *pottypes.MsgVolumeReport:
return []string{msg.Reporter, msg.ReporterOwner}, nil

case *pottypes.MsgWithdraw:
return []string{msg.WalletAddress, msg.TargetAddress}, nil

case *pottypes.MsgFoundationDeposit:
return []string{msg.From}, nil

case *pottypes.MsgSlashingResourceNode:
return []string{msg.NetworkAddress, msg.WalletAddress}, nil
}

return nil, junomessages.MessageNotSupported(cosmosMsg)
}

// sdsMessageAddressesParser represents a MessageAddressesParser for the x/sds module
func sdsMessageAddressesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {

case *sdstypes.MsgFileUpload:
return []string{msg.From, msg.Reporter, msg.Uploader}, nil

case *sdstypes.MsgPrepay:
return []string{msg.Sender}, nil
}

return nil, junomessages.MessageNotSupported(cosmosMsg)
}
4 changes: 3 additions & 1 deletion cmd/migrate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/spf13/cobra"

v3 "github.com/forbole/bdjuno/v4/cmd/migrate/v3"
v5 "github.com/forbole/bdjuno/v4/cmd/migrate/v5"
)

type Migrator func(parseCfg *parsecmdtypes.Config) error

var (
migrations = map[string]Migrator{
"v3": v3.RunMigration,
"v5": v5.RunMigration,
}
)

Expand All @@ -32,7 +34,7 @@ func NewMigrateCmd(appName string, parseConfig *parsecmdtypes.Config) *cobra.Com
Use: "migrate [to-version]",
Short: "Perform the migrations from the current version to the specified one",
Long: `Migrates all the necessary things (config file, database, etc) from the current version to the new one.
Note that migrations must be performed in order: to migrate from vX to vX+2 you need to do vX -> vX+1 and then vX+1 -> vX+2.
Note that migrations must be performed in order: to migrate from vX to vX+2 you need to do vX -> vX+1 and then vX+1 -> vX+2.
`,
Example: fmt.Sprintf("%s migrate v3", appName),
Args: cobra.RangeArgs(0, 1),
Expand Down
43 changes: 43 additions & 0 deletions cmd/migrate/v5/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package v5

import (
"fmt"

v5db "github.com/forbole/bdjuno/v4/database/migrate/v5"
parse "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/database"
"github.com/forbole/juno/v5/database/postgresql"
"github.com/forbole/juno/v5/types/config"
)

// RunMigration runs the migrations to v5
func RunMigration(parseConfig *parse.Config) error {
cfg, err := GetConfig()
if err != nil {
return fmt.Errorf("error while reading config: %s", err)
}

// Migrate the database
err = migrateDb(cfg, parseConfig)
if err != nil {
return fmt.Errorf("error while migrating database: %s", err)
}

return nil
}

func migrateDb(cfg config.Config, parseConfig *parse.Config) error {
// Build the codec
encodingConfig := parseConfig.GetEncodingConfigBuilder()()

// Get the db
databaseCtx := database.NewContext(cfg.Database, &encodingConfig, parseConfig.GetLogger())
db, err := postgresql.Builder(databaseCtx)
if err != nil {
return fmt.Errorf("error while building the db: %s", err)
}

// Build the migrator and perform the migrations
migrator := v5db.NewMigrator(db.(*postgresql.Database))
return migrator.Migrate()
}
29 changes: 29 additions & 0 deletions cmd/migrate/v5/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v5

import (
"fmt"
"os"
"path"

"github.com/forbole/juno/v5/types/config"
"gopkg.in/yaml.v3"
)

// GetConfig returns the configuration reading it from the config.yaml file present inside the home directory
func GetConfig() (config.Config, error) {
file := path.Join(config.HomePath, "config.yaml")

// Make sure the path exists
if _, err := os.Stat(file); os.IsNotExist(err) {
return config.Config{}, fmt.Errorf("config file does not exist")
}

bz, err := os.ReadFile(file)
if err != nil {
return config.Config{}, fmt.Errorf("error while reading config file: %s", err)
}

var cfg config.Config
err = yaml.Unmarshal(bz, &cfg)
return cfg, err
}
8 changes: 7 additions & 1 deletion cmd/parse/pricefeed/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pricefeed
import (
"fmt"

"github.com/forbole/bdjuno/v4/modules/types"
parsecmdtypes "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/types/config"
"github.com/spf13/cobra"
Expand All @@ -25,8 +26,13 @@ func priceCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
// Get the database
db := database.Cast(parseCtx.Database)

sources, err := types.BuildSources(config.Cfg.Node, parseCtx.EncodingConfig)
if err != nil {
panic(err)
}

// Build pricefeed module
pricefeedModule := pricefeed.NewModule(config.Cfg, parseCtx.EncodingConfig.Codec, db)
pricefeedModule := pricefeed.NewModule(config.Cfg, parseCtx.EncodingConfig.Codec, db, sources.PotSource)

err = pricefeedModule.RunAdditionalOperations()
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion cmd/parse/pricefeed/pricehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pricefeed
import (
"fmt"

"github.com/forbole/bdjuno/v4/modules/types"
parsecmdtypes "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/types/config"
"github.com/spf13/cobra"
Expand All @@ -25,8 +26,13 @@ func priceHistoryCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
// Get the database
db := database.Cast(parseCtx.Database)

sources, err := types.BuildSources(config.Cfg.Node, parseCtx.EncodingConfig)
if err != nil {
panic(err)
}

// Build pricefeed module
pricefeedModule := pricefeed.NewModule(config.Cfg, parseCtx.EncodingConfig.Codec, db)
pricefeedModule := pricefeed.NewModule(config.Cfg, parseCtx.EncodingConfig.Codec, db, sources.PotSource)

err = pricefeedModule.RunAdditionalOperations()
if err != nil {
Expand Down
Loading
Loading