Skip to content

Commit

Permalink
Merge branch 'main' into feat/get-work-reports-v0.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Nov 15, 2024
2 parents 8c0e043 + e172b6d commit 455819a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
11 changes: 5 additions & 6 deletions internal/state/block.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package state

import (
"github.com/eigerco/strawberry/internal/common"
"github.com/eigerco/strawberry/internal/crypto"
)

// BlockState represents the details of the most recent blocks.
// BlockState represents the details of the most recent blocks. (v0.4.5)
type BlockState struct {
HeaderHash crypto.Hash // Hash of the block header (h)
StateRoot crypto.Hash // State root (b)
AccumulationResultMMR crypto.Hash // Accumulation-result MMR (s)
WorkReportHashes [common.TotalNumberOfCores]crypto.Hash // Hashes of work-reports (p)
HeaderHash crypto.Hash // Hash of the block header (h)
StateRoot crypto.Hash // State root (b)
AccumulationResultMMR crypto.Hash // Accumulation-result MMR (s)
WorkReportHashes map[crypto.Hash]crypto.Hash // Hashes of work-reports (p)
}
6 changes: 4 additions & 2 deletions internal/state/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ func RandomBlockState(t *testing.T) BlockState {
state.HeaderHash = testutils.RandomHash(t)
state.StateRoot = testutils.RandomHash(t)
state.AccumulationResultMMR = testutils.RandomHash(t)
for i := range state.WorkReportHashes {
state.WorkReportHashes[i] = testutils.RandomHash(t)
workReportHashes := make(map[crypto.Hash]crypto.Hash)
for i := uint16(0); i < common.TotalNumberOfCores; i++ {
workReportHashes[testutils.RandomHash(t)] = testutils.RandomHash(t)
}
state.WorkReportHashes = workReportHashes
return state
}

Expand Down
50 changes: 23 additions & 27 deletions internal/statetransition/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,39 +278,38 @@ func calculateNewTimeState(header block.Header) jamtime.Timeslot {
return header.TimeSlotIndex
}

// calculateNewRecentBlocks Equation 18: β′ ≺ (H, EG, β†, C)
// calculateNewRecentBlocks Equation 18: β′ ≺ (H, EG, β†, C) v0.4.5
func calculateNewRecentBlocks(header block.Header, guarantees block.GuaranteesExtrinsic, intermediateRecentBlocks []state.BlockState, serviceHashPairs ServiceHashPairs) ([]state.BlockState, error) {
// Calculate accumulation-result Merkle tree root (r)
// Equation 83: let r = M_B([s ^^ E_4(s) ⌢ E(h) | (s, h) ∈ C], H_K)
accumulationRoot := calculateAccumulationRoot(serviceHashPairs)

// Append to the previous block's Merkle mountain range (b)
// Equation 83: let b = A(last([[]] ⌢ [x_b | x <− β]), r, H_K)
var lastBlockMMR crypto.Hash
if len(intermediateRecentBlocks) > 0 {
lastBlockMMR = intermediateRecentBlocks[len(intermediateRecentBlocks)-1].AccumulationResultMMR
}
newMMR := AppendToMMR(lastBlockMMR, accumulationRoot)
headerBytes, err := json.Marshal(header)
if err != nil {
return nil, err
}

// Create new block state (n)
reportHashes, err := calculateWorkReportHashes(guarantees)
// Equation 83: p = {((g_w)_s)_h ↦ ((g_w)_s)_e | g ∈ E_G}
workPackageMapping := buildWorkPackageMapping(guarantees.Guarantees)

// Equation 83: let n = {p, h ▸▸ H(H), b, s ▸▸ H_0}
headerBytes, err := json.Marshal(header)
if err != nil {
return nil, err
}

newBlockState := state.BlockState{
HeaderHash: crypto.HashData(headerBytes),
StateRoot: header.PriorStateRoot,
AccumulationResultMMR: newMMR,
WorkReportHashes: reportHashes,
HeaderHash: crypto.HashData(headerBytes), // h ▸▸ H(H)
StateRoot: crypto.Hash{}, // s ▸▸ H_0
AccumulationResultMMR: newMMR, // b
WorkReportHashes: workPackageMapping, // p
}

// Update β† with the new block state (Equation 83)
// Equation 84: β′ ≡ ←────── β† n_H
// First append new block state
newRecentBlocks := append(intermediateRecentBlocks, newBlockState)

// Ensure we only keep the most recent H blocks
// Then keep only last H blocks
if len(newRecentBlocks) > state.MaxRecentBlocks {
newRecentBlocks = newRecentBlocks[len(newRecentBlocks)-state.MaxRecentBlocks:]
}
Expand All @@ -329,18 +328,15 @@ func calculateAccumulationRoot(accumulations ServiceHashPairs) crypto.Hash {
return crypto.Hash{}
}

func calculateWorkReportHashes(guarantees block.GuaranteesExtrinsic) ([common.TotalNumberOfCores]crypto.Hash, error) {
var hashes [common.TotalNumberOfCores]crypto.Hash
for _, guarantee := range guarantees.Guarantees {
// Assuming CoreIndex is part of the WorkReport struct
coreIndex := guarantee.WorkReport.CoreIndex
reportBytes, err := json.Marshal(guarantee.WorkReport)
if err != nil {
return [common.TotalNumberOfCores]crypto.Hash{}, err
}
hashes[coreIndex] = crypto.HashData(reportBytes)
// buildWorkPackageMapping creates the work package mapping p from equation 83:
// p = {((gw)s)h ↦ ((gw)s)e | g ∈ EG}
func buildWorkPackageMapping(guarantees []block.Guarantee) map[crypto.Hash]crypto.Hash {
workPackages := make(map[crypto.Hash]crypto.Hash)
for _, g := range guarantees {
workPackages[g.WorkReport.WorkPackageSpecification.WorkPackageHash] =
g.WorkReport.WorkPackageSpecification.SegmentRoot
}
return hashes, nil
return workPackages
}

// calculateNewSafroleState Equation 19: γ′ ≺ (H, τ, ET , γ, ι, η′, κ′)
Expand Down

0 comments on commit 455819a

Please sign in to comment.