Skip to content

Commit

Permalink
no need special hanlding for MMR root (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored Aug 25, 2024
1 parent 594c989 commit 10a79a9
Showing 1 changed file with 1 addition and 27 deletions.
28 changes: 1 addition & 27 deletions Blockchain/Sources/Blockchain/Types/State+Merklization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension State {

res[Self.constructKey(1)] = try JamEncoder.encode(coreAuthorizationPool)
res[Self.constructKey(2)] = try JamEncoder.encode(authorizationQueue)
res[Self.constructKey(3)] = try encode(recentHistory)
res[Self.constructKey(3)] = try JamEncoder.encode(recentHistory)
res[Self.constructKey(4)] = try JamEncoder.encode(safroleState)
res[Self.constructKey(5)] = try JamEncoder.encode(judgements)
res[Self.constructKey(6)] = try JamEncoder.encode(entropyPool)
Expand Down Expand Up @@ -80,32 +80,6 @@ extension State {
return res
}

private func encode(_ recentHistory: RecentHistory) throws -> Data {
// can't just encode RecentHistory directly because the MMR root is hashed
// modify the default Encodable means it won't be able to decode the encoded data, which is not what we want

var data = Data()
let capacity = 2 + // length prefix
recentHistory.items.count * (32 * 3 + 2) + // headerHash, mmrRoots, stateRoot + workReportHashes length prefix
recentHistory.items.reduce(into: 0) { res, item in
res += item.workReportHashes.count * 32
}
data.reserveCapacity(capacity)

// length prefix
data.append(contentsOf: UInt(recentHistory.items.count).encode(method: .variableWidth))

let encoder = JamEncoder(data)
for item in recentHistory.items {
try encoder.encode(item.headerHash)
try encoder.encode(JamEncoder.encode(item.mmrRoots).blake2b256hash())
try encoder.encode(item.stateRoot)
try encoder.encode(item.workReportHashes)
}

return encoder.data.blake2b256hash().data
}

private func encode(_ account: ServiceAccount) throws -> Data {
var data = Data()
data.reserveCapacity(32 + 8 * 3) // codeHash, balance, accumlateGasLimit, onTransferGasLimit
Expand Down

0 comments on commit 10a79a9

Please sign in to comment.