Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: accumulate upgrade to version 0.5.4 of GP #227

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 +2501,28 @@ 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)
}

Expand Down Expand Up @@ -2535,6 +2568,14 @@ func (a *Accumulator) ParallelDelta(
// Wait for all goroutines to complete
wg.Wait()

// (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
})

// Sort accumulation pairs by service ID to ensure deterministic output
sort.Slice(accumHashPairs, func(i, j int) bool {
return accumHashPairs[i].ServiceId < accumHashPairs[j].ServiceId
Expand All @@ -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
Loading