Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RPC 0.8.0 #220

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft

Support RPC 0.8.0 #220

wants to merge 24 commits into from

Conversation

franciszekjob
Copy link
Collaborator

@franciszekjob franciszekjob commented Oct 24, 2024

Describe your changes

Add support for RPC 0.8.0

  • starknet_getStorageProof
    • Add StarknetGetStorageProofResponse, GlobalRoots, ContractsProof, ContractLeafData, NodeHashToNodeMappingItem, BinaryNode, EdgeNode, ContractStorageKey structs
    • Add getStorageProof method in StarknetRequest
  • starknet_getMessagesStatus
    • Add MessageStatus struct
    • Add getMessagesStatus method in StarknetRequest
  • Adapt execution resources to new receipt
    • StarknetAccountProtocol:
      • signDeployAccountV3 has now resourceBounds param instead of l1ResourceBounds
    • StarknetInvokeParamsV3, StarknetDeployAccountParamsV3, StarknetOptionalInvokeParamsV3:
      • Change l1ResourceBounds param to resourceBounds in constructors
    • StarknetFeeEstimate :
      • rename gasConsumed to l1GasConsumed
      • rename gasPrice to l1GasPrice
      • rename dataGasPrice to l1DataGasPrice
      • rename dataGasConsumed to l1DataGasConsumed
      • add l2GasConsumed, l2GasPrice fields
    • Update StarknetResources protocol to have only l1Gas and l2Gas fields
    • Update StarknetExecutionResources fields
    • Remove StarknetDataAvailability and StarknetComputationResources
    • Add StarknetInnerCallExecutionResources
    • Remove constructor accepting only l1Gas in StarknetResourceBoundsMapping
    • Rename computationResources to executionResources in StarknetFunctionInvocation and change its type to StarknetInnerCallExecutionResources instead of StarknetComputationResources
  • Add failureReason to StarknetGetTransactionStatusResponse

Linked issues

Closes #219

Breaking changes

  • This issue contains breaking changes
  • Not compatible with JSON-RPC 0.7.0 spec
  • Use resourceBounds instead of l1ResourceBounds param
  • StarknetComputationResources has been removed; StarknetExecutionResources and StarknetFeeEstimate have been updated

@franciszekjob franciszekjob marked this pull request as draft October 24, 2024 22:45
@franciszekjob franciszekjob changed the title Support JSON RPC 0.8.0 Support RPC 0.8.0 Oct 25, 2024
@franciszekjob franciszekjob removed the request for review from DelevoXDG October 28, 2024 08:33
Comment on lines +8 to +28
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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me this is slightly overcomplicated:

Suggested change
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)
}
if let binaryNode = try? BinaryNode(from: decoder) {
self = .binaryNode(binaryNode)
} else if let edgeNode = try? EdgeNode(from: decoder) {
self = .edgeNode(edgeNode)
} else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Failed to decode MerkleNode."))
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And while the spec is not merged yet, do you think maybe it's worth it to reach out to starknet-specs maintainers and ask "type" field to be added? "Type" fields are used heavily in the spec overall, so this might be a reasonable ask.

Comment on lines +42 to +51
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
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary? 🤔

The advantage of the enum approach is that Equatable conformance should be synthesized automatically.

Suggested change
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
}
}


let node = try decoder.decode(MerkleNode.self, from: json)
if case let .edgeNode(edgeNode) = node {
XCTAssertEqual(edgeNode.path, 123)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x123 != 123 😅

Comment on lines +10 to +15
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
nodes = try container.decode(NodeHashToNodeMapping.self, forKey: .nodes)
contractLeavesData = try container.decode([ContractLeafData].self, forKey: .contractLeavesData)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
nodes = try container.decode(NodeHashToNodeMapping.self, forKey: .nodes)
contractLeavesData = try container.decode([ContractLeafData].self, forKey: .contractLeavesData)
}

Comment on lines +16 to +19
public static func == (lhs: ContractsProof, rhs: ContractsProof) -> Bool {
lhs.nodes == rhs.nodes && lhs.contractLeavesData == rhs.contractLeavesData
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static func == (lhs: ContractsProof, rhs: ContractsProof) -> Bool {
lhs.nodes == rhs.nodes && lhs.contractLeavesData == rhs.contractLeavesData
}

Comment on lines +11 to +21

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

nodeHash = try container.decode(Felt.self, forKey: .nodeHash)
node = try container.decode(MerkleNode.self, forKey: .node)
}

public static func == (lhs: NodeHashToNodeMappingItem, rhs: NodeHashToNodeMappingItem) -> Bool {
lhs.nodeHash == rhs.nodeHash && lhs.node == rhs.node
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
nodeHash = try container.decode(Felt.self, forKey: .nodeHash)
node = try container.decode(MerkleNode.self, forKey: .node)
}
public static func == (lhs: NodeHashToNodeMappingItem, rhs: NodeHashToNodeMappingItem) -> Bool {
lhs.nodeHash == rhs.nodeHash && lhs.node == rhs.node
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support JSON-RPC v0.8.0
2 participants