forked from TrueCloudLab/frostfs-node
[#141] Fix double sidechain GAS emission on asset mint
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
0c49c08609
commit
97077294fc
5 changed files with 89 additions and 41 deletions
|
@ -85,4 +85,7 @@ func defaultConfiguration(cfg *viper.Viper) {
|
||||||
cfg.SetDefault("netmap_cleaner.threshold", 3)
|
cfg.SetDefault("netmap_cleaner.threshold", 3)
|
||||||
|
|
||||||
cfg.SetDefault("emit.storage.amount", 0)
|
cfg.SetDefault("emit.storage.amount", 0)
|
||||||
|
cfg.SetDefault("emit.mint.cache_size", 1000)
|
||||||
|
cfg.SetDefault("emit.mint.threshold", 1)
|
||||||
|
cfg.SetDefault("emit.mint.value", 20000000) // 0.2 Fixed8
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -7,6 +7,7 @@ require (
|
||||||
github.com/alecthomas/participle v0.6.0
|
github.com/alecthomas/participle v0.6.0
|
||||||
github.com/golang/protobuf v1.4.3
|
github.com/golang/protobuf v1.4.3
|
||||||
github.com/google/uuid v1.1.1
|
github.com/google/uuid v1.1.1
|
||||||
|
github.com/hashicorp/golang-lru v0.5.4
|
||||||
github.com/mitchellh/go-homedir v1.1.0
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
github.com/mr-tron/base58 v1.1.3
|
github.com/mr-tron/base58 v1.1.3
|
||||||
github.com/multiformats/go-multiaddr v0.2.0
|
github.com/multiformats/go-multiaddr v0.2.0
|
||||||
|
|
|
@ -231,15 +231,18 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
|
||||||
|
|
||||||
// create mainnnet neofs processor
|
// create mainnnet neofs processor
|
||||||
neofsProcessor, err := neofs.New(&neofs.Params{
|
neofsProcessor, err := neofs.New(&neofs.Params{
|
||||||
Log: log,
|
Log: log,
|
||||||
PoolSize: cfg.GetInt("workers.neofs"),
|
PoolSize: cfg.GetInt("workers.neofs"),
|
||||||
NeoFSContract: server.contracts.neofs,
|
NeoFSContract: server.contracts.neofs,
|
||||||
BalanceContract: server.contracts.balance,
|
BalanceContract: server.contracts.balance,
|
||||||
NetmapContract: server.contracts.netmap,
|
NetmapContract: server.contracts.netmap,
|
||||||
MorphClient: server.morphClient,
|
MorphClient: server.morphClient,
|
||||||
EpochState: server,
|
EpochState: server,
|
||||||
ActiveState: server,
|
ActiveState: server,
|
||||||
Converter: &server.precision,
|
Converter: &server.precision,
|
||||||
|
MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"),
|
||||||
|
MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"),
|
||||||
|
MintEmitValue: util.Fixed8(cfg.GetInt64("emit.mint.value")),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -34,12 +34,34 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) {
|
||||||
np.log.Error("can't transfer assets to balance contract", zap.Error(err))
|
np.log.Error("can't transfer assets to balance contract", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// fixme: send gas when ^ tx accepted
|
curEpoch := np.epochState.EpochCounter()
|
||||||
// fixme: do not send gas to the same user twice per epoch
|
receiver := deposit.To()
|
||||||
err = np.morphClient.TransferGas(deposit.To(), util.Fixed8FromInt64(2))
|
|
||||||
if err != nil {
|
// check if receiver already received some mint GAS emissions
|
||||||
np.log.Error("can't transfer native gas to receiver", zap.Error(err))
|
// we should lock there even though LRU cache is already thread save
|
||||||
|
// we lock there because GAS transfer AND cache update must be atomic
|
||||||
|
np.mintEmitLock.Lock()
|
||||||
|
defer np.mintEmitLock.Unlock()
|
||||||
|
|
||||||
|
val, ok := np.mintEmitCache.Get(receiver.String())
|
||||||
|
if ok && val.(uint64)+np.mintEmitThreshold >= curEpoch {
|
||||||
|
np.log.Warn("double mint emission declined",
|
||||||
|
zap.String("receiver", receiver.String()),
|
||||||
|
zap.Uint64("last_emission", val.(uint64)),
|
||||||
|
zap.Uint64("current_epoch", curEpoch))
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = np.morphClient.TransferGas(receiver, np.mintEmitValue)
|
||||||
|
if err != nil {
|
||||||
|
np.log.Error("can't transfer native gas to receiver",
|
||||||
|
zap.String("error", err.Error()))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
np.mintEmitCache.Add(receiver.String(), curEpoch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process withdraw event by locking assets in balance account.
|
// Process withdraw event by locking assets in balance account.
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package neofs
|
package neofs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
lru "github.com/hashicorp/golang-lru"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
||||||
|
@ -28,28 +31,35 @@ type (
|
||||||
|
|
||||||
// Processor of events produced by neofs contract in main net.
|
// Processor of events produced by neofs contract in main net.
|
||||||
Processor struct {
|
Processor struct {
|
||||||
log *zap.Logger
|
log *zap.Logger
|
||||||
pool *ants.Pool
|
pool *ants.Pool
|
||||||
neofsContract util.Uint160
|
neofsContract util.Uint160
|
||||||
balanceContract util.Uint160
|
balanceContract util.Uint160
|
||||||
netmapContract util.Uint160
|
netmapContract util.Uint160
|
||||||
morphClient *client.Client
|
morphClient *client.Client
|
||||||
epochState EpochState
|
epochState EpochState
|
||||||
activeState ActiveState
|
activeState ActiveState
|
||||||
converter PrecisionConverter
|
converter PrecisionConverter
|
||||||
|
mintEmitLock *sync.Mutex
|
||||||
|
mintEmitCache *lru.Cache
|
||||||
|
mintEmitThreshold uint64
|
||||||
|
mintEmitValue util.Fixed8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Params of the processor constructor.
|
// Params of the processor constructor.
|
||||||
Params struct {
|
Params struct {
|
||||||
Log *zap.Logger
|
Log *zap.Logger
|
||||||
PoolSize int
|
PoolSize int
|
||||||
NeoFSContract util.Uint160
|
NeoFSContract util.Uint160
|
||||||
BalanceContract util.Uint160
|
BalanceContract util.Uint160
|
||||||
NetmapContract util.Uint160
|
NetmapContract util.Uint160
|
||||||
MorphClient *client.Client
|
MorphClient *client.Client
|
||||||
EpochState EpochState
|
EpochState EpochState
|
||||||
ActiveState ActiveState
|
ActiveState ActiveState
|
||||||
Converter PrecisionConverter
|
Converter PrecisionConverter
|
||||||
|
MintEmitCacheSize int
|
||||||
|
MintEmitThreshold uint64 // in epochs
|
||||||
|
MintEmitValue util.Fixed8
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -83,16 +93,25 @@ func New(p *Params) (*Processor, error) {
|
||||||
return nil, errors.Wrap(err, "ir/neofs: can't create worker pool")
|
return nil, errors.Wrap(err, "ir/neofs: can't create worker pool")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lruCache, err := lru.New(p.MintEmitCacheSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "ir/neofs: can't create LRU cache for gas emission")
|
||||||
|
}
|
||||||
|
|
||||||
return &Processor{
|
return &Processor{
|
||||||
log: p.Log,
|
log: p.Log,
|
||||||
pool: pool,
|
pool: pool,
|
||||||
neofsContract: p.NeoFSContract,
|
neofsContract: p.NeoFSContract,
|
||||||
balanceContract: p.BalanceContract,
|
balanceContract: p.BalanceContract,
|
||||||
netmapContract: p.NetmapContract,
|
netmapContract: p.NetmapContract,
|
||||||
morphClient: p.MorphClient,
|
morphClient: p.MorphClient,
|
||||||
epochState: p.EpochState,
|
epochState: p.EpochState,
|
||||||
activeState: p.ActiveState,
|
activeState: p.ActiveState,
|
||||||
converter: p.Converter,
|
converter: p.Converter,
|
||||||
|
mintEmitLock: new(sync.Mutex),
|
||||||
|
mintEmitCache: lruCache,
|
||||||
|
mintEmitThreshold: p.MintEmitThreshold,
|
||||||
|
mintEmitValue: p.MintEmitValue,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue