2021-01-18 12:52:51 +00:00
|
|
|
package extpool
|
|
|
|
|
|
|
|
import (
|
2021-05-04 14:54:16 +00:00
|
|
|
"container/list"
|
2021-01-18 12:52:51 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2022-01-14 01:09:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2021-01-18 12:52:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2022-01-14 01:09:54 +00:00
|
|
|
// Ledger is enough of Blockchain to satisfy Pool.
|
|
|
|
type Ledger interface {
|
|
|
|
BlockHeight() uint32
|
|
|
|
IsExtensibleAllowed(util.Uint160) bool
|
|
|
|
VerifyWitness(util.Uint160, hash.Hashable, *transaction.Witness, int64) (int64, error)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Pool represents a pool of extensible payloads.
|
2021-01-18 12:52:51 +00:00
|
|
|
type Pool struct {
|
|
|
|
lock sync.RWMutex
|
2021-05-04 14:54:16 +00:00
|
|
|
verified map[util.Uint256]*list.Element
|
|
|
|
senders map[util.Uint160]*list.List
|
2022-04-20 18:30:09 +00:00
|
|
|
// singleCap represents the maximum number of payloads from a single sender.
|
2021-05-04 14:54:16 +00:00
|
|
|
singleCap int
|
2022-01-14 01:09:54 +00:00
|
|
|
chain Ledger
|
2021-01-18 12:52:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// New returns a new payload pool using the provided chain.
|
2022-01-14 01:09:54 +00:00
|
|
|
func New(bc Ledger, capacity int) *Pool {
|
2021-05-04 14:54:16 +00:00
|
|
|
if capacity <= 0 {
|
|
|
|
panic("invalid capacity")
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:52:51 +00:00
|
|
|
return &Pool{
|
2021-05-04 14:54:16 +00:00
|
|
|
verified: make(map[util.Uint256]*list.Element),
|
|
|
|
senders: make(map[util.Uint160]*list.List),
|
|
|
|
singleCap: capacity,
|
|
|
|
chain: bc,
|
2021-01-18 12:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errDisallowedSender = errors.New("disallowed sender")
|
|
|
|
errInvalidHeight = errors.New("invalid height")
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Add adds an extensible payload to the pool.
|
|
|
|
// First return value specifies if the payload was new.
|
|
|
|
// Second one is nil if and only if the payload is valid.
|
2021-01-18 12:52:51 +00:00
|
|
|
func (p *Pool) Add(e *payload.Extensible) (bool, error) {
|
|
|
|
if ok, err := p.verify(e); err != nil || !ok {
|
|
|
|
return ok, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
h := e.Hash()
|
|
|
|
if _, ok := p.verified[h]; ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-05-04 14:54:16 +00:00
|
|
|
|
|
|
|
lst, ok := p.senders[e.Sender]
|
|
|
|
if ok && lst.Len() >= p.singleCap {
|
|
|
|
value := lst.Remove(lst.Front())
|
|
|
|
delete(p.verified, value.(*payload.Extensible).Hash())
|
|
|
|
} else if !ok {
|
|
|
|
lst = list.New()
|
|
|
|
p.senders[e.Sender] = lst
|
|
|
|
}
|
|
|
|
|
|
|
|
p.verified[h] = lst.PushBack(e)
|
2021-01-18 12:52:51 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pool) verify(e *payload.Extensible) (bool, error) {
|
2021-10-25 14:42:20 +00:00
|
|
|
if _, err := p.chain.VerifyWitness(e.Sender, e, &e.Witness, extensibleVerifyMaxGAS); err != nil {
|
2021-01-18 12:52:51 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
h := p.chain.BlockHeight()
|
|
|
|
if h < e.ValidBlockStart || e.ValidBlockEnd <= h {
|
2022-04-20 18:30:09 +00:00
|
|
|
// We can receive a consensus payload for the last or next block
|
|
|
|
// which leads to an unwanted node disconnect.
|
2021-01-18 12:52:51 +00:00
|
|
|
if e.ValidBlockEnd == h {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, errInvalidHeight
|
|
|
|
}
|
|
|
|
if !p.chain.IsExtensibleAllowed(e.Sender) {
|
|
|
|
return false, errDisallowedSender
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns payload by hash.
|
|
|
|
func (p *Pool) Get(h util.Uint256) *payload.Extensible {
|
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
|
2021-05-04 14:54:16 +00:00
|
|
|
elem, ok := p.verified[h]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return elem.Value.(*payload.Extensible)
|
2021-01-18 12:52:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 13:14:49 +00:00
|
|
|
const extensibleVerifyMaxGAS = 6000000
|
2021-01-18 12:52:51 +00:00
|
|
|
|
|
|
|
// RemoveStale removes invalid payloads after block processing.
|
|
|
|
func (p *Pool) RemoveStale(index uint32) {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
2021-05-04 14:54:16 +00:00
|
|
|
|
|
|
|
for s, lst := range p.senders {
|
|
|
|
for elem := lst.Front(); elem != nil; {
|
|
|
|
e := elem.Value.(*payload.Extensible)
|
|
|
|
h := e.Hash()
|
|
|
|
old := elem
|
|
|
|
elem = elem.Next()
|
|
|
|
|
|
|
|
if e.ValidBlockEnd <= index || !p.chain.IsExtensibleAllowed(e.Sender) {
|
|
|
|
delete(p.verified, h)
|
|
|
|
lst.Remove(old)
|
|
|
|
continue
|
|
|
|
}
|
2021-10-25 14:42:20 +00:00
|
|
|
if _, err := p.chain.VerifyWitness(e.Sender, e, &e.Witness, extensibleVerifyMaxGAS); err != nil {
|
2021-05-04 14:54:16 +00:00
|
|
|
delete(p.verified, h)
|
|
|
|
lst.Remove(old)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-18 12:52:51 +00:00
|
|
|
}
|
2021-05-04 14:54:16 +00:00
|
|
|
if lst.Len() == 0 {
|
|
|
|
delete(p.senders, s)
|
2021-01-18 12:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|