Skip to content

feat(target flag): target mask config to separate networks commands #137

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

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions cmd/discord/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func runCommand(parentCmd *cobra.Command) {
run := &cobra.Command{
Use: "run",
Short: "Runs a mainnet instance of Pagu",
Short: "Runs an instance of Pagu",
}

parentCmd.AddCommand(run)
Expand All @@ -37,7 +37,7 @@ func runCommand(parentCmd *cobra.Command) {
botEngine.RegisterAllCommands()
botEngine.Start()

discordBot, err := discord.NewDiscordBot(botEngine, configs.DiscordBot.Token, configs.DiscordBot)
discordBot, err := discord.NewDiscordBot(botEngine, configs.DiscordBot, configs.TargetMask)
pCmd.ExitOnError(cmd, err)

err = discordBot.Start()
Expand Down
42 changes: 15 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
)

type Config struct {
Network string `yaml:"network"`
NetworkNodes []string `yaml:"network_nodes"`
LocalNode string `yaml:"local_node"`
Database Database `yaml:"database"`
AuthIDs []string `yaml:"auth_ids"`
DiscordBot DiscordBot `yaml:"discord"`
GRPC GRPC `yaml:"grpc"`
Wallet Wallet `yaml:"main_net_wallet"`
TestNetWallet Wallet `yaml:"test_net_wallet"`
Logger Logger `yaml:"logger"`
HTTP HTTP `yaml:"http"`
Phoenix PhoenixNetwork `yaml:"phoenix"`
Telegram Telegram `yaml:"telegram"`
Network string `yaml:"network"`
NetworkNodes []string `yaml:"network_nodes"`
LocalNode string `yaml:"local_node"`
Database Database `yaml:"database"`
AuthIDs []string `yaml:"auth_ids"`
GRPC GRPC `yaml:"grpc"`
Wallet Wallet `yaml:"wallet"`
Logger Logger `yaml:"logger"`
HTTP HTTP `yaml:"http"`
Phoenix PhoenixNetwork `yaml:"phoenix"`
DiscordBot DiscordBot `yaml:"discord"`
Telegram Telegram `yaml:"telegram"`
TargetMask int `yaml:"target_mask"`
}

type Database struct {
Expand Down Expand Up @@ -50,8 +50,7 @@ type HTTP struct {
}

type PhoenixNetwork struct {
NetworkNodes []string `yaml:"network_nodes"`
FaucetAmount uint `yaml:"faucet_amount"`
FaucetAmount uint `yaml:"faucet_amount"`
}

type Logger struct {
Expand Down Expand Up @@ -101,18 +100,7 @@ func (cfg *Config) BasicCheck() error {
}
}

if cfg.TestNetWallet.Enable {
if cfg.TestNetWallet.Address == "" {
return fmt.Errorf("config: basic check error: TESTNET_WALLET_ADDRESS dose not set")
}

// Check if the WalletPath exists.
if !util.PathExists(cfg.TestNetWallet.Path) {
return fmt.Errorf("config: basic check error: TESTNET_WALLET_PATH does not exist: %s", cfg.TestNetWallet.Path)
}
}

if len(cfg.NetworkNodes) == 0 || len(cfg.Phoenix.NetworkNodes) == 0 {
if len(cfg.NetworkNodes) == 0 {
return fmt.Errorf("config: basic check error: NETWORK_NODES is not set or incorrect")
}

Expand Down
4 changes: 1 addition & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func TestBasicCheck(t *testing.T) {
Token: "MTEabc123",
GuildID: "123456789",
},
Phoenix: PhoenixNetwork{
NetworkNodes: []string{""},
},
Phoenix: PhoenixNetwork{},
},
wantErr: false,
},
Expand Down
8 changes: 8 additions & 0 deletions config/global.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package config

var (
TargetMaskMain = 1
TargetMaskTest = 2
TargetMaskModerator = 4

TargetMaskAll = TargetMaskMain | TargetMaskTest | TargetMaskModerator
)

const (
PriceCacheKey = "PriceCacheKey"
)
Expand Down
11 changes: 6 additions & 5 deletions internal/engine/command/calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const (
)

type Calculator struct {
clientMgr *client.Mgr
clientMgr *client.Mgr
targetMask int
}

func NewCalculator(
clientMgr *client.Mgr,
) Calculator {
func NewCalculator(clientMgr *client.Mgr, targetMask int) Calculator {
return Calculator{
clientMgr: clientMgr,
clientMgr: clientMgr,
targetMask: targetMask,
}
}

Expand Down Expand Up @@ -71,6 +71,7 @@ func (bc *Calculator) GetCommand() command.Command {
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
TargetMask: bc.targetMask,
}

cmdBlockchain.AddSubCommand(subCmdCalcReward)
Expand Down
1 change: 1 addition & 0 deletions internal/engine/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Command struct {
Middlewares []MiddlewareFunc
Handler HandlerFunc
User *entity.User
TargetMask int
}

type CommandResult struct {
Expand Down
5 changes: 4 additions & 1 deletion internal/engine/command/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const (
type Market struct {
clientMgr *client.Mgr
priceCache cache.Cache[string, entity.Price]
targetMask int
}

func NewMarket(clientMgr *client.Mgr, priceCache cache.Cache[string, entity.Price]) Market {
func NewMarket(clientMgr *client.Mgr, priceCache cache.Cache[string, entity.Price], target int) Market {
return Market{
clientMgr: clientMgr,
priceCache: priceCache,
targetMask: target,
}
}

Expand All @@ -44,6 +46,7 @@ func (m *Market) GetCommand() command.Command {
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
TargetMask: m.targetMask,
}

cmdMarket.AddSubCommand(subCmdPrice)
Expand Down
5 changes: 4 additions & 1 deletion internal/engine/command/market/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
"time"

"github.com/pagu-project/Pagu/config"

"github.com/pagu-project/Pagu/internal/engine/command"
"github.com/pagu-project/Pagu/internal/entity"
"github.com/pagu-project/Pagu/internal/job"
Expand All @@ -17,7 +19,7 @@ func setup() (Market, command.Command) {
priceJobSched := job.NewScheduler()
priceJobSched.Submit(priceJob)
go priceJobSched.Run()
m := NewMarket(nil, priceCache)
m := NewMarket(nil, priceCache, config.TargetMaskMain)

return m, command.Command{
Name: PriceCommandName,
Expand All @@ -26,6 +28,7 @@ func setup() (Market, command.Command) {
Args: []command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
TargetMask: m.targetMask,
}
}

Expand Down
15 changes: 8 additions & 7 deletions internal/engine/command/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ const (
)

type Network struct {
ctx context.Context
clientMgr *client.Mgr
ctx context.Context
clientMgr *client.Mgr
targetMask int
}

func NewNetwork(ctx context.Context,
clientMgr *client.Mgr,
) Network {
func NewNetwork(ctx context.Context, clientMgr *client.Mgr, target int) Network {
return Network{
ctx: ctx,
clientMgr: clientMgr,
ctx: ctx,
clientMgr: clientMgr,
targetMask: target,
}
}

Expand Down Expand Up @@ -106,6 +106,7 @@ func (n *Network) GetCommand() command.Command {
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
TargetMask: n.targetMask,
}

cmdNetwork.AddSubCommand(subCmdHealth)
Expand Down
10 changes: 5 additions & 5 deletions internal/engine/command/network/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
utils2 "github.com/pagu-project/Pagu/pkg/utils"
)

func (be *Network) networkStatusHandler(cmd command.Command, _ entity.AppID, _ string, _ ...string) command.CommandResult {
netInfo, err := be.clientMgr.GetNetworkInfo()
func (n *Network) networkStatusHandler(cmd command.Command, _ entity.AppID, _ string, _ ...string) command.CommandResult {
netInfo, err := n.clientMgr.GetNetworkInfo()
if err != nil {
return cmd.ErrorResult(err)
}

chainInfo, err := be.clientMgr.GetBlockchainInfo()
chainInfo, err := n.clientMgr.GetBlockchainInfo()
if err != nil {
return cmd.ErrorResult(err)
}

cs, err := be.clientMgr.GetCirculatingSupply()
cs, err := n.clientMgr.GetCirculatingSupply()
if err != nil {
cs = 0
}
Expand All @@ -40,7 +40,7 @@ func (be *Network) networkStatusHandler(cmd command.Command, _ entity.AppID, _ s

return cmd.SuccessfulResult("Network Name: %s\nConnected Peers: %v\n"+
"Validators Count: %v\nAccounts Count: %v\nCurrent Block Height: %v\nTotal Power: %v PAC\nTotal Committee Power: %v PAC\nCirculating Supply: %v PAC\n"+
"\n> Note📝: This info is from one random network node. Non-calculator data may not be consistent.",
"\n> Note📝: This info is from one random network node. Non-calculator data may not n consistent.",
net.NetworkName,
utils2.FormatNumber(int64(net.ConnectedPeersCount)),
utils2.FormatNumber(int64(net.ValidatorsCount)),
Expand Down
6 changes: 4 additions & 2 deletions internal/engine/command/phoenix/phoenix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ type Phoenix struct {
db repository.DB
clientMgr *client.Mgr
faucetAmount uint
targetMask int
}

func NewPhoenix(wallet *wallet.Wallet, faucetAmount uint, clientMgr *client.Mgr, db repository.DB,
) Phoenix {
func NewPhoenix(wallet *wallet.Wallet, faucetAmount uint, clientMgr *client.Mgr, db repository.DB, target int) Phoenix {
return Phoenix{
wallet: wallet,
clientMgr: clientMgr,
db: db,
faucetAmount: faucetAmount,
targetMask: target,
}
}

Expand Down Expand Up @@ -76,6 +77,7 @@ func (pt *Phoenix) GetCommand() command.Command {
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
TargetMask: pt.targetMask,
}

cmdPhoenix.AddSubCommand(subCmdFaucet)
Expand Down
5 changes: 4 additions & 1 deletion internal/engine/command/voucher/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ type Voucher struct {
db *repository.DB
wallet *wallet.Wallet
clientManager *client.Mgr
targetMask int
}

func NewVoucher(db *repository.DB, wallet *wallet.Wallet, cli *client.Mgr) Voucher {
func NewVoucher(db *repository.DB, wallet *wallet.Wallet, cli *client.Mgr, target int) Voucher {
return Voucher{
db: db,
wallet: wallet,
clientManager: cli,
targetMask: target,
}
}

Expand Down Expand Up @@ -58,6 +60,7 @@ func (v *Voucher) GetCommand() command.Command {
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
TargetMask: v.targetMask,
}

cmdVoucher.AddSubCommand(subCmdClaim)
Expand Down
15 changes: 8 additions & 7 deletions internal/engine/command/zealy/zealy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const (
)

type Zealy struct {
db *repository.DB
wallet *wallet.Wallet
db *repository.DB
wallet *wallet.Wallet
targetMask int
}

func NewZealy(
db *repository.DB, wallet *wallet.Wallet,
) Zealy {
func NewZealy(db *repository.DB, wallet *wallet.Wallet, target int) Zealy {
return Zealy{
db: db,
wallet: wallet,
db: db,
wallet: wallet,
targetMask: target,
}
}

Expand Down Expand Up @@ -63,6 +63,7 @@ func (z *Zealy) GetCommand() command.Command {
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
TargetMask: z.targetMask,
}

cmdZealy.AddSubCommand(subCmdClaim)
Expand Down
Loading
Loading