Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Commit

Permalink
feat: Add new chain RPC methods get_header, get_header_by_number
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Jul 29, 2019
1 parent 1dd900d commit 0a9f7af
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 28 deletions.
8 changes: 4 additions & 4 deletions CKB.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
1A2D6B2922A35C5A00724E66 /* genesisBlock.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F43A227694D400C6546F /* genesisBlock.json */; };
1A2D6B2A22A35C5A00724E66 /* epoch.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A2CDAE22281796D0024CA71 /* epoch.json */; };
1A2D6B2B22A35C5A00724E66 /* transaction.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F43C2276981700C6546F /* transaction.json */; };
1A2D6B2C22A35C5A00724E66 /* tipHeader.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F43E2276994000C6546F /* tipHeader.json */; };
1A2D6B2C22A35C5A00724E66 /* header.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F43E2276994000C6546F /* header.json */; };
1A2D6B2D22A35C5A00724E66 /* tipBlockNumber.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F4402276998900C6546F /* tipBlockNumber.json */; };
1A2D6B2E22A35C5A00724E66 /* sendTransactionEmpty.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F442227699D400C6546F /* sendTransactionEmpty.json */; };
1A2D6B2F22A35C5A00724E66 /* localNodeInfo.json in Resources */ = {isa = PBXBuildFile; fileRef = 1A58F44422769A0600C6546F /* localNodeInfo.json */; };
Expand Down Expand Up @@ -156,7 +156,7 @@
1A58F4382276939D00C6546F /* genesisBlockHash.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = genesisBlockHash.json; sourceTree = "<group>"; };
1A58F43A227694D400C6546F /* genesisBlock.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = genesisBlock.json; sourceTree = "<group>"; };
1A58F43C2276981700C6546F /* transaction.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = transaction.json; sourceTree = "<group>"; };
1A58F43E2276994000C6546F /* tipHeader.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = tipHeader.json; sourceTree = "<group>"; };
1A58F43E2276994000C6546F /* header.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = header.json; sourceTree = "<group>"; };
1A58F4402276998900C6546F /* tipBlockNumber.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = tipBlockNumber.json; sourceTree = "<group>"; };
1A58F442227699D400C6546F /* sendTransactionEmpty.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = sendTransactionEmpty.json; sourceTree = "<group>"; };
1A58F44422769A0600C6546F /* localNodeInfo.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = localNodeInfo.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -252,7 +252,7 @@
1A58F43A227694D400C6546F /* genesisBlock.json */,
1A2CDAE22281796D0024CA71 /* epoch.json */,
1A58F43C2276981700C6546F /* transaction.json */,
1A58F43E2276994000C6546F /* tipHeader.json */,
1A58F43E2276994000C6546F /* header.json */,
1A58F4402276998900C6546F /* tipBlockNumber.json */,
1A58F442227699D400C6546F /* sendTransactionEmpty.json */,
1A58F44422769A0600C6546F /* localNodeInfo.json */,
Expand Down Expand Up @@ -554,7 +554,7 @@
1A2D6B2E22A35C5A00724E66 /* sendTransactionEmpty.json in Resources */,
1A2D6B3222A35C5A00724E66 /* txPoolInfo.json in Resources */,
1A199DEF22BA352D000C0C34 /* deindexLockHash.json in Resources */,
1A2D6B2C22A35C5A00724E66 /* tipHeader.json in Resources */,
1A2D6B2C22A35C5A00724E66 /* header.json in Resources */,
1A199DF022BA352D000C0C34 /* getLockHashIndexStates.json in Resources */,
1A2D6B2922A35C5A00724E66 /* genesisBlock.json in Resources */,
1A2D6B2D22A35C5A00724E66 /* tipBlockNumber.json in Resources */,
Expand Down
8 changes: 8 additions & 0 deletions Source/API/APIClient+Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public extension APIClient {
return try load(APIRequest<Header>(method: "get_tip_header"))
}

func getHeader(blockHash: H256) throws -> Header {
return try load(APIRequest<Header>(method: "get_header", params: [blockHash]))
}

func getHeaderByNumber(number: BlockNumber) throws -> Header {
return try load(APIRequest<Header>(method: "get_header_by_number", params: [number]))
}

func getCellsByLockHash(lockHash: H256, from: BlockNumber, to: BlockNumber) throws -> [CellOutputWithOutPoint] {
return try load(APIRequest<[CellOutputWithOutPoint]>(method: "get_cells_by_lock_hash", params: [lockHash, from, to]))
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/API/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ class APIClientTests: RPCTestSkippable {
XCTAssertTrue(Int64(result.number)! >= 0)
}

func testGetHeader() throws {
let tipHeader = try client.getTipHeader()
let hash = tipHeader.hash
let result = try client.getHeader(blockHash: hash)
XCTAssertNotNil(result)
XCTAssertEqual(result.hash, hash)
}

func testGetHeaderByNumber() throws {
let tipHeader = try client.getTipHeader()
let number = tipHeader.number
let result = try client.getHeaderByNumber(number: number)
XCTAssertNotNil(result)
XCTAssertEqual(result.number, number)
}

func testGetCellsByLockHash() throws {
let result = try client.getCellsByLockHash(lockHash: "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff", from: "1", to: "100")
XCTAssertNotNil(result)
Expand Down
16 changes: 15 additions & 1 deletion Tests/API/APIMockingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,25 @@ class APIMockingTests: XCTestCase {
}

func testGetTipHeader() throws {
let result = try getClient(json: "tipHeader").getTipHeader()
let result = try getClient(json: "header").getTipHeader()
XCTAssertNotNil(result)
XCTAssertTrue(Int64(result.number)! >= 0)
}

func testGetHeader() throws {
let hash = "0xba0d878d2c3711d38b5ddc2bc917312ca3898cad98457cc7960e28ec31f26e7f"
let result = try getClient(json: "header").getHeader(blockHash: hash)
XCTAssertNotNil(result)
XCTAssertEqual(result.hash, hash)
}

func testGetHeaderByNumber() throws {
let number = "1024"
let result = try getClient(json: "header").getHeaderByNumber(number: number)
XCTAssertNotNil(result)
XCTAssertEqual(result.number, number)
}

func testGetLiveCell() throws {
let outPoint = OutPoint(
blockHash: "0x4c2f8ba5f5a0104eaf84fcbb16af4b0e7ca2f2fdb076e748d54ef876d085d49e",
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions Tests/API/RPC Fixtures/header.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"dao": "0x01000000000000000000c16ff286230000203d88792d0000000961f400000000",
"difficulty": "0x3e8",
"epoch": "0",
"hash": "0xba0d878d2c3711d38b5ddc2bc917312ca3898cad98457cc7960e28ec31f26e7f",
"number": "1024",
"parent_hash": "0x3533d5f0882a60d3b25b7dd57002a5d9eb591e89a98231e8abc5bf48f7ee0592",
"proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"seal": {
"nonce": "0",
"proof": "0x"
},
"timestamp": "1557311767",
"transactions_root": "0x81389ffabda2d1658c75a81e390a82b335d4cb849b2269d134b042fea9cb9513",
"uncles_count": "0",
"uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": "0",
"witnesses_root": "0xb36423f72fd5cdbf6297c189b9d74e970bf0b971a537f41b8334088afe27900a"
}
}
23 changes: 0 additions & 23 deletions Tests/API/RPC Fixtures/tipHeader.json

This file was deleted.

0 comments on commit 0a9f7af

Please sign in to comment.