diff --git a/cmd/neo-exporter/monitor.go b/cmd/neo-exporter/monitor.go index 193406c..3847601 100644 --- a/cmd/neo-exporter/monitor.go +++ b/cmd/neo-exporter/monitor.go @@ -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 @@ -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 } diff --git a/pkg/monitor/monitor.go b/pkg/monitor/monitor.go index c0c2e08..e24edc6 100644 --- a/pkg/monitor/monitor.go +++ b/pkg/monitor/monitor.go @@ -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) } diff --git a/pkg/monitor/notary_fetcher.go b/pkg/monitor/notary_fetcher.go new file mode 100644 index 0000000..3da2df0 --- /dev/null +++ b/pkg/monitor/notary_fetcher.go @@ -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 +} diff --git a/pkg/monitor/side_job.go b/pkg/monitor/side_job.go index d9fbb69..e3e61fd 100644 --- a/pkg/monitor/side_job.go +++ b/pkg/monitor/side_job.go @@ -7,7 +7,6 @@ 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" @@ -15,31 +14,33 @@ import ( 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 { @@ -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, } } @@ -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), @@ -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 {