Skip to content

Commit

Permalink
Fix: fix more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Oct 30, 2023
1 parent 2ae4268 commit f560614
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
12 changes: 7 additions & 5 deletions pkg/protocol/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ func (c *Chain) initClaimedWeight() {

func (c *Chain) initAttestedWeight() {
c.LatestAttestedCommitment.OnUpdateWithContext(func(_, latestAttestedCommitment *Commitment, unsubscribeOnUpdate func(subscriptionFactory func() (unsubscribe func()))) {
setupInheritance := func() func() {
return c.AttestedWeight.InheritFrom(latestAttestedCommitment.CumulativeAttestedWeight)
}
if latestAttestedCommitment != nil {
setupInheritance := func() func() {
return c.AttestedWeight.InheritFrom(latestAttestedCommitment.CumulativeAttestedWeight)
}

unsubscribeOnUpdate(setupInheritance)
unsubscribeOnUpdate(setupInheritance)
}
})
}

Expand Down Expand Up @@ -270,7 +272,7 @@ func (c *Chain) DispatchBlock(block *model.Block, src peer.ID) (success bool) {

for _, chain := range append([]*Chain{c}, c.ChildChains.ToSlice()...) {
if chain.VerifyState.Get() {
if targetEngine := chain.SpawnedEngine.Get(); targetEngine != nil && !chain.WarpSync.Get() || targetEngine.BlockRequester.HasTicker(block.ID()) {
if targetEngine := chain.Engine.Get(); targetEngine != nil && !chain.WarpSync.Get() || targetEngine.BlockRequester.HasTicker(block.ID()) {
targetEngine.ProcessBlockFromPeer(block, src)

success = true
Expand Down
92 changes: 58 additions & 34 deletions pkg/protocol/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ import (
type Commitment struct {
*model.Commitment

Parent reactive.Variable[*Commitment]
Children reactive.Set[*Commitment]
MainChild reactive.Variable[*Commitment]
SpawnedChain reactive.Variable[*Chain]
Chain reactive.Variable[*Chain]
Engine reactive.Variable[*engine.Engine]
RequestAttestations reactive.Variable[bool]
RequestBlocks reactive.Variable[bool]
RequestedBlocksReceived reactive.Variable[bool]
Weight reactive.Variable[uint64]
AttestedWeight reactive.Variable[uint64]
CumulativeAttestedWeight reactive.Variable[uint64]
IsSolid reactive.Event
IsAttested reactive.Event
IsVerified reactive.Event
IsRoot reactive.Event
IsEvicted reactive.Event
Parent reactive.Variable[*Commitment]
Children reactive.Set[*Commitment]
MainChild reactive.Variable[*Commitment]
SpawnedChain reactive.Variable[*Chain]
Chain reactive.Variable[*Chain]
Engine reactive.Variable[*engine.Engine]
RequestAttestations reactive.Variable[bool]
RequestBlocks reactive.Variable[bool]
RequestedBlocksReceived reactive.Variable[bool]
Weight reactive.Variable[uint64]
AttestedWeight reactive.Variable[uint64]
CumulativeAttestedWeight reactive.Variable[uint64]
IsSolid reactive.Event
IsAttested reactive.Event
IsVerified reactive.Event
IsRoot reactive.Event
IsEvicted reactive.Event
IsAboveLatestVerifiedCommitment reactive.Variable[bool]
ReplayBlocks reactive.Variable[bool]

protocol *Protocol
isDirectlyAboveLatestAttestedCommitment reactive.Variable[bool]
Expand All @@ -45,23 +47,25 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
c := &Commitment{
Commitment: commitment,

Parent: reactive.NewVariable[*Commitment](),
MainChild: reactive.NewVariable[*Commitment](),
Children: reactive.NewSet[*Commitment](),
SpawnedChain: reactive.NewVariable[*Chain](),
Chain: reactive.NewVariable[*Chain](),
Engine: reactive.NewVariable[*engine.Engine](),
RequestAttestations: reactive.NewVariable[bool](),
RequestBlocks: reactive.NewVariable[bool](),
RequestedBlocksReceived: reactive.NewVariable[bool](),
Weight: reactive.NewVariable[uint64](),
AttestedWeight: reactive.NewVariable[uint64](func(currentValue uint64, newValue uint64) uint64 { return max(currentValue, newValue) }),
CumulativeAttestedWeight: reactive.NewVariable[uint64](),
IsSolid: reactive.NewEvent(),
IsAttested: reactive.NewEvent(),
IsVerified: reactive.NewEvent(),
IsRoot: reactive.NewEvent(),
IsEvicted: reactive.NewEvent(),
Parent: reactive.NewVariable[*Commitment](),
MainChild: reactive.NewVariable[*Commitment](),
Children: reactive.NewSet[*Commitment](),
SpawnedChain: reactive.NewVariable[*Chain](),
Chain: reactive.NewVariable[*Chain](),
Engine: reactive.NewVariable[*engine.Engine](),
RequestAttestations: reactive.NewVariable[bool](),
RequestBlocks: reactive.NewVariable[bool](),
RequestedBlocksReceived: reactive.NewVariable[bool](),
Weight: reactive.NewVariable[uint64](),
AttestedWeight: reactive.NewVariable[uint64](func(currentValue uint64, newValue uint64) uint64 { return max(currentValue, newValue) }),
CumulativeAttestedWeight: reactive.NewVariable[uint64](),
IsSolid: reactive.NewEvent(),
IsAttested: reactive.NewEvent(),
IsVerified: reactive.NewEvent(),
IsRoot: reactive.NewEvent(),
IsEvicted: reactive.NewEvent(),
IsAboveLatestVerifiedCommitment: reactive.NewVariable[bool](),
ReplayBlocks: reactive.NewVariable[bool](),

protocol: protocol,
isDirectlyAboveLatestAttestedCommitment: reactive.NewVariable[bool](),
Expand All @@ -70,6 +74,26 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
isBelowWarpSyncThreshold: reactive.NewEvent(),
}

c.Parent.OnUpdateWithContext(func(_, parent *Commitment, unsubscribeOnUpdate func(subscriptionFactory func() (unsubscribe func()))) {
if parent != nil {
unsubscribeOnUpdate(func() func() {
return c.IsAboveLatestVerifiedCommitment.InheritFrom(reactive.NewDerivedVariable2(func(parentAboveLatestVerifiedCommitment, directlyAboveLatestVerifiedCommitment bool) bool {
return parentAboveLatestVerifiedCommitment || directlyAboveLatestVerifiedCommitment
}, parent.IsAboveLatestVerifiedCommitment, c.isDirectlyAboveLatestVerifiedCommitment))
})
}
})

c.Chain.OnUpdateWithContext(func(_, chain *Chain, unsubscribeOnUpdate func(subscriptionFactory func() (unsubscribe func()))) {
if chain != nil {
unsubscribeOnUpdate(func() func() {
return c.ReplayBlocks.InheritFrom(reactive.NewDerivedVariable3(func(spawnedEngine *engine.Engine, warpSyncing, isAboveLatestVerifiedCommitment bool) bool {
return spawnedEngine != nil && !warpSyncing && isAboveLatestVerifiedCommitment
}, chain.SpawnedEngine, chain.WarpSync, c.IsAboveLatestVerifiedCommitment))
})
}
})

c.Parent.OnUpdateOnce(func(_, parent *Commitment) {
parent.registerChild(c)

Expand Down
12 changes: 6 additions & 6 deletions pkg/protocol/protocol_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func NewBlocksProtocol(protocol *Protocol) *BlocksProtocol {

protocol.Constructed.OnTrigger(func() {
protocol.CommitmentCreated.Hook(func(commitment *Commitment) {
commitment.isDirectlyAboveLatestVerifiedCommitment.OnUpdate(func(_, isDirectlyAboveLatestVerifiedCommitment bool) {
if !isDirectlyAboveLatestVerifiedCommitment {
return
}
commitment.ReplayBlocks.OnUpdate(func(_, replayBlocks bool) {
if replayBlocks {
b.LogDebug("replaying blocks", "commitmentID", commitment.ID())

for _, droppedBlock := range b.droppedBlocksBuffer.GetValues(commitment.ID()) {
b.ProcessResponse(droppedBlock.A, droppedBlock.B)
for _, droppedBlock := range b.droppedBlocksBuffer.GetValues(commitment.ID()) {
b.ProcessResponse(droppedBlock.A, droppedBlock.B)
}
}
})
})
Expand Down

0 comments on commit f560614

Please sign in to comment.