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 ...@@ -332,7 +332,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
lightNodes := lightnode.NewContainer(swarmAddress) 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{ p2ps, err := libp2p.New(p2pCtx, signer, networkID, swarmAddress, addr, addressbook, stateStore, lightNodes, senderMatcher, logger, tracer, libp2p.Options{
PrivateKey: libp2pPrivateKey, PrivateKey: libp2pPrivateKey,
......
...@@ -6,18 +6,29 @@ import ( ...@@ -6,18 +6,29 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
) )
type Matcher struct { type Matcher struct {
backend Backend backend Backend
storage storage.StateStorer
signer types.Signer signer types.Signer
} }
const (
overlayPrefix = "verified_overlay_"
)
func peerOverlayKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", overlayPrefix, peer.String())
}
var ( var (
ErrTransactionNotFound = errors.New("transaction not found") ErrTransactionNotFound = errors.New("transaction not found")
ErrTransactionPending = errors.New("transaction in pending status") ErrTransactionPending = errors.New("transaction in pending status")
...@@ -26,8 +37,15 @@ var ( ...@@ -26,8 +37,15 @@ var (
ErrOverlayMismatch = errors.New("overlay mismatch") 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{ return &Matcher{
storage: storage,
backend: backend, backend: backend,
signer: signer, signer: signer,
} }
...@@ -35,6 +53,17 @@ func NewMatcher(backend Backend, signer types.Signer) *Matcher { ...@@ -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) { 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) incomingTx := common.BytesToHash(tx)
nTx, isPending, err := m.backend.TransactionByHash(ctx, incomingTx) nTx, isPending, err := m.backend.TransactionByHash(ctx, incomingTx)
...@@ -75,5 +104,15 @@ func (m *Matcher) Matches(ctx context.Context, tx []byte, networkID uint64, send ...@@ -75,5 +104,15 @@ func (m *Matcher) Matches(ctx context.Context, tx []byte, networkID uint64, send
return nil, ErrOverlayMismatch 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 return nextBlockHash, nil
} }
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto" "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/swarm"
"github.com/ethersphere/bee/pkg/transaction" "github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/bee/pkg/transaction/backendmock" "github.com/ethersphere/bee/pkg/transaction/backendmock"
...@@ -30,7 +31,7 @@ func TestMatchesSender(t *testing.T) { ...@@ -30,7 +31,7 @@ func TestMatchesSender(t *testing.T) {
return nil, false, errors.New("transaction not found by hash") 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{})) _, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if !errors.Is(err, transaction.ErrTransactionNotFound) { if !errors.Is(err, transaction.ErrTransactionNotFound) {
...@@ -43,7 +44,7 @@ func TestMatchesSender(t *testing.T) { ...@@ -43,7 +44,7 @@ func TestMatchesSender(t *testing.T) {
return nil, true, nil 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{})) _, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if !errors.Is(err, transaction.ErrTransactionPending) { if !errors.Is(err, transaction.ErrTransactionPending) {
...@@ -59,7 +60,7 @@ func TestMatchesSender(t *testing.T) { ...@@ -59,7 +60,7 @@ func TestMatchesSender(t *testing.T) {
signer := &mockSigner{ signer := &mockSigner{
err: errors.New("can not sign"), 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{})) _, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if !errors.Is(err, transaction.ErrTransactionSenderInvalid) { if !errors.Is(err, transaction.ErrTransactionSenderInvalid) {
...@@ -93,7 +94,7 @@ func TestMatchesSender(t *testing.T) { ...@@ -93,7 +94,7 @@ func TestMatchesSender(t *testing.T) {
}, nil }, 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{})) _, err := matcher.Matches(context.Background(), trx, 0, swarm.NewAddress([]byte{}))
if err == nil { if err == nil {
...@@ -127,7 +128,7 @@ func TestMatchesSender(t *testing.T) { ...@@ -127,7 +128,7 @@ func TestMatchesSender(t *testing.T) {
addr: common.HexToAddress("0xff"), 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()) 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