Skip to content

Commit

Permalink
Merge pull request #382 from soramitsu/develop
Browse files Browse the repository at this point in the history
v1.9.4 RC2
  • Loading branch information
ERussel authored Sep 3, 2021
2 parents 6556c23 + 8292566 commit 9151b48
Show file tree
Hide file tree
Showing 31 changed files with 860 additions and 541 deletions.
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract_target 'fearlessAll' do
pod 'SoraKeystore'
pod 'SoraUI', '~> 1.10.3'
pod 'RobinHood', '~> 2.6.0'
pod 'CommonWallet/Core', :git => 'https://github.com/soramitsu/Capital-iOS.git', :commit => '32bbf882c14751edf6cdc27005c6abc997b01f3f'
pod 'CommonWallet/Core', :git => 'https://github.com/soramitsu/Capital-iOS.git', :commit => '4133ff9b81cda094dd8e2b7f32129172224b0227'
pod 'SoraFoundation', '~> 1.0.0'
pod 'SwiftyBeaver'
pod 'Starscream', :git => 'https://github.com/ERussel/Starscream.git', :branch => 'feature/without-origin'
Expand All @@ -30,7 +30,7 @@ abstract_target 'fearlessAll' do
pod 'FireMock', :inhibit_warnings => true
pod 'SoraKeystore'
pod 'RobinHood', '~> 2.6.0'
pod 'CommonWallet/Core', :git => 'https://github.com/soramitsu/Capital-iOS.git', :commit => '32bbf882c14751edf6cdc27005c6abc997b01f3f'
pod 'CommonWallet/Core', :git => 'https://github.com/soramitsu/Capital-iOS.git', :commit => '4133ff9b81cda094dd8e2b7f32129172224b0227'
pod 'Sourcery', '~> 1.4'

end
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ PODS:
- xxHash-Swift (1.0.13)

DEPENDENCIES:
- CommonWallet/Core (from `https://github.com/soramitsu/Capital-iOS.git`, commit `32bbf882c14751edf6cdc27005c6abc997b01f3f`)
- CommonWallet/Core (from `https://github.com/soramitsu/Capital-iOS.git`, commit `4133ff9b81cda094dd8e2b7f32129172224b0227`)
- Cuckoo
- FearlessUtils
- FireMock
Expand Down Expand Up @@ -160,7 +160,7 @@ SPEC REPOS:

EXTERNAL SOURCES:
CommonWallet:
:commit: 32bbf882c14751edf6cdc27005c6abc997b01f3f
:commit: 4133ff9b81cda094dd8e2b7f32129172224b0227
:git: https://github.com/soramitsu/Capital-iOS.git
Starscream:
:branch: feature/without-origin
Expand All @@ -171,7 +171,7 @@ EXTERNAL SOURCES:

CHECKOUT OPTIONS:
CommonWallet:
:commit: 32bbf882c14751edf6cdc27005c6abc997b01f3f
:commit: 4133ff9b81cda094dd8e2b7f32129172224b0227
:git: https://github.com/soramitsu/Capital-iOS.git
Starscream:
:commit: b9e69390d96e71427463469f47cdafb8c0db1b21
Expand Down Expand Up @@ -208,6 +208,6 @@ SPEC CHECKSUMS:
TweetNacl: 3abf4d1d2082b0114e7a67410e300892448951e6
xxHash-Swift: 30bd6a7507b3b7348a277c49b1cb6346c2905ec7

PODFILE CHECKSUM: eb7e972783401bd9aded2ca6e5526391591cc23b
PODFILE CHECKSUM: 2b66ed0117007f5bb0c55590f608dfd3e90323ea

