Skip to content

Commit

Permalink
fix recursive stack overflow consensus reload
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Dec 29, 2024
1 parent cb7fec5 commit a86a4ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
10 changes: 7 additions & 3 deletions kernel/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ func (node *Node) reloadConsensusState(s *common.Snapshot, tx *common.VersionedT
common.TransactionTypeNodeRemove,
common.TransactionTypeCustodianUpdateNodes,
common.TransactionTypeCustodianSlashNodes:
err := node.persistStore.WriteConsensusSnapshot(s, tx)
if err != nil {
return err
}
default:
return nil
}
Expand All @@ -399,13 +403,13 @@ func (node *Node) reloadConsensusState(s *common.Snapshot, tx *common.VersionedT
case common.TransactionTypeNodeAccept:
case common.TransactionTypeNodeRemove:
default:
return node.persistStore.WriteConsensusSnapshot(s, tx)
return nil
}

signer := tx.NodeTransactionExtraAsSigner()
id := signer.Hash().ForNetwork(node.networkId)
if id == s.NodeId {
return node.persistStore.WriteConsensusSnapshot(s, tx)
return nil
}

chain = node.BootChain(id)
Expand All @@ -416,7 +420,7 @@ func (node *Node) reloadConsensusState(s *common.Snapshot, tx *common.VersionedT
if chain.ConsensusInfo == nil {
panic("should never be here")
}
return node.persistStore.WriteConsensusSnapshot(s, tx)
return nil
}

func (node *Node) finalizeNodeAcceptSnapshot(s *common.Snapshot, signers []crypto.Hash) error {
Expand Down
9 changes: 5 additions & 4 deletions kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func SetupNode(custom *config.Custom, store storage.Store, cache *ristretto.Cach
if err != nil {
return nil, fmt.Errorf("LoadConsensusNodes() => %v", err)
}
s, tx := node.persistStore.LastSnapshot()
err = node.reloadConsensusState(s.Snapshot, tx)
if err != nil {
return nil, fmt.Errorf("reloadConsensusState(%v) => %v", s, err)
}

err = node.LoadAllChainsAndGraphTimestamp(node.persistStore, node.networkId)
if err != nil {
Expand Down Expand Up @@ -323,10 +328,6 @@ func (node *Node) LoadConsensusNodes() error {
node.allNodesSortedWithState = cnodes
node.nodeStateSequences = node.buildNodeStateSequences(cnodes, false)
node.acceptedNodeStateSequences = node.buildNodeStateSequences(cnodes, true)
s, tx := node.persistStore.LastSnapshot()
if s != nil {
return node.reloadConsensusState(s.Snapshot, tx)
}
return nil
}

Expand Down
23 changes: 15 additions & 8 deletions storage/badger_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ func readSnapshotsSinceTopology(txn *badger.Txn, topologyOffset, count uint64) (
return snapshots, nil
}

func (s *BadgerStore) LastSnapshot() (*common.SnapshotWithTopologicalOrder, *common.VersionedTransaction) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()

func readLastTopology(txn *badger.Txn) uint64 {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
opts.Reverse = true
Expand All @@ -132,15 +129,25 @@ func (s *BadgerStore) LastSnapshot() (*common.SnapshotWithTopologicalOrder, *com
defer it.Close()

it.Seek(graphTopologyKey(^uint64(0)))
if !it.ValidForPrefix([]byte(graphPrefixTopology)) {
return nil, nil
if it.ValidForPrefix([]byte(graphPrefixTopology)) {
key := it.Item().KeyCopy(nil)
return graphTopologyOrder(key)
}
key := it.Item().KeyCopy(nil)
topo := graphTopologyOrder(key)
return 0
}

func (s *BadgerStore) LastSnapshot() (*common.SnapshotWithTopologicalOrder, *common.VersionedTransaction) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()

topo := readLastTopology(txn)
snaps, err := readSnapshotsSinceTopology(txn, topo, 10)
if err != nil {
panic(err)
}
if topo == 0 && len(snaps) == 0 {
return nil, nil
}
if len(snaps) != 1 {
panic(topo)
}
Expand Down

0 comments on commit a86a4ce

Please sign in to comment.