Skip to content

Commit

Permalink
wip optimize e1, e2, r2
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed Feb 13, 2025
1 parent fa5959b commit 03bb4ca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 90 deletions.
4 changes: 4 additions & 0 deletions sequencer/task/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ func (p *Proposer) ChooseFirstExtendEndorsePair(shuffleEndorseCandidates bool, p
var ret *attacher.IncrementalAttacher
for _, endorse := range endorseCandidates {
p.Tracef(TraceTagChooseFirstExtendEndorsePair, "check endorse candidate: %s", endorse.IDShortString)

select {
case <-p.ctx.Done():
return nil
default:
}

if !ledger.ValidTransactionPace(endorse.Timestamp(), p.targetTs) {
// cannot endorse candidate because of ledger time constraint
p.Tracef(TraceTagChooseFirstExtendEndorsePair, ">>>>>>>>>>>>>>> !ledger.ValidTransactionPace")
Expand Down Expand Up @@ -178,6 +180,7 @@ func (p *Proposer) chooseEndorseExtendPairAttacher(endorse *vertex.WrappedTx, ex
}
a, err = attacher.NewIncrementalAttacher(p.Name, p, p.targetTs, extend, endorse)
if err != nil {
p.Task.slotData.markCombinationChecked(false, extend, endorse)
p.Tracef(TraceTagChooseFirstExtendEndorsePair, "%s can't extend %s and endorse %s: %v", p.targetTs.String, extend.IDShortString, endorse.IDShortString, err)
continue
}
Expand Down Expand Up @@ -205,6 +208,7 @@ func (p *Proposer) chooseEndorseExtendPairAttacher(endorse *vertex.WrappedTx, ex
p.targetTs.String, extend.IDShortString, endorse.IDShortString, util.Th(a.LedgerCoverage()))
a.Close()
}
p.Task.slotData.markCombinationChecked(true, extend, endorse)
}
return ret
}
Expand Down
21 changes: 2 additions & 19 deletions sequencer/task/proposer_endorse1.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,8 @@ func endorse1ProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bool)
// use pair with new tag-along outputs
return true
}
return p.Task.slotData.checkIfCombinationIsNew(extend, endorse)

//combHash := extendEndorseCombinationHash(extend, endorse)
//if !p.Task.slotData.alreadyCheckedExtendEndorseCombination.Contains(combHash) {
// // it is new pair. Use it and save it as already checked -> next time will be filtered out
// p.Task.slotData.alreadyCheckedExtendEndorseCombination.Insert(combHash)
// return true
//}
//
//pair := extendEndorsePair{
// extend: extend,
// endorse: endorse,
//}
//if !p.Task.slotData.alreadyCheckedE1.Contains(pair) {
// // it is new pair. Use it and save it as already checked -> next time will be filtered out
// p.Task.slotData.alreadyCheckedE1.Insert(pair)
// return true
//}
//return false
alreadyChecked, _ := p.Task.slotData.wasCombinationChecked(extend, endorse)
return !alreadyChecked
})

if a == nil {
Expand Down
34 changes: 7 additions & 27 deletions sequencer/task/proposer_endorse2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/lunfardo314/proxima/core/attacher"
"github.com/lunfardo314/proxima/core/vertex"
)

const TraceTagEndorse2Proposer = "propose-endorse2"
Expand All @@ -23,7 +24,10 @@ func endorse2ProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bool)
}

// Check all pairs, in descending order
a := p.ChooseFirstExtendEndorsePair(false, nil)
a := p.ChooseFirstExtendEndorsePair(false, func(extend vertex.WrappedOutput, endorse *vertex.WrappedTx) bool {
checked, consistent := p.Task.slotData.wasCombinationChecked(extend, endorse)
return !checked || consistent
})
if a == nil {
p.Tracef(TraceTagEndorse2Proposer, "propose: ChooseFirstExtendEndorsePair returned nil")
return nil, false
Expand Down Expand Up @@ -53,36 +57,12 @@ func endorse2ProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bool)
continue
}
if !newOutputArrived {
if !p.Task.slotData.checkIfCombinationIsNew(extending, endorsing, endorsementCandidate) {
checked, _ := p.Task.slotData.wasCombinationChecked(extending, endorsing, endorsementCandidate)
if checked {
continue
}
}

//triplet := extendEndorseTriplet{
// extend: extending,
// endorse1: endorsing,
// endorse2: endorsementCandidate,
//}
//if !newOutputArrived {
// checkedInThePast := false
// p.slotData.withWriteLock(func() {
// // optimization: skipping repeating triplets if new outputs didn't arrive meanwhile
// checkedInThePast = p.slotData.alreadyCheckedTriplets.Contains(triplet)
// if !checkedInThePast {
// // assume invariance wrt order of endorsements
// // check triplet with swapped endorsements
// checkedInThePast = p.slotData.alreadyCheckedTriplets.Contains(extendEndorseTriplet{
// extend: extending,
// endorse1: endorsementCandidate,
// endorse2: endorsing,
// })
// }
// })
// if checkedInThePast {
// continue
// }
//}

if err := a.InsertEndorsement(endorsementCandidate); err == nil {
addedSecond = true
break //>>>> return attacher
Expand Down
34 changes: 7 additions & 27 deletions sequencer/task/proposer_endorse2rnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/lunfardo314/proxima/core/attacher"
"github.com/lunfardo314/proxima/core/vertex"
)

const TraceTagEndorse2RndProposer = "propose-endorse2rnd"
Expand All @@ -23,7 +24,10 @@ func endorse2RndProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bo
}

