Skip to content

Commit

Permalink
Fix caching malfeasance info in atxsdata store (#5932)
Browse files Browse the repository at this point in the history
## Motivation

Hare depends on `atxsdata` to check whether a smesher is considered malicious.
  • Loading branch information
poszu committed May 13, 2024
1 parent 2452f59 commit 9f827d8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
13 changes: 11 additions & 2 deletions datastore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type CachedDB struct {
sql.QueryCache
logger log.Log

// cache is optional
// cache is optional in tests. It MUST be set for the 'App'
// for properly checking malfeasance.
atxsdata *atxsdata.Data

atxCache *lru.Cache[types.ATXID, *types.ActivationTx]
Expand All @@ -71,7 +72,8 @@ func DefaultConfig() Config {
}

type cacheOpts struct {
cfg Config
cfg Config
atxsdata *atxsdata.Data
}

type Opt func(*cacheOpts)
Expand All @@ -82,6 +84,12 @@ func WithConfig(cfg Config) Opt {
}
}

func WithConsensusCache(c *atxsdata.Data) Opt {
return func(o *cacheOpts) {
o.atxsdata = c
}
}

// NewCachedDB create an instance of a CachedDB.
func NewCachedDB(db Executor, lg log.Log, opts ...Opt) *CachedDB {
o := cacheOpts{cfg: DefaultConfig()}
Expand Down Expand Up @@ -109,6 +117,7 @@ func NewCachedDB(db Executor, lg log.Log, opts ...Opt) *CachedDB {
Executor: db,
QueryCache: db.QueryCache(),
logger: lg,
atxsdata: o.atxsdata,
atxCache: atxHdrCache,
malfeasanceCache: malfeasanceCache,
vrfNonceCache: vrfNonceCache,
Expand Down
14 changes: 14 additions & 0 deletions datastore/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/fixture"
"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -406,3 +407,16 @@ func TestBlobStore_GetActiveSet(t *testing.T) {
require.NoError(t, err)
require.Equal(t, codec.MustEncode(as), got)
}

func Test_MarkingMalicious(t *testing.T) {
db := sql.InMemory()
store := atxsdata.New()
id := types.RandomNodeID()
cdb := datastore.NewCachedDB(db, logtest.New(t), datastore.WithConsensusCache(store))

cdb.CacheMalfeasanceProof(id, &mwire.MalfeasanceProof{})
m, err := cdb.IsMalicious(id)
require.NoError(t, err)
require.True(t, m)
require.True(t, store.IsMalicious(id))
}
10 changes: 9 additions & 1 deletion malfeasance/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.uber.org/mock/gomock"

awire "github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
Expand Down Expand Up @@ -51,8 +52,9 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
trt := malfeasance.NewMocktortoise(ctrl)
postVerifier := malfeasance.NewMockpostVerifier(ctrl)

store := atxsdata.New()
h := malfeasance.NewHandler(
datastore.NewCachedDB(db, lg),
datastore.NewCachedDB(db, lg, datastore.WithConsensusCache(store)),
lg,
"self",
[]types.NodeID{types.RandomNodeID()},
Expand Down Expand Up @@ -103,6 +105,7 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
malProof, err := identities.GetMalfeasanceProof(db, sig.NodeID())
require.ErrorIs(t, err, sql.ErrNotFound)
require.Nil(t, malProof)
require.False(t, store.IsMalicious(sig.NodeID()))
})

createIdentity(t, db, sig)
Expand Down Expand Up @@ -130,6 +133,7 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
malProof, err := identities.GetMalfeasanceProof(db, sig.NodeID())
require.ErrorIs(t, err, sql.ErrNotFound)
require.Nil(t, malProof)
require.False(t, store.IsMalicious(sig.NodeID()))
})

t.Run("different epoch", func(t *testing.T) {
Expand All @@ -153,6 +157,7 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
malProof, err := identities.GetMalfeasanceProof(db, sig.NodeID())
require.ErrorIs(t, err, sql.ErrNotFound)
require.Nil(t, malProof)
require.False(t, store.IsMalicious(sig.NodeID()))
})

t.Run("different signer", func(t *testing.T) {
Expand All @@ -177,6 +182,7 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
malProof, err := identities.GetMalfeasanceProof(db, sig.NodeID())
require.ErrorIs(t, err, sql.ErrNotFound)
require.Nil(t, malProof)
require.False(t, store.IsMalicious(sig.NodeID()))
})

t.Run("invalid hare eligibility", func(t *testing.T) {
Expand All @@ -198,6 +204,7 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
data, err := codec.Encode(gossip)
require.NoError(t, err)
require.Error(t, h.HandleMalfeasanceProof(context.Background(), "peer", data))
require.False(t, store.IsMalicious(sig.NodeID()))
})

t.Run("valid", func(t *testing.T) {
Expand Down Expand Up @@ -225,6 +232,7 @@ func TestHandler_HandleMalfeasanceProof_multipleATXs(t *testing.T) {
require.NotNil(t, malProof.Received())
malProof.SetReceived(time.Time{})
require.Equal(t, gossip.MalfeasanceProof, *malProof)
require.True(t, store.IsMalicious(sig.NodeID()))
})

t.Run("proof equivalence", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,7 @@ func (app *App) setupDBs(ctx context.Context, lg log.Log) error {
app.log.With().Info("cache warmup", log.Duration("duration", time.Since(start)))
app.cachedDB = datastore.NewCachedDB(sqlDB, app.addLogger(CachedDBLogger, lg),
datastore.WithConfig(app.Config.Cache),
datastore.WithConsensusCache(data),
)

migrations, err = sql.LocalMigrations()
Expand Down

0 comments on commit 9f827d8

Please sign in to comment.