// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8; import "rainbow-bridge-sol/nearbridge/contracts/Borsh.sol"; import "rainbow-bridge-sol/nearbridge/contracts/NearDecoder.sol"; library ProofDecoder { using Borsh for Borsh.Data; using ProofDecoder for Borsh.Data; using NearDecoder for Borsh.Data; struct FullOutcomeProof { ExecutionOutcomeWithIdAndProof outcome_proof; MerklePath outcome_root_proof; // TODO: now empty array BlockHeaderLight block_header_lite; MerklePath block_proof; } function decodeFullOutcomeProof(Borsh.Data memory data) internal view returns (FullOutcomeProof memory proof) { proof.outcome_proof = data.decodeExecutionOutcomeWithIdAndProof(); proof.outcome_root_proof = data.decodeMerklePath(); proof.block_header_lite = data.decodeBlockHeaderLight(); proof.block_proof = data.decodeMerklePath(); } struct BlockHeaderLight { bytes32 prev_block_hash; bytes32 inner_rest_hash; NearDecoder.BlockHeaderInnerLite inner_lite; bytes32 hash; // Computable } function decodeBlockHeaderLight(Borsh.Data memory data) internal view returns (BlockHeaderLight memory header) { header.prev_block_hash = data.decodeBytes32(); header.inner_rest_hash = data.decodeBytes32(); header.inner_lite = data.decodeBlockHeaderInnerLite(); header.hash = sha256( abi.encodePacked( sha256(abi.encodePacked(header.inner_lite.hash, header.inner_rest_hash)), header.prev_block_hash ) ); } struct ExecutionStatus { uint8 enumIndex; bool unknown; bool failed; bytes successValue; // The final action succeeded and returned some value or an empty vec. bytes32 successReceiptId; // The final action of the receipt returned a promise or the signed transaction was converted to a receipt. Contains the receipt_id of the generated receipt. } function decodeExecutionStatus(Borsh.Data memory data) internal pure returns (ExecutionStatus memory executionStatus) { executionStatus.enumIndex = data.decodeU8(); if (executionStatus.enumIndex == 0) { executionStatus.unknown = true; } else if (executionStatus.enumIndex == 1) { // Can avoid revert since ExecutionStatus is latest field in all parent structures executionStatus.failed = true; } else if (executionStatus.enumIndex == 2) { executionStatus.successValue = data.decodeBytes(); } else if (executionStatus.enumIndex == 3) { executionStatus.successReceiptId = data.decodeBytes32(); } else { revert("NearDecoder: decodeExecutionStatus index out of range"); } } struct ExecutionOutcome { bytes[] logs; // Logs from this transaction or receipt. bytes32[] receipt_ids; // Receipt IDs generated by this transaction or receipt. uint64 gas_burnt; // The amount of the gas burnt by the given transaction or receipt. uint128 tokens_burnt; // The total number of the tokens burnt by the given transaction or receipt. bytes executor_id; // Hash of the transaction or receipt id that produced this outcome. ExecutionStatus status; // Execution status. Contains the result in case of successful execution. bytes32[] merkelization_hashes; } function decodeExecutionOutcome(Borsh.Data memory data) internal view returns (ExecutionOutcome memory outcome) { outcome.logs = new bytes[](data.decodeU32()); for (uint i = 0; i < outcome.logs.length; i++) { outcome.logs[i] = data.decodeBytes(); } uint256 start = data.ptr; outcome.receipt_ids = new bytes32[](data.decodeU32()); for (uint i = 0; i < outcome.receipt_ids.length; i++) { outcome.receipt_ids[i] = data.decodeBytes32(); } outcome.gas_burnt = data.decodeU64(); outcome.tokens_burnt = data.decodeU128(); outcome.executor_id = data.decodeBytes(); outcome.status = data.decodeExecutionStatus(); outcome.merkelization_hashes = new bytes32[](1 + outcome.logs.length); outcome.merkelization_hashes[0] = Utils.sha256Raw(start, data.ptr - start); for (uint i = 0; i < outcome.logs.length; i++) { outcome.merkelization_hashes[i + 1] = sha256(outcome.logs[i]); } } struct ExecutionOutcomeWithId { bytes32 id; // The transaction hash or the receipt ID. ExecutionOutcome outcome; bytes32 hash; } function decodeExecutionOutcomeWithId(Borsh.Data memory data) internal view returns (ExecutionOutcomeWithId memory outcome) { outcome.id = data.decodeBytes32(); outcome.outcome = data.decodeExecutionOutcome(); uint256 len = 1 + outcome.outcome.merkelization_hashes.length; outcome.hash = sha256( abi.encodePacked(Utils.swapBytes4(uint32(len)), outcome.id, outcome.outcome.merkelization_hashes) ); } struct MerklePathItem { bytes32 hash; uint8 direction; // 0 = left, 1 = right } function decodeMerklePathItem(Borsh.Data memory data) internal pure returns (MerklePathItem memory item) { item.hash = data.decodeBytes32(); item.direction = data.decodeU8(); require(item.direction < 2, "ProofDecoder: MerklePathItem direction should be 0 or 1"); } struct MerklePath { MerklePathItem[] items; } function decodeMerklePath(Borsh.Data memory data) internal pure returns (MerklePath memory path) { path.items = new MerklePathItem[](data.decodeU32()); for (uint i = 0; i < path.items.length; i++) { path.items[i] = data.decodeMerklePathItem(); } } struct ExecutionOutcomeWithIdAndProof { MerklePath proof; bytes32 block_hash; ExecutionOutcomeWithId outcome_with_id; } function decodeExecutionOutcomeWithIdAndProof(Borsh.Data memory data) internal view returns (ExecutionOutcomeWithIdAndProof memory outcome) { outcome.proof = data.decodeMerklePath(); outcome.block_hash = data.decodeBytes32(); outcome.outcome_with_id = data.decodeExecutionOutcomeWithId(); } }