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("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/golang/protobuf v1.4.3
|
||||
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/mr-tron/base58 v1.1.3
|
||||
github.com/multiformats/go-multiaddr v0.2.0
|
||||
|
|
|
@ -240,6 +240,9 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
|
|||
EpochState: server,
|
||||
ActiveState: server,
|
||||
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 {
|
||||
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))
|
||||
}
|
||||
|
||||
// fixme: send gas when ^ tx accepted
|
||||
// fixme: do not send gas to the same user twice per epoch
|
||||
err = np.morphClient.TransferGas(deposit.To(), util.Fixed8FromInt64(2))
|
||||
if err != nil {
|
||||
np.log.Error("can't transfer native gas to receiver", zap.Error(err))
|
||||
curEpoch := np.epochState.EpochCounter()
|
||||
receiver := deposit.To()
|
||||
|
||||
// check if receiver already received some mint GAS emissions
|
||||
// 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.
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package neofs
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
"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/event"
|
||||
|
@ -37,6 +40,10 @@ type (
|
|||
epochState EpochState
|
||||
activeState ActiveState
|
||||
converter PrecisionConverter
|
||||
mintEmitLock *sync.Mutex
|
||||
mintEmitCache *lru.Cache
|
||||
mintEmitThreshold uint64
|
||||
mintEmitValue util.Fixed8
|
||||
}
|
||||
|
||||
// Params of the processor constructor.
|
||||
|
@ -50,6 +57,9 @@ type (
|
|||
EpochState EpochState
|
||||
ActiveState ActiveState
|
||||
Converter PrecisionConverter
|
||||
MintEmitCacheSize int
|
||||
MintEmitThreshold uint64 // in epochs
|
||||
MintEmitValue util.Fixed8
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -83,6 +93,11 @@ func New(p *Params) (*Processor, error) {
|
|||
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{
|
||||
log: p.Log,
|
||||
pool: pool,
|
||||
|
@ -93,6 +108,10 @@ func New(p *Params) (*Processor, error) {
|
|||
epochState: p.EpochState,
|
||||
activeState: p.ActiveState,
|
||||
converter: p.Converter,
|
||||
mintEmitLock: new(sync.Mutex),
|
||||
mintEmitCache: lruCache,
|
||||
mintEmitThreshold: p.MintEmitThreshold,
|
||||
mintEmitValue: p.MintEmitValue,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue