track required funds in message set

This commit is contained in:
vyzo 2020-08-26 14:38:26 +03:00
parent 77f6e0d4f8
commit 4ac9828b0c

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
stdbig "math/big"
"sort" "sort"
"sync" "sync"
"time" "time"
@ -120,11 +121,13 @@ type MessagePool struct {
type msgSet struct { type msgSet struct {
msgs map[uint64]*types.SignedMessage msgs map[uint64]*types.SignedMessage
nextNonce uint64 nextNonce uint64
requiredFunds *stdbig.Int
} }
func newMsgSet() *msgSet { func newMsgSet() *msgSet {
return &msgSet{ return &msgSet{
msgs: make(map[uint64]*types.SignedMessage), msgs: make(map[uint64]*types.SignedMessage),
requiredFunds: stdbig.NewInt(0),
} }
} }
@ -150,12 +153,27 @@ func (ms *msgSet) add(m *types.SignedMessage, mp *MessagePool) (bool, error) {
ErrRBFTooLowPremium) ErrRBFTooLowPremium)
} }
} }
ms.requiredFunds.Sub(ms.requiredFunds, exms.Message.RequiredFunds().Int)
ms.requiredFunds.Sub(ms.requiredFunds, exms.Message.Value.Int)
} }
ms.msgs[m.Message.Nonce] = m ms.msgs[m.Message.Nonce] = m
ms.requiredFunds.Add(ms.requiredFunds, m.Message.RequiredFunds().Int)
ms.requiredFunds.Add(ms.requiredFunds, m.Message.Value.Int)
return !has, nil return !has, nil
} }
func (ms *msgSet) rm(nonce uint64) {
m, has := ms.msgs[nonce]
if has {
ms.requiredFunds.Sub(ms.requiredFunds, m.Message.RequiredFunds().Int)
ms.requiredFunds.Sub(ms.requiredFunds, m.Message.Value.Int)
delete(ms.msgs, nonce)
}
}
func New(api Provider, ds dtypes.MetadataDS, netName dtypes.NetworkName) (*MessagePool, error) { func New(api Provider, ds dtypes.MetadataDS, netName dtypes.NetworkName) (*MessagePool, error) {
cache, _ := lru.New2Q(build.BlsSignatureCacheSize) cache, _ := lru.New2Q(build.BlsSignatureCacheSize)
verifcache, _ := lru.New2Q(build.VerifSigCacheSize) verifcache, _ := lru.New2Q(build.VerifSigCacheSize)
@ -638,7 +656,7 @@ func (mp *MessagePool) remove(from address.Address, nonce uint64) {
// NB: This deletes any message with the given nonce. This makes sense // NB: This deletes any message with the given nonce. This makes sense
// as two messages with the same sender cannot have the same nonce // as two messages with the same sender cannot have the same nonce
delete(mset.msgs, nonce) mset.rm(nonce)
if len(mset.msgs) == 0 { if len(mset.msgs) == 0 {
delete(mp.pending, from) delete(mp.pending, from)