Skip to content

Commit

Permalink
hare: use atxdata for atxs info and malfeasence identities (#5545)
Browse files Browse the repository at this point in the history
depends on: #5539
related: #5192

removes dependency on lru cache. instead it queries atxdata to check which identity is malfeasent
or to get atx height during proposals preparation.
  • Loading branch information
dshulyak committed Feb 13, 2024
1 parent a2d7dfa commit f7ceab4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions atxsdata/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestData(t *testing.T) {
data := c.Get(types.EpochID(epoch), types.ATXID{byte(epoch)})
require.True(t, data.Malicious)
}
require.True(t, c.IsMalicious(node))
})
t.Run("eviction", func(t *testing.T) {
const (
Expand Down
49 changes: 29 additions & 20 deletions hare3/hare.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import (
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"

"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"
"github.com/spacemeshos/go-spacemesh/layerpatrol"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/beacons"
"github.com/spacemeshos/go-spacemesh/sql/identities"
Expand Down Expand Up @@ -146,7 +147,8 @@ type nodeclock interface {
func New(
nodeclock nodeclock,
pubsub pubsub.PublishSubsciber,
db *datastore.CachedDB,
db *sql.Database,
atxsdata *atxsdata.Data,
verifier *signing.EdVerifier,
oracle oracle,
sync system.SyncStateProvider,
Expand All @@ -169,6 +171,7 @@ func New(
nodeclock: nodeclock,
pubsub: pubsub,
db: db,
atxsdata: atxsdata,
verifier: verifier,
oracle: &legacyOracle{
log: zap.NewNop(),
Expand Down Expand Up @@ -204,7 +207,8 @@ type Hare struct {
// dependencies
nodeclock nodeclock
pubsub pubsub.PublishSubsciber
db *datastore.CachedDB
db *sql.Database
atxsdata *atxsdata.Data
verifier *signing.EdVerifier
oracle *legacyOracle
sync system.SyncStateProvider
Expand Down Expand Up @@ -282,11 +286,8 @@ func (h *Hare) Handler(ctx context.Context, peer p2p.Peer, buf []byte) error {
signatureError.Inc()
return fmt.Errorf("%w: invalid signature", pubsub.ErrValidationReject)
}
malicious, err := h.db.IsMalicious(msg.Sender)
if err != nil {
maliciousError.Inc()
return fmt.Errorf("database error %s", err.Error())
}
malicious := h.atxsdata.IsMalicious(msg.Sender)

start := time.Now()
g := h.oracle.validate(msg)
oracleLatency.Observe(time.Since(start).Seconds())
Expand Down Expand Up @@ -314,7 +315,7 @@ func (h *Hare) Handler(ctx context.Context, peer p2p.Peer, buf []byte) error {
h.db, equivocation.Messages[0].SmesherID, codec.MustEncode(proof), time.Now()); err != nil {
h.log.Error("failed to save malicious identity", zap.Error(err))
}
h.db.CacheMalfeasanceProof(equivocation.Messages[0].SmesherID, proof)
h.atxsdata.SetMalicious(equivocation.Messages[0].SmesherID)
}
if !gossip {
droppedMessages.Inc()
Expand Down Expand Up @@ -521,15 +522,23 @@ func (h *Hare) proposals(session *session) []types.ProposalID {
var (
beacon types.Beacon
result []types.ProposalID
min, own *types.ActivationTxHeader
min, own *atxsdata.ATX
)
target := session.lid.GetEpoch()
publish := target - 1
for _, signer := range session.signers {
own, err = h.db.GetEpochAtx(session.lid.GetEpoch()-1, signer.NodeID())
if err != nil && !errors.Is(err, sql.ErrNotFound) {
atxid, err := atxs.GetIDByEpochAndNodeID(h.db, publish, signer.NodeID())
switch {
case errors.Is(err, sql.ErrNotFound):
// if atx is not registered for identity we will get sql.ErrNotFound
case err != nil:
h.log.Error("failed to get atx id by epoch and node id", zap.Error(err))
return []types.ProposalID{}
}
if min == nil || (min != nil && own != nil && own.TickHeight() < min.TickHeight()) {
min = own
default:
own = h.atxsdata.Get(target, atxid)
if min == nil || (min != nil && own != nil && own.Height < min.Height) {
min = own
}
}
}
if min == nil {
Expand All @@ -555,17 +564,17 @@ func (h *Hare) proposals(session *session) []types.ProposalID {
)
continue
}
hdr, err := h.db.GetAtxHeader(p.AtxID)
if err != nil {
header := h.atxsdata.Get(target, p.AtxID)
if header == nil {
h.log.Error("atx is not loaded", zap.Error(err), zap.Stringer("atxid", p.AtxID))
return []types.ProposalID{}
}
if hdr.BaseTickHeight >= min.TickHeight() {
if header.BaseHeight >= min.Height {
// does not vote for future proposal
h.log.Warn("proposal base tick height too high. skipping",
zap.Uint32("lid", session.lid.Uint32()),
zap.Uint64("proposal_height", hdr.BaseTickHeight),
zap.Uint64("own_height", own.TickHeight()),
zap.Uint64("proposal_height", header.BaseHeight),
zap.Uint64("own_height", own.Height),
)
continue
}
Expand Down
15 changes: 8 additions & 7 deletions hare3/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import (
"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"
"github.com/spacemeshos/go-spacemesh/hare/eligibility"
"github.com/spacemeshos/go-spacemesh/layerpatrol"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
pmocks "github.com/spacemeshos/go-spacemesh/p2p/pubsub/mocks"
Expand Down Expand Up @@ -116,7 +114,7 @@ type node struct {
vrfsigner *signing.VRFSigner
atx *types.VerifiedActivationTx
oracle *eligibility.Oracle
db *datastore.CachedDB
db *sql.Database
atxsdata *atxsdata.Data

ctrl *gomock.Controller
Expand Down Expand Up @@ -147,7 +145,7 @@ func (n *node) reuseSigner(signer *signing.EdSigner) *node {
}

func (n *node) withDb() *node {
n.db = datastore.NewCachedDB(sql.InMemory(), log.NewNop())
n.db = sql.InMemory()
n.atxsdata = atxsdata.New()
return n
}
Expand Down Expand Up @@ -216,7 +214,7 @@ func (n *node) withHare() *node {
tracer := newTestTracer(n.t)
n.tracer = tracer
n.patrol = layerpatrol.New()
n.hare = New(n.nclock, n.mpublisher, n.db, signing.NewEdVerifier(), n.oracle, n.msyncer, n.patrol,
n.hare = New(n.nclock, n.mpublisher, n.db, n.atxsdata, signing.NewEdVerifier(), n.oracle, n.msyncer, n.patrol,
WithConfig(n.t.cfg),
WithLogger(logger.Zap()),
WithWallclock(n.clock),
Expand Down Expand Up @@ -972,17 +970,20 @@ func TestProposals(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
db := datastore.NewCachedDB(sql.InMemory(), log.NewNop())
hare := New(nil, nil, db, nil, nil, nil, layerpatrol.New(), WithLogger(logtest.New(t).Zap()))
db := sql.InMemory()
atxsdata := atxsdata.New()
hare := New(nil, nil, db, atxsdata, nil, nil, nil, layerpatrol.New(), WithLogger(logtest.New(t).Zap()))
for _, atx := range tc.atxs {
require.NoError(t, atxs.Add(db, &atx))
atxsdata.AddFromHeader(atx.ToHeader(), *atx.VRFNonce, false)
}
for _, proposal := range tc.proposals {
require.NoError(t, proposals.Add(db, &proposal))
require.NoError(t, ballots.Add(db, &proposal.Ballot))
}
for _, id := range tc.malicious {
require.NoError(t, identities.SetMalicious(db, id, []byte("non empty"), time.Time{}))
atxsdata.SetMalicious(id)
}
require.Equal(t, tc.expect, hare.proposals(&session{
lid: tc.layer,
Expand Down
1 change: 0 additions & 1 deletion hare3/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var (
malformedError = validationError.WithLabelValues("malformed")
signatureError = validationError.WithLabelValues("signature")
oracleError = validationError.WithLabelValues("oracle")
maliciousError = validationError.WithLabelValues("malicious")

droppedMessages = metrics.NewCounter(
"dropped_msgs",
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ func (app *App) initServices(ctx context.Context) error {
}
logger := app.addLogger(HareLogger, lg).Zap()
app.hare3 = hare3.New(
app.clock, app.host, app.cachedDB, app.edVerifier, app.hOracle, newSyncer, patrol,
app.clock, app.host, app.db, app.atxsdata, app.edVerifier, app.hOracle, newSyncer, patrol,
hare3.WithLogger(logger),
hare3.WithConfig(app.Config.HARE3),
)
Expand Down

0 comments on commit f7ceab4

Please sign in to comment.