-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2775 from jorgemmsilva/fix/mempool-nonce
feat: mempool nonce override
- Loading branch information
Showing
4 changed files
with
267 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright 2020 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package mempool | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"golang.org/x/exp/slices" | ||
|
||
"github.com/iotaledger/hive.go/ds/shrinkingmap" | ||
"github.com/iotaledger/hive.go/logger" | ||
"github.com/iotaledger/wasp/packages/isc" | ||
) | ||
|
||
// keeps a map of requests ordered by nonce for each account | ||
type TypedPoolByNonce[V isc.OffLedgerRequest] struct { | ||
waitReq WaitReq | ||
refLUT *shrinkingmap.ShrinkingMap[isc.RequestRefKey, *OrderedPoolEntry[V]] | ||
// reqsByAcountOrdered keeps an ordered map of reqsByAcountOrdered for each account by nonce | ||
reqsByAcountOrdered *shrinkingmap.ShrinkingMap[string, []*OrderedPoolEntry[V]] // string is isc.AgentID.String() | ||
sizeMetric func(int) | ||
timeMetric func(time.Duration) | ||
log *logger.Logger | ||
} | ||
|
||
var _ RequestPool[isc.OffLedgerRequest] = &TypedPoolByNonce[isc.OffLedgerRequest]{} | ||
|
||
func NewTypedPoolByNonce[V isc.OffLedgerRequest](waitReq WaitReq, sizeMetric func(int), timeMetric func(time.Duration), log *logger.Logger) *TypedPoolByNonce[V] { | ||
return &TypedPoolByNonce[V]{ | ||
waitReq: waitReq, | ||
reqsByAcountOrdered: shrinkingmap.New[string, []*OrderedPoolEntry[V]](), | ||
refLUT: shrinkingmap.New[isc.RequestRefKey, *OrderedPoolEntry[V]](), | ||
sizeMetric: sizeMetric, | ||
timeMetric: timeMetric, | ||
log: log, | ||
} | ||
} | ||
|
||
type OrderedPoolEntry[V isc.OffLedgerRequest] struct { | ||
req V | ||
old bool | ||
ts time.Time | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) Has(reqRef *isc.RequestRef) bool { | ||
return p.refLUT.Has(reqRef.AsKey()) | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) Get(reqRef *isc.RequestRef) V { | ||
entry, exists := p.refLUT.Get(reqRef.AsKey()) | ||
if !exists { | ||
return *new(V) | ||
} | ||
return entry.req | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) Add(request V) { | ||
ref := isc.RequestRefFromRequest(request) | ||
entry := &OrderedPoolEntry[V]{req: request, ts: time.Now()} | ||
account := request.SenderAccount().String() | ||
|
||
if !p.refLUT.Set(ref.AsKey(), entry) { | ||
p.log.Debugf("NOT ADDED, already exists. reqID: %v as key=%v, senderAccount: ", request.ID(), ref, account) | ||
return // not added already exists | ||
} | ||
|
||
defer func() { | ||
p.log.Debugf("ADD %v as key=%v, senderAccount: ", request.ID(), ref, account) | ||
p.sizeMetric(p.refLUT.Size()) | ||
p.waitReq.MarkAvailable(request) | ||
}() | ||
|
||
reqsForAcount, exists := p.reqsByAcountOrdered.Get(account) | ||
if !exists { | ||
// no other requests for this account | ||
p.reqsByAcountOrdered.Set(account, []*OrderedPoolEntry[V]{entry}) | ||
return | ||
} | ||
|
||
// add to the account requests, keep the slice ordered | ||
|
||
// find the index where the new entry should be added | ||
index, exists := slices.BinarySearchFunc(reqsForAcount, entry, | ||
func(a, b *OrderedPoolEntry[V]) int { | ||
aNonce := a.req.Nonce() | ||
bNonce := b.req.Nonce() | ||
if aNonce == bNonce { | ||
return 0 | ||
} | ||
if aNonce > bNonce { | ||
return 1 | ||
} | ||
return -1 | ||
}, | ||
) | ||
if exists { | ||
// same nonce, mark the existing request with overlapping nonce as "old", place the new one | ||
// NOTE: do not delete the request here, as it might already be part of an on-going consensus round | ||
reqsForAcount[index].old = true | ||
} | ||
|
||
reqsForAcount = append(reqsForAcount, entry) // add to the end of the list (thus extending the array) | ||
|
||
// make room if target position is not at the end | ||
if index != len(reqsForAcount)+1 { | ||
copy(reqsForAcount[index+1:], reqsForAcount[index:]) | ||
reqsForAcount[index] = entry | ||
} | ||
p.reqsByAcountOrdered.Set(account, reqsForAcount) | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) Remove(request V) { | ||
refKey := isc.RequestRefFromRequest(request).AsKey() | ||
entry, exists := p.refLUT.Get(refKey) | ||
if !exists { | ||
return // does not exist | ||
} | ||
defer func() { | ||
p.sizeMetric(p.refLUT.Size()) | ||
p.timeMetric(time.Since(entry.ts)) | ||
}() | ||
if p.refLUT.Delete(refKey) { | ||
p.log.Debugf("DEL %v as key=%v", request.ID(), refKey) | ||
} | ||
account := entry.req.SenderAccount().String() | ||
reqsByAccount, exists := p.reqsByAcountOrdered.Get(account) | ||
if !exists { | ||
p.log.Error("inconsistency trying to DEL %v as key=%v, no request list for account %s", request.ID(), refKey, account) | ||
return | ||
} | ||
// find the request in the accounts map | ||
indexToDel := slices.IndexFunc(reqsByAccount, func(e *OrderedPoolEntry[V]) bool { | ||
return true | ||
}) | ||
if indexToDel == -1 { | ||
p.log.Error("inconsistency trying to DEL %v as key=%v, request not found in list for account %s", request.ID(), refKey, account) | ||
return | ||
} | ||
if len(reqsByAccount) == 1 { // just remove the entire array for the account | ||
p.reqsByAcountOrdered.Delete(account) | ||
return | ||
} | ||
reqsByAccount[indexToDel] = nil // remove the pointer reference to allow GC of the entry object | ||
reqsByAccount = slices.Delete(reqsByAccount, indexToDel, indexToDel+1) | ||
p.reqsByAcountOrdered.Set(account, reqsByAccount) | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) Iterate(f func(account string, requests []*OrderedPoolEntry[V])) { | ||
p.reqsByAcountOrdered.ForEach(func(acc string, reqs []*OrderedPoolEntry[V]) bool { | ||
f(acc, reqs) | ||
return true | ||
}) | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) Filter(predicate func(request V, ts time.Time) bool) { | ||
p.refLUT.ForEach(func(refKey isc.RequestRefKey, entry *OrderedPoolEntry[V]) bool { | ||
if !predicate(entry.req, entry.ts) { | ||
p.Remove(entry.req) | ||
} | ||
return true | ||
}) | ||
p.sizeMetric(p.refLUT.Size()) | ||
} | ||
|
||
func (p *TypedPoolByNonce[V]) StatusString() string { | ||
return fmt.Sprintf("{|req|=%d}", p.refLUT.Size()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters