Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DashSync/shared/Categories/BigIntTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define NoTimeLog(format, ...) CFShow([NSString stringWithFormat:format, ##__VA_ARGS__]);
#endif

#define FLAG_IS_SET(value, flag) ((value & flag) == flag)

typedef union _UInt768 {
uint8_t u8[768 / 8];
Expand Down
4 changes: 2 additions & 2 deletions DashSync/shared/Categories/NSData/NSData+Dash.m
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ uint16_t compactBitsLE(UInt256 number) {
for (int pos = 7; pos >= 0; pos--) {
if (number.u32[pos]) {
for (int bits = 31; bits > 0; bits--) {
if (number.u32[pos] & 1 << bits)
if (number.u32[pos] & 1U << bits)
return 32 * pos + bits + 1;
}
return 32 * pos + 1;
Expand Down Expand Up @@ -993,7 +993,7 @@ UInt256 uInt256DivideLE(UInt256 a, UInt256 b) {
while (shift >= 0) {
if (uint256_supeq(num, div)) {
num = uInt256SubtractLE(num, div);
r.u32[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
r.u32[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
}
div = uInt256ShiftRightLE(div, 1); // shift back.
shift--;
Expand Down
98 changes: 6 additions & 92 deletions DashSync/shared/DSDashSharedCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//

#import "DSBlock.h"
#import "DSChain+Checkpoint.h"
#import "DSChain+Params.h"
#import "DSChain+Protected.h"
#import "DSChain+Identity.h"
Expand Down Expand Up @@ -46,31 +47,13 @@
#define GetBlockHeightByHash Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_u32
#define GetBlockHashByHeight Fn_ARGS_std_os_raw_c_void_u32_RTRN_Option_u8_32

#define MerkleBlockByBlockHash Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_Result_ok_dash_spv_masternode_processor_common_block_MBlock_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError
#define LastMerkleBlockByBlockHashForPeer Fn_ARGS_std_os_raw_c_void_Arr_u8_32_std_os_raw_c_void_RTRN_Result_ok_dash_spv_masternode_processor_common_block_MBlock_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError


#define AddInsight Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_

#define HasPersistInRetrieval Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_bool
#define GetBlockHeightOrLastTerminal Fn_ARGS_std_os_raw_c_void_u32_RTRN_Result_ok_dash_spv_masternode_processor_common_block_Block_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError

#define FnMaybeCLSignature Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_Result_ok_dashcore_bls_sig_utils_BLSSignature_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError
#define DMaybeCLSignature Result_ok_dashcore_bls_sig_utils_BLSSignature_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError
#define DMaybeCLSignatureCtor(ok, err) Result_ok_dashcore_bls_sig_utils_BLSSignature_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError_ctor(ok, err)
#define LoadMasternodeList Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_Result_ok_dashcore_sml_masternode_list_MasternodeList_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError
#define SaveMasternodeList Fn_ARGS_std_os_raw_c_void_Arr_u8_32_std_collections_Map_keys_u8_arr_32_values_dashcore_sml_masternode_list_entry_qualified_masternode_list_entry_QualifiedMasternodeListEntry_RTRN_Result_ok_bool_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError
#define LoadLLMQSnapshot Fn_ARGS_std_os_raw_c_void_Arr_u8_32_RTRN_Result_ok_dashcore_network_message_qrinfo_QuorumSnapshot_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError
#define SaveLLMQSnapshot Fn_ARGS_std_os_raw_c_void_Arr_u8_32_dashcore_network_message_qrinfo_QuorumSnapshot_RTRN_Result_ok_bool_err_dash_spv_masternode_processor_processing_core_provider_CoreProviderError

#define UpdateMasternodesAddressUsage Fn_ARGS_std_os_raw_c_void_Vec_dashcore_sml_masternode_list_entry_qualified_masternode_list_entry_QualifiedMasternodeListEntry_RTRN_

#define IssueWithMasternodeList Fn_ARGS_std_os_raw_c_void_bool_std_os_raw_c_void_RTRN_
#define NotifySyncState Fn_ARGS_std_os_raw_c_void_dash_spv_masternode_processor_models_sync_state_CacheState_RTRN_

@interface DSDashSharedCore ()

@property (nonatomic) DSChain *chain;
@property (nonatomic, strong) DSChain *chain;
@property (nonatomic, assign) DashSPVCore *core;
@property (nonatomic, strong) NSMutableDictionary *devnetSharedCoreDictionary;

Expand Down Expand Up @@ -149,16 +132,6 @@ - (instancetype)initOnChain:(DSChain *)chain {
UpdateMasternodesAddressUsage update_address_usage_of_masternodes = {
.caller = &update_address_usage_of_masternodes_caller
};
IssueWithMasternodeList issue_with_masternode_list_from_peer = {
.caller = &issue_with_masternode_list_from_peer_caller
};
FnMaybeCLSignature get_cl_signature_by_block_hash = {
.caller = &get_cl_signature_by_block_hash_caller,
.destructor = &get_cl_signature_by_block_hash_dtor
};
NotifySyncState notify_sync_state = {
.caller = &notify_sync_state_caller,
};

NSArray<NSString *> *addresses = @[@"127.0.0.1"];
switch (chain.chainType->tag) {
Expand All @@ -178,17 +151,8 @@ - (instancetype)initOnChain:(DSChain *)chain {
break;
}
Vec_ *address_list = [NSArray ffi_to_vec:addresses];
dash_spv_apple_bindings_DiffConfig *diff_config = NULL;
if ([chain isMainnet]) {
NSString *bundlePath = [[NSBundle bundleForClass:self.class] pathForResource:@"DashSync" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *filePath = [bundle pathForResource:@"mn_list_diff_0_2227096" ofType:@"bin"];
NSData *data = [NSData dataWithContentsOfFile:filePath];

diff_config = dash_spv_apple_bindings_DiffConfig_ctor(bytes_ctor(data), 2227096);
}

self.core = dash_spv_apple_bindings_DashSPVCore_with_callbacks(chain.chainType, diff_config, address_list, get_data_contract, get_platform_activation_height, callback_signer, callback_can_sign, get_block_height_by_hash, get_block_hash_by_height, get_cl_signature_by_block_hash, update_address_usage_of_masternodes, issue_with_masternode_list_from_peer, notify_sync_state, context);
dash_spv_masternode_processor_processing_processor_DiffConfig *diff_config = [chain createDiffConfig];
self.core = dash_spv_apple_bindings_DashSPVCore_with_callbacks(chain.chainType, diff_config, address_list, get_data_contract, get_platform_activation_height, callback_signer, callback_can_sign, get_block_height_by_hash, get_block_hash_by_height, update_address_usage_of_masternodes, context);
return self;
}

Expand Down Expand Up @@ -248,7 +212,8 @@ uint32_t get_block_height_by_hash_caller(const void *context, u256 *block_hash)
height = [[core.chain insightVerifiedBlocksByHashDictionary] objectForKey:NSDataFromPtr(block_hash)].height;
}
u256_dtor(block_hash);
DSLog(@"[SDK] get_block_height_by_hash_caller: %@ = %u", uint256_hex(blockHash), height);
if (height == UINT32_MAX)
DSLog(@"[SDK] get_block_height_by_hash_caller: %@ = %u", uint256_hex(blockHash), height);
return height;
}
void get_block_height_by_hash_dtor(uint32_t result) {}
Expand All @@ -268,61 +233,10 @@ void get_block_height_by_hash_dtor(uint32_t result) {}

void get_block_hash_by_height_dtor(u256 *result) {}

DMaybeCLSignature *get_cl_signature_by_block_hash_caller(const void *context, u256 *block_hash) {
DSDashSharedCore *core = AS_OBJC(context);
UInt256 blockHash = u256_cast(block_hash);
UInt256 blockHashRev = uint256_reverse(blockHash);
u256_dtor(block_hash);
DSChainLock *chainLock = [core.chain.chainManager chainLockForBlockHash:blockHash];
DSChainLock *chainLockRev = [core.chain.chainManager chainLockForBlockHash:blockHashRev];
if (chainLock) {
DBLSSignature *bls_sig = DChainLockSignature(chainLock.lock);
DSLog(@"[SDK] get_cl_signature_by_block_hash_caller: %@ = %@", uint256_hex(blockHash), u768_hex(bls_sig->_0));
return DMaybeCLSignatureCtor(bls_sig, NULL);
} else if (chainLockRev) {
DBLSSignature *bls_sig = DChainLockSignature(chainLockRev.lock);
DSLog(@"[SDK] get_cl_signature_by_block_hash_caller: %@ = %@", uint256_hex(blockHashRev), u768_hex(bls_sig->_0));
return DMaybeCLSignatureCtor(bls_sig, NULL);
} else {
DSLog(@"[SDK] get_cl_signature_by_block_hash_caller: %@ = None", uint256_hex(blockHash));
return DMaybeCLSignatureCtor(NULL, DCoreProviderErrorNullResultCtor(DSLocalizedChar(@"No clsig for block hash %@", nil, uint256_hex(blockHash))));
}
}
void get_cl_signature_by_block_hash_dtor(DMaybeCLSignature *result) {}


void update_address_usage_of_masternodes_caller(const void *context, DMasternodeEntryList *masternodes) {
DSDashSharedCore *core = AS_OBJC(context);
[core.chain updateAddressUsageOfSimplifiedMasternodeEntries:masternodes];
DMasternodeEntryListDtor(masternodes);
}

void issue_with_masternode_list_from_peer_caller(const void *context, bool is_dip24, const void *peer_context) {
DSDashSharedCore *core = AS_OBJC(context);
DSPeer *peer = ((__bridge DSPeer *)(peer_context));
[core.chain.masternodeManager issueWithMasternodeListFromPeer:peer];
}

void notify_sync_state_caller(const void *context, DMNSyncState *state) {
DSDashSharedCore *core = AS_OBJC(context);
DSMasternodeListSyncState *syncInfo = core.chain.chainManager.syncState.masternodeListSyncInfo;
@synchronized (syncInfo) {
[syncInfo updateWithSyncState:state];
switch (state->tag) {
case DMNSyncStateQueueChanged:
DSLog(@"[%@] Masternode list queue updated: %lu/%lu", core.chain.name, state->queue_changed.count, state->queue_changed.max_amount);
break;
case DMNSyncStateStoreChanged:
DSLog(@"[%@] Masternode list store updated: %lu/%u", core.chain.name, state->store_changed.count, state->store_changed.last_block_height);
break;
case DMNSyncStateStubCount:
DSLog(@"[%@] Masternode list DB updated: %lu", core.chain.name, state->stub_count.count);
default:
break;
}
DMNSyncStateDtor(state);
[core.chain.chainManager notifySyncStateChanged];
}
}

@end
12 changes: 8 additions & 4 deletions DashSync/shared/DashSync.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ - (void)wipeBlockchainDataForChain:(DSChain *)chain inContext:(NSManagedObjectCo
[DSDerivationPathEntity deleteDerivationPathsOnChainEntity:chainEntity];
[DSFriendRequestEntity deleteFriendRequestsOnChainEntity:chainEntity];
[chain wipeBlockchainInfoInContext:context];
[chain.chainManager restartChainSyncStartHeight];
[chain.chainManager restartTerminalSyncStartHeight];
chain.chainManager.syncPhase = DSChainSyncPhase_InitialTerminalBlocks;
dispatch_async(chain.networkingQueue, ^{
[chain.chainManager restartChainSyncStartHeight];
[chain.chainManager restartTerminalSyncStartHeight];
chain.chainManager.syncPhase = DSChainSyncPhase_InitialTerminalBlocks;
});
[DSBlockchainIdentityEntity deleteBlockchainIdentitiesOnChainEntity:chainEntity];
[DSDashpayUserEntity deleteContactsOnChainEntity:chainEntity]; // this must move after wipeBlockchainInfo where blockchain identities are removed
[context ds_save];
Expand Down Expand Up @@ -192,7 +194,9 @@ - (void)wipeBlockchainNonTerminalDataForChain:(DSChain *)chain inContext:(NSMana
[DSDerivationPathEntity deleteDerivationPathsOnChainEntity:chainEntity];
[DSFriendRequestEntity deleteFriendRequestsOnChainEntity:chainEntity];
[chain wipeBlockchainNonTerminalInfoInContext:context];
[chain.chainManager restartChainSyncStartHeight];
dispatch_async(chain.networkingQueue, ^{
[chain.chainManager restartChainSyncStartHeight];
});
[DSBlockchainIdentityEntity deleteBlockchainIdentitiesOnChainEntity:chainEntity];
[DSDashpayUserEntity deleteContactsOnChainEntity:chainEntity]; // this must move after wipeBlockchainInfo where blockchain identities are removed
[context ds_save];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSError *)ffi_from_sml_error:(dashcore_sml_error_SmlError *)ffi_ref;
@end

@interface NSError (dashcore_sml_quorum_validation_error_QuorumValidationError)
+ (NSError *)ffi_from_quorum_validation_error:(dashcore_sml_quorum_validation_error_QuorumValidationError *)ffi_ref;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ @implementation NSError (dashcore_sml_quorum_validation_error_QuorumValidationEr
+ (NSError *)ffi_from_quorum_validation_error:(dashcore_sml_quorum_validation_error_QuorumValidationError *)ffi_ref {
switch (ffi_ref->tag) {
case dashcore_sml_quorum_validation_error_QuorumValidationError_RequiredBlockNotPresent: {
u256 *block_hash = dashcore_hash_types_BlockHash_inner(ffi_ref->required_block_not_present);
u256 *block_hash = dashcore_hash_types_BlockHash_inner(ffi_ref->required_block_not_present._0);
return [NSError errorWithCode:0 descriptionKey:DSLocalizedFormat(@"QuorumValidationError::RequiredBlockNotPresent: %@ (%@)", nil, u256_hex(block_hash), u256_reversed_hex(block_hash))];
}
case dashcore_sml_quorum_validation_error_QuorumValidationError_RequiredBlockHeightNotPresent:
Expand Down Expand Up @@ -206,6 +206,8 @@ + (NSError *)ffi_from_processing_error:(DProcessingError *)ffi_ref {
return [NSError errorWithCode:0 descriptionKey:DSLocalizedFormat(@"Decode Error: %@", nil, NSStringFromPtr(ffi_ref->decode_error))];
case dash_spv_masternode_processor_processing_processor_processing_error_ProcessingError_QuorumValidationError:
return [NSError ffi_from_quorum_validation_error:ffi_ref->quorum_validation_error];
case dash_spv_masternode_processor_processing_processor_processing_error_ProcessingError_SML:
return [NSError ffi_from_sml_error:ffi_ref->sml];
}
}
@end
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions DashSync/shared/Models/Chain/DSChain+Checkpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ NS_ASSUME_NONNULL_BEGIN
// MARK: Protected
+ (NSMutableArray *)createCheckpointsArrayFromCheckpoints:(checkpoint *)checkpoints count:(NSUInteger)checkpointCount;

- (dash_spv_masternode_processor_processing_processor_DiffConfig *_Nullable)createDiffConfig;
@end

NS_ASSUME_NONNULL_END
19 changes: 19 additions & 0 deletions DashSync/shared/Models/Chain/DSChain+Checkpoint.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,23 @@ + (NSMutableArray *)createCheckpointsArrayFromCheckpoints:(checkpoint *)checkpoi
return [checkpointMutableArray copy];
}

- (dash_spv_masternode_processor_processing_processor_DiffConfig *_Nullable)createDiffConfig {
dash_spv_masternode_processor_processing_processor_DiffConfig *diff_config = NULL;
if ([self isMainnet]) {
NSString *bundlePath = [[NSBundle bundleForClass:self.class] pathForResource:@"DashSync" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *filePath = [bundle pathForResource:@"mn_list_diff_0_2227096" ofType:@"dat"];
NSData *data = [NSData dataWithContentsOfFile:filePath];

diff_config = dash_spv_masternode_processor_processing_processor_DiffConfig_ctor(bytes_ctor(data), 2227096);
} else if ([self isTestnet]) {
NSString *bundlePath = [[NSBundle bundleForClass:self.class] pathForResource:@"DashSync" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *filePath = [bundle pathForResource:@"MNL_TESTNET_0_1220040" ofType:@"dat"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
diff_config = dash_spv_masternode_processor_processing_processor_DiffConfig_ctor(bytes_ctor(data), 1220040);
}
return diff_config;
}

@end
4 changes: 2 additions & 2 deletions DashSync/shared/Models/Chain/DSChain+Transaction.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ - (uint64_t)amountSentByTransaction:(DSTransaction *)transaction {

//Does the chain mat
- (BOOL)transactionHasLocalReferences:(DSTransaction *)transaction {
if ([self firstAccountThatCanContainTransaction:transaction]) return TRUE;

//PROVIDERS
if ([transaction isKindOfClass:[DSProviderRegistrationTransaction class]]) {
DSProviderRegistrationTransaction *tx = (DSProviderRegistrationTransaction *)transaction;
Expand Down Expand Up @@ -230,6 +228,7 @@ - (BOOL)registerProviderUpdateRevocationTransaction:(DSProviderUpdateRevocationT
return NO;
}
}
// always from chain.networkingQueue
- (BOOL)registerAssetLockTransaction:(DSAssetLockTransaction *)transaction saveImmediately:(BOOL)saveImmediately {
DSAssetLockTransaction *assetLockTransaction = (DSAssetLockTransaction *)transaction;
UInt160 creditBurnPublicKeyHash = assetLockTransaction.creditBurnPublicKeyHash;
Expand Down Expand Up @@ -272,6 +271,7 @@ - (BOOL)registerAssetLockTransaction:(DSAssetLockTransaction *)transaction saveI
return isNewIdentity;
}

// always from chain.networkingQueue
- (BOOL)registerSpecialTransaction:(DSTransaction *)transaction saveImmediately:(BOOL)saveImmediately {
if ([transaction isKindOfClass:[DSProviderRegistrationTransaction class]]) {
return [self registerProviderRegistrationTransaction:(DSProviderRegistrationTransaction *)transaction saveImmediately:saveImmediately];
Expand Down
Loading
Loading