Skip to content

Commit

Permalink
Merge pull request #90 from hyperledger/feature/serialization-factory
Browse files Browse the repository at this point in the history
Serialization factory
  • Loading branch information
ERussel authored Mar 26, 2019
2 parents 01a6f7a + 1ddfe9b commit 9ab8640
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 26 deletions.
80 changes: 80 additions & 0 deletions Example/IrohaCommunication.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
6003F5B2195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; };
7C8964A2BAE86B246752F5D5 /* Pods_UnitTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 872B4DCAED8E7A58AFC8B200 /* Pods_UnitTests.framework */; };
844761D3224A4946009ADC0E /* IRInvalidSerializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761D2224A4946009ADC0E /* IRInvalidSerializationTests.m */; };
844761E7224A4EFF009ADC0E /* Block.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761D6224A4EFF009ADC0E /* Block.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761E8224A4EFF009ADC0E /* Endpoint.pbrpc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761D8224A4EFF009ADC0E /* Endpoint.pbrpc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761E9224A4EFF009ADC0E /* QryResponses.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DA224A4EFF009ADC0E /* QryResponses.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761EA224A4EFF009ADC0E /* Queries.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DC224A4EFF009ADC0E /* Queries.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761EB224A4EFF009ADC0E /* Transaction.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DD224A4EFF009ADC0E /* Transaction.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761EC224A4EFF009ADC0E /* Endpoint.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DE224A4EFF009ADC0E /* Endpoint.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761ED224A4EFF009ADC0E /* Primitive.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761E0224A4EFF009ADC0E /* Primitive.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761EE224A4EFF009ADC0E /* Proposal.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761E2224A4EFF009ADC0E /* Proposal.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761EF224A4EFF009ADC0E /* Commands.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761E5224A4EFF009ADC0E /* Commands.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
844761F1224A5133009ADC0E /* IRQueryResponseDeserializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761F0224A5133009ADC0E /* IRQueryResponseDeserializationTests.m */; };
8CA1A478927FFA33BA30A5B4 /* Pods_IntegrationTestHost.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 175938D8DE70C85E0CEA25AF /* Pods_IntegrationTestHost.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -139,6 +150,26 @@
606FC2411953D9B200FFA9A0 /* Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Prefix.pch"; sourceTree = "<group>"; };
6BF3097DA0B1239D7D3C0799 /* Pods-UnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.release.xcconfig"; sourceTree = "<group>"; };
74D33D817C5F7989BD3071B1 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
844761D2224A4946009ADC0E /* IRInvalidSerializationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IRInvalidSerializationTests.m; sourceTree = "<group>"; };
844761D5224A4EFF009ADC0E /* Primitive.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Primitive.pbobjc.h; sourceTree = "<group>"; };
844761D6224A4EFF009ADC0E /* Block.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Block.pbobjc.m; sourceTree = "<group>"; };
844761D7224A4EFF009ADC0E /* Endpoint.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Endpoint.pbobjc.h; sourceTree = "<group>"; };
844761D8224A4EFF009ADC0E /* Endpoint.pbrpc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Endpoint.pbrpc.m; sourceTree = "<group>"; };
844761D9224A4EFF009ADC0E /* Proposal.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Proposal.pbobjc.h; sourceTree = "<group>"; };
844761DA224A4EFF009ADC0E /* QryResponses.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QryResponses.pbobjc.m; sourceTree = "<group>"; };
844761DB224A4EFF009ADC0E /* Commands.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Commands.pbobjc.h; sourceTree = "<group>"; };
844761DC224A4EFF009ADC0E /* Queries.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Queries.pbobjc.m; sourceTree = "<group>"; };
844761DD224A4EFF009ADC0E /* Transaction.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Transaction.pbobjc.m; sourceTree = "<group>"; };
844761DE224A4EFF009ADC0E /* Endpoint.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Endpoint.pbobjc.m; sourceTree = "<group>"; };
844761DF224A4EFF009ADC0E /* Block.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Block.pbobjc.h; sourceTree = "<group>"; };
844761E0224A4EFF009ADC0E /* Primitive.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Primitive.pbobjc.m; sourceTree = "<group>"; };
844761E1224A4EFF009ADC0E /* QryResponses.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QryResponses.pbobjc.h; sourceTree = "<group>"; };
844761E2224A4EFF009ADC0E /* Proposal.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Proposal.pbobjc.m; sourceTree = "<group>"; };
844761E3224A4EFF009ADC0E /* Endpoint.pbrpc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Endpoint.pbrpc.h; sourceTree = "<group>"; };
844761E4224A4EFF009ADC0E /* Queries.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Queries.pbobjc.h; sourceTree = "<group>"; };
844761E5224A4EFF009ADC0E /* Commands.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Commands.pbobjc.m; sourceTree = "<group>"; };
844761E6224A4EFF009ADC0E /* Transaction.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Transaction.pbobjc.h; sourceTree = "<group>"; };
844761F0224A5133009ADC0E /* IRQueryResponseDeserializationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IRQueryResponseDeserializationTests.m; sourceTree = "<group>"; };
84EA860F14455E442B5C9B29 /* IrohaCommunication.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = IrohaCommunication.podspec; path = ../IrohaCommunication.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
86F19532265991B547599707 /* Pods-IrohaExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-IrohaExample.release.xcconfig"; path = "Pods/Target Support Files/Pods-IrohaExample/Pods-IrohaExample.release.xcconfig"; sourceTree = "<group>"; };
872B4DCAED8E7A58AFC8B200 /* Pods_UnitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UnitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -381,6 +412,8 @@
6003F5B5195388D20070C39A /* Tests */ = {
isa = PBXGroup;
children = (
844761D4224A4EFF009ADC0E /* ProtoGen */,
844761D1224A4946009ADC0E /* Serialization */,
021C24E621DD3C90005D5304 /* Query */,
02E319DB21DBB18500B33B66 /* Promise */,
02E3198921D0FED500B33B66 /* Transaction */,
Expand Down Expand Up @@ -410,6 +443,42 @@
name = "Podspec Metadata";
sourceTree = "<group>";
};
844761D1224A4946009ADC0E /* Serialization */ = {
isa = PBXGroup;
children = (
844761D2224A4946009ADC0E /* IRInvalidSerializationTests.m */,
844761F0224A5133009ADC0E /* IRQueryResponseDeserializationTests.m */,
);
name = Serialization;
path = ../../Tests/Serialization;
sourceTree = "<group>";
};
844761D4224A4EFF009ADC0E /* ProtoGen */ = {
isa = PBXGroup;
children = (
844761D5224A4EFF009ADC0E /* Primitive.pbobjc.h */,
844761D6224A4EFF009ADC0E /* Block.pbobjc.m */,
844761D7224A4EFF009ADC0E /* Endpoint.pbobjc.h */,
844761D8224A4EFF009ADC0E /* Endpoint.pbrpc.m */,
844761D9224A4EFF009ADC0E /* Proposal.pbobjc.h */,
844761DA224A4EFF009ADC0E /* QryResponses.pbobjc.m */,
844761DB224A4EFF009ADC0E /* Commands.pbobjc.h */,
844761DC224A4EFF009ADC0E /* Queries.pbobjc.m */,
844761DD224A4EFF009ADC0E /* Transaction.pbobjc.m */,
844761DE224A4EFF009ADC0E /* Endpoint.pbobjc.m */,
844761DF224A4EFF009ADC0E /* Block.pbobjc.h */,
844761E0224A4EFF009ADC0E /* Primitive.pbobjc.m */,
844761E1224A4EFF009ADC0E /* QryResponses.pbobjc.h */,
844761E2224A4EFF009ADC0E /* Proposal.pbobjc.m */,
844761E3224A4EFF009ADC0E /* Endpoint.pbrpc.h */,
844761E4224A4EFF009ADC0E /* Queries.pbobjc.h */,
844761E5224A4EFF009ADC0E /* Commands.pbobjc.m */,
844761E6224A4EFF009ADC0E /* Transaction.pbobjc.h */,
);
name = ProtoGen;
path = ../../ProtoGen;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -788,16 +857,27 @@
files = (
02E3198B21D0FEF000B33B66 /* IRTransactionTests.m in Sources */,
0200346F21C840D1001A6A53 /* IRAmountTests.m in Sources */,
844761E7224A4EFF009ADC0E /* Block.pbobjc.m in Sources */,
02E319DD21DBB18500B33B66 /* IRPromiseTests.m in Sources */,
844761ED224A4EFF009ADC0E /* Primitive.pbobjc.m in Sources */,
0200347521C8516F001A6A53 /* IRGrantablePermissionTests.m in Sources */,
844761EC224A4EFF009ADC0E /* Endpoint.pbobjc.m in Sources */,
0200347121C849FF001A6A53 /* IRAssetIdTests.m in Sources */,
844761D3224A4946009ADC0E /* IRInvalidSerializationTests.m in Sources */,
844761EA224A4EFF009ADC0E /* Queries.pbobjc.m in Sources */,
021C24E821DD3C90005D5304 /* IRQueryBuilderTests.m in Sources */,
844761E8224A4EFF009ADC0E /* Endpoint.pbrpc.m in Sources */,
844761EF224A4EFF009ADC0E /* Commands.pbobjc.m in Sources */,
0200347721C853E2001A6A53 /* IRRolePermissionTests.m in Sources */,
021C24F021DDF706005D5304 /* IRAccountAssetTests.m in Sources */,
0200345E21C62D3F001A6A53 /* IRDomainTests.m in Sources */,
844761E9224A4EFF009ADC0E /* QryResponses.pbobjc.m in Sources */,
021C843D21EA6E950037F633 /* IRBlockQueryBuilderTests.m in Sources */,
844761EB224A4EFF009ADC0E /* Transaction.pbobjc.m in Sources */,
0200345F21C62D3F001A6A53 /* IRAccountIdTests.m in Sources */,
844761F1224A5133009ADC0E /* IRQueryResponseDeserializationTests.m in Sources */,
0200346121C63F45001A6A53 /* IRAddressTests.m in Sources */,
844761EE224A4EFF009ADC0E /* Proposal.pbobjc.m in Sources */,
021C24AF21DCDDE3005D5304 /* IRPaginationTests.m in Sources */,
0200347321C84E5C001A6A53 /* IRRoleNameTests.m in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions IrohaCommunication.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'IrohaCommunication'
s.version = '3.0.2'
s.version = '3.1.0'
s.summary = 'Helper classes to use for communication with Iroha blockchain.'

s.homepage = 'https://github.com/hyperledger/iroha-ios'
Expand All @@ -33,7 +33,7 @@ Pod::Spec.new do |s|
s.pod_target_xcconfig = { 'GCC_PREPROCESSOR_DEFINITIONS' => 'GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1' }

s.test_spec do |ts|
ts.source_files = 'Tests/**/*.{h,m}'
ts.source_files = 'Tests/**/*.{h,m}', 'ProtoGen/*.{h,m}'
end

end
8 changes: 0 additions & 8 deletions IrohaCommunication/Classes/Private/IRTransactionImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ - (nullable id)transform:(NSError*_Nullable*_Nullable)error {
return transaction;
}

#pragma mark - Transaction data

- (nullable NSData *)transactionData {
Transaction *transaction = [self transform:nil];

return transaction.data;
}

#pragma mark - Signable

- (nullable NSData*)transactionHashWithError:(NSError **)error {
Expand Down
1 change: 0 additions & 1 deletion IrohaCommunication/Classes/Public/Model/IRTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ typedef NS_ENUM(NSUInteger, IRTransactionBatchType) {
@property(nonatomic, readonly)NSArray<id<IRPeerSignature>>* _Nullable signatures;
@property(nonatomic, readonly)NSArray<NSData*>* _Nullable batchHashes;
@property(nonatomic, readonly)IRTransactionBatchType batchType;
@property(nonatomic, readonly)NSData* _Nullable transactionData;

- (nullable NSData*)transactionHashWithError:(NSError **)error;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#import <Foundation/Foundation.h>
#import "IRTransaction.h"
#import "IRQueryRequest.h"
#import "IRQueryResponse.h"

@protocol IRSerializationFactoryProtocol <NSObject>

+ (nullable NSData*)serializeTransaction:(nonnull id<IRTransaction>)transaction error:(NSError**)error;
+ (nullable NSData*)serializeQueryRequest:(nonnull id<IRQueryRequest>)queryRequest error:(NSError**)error;

+ (nullable id<IRTransaction>)deserializeTransactionFromData:(nonnull NSData*)data error:(NSError**)error;
+ (nullable id<IRQueryResponse>)deserializeQueryResponseFromData:(nonnull NSData*)data error:(NSError**)error;

@end

@interface IRSerializationFactory : NSObject<IRSerializationFactoryProtocol>

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#import "IRSerializationFactory.h"
#import "IRTransactionImpl.h"
#import "IRQueryRequestImpl.h"
#import "IRTransactionImpl+Proto.h"
#import "IRQueryResponse+Proto.h"
#import "Transaction.pbobjc.h"
#import "Queries.pbobjc.h"
#import "QryResponses.pbobjc.h"

@implementation IRSerializationFactory

+ (nullable NSData*)serializeTransaction:(nonnull id<IRTransaction>)transaction error:(NSError**)error {
if (![transaction conformsToProtocol:@protocol(IRProtobufTransformable)]) {

if (error) {
NSString *message = @"Unsupported transaction implementation";
*error = [NSError errorWithDomain:NSStringFromClass([IRSerializationFactory class])
code:IRTransactionErrorSerialization
userInfo:@{NSLocalizedDescriptionKey: message}];
}

return nil;
}

Transaction *pbTransaction = [(id<IRProtobufTransformable>)transaction transform:error];

if (!pbTransaction) {
return nil;
}

return [pbTransaction data];
}

+ (nullable NSData*)serializeQueryRequest:(nonnull id<IRQueryRequest>)queryRequest error:(NSError**)error {
if (![queryRequest conformsToProtocol:@protocol(IRProtobufTransformable)]) {

if (error) {
NSString *message = @"Unsupported query request implementation";
*error = [NSError errorWithDomain:NSStringFromClass([IRSerializationFactory class])
code:IRQueryRequestErrorSerialization
userInfo:@{NSLocalizedDescriptionKey: message}];
}

return nil;
}

Query *protobufQuery = [(id<IRProtobufTransformable>)queryRequest transform:error];

if (!protobufQuery) {
return nil;
}

return [protobufQuery data];
}

+ (nullable id<IRTransaction>)deserializeTransactionFromData:(nonnull NSData*)data error:(NSError**)error {
Transaction *transaction = [[Transaction alloc] initWithData:data error:error];

if (!transaction) {
return nil;
}

return [IRTransaction transactionFromPbTransaction:transaction error:error];
}

+ (nullable id<IRQueryResponse>)deserializeQueryResponseFromData:(nonnull NSData*)data error:(NSError**)error {
QueryResponse *response = [[QueryResponse alloc] initWithData:data error:error];

if (!response) {
return nil;
}

return [IRQueryResponseProtoFactory responseFromProtobuf:response error:error];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ - (nonnull IRPromise*)executeQueryRequest:(nonnull id<IRQueryRequest>)queryReque
if (![queryRequest conformsToProtocol:@protocol(IRProtobufTransformable)]) {
NSString *message = @"Unsupported query implementation";
NSError *error = [NSError errorWithDomain:NSStringFromClass([IRNetworkService class])
code:IRTransactionErrorSerialization
code:IRQueryRequestErrorSerialization
userInfo:@{NSLocalizedDescriptionKey: message}];

[promise fulfillWithResult:error];
Expand Down
1 change: 0 additions & 1 deletion Tests/Promise/IRPromiseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@import XCTest;
@import IrohaCommunication;


@interface IRPromiseTests : XCTestCase

@end
Expand Down
7 changes: 7 additions & 0 deletions Tests/Query/IRQueryBuilderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ - (void)testQueries {

XCTAssertNotNil(signedQueryRequest.peerSignature);
XCTAssertNil(error);

error = nil;
NSData *rawQueryRequestData = [IRSerializationFactory serializeQueryRequest:signedQueryRequest
error:&error];

XCTAssertNotNil(rawQueryRequestData);
XCTAssertNil(error);
}

}
Expand Down
35 changes: 35 additions & 0 deletions Tests/Serialization/IRInvalidSerializationTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

@import XCTest;
@import IrohaCommunication;

@interface IRInvalidSerializationTests : XCTestCase

@end

@implementation IRInvalidSerializationTests

- (void)testInvalidTransactionDeserialization {
NSError *error = nil;
NSData *invalidTransactionData = [[NSData alloc] init];
id<IRTransaction> invalidTransaction = [IRSerializationFactory deserializeTransactionFromData:invalidTransactionData
error:&error];

XCTAssertNil(invalidTransaction);
XCTAssertNotNil(error);
}

- (void)testInvalidQueryResponseDeserialization {
NSError *error = nil;
NSData *invalidQueryResponseData = [[NSData alloc] init];
id<IRQueryResponse> invalidQueryResponse = [IRSerializationFactory deserializeQueryResponseFromData:invalidQueryResponseData
error:&error];

XCTAssertNil(invalidQueryResponse);
XCTAssertNotNil(error);
}

@end
Loading

0 comments on commit 9ab8640

Please sign in to comment.