-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMerkleNode.swift
74 lines (63 loc) · 2.29 KB
/
MerkleNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Foundation
public enum MerkleNode: Codable, Equatable {
case binaryNode(BinaryNode)
case edgeNode(EdgeNode)
public init(from decoder: Decoder) throws {
let binaryNodeKeys = Set(BinaryNode.CodingKeys.allCases.map(\.stringValue))
let edgeNodeKeys = Set(EdgeNode.CodingKeys.allCases.map(\.stringValue))
let binaryNodeContainer = try decoder.container(keyedBy: BinaryNode.CodingKeys.self)
if Set(binaryNodeContainer.allKeys.map(\.stringValue)) == binaryNodeKeys {
let binaryNode = try BinaryNode(from: decoder)
self = .binaryNode(binaryNode)
} else if let edgeNodeContainer = try? decoder.container(keyedBy: EdgeNode.CodingKeys.self),
Set(edgeNodeContainer.allKeys.map(\.stringValue)) == edgeNodeKeys
{
let edgeNode = try EdgeNode(from: decoder)
self = .edgeNode(edgeNode)
} else {
let context = DecodingError.Context(
codingPath: decoder.codingPath,
// TODO: Improve error message.
debugDescription: "Failed to decode MerkleNode from the given data."
)
throw DecodingError.dataCorrupted(context)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .binaryNode(binaryNode):
try container.encode(binaryNode)
case let .edgeNode(edgeNode):
try container.encode(edgeNode)
}
}
public static func == (lhs: MerkleNode, rhs: MerkleNode) -> Bool {
switch (lhs, rhs) {
case let (.binaryNode(lhsBinaryNode), .binaryNode(rhsBinaryNode)):
lhsBinaryNode == rhsBinaryNode
case let (.edgeNode(lhsEdgeNode), .edgeNode(rhsEdgeNode)):
lhsEdgeNode == rhsEdgeNode
default:
false
}
}
}
public struct BinaryNode: Codable, Equatable {
let left: Felt
let right: Felt
enum CodingKeys: String, CodingKey, CaseIterable {
case left
case right
}
}
public struct EdgeNode: Codable, Equatable {
let path: NumAsHex
let length: Int
let child: Felt
enum CodingKeys: String, CodingKey, CaseIterable {
case path
case length
case child
}
}