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

Commit

Permalink
feat: Encode/Parse numbers with hex format for RPC calling
Browse files Browse the repository at this point in the history
while using integer from API client
  • Loading branch information
ashchan committed Sep 13, 2019
1 parent fdabbec commit 2ea114c
Show file tree
Hide file tree
Showing 31 changed files with 265 additions and 144 deletions.
15 changes: 9 additions & 6 deletions Source/API/APIClient+Chain.swift
Expand Up @@ -12,15 +12,15 @@ public extension APIClient {
}

func getBlockByNumber(number: BlockNumber) throws -> Block {
return try load(APIRequest<Block>(method: "get_block_by_number", params: [number]))
return try load(APIRequest<Block>(method: "get_block_by_number", params: [number.hexString]))
}

func getTransaction(hash: H256) throws -> TransactionWithStatus {
return try load(APIRequest<TransactionWithStatus>(method: "get_transaction", params: [hash]))
}

func getBlockHash(number: BlockNumber) throws -> H256 {
return try load(APIRequest<String>(method: "get_block_hash", params: [number]))
return try load(APIRequest<String>(method: "get_block_hash", params: [number.hexString]))
}

func getTipHeader() throws -> Header {
Expand All @@ -32,27 +32,30 @@ public extension APIClient {
}

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

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]))
return try load(APIRequest<[CellOutputWithOutPoint]>(
method: "get_cells_by_lock_hash",
params: [lockHash, from.hexString, to.hexString]
))
}

func getLiveCell(outPoint: OutPoint, withData: Bool = true) throws -> CellWithStatus {
return try load(APIRequest<CellWithStatus>(method: "get_live_cell", params: [outPoint.param, withData]))
}

func getTipBlockNumber() throws -> BlockNumber {
return try load(APIRequest<BlockNumber>(method: "get_tip_block_number"))
return BlockNumber(hexValue: try load(APIRequest<HexString>(method: "get_tip_block_number")))!
}

func getCurrentEpoch() throws -> Epoch {
return try load(APIRequest<Epoch>(method: "get_current_epoch"))
}

func getEpochByNumber(number: EpochNumber) throws -> Epoch {
return try load(APIRequest<Epoch>(method: "get_epoch_by_number", params: [number]))
return try load(APIRequest<Epoch>(method: "get_epoch_by_number", params: [number.hexString]))
}

