From c9be69ba28db129892fe70db3ee2fc4732fba224 Mon Sep 17 00:00:00 2001 From: Cassandra Heart Date: Mon, 18 Nov 2024 01:45:50 -0600 Subject: [PATCH] seniority map repair --- .../token/application/token_handle_mint.go | 1 + .../token/token_execution_engine.go | 97 ++++++++++++++++++- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/node/execution/intrinsics/token/application/token_handle_mint.go b/node/execution/intrinsics/token/application/token_handle_mint.go index 5ec7c09..4caf103 100644 --- a/node/execution/intrinsics/token/application/token_handle_mint.go +++ b/node/execution/intrinsics/token/application/token_handle_mint.go @@ -23,6 +23,7 @@ const PROOF_FRAME_CUTOFF = 1 const PROOF_FRAME_RING_RESET = 5750 const PROOF_FRAME_RING_RESET_2 = 7650 const PROOF_FRAME_RING_RESET_3 = 13369 +const PROOF_FRAME_SENIORITY_REPAIR = 59029 func (a *TokenApplication) handleMint( currentFrameNumber uint64, diff --git a/node/execution/intrinsics/token/token_execution_engine.go b/node/execution/intrinsics/token/token_execution_engine.go index eab5303..ddd83b7 100644 --- a/node/execution/intrinsics/token/token_execution_engine.go +++ b/node/execution/intrinsics/token/token_execution_engine.go @@ -890,6 +890,10 @@ func (e *TokenExecutionEngine) ProcessFrame( ProcessJoinsAndLeaves(joinReqs, leaveReqs, app, e.peerSeniority, frame) + if frame.FrameNumber == application.PROOF_FRAME_SENIORITY_REPAIR { + e.performSeniorityMapRepair(activeMap, frame) + } + err = e.clockStore.PutPeerSeniorityMap( txn, e.intrinsicFilter, @@ -936,6 +940,95 @@ func (e *TokenExecutionEngine) ProcessFrame( return app.Tries, nil } +func (e *TokenExecutionEngine) performSeniorityMapRepair( + activeMap *PeerSeniority, + frame *protobufs.ClockFrame, +) { + if e.pubSub.GetNetwork() != 0 { + return + } + + e.logger.Info( + "repairing seniority map from historic data, this may take a while", + ) + + RebuildPeerSeniority(0) + for f := uint64(53028); f < frame.FrameNumber; f++ { + frame, _, err := e.clockStore.GetDataClockFrame(e.intrinsicFilter, f, false) + if err != nil { + break + } + + reqs, _, _ := application.GetOutputsFromClockFrame(frame) + + for _, req := range reqs.Requests { + switch t := req.Request.(type) { + case *protobufs.TokenRequest_Join: + if t.Join.Announce != nil && len( + t.Join.Announce.PublicKeySignaturesEd448, + ) > 0 { + addr, err := e.getAddressFromSignature( + t.Join.Announce.PublicKeySignaturesEd448[0], + ) + if err != nil { + continue + } + + peerId, err := e.getPeerIdFromSignature( + t.Join.Announce.PublicKeySignaturesEd448[0], + ) + if err != nil { + continue + } + + additional := uint64(0) + + _, prfs, err := e.coinStore.GetPreCoinProofsForOwner(addr) + for _, pr := range prfs { + if pr.IndexProof == nil && pr.Difficulty == 0 && pr.Commitment == nil { + // approximate average per interval: + add := new(big.Int).SetBytes(pr.Amount) + add.Quo(add, big.NewInt(58800000)) + if add.Cmp(big.NewInt(4000000)) > 0 { + add = big.NewInt(4000000) + } + additional = add.Uint64() + } + } + + if err != nil && !errors.Is(err, store.ErrNotFound) { + continue + } + peerIds := []string{peerId.String()} + if len(t.Join.Announce.PublicKeySignaturesEd448) > 1 { + for _, announce := range t.Join.Announce.PublicKeySignaturesEd448[1:] { + peerId, err := e.getPeerIdFromSignature( + announce, + ) + if err != nil { + continue + } + + peerIds = append(peerIds, peerId.String()) + } + } + + aggregated := GetAggregatedSeniority(peerIds).Uint64() + total := aggregated + additional + sen, ok := (*activeMap)[string(addr)] + + if !ok || sen.seniority < total { + (*activeMap)[string(addr)] = PeerSeniorityItem{ + seniority: total, + addr: string(addr), + } + } + } + } + } + } +} + func ProcessJoinsAndLeaves( joinReqs []PeerSeniorityItem, leaveReqs []PeerSeniorityItem, @@ -1052,9 +1145,7 @@ func (e *TokenExecutionEngine) VerifyExecution( } parent, tries, err := e.clockStore.GetDataClockFrame( - append( - p2p.GetBloomFilter(application.TOKEN_ADDRESS, 256, 3), - ), + p2p.GetBloomFilter(application.TOKEN_ADDRESS, 256, 3), frame.FrameNumber-1, false, )