Skip to content

Commit

Permalink
avoid processing already finalized snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Jan 27, 2024
1 parent 1383faf commit 56d4619
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
7 changes: 6 additions & 1 deletion kernel/cosi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,16 +1022,21 @@ func (node *Node) VerifyAndQueueAppendSnapshotFinalization(peerId crypto.Hash, s
return nil
}

tx, err := node.checkTxInStorage(s.SoleTransaction())
tx, finalized, err := node.checkTxInStorage(s.SoleTransaction())
if err != nil {
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) check tx error %s\n", peerId, s.Hash, err)
} else if tx == nil {
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) SendTransactionRequestMessage %s\n",
peerId, s.Hash, s.SoleTransaction())
node.Peer.SendTransactionRequestMessage(peerId, s.SoleTransaction())
} else if finalized == s.Hash.String() {
return nil
}

chain := node.getOrCreateChain(s.NodeId)
if cs := chain.State; cs != nil && cs.CacheRound.index[s.Hash] {
return nil
}
if _, finalized := chain.verifyFinalization(s); !finalized {
logger.Verbosef("ERROR VerifyAndQueueAppendSnapshotFinalization %s %v %d %t %v %v\n",
peerId, s, node.ConsensusThreshold(s.Timestamp, true), chain.IsPledging(), chain.State, chain.ConsensusInfo)
Expand Down
1 change: 1 addition & 0 deletions kernel/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ func (node *Node) finalizeNodeAcceptSnapshot(s *common.Snapshot, signers []crypt
Self: final.Hash,
External: external.Hash,
},
index: make(map[crypto.Hash]bool),
}
err = node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, final.Start)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions kernel/final.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"github.com/MixinNetwork/mixin/crypto"
)

func (node *Node) checkTxInStorage(id crypto.Hash) (*common.VersionedTransaction, error) {
tx, _, err := node.persistStore.ReadTransaction(id)
func (node *Node) checkTxInStorage(id crypto.Hash) (*common.VersionedTransaction, string, error) {
tx, snap, err := node.persistStore.ReadTransaction(id)
if err != nil || tx != nil {
return tx, err
return tx, snap, err
}

return node.persistStore.CacheGetTransaction(id)
tx, err = node.persistStore.CacheGetTransaction(id)
return tx, "", err
}
1 change: 1 addition & 0 deletions kernel/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (chain *Chain) startNewRoundAndPersist(cache *CacheRound, references *commo
Number: final.Number + 1,
Timestamp: timestamp,
References: references.Copy(),
index: make(map[crypto.Hash]bool),
}
if dummy {
cache.References.External = dummyExternal
Expand Down
2 changes: 1 addition & 1 deletion kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (node *Node) Authenticate(msg []byte) (crypto.Hash, string, error) {
}

func (node *Node) SendTransactionToPeer(peerId, hash crypto.Hash) error {
tx, err := node.checkTxInStorage(hash)
tx, _, err := node.checkTxInStorage(hash)
if err != nil || tx == nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions kernel/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type CacheRound struct {
Timestamp uint64
References *common.RoundLink
Snapshots []*common.Snapshot
index map[crypto.Hash]bool
}

type FinalRound struct {
Expand Down Expand Up @@ -96,6 +97,7 @@ func loadHeadRoundForNode(store storage.Store, nodeIdWithNetwork crypto.Hash) (*
Number: meta.Number,
Timestamp: meta.Timestamp,
References: meta.References,
index: make(map[crypto.Hash]bool),
}
topos, err := store.ReadSnapshotsForNodeRound(round.NodeId, round.Number)
if err != nil {
Expand All @@ -105,6 +107,7 @@ func loadHeadRoundForNode(store storage.Store, nodeIdWithNetwork crypto.Hash) (*
s := t.Snapshot
s.Hash = s.PayloadHash()
round.Snapshots = append(round.Snapshots, s)
round.index[s.Hash] = true
}
return round, nil
}
Expand Down Expand Up @@ -142,6 +145,7 @@ func (c *CacheRound) Copy() *CacheRound {
External: c.References.External,
},
Snapshots: append([]*common.Snapshot{}, c.Snapshots...),
index: c.index,
}
}

Expand Down Expand Up @@ -184,10 +188,12 @@ func (c *CacheRound) Gap() (uint64, uint64) {

func (chain *Chain) AddSnapshot(final *FinalRound, cache *CacheRound, s *common.Snapshot, signers []crypto.Hash) error {
chain.node.TopoWrite(s, signers)
if err := cache.validateSnapshot(s, true); err != nil {
panic("should never be here")
err := cache.validateSnapshot(s, true)
if err != nil {
panic(err)
}
chain.assignNewGraphRound(final, cache)
chain.State.CacheRound.index[s.Hash] = true
return nil
}

Expand Down

0 comments on commit 56d4619

Please sign in to comment.