Commit d4aeb344 authored by metacertain's avatar metacertain Committed by GitHub

fix: cache verifications (#2160)

parent 003bea49
......@@ -332,7 +332,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
lightNodes := lightnode.NewContainer(swarmAddress)
senderMatcher := transaction.NewMatcher(swapBackend, types.NewEIP155Signer(big.NewInt(chainID)))
senderMatcher := transaction.NewMatcher(swapBackend, types.NewEIP155Signer(big.NewInt(chainID)), stateStore)
p2ps, err := libp2p.New(p2pCtx, signer, networkID, swarmAddress, addr, addressbook, stateStore, lightNodes, senderMatcher, logger, tracer, libp2p.Options{
PrivateKey: libp2pPrivateKey,
......
......@@ -6,18 +6,29 @@ import (
"errors"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
type Matcher struct {
backend Backend
storage storage.StateStorer
signer types.Signer
}
const (
overlayPrefix = "verified_overlay_"
)
func peerOverlayKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", overlayPrefix, peer.String())
}
var (
ErrTransactionNotFound = errors.New("transaction not found")
ErrTransactionPending = errors.New("transaction in pending status")
......@@ -26,8 +37,15 @@ var (
ErrOverlayMismatch = errors.New("overlay mismatch")
)
func NewMatcher(backend Backend, signer types.Signer) *Matcher {
type overlayVerification struct {
nextBlockHash []byte
verified bool
timeStamp time.Time
}
func NewMatcher(backend Backend, signer types.Signer, storage storage.StateStorer) *Matcher {
return &Matcher{
storage: storage,
backend: backend,
signer: signer,
}
......@@ -35,6 +53,17 @@ func NewMatcher(backend Backend, signer types.Signer) *Matcher {
func (m *Matcher) Matches(ctx context.Context, tx []byte, networkID uint64, senderOverlay swarm.Address) ([]byte, error) {
var val overlayVerification
err := m.storage.Get(peerOverlayKey(senderOverlay), &val)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return nil, err
} else if val.verified {
// add cache invalidation
return val.nextBlockHash, nil
}
incomingTx := common.BytesToHash(tx)
nTx, isPending, err := m.backend.TransactionByHash(ctx, incomingTx)
......@@ -75,5 +104,15 @@ func (m *Matcher) Matches(ctx context.Context, tx []byte, networkID uint64, send
return nil, ErrOverlayMismatch
}
err = m.storage.Put(peerOverlayKey(senderOverlay), &overlayVerification{
timeStamp: time.Now(),
verified: true,
nextBlockHash: nextBlockHash,
})
if err != nil {
return nil, err
}
return nextBlockHash, nil
}
......@@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto"
statestore "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/bee/pkg/transaction/backendmock"
......@@ -30,7 +31,7 @@ func TestMatchesSender(t *testing.T) {
return nil, false, errors.New("transaction not found by hash")
})
matcher := transaction.NewMatcher(backendmock.New(txByHash), nil)
matcher := transaction.NewMatcher(backendmock.New(txByHash), nil, statestore.NewStateStore())
_, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if !errors.Is(err, transaction.ErrTransactionNotFound) {
......@@ -43,7 +44,7 @@ func TestMatchesSender(t *testing.T) {
return nil, true, nil
})
matcher := transaction.NewMatcher(backendmock.New(txByHash), nil)
matcher := transaction.NewMatcher(backendmock.New(txByHash), nil, statestore.NewStateStore())
_, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if !errors.Is(err, transaction.ErrTransactionPending) {
......@@ -59,7 +60,7 @@ func TestMatchesSender(t *testing.T) {
signer := &mockSigner{
err: errors.New("can not sign"),
}
matcher := transaction.NewMatcher(backendmock.New(txByHash), signer)
matcher := transaction.NewMatcher(backendmock.New(txByHash), signer, statestore.NewStateStore())
_, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if !errors.Is(err, transaction.ErrTransactionSenderInvalid) {
......@@ -93,7 +94,7 @@ func TestMatchesSender(t *testing.T) {
}, nil
})
matcher := transaction.NewMatcher(backendmock.New(txByHash, trxReceipt, headerByNum), signer)
matcher := transaction.NewMatcher(backendmock.New(txByHash, trxReceipt, headerByNum), signer, statestore.NewStateStore())
_, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if err == nil {
......@@ -127,7 +128,7 @@ func TestMatchesSender(t *testing.T) {
addr: common.HexToAddress("0xff"),
}
matcher := transaction.NewMatcher(backendmock.New(trxReceipt, headerByNum, txByHash), signer)
matcher := transaction.NewMatcher(backendmock.New(trxReceipt, headerByNum, txByHash), signer, statestore.NewStateStore())
senderOverlay := crypto.NewOverlayFromEthereumAddress(signer.addr.Bytes(), 0, nextBlockHeader.Hash().Bytes())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment