2021-05-04 08:50:13 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-11 17:09:52 +00:00
|
|
|
"fmt"
|
2021-05-04 08:50:13 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
2021-05-04 08:50:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-11-01 15:55:25 +00:00
|
|
|
"go.uber.org/zap"
|
2021-05-04 08:50:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
notaryConfig struct {
|
2021-10-12 13:56:16 +00:00
|
|
|
disabled bool // true if notary disabled on chain
|
2021-05-04 08:50:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-11 17:09:52 +00:00
|
|
|
const (
|
|
|
|
// gasMultiplier defines how many times more the notary
|
|
|
|
// balance must be compared to the GAS balance of the IR:
|
2022-10-17 12:03:55 +00:00
|
|
|
// notaryBalance = GASBalance * gasMultiplier.
|
2021-10-11 17:09:52 +00:00
|
|
|
gasMultiplier = 3
|
|
|
|
|
|
|
|
// gasDivisor defines what part of GAS balance (1/gasDivisor)
|
2022-10-17 12:03:55 +00:00
|
|
|
// should be transferred to the notary service.
|
2021-10-11 17:09:52 +00:00
|
|
|
gasDivisor = 2
|
|
|
|
)
|
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
func (s *Server) depositMainNotary() (tx util.Uint256, err error) {
|
2021-10-11 17:09:52 +00:00
|
|
|
depositAmount, err := client.CalculateNotaryDepositAmount(s.mainnetClient, gasMultiplier, gasDivisor)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("could not calculate main notary deposit amount: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
return s.mainnetClient.DepositNotary(
|
2021-10-11 17:09:52 +00:00
|
|
|
depositAmount,
|
|
|
|
uint32(s.epochDuration.Load())+notaryExtraBlocks,
|
2021-05-04 08:50:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-31 10:13:03 +00:00
|
|
|
func (s *Server) depositSideNotary() (util.Uint256, error) {
|
2021-10-11 17:09:52 +00:00
|
|
|
depositAmount, err := client.CalculateNotaryDepositAmount(s.morphClient, gasMultiplier, gasDivisor)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("could not calculate side notary deposit amount: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-31 10:13:03 +00:00
|
|
|
tx, _, err := s.morphClient.DepositEndlessNotary(depositAmount)
|
|
|
|
return tx, err
|
2021-05-04 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:55:25 +00:00
|
|
|
func (s *Server) notaryHandler(_ event.Event) {
|
|
|
|
if !s.mainNotaryConfig.disabled {
|
|
|
|
_, err := s.depositMainNotary()
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Error(context.Background(), logs.InnerringCantMakeNotaryDepositInMainChain, zap.Error(err))
|
2021-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 14:56:11 +00:00
|
|
|
if _, err := s.depositSideNotary(); err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Error(context.Background(), logs.InnerringCantMakeNotaryDepositInSideChain, zap.Error(err))
|
2021-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
func (s *Server) awaitMainNotaryDeposit(ctx context.Context, tx util.Uint256) error {
|
|
|
|
return awaitNotaryDepositInClient(ctx, s.mainnetClient, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) awaitSideNotaryDeposit(ctx context.Context, tx util.Uint256) error {
|
|
|
|
return awaitNotaryDepositInClient(ctx, s.morphClient, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initNotary(ctx context.Context, deposit depositor, await awaiter, msg string) error {
|
|
|
|
tx, err := deposit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-28 15:46:25 +00:00
|
|
|
if tx.Equals(util.Uint256{}) {
|
|
|
|
// non-error deposit with an empty TX hash means
|
|
|
|
// that the deposit has already been made; no
|
|
|
|
// need to wait it.
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Info(ctx, logs.InnerringNotaryDepositHasAlreadyBeenMade)
|
2023-03-28 15:46:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Info(ctx, msg)
|
2021-05-04 08:50:13 +00:00
|
|
|
|
|
|
|
return await(ctx, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func awaitNotaryDepositInClient(ctx context.Context, cli *client.Client, txHash util.Uint256) error {
|
2024-08-30 16:20:55 +00:00
|
|
|
for range notaryDepositTimeout {
|
2021-05-04 08:50:13 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-03-28 14:58:28 +00:00
|
|
|
return ctx.Err()
|
2021-05-04 08:50:13 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := cli.TxHalt(txHash)
|
|
|
|
if err == nil {
|
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errDepositFail
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:14:45 +00:00
|
|
|
_ = cli.Wait(ctx, 1)
|
2021-05-04 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errDepositTimeout
|
|
|
|
}
|
|
|
|
|
2023-05-17 14:56:11 +00:00
|
|
|
func notaryConfigs(withMainNotary bool) (main *notaryConfig) {
|
2021-05-04 08:50:13 +00:00
|
|
|
main = new(notaryConfig)
|
|
|
|
|
2021-08-03 17:51:02 +00:00
|
|
|
main.disabled = !withMainNotary
|
2021-05-04 08:50:13 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|