COCOAPODS: 1.10.2
36 changes: 20 additions & 16 deletions fearless.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions fearless/Common/DataProvider/SingleValueProviderFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ extension SingleValueProviderFactory: SingleValueProviderFactoryProtocol {
) throws -> AnySingleValueProvider<TotalRewardItem> {
clearIfNeeded()

guard let historyURL = assetId.subqueryHistoryUrl else {
guard let url = assetId.subqueryHistoryUrl else {
throw DataProviderError.unexpectedSourceResult
}

Expand All @@ -240,10 +240,7 @@ extension SingleValueProviderFactory: SingleValueProviderFactoryProtocol {

let trigger = DataProviderProxyTrigger()

let operationFactory = SubqueryHistoryOperationFactory(
url: historyURL,
filter: [.rewardsAndSlashes]
)
let operationFactory = SubqueryRewardOperationFactory(url: url)

let source = SubqueryRewardSource(
address: address,
Expand Down
129 changes: 19 additions & 110 deletions fearless/Common/DataProvider/Sources/Rewards/SubqueryRewardSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@ import CommonWallet
final class SubqueryRewardSource {
typealias Model = TotalRewardItem

struct SyncState {
let lastId: String?
let receivedCount: Int
let reward: Decimal?
}

let address: String
let chain: Chain
let targetIdentifier: String
let repository: AnyDataProviderRepository<SingleValueProviderObject>
let operationFactory: SubqueryHistoryOperationFactoryProtocol
let operationFactory: SubqueryRewardOperationFactoryProtocol
let trigger: DataProviderTriggerProtocol
let operationManager: OperationManagerProtocol
let pageSize: Int
let logger: LoggerProtocol?

private var lastSyncError: Error?
private var syncing: SyncState?
private var syncing: Bool = false
private var totalReward: TotalRewardItem?
private let mutex = NSLock()

Expand All @@ -32,10 +25,9 @@ final class SubqueryRewardSource {
chain: Chain,
targetIdentifier: String,
repository: AnyDataProviderRepository<SingleValueProviderObject>,
operationFactory: SubqueryHistoryOperationFactoryProtocol,
operationFactory: SubqueryRewardOperationFactoryProtocol,
trigger: DataProviderTriggerProtocol,
operationManager: OperationManagerProtocol,
pageSize: Int = 100,
logger: LoggerProtocol? = nil
) {
self.address = address
Expand All @@ -45,7 +37,6 @@ final class SubqueryRewardSource {
self.operationFactory = operationFactory
self.trigger = trigger
self.operationManager = operationManager
self.pageSize = pageSize
self.logger = logger

sync()
Expand Down Expand Up @@ -74,139 +65,57 @@ final class SubqueryRewardSource {
}

private func sync() {
guard syncing == nil else {
guard !syncing else {
return
}

syncing = SyncState(
lastId: nil,
receivedCount: 0,
reward: nil
)
syncing = true

fetch(cursor: nil)
fetch()
}

private func fetch(cursor: String?) {
let localWrapper = createLocalFetchWrapper()

let remoteOperation = operationFactory.createOperation(
address: address,
count: pageSize,
cursor: cursor
)

let syncOperation = Operation()
private func fetch() {
let remoteOperation = operationFactory.createOperation(address: address)

localWrapper.allOperations.forEach { syncOperation.addDependency($0) }
syncOperation.addDependency(remoteOperation)

syncOperation.completionBlock = {
remoteOperation.completionBlock = {
DispatchQueue.global(qos: .userInitiated).async {
self.mutex.lock()

defer {
self.mutex.unlock()
}

self.processOperations(
localWrapper.targetOperation,
remoteOperation: remoteOperation,
cursor: cursor
)
self.processOperations(remoteOperation: remoteOperation)
}
}

let allOperations = localWrapper.allOperations + [remoteOperation, syncOperation]
operationManager.enqueue(operations: allOperations, in: .transient)
operationManager.enqueue(operations: [remoteOperation], in: .transient)
}

private func processOperations(
_ localOperation: BaseOperation<TotalRewardItem?>,
remoteOperation: BaseOperation<SubqueryHistoryData>,
cursor _: String?
) {
private func processOperations(remoteOperation: BaseOperation<SubqueryRewardOrSlashData>) {
do {
let totalReward = try localOperation.extractNoCancellableResultData()
let remoteData = try remoteOperation.extractNoCancellableResultData()
let newReward = calculateReward(from: remoteData.historyElements.nodes)

let endIndex: Int?

if let reward = totalReward {
endIndex = remoteData.historyElements.nodes.firstIndex {
$0.identifier == reward.lastId
}
} else {
endIndex = nil
}

let allRemoteItems = remoteData.historyElements.nodes
let count = endIndex ?? allRemoteItems.count

let newRemoteItems = Array(allRemoteItems[0 ..< count])
let pageReward = calculateReward(from: newRemoteItems)

let receivedCount = (syncing?.receivedCount ?? 0) + newRemoteItems.count

syncing = SyncState(
lastId: syncing?.lastId ?? newRemoteItems.first?.identifier,
receivedCount: receivedCount,
reward: (syncing?.reward ?? 0.0) + pageReward
)

logger?.debug("Synced id: \(String(describing: allRemoteItems.last?.identifier))")
logger?.debug("Persistent id: \(String(describing: totalReward?.lastId))")
logger?.debug("Page reward: \(pageReward)")

let newCursor = remoteData.historyElements.pageInfo.endCursor

if endIndex != nil || newCursor == nil {
finalize(with: totalReward)
} else {
fetch(cursor: newCursor)
}
logger?.debug("New total reward: \(newReward)")

finalize(with: newReward)
} catch {
finalize(with: error)
}
}

private func finalize(with previousReward: TotalRewardItem?) {
guard let syncState = syncing else {
logger?.warning("Can't finalize sync because of nil")
return
}
private func finalize(with newReward: Decimal) {
syncing = false

if let lastId = syncState.lastId, let reward = syncState.reward {
let newAmount = reward + (previousReward?.amount.decimalValue ?? 0.0)
totalReward = TotalRewardItem(
address: address,
lastId: lastId,
amount: AmountDecimal(value: newAmount)
)

syncing = nil

logger?.debug("Did receive new reward: \(reward)")
} else {
logger?.debug("Sync completed: nothing changed")

totalReward = TotalRewardItem(
address: address,
lastId: previousReward?.lastId ?? "",
amount: previousReward?.amount ?? AmountDecimal(value: 0.0)
)

syncing = nil
}
totalReward = TotalRewardItem(address: address, amount: AmountDecimal(value: newReward))

DispatchQueue.global().async {
self.trigger.delegate?.didTrigger()
}
}

private func restartSync() {
syncing = nil
totalReward = nil
lastSyncError = nil

Expand All @@ -220,7 +129,7 @@ final class SubqueryRewardSource {
private func finalize(with error: Error) {
totalReward = nil
lastSyncError = error
syncing = nil
syncing = false

logger?.error("Did receive sync error: \(error)")

Expand Down
4 changes: 2 additions & 2 deletions fearless/Common/Extension/Model/ConnectionItem+Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension ConnectionItem {
),
ConnectionItem(
title: "Kusama, Patract node",
url: URL(string: "wss://kusama.elara.patract.io")!,
url: URL(string: "wss://pub.elara.patract.io/kusama")!,
type: SNAddressType.kusamaMain
),
ConnectionItem(
Expand All @@ -39,7 +39,7 @@ extension ConnectionItem {
),
ConnectionItem(
title: "Polkadot, Patract node",
url: URL(string: "wss://polkadot.elara.patract.io")!,
url: URL(string: "wss://pub.elara.patract.io/polkadot")!,
type: SNAddressType.polkadotMain
),
ConnectionItem(
Expand Down
Loading

0 comments on commit 9151b48

Please sign in to comment.