This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
DP-701 queue #53
Open
cnotethegr8
wants to merge
54
commits into
epic/DP-665_batch-payments
Choose a base branch
from
DP-701_queue
base: epic/DP-665_batch-payments
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
DP-701 queue #53
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
385fa47
Merge branch 'DP-689_stub-api' into epic/DP-665_batch-payments
cnotethegr8 8a5e189
Merge branch 'DP-689_stub-api' into epic/DP-665_batch-payments
cnotethegr8 25b60fc
building queues
cnotethegr8 f584a01
building tests
cnotethegr8 e6440a7
tests for payments queue
cnotethegr8 3aca2d1
initial work on connecting transaction params to queue
cnotethegr8 1ad83c7
removed external signing and abstract account interface
cnotethegr8 0dc8c28
adding operation to tx params
cnotethegr8 57b3214
simplified kin to quark conversions
cnotethegr8 f80ad7e
assets tests
cnotethegr8 7a1bce3
restructured node to static prop
cnotethegr8 ddd25dc
connected transaction params operation values
cnotethegr8 df89a16
combined common operation code. working on tests
cnotethegr8 0d2b015
Network class now holds url
cnotethegr8 cb06f80
removed external passphrase
cnotethegr8 7eb03a0
fixed data types
cnotethegr8 334443c
async operation
cnotethegr8 f093999
tests and further connections to the queue
cnotethegr8 9bbbff2
fixed tests
cnotethegr8 f0ada8f
improved test
cnotethegr8 7db0d9b
optional interceptor
cnotethegr8 077ae69
nullable metadata
cnotethegr8 528b36b
removed unneeded code
cnotethegr8 6fd0ad3
async operation tests
cnotethegr8 454244a
improving tests
cnotethegr8 cb8ea66
improving tests
cnotethegr8 d2b7b68
created pending payments transaction
cnotethegr8 6be9e19
building transaction queue merge and tests
cnotethegr8 8fc3096
transaction queue tests completed
cnotethegr8 55b3c9e
starting to implement the interceptor
cnotethegr8 cb09395
working on the interceptor
cnotethegr8 75a7301
cleaned up warnings
cnotethegr8 e8284cc
building operations and transactions
cnotethegr8 26f08bf
fixed queue in progress var
cnotethegr8 a043f2b
removed comment
cnotethegr8 94c1686
fixed tests
cnotethegr8 85eca14
Merge remote-tracking branch 'origin/epic/DP-665_batch-payments' into…
cnotethegr8 6dc786f
passing source public address to batch payments transaction
cnotethegr8 ec1c59d
fixed failing tests
cnotethegr8 2b14db6
simplified common logic
cnotethegr8 972bd77
filled in transaction process send functions
cnotethegr8 08aa723
cleanup
cnotethegr8 d2f5617
interceptor is weak
cnotethegr8 820d991
starting tx process tests
cnotethegr8 dfc35d5
merged tests branch. extended interface to use generics
cnotethegr8 13e338d
removed async operation class
cnotethegr8 584ab19
essentials
cnotethegr8 c5c0d7b
fixed error
cnotethegr8 9377aaa
passing better defined data
cnotethegr8 c59eef3
removed account from stellar as property
cnotethegr8 6471b7b
fixed tests
cnotethegr8 ece94d4
stellaraccount public address is no longer optional
cnotethegr8 c61f01c
created kin account protocol
cnotethegr8 2cb3d2e
moved kin account protocol to its own file
cnotethegr8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Asset.swift | ||
// KinSDK | ||
// | ||
// Created by Corey Werner on 20/08/2019. | ||
// Copyright © 2019 Kin Foundation. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Kin is the native currency of the network. | ||
*/ | ||
public typealias Kin = Decimal | ||
|
||
extension Kin { | ||
/** | ||
Convert `Kin` into `Quark`. | ||
|
||
This value should only be used to fees. | ||
*/ | ||
public func toQuark() -> Quark { | ||
return (self * Decimal(AssetUnitDivisor) as NSDecimalNumber).uint32Value | ||
} | ||
|
||
func toQuarkAsBlockchainUnit() -> Int64 { | ||
return (self * Decimal(AssetUnitDivisor) as NSDecimalNumber).int64Value | ||
} | ||
} | ||
|
||
/** | ||
Quark is the smallest amount unit. It is one-hundred-thousandth of a Kin: `1/100000` or `0.00001`. | ||
*/ | ||
public typealias Quark = UInt32 | ||
|
||
extension Quark { | ||
public func toKin() -> Kin { | ||
return Decimal(self) / Decimal(AssetUnitDivisor) | ||
} | ||
} | ||
|
||
@available(*, deprecated, renamed: "Quark") | ||
public typealias Stroop = Quark | ||
|
||
let AssetUnitDivisor: UInt32 = 100_000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
KinSDK/KinSDK/BulkPayments/PaymentQueueTransactionProcess.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// PaymentQueueTransactionProcess.swift | ||
// KinSDK | ||
// | ||
// Created by Corey Werner on 05/09/2019. | ||
// Copyright © 2019 Kin Foundation. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class PaymentQueueTransactionProcess: TransactionProcess { | ||
public let pendingPayments: [PendingPayment] | ||
let fee: Quark | ||
let stellarAccount: StellarAccount | ||
|
||
init(pendingPayments: [PendingPayment], fee: Quark, stellar: StellarProtocol, stellarAccount: StellarAccount) { | ||
self.pendingPayments = pendingPayments | ||
self.fee = fee | ||
self.stellarAccount = stellarAccount | ||
|
||
super.init(stellar: stellar) | ||
} | ||
|
||
public override func transaction() throws -> BatchPaymentTransaction { | ||
return try transaction(memo: nil) | ||
} | ||
|
||
public func transaction(memo memoString: String?) throws -> BatchPaymentTransaction { | ||
let dispatchGroup = DispatchGroup() | ||
dispatchGroup.enter() | ||
|
||
var result: Result<BatchPaymentTransaction, Error> = .failure(KinError.internalInconsistency) | ||
|
||
var memo: Memo = .MEMO_NONE | ||
|
||
if let memoString = memoString { | ||
memo = try Memo(memoString) | ||
} | ||
|
||
stellar.transaction(sourceStellarAccount: stellarAccount, pendingPayments: pendingPayments, memo: memo, fee: fee) | ||
.then { transaction in | ||
result = .success(transaction) | ||
dispatchGroup.leave() | ||
} | ||
.error { error in | ||
result = .failure(error) | ||
dispatchGroup.leave() | ||
} | ||
|
||
dispatchGroup.wait() | ||
|
||
switch result { | ||
case .success(let transaction): | ||
return transaction | ||
case .failure(let error): | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// PaymentsQueueManager.swift | ||
// KinSDK | ||
// | ||
// Created by Corey Werner on 15/08/2019. | ||
// Copyright © 2019 Kin Foundation. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol PaymentsQueueManagerDelegate: NSObjectProtocol { | ||
func paymentsQueueManager(_ manager: PaymentsQueueManager, dequeueing pendingPayments: [PendingPayment]) | ||
} | ||
|
||
class PaymentsQueueManager { | ||
let maxPendingPayments = TransactionTasksQueueManager.maxPendingPaymentCount | ||
let maxPaymentsTime: TimeInterval | ||
let maxTimeoutTime: TimeInterval | ||
private var paymentsTimer: Timer? | ||
private var timeoutTimer: Timer? | ||
private var payments: [PendingPayment] = [] | ||
|
||
weak var delegate: PaymentsQueueManagerDelegate? | ||
|
||
init(maxPaymentsTime: TimeInterval = 4, maxTimeoutTime: TimeInterval = 10) { | ||
self.maxPaymentsTime = maxPaymentsTime | ||
self.maxTimeoutTime = maxTimeoutTime | ||
} | ||
|
||
var inProgress: Bool { | ||
cnotethegr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return timeoutTimer != nil || paymentsTimer != nil | ||
} | ||
|
||
var operationsCount: Int { | ||
return payments.count | ||
} | ||
|
||
func enqueue(pendingPayment: PendingPayment) { | ||
cnotethegr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func enqueu() { | ||
updateTimers() | ||
|
||
payments.append(pendingPayment) | ||
|
||
if operationsCount >= maxPendingPayments { | ||
dequeue() | ||
} | ||
} | ||
|
||
if Thread.isMainThread { | ||
enqueu() | ||
} | ||
else { | ||
DispatchQueue.main.async { | ||
enqueu() | ||
} | ||
} | ||
} | ||
|
||
@objc private func dequeue() { | ||
removeTimers() | ||
|
||
delegate?.paymentsQueueManager(self, dequeueing: payments) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't you using the same reference here? I think you should clone payments o.w. unexpected things can happen when we clear here the list. |
||
|
||
payments.removeAll() | ||
} | ||
|
||
private func updateTimers() { | ||
if timeoutTimer == nil { | ||
timeoutTimer = .scheduledTimer(timeInterval: maxTimeoutTime, target: self, selector: #selector(dequeue), userInfo: nil, repeats: false) | ||
} | ||
|
||
paymentsTimer?.invalidate() | ||
paymentsTimer = .scheduledTimer(timeInterval: maxPaymentsTime, target: self, selector: #selector(dequeue), userInfo: nil, repeats: false) | ||
} | ||
|
||
private func removeTimers() { | ||
timeoutTimer?.invalidate() | ||
timeoutTimer = nil | ||
|
||
paymentsTimer?.invalidate() | ||
paymentsTimer = nil | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
failed delegate shouldn't have transaction object (or have the standard (?) pattern of single
transactionCompleted
completion with both optional error and result)