|
| 1 | +import Blockchain |
| 2 | +import Codec |
| 3 | +import Foundation |
| 4 | +import Utils |
| 5 | + |
| 6 | +struct JamTestnetTestcase: Codable { |
| 7 | + var preState: TestState |
| 8 | + var block: Block |
| 9 | + var postState: TestState |
| 10 | +} |
| 11 | + |
| 12 | +extension ProtocolConfig { |
| 13 | + public enum Int4: ReadInt { |
| 14 | + public typealias TConfig = ProtocolConfigRef |
| 15 | + public static func read(config _: ProtocolConfigRef) -> Int { |
| 16 | + 4 |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +struct TestState: Codable { |
| 22 | + var root: Data32 |
| 23 | + var keyvals: [ConfigFixedSizeArray<Data, ProtocolConfig.Int4>] |
| 24 | + |
| 25 | + func toDict() -> [Data32: Data] { |
| 26 | + // keyvals are array of 4 item arrays, we only need first 2 item of each array |
| 27 | + let tuples = keyvals.map { (Data32($0[0])!, $0[1]) } |
| 28 | + return tuples.reduce(into: [:]) { $0[$1.0] = $1.1 } |
| 29 | + } |
| 30 | + |
| 31 | + // extract from string like "s=0|h=0x8c30f2c101674af1da31769e96ce72e81a4a44c89526d7d3ff0a1a511d5f3c9f l=25|t=[0] tlen=1" |
| 32 | + func extractDetails(from input: String) -> [String: String] { |
| 33 | + var result = [String: String]() |
| 34 | + let components = input.split(separator: "|") |
| 35 | + for component in components { |
| 36 | + let subComponents = component.split(separator: " ") |
| 37 | + for subComponent in subComponents { |
| 38 | + let pair = subComponent.split(separator: "=", maxSplits: 1).map(String.init) |
| 39 | + if pair.count == 2 { |
| 40 | + result[pair[0]] = pair[1] |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return result |
| 45 | + } |
| 46 | + |
| 47 | + func toState(config: ProtocolConfigRef = TestVariants.tiny.config) throws -> State { |
| 48 | + var changes: [(key: any StateKey, value: Codable & Sendable)] = [] |
| 49 | + |
| 50 | + for arr in keyvals { |
| 51 | + let key: any StateKey |
| 52 | + let value: Codable & Sendable |
| 53 | + |
| 54 | + guard let desc = String(data: arr[2], encoding: .utf8) else { |
| 55 | + fatalError("invalid description") |
| 56 | + } |
| 57 | + guard let detail = String(data: arr[3], encoding: .utf8) else { |
| 58 | + fatalError("invalid detail") |
| 59 | + } |
| 60 | + |
| 61 | + switch desc { |
| 62 | + case "c1": |
| 63 | + key = StateKeys.CoreAuthorizationPoolKey() |
| 64 | + value = try JamDecoder.decode(StateKeys.CoreAuthorizationPoolKey.Value.self, from: arr[1], withConfig: config) |
| 65 | + case "c2": |
| 66 | + key = StateKeys.AuthorizationQueueKey() |
| 67 | + value = try JamDecoder.decode(StateKeys.AuthorizationQueueKey.Value.self, from: arr[1], withConfig: config) |
| 68 | + case "c3": |
| 69 | + key = StateKeys.RecentHistoryKey() |
| 70 | + value = try JamDecoder.decode(StateKeys.RecentHistoryKey.Value.self, from: arr[1], withConfig: config) |
| 71 | + case "c4": |
| 72 | + key = StateKeys.SafroleStateKey() |
| 73 | + value = try JamDecoder.decode(StateKeys.SafroleStateKey.Value.self, from: arr[1], withConfig: config) |
| 74 | + case "c5": |
| 75 | + key = StateKeys.JudgementsKey() |
| 76 | + value = try JamDecoder.decode(StateKeys.JudgementsKey.Value.self, from: arr[1], withConfig: config) |
| 77 | + case "c6": |
| 78 | + key = StateKeys.EntropyPoolKey() |
| 79 | + value = try JamDecoder.decode(StateKeys.EntropyPoolKey.Value.self, from: arr[1], withConfig: config) |
| 80 | + case "c7": |
| 81 | + key = StateKeys.ValidatorQueueKey() |
| 82 | + value = try JamDecoder.decode(StateKeys.ValidatorQueueKey.Value.self, from: arr[1], withConfig: config) |
| 83 | + case "c8": |
| 84 | + key = StateKeys.CurrentValidatorsKey() |
| 85 | + value = try JamDecoder.decode(StateKeys.CurrentValidatorsKey.Value.self, from: arr[1], withConfig: config) |
| 86 | + case "c9": |
| 87 | + key = StateKeys.PreviousValidatorsKey() |
| 88 | + value = try JamDecoder.decode(StateKeys.PreviousValidatorsKey.Value.self, from: arr[1], withConfig: config) |
| 89 | + case "c10": |
| 90 | + key = StateKeys.ReportsKey() |
| 91 | + value = try JamDecoder.decode(StateKeys.ReportsKey.Value.self, from: arr[1], withConfig: config) |
| 92 | + case "c11": |
| 93 | + key = StateKeys.TimeslotKey() |
| 94 | + value = try JamDecoder.decode(StateKeys.TimeslotKey.Value.self, from: arr[1], withConfig: config) |
| 95 | + case "c12": |
| 96 | + key = StateKeys.PrivilegedServicesKey() |
| 97 | + value = try JamDecoder.decode(StateKeys.PrivilegedServicesKey.Value.self, from: arr[1], withConfig: config) |
| 98 | + case "c13": |
| 99 | + key = StateKeys.ActivityStatisticsKey() |
| 100 | + value = try JamDecoder.decode(StateKeys.ActivityStatisticsKey.Value.self, from: arr[1], withConfig: config) |
| 101 | + case "c14": |
| 102 | + key = StateKeys.AccumulationQueueKey() |
| 103 | + value = try JamDecoder.decode(StateKeys.AccumulationQueueKey.Value.self, from: arr[1], withConfig: config) |
| 104 | + case "c15": |
| 105 | + key = StateKeys.AccumulationHistoryKey() |
| 106 | + value = try JamDecoder.decode(StateKeys.AccumulationHistoryKey.Value.self, from: arr[1], withConfig: config) |
| 107 | + case "service_account": |
| 108 | + let details = extractDetails(from: detail) |
| 109 | + key = StateKeys.ServiceAccountKey(index: UInt32(details["s"]!)!) |
| 110 | + value = try JamDecoder.decode(StateKeys.ServiceAccountKey.Value.self, from: arr[1], withConfig: config) |
| 111 | + case "account_storage": |
| 112 | + let details = extractDetails(from: detail) |
| 113 | + key = StateKeys.ServiceAccountStorageKey( |
| 114 | + index: UInt32(details["s"]!)!, |
| 115 | + key: Data32(fromHexString: String(details["k"]!.suffix(64)))! |
| 116 | + ) |
| 117 | + value = try JamDecoder.decode(StateKeys.ServiceAccountStorageKey.Value.self, from: arr[1], withConfig: config) |
| 118 | + case "account_preimage": |
| 119 | + let details = extractDetails(from: detail) |
| 120 | + key = StateKeys.ServiceAccountPreimagesKey( |
| 121 | + index: UInt32(details["s"]!)!, |
| 122 | + hash: Data32(fromHexString: String(details["h"]!.suffix(64)))! |
| 123 | + ) |
| 124 | + value = arr[1] |
| 125 | + case "account_lookup": |
| 126 | + let details = extractDetails(from: detail) |
| 127 | + key = StateKeys.ServiceAccountPreimageInfoKey( |
| 128 | + index: UInt32(details["s"]!)!, |
| 129 | + hash: Data32(fromHexString: String(details["h"]!.suffix(64)))!, |
| 130 | + length: UInt32(details["l"]!)! |
| 131 | + ) |
| 132 | + value = try JamDecoder.decode(StateKeys.ServiceAccountPreimageInfoKey.Value.self, from: arr[1], withConfig: config) |
| 133 | + default: |
| 134 | + fatalError("invalid key") |
| 135 | + } |
| 136 | + changes.append((key, value)) |
| 137 | + } |
| 138 | + |
| 139 | + let backend = StateBackend(InMemoryBackend(), config: config, rootHash: root) |
| 140 | + |
| 141 | + let layer = StateLayer(changes: changes) |
| 142 | + |
| 143 | + return State(backend: backend, layer: layer) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +enum JamTestnet { |
| 148 | + static func loadTests(path: String, src: TestsSource) throws -> [Testcase] { |
| 149 | + try TestLoader.getTestcases(path: path, extension: "bin", src: src) |
| 150 | + } |
| 151 | + |
| 152 | + static func decodeTestcase(_ input: Testcase, config: ProtocolConfigRef = TestVariants.tiny.config) throws -> JamTestnetTestcase { |
| 153 | + // NOTE: some tests have trailing bytes |
| 154 | + try JamDecoder.decode(JamTestnetTestcase.self, from: input.data, withConfig: config, allowTrailingBytes: true) |
| 155 | + } |
| 156 | + |
| 157 | + static func runSTF( |
| 158 | + _ testcase: JamTestnetTestcase, |
| 159 | + config: ProtocolConfigRef = TestVariants.tiny.config |
| 160 | + ) async throws -> Result<StateRef, Error> { |
| 161 | + let runtime = Runtime(config: config) |
| 162 | + |
| 163 | + let result = await Result { |
| 164 | + // NOTE: skip block validate first, some tests does not ensure this |
| 165 | + let blockRef = try testcase.block.asRef().toValidated(config: config) |
| 166 | + let stateRef = try testcase.preState.toState(config: config).asRef() |
| 167 | + return try await runtime.apply(block: blockRef, state: stateRef) |
| 168 | + } |
| 169 | + |
| 170 | + return result |
| 171 | + } |
| 172 | +} |
0 commit comments