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

Notary contract invocation support #404

Merged
merged 9 commits into from
Mar 4, 2021
6 changes: 4 additions & 2 deletions cmd/neofs-ir/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,22 @@ func defaultConfiguration(cfg *viper.Viper) {
cfg.SetDefault("contracts.balance", "")
cfg.SetDefault("contracts.container", "")
cfg.SetDefault("contracts.audit", "")
cfg.SetDefault("contracts.proxy", "")
// alphabet contracts
cfg.SetDefault("contracts.alphabet.amount", 7)
// gas native contract in LE
cfg.SetDefault("contracts.gas", "70e2301955bf1e74cbb31d18c2f96972abadb328")

cfg.SetDefault("timers.epoch", "0")
cfg.SetDefault("timers.emit", "0")
cfg.SetDefault("timers.notary", "1000")
cfg.SetDefault("timers.stop_estimation.mul", 1)
cfg.SetDefault("timers.stop_estimation.div", 1)
cfg.SetDefault("timers.collect_basic_income.mul", 1)
cfg.SetDefault("timers.collect_basic_income.div", 1)
cfg.SetDefault("timers.distribute_basic_income.mul", 1)
cfg.SetDefault("timers.distribute_basic_income.div", 1)

cfg.SetDefault("notary.deposit_amount", 1_0000_0000) // 1.0 Fixed8

cfg.SetDefault("workers.netmap", "10")
cfg.SetDefault("workers.balance", "10")
cfg.SetDefault("workers.neofs", "10")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/multiformats/go-multiaddr-net v0.1.2 // v0.1.1 => v0.1.2
github.com/multiformats/go-multihash v0.0.13 // indirect
github.com/nspcc-dev/hrw v1.0.9
github.com/nspcc-dev/neo-go v0.93.0
github.com/nspcc-dev/neo-go v0.94.0-pre.0.20210301112733-3227de8050f8
github.com/nspcc-dev/neofs-api-go v1.24.0
github.com/nspcc-dev/neofs-crypto v0.3.0
github.com/nspcc-dev/tzhash v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y=
github.com/nspcc-dev/hrw v1.0.9/go.mod h1:l/W2vx83vMQo6aStyx2AuZrJ+07lGv2JQGlVkPG06MU=
github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg=
github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc=
github.com/nspcc-dev/neo-go v0.93.0 h1:B20D8z+/dByegXZoKpzLMkztsOtMozBGUnDjfVaI+zE=
github.com/nspcc-dev/neo-go v0.93.0/go.mod h1:6tixfAd+d8TIm05DA874j6t898G/fyqA2fHVJxkJCXQ=
github.com/nspcc-dev/neo-go v0.94.0-pre.0.20210301112733-3227de8050f8 h1:9oaPafI3aSteb6o5tRcaHLQGTUq9+iKFzyHLQ+fB7yg=
github.com/nspcc-dev/neo-go v0.94.0-pre.0.20210301112733-3227de8050f8/go.mod h1:6tixfAd+d8TIm05DA874j6t898G/fyqA2fHVJxkJCXQ=
github.com/nspcc-dev/neofs-api-go v1.24.0 h1:ZxROxsaCmIiv56sNHPf/FBeGHTT51fldp6uhDq0xwVE=
github.com/nspcc-dev/neofs-api-go v1.24.0/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
Expand Down
21 changes: 21 additions & 0 deletions pkg/innerring/blocktimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ type (

emitDuration uint32 // in blocks
}

notaryDepositArgs struct {
l *zap.Logger

depositor func() error

notaryDuration uint32 // in blocks
}
)

func (s *Server) addBlockTimer(t *timers.BlockTimer) {
Expand Down Expand Up @@ -130,3 +138,16 @@ func newEmissionTimer(args *emitTimerArgs) *timers.BlockTimer {
},
)
}

func newNotaryDepositTimer(args *notaryDepositArgs) *timers.BlockTimer {
return timers.NewBlockTimer(
timers.StaticBlockMeter(args.notaryDuration),
func() {
err := args.depositor()
if err != nil {
args.l.Warn("can't deposit notary contract",
zap.String("error", err.Error()))
}
},
)
}
94 changes: 85 additions & 9 deletions pkg/innerring/innerring.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type (
precision precision.Fixed8Converter
auditClient *auditWrapper.ClientWrapper

notaryDepositAmount fixedn.Fixed8
notaryDuration uint32

// internal variables
key *ecdsa.PrivateKey
pubKey []byte
Expand Down Expand Up @@ -88,7 +91,7 @@ type (
balance util.Uint160 // in morph
container util.Uint160 // in morph
audit util.Uint160 // in morph
gas util.Uint160 // native contract in both chains
proxy util.Uint160 // in morph

alphabet alphabetContracts // in morph
}
Expand All @@ -105,6 +108,16 @@ type (
const (
morphPrefix = "morph"
mainnetPrefix = "mainnet"

// extra blocks to overlap two deposits, we do that to make sure that
// there won't be any blocks without deposited assets in notary contract.
notaryExtraBlocks = 100
// amount of tries before notary deposit timeout.
notaryDepositTimeout = 100
)

var (
errDepositTimeout = errors.New("notary deposit didn't appeared in the network")
)

// Start runs all event providers.
Expand All @@ -120,6 +133,20 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) error {
return err
}

// make an initial deposit to notary contract to enable it
err = s.depositNotary()
if err != nil {
return err
}

// wait a bit for notary contract deposit
s.log.Info("waiting to accept notary deposit")

err = s.awaitNotaryDeposit(ctx)
if err != nil {
return err
}

// vote for sidechain validator if it is prepared in config
err = s.voteForSidechainValidator(s.predefinedValidators)
if err != nil {
Expand Down Expand Up @@ -222,7 +249,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
log: log,
cfg: cfg,
key: server.key,
gas: server.contracts.gas,
name: morphPrefix,
}

Expand All @@ -238,6 +264,14 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
return nil, err
}

err = server.morphClient.EnableNotarySupport(
server.contracts.proxy,
server.contracts.netmap,
)
if err != nil {
return nil, err
}

if cfg.GetBool("without_mainnet") {
// This works as long as event Listener starts listening loop once,
// otherwise Server.Start will run two similar routines.
Expand Down Expand Up @@ -526,6 +560,18 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error

server.addBlockTimer(emissionTimer)

// initialize notary deposit timer
server.notaryDepositAmount = fixedn.Fixed8(cfg.GetInt64("notary.deposit_amount"))
server.notaryDuration = cfg.GetUint32("timers.notary")

notaryTimer := newNotaryDepositTimer(&notaryDepositArgs{
l: log,
depositor: server.depositNotary,
notaryDuration: server.notaryDuration,
})

server.addBlockTimer(notaryTimer)

return server, nil
}

Expand Down Expand Up @@ -557,7 +603,6 @@ func createClient(ctx context.Context, p *chainParams) (*client.Client, error) {
client.WithContext(ctx),
client.WithLogger(p.log),
client.WithDialTimeout(p.cfg.GetDuration(p.name+".dial_timeouts")),
client.WithGasContract(p.gas),
)
}

Expand All @@ -570,9 +615,9 @@ func parseContracts(cfg *viper.Viper) (*contracts, error) {
netmapContractStr := cfg.GetString("contracts.netmap")
neofsContractStr := cfg.GetString("contracts.neofs")
balanceContractStr := cfg.GetString("contracts.balance")
nativeGasContractStr := cfg.GetString("contracts.gas")
containerContractStr := cfg.GetString("contracts.container")
auditContractStr := cfg.GetString("contracts.audit")
proxyContractStr := cfg.GetString("contracts.proxy")

result.netmap, err = util.Uint160DecodeStringLE(netmapContractStr)
if err != nil {
Expand All @@ -589,11 +634,6 @@ func parseContracts(cfg *viper.Viper) (*contracts, error) {
return nil, errors.Wrap(err, "ir: can't read balance script-hash")
}

result.gas, err = util.Uint160DecodeStringLE(nativeGasContractStr)
if err != nil {
return nil, errors.Wrap(err, "ir: can't read native gas script-hash")
}

result.container, err = util.Uint160DecodeStringLE(containerContractStr)
if err != nil {
return nil, errors.Wrap(err, "ir: can't read container script-hash")
Expand All @@ -604,6 +644,11 @@ func parseContracts(cfg *viper.Viper) (*contracts, error) {
return nil, errors.Wrap(err, "ir: can't read audit script-hash")
}

result.proxy, err = util.Uint160DecodeStringLE(proxyContractStr)
if err != nil {
return nil, errors.Wrap(err, "ir: can't read proxy script-hash")
}

result.alphabet, err = parseAlphabetContracts(cfg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -701,3 +746,34 @@ func (s *Server) onlyActiveEventHandler(f event.Handler) event.Handler {
}
}
}

func (s *Server) depositNotary() error {
return s.morphClient.DepositNotary(
s.notaryDepositAmount,
s.notaryDuration+notaryExtraBlocks,
)
}

func (s *Server) awaitNotaryDeposit(ctx context.Context) error {
for i := 0; i < notaryDepositTimeout; i++ {
select {
case <-ctx.Done():
return nil
default:
}

deposit, err := s.morphClient.GetNotaryDeposit()
if err != nil {
return errors.Wrap(err, "can't get notary deposit")
}

if deposit > 0 {
return nil
}

s.log.Info("empty notary deposit, waiting one more block")
s.morphClient.Wait(ctx, 1)
}

return errDepositTimeout
}
2 changes: 1 addition & 1 deletion pkg/innerring/invoke/alphabet.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func AlphabetVote(cli *client.Client, con util.Uint160, epoch uint64, keys []key
binaryKeys = append(binaryKeys, keys[i].Bytes())
}

return cli.Invoke(con, extraFee, voteMethod, int64(epoch), binaryKeys)
return cli.NotaryInvoke(con, voteMethod, int64(epoch), binaryKeys)
}
6 changes: 3 additions & 3 deletions pkg/innerring/invoke/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Mint(cli *client.Client, con util.Uint160, p *MintBurnParams) error {
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, mintMethod,
return cli.NotaryInvoke(con, mintMethod,
p.ScriptHash,
p.Amount,
p.Comment,
Expand All @@ -50,7 +50,7 @@ func Burn(cli *client.Client, con util.Uint160, p *MintBurnParams) error {
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, burnMethod,
return cli.NotaryInvoke(con, burnMethod,
p.ScriptHash,
p.Amount,
p.Comment,
Expand All @@ -63,7 +63,7 @@ func LockAsset(cli *client.Client, con util.Uint160, p *LockParams) error {
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, lockMethod,
return cli.NotaryInvoke(con, lockMethod,
p.ID,
p.User.BytesBE(),
p.LockAccount.BytesBE(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/innerring/invoke/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RegisterContainer(cli *client.Client, con util.Uint160, p *ContainerParams)
return client.ErrNilClient
}

return cli.Invoke(con, 3*extraFee, putContainerMethod,
return cli.NotaryInvoke(con, putContainerMethod,
p.Container,
p.Signature,
p.Key.Bytes(),
Expand All @@ -45,7 +45,7 @@ func RemoveContainer(cli *client.Client, con util.Uint160, p *RemoveContainerPar
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, deleteContainerMethod,
return cli.NotaryInvoke(con, deleteContainerMethod,
p.ContainerID,
p.Signature,
)
Expand Down
10 changes: 5 additions & 5 deletions pkg/innerring/invoke/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func SetNewEpoch(cli *client.Client, con util.Uint160, epoch uint64) error {
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, setNewEpochMethod, int64(epoch))
return cli.NotaryInvoke(con, setNewEpochMethod, int64(epoch))
}

// ApprovePeer invokes addPeer method.
Expand All @@ -66,7 +66,7 @@ func ApprovePeer(cli *client.Client, con util.Uint160, peer []byte) error {
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, approvePeerMethod, peer)
return cli.NotaryInvoke(con, approvePeerMethod, peer)
}

// UpdatePeerState invokes addPeer method.
Expand All @@ -75,7 +75,7 @@ func UpdatePeerState(cli *client.Client, con util.Uint160, args *UpdatePeerArgs)
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, updatePeerStateMethod,
return cli.NotaryInvoke(con, updatePeerStateMethod,
int64(args.Status.ToV2()),
args.Key.Bytes(),
)
Expand All @@ -87,7 +87,7 @@ func SetConfig(cli *client.Client, con util.Uint160, args *SetConfigArgs) error
return client.ErrNilClient
}

return cli.Invoke(con, extraFee, setConfigMethod,
return cli.NotaryInvoke(con, setConfigMethod,
args.ID,
args.Key,
args.Value,
Expand All @@ -105,7 +105,7 @@ func UpdateInnerRing(cli *client.Client, con util.Uint160, list []*keys.PublicKe
rawKeys = append(rawKeys, list[i].Bytes())
}

return cli.Invoke(con, extraFee, updateInnerRingMethod, rawKeys)
return cli.NotaryInvoke(con, updateInnerRingMethod, rawKeys)
}

// NetmapSnapshot returns current netmap node infos.
Expand Down
2 changes: 1 addition & 1 deletion pkg/innerring/settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (s settlementDeps) transfer(sender, recipient *owner.ID, amount *big.Int, d
return
}

if err := s.balanceClient.TransferX(balanceClient.TransferPrm{
if err := s.balanceClient.TransferXNotary(balanceClient.TransferPrm{
Amount: amount.Int64(),
From: sender,
To: recipient,
Expand Down
19 changes: 17 additions & 2 deletions pkg/morph/client/balance/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,25 @@ func (t *TransferXArgs) SetDetails(v []byte) {
t.details = v
}

// TransferX invokes the call of "transferX" method
// TransferX directly invokes the call of "transferX" method
// of NeoFS Balance contract.
func (c *Client) TransferX(args TransferXArgs) error {
return errors.Wrapf(c.client.Invoke(
return c.transferX(false, args)
}

// TransferXNotary invokes the call of "transferX" method
// of NeoFS Balance contract via notary contract.
func (c *Client) TransferXNotary(args TransferXArgs) error {
return c.transferX(true, args)
}

func (c *Client) transferX(notary bool, args TransferXArgs) error {
f := c.client.Invoke
if notary {
f = c.client.NotaryInvoke
}

return errors.Wrapf(f(
c.transferXMethod,
args.sender,
args.recipient,
Expand Down
Loading