Skip to content

Commit

Permalink
refactor: upgrade accumulation to GP v0.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Dec 16, 2024
1 parent 83fc552 commit 6887bcd
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 178 deletions.
6 changes: 3 additions & 3 deletions internal/block/preimage.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package block

// PreimageItem represents a single preimage item in the extrinsic
// Preimage represents a single preimage item in the extrinsic
type Preimage struct {
ServiceIndex uint32
Data []byte
ServiceIndex uint32 // s
Data []byte // p
}

type PreimageExtrinsic []Preimage
2 changes: 1 addition & 1 deletion internal/polkavm/host_call/accumulate_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Assign(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair)
if err := mem.Read(uint32(addr)+uint32(32*i), bytes); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}
ctxPair.RegularCtx.AccumulationState.WorkReportsQueue[core][i] = crypto.Hash(bytes)
ctxPair.RegularCtx.AccumulationState.PendingAuthorizersQueues[core][i] = crypto.Hash(bytes)
}
return gas, withCode(regs, OK), mem, ctxPair, nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/polkavm/host_call/accumulate_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccumulate(t *testing.T) {
validatorKeys := generate(t, testutils.RandomValidatorKey, common.NumberOfValidators)
checkpointCtx := AccumulateContext{
AccumulationState: state.AccumulationState{
WorkReportsQueue: state.PendingAuthorizersQueues{
PendingAuthorizersQueues: state.PendingAuthorizersQueues{
0: [state.PendingAuthorizersQueueSize]crypto.Hash(
generate(t, testutils.RandomHash, state.PendingAuthorizersQueueSize),
),
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestAccumulate(t *testing.T) {
},
expectedX: AccumulateContext{
AccumulationState: state.AccumulationState{
WorkReportsQueue: state.PendingAuthorizersQueues{
PendingAuthorizersQueues: state.PendingAuthorizersQueues{
1: [state.PendingAuthorizersQueueSize]crypto.Hash(authHashes),
},
},
Expand Down
10 changes: 5 additions & 5 deletions internal/state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ type AccumulationHistory [jamtime.TimeslotsPerEpoch]map[crypto.Hash]struct{} //

// AccumulationState characterization of state components (equation 174 v0.4.5)
type AccumulationState struct {
ServiceState service.ServiceState // Service accounts δ (d ∈ D⟨NS → A⟩)
ValidatorKeys safrole.ValidatorsData // Validator keys ι (i ∈ ⟦K⟧V)
WorkReportsQueue [common.TotalNumberOfCores][PendingAuthorizersQueueSize]crypto.Hash // Queue of work-reports (q ∈ C⟦H⟧QHC)
PrivilegedServices service.PrivilegedServices // Privileges state (x ∈ (NS, NS, NS, D⟨NS → NG⟩))
ServiceState service.ServiceState // Service accounts δ (d ∈ D⟨NS → A⟩)
ValidatorKeys safrole.ValidatorsData // Validator keys ι (i ∈ ⟦K⟧V)
PendingAuthorizersQueues [common.TotalNumberOfCores][PendingAuthorizersQueueSize]crypto.Hash // Queue of authorizers (q ∈ C⟦H⟧QHC)
PrivilegedServices service.PrivilegedServices // Privileges state (x ∈ (NS, NS, NS, D⟨NS → NG⟩))
}

// AccumulationOperand represents a single operand for accumulation (equation 179 v0.4.5)
Expand All @@ -64,7 +64,7 @@ type AccumulationResult struct {
AccumulationRoot *crypto.Hash // r - Optional accumulation result hash
CoreAssignments PendingAuthorizersQueues // c - Core authorizations queue
NewServices service.ServiceState // n - Newly created services mapping
PrivilegedUpdates struct { // p - Privileged service updates
PrivilegedUpdates struct { // p - Privileged service updates
ManagerServiceId block.ServiceId // m - Manager service
AssignServiceId block.ServiceId // a - Assign service
DesignateServiceId block.ServiceId // v - Designate service
Expand Down
59 changes: 31 additions & 28 deletions internal/statetransition/accumulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package statetransition

import (
"errors"
"github.com/eigerco/strawberry/internal/jamtime"
"log"
"maps"

Expand All @@ -20,20 +21,22 @@ const (
AccumulateCost = 10
)

func NewAccumulator(state *state.State, header *block.Header) *Accumulator {
func NewAccumulator(state *state.State, header *block.Header, newTimeslot jamtime.Timeslot) *Accumulator {
return &Accumulator{
header: header,
state: state,
header: header,
state: state,
newTimeslot: newTimeslot,
}
}

type Accumulator struct {
header *block.Header
state *state.State
header *block.Header
state *state.State
newTimeslot jamtime.Timeslot
}

// InvokePVM ΨA(U, N_S , N_G, ⟦O⟧) → (U, ⟦T⟧, H?, N_G) Equation (B.8)
func (a *Accumulator) InvokePVM(accState state.AccumulationState, serviceIndex block.ServiceId, gas uint64, accOperand []state.AccumulationOperand) (state.AccumulationState, []service.DeferredTransfer, *crypto.Hash, uint64) {
func (a *Accumulator) InvokePVM(accState state.AccumulationState, newTime jamtime.Timeslot, serviceIndex block.ServiceId, gas uint64, accOperand []state.AccumulationOperand) (state.AccumulationState, []service.DeferredTransfer, *crypto.Hash, uint64) {
// if ud[s]c = ∅
if accState.ServiceState[serviceIndex].Code() == nil {
ctx, err := a.newCtx(accState, serviceIndex)
Expand All @@ -55,8 +58,16 @@ func (a *Accumulator) InvokePVM(accState state.AccumulationState, serviceIndex b
ExceptionalCtx: ctx,
}

// E(↕o)
args, err := jam.Marshal(accOperand)
// E(t, s, ↕o)
args, err := jam.Marshal(struct {
Timeslot jamtime.Timeslot
ServiceID block.ServiceId
AccumulationOperands []state.AccumulationOperand
}{
Timeslot: newTime,
ServiceID: serviceIndex,
AccumulationOperands: accOperand,
})
if err != nil {
log.Println("error encoding arguments", "err", err)
return ctx.AccumulationState, []service.DeferredTransfer{}, nil, 0
Expand Down Expand Up @@ -137,9 +148,9 @@ func (a *Accumulator) newCtx(u state.AccumulationState, serviceIndex block.Servi
ServiceState: map[block.ServiceId]service.ServiceAccount{
serviceIndex: u.ServiceState[serviceIndex],
},
ValidatorKeys: u.ValidatorKeys,
WorkReportsQueue: u.WorkReportsQueue,
PrivilegedServices: u.PrivilegedServices,
ValidatorKeys: u.ValidatorKeys,
PendingAuthorizersQueues: u.PendingAuthorizersQueues,
PrivilegedServices: u.PrivilegedServices,
},
DeferredTransfers: []service.DeferredTransfer{},
}
Expand All @@ -153,31 +164,23 @@ func (a *Accumulator) newCtx(u state.AccumulationState, serviceIndex block.Servi
}

func (a *Accumulator) newServiceID(serviceIndex block.ServiceId) (block.ServiceId, error) {
var hashBytes []byte
bb, err := jam.Marshal(serviceIndex)
hashBytes, err := jam.Marshal(struct {
ServiceID block.ServiceId
Entropy crypto.Hash
Timeslot jamtime.Timeslot
}{
ServiceID: serviceIndex,
Entropy: a.state.EntropyPool[0],
Timeslot: a.header.TimeSlotIndex,
})
if err != nil {
return 0, err
}
hashBytes = append(hashBytes, bb...)

bb, err = jam.Marshal(a.state.EntropyPool[0])
if err != nil {
return 0, err
}
hashBytes = append(hashBytes, bb...)

bb, err = jam.Marshal(a.header.TimeSlotIndex)
if err != nil {
return 0, err
}
hashBytes = append(hashBytes, bb...)

hashData := crypto.HashData(hashBytes)
newId := block.ServiceId(0)
err = jam.Unmarshal(hashData[:], &newId)
if err != nil {
return 0, err
}

return newId, nil
}
Loading

0 comments on commit 6887bcd

Please sign in to comment.