From 8fed319d5179f5857955cd6f133e403e8f0ad32f Mon Sep 17 00:00:00 2001 From: Carter Harrison Date: Tue, 12 Feb 2019 19:40:32 -0800 Subject: [PATCH 1/2] added indexing to block repo and updated objectmodel --- Podfile | 2 +- Podfile.lock | 10 ++-- sdk-core-swift.podspec | 2 +- sdk-core-swift.xcodeproj/project.pbxproj | 31 ++---------- .../boundWitness/XyoBoundWitness.swift | 6 +-- .../boundWitness/XyoBoundWitnessUtil.swift | 4 +- .../boundWitness/XyoZigZagBoundWitness.swift | 6 +-- sdk-core-swift/network/tcp/XyoTcpPeer.swift | 2 - sdk-core-swift/node/XyoBridgingOption.swift | 2 +- ...oStrageProviderOriginBlockRepository.swift | 47 ++++++++++++++++++- 10 files changed, 65 insertions(+), 47 deletions(-) diff --git a/Podfile b/Podfile index e75b112..002b652 100644 --- a/Podfile +++ b/Podfile @@ -8,7 +8,7 @@ target 'sdk-core-swift' do # Pods for sdk-core-swift pod 'PromisesSwift', '~> 1.2.3' - pod 'sdk-objectmodel-swift', '~> 0.1.1-beta.0' + pod 'sdk-objectmodel-swift', '~> 0.1.2-beta.0' target 'sdk-core-swiftTests' do inherit! :search_paths diff --git a/Podfile.lock b/Podfile.lock index ccf7cab..935534e 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -2,11 +2,11 @@ PODS: - PromisesObjC (1.2.5) - PromisesSwift (1.2.5): - PromisesObjC (= 1.2.5) - - sdk-objectmodel-swift (0.1.1-beta.0) + - sdk-objectmodel-swift (0.1.2-beta.0) DEPENDENCIES: - PromisesSwift (~> 1.2.3) - - sdk-objectmodel-swift (~> 0.1.1-beta.0) + - sdk-objectmodel-swift (~> 0.1.2-beta.0) SPEC REPOS: https://github.com/cocoapods/specs.git: @@ -17,8 +17,8 @@ SPEC REPOS: SPEC CHECKSUMS: PromisesObjC: 2b402ef5333bb9b436c74ee1bd7dcb2511b68caf PromisesSwift: 63d93b8cc31b4245ca1014ff5cc24d6ae952a274 - sdk-objectmodel-swift: 7356581ad62bf22700a195ecc266ef0a537ec31e + sdk-objectmodel-swift: c0ae76ff76772989d60a9ee9e53cd2ef24d8fb22 -PODFILE CHECKSUM: 563cc283d09f32c96e9a624ae17efce027639b83 +PODFILE CHECKSUM: 6073acd8291963907908efa4a70a821a541b7d54 -COCOAPODS: 1.5.3 +COCOAPODS: 1.6.0 diff --git a/sdk-core-swift.podspec b/sdk-core-swift.podspec index a87807e..cb14825 100644 --- a/sdk-core-swift.podspec +++ b/sdk-core-swift.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'sdk-core-swift' - s.version = '0.1.2-beta.0' + s.version = '0.1.2-beta.1' s.summary = 'Core Library for XYO Network in Swift.' # This description is used to generate tags and improve search results. diff --git a/sdk-core-swift.xcodeproj/project.pbxproj b/sdk-core-swift.xcodeproj/project.pbxproj index 9ba163a..2aaaea6 100644 --- a/sdk-core-swift.xcodeproj/project.pbxproj +++ b/sdk-core-swift.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 3F217B3DD64FF1E6A136C46B /* Pods_sdk_core_swiftTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B18D506875636A5D6A1F6E96 /* Pods_sdk_core_swiftTests.framework */; }; + 8C361F3A2213C7710049B9EF /* XyoTcpPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C361F392213C7710049B9EF /* XyoTcpPeer.swift */; }; 8C42B7C021F7C5CC006F6F86 /* sdk_core_swift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8C42B7B621F7C5CC006F6F86 /* sdk_core_swift.framework */; }; 8C42B7C721F7C5CC006F6F86 /* sdk_core_swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 8C42B7B921F7C5CC006F6F86 /* sdk_core_swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 8C54005021FF721B008A3CE3 /* XyoOriginChainCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C54004F21FF721B008A3CE3 /* XyoOriginChainCreator.swift */; }; @@ -76,6 +77,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 8C361F392213C7710049B9EF /* XyoTcpPeer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XyoTcpPeer.swift; sourceTree = ""; }; 8C42B7B621F7C5CC006F6F86 /* sdk_core_swift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = sdk_core_swift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 8C42B7B921F7C5CC006F6F86 /* sdk_core_swift.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sdk_core_swift.h; sourceTree = ""; }; 8C42B7BA21F7C5CC006F6F86 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -431,6 +433,7 @@ children = ( 8CF3AE352200C83C00D6EED9 /* XyoTcpSocket.swift */, 8CF3AE392200FB9900D6EED9 /* XyoTcpSocketPipe.swift */, + 8C361F392213C7710049B9EF /* XyoTcpPeer.swift */, ); path = tcp; sourceTree = ""; @@ -487,7 +490,6 @@ 8C42B7BB21F7C5CC006F6F86 /* Sources */, 8C42B7BC21F7C5CC006F6F86 /* Frameworks */, 8C42B7BD21F7C5CC006F6F86 /* Resources */, - EBE827EF746A572F50E39265 /* [CP] Embed Pods Frameworks */, ); buildRules = ( ); @@ -576,32 +578,6 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - EBE827EF746A572F50E39265 /* [CP] Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputFileListPaths = ( - ); - inputPaths = ( - "${SRCROOT}/Pods/Target Support Files/Pods-sdk-core-swiftTests/Pods-sdk-core-swiftTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/PromisesObjC/FBLPromises.framework", - "${BUILT_PRODUCTS_DIR}/PromisesSwift/Promises.framework", - "${BUILT_PRODUCTS_DIR}/sdk-objectmodel-swift/sdk_objectmodel_swift.framework", - ); - name = "[CP] Embed Pods Frameworks"; - outputFileListPaths = ( - ); - outputPaths = ( - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FBLPromises.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Promises.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/sdk_objectmodel_swift.framework", - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-sdk-core-swiftTests/Pods-sdk-core-swiftTests-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; F83AFD414FF04C8CAD185E95 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -657,6 +633,7 @@ 8CF3AE3121FFE30D00D6EED9 /* XyoFlagProcedureCatalogue.swift in Sources */, 8C6542B321F7E8A100124CB0 /* XyoHasher.swift in Sources */, 8C6542B521F7E97F00124CB0 /* XyoSha256.swift in Sources */, + 8C361F3A2213C7710049B9EF /* XyoTcpPeer.swift in Sources */, 8C6542D521F91A0E00124CB0 /* XyoBoundWitnessUtil.swift in Sources */, 8C6542E521FAA7A500124CB0 /* XyoZigZagBoundWitnessSession.swift in Sources */, 8C54005D21FF9797008A3CE3 /* XyoBridgeQueue.swift in Sources */, diff --git a/sdk-core-swift/boundWitness/XyoBoundWitness.swift b/sdk-core-swift/boundWitness/XyoBoundWitness.swift index b381017..711c004 100644 --- a/sdk-core-swift/boundWitness/XyoBoundWitness.swift +++ b/sdk-core-swift/boundWitness/XyoBoundWitness.swift @@ -100,10 +100,10 @@ open class XyoBoundWitness : XyoIterableStructure { static func createMasterArrayWithSubArray (masterSchema : XyoObjectSchema, subSchema : XyoObjectSchema, masterItems: [XyoObjectStructure], - subItems: [XyoObjectStructure]) throws -> XyoObjectStructure { - let sub = try XyoIterableStructure.createUntypedIterableObject(schema: subSchema, values: subItems) + subItems: [XyoObjectStructure]) -> XyoObjectStructure { + let sub = XyoIterableStructure.createUntypedIterableObject(schema: subSchema, values: subItems) var itemsInMaster : [XyoObjectStructure] = [sub] itemsInMaster.append(contentsOf: masterItems) - return try XyoIterableStructure.createUntypedIterableObject(schema: masterSchema, values: itemsInMaster) + return XyoIterableStructure.createUntypedIterableObject(schema: masterSchema, values: itemsInMaster) } } diff --git a/sdk-core-swift/boundWitness/XyoBoundWitnessUtil.swift b/sdk-core-swift/boundWitness/XyoBoundWitnessUtil.swift index 860ea33..f486fd5 100644 --- a/sdk-core-swift/boundWitness/XyoBoundWitnessUtil.swift +++ b/sdk-core-swift/boundWitness/XyoBoundWitnessUtil.swift @@ -23,7 +23,7 @@ public struct XyoBoundWitnessUtil { newBoundWitnessLedger.append(try removeTypeFromWitness(witness: witness, id: id)) } - let createBoundWitness = try XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.BW, values: newBoundWitnessLedger).getBuffer() + let createBoundWitness = XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.BW, values: newBoundWitnessLedger).getBuffer() return XyoBoundWitness(value: createBoundWitness) } @@ -44,7 +44,7 @@ public struct XyoBoundWitnessUtil { } } - return try XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.WITNESS, values: newWitnessContents) + return XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.WITNESS, values: newWitnessContents) } public static func getPartyNumberFromPublicKey (publickey : XyoObjectStructure, boundWitness : XyoBoundWitness) throws -> Int? { diff --git a/sdk-core-swift/boundWitness/XyoZigZagBoundWitness.swift b/sdk-core-swift/boundWitness/XyoZigZagBoundWitness.swift index b9f2e1b..c837f38 100644 --- a/sdk-core-swift/boundWitness/XyoZigZagBoundWitness.swift +++ b/sdk-core-swift/boundWitness/XyoZigZagBoundWitness.swift @@ -20,7 +20,7 @@ class XyoZigZagBoundWitness : XyoBoundWitness { self.signedPayload = signedPayload self.unsignedPayload = unsignedPayload - super.init(value: try XyoIterableStructure.encodeUntypedIterableObject(schema: XyoSchemas.BW, values: [])) + super.init(value: XyoIterableStructure.encodeUntypedIterableObject(schema: XyoSchemas.BW, values: [])) } public func incomingData (transfer : XyoIterableStructure?, endpoint: Bool) throws -> XyoIterableStructure { @@ -108,7 +108,7 @@ class XyoZigZagBoundWitness : XyoBoundWitness { return try XyoIterableStructure.createTypedIterableObject(schema: XyoSchemas.FETTER_SET, values: fetters) } - return try XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.BW_FRAGMENT, values: items) + return XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.BW_FRAGMENT, values: items) } private func getNumberOfWitnessesFromTransfer (transfer: XyoIterableStructure?) throws -> Int { @@ -143,7 +143,7 @@ class XyoZigZagBoundWitness : XyoBoundWitness { signatures.append(try signCurrent(signer: signer)) } - let witness = try XyoBoundWitness.createMasterArrayWithSubArray(masterSchema: XyoSchemas.WITNESS, + let witness = XyoBoundWitness.createMasterArrayWithSubArray(masterSchema: XyoSchemas.WITNESS, subSchema: XyoSchemas.SIGNATURE_SET, masterItems: unsignedPayload, subItems: signatures) diff --git a/sdk-core-swift/network/tcp/XyoTcpPeer.swift b/sdk-core-swift/network/tcp/XyoTcpPeer.swift index 13e0404..e148844 100644 --- a/sdk-core-swift/network/tcp/XyoTcpPeer.swift +++ b/sdk-core-swift/network/tcp/XyoTcpPeer.swift @@ -1,11 +1,9 @@ - // // XyoTcpPeer.swift // mod-ble-swift // // Created by Carter Harrison on 2/12/19. // - import Foundation public struct XyoTcpPeer { diff --git a/sdk-core-swift/node/XyoBridgingOption.swift b/sdk-core-swift/node/XyoBridgingOption.swift index b9927c7..6961236 100644 --- a/sdk-core-swift/node/XyoBridgingOption.swift +++ b/sdk-core-swift/node/XyoBridgingOption.swift @@ -39,7 +39,7 @@ class XyoBridgingOption: XyoBoundWitnessOption { if (blockHashes.count > 0) { let hashSet = try XyoIterableStructure.createTypedIterableObject(schema: XyoSchemas.BRIDGE_HASH_SET, values: blockHashes) - let blockSet = try XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.BRIDGE_BLOCK_SET, values: blocks) + let blockSet = XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.BRIDGE_BLOCK_SET, values: blocks) return XyoBoundWitnessHueresticPair(signedPayload: [hashSet], unsignedPayload: [blockSet]) } diff --git a/sdk-core-swift/persist/XyoStrageProviderOriginBlockRepository.swift b/sdk-core-swift/persist/XyoStrageProviderOriginBlockRepository.swift index 3ef0fc0..32e3241 100644 --- a/sdk-core-swift/persist/XyoStrageProviderOriginBlockRepository.swift +++ b/sdk-core-swift/persist/XyoStrageProviderOriginBlockRepository.swift @@ -10,16 +10,18 @@ import Foundation import sdk_objectmodel_swift public class XyoStrageProviderOriginBlockRepository: XyoOriginBlockRepository { + private static let BLOCK_INDEX_KEY : [UInt8] = [0x00, 0x00] private let storageProvider : XyoStorageProvider private let hasher : XyoHasher - public init(storageProvider : XyoStorageProvider, hasher : XyoHasher) throws { + public init(storageProvider : XyoStorageProvider, hasher : XyoHasher) { self.storageProvider = storageProvider self.hasher = hasher } public func removeOriginBlock (originBlockHash : [UInt8]) throws { try storageProvider.delete(key: originBlockHash) + try updateBlockIndex(hashToRemove: originBlockHash) } public func getOriginBlock (originBlockHash : [UInt8]) throws -> XyoBoundWitness? { @@ -35,9 +37,50 @@ public class XyoStrageProviderOriginBlockRepository: XyoOriginBlockRepository { } public func addOriginBlock (originBlock : XyoBoundWitness) throws { - let key = try originBlock.getHash(hasher: hasher).getBuffer().toByteArray() + let hash = try originBlock.getHash(hasher: hasher) + let key = hash.getBuffer().toByteArray() let value = originBlock.getBuffer().toByteArray() try storageProvider.write(key: key, value: value) + try updateBlockIndex(hashToAdd: hash) + } + + private func getBlockIndex () throws -> XyoIterableStructure { + guard let value = try storageProvider.read(key: XyoStrageProviderOriginBlockRepository.BLOCK_INDEX_KEY) else { + return XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.ARRAY_UNTYPED, values: []) + } + + return XyoIterableStructure(value: XyoBuffer(data: value)) + } + + private func updateBlockIndex (hashToAdd : XyoObjectStructure) throws { + let currentIndex = try getBlockIndex() + try currentIndex.addElement(element: hashToAdd) + try storageProvider.write(key: XyoStrageProviderOriginBlockRepository.BLOCK_INDEX_KEY, value: currentIndex.getBuffer().toByteArray()) + } + + private func updateBlockIndex (hashToRemove : [UInt8]) throws { + var newHashes = [XyoObjectStructure]() + let currentIndex = try getBlockIndex().getNewIterator() + + while try currentIndex.hasNext() { + let hashInList = try currentIndex.next() + + if (hashInList.getBuffer().toByteArray() != hashToRemove) { + newHashes.append(hashInList) + } + } + + let newIndex = XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.ARRAY_UNTYPED, values: newHashes) + try storageProvider.write(key: XyoStrageProviderOriginBlockRepository.BLOCK_INDEX_KEY, value: newIndex.getBuffer().toByteArray()) + } + + public func getAllOriginBlockHashes () -> XyoIterableStructure { + do { + return try getBlockIndex() + } catch { + return XyoIterableStructure.createUntypedIterableObject(schema: XyoSchemas.ARRAY_UNTYPED, values: []) + } } } + From 3604275275c5ef8839877e6c609e6470ff589e7f Mon Sep 17 00:00:00 2001 From: Carter Harrison Date: Tue, 12 Feb 2019 19:41:32 -0800 Subject: [PATCH 2/2] update podspec --- sdk-core-swift.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk-core-swift.podspec b/sdk-core-swift.podspec index cb14825..f2a6560 100644 --- a/sdk-core-swift.podspec +++ b/sdk-core-swift.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'sdk-core-swift' - s.version = '0.1.2-beta.1' + s.version = '0.1.2-beta.2' s.summary = 'Core Library for XYO Network in Swift.' # This description is used to generate tags and improve search results.