Skip to content

Commit

Permalink
refactor: upgrade to version 0.5.4 of GP
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Jan 16, 2025
1 parent 1712821 commit 8283884
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions internal/statetransition/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1813,12 +1813,13 @@ func CalculateWorkReportsAndAccumulate(header *block.Header, currentState *state
gasLimit := max(service.TotalGasAccumulation, common.MaxAllocatedGasAccumulation*uint64(common.TotalNumberOfCores)+privSvcGas)

// let (n, o, t, C) = ∆+(g, W∗, (χ, δ, ι, φ), χg ) (eq. 12.21)
maxReports, newAccumulationState, transfers, hashPairs := NewAccumulator(currentState, header, newTimeslot).SequentialDelta(gasLimit, accumulatableWorkReports, state.AccumulationState{
PrivilegedServices: currentState.PrivilegedServices,
ServiceState: currentState.Services,
ValidatorKeys: currentState.ValidatorState.QueuedValidators,
PendingAuthorizersQueues: currentState.PendingAuthorizersQueues,
}, currentState.PrivilegedServices)
maxReports, newAccumulationState, transfers, hashPairs := NewAccumulator(currentState, header, newTimeslot).
SequentialDelta(gasLimit, accumulatableWorkReports, state.AccumulationState{
PrivilegedServices: currentState.PrivilegedServices,
ServiceState: currentState.Services,
ValidatorKeys: currentState.ValidatorState.QueuedValidators,
PendingAuthorizersQueues: currentState.PendingAuthorizersQueues,
}, currentState.PrivilegedServices)

// (χ′, δ†, ι′, φ′) ≡ o (eq. 12.22)
intermediateServiceState := newAccumulationState.ServiceState
Expand Down Expand Up @@ -2423,6 +2424,14 @@ func (a *Accumulator) SequentialDelta(
return uint32(maxReports), newCtx, transfers, hashPairs
}

func getMapKeys[K comparable, V any](m map[K]V) map[K]struct{} {
m2 := make(map[K]struct{}, len(m))
for k := range m {
m2[k] = struct{}{}
}
return m2
}

// ParallelDelta implements equation 12.17 (∆*)
func (a *Accumulator) ParallelDelta(
initialAccState state.AccumulationState,
Expand All @@ -2435,7 +2444,7 @@ func (a *Accumulator) ParallelDelta(
ServiceHashPairs, // accumulation outputs
) {
// Get all unique service indices involved (s)
// s = {rs S w ∈ w, r ∈ wr} ∪ K(f)
// s = {rs | w ∈ w, r ∈ wr} ∪ K(f)
serviceIndices := make(map[block.ServiceId]struct{})

// From work reports
Expand All @@ -2460,6 +2469,12 @@ func (a *Accumulator) ParallelDelta(
var mu sync.Mutex
var wg sync.WaitGroup

// n = ⋃[s∈s]({(∆1(o, w, f , s)o)d ∖ K(d ∖ {s})})
allResultServices := make(map[block.ServiceId]service.ServiceAccount)

// m = ⋃[s∈s](K(d) ∖ K((∆1(o, w, f , s)o)d))
resultServicesExclude := make(map[block.ServiceId]struct{})

for svcId := range serviceIndices {
wg.Add(1)
go func(serviceId block.ServiceId) {
Expand All @@ -2486,13 +2501,39 @@ func (a *Accumulator) ParallelDelta(
})
}

// d′ = {s ↦ ds S s ∈ K(d) ∖ s} ∪ [⋃ s∈s] ((∆1(o, w, f , s)o)d
for serviceId, serviceAccount := range accState.ServiceState {
newAccState.ServiceState[serviceId] = serviceAccount
}
resultServices := maps.Clone(accState.ServiceState)

// (∆1(o, w, f , s)o)d ∖ K(d ∖ {s})
maps.DeleteFunc(resultServices, func(id block.ServiceId, _ service.ServiceAccount) bool {
if id == serviceId {
return false
}

_, ok := initialAccState.ServiceState[id]
return ok
})

maps.Copy(allResultServices, resultServices)

initialServicesKeys := getMapKeys(initialAccState.ServiceState)
maps.DeleteFunc(initialServicesKeys, func(id block.ServiceId, _ struct{}) bool {
_, ok := accState.ServiceState[id]
return ok
})

maps.Copy(resultServicesExclude, initialServicesKeys)

}(svcId)
}

// (d ∪ n) ∖ m
maps.Copy(newAccState.ServiceState, initialAccState.ServiceState)
maps.Copy(newAccState.ServiceState, allResultServices)
maps.DeleteFunc(newAccState.ServiceState, func(id block.ServiceId, _ service.ServiceAccount) bool {
_, ok := resultServicesExclude[id]
return ok
})

wg.Add(1)
go func(serviceId block.ServiceId) {
defer wg.Done()
Expand Down Expand Up @@ -2543,7 +2584,7 @@ func (a *Accumulator) ParallelDelta(
return totalGasUsed, newAccState, allTransfers, accumHashPairs
}

// Delta1 implements equation 12.19 (∆1)
// Delta1 implements equation 12.19 ∆1 (U, ⟦W⟧, D⟨NS → NG⟩, NS ) → (U, ⟦T⟧, H?, NG)
func (a *Accumulator) Delta1(
accumulationState state.AccumulationState,
workReports []block.WorkReport,
Expand Down

0 comments on commit 8283884

Please sign in to comment.