// Check peers in RANDOM order
a := p.ChooseFirstExtendEndorsePair(true, nil)
a := p.ChooseFirstExtendEndorsePair(true, func(extend vertex.WrappedOutput, endorse *vertex.WrappedTx) bool {
checked, consistent := p.Task.slotData.wasCombinationChecked(extend, endorse)
return !checked || consistent
})
if a == nil {
p.Tracef(TraceTagEndorse2RndProposer, "propose: ChooseFirstExtendEndorsePair returned nil")
return nil, false
Expand Down Expand Up @@ -54,35 +58,11 @@ func endorse2RndProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bo
continue
}
if !newOutputArrived {
if !p.slotData.checkIfCombinationIsNew(extending, endorsing, endorsementCandidate) {
checked, _ := p.slotData.wasCombinationChecked(extending, endorsing, endorsementCandidate)
if checked {
continue
}
}
//
//triplet := extendEndorseTriplet{
// extend: extending,
// endorse1: endorsing,
// endorse2: endorsementCandidate,
//}
//if !newOutputArrived {
// checkedInThePast := false
// p.slotData.withWriteLock(func() {
// // optimization: skipping repeating triplets if new outputs didn't arrive meanwhile
// checkedInThePast = p.slotData.alreadyCheckedTriplets.Contains(triplet)
// if !checkedInThePast {
// // assume invariance wrt order of endorsements
// // check triplet with swapped endorsements
// checkedInThePast = p.slotData.alreadyCheckedTriplets.Contains(extendEndorseTriplet{
// extend: extending,
// endorse1: endorsementCandidate,
// endorse2: endorsing,
// })
// }
// })
// if checkedInThePast {
// continue
// }
//}

if err := a.InsertEndorsement(endorsementCandidate); err == nil {
// remember triplet for the next check, same list for e2 and r2
Expand Down
8 changes: 4 additions & 4 deletions sequencer/task/proposer_endorse3.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func endorse3ProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bool)
continue
}
if !newOutputArrived {
continue
}
if !p.Task.slotData.checkIfCombinationIsNew(extending, endorsing, endorsementCandidate) {
continue
checked, _ := p.Task.slotData.wasCombinationChecked(extending, endorsing, endorsementCandidate)
if checked {
continue
}
}

if err := a.InsertEndorsement(endorsementCandidate); err == nil {
Expand Down
26 changes: 13 additions & 13 deletions sequencer/task/slot_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type (
lastTimeBacklogCheckedE2 time.Time
lastTimeBacklogCheckedR2 time.Time
alreadyCheckedTriplets set.Set[extendEndorseTriplet] //shared by e2 and r2
// extend proposers optimization
alreadyCheckedExtendEndorseCombination set.Set[combinationHash]
// extend proposers optimization. If combination was already checked, flag indicates if it was consistent
alreadyCheckedExtendEndorseCombination map[combinationHash]bool
}

combinationHash [8]byte
Expand All @@ -61,7 +61,7 @@ func NewSlotData(slot ledger.Slot) *SlotData {
proposalsByProposer: make(map[string]int),
alreadyCheckedE1: set.New[extendEndorsePair](),
alreadyCheckedTriplets: set.New[extendEndorseTriplet](),
alreadyCheckedExtendEndorseCombination: set.New[combinationHash](),
alreadyCheckedExtendEndorseCombination: make(map[combinationHash]bool),
}
}

Expand Down Expand Up @@ -138,29 +138,29 @@ func extendEndorseCombinationHash(extend vertex.WrappedOutput, endorse ...*verte
})

var buf bytes.Buffer

for i := range endorseSorted {
buf.Write(endorseSorted[i].ID[:])
}
buf.Write(extend.VID.ID[:])
buf.WriteByte(extend.Index)
buf.WriteByte(byte(len(endorse))) // need this to distinguish between target extend-endorse combinations

retSlice := blake2b.Sum256(buf.Bytes())
copy(ret[:], retSlice[:])
return
}

// checkIfCombinationIsNew checks combination and inserts into the list. Returns true if it is new combination
func (s *SlotData) checkIfCombinationIsNew(extend vertex.WrappedOutput, endorse ...*vertex.WrappedTx) bool {
combHash := extendEndorseCombinationHash(extend, endorse...)
// wasCombinationChecked checks combination and inserts into the list. Returns true if it is new combination
func (s *SlotData) wasCombinationChecked(extend vertex.WrappedOutput, endorse ...*vertex.WrappedTx) (checked bool, consistent bool) {
s.mutex.RLock()
defer s.mutex.RUnlock()

checked, consistent = s.alreadyCheckedExtendEndorseCombination[extendEndorseCombinationHash(extend, endorse...)]
return
}

func (s *SlotData) markCombinationChecked(consistent bool, extend vertex.WrappedOutput, endorse ...*vertex.WrappedTx) {
s.mutex.Lock()
defer s.mutex.Unlock()

if s.alreadyCheckedExtendEndorseCombination.Contains(combHash) {
return false
}
s.alreadyCheckedExtendEndorseCombination.Insert(combHash)
return true
s.alreadyCheckedExtendEndorseCombination[extendEndorseCombinationHash(extend, endorse...)] = consistent
}

0 comments on commit 03bb4ca

Please sign in to comment.