Skip to content

Commit

Permalink
Merge pull request #17 from greymass/apporc_working
Browse files Browse the repository at this point in the history
Fix unit tests and Array<Never> related bugs
  • Loading branch information
dafuga authored Mar 26, 2024
2 parents fb5b20b + a9707b2 commit 4dd389c
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/Packages
/*.xcodeproj
xcuserdata/
/.swiftpm
12 changes: 0 additions & 12 deletions Sources/EOSIO/ABICodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,6 @@ public struct BinaryExtension<Value: ABICodable>: ABICodable {
extension BinaryExtension: Equatable where Value: Equatable {}
extension BinaryExtension: Hashable where Value: Hashable {}

extension Never: ABICodable {
public init(from decoder: Decoder) throws {
let ctx = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Attempted to decode Never")
throw DecodingError.dataCorrupted(ctx)
}

public func encode(to encoder: Encoder) throws {
let ctx = EncodingError.Context(codingPath: encoder.codingPath, debugDescription: "Attempted to encode Never")
throw EncodingError.invalidValue(self, ctx)
}
}

// MARK: Dynamic ABI Coding

public extension CodingUserInfoKey {
Expand Down
13 changes: 1 addition & 12 deletions Sources/EOSIO/ABIDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,8 @@ public extension ABIDecoder {
return Int(try self.readVarint()) as! T
case is UInt.Type:
return UInt(try self.readVaruint()) as! T
case is Array<Never>.Type:
return [] as! T
case let abiType as ABIDecodable.Type:
let decodedAbiType = try abiType.init(fromAbi: self)

// Instead of forcefully casting, verify the type at runtime.
if let result = decodedAbiType as? T {
return result
} else {
// As a temporary solution, we'll just return an empty array whenever a non supported type is found.
// This is not ideal, but it's better than crashing the app.
return [] as! T
}
return try abiType.init(fromAbi: self) as! T
default:
throw Error.typeNotConformingToABIDecodable(type)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/EOSIO/API/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ extension Client {
encoder.dataEncodingStrategy = self.dataEncoder
encoder.dateEncodingStrategy = self.dateEncoder
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.outputFormatting = .sortedKeys
return encoder
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/EOSIO/API/V1/Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public extension API.V1.Chain {
}

/// Create a new `get_table_rows` request with scope set from any type representable by a 64-bit unsigned integer.
public init<T: RawRepresentable>(code: Name, table: Name, scope: T) where T.RawValue == UInt64 {
public init<RT: RawRepresentable>(code: Name, table: Name, scope: RT) where RT.RawValue == UInt64 {
self.code = code
self.scope = Name(rawValue: scope.rawValue).stringValue
self.table = table
Expand Down
11 changes: 8 additions & 3 deletions Sources/EOSIO/Chain/ABI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ public extension ABI {
let errorCode: UInt64
let errorMsg: String
}

private struct ExtensionsEntry: ABICodable, Equatable, Hashable {
let errorCode: UInt16
let errorMsg: Data
}
}

// MARK: ABI Coding
Expand Down Expand Up @@ -345,7 +350,7 @@ extension ABI: ABICodable {
self.tables = try decoder.decode([ABI.Table].self)
self.ricardianClauses = try decoder.decode([ABI.Clause].self)
_ = try decoder.decode([ABI.ErrorMessage].self) // ignore error messages, used only by abi compiler
_ = try decoder.decode([Never].self) // abi extensions not used
_ = try decoder.decode([ABI.ExtensionsEntry].self) // abi extensions not used
// decode variant typedefs (Y U NO USE EXTENSIONS?!)
do {
self.variants = try decoder.decode([ABI.Variant].self)
Expand All @@ -362,8 +367,8 @@ extension ABI: ABICodable {
try container.encode(self.actions, forKey: .actions)
try container.encode(self.tables, forKey: .tables)
try container.encode(self.ricardianClauses, forKey: .ricardianClauses)
try container.encode([] as [Never], forKey: .errorMessages)
try container.encode([] as [Never], forKey: .abiExtensions)
try container.encode([] as [ABI.ErrorMessage], forKey: .errorMessages)
try container.encode([] as [ABI.ExtensionsEntry], forKey: .abiExtensions)
try container.encode(self.variants, forKey: .variants)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/EOSIO/SigningRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ public enum ChainName: UInt8, CaseIterable, CustomStringConvertible {
case .telos:
return "4667b205c6838ef70ff7988f6e8257e8be0e1284a2f59699054a018f743b1d11"
case .jungle:
return "e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473"
return "73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d"
case .kylin:
return "5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191"
case .worbli:
Expand Down
46 changes: 36 additions & 10 deletions Tests/EOSIOTests/ABICodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ final class ABICodableTests: XCTestCase {

func testComplexABI() {
let abi = try! ABI(json: loadTestResource("atomicassets.abi.json"))
let json = """
let colJson = """
{
"author": "foobar",
"collection_name": "test",
Expand All @@ -248,15 +248,41 @@ final class ABICodableTests: XCTestCase {
]
}
"""
let object = try! JSONDecoder().decode("createcol", from: json.utf8Data, using: abi)
let recoded = try! JSONEncoder().encode(object, asType: "createcol", using: abi)
XCTAssertEqual(json.normalizedJSON, recoded.utf8String.normalizedJSON)

let data = try! ABIEncoder().encode(object, asType: "createcol", using: abi)
XCTAssertEqual(data.hexEncodedString(), "000000005c73285d000000000090b1ca0102000000005c73285d0000000050baae3902000000005c73285d0000000050baae391bde8342cac0f33f01036f6e65083d0ad73e")
let object2 = try! ABIDecoder().decode("createcol", from: data, using: abi)
let recoded2 = try! JSONEncoder().encode(object2, asType: "createcol", using: abi)
XCTAssertEqual(json.normalizedJSON, recoded2.utf8String.normalizedJSON)
let colObject = try! JSONDecoder().decode("createcol", from: colJson.utf8Data, using: abi)
let colRecoded = try! JSONEncoder().encode(colObject, asType: "createcol", using: abi)
XCTAssertEqual(colJson.normalizedJSON, colRecoded.utf8String.normalizedJSON)

let colData = try! ABIEncoder().encode(colObject, asType: "createcol", using: abi)
XCTAssertEqual(colData.hexEncodedString(), "000000005c73285d000000000090b1ca0102000000005c73285d0000000050baae3902000000005c73285d0000000050baae391bde8342cac0f33f01036f6e65083d0ad73e")
let colObject2 = try! ABIDecoder().decode("createcol", from: colData, using: abi)
let colRecoded2 = try! JSONEncoder().encode(colObject2, asType: "createcol", using: abi)
XCTAssertEqual(colJson.normalizedJSON, colRecoded2.utf8String.normalizedJSON)

let templateJson = """
{
"authorized_creator": "foobar",
"collection_name": "test",
"schema_name": "test",
"transferable": true,
"burnable": true,
"max_supply": 0,
"immutable_data": [
{"key": "one", "value": ["float32", "0.42"]},
{"key": "name", "value": ["string", "Test"]}
]
}
"""

let templateObject = try! JSONDecoder().decode("createtempl", from: templateJson.utf8Data, using: abi)
let templateRecoded = try! JSONEncoder().encode(templateObject, asType: "createtempl", using: abi)
XCTAssertEqual(templateJson.normalizedJSON, templateRecoded.utf8String.normalizedJSON)

let templateData = try! ABIEncoder().encode(templateObject, asType: "createtempl", using: abi)
XCTAssertEqual(templateData.hexEncodedString(), "000000005c73285d000000000090b1ca000000000090b1ca01010000000002036f6e65083d0ad73e046e616d650a0454657374")
let templateObject2 = try! ABIDecoder().decode("createtempl", from: templateData, using: abi)
let templateRecoded2 = try! JSONEncoder().encode(templateObject2, asType: "createtempl", using: abi)
XCTAssertEqual(templateJson.normalizedJSON, templateRecoded2.utf8String.normalizedJSON)

}

func testComplexVariant() {
Expand Down
18 changes: 9 additions & 9 deletions Tests/EOSIOTests/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var mockSession = MockSession(
resourcePath.appendingPathComponent("API", isDirectory: true),
mode: env["MOCK_RECORD"] != nil ? .record : .replay
)
let nodeAddress = URL(string: "https://jungle3.greymass.com")! // only used when recording
let nodeAddress = URL(string: "https://jungle4.greymass.com")! // only used when recording
let client = Client(address: nodeAddress, session: mockSession)

final class APITests: XCTestCase {
Expand All @@ -39,7 +39,7 @@ final class APITests: XCTestCase {
let req = API.V1.Chain.GetTableRows<CurrencyStats>(
code: "eosio.token",
table: "stat",
scope: Asset.Symbol("4,EOS").symbolCode
scope: Asset.Symbol("4,EOS").name
)
let res = try? client.sendSync(req).get()
XCTAssertGreaterThan(res?.rows.first?.supply ?? "0.0000 EOS", "1000000000.0000 EOS")
Expand All @@ -50,7 +50,7 @@ final class APITests: XCTestCase {
func testGetInfo() {
let req = API.V1.Chain.GetInfo()
let res = try? client.sendSync(req).get()
XCTAssertEqual(res?.chainId, "e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473")
XCTAssertEqual(res?.chainId, "73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d")
}

func testGetRawAbi() {
Expand Down Expand Up @@ -98,24 +98,24 @@ final class APITests: XCTestCase {
let res = try! client.sendSync(req).get()

XCTAssertEqual(res.transactionId, transaction.id)
XCTAssertEqual(res.processed["blockNum"] as! Int, 60_423_261)
XCTAssertEqual(res.processed["blockNum"] as! Int, 129_978_519)
}

func testGetAccount() {
let req = API.V1.Chain.GetAccount("eosio")
let res = try! client.sendSync(req).get()
XCTAssertEqual(res.accountName, "eosio")
XCTAssertEqual(res.cpuLimit.used, -1)
XCTAssertEqual(res.cpuLimit.used, 149300)
XCTAssertEqual(res.voterInfo?.proxy, 0)
XCTAssertEqual(res.voterInfo?.producers.count, 0)
}

func testGetAccountsByAuthorizersUsingKey() {
let pubkey = PublicKey("EOS8X5SC2m6Q1iBpvP91mLBNpEu9LYuynC44os35n5RKaAVt9n7Ji")
let pubkey = PublicKey("EOS8aL4dzqLudQQbcNZYVeyuHfjE7K76BMNGRRT8hwyURPPapDt4h")
let req = API.V1.Chain.GetAccountsByAuthorizers(keys: [pubkey])
let res = try! client.sendSync(req).get()
XCTAssertEqual(res.accounts.first?.accountName, "jestasmobile")
XCTAssertEqual(res.accounts.first?.permissionName, "owner")
XCTAssertEqual(res.accounts.first?.accountName, "iamthewalrus")
XCTAssertEqual(res.accounts.first?.permissionName, "active")
XCTAssertEqual(res.accounts.first?.authorizingKey, pubkey)
XCTAssertEqual(res.accounts.first?.threshold, 1)
XCTAssertEqual(res.accounts.first?.weight, 1)
Expand Down Expand Up @@ -176,7 +176,7 @@ final class APITests: XCTestCase {
)
let req = API.V1.Chain.GetAccount("teamgreymass")
let res = try! client.sendSync(req).get()
XCTAssertEqual(res.netWeight, 432_714_455_333)
XCTAssertEqual(res.netWeight, 455_096_542_392)
XCTAssert(res.netWeight > 0xFFFFFF)
}

Expand Down
Loading

0 comments on commit 4dd389c

Please sign in to comment.