Skip to content

Commit

Permalink
refactor: bug fixes and PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Nov 14, 2024
1 parent 4c5ef08 commit eabccc5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
10 changes: 4 additions & 6 deletions internal/polkavm/host_call/general_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,12 @@ func Info(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, serviceId
sID := regs[polkavm.A0]
omega1 := regs[polkavm.A1]

t := serviceState[serviceId]
t, exists := serviceState[serviceId]
if sID != math.MaxUint32 {
var exists bool
t, exists = serviceState[block.ServiceId(sID)]
if !exists {
regs[polkavm.A0] = uint32(NONE)
return gas, regs, mem, nil
}
}
if !exists {
return gas, withCode(regs, NONE), mem, nil
}

accountInfo := AccountInfo{
Expand Down
56 changes: 30 additions & 26 deletions internal/statetransition/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,20 @@ func calculateWorkReportsAndAccumulate(
newWorkReportsQueue state.PendingAuthorizersQueues,
hashPairs ServiceHashPairs,
) {
// TODO (156) ∀w ∈ w, ∀r ∈ wr ∶ rc = δ[rs]c
// Ensure all service code hashes match
//var expectedCodeHash *crypto.Hash
//for _, report := range workReports {
// for _, result := range report.WorkResults {
// if result.ServiceId == serviceIndex {
// if expectedCodeHash == nil {
// expectedCodeHash = &result.ServiceHashCode
// } else if *expectedCodeHash != result.ServiceHashCode {
// return nil, 0, fmt.Errorf("inconsistent service code hash for service %d", serviceIndex)
// }
// }
// }
//}

//TODO (166) WQ ≡ E([D(w) S w <− W, (wx)p ≠ ∅ ∨ wl ≠ {}], {ξ)
var queuedWorkReports []state.WorkReportWithUnAccumulatedDependencies
Expand Down Expand Up @@ -1257,7 +1271,7 @@ func SequentialDelta(
gasUsed, newCtx, transfers, hashPairs := ParallelDelta(
ctx,
workReports[:maxReports],
privileged,
privileged.AmountOfGasPerServiceId,
)

// If we have remaining reports and gas, process recursively (∆+)
Expand Down Expand Up @@ -1285,7 +1299,7 @@ func SequentialDelta(
func ParallelDelta(
initialAccState state.AccumulationState,
workReports []block.WorkReport,
privileged service.PrivilegedServices,
privilegedGas map[block.ServiceId]uint64, // D⟨NS → NG⟩
) (
uint64, // total gas used
state.AccumulationState, // updated context
Expand All @@ -1303,16 +1317,17 @@ func ParallelDelta(
}
}

// TODO where is it described in the paper?
// From privileged gas assignments
//for svcId := range privileged.AmountOfGasPerServiceId {
// serviceIndices[svcId] = struct{}{}
//}
for svcId := range privilegedGas {
serviceIndices[svcId] = struct{}{}
}

var totalGasUsed uint64
var allTransfers []service.DeferredTransfer
accumHashPairs := make(ServiceHashPairs, 0)
newAccState := state.AccumulationState{}
newAccState := state.AccumulationState{
ServiceState: make(service.ServiceState),
}

var mu sync.Mutex
var wg sync.WaitGroup
Expand All @@ -1323,7 +1338,7 @@ func ParallelDelta(
defer wg.Done()

// Process single service using Delta1
accState, deferredTransfers, resultHash, gasUsed := Delta1(initialAccState, workReports, privileged.AmountOfGasPerServiceId, serviceId)
accState, deferredTransfers, resultHash, gasUsed := Delta1(initialAccState, workReports, privilegedGas, serviceId)
mu.Lock()
defer mu.Unlock()

Expand All @@ -1349,35 +1364,39 @@ func ParallelDelta(
}
}(svcId)
}

wg.Add(1)
go func(serviceId block.ServiceId) {
defer wg.Done()

// Process single service using Delta1
accState, _, _, _ := Delta1(initialAccState, workReports, privileged.AmountOfGasPerServiceId, serviceId)
accState, _, _, _ := Delta1(initialAccState, workReports, privilegedGas, serviceId)
mu.Lock()
defer mu.Unlock()

newAccState.PrivilegedServices = accState.PrivilegedServices

}(initialAccState.PrivilegedServices.ManagerServiceId)

wg.Add(1)
go func(serviceId block.ServiceId) {
defer wg.Done()

// Process single service using Delta1
accState, _, _, _ := Delta1(initialAccState, workReports, privileged.AmountOfGasPerServiceId, serviceId)
accState, _, _, _ := Delta1(initialAccState, workReports, privilegedGas, serviceId)
mu.Lock()
defer mu.Unlock()

newAccState.ValidatorKeys = accState.ValidatorKeys

}(initialAccState.PrivilegedServices.AssignServiceId)

wg.Add(1)
go func(serviceId block.ServiceId) {
defer wg.Done()

// Process single service using Delta1
accState, _, _, _ := Delta1(initialAccState, workReports, privileged.AmountOfGasPerServiceId, serviceId)
accState, _, _, _ := Delta1(initialAccState, workReports, privilegedGas, serviceId)
mu.Lock()
defer mu.Unlock()

Expand Down Expand Up @@ -1434,21 +1453,6 @@ func Delta1(
}
}

// TODO clarify why is it needed and where is it described in the paper
// Ensure all service code hashes match
//var expectedCodeHash *crypto.Hash
//for _, report := range workReports {
// for _, result := range report.WorkResults {
// if result.ServiceId == serviceIndex {
// if expectedCodeHash == nil {
// expectedCodeHash = &result.ServiceHashCode
// } else if *expectedCodeHash != result.ServiceHashCode {
// return nil, 0, fmt.Errorf("inconsistent service code hash for service %d", serviceIndex)
// }
// }
// }
//}

// TODO pass in state and header
// Invoke VM for accumulation (ΨA)
return invocations.NewAccumulator(nil, nil).Invoke(
Expand Down

0 comments on commit eabccc5

Please sign in to comment.