-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lunfardo314
committed
Feb 13, 2025
1 parent
9186ef6
commit 079ae3a
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package task | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/lunfardo314/proxima/core/attacher" | ||
"github.com/lunfardo314/proxima/core/vertex" | ||
) | ||
|
||
const TraceTagEndorse3RndProposer = "propose-endorse3rnd" | ||
|
||
func init() { | ||
registerProposerStrategy(&Strategy{ | ||
Name: "endorse3rnd", | ||
ShortName: "r3", | ||
GenerateProposal: endorse3RndProposeGenerator, | ||
}) | ||
} | ||
|
||
func endorse3RndProposeGenerator(p *Proposer) (*attacher.IncrementalAttacher, bool) { | ||
if p.targetTs.IsSlotBoundary() { | ||
// the proposer does not generate branch transactions | ||
return nil, true | ||
} | ||
|
||
// Check all pairs, in descending order | ||
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(TraceTagEndorse3RndProposer, "propose: ChooseFirstExtendEndorsePair returned nil") | ||
return nil, false | ||
} | ||
endorsing0 := a.Endorsing()[0] | ||
extending := a.Extending() | ||
if !a.Completed() { | ||
a.Close() | ||
p.Tracef(TraceTagEndorse3RndProposer, "proposal [extend=%s, endorsing=%s] not complete 1", extending.IDShortString, endorsing0.IDShortString) | ||
return nil, false | ||
} | ||
|
||
newOutputArrived := p.Backlog().ArrivedOutputsSince(p.slotData.lastTimeBacklogCheckedE3) | ||
p.slotData.lastTimeBacklogCheckedE3 = time.Now() | ||
|
||
// then try to add one endorsement more | ||
var endorsing1 *vertex.WrappedTx | ||
|
||
for _, endorsementCandidate := range p.Backlog().CandidatesToEndorseShuffled(p.targetTs) { | ||
select { | ||
case <-p.ctx.Done(): | ||
a.Close() | ||
return nil, true | ||
default: | ||
} | ||
if endorsementCandidate == endorsing0 { | ||
continue | ||
} | ||
if !newOutputArrived { | ||
checked, _ := p.Task.slotData.wasCombinationChecked(extending, endorsing0, endorsementCandidate) | ||
if checked { | ||
continue | ||
} | ||
} | ||
if err := a.InsertEndorsement(endorsementCandidate); err == nil { | ||
p.Task.slotData.markCombinationChecked(true, extending, endorsing0, endorsementCandidate) | ||
endorsing1 = endorsementCandidate | ||
break //>>>> second endorsement | ||
} else { | ||
p.Task.slotData.markCombinationChecked(false, extending, endorsing0, endorsementCandidate) | ||
} | ||
p.Tracef(TraceTagEndorse3RndProposer, "failed to include endorsement target %s", endorsementCandidate.IDShortString) | ||
} | ||
if endorsing1 == nil { | ||
// no need to repeat job of endorse1 | ||
a.Close() | ||
return nil, false | ||
} | ||
// try to add 3rd endorsement | ||
var endorsing2 *vertex.WrappedTx | ||
|
||
for _, endorsementCandidate := range p.Backlog().CandidatesToEndorseShuffled(p.targetTs) { | ||
select { | ||
case <-p.ctx.Done(): | ||
a.Close() | ||
return nil, true | ||
default: | ||
} | ||
if endorsementCandidate == endorsing0 || endorsementCandidate == endorsing1 { | ||
continue | ||
} | ||
if !newOutputArrived { | ||
checked, _ := p.Task.slotData.wasCombinationChecked(extending, endorsing0, endorsing1, endorsementCandidate) | ||
if checked { | ||
continue | ||
} | ||
} | ||
if err := a.InsertEndorsement(endorsementCandidate); err == nil { | ||
p.Task.slotData.markCombinationChecked(true, extending, endorsing0, endorsing1, endorsementCandidate) | ||
endorsing2 = endorsementCandidate | ||
break //>>>> third endorsement | ||
} else { | ||
p.Task.slotData.markCombinationChecked(false, extending, endorsing0, endorsing1, endorsementCandidate) | ||
} | ||
p.Tracef(TraceTagEndorse3RndProposer, "failed to include endorsement target %s", endorsementCandidate.IDShortString) | ||
} | ||
if endorsing2 == nil { | ||
// no need to repeat job of endorse2 | ||
a.Close() | ||
return nil, false | ||
} | ||
|
||
// insert tag along and delegation outputs | ||
p.insertInputs(a) | ||
|
||
if !a.Completed() { | ||
a.Close() | ||
endorsing0 = a.Endorsing()[0] | ||
endorsing1 = a.Endorsing()[1] | ||
endorsing2 = a.Endorsing()[2] | ||
extending = a.Extending() | ||
p.Tracef(TraceTagEndorse3RndProposer, "proposal [extend=%s, endorsing=%s, %s, %s] not complete 2", | ||
extending.IDShortString, endorsing0.IDShortString, endorsing1.IDShortString, endorsing2.IDShortString) | ||
return nil, false | ||
} | ||
return a, false | ||
} |