func getCellbaseOutputCapacityDetails(blockHash: H256) throws -> BlockReward? {
Expand Down
10 changes: 5 additions & 5 deletions Source/API/APIClient+Indexer.swift
Expand Up @@ -10,7 +10,7 @@ public extension APIClient {
func indexLockHash(lockHash: H256, indexFrom: BlockNumber? = nil) throws -> LockHashIndexState {
return try load(APIRequest<LockHashIndexState>(
method: "index_lock_hash",
params: [lockHash, indexFrom].compactMap { $0 }
params: [lockHash, indexFrom?.hexString].compactMap { $0 }
))
}

Expand All @@ -22,17 +22,17 @@ public extension APIClient {
return try load(APIRequest<[LockHashIndexState]>(method: "get_lock_hash_index_states", params: []))
}

func getLiveCellsByLockHash(lockHash: H256, page: Number, pageSize: Number, reverseOrder: Bool = false) throws -> [LiveCell] {
func getLiveCellsByLockHash(lockHash: H256, page: UInt32, pageSize: UInt32, reverseOrder: Bool = false) throws -> [LiveCell] {
return try load(APIRequest<[LiveCell]>(
method: "get_live_cells_by_lock_hash",
params: [lockHash, page, pageSize, reverseOrder]
params: [lockHash, page.hexString, pageSize.hexString, reverseOrder]
))
}

func getTransactionsByLockHash(lockHash: H256, page: Number, pageSize: Number, reverseOrder: Bool = false) throws -> [CellTransaction] {
func getTransactionsByLockHash(lockHash: H256, page: UInt32, pageSize: UInt32, reverseOrder: Bool = false) throws -> [CellTransaction] {
return try load(APIRequest<[CellTransaction]>(
method: "get_transactions_by_lock_hash",
params: [lockHash, page, pageSize, reverseOrder]
params: [lockHash, page.hexString, pageSize.hexString, reverseOrder]
))
}
}
4 changes: 2 additions & 2 deletions Source/API/APIClient.swift
Expand Up @@ -69,10 +69,10 @@ public class APIClient {

extension APIClient {
public func genesisBlockHash() throws -> H256 {
return try getBlockHash(number: "0x0")
return try getBlockHash(number: 0)
}

public func genesisBlock() throws -> Block {
return try getBlockByNumber(number: "0x0")
return try getBlockByNumber(number: 0)
}
}
2 changes: 1 addition & 1 deletion Source/Serialization/Serializer.swift
Expand Up @@ -57,7 +57,7 @@ struct UnsignedIntSerializer<T>: ObjectSerializer where T: UnsignedInteger & Fix
self.value = value
}

init?(value: Number) {
init?(value: String) {
guard let uint = value.starts(with: "0x") ? T(value.dropFirst(2), radix: 16) : T(value) else {
return nil
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ final class CellInputSerializer: StructSerializer<CellInput> {
super.init(
value: value,
fieldSerializers: [
UInt64Serializer(value: value.since)!,
UInt64Serializer(value: value.since),
OutPointSerializer(value: value.previousOutput)
]
)
Expand Down
Expand Up @@ -11,7 +11,7 @@ final class CellOutputSerializer: TableSerializer<CellOutput> {
super.init(
value: value,
fieldSerializers: [
UInt64Serializer(value: value.capacity)!,
UInt64Serializer(value: value.capacity),
value.lock.serializer,
OptionSerializer(value: value.type, serializer: value.type?.serializer as? ScriptSerializer)
]
Expand Down
Expand Up @@ -12,7 +12,7 @@ final class OutPointSerializer: StructSerializer<OutPoint> {
value: value,
fieldSerializers: [
Byte32Serializer(value: value.txHash)!,
UInt32Serializer(value: value.index)!
UInt32Serializer(value: value.index)
]
)
}
Expand Down
Expand Up @@ -15,7 +15,7 @@ public final class TransactionSerializer: TableSerializer<Transaction> {
super.init(
value: value,
fieldSerializers: [
UInt32Serializer(value: value.version)!,
UInt32Serializer(value: value.version),
FixVecSerializer<CellDep, CellDepSerializer>(value: value.cellDeps),
FixVecSerializer<[Byte], Byte32Serializer>(value: hexStringsToArrayOfBytes(value.headerDeps)),
FixVecSerializer<CellInput, CellInputSerializer>(value: value.inputs),
Expand Down
9 changes: 9 additions & 0 deletions Source/Types/BlockReward.swift
Expand Up @@ -20,4 +20,13 @@ public struct BlockReward: Codable {
case total
case txFee = "tx_fee"
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
proposalReward = Capacity(hexValue: try container.decode(String.self, forKey: .proposalReward))!
primary = Capacity(hexValue: try container.decode(String.self, forKey: .primary))!
secondary = Capacity(hexValue: try container.decode(String.self, forKey: .secondary))!
total = Capacity(hexValue: try container.decode(String.self, forKey: .total))!
txFee = Capacity(hexValue: try container.decode(String.self, forKey: .txFee))!
}
}
12 changes: 9 additions & 3 deletions Source/Types/CellInput.swift
Expand Up @@ -8,9 +8,9 @@ import Foundation

public struct CellInput: Codable, Param {
public let previousOutput: OutPoint
public let since: Number
public let since: UInt64

public init(previousOutput: OutPoint, since: Number) {
public init(previousOutput: OutPoint, since: UInt64) {
self.previousOutput = previousOutput
self.since = since
}
Expand All @@ -20,10 +20,16 @@ public struct CellInput: Codable, Param {
case since
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
previousOutput = try container.decode(OutPoint.self, forKey: .previousOutput)
since = UInt64(hexValue: try container.decode(String.self, forKey: .since))!
}

public var param: [String: Any] {
return [
CodingKeys.previousOutput.rawValue: previousOutput.param,
CodingKeys.since.rawValue: since
CodingKeys.since.rawValue: since.hexString
]
}
}
11 changes: 10 additions & 1 deletion Source/Types/CellOutput.swift
Expand Up @@ -17,9 +17,16 @@ public struct CellOutput: Codable, Param {
self.type = type
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
capacity = Capacity(hexValue: try container.decode(String.self, forKey: .capacity))!
lock = try container.decode(Script.self, forKey: .lock)
type = try container.decodeIfPresent(Script.self, forKey: .type)
}

public var param: [String: Any] {
var result: [String: Any] = [
"capacity": capacity,
"capacity": capacity.hexString,
"lock": lock.param
]
if let type = type {
Expand All @@ -31,11 +38,13 @@ public struct CellOutput: Codable, Param {

public struct CellOutputWithOutPoint: Codable {
public let outPoint: OutPoint
public let blockHash: H256
public let capacity: Capacity
public let lock: Script

enum CodingKeys: String, CodingKey {
case outPoint = "out_point"
case blockHash = "block_hash"
case capacity, lock
}
}
10 changes: 10 additions & 0 deletions Source/Types/ChainInfo.swift
Expand Up @@ -22,4 +22,14 @@ public struct ChainInfo: Codable {
case isInitialBlockDownload = "is_initial_block_download"
case alerts
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
chain = try container.decode(String.self, forKey: .chain)
medianTime = try container.decode(Timestamp.self, forKey: .medianTime)
epoch = EpochNumber(hexValue: try container.decode(String.self, forKey: .epoch))!
difficulty = try container.decode(HexNumber.self, forKey: .difficulty)
isInitialBlockDownload = try container.decode(Bool.self, forKey: .isInitialBlockDownload)
alerts = try container.decode([AlertMessage].self, forKey: .alerts)
}
}
5 changes: 5 additions & 0 deletions Source/Types/DryRunResult.swift
Expand Up @@ -8,4 +8,9 @@ import Foundation

public struct DryRunResult: Codable {
public let cycles: Cycle

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
cycles = Cycle(hexValue: try container.decode(String.self, forKey: .cycles))!
}
}
8 changes: 8 additions & 0 deletions Source/Types/Epoch.swift
Expand Up @@ -18,4 +18,12 @@ public struct Epoch: Codable {
case length
case difficulty
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
number = EpochNumber(hexValue: try container.decode(String.self, forKey: .number))!
startNumber = BlockNumber(hexValue: try container.decode(String.self, forKey: .startNumber))!
length = BlockNumber(hexValue: try container.decode(String.self, forKey: .length))!
difficulty = try container.decode(HexNumber.self, forKey: .difficulty)
}
}
27 changes: 24 additions & 3 deletions Source/Types/Header.swift
Expand Up @@ -17,10 +17,11 @@ public struct Header: Codable {
public let witnessesRoot: H256
public let difficulty: HexNumber
public let unclesHash: H256
public let unclesCount: Number
public let unclesCount: UInt32
public let dao: String
public let chainRoot: String
public let nonce: UInt64
public let hash: H256
public let nonce: Number

enum CodingKeys: String, CodingKey {
case version
Expand All @@ -35,7 +36,27 @@ public struct Header: Codable {
case unclesHash = "uncles_hash"
case unclesCount = "uncles_count"
case dao
case hash
case chainRoot = "chain_root"
case nonce
case hash
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
version = Version(hexValue: try container.decode(String.self, forKey: .version))!
parentHash = try container.decode(H256.self, forKey: .parentHash)
timestamp = try container.decode(Timestamp.self, forKey: .timestamp)
number = BlockNumber(hexValue: try container.decode(String.self, forKey: .number))!
epoch = EpochNumber(hexValue: try container.decode(String.self, forKey: .epoch))!
transactionsRoot = try container.decode(H256.self, forKey: .transactionsRoot)
proposalsHash = try container.decode(H256.self, forKey: .proposalsHash)
witnessesRoot = try container.decode(H256.self, forKey: .witnessesRoot)
difficulty = try container.decode(HexString.self, forKey: .difficulty)
unclesHash = try container.decode(H256.self, forKey: .unclesHash)
unclesCount = UInt32(hexValue: try container.decode(String.self, forKey: .unclesCount))!
dao = try container.decode(String.self, forKey: .dao)
chainRoot = try container.decode(String.self, forKey: .chainRoot)
nonce = UInt64(hexValue: try container.decode(String.self, forKey: .nonce))!
hash = try container.decode(H256.self, forKey: .hash)
}
}
7 changes: 7 additions & 0 deletions Source/Types/LockHashIndexState.swift
Expand Up @@ -16,4 +16,11 @@ public struct LockHashIndexState: Codable {
case blockNumber = "block_number"
case blockHash = "block_hash"
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
lockHash = try container.decode(H256.self, forKey: .lockHash)
blockNumber = BlockNumber(hexValue: try container.decode(String.self, forKey: .blockNumber))!
blockHash = try container.decode(H256.self, forKey: .blockHash)
}
}
14 changes: 12 additions & 2 deletions Source/Types/Node.swift
Expand Up @@ -8,11 +8,21 @@ import Foundation

public struct NodeAddress: Codable {
public let address: String
public let score: Number
public let score: UInt64

enum CodingKeys: String, CodingKey {
case address, score
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
address = try container.decode(String.self, forKey: .address)
score = UInt64(hexValue: try container.decode(String.self, forKey: .score))!
}
}

public struct Node: Codable {
public let version: Version
public let version: String
public let nodeId: String
public let addresses: [NodeAddress]
public let isOutbound: Bool?
Expand Down
32 changes: 8 additions & 24 deletions Source/Types/OutPoint.swift
Expand Up @@ -8,44 +8,28 @@ import Foundation

public struct OutPoint: Codable, Param {
public let txHash: H256
public let index: Number
public let index: UInt32

enum CodingKeys: String, CodingKey {
case txHash = "tx_hash"
case index
}

public init(txHash: H256, index: Number) {
public init(txHash: H256, index: UInt32) {
self.txHash = txHash
self.index = index
}

public var param: [String: Any] {
return [
CodingKeys.txHash.rawValue: txHash,
CodingKeys.index.rawValue: index
]
}
}

public struct CellOutPoint: Codable, Param {
public let txHash: H256
public let index: Number

enum CodingKeys: String, CodingKey {
case txHash = "tx_hash"
case index
}

public init(txHash: H256, index: Number) {
self.txHash = txHash
self.index = index
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
txHash = try container.decode(H256.self, forKey: .txHash)
index = UInt32(hexValue: try container.decode(String.self, forKey: .index))!
}

public var param: [String: Any] {
return [
"tx_hash": txHash,
"index": index
CodingKeys.txHash.rawValue: txHash,
CodingKeys.index.rawValue: index.hexString
]
}
}

0 comments on commit 2ea114c

Please sign in to comment.