Skip to content

Commit

Permalink
fix: failing tests (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jan 31, 2024
1 parent 67b7cda commit 5f4f515
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 129 deletions.
118 changes: 40 additions & 78 deletions client/client_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import (
)

type Mgr struct {
clients map[string]IClient
clients []IClient
}

func NewClientMgr() *Mgr {
return &Mgr{
clients: make(map[string]IClient),
clients: make([]IClient, 0),
}
}

func (cm *Mgr) AddClient(addr string, c IClient) {
cm.clients[addr] = c
func (cm *Mgr) AddClient(c IClient) {
cm.clients = append(cm.clients, c)
}

// NOTE: local client is always the first client.
func (cm *Mgr) getLocalClient() IClient {
return cm.clients[0]
}

func (cm *Mgr) GetRandomClient() IClient {
Expand All @@ -30,52 +35,35 @@ func (cm *Mgr) GetRandomClient() IClient {
}

func (cm *Mgr) GetBlockchainInfo() (*pactus.GetBlockchainInfoResponse, error) {
for _, c := range cm.clients {
info, err := c.GetBlockchainInfo()
if err != nil {
continue
}
return info, nil
localClient := cm.getLocalClient()
info, err := localClient.GetBlockchainInfo()
if err != nil {
return nil, err
}

return nil, errors.New("unable to get blockchain info")
return info, nil
}

func (cm *Mgr) GetBlockchainHeight() (uint32, error) {
for _, c := range cm.clients {
height, err := c.GetBlockchainHeight()
if err != nil {
continue
}
return height, nil
localClient := cm.getLocalClient()
height, err := localClient.GetBlockchainHeight()
if err != nil {
return 0, err
}

return 0, errors.New("unable to get blockchain height")
return height, nil
}

func (cm *Mgr) GetLastBlockTime() (uint32, uint32) {
var lastBlockTime uint32 = 0
var lastBlockHeight uint32 = 0
for _, c := range cm.clients {
t, h, err := c.LastBlockTime()
if err != nil {
continue
}
if t > lastBlockTime {
lastBlockTime = t
lastBlockHeight = h
}
localClient := cm.getLocalClient()
lastBlockTime, lastBlockHeight, err := localClient.LastBlockTime()
if err != nil {
return 0, 0
}

return lastBlockTime, lastBlockHeight
}

func (cm *Mgr) GetNetworkInfo() (*pactus.GetNetworkInfoResponse, error) {
for name, c := range cm.clients {
if name == "local-node" {
continue
}

for _, c := range cm.clients {
info, err := c.GetNetworkInfo()
if err != nil {
continue
Expand All @@ -87,11 +75,7 @@ func (cm *Mgr) GetNetworkInfo() (*pactus.GetNetworkInfoResponse, error) {
}

func (cm *Mgr) GetPeerInfoFirstVal(address string) (*pactus.PeerInfo, error) {
for name, c := range cm.clients {
if name == "local-node" {
continue
}

for _, c := range cm.clients {
networkInfo, err := c.GetNetworkInfo()
if err != nil {
continue
Expand Down Expand Up @@ -135,53 +119,31 @@ func (cm *Mgr) GetPeerInfo(address string) (*pactus.PeerInfo, error) {
return nil, errors.New("peer does not exist")
}

func (cm *Mgr) IsStakedValidator(address string) bool {
c, ok := cm.clients["local-node"]
if ok {
val, err := c.GetValidatorInfo(address)
if err != nil {
return false
}
return val.Validator.Stake > 0
}

return false
}

func (cm *Mgr) GetValidatorInfo(address string) (*pactus.GetValidatorResponse, error) {
for _, c := range cm.clients {
val, err := c.GetValidatorInfo(address)
if err != nil {
continue
}
return val, nil
localClient := cm.getLocalClient()
val, err := localClient.GetValidatorInfo(address)
if err != nil {
return nil, err
}

return nil, errors.New("unable to get validator info")
return val, nil
}

func (cm *Mgr) GetValidatorInfoByNumber(num int32) (*pactus.GetValidatorResponse, error) {
for _, c := range cm.clients {
val, err := c.GetValidatorInfoByNumber(num)
if err != nil {
continue
}
return val, nil
localClient := cm.getLocalClient()
val, err := localClient.GetValidatorInfoByNumber(num)
if err != nil {
return nil, err
}

return nil, errors.New("unable to get validator info")
return val, nil
}

func (cm *Mgr) GetTransactionData(txID string) (*pactus.GetTransactionResponse, error) {
for _, c := range cm.clients {
txData, err := c.GetTransactionData(txID)
if err != nil {
continue
}
return txData, nil
localClient := cm.getLocalClient()
txData, err := localClient.GetTransactionData(txID)
if err != nil {
return nil, err
}

return nil, errors.New("unable to get transaction data")
return txData, nil
}

func (cm *Mgr) Stop() {
Expand Down
10 changes: 5 additions & 5 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ type BotEngine struct {
func NewBotEngine(cfg *config.Config) (IEngine, error) {
cm := client.NewClientMgr()

c, err := client.NewClient(cfg.LocalNode)
localClient, err := client.NewClient(cfg.LocalNode)
if err != nil {
log.Error("can't make a new local client", "err", err, "addr", cfg.LocalNode)
return nil, err
}

cm.AddClient("local-node", c)
cm.AddClient(localClient)

for _, nn := range cfg.NetworkNodes {
c, err := client.NewClient(nn)
if err != nil {
log.Error("can't add new network node client", "err", err, "addr", nn)
}
cm.AddClient("client", c)
cm.AddClient(c)
}

// initializing logger global instance.
Expand Down Expand Up @@ -181,8 +181,8 @@ func (be *BotEngine) Claim(discordID string, testnetAddr string, mainnetAddr str

be.logger.Info("new claim request", "mainnetAddr", mainnetAddr, "testnetAddr", testnetAddr, "discordID", discordID)

isValidator := be.Cm.IsStakedValidator(mainnetAddr)
if isValidator {
valInfo, _ := be.Cm.GetValidatorInfo(mainnetAddr)
if valInfo != nil {
return "", errors.New("this address is already a staked validator")
}

Expand Down
Loading

0 comments on commit 5f4f515

Please sign in to comment.