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

Add separate notary fetcher #122

Merged
merged 1 commit into from
Mar 1, 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
28 changes: 17 additions & 11 deletions cmd/neo-exporter/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func sideChainJob(cfg *viper.Viper, neogoClient *pool.Pool, logger *zap.Logger)
return nil, fmt.Errorf("can't initialize side balance fetcher: %w", err)
}

notaryBalanceFetcher, err := monitor.NewNotaryFetcher(neogoClient)
if err != nil {
return nil, fmt.Errorf("can't initialize notary side balance fetcher: %w", err)
}

var (
balance util.Uint160
proxy *util.Uint160
Expand Down Expand Up @@ -181,16 +186,17 @@ func sideChainJob(cfg *viper.Viper, neogoClient *pool.Pool, logger *zap.Logger)
}

return monitor.NewSideJob(monitor.SideJobArgs{
Logger: logger,
Balance: balance,
Proxy: proxy,
AlphabetFetcher: alphabetFetcher,
NmFetcher: nmFetcher,
IRFetcher: nmFetcher,
BalanceFetcher: balanceFetcher,
CnrFetcher: cnrFetcher,
HeightFetcher: neogoClient,
StateFetcher: neogoClient,
Nep17tracker: nep17tracker,
Logger: logger,
Balance: balance,
Proxy: proxy,
AlphabetFetcher: alphabetFetcher,
NmFetcher: nmFetcher,
IRFetcher: nmFetcher,
BalanceFetcher: balanceFetcher,
NotaryBalanceFetcher: notaryBalanceFetcher,
CnrFetcher: cnrFetcher,
HeightFetcher: neogoClient,
StateFetcher: neogoClient,
Nep17tracker: nep17tracker,
}), nil
}
4 changes: 4 additions & 0 deletions pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type (
Symbol(tokenHash util.Uint160) (string, error)
}

NotaryBalanceFetcher interface {
FetchNotary(account util.Uint160) (float64, error)
}

AlphabetFetcher interface {
FetchAlphabet() (keys.PublicKeys, error)
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/monitor/notary_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package monitor

import (
"fmt"
"math"
"math/big"

"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
"github.com/nspcc-dev/neo-go/pkg/util"
)

type (
// NotaryFetcher allows to fetch notary balances from account.
NotaryFetcher struct {
cli nep17.Invoker
decimals *big.Float
}
)

const (
decimals = 8
)

// NewNotaryFetcher is a constructor for NotaryFetcher.
func NewNotaryFetcher(cli nep17.Invoker) (*NotaryFetcher, error) {
return &NotaryFetcher{
cli: cli,
decimals: big.NewFloat(math.Pow10(decimals)),
}, nil
}

func (b *NotaryFetcher) format(balance *big.Int) (float64, error) {
var bigFloat big.Float
bigFloat.SetInt(balance)
bigFloat.Quo(&bigFloat, b.decimals)

res, _ := bigFloat.Float64()

return res, nil
}

// FetchNotary returns the notary balance of the given account.
func (b *NotaryFetcher) FetchNotary(account util.Uint160) (float64, error) {
balance, err := notary.NewReader(b.cli).BalanceOf(account)
if err != nil {
return 0, fmt.Errorf("balanceOf: %w", err)
}

res, err := b.format(balance)
if err != nil {
return 0, fmt.Errorf("format: %w", err)
}

return res, nil
}
74 changes: 38 additions & 36 deletions pkg/monitor/side_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@ import (
"github.com/nspcc-dev/locode-db/pkg/locodedb"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)

type (
SideJobArgs struct {
Logger *zap.Logger
Balance util.Uint160
Proxy *util.Uint160
AlphabetFetcher AlphabetFetcher
NmFetcher NetmapFetcher
IRFetcher InnerRingFetcher
BalanceFetcher Nep17BalanceFetcher
CnrFetcher ContainerFetcher
HeightFetcher HeightFetcher
StateFetcher StateFetcher
Nep17tracker *Nep17tracker
Logger *zap.Logger
Balance util.Uint160
Proxy *util.Uint160
AlphabetFetcher AlphabetFetcher
NmFetcher NetmapFetcher
IRFetcher InnerRingFetcher
BalanceFetcher Nep17BalanceFetcher
NotaryBalanceFetcher NotaryBalanceFetcher
CnrFetcher ContainerFetcher
HeightFetcher HeightFetcher
StateFetcher StateFetcher
Nep17tracker *Nep17tracker
}

SideJob struct {
logger *zap.Logger
nmFetcher NetmapFetcher
irFetcher InnerRingFetcher
balanceFetcher Nep17BalanceFetcher
proxy *util.Uint160
cnrFetcher ContainerFetcher
heightFetcher HeightFetcher
stateFetcher StateFetcher
alphabetFetcher AlphabetFetcher
balance util.Uint160
nep17tracker *Nep17tracker
logger *zap.Logger
nmFetcher NetmapFetcher
irFetcher InnerRingFetcher
balanceFetcher Nep17BalanceFetcher
notaryBalanceFetcher NotaryBalanceFetcher
proxy *util.Uint160
cnrFetcher ContainerFetcher
heightFetcher HeightFetcher
stateFetcher StateFetcher
alphabetFetcher AlphabetFetcher
balance util.Uint160
nep17tracker *Nep17tracker
}

diffNode struct {
Expand Down Expand Up @@ -104,17 +105,18 @@ type (

func NewSideJob(args SideJobArgs) *SideJob {
return &SideJob{
logger: args.Logger,
nmFetcher: args.NmFetcher,
irFetcher: args.IRFetcher,
balanceFetcher: args.BalanceFetcher,
proxy: args.Proxy,
cnrFetcher: args.CnrFetcher,
heightFetcher: args.HeightFetcher,
stateFetcher: args.StateFetcher,
alphabetFetcher: args.AlphabetFetcher,
balance: args.Balance,
nep17tracker: args.Nep17tracker,
logger: args.Logger,
nmFetcher: args.NmFetcher,
irFetcher: args.IRFetcher,
balanceFetcher: args.BalanceFetcher,
notaryBalanceFetcher: args.NotaryBalanceFetcher,
proxy: args.Proxy,
cnrFetcher: args.CnrFetcher,
heightFetcher: args.HeightFetcher,
stateFetcher: args.StateFetcher,
alphabetFetcher: args.AlphabetFetcher,
balance: args.Balance,
nep17tracker: args.Nep17tracker,
}
}

Expand Down Expand Up @@ -203,7 +205,7 @@ func (m *SideJob) processNetworkMap(nm NetmapInfo, candidates NetmapCandidatesIn
exportCountries[nodeLoc]++
}

balanceNotary, err := m.balanceFetcher.Fetch(notary.Hash, scriptHash)
balanceNotary, err := m.notaryBalanceFetcher.FetchNotary(scriptHash)
if err != nil {
m.logger.Debug("can't fetch notary balance of node from the NeoFS network map",
zap.String("key", keyHex),
Expand Down Expand Up @@ -295,7 +297,7 @@ func (m *SideJob) processSideAlphabet(alphabet keys.PublicKeys) {
for _, key := range alphabet {
keyHex := hex.EncodeToString(key.Bytes())

balanceNotary, err := m.balanceFetcher.Fetch(notary.Hash, key.GetScriptHash())
balanceNotary, err := m.notaryBalanceFetcher.FetchNotary(key.GetScriptHash())
if err != nil {
m.logger.Debug("can't fetch notary balance of the NeoFS Alphabet member", zap.String("key", keyHex), zap.Error(err))
} else {
Expand Down
Loading