From 6bba34dcce96dc4f76f6dbd1174744fb9e0a51d5 Mon Sep 17 00:00:00 2001 From: Ed Gamble Date: Sat, 29 Sep 2018 14:24:05 -0700 Subject: [PATCH] WIP #1 --- .gitignore | 1 + Swift/BRCore/BREthereum.swift | 22 ++++ ethereum/BREthereum.c | 18 ++++ ethereum/BREthereum.h | 33 +++--- ethereum/bcs/BREthereumBCS.c | 110 ++++++++++++++++--- ethereum/bcs/BREthereumBCS.h | 23 +++- ethereum/bcs/BREthereumBCSSync.c | 2 +- ethereum/ewm/BREthereumEWM.c | 33 +++++- ethereum/ewm/BREthereumEWMClient.c | 146 ++++++++++++-------------- ethereum/ewm/BREthereumEWMEvent.c | 44 +++++++- ethereum/ewm/BREthereumEWMPrivate.h | 21 +++- ethereum/ewm/testEwm.c | 107 ++++++++++++++++++- ethereum/les/BREthereumNodeEndpoint.c | 2 +- 13 files changed, 436 insertions(+), 126 deletions(-) diff --git a/.gitignore b/.gitignore index 4a2afb1d5..429f6037c 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ xcuserdata/ .idea/ test +.git-re* diff --git a/Swift/BRCore/BREthereum.swift b/Swift/BRCore/BREthereum.swift index e4d45b5fa..f43b6a757 100644 --- a/Swift/BRCore/BREthereum.swift +++ b/Swift/BRCore/BREthereum.swift @@ -608,6 +608,13 @@ public protocol EthereumClient : class { event: String, rid: Int32) -> Void + func getBlocks (ewm: EthereumWalletManager, + address: String, + flags: Int32, + blockStart: UInt64, + blockStop: UInt64, + rid: Int32) -> Void + func getTokens (ewm: EthereumWalletManager, rid: Int32) -> Void @@ -940,6 +947,17 @@ public class EthereumWalletManager { rid: rid) }}, + funcGetBlocks: { (coreClient, coreEWM, address, flags, blockStart, blockStop, rid) in + if let client = coreClient.map ({ Unmanaged.fromOpaque($0).takeUnretainedValue() }), + let ewm = EthereumWalletManager.lookup(core: coreEWM) { + client.getBlocks (ewm: ewm, + address: asUTF8String (address!), + flags: flags, + blockStart: blockStart, + blockStop: blockStop, + rid: rid) + }}, + funcGetTokens: { (coreClient, coreEWM, rid) in if let client = coreClient.map ({ Unmanaged.fromOpaque($0).takeUnretainedValue() }), let ewm = EthereumWalletManager.lookup(core: coreEWM) { @@ -1223,6 +1241,10 @@ class AnyEthereumClient : EthereumClient { base.getLogs(ewm: ewm, address: address, event: event, rid: rid) } + func getBlocks (ewm: EthereumWalletManager, address: String, flags: Int32, blockStart: UInt64, blockStop: UInt64, rid: Int32) { + base.getBlocks (ewm: ewm, address: address, flags: flags, blockStart: blockStart, blockStop: blockStop, rid: rid) + } + func getTokens (ewm: EthereumWalletManager, rid: Int32) { base.getTokens(ewm: ewm, rid: rid) } diff --git a/ethereum/BREthereum.c b/ethereum/BREthereum.c index fd373a8ef..ec02185c2 100644 --- a/ethereum/BREthereum.c +++ b/ethereum/BREthereum.c @@ -835,6 +835,24 @@ ethereumClientAnnounceLog (BREthereumEWM ewm, // ... // } +// +// Blocks +// +extern BREthereumStatus +ethereumClientAnnounceBlocks (BREthereumEWM ewm, + int id, + // const char *strBlockHash, + BRArrayOf(uint64_t) blockNumbers) { // BRArrayOf(const char *) strBlockNumbers ?? + assert (EWM_USE_LES == ewm->type); + assert (NULL != ewm->bcs); + + // into bcs... + bcsReportInterestingBlocks (ewm->bcs, blockNumbers); + + return SUCCESS; +} + + // // Tokens // diff --git a/ethereum/BREthereum.h b/ethereum/BREthereum.h index 7b6df025c..97334e795 100644 --- a/ethereum/BREthereum.h +++ b/ethereum/BREthereum.h @@ -174,7 +174,16 @@ typedef void const char *address, int rid); -// +typedef void +(*BREthereumClientHandlerGetBlocks) (BREthereumClientContext context, + BREthereumEWM ewm, + const char *address, + int interestFlags, + uint64_t blockNumberStart, + uint64_t blockNumberStop, + int rid); + + // // Save Sync (and other) State // typedef enum { @@ -339,6 +348,7 @@ typedef struct { BREthereumClientHandlerSubmitTransaction funcSubmitTransaction; BREthereumClientHandlerGetTransactions funcGetTransactions; // announce one-by-one BREthereumClientHandlerGetLogs funcGetLogs; // announce one-by-one + BREthereumClientHandlerGetBlocks funcGetBlocks; BREthereumClientHandlerGetTokens funcGetTokens; // announce one-by-one BREthereumClientHandlerGetBlockNumber funcGetBlockNumber; BREthereumClientHandlerGetNonce funcGetNonce; @@ -385,18 +395,6 @@ typedef enum { EWM_USE_LES } BREthereumType; -/*! - * @typedef BREthereumSyncMode - * - * @abstract When starting the EWM we can prime the synchronization with transactions and logs - * queried from the Bread endpoint or we can use a full blockchain synchronization. (After the - * first full sync, partial syncs are used). - */ -typedef enum { - SYNC_MODE_FULL_BLOCKCHAIN, - SYNC_MODE_PRIME_WITH_ENDPOINT -} BREthereumSyncMode; - /** * Create a EWM managing the account associated with the paperKey. (The `paperKey` must * use words from the defaul wordList (Use installSharedWordList). The `paperKey` is used for @@ -945,6 +943,15 @@ ethereumClientAnnounceLog (BREthereumEWM ewm, const char *strBlockTransactionIndex, const char *strBlockTimestamp); +// +// Blocks +// +extern BREthereumStatus +ethereumClientAnnounceBlocks (BREthereumEWM ewm, + int id, + // const char *strBlockHash, + BRArrayOf(uint64_t) blockNumbers); + // // Tokens // diff --git a/ethereum/bcs/BREthereumBCS.c b/ethereum/bcs/BREthereumBCS.c index 5d9d908c1..ef433c3ab 100644 --- a/ethereum/bcs/BREthereumBCS.c +++ b/ethereum/bcs/BREthereumBCS.c @@ -24,6 +24,7 @@ // THE SOFTWARE. #include +#include #include "BRArray.h" #include "BRSet.h" #include "BREthereumBCSPrivate.h" @@ -74,6 +75,21 @@ bcsSyncReportProgressCallback (BREthereumBCS bcs, uint64_t blockNumberBeg, uint64_t blockNumberNow, uint64_t blockNumberEnd); + +static inline BREthereumSyncInterestSet +syncInterestsCreate (int count, /* BREthereumSyncInterest*/ ...) { + BREthereumSyncInterestSet interests = 0;; + + va_list args; + va_start (args, count); + for (int i = 0; i < count; i++) + interests |= va_arg (args, BREthereumSyncInterest); + va_end(args); + + return interests; +} + + /** */ static void @@ -165,6 +181,7 @@ extern BREthereumBCS bcsCreate (BREthereumNetwork network, BREthereumAddress address, BREthereumBCSListener listener, + BREthereumSyncMode syncMode, BRArrayOf(BREthereumNodeConfig) peers, BRArrayOf(BREthereumBlock) blocks, BRArrayOf(BREthereumTransaction) transactions, @@ -176,6 +193,7 @@ bcsCreate (BREthereumNetwork network, bcs->network = network; bcs->address = address; bcs->accountState = accountStateCreateEmpty (); + bcs->syncMode = syncMode; bcs->filterForAddressOnTransactions = bloomFilterCreateAddress(bcs->address); bcs->filterForAddressOnLogs = logTopicGetBloomFilterAddress(bcs->address); @@ -255,12 +273,14 @@ bcsCreate (BREthereumNetwork network, blockGetHash(bcs->chain), peers); - bcs->sync = bcsSyncCreate ((BREthereumBCSSyncContext) bcs, - (BREthereumBCSSyncReportBlocks) bcsSyncReportBlocksCallback, - (BREthereumBCSSyncReportProgress) bcsSyncReportProgressCallback, - bcs->address, - bcs->les, - bcs->handler); + bcs->sync = NULL; + if (SYNC_MODE_FULL_BLOCKCHAIN == bcs->syncMode) + bcs->sync = bcsSyncCreate ((BREthereumBCSSyncContext) bcs, + (BREthereumBCSSyncReportBlocks) bcsSyncReportBlocksCallback, + (BREthereumBCSSyncReportProgress) bcsSyncReportProgressCallback, + bcs->address, + bcs->les, + bcs->handler); return bcs; } @@ -316,15 +336,68 @@ bcsDestroy (BREthereumBCS bcs) { free (bcs); } +static void +bcsSyncRange (BREthereumBCS bcs, + uint64_t blockNumberStart, + uint64_t blockNumberStop) { + switch (bcs->syncMode) { + case SYNC_MODE_FULL_BLOCKCHAIN: { + // + // For a FULL_BLOCKCHAIN sync we run our 'N-Ary Search on Account Changes' algorithm + // which has a (current) weakness on 'ERC20 transfers w/ address as target'. So, we + // exploit the BRD backend, view the `getBlocksCallback()`, to get interesting blocks. + // + assert (NULL != bcs->sync); + if (ETHEREUM_BOOLEAN_IS_FALSE (bcsSyncIsActive(bcs->sync))) { + BREthereumSyncInterestSet interests = syncInterestsCreate(1, CLIENT_GET_BLOCKS_LOGS_AS_TARGET); + bcs->listener.getBlocksCallback (bcs->listener.context, + bcs->address, + interests, + blockNumberStart, + blockNumberStop); + + // Run the 'N-Ary Search' algorithm. + bcsSyncStart(bcs->sync, blockNumberStart, blockNumberStop); + } + break; + } + + case SYNC_MODE_PRIME_WITH_ENDPOINT: { + // + // For a PRIME_WITH_ENDPOINT sync we rely 100% on the BRD backend to provide any and + // all blocks of interest - which is any block involving `address` in a transaction + // or a log. + // + BREthereumSyncInterestSet interests = syncInterestsCreate (4, + CLIENT_GET_BLOCKS_LOGS_AS_SOURCE, + CLIENT_GET_BLOCKS_LOGS_AS_TARGET, + CLIENT_GET_BLOCKS_TRANSACTIONS_AS_SOURCE, + CLIENT_GET_BLOCKS_TRANSACTIONS_AS_TARGET); + bcs->listener.getBlocksCallback (bcs->listener.context, + bcs->address, + interests, + blockNumberStart, + blockNumberStop); + break; + } + } +} + extern void bcsSync (BREthereumBCS bcs, uint64_t blockNumber) { - bcsSyncStart (bcs->sync, blockGetNumber(bcs->chain), blockNumber); + bcsSyncRange (bcs, blockGetNumber(bcs->chain), blockNumber); } extern BREthereumBoolean bcsSyncInProgress (BREthereumBCS bcs) { - return bcsSyncIsActive (bcs->sync); + switch (bcs->syncMode) { + case SYNC_MODE_FULL_BLOCKCHAIN: + assert (NULL != bcs->sync); + return bcsSyncIsActive (bcs->sync); + case SYNC_MODE_PRIME_WITH_ENDPOINT: + return ETHEREUM_BOOLEAN_FALSE; // Nope + } } extern void @@ -355,6 +428,19 @@ bcsSendLogRequest (BREthereumBCS bcs, blockNumber, 1, 0, ETHEREUM_BOOLEAN_FALSE); } +extern void +bcsReportInterestingBlocks (BREthereumBCS bcs, + // interest + // request id + BRArrayOf(uint64_t) blockNumbers) { + eth_log ("BCS", "Report Interesting Blocks: %zu", array_count(blockNumbers)); + for (size_t index = 0; index < array_count(blockNumbers); index++) + lesProvideBlockHeaders (bcs->les, + (BREthereumLESProvisionContext) bcs, + (BREthereumLESProvisionCallback) bcsSignalProvision, + blockNumbers[index], 1, 0, ETHEREUM_BOOLEAN_FALSE); +} + extern void bcsHandleSubmitTransaction (BREthereumBCS bcs, BREthereumTransaction transaction) { @@ -370,7 +456,7 @@ extern void bcsHandleStatus (BREthereumBCS bcs, BREthereumHash headHash, uint64_t headNumber) { - bcsSyncStart(bcs->sync, blockGetNumber(bcs->chain), headNumber); + bcsSyncRange (bcs, blockGetNumber(bcs->chain), headNumber); } /*! @@ -818,9 +904,9 @@ bcsExtendChainIfPossible (BREthereumBCS bcs, // sync to recover (might not actually perform a sync - just attempt). uint64_t orphanBlockNumberMinumum = bcsGetOrphanBlockNumberMinimum(bcs); if (UINT64_MAX != orphanBlockNumberMinumum) - bcsSyncStart(bcs->sync, - blockGetNumber(bcs->chain), - orphanBlockNumberMinumum); + bcsSyncRange (bcs, + blockGetNumber(bcs->chain), + orphanBlockNumberMinumum); return; } diff --git a/ethereum/bcs/BREthereumBCS.h b/ethereum/bcs/BREthereumBCS.h index 27dd7007e..d1057c9bd 100644 --- a/ethereum/bcs/BREthereumBCS.h +++ b/ethereum/bcs/BREthereumBCS.h @@ -29,9 +29,6 @@ #include "../base/BREthereumBase.h" #include "../les/BREthereumLES.h" -#define BRSetOf(type) BRSet* -#define BRArrayOf(type) type* - #ifdef __cplusplus extern "C" { #endif @@ -125,6 +122,17 @@ typedef void uint64_t blockNumberCurrent, uint64_t blockNumberStop); +/** + * Get (Interesting) Blocks. Depending on the sync-mode we may ask the BRD endpoint for blocks + * that contain transactions or logs of interest. + */ +typedef void +(*BREthereumBCSCallbackGetBlocks) (BREthereumBCSCallbackContext context, + BREthereumAddress address, + BREthereumSyncInterestSet interests, + uint64_t blockStart, + uint64_t blockStop); + typedef struct { BREthereumBCSCallbackContext context; BREthereumBCSCallbackBlockchain blockChainCallback; @@ -134,9 +142,9 @@ typedef struct { BREthereumBCSCallbackSaveBlocks saveBlocksCallback; BREthereumBCSCallbackSavePeers savePeersCallback; BREthereumBCSCallbackSync syncCallback; + BREthereumBCSCallbackGetBlocks getBlocksCallback; } BREthereumBCSListener; - /** * Create BCS (a 'BlockChain Slice`) providing a view of the Ethereum blockchain for `network` * focused on the `account` primary address. Initialize the synchronization with the previously @@ -149,6 +157,7 @@ extern BREthereumBCS bcsCreate (BREthereumNetwork network, BREthereumAddress address, BREthereumBCSListener listener, + BREthereumSyncMode syncMode, BRArrayOf(BREthereumNodeConfig) peers, BRArrayOf(BREthereumBlock) blocks, BRArrayOf(BREthereumTransaction) transactions, @@ -211,6 +220,12 @@ bcsSendLogRequest (BREthereumBCS bcs, uint64_t blockNumber, uint64_t blockTransactionIndex); +extern void +bcsReportInterestingBlocks (BREthereumBCS bcs, + // interest + // request id + BRArrayOf(uint64_t) blockNumbers); + #ifdef __cplusplus } #endif diff --git a/ethereum/bcs/BREthereumBCSSync.c b/ethereum/bcs/BREthereumBCSSync.c index 1c3050825..0367de43d 100644 --- a/ethereum/bcs/BREthereumBCSSync.c +++ b/ethereum/bcs/BREthereumBCSSync.c @@ -737,7 +737,7 @@ bcsSyncStart (BREthereumBCSSync sync, SYNC_LINEAR_LIMIT)); } - // Kick of the new sync. + // Kick off the new sync. syncRangeDispatch (sync->root); } diff --git a/ethereum/ewm/BREthereumEWM.c b/ethereum/ewm/BREthereumEWM.c index 9129f491c..dfb9fc31d 100644 --- a/ethereum/ewm/BREthereumEWM.c +++ b/ethereum/ewm/BREthereumEWM.c @@ -170,7 +170,6 @@ createEWM (BREthereumNetwork network, ewm->state = LIGHT_NODE_CREATED; ewm->type = type; - ewm->syncMode = syncMode; ewm->network = network; ewm->account = account; ewm->bcs = NULL; @@ -236,12 +235,14 @@ createEWM (BREthereumNetwork network, (BREthereumBCSCallbackLog) ewmSignalLog, (BREthereumBCSCallbackSaveBlocks) ewmSignalSaveBlocks, (BREthereumBCSCallbackSavePeers) ewmSignalSaveNodes, - (BREthereumBCSCallbackSync) ewmSignalSync + (BREthereumBCSCallbackSync) ewmSignalSync, + (BREthereumBCSCallbackGetBlocks) ewmSignalGetBlocks }; ewm->bcs = bcsCreate (network, accountGetPrimaryAddress (account), listener, + syncMode, nodes, blocks, transactions, @@ -1000,7 +1001,8 @@ ewmHandleSaveBlocks (BREthereumEWM ewm, } // TODO: ewmSignalSaveBlocks(ewm, blocks) - ewm->client.funcSaveBlocks (ewm->client.context, ewm, + ewm->client.funcSaveBlocks (ewm->client.context, + ewm, blocksToSave); array_free (blocks); @@ -1025,7 +1027,8 @@ ewmHandleSaveNodes (BREthereumEWM ewm, } // TODO: ewmSignalSavenodes(ewm, nodes); - ewm->client.funcSaveNodes (ewm->client.context, ewm, + ewm->client.funcSaveNodes (ewm->client.context, + ewm, nodesToSave); eth_log("EWM", "Save nodes: %zu", nodesCount); @@ -1039,18 +1042,38 @@ ewmHandleSync (BREthereumEWM ewm, uint64_t blockNumberStart, uint64_t blockNumberCurrent, uint64_t blockNumberStop) { + assert (EWM_USE_LES == ewm->type); + BREthereumEWMEvent event = (blockNumberCurrent == blockNumberStart ? EWM_EVENT_SYNC_STARTED : (blockNumberCurrent == blockNumberStop ? EWM_EVENT_SYNC_STOPPED : EWM_EVENT_SYNC_CONTINUES)); double syncCompletePercent = 100.0 * (blockNumberCurrent - blockNumberStart) / (blockNumberStop - blockNumberStart); - + ewmClientSignalEWMEvent (ewm, event, SUCCESS, NULL); eth_log ("EWM", "Sync: %d, %.2f%%", type, syncCompletePercent); } +extern void +ewmHandleGetBlocks (BREthereumEWM ewm, + BREthereumAddress address, + BREthereumSyncInterestSet interests, + uint64_t blockStart, + uint64_t blockStop) { + + char *strAddress = addressGetEncodedString(address, 0); + + ewm->client.funcGetBlocks (ewm->client.context, + ewm, + strAddress, + interests, + blockStart, + blockStop, + ++ewm->requestId); +} + // // Periodic Dispatcher // diff --git a/ethereum/ewm/BREthereumEWMClient.c b/ethereum/ewm/BREthereumEWMClient.c index 71c4cf842..72327ee3c 100644 --- a/ethereum/ewm/BREthereumEWMClient.c +++ b/ethereum/ewm/BREthereumEWMClient.c @@ -66,12 +66,11 @@ ewmUpdateWalletBalance(BREthereumEWM ewm, case EWM_USE_BRD: { char *address = addressGetEncodedString(walletGetAddress(wallet), 0); - ewm->client.funcGetBalance - (ewm->client.context, - ewm, - wid, - address, - ++ewm->requestId); + ewm->client.funcGetBalance (ewm->client.context, + ewm, + wid, + address, + ++ewm->requestId); free(address); break; @@ -125,11 +124,10 @@ ewmUpdateWalletDefaultGasPrice (BREthereumEWM ewm, case EWM_USE_LES: // fall-through case EWM_USE_BRD: { - ewm->client.funcGetGasPrice - (ewm->client.context, - ewm, - wid, - ++ewm->requestId); + ewm->client.funcGetGasPrice (ewm->client.context, + ewm, + wid, + ++ewm->requestId); break; } } @@ -178,15 +176,14 @@ ewmUpdateTransferGasEstimate (BREthereumEWM ewm, char *amount = coerceString(amountInEther.valueInWEI, 16); char *data = (char *) transactionGetData(transaction); - ewm->client.funcEstimateGas - (ewm->client.context, - ewm, - wid, - tid, - to, - amount, - data, - ++ewm->requestId); + ewm->client.funcEstimateGas (ewm->client.context, + ewm, + wid, + tid, + to, + amount, + data, + ++ewm->requestId); free(to); free(amount); @@ -222,10 +219,9 @@ ewmUpdateBlockNumber (BREthereumEWM ewm) { break; case EWM_USE_BRD: - ewm->client.funcGetBlockNumber - (ewm->client.context, - ewm, - ++ewm->requestId); + ewm->client.funcGetBlockNumber (ewm->client.context, + ewm, + ++ewm->requestId); break; } } @@ -260,11 +256,10 @@ ewmUpdateNonce (BREthereumEWM ewm) { case EWM_USE_BRD: { char *address = addressGetEncodedString(accountGetPrimaryAddress(ewm->account), 0); - ewm->client.funcGetNonce - (ewm->client.context, - ewm, - address, - ++ewm->requestId); + ewm->client.funcGetNonce (ewm->client.context, + ewm, + address, + ++ewm->requestId); free (address); break; @@ -309,11 +304,10 @@ ewmUpdateTransactions (BREthereumEWM ewm) { case EWM_USE_BRD: { char *address = addressGetEncodedString(accountGetPrimaryAddress(ewm->account), 0); - ewm->client.funcGetTransactions - (ewm->client.context, - ewm, - address, - ++ewm->requestId); + ewm->client.funcGetTransactions (ewm->client.context, + ewm, + address, + ++ewm->requestId); free (address); break; @@ -406,13 +400,12 @@ ewmUpdateLogs (BREthereumEWM ewm, eventERC20TransferEncodeAddress (event, address); const char *contract =ewmGetWalletContractAddress(ewm, wid); - ewm->client.funcGetLogs - (ewm->client.context, - ewm, - contract, - encodedAddress, - eventGetSelector(event), - ++ewm->requestId); + ewm->client.funcGetLogs (ewm->client.context, + ewm, + contract, + encodedAddress, + eventGetSelector(event), + ++ewm->requestId); free (encodedAddress); free (address); @@ -500,13 +493,12 @@ ewmWalletSubmitTransfer(BREthereumEWM ewm, : RLP_TYPE_TRANSACTION_UNSIGNED), "0x"); - ewm->client.funcSubmitTransaction - (ewm->client.context, - ewm, - ewmLookupWalletId(ewm, wallet), - ewmLookupTransferId(ewm, transfer), - rawTransaction, - ++ewm->requestId); + ewm->client.funcSubmitTransaction (ewm->client.context, + ewm, + ewmLookupWalletId(ewm, wallet), + ewmLookupTransferId(ewm, transfer), + rawTransaction, + ++ewm->requestId); free(rawTransaction); break; @@ -589,13 +581,12 @@ ewmClientHandleBlockEvent(BREthereumEWM ewm, BREthereumBlockEvent event, BREthereumStatus status, const char *errorDescription) { - ewm->client.funcBlockEvent - (ewm->client.context, - ewm, - bid, - event, - status, - errorDescription); + ewm->client.funcBlockEvent (ewm->client.context, + ewm, + bid, + event, + status, + errorDescription); } extern void @@ -636,14 +627,13 @@ ewmClientHandleTransferEvent (BREthereumEWM ewm, ewm->client.funcChangeLog (ewm->client.context, ewm, type, persistData); } - ewm->client.funcTransferEvent - (ewm->client.context, - ewm, - wid, - tid, - event, - status, - errorDescription); + ewm->client.funcTransferEvent (ewm->client.context, + ewm, + wid, + tid, + event, + status, + errorDescription); } extern void @@ -653,14 +643,13 @@ ewmClientHandlePeerEvent(BREthereumEWM ewm, BREthereumPeerEvent event, BREthereumStatus status, const char *errorDescription) { - ewm->client.funcPeerEvent - (ewm->client.context, - ewm, - // event->wid, - // event->tid, - event, - status, - errorDescription); + ewm->client.funcPeerEvent (ewm->client.context, + ewm, + // event->wid, + // event->tid, + event, + status, + errorDescription); } extern void @@ -670,14 +659,13 @@ ewmClientHandleEWMEvent(BREthereumEWM ewm, BREthereumEWMEvent event, BREthereumStatus status, const char *errorDescription) { - ewm->client.funcEWMEvent - (ewm->client.context, - ewm, - //event->wid, - // event->tid, - event, - status, - errorDescription); + ewm->client.funcEWMEvent (ewm->client.context, + ewm, + //event->wid, + // event->tid, + event, + status, + errorDescription); } diff --git a/ethereum/ewm/BREthereumEWMEvent.c b/ethereum/ewm/BREthereumEWMEvent.c index b65e124a0..fe05b5a2e 100644 --- a/ethereum/ewm/BREthereumEWMEvent.c +++ b/ethereum/ewm/BREthereumEWMEvent.c @@ -293,7 +293,7 @@ ewmSignalSaveBlocks (BREthereumEWM ewm, // ============================================================================================== // -// Handle SavePeers +// Handle GetBlocks // typedef struct { BREvent base; @@ -302,7 +302,7 @@ typedef struct { } BREthereumHandleSaveNodesEvent; static void -ewmHandleSavePeersEventDispatcher(BREventHandler ignore, +ewmHandleSaveNodesEventDispatcher(BREventHandler ignore, BREthereumHandleSaveNodesEvent *event) { ewmHandleSaveNodes(event->ewm, event->nodes); } @@ -310,7 +310,7 @@ ewmHandleSavePeersEventDispatcher(BREventHandler ignore, BREventType handleSaveNodesEventType = { "EWM: Handle SaveNodes Event", sizeof (BREthereumHandleSaveNodesEvent), - (BREventDispatcher) ewmHandleSavePeersEventDispatcher + (BREventDispatcher) ewmHandleSaveNodesEventDispatcher }; extern void @@ -365,6 +365,40 @@ ewmSignalSync (BREthereumEWM ewm, eventHandlerSignalEvent(ewm->handlerForMain, (BREvent*) &event); } +// ============================================================================================== +// +// Handle GetBlocks +// +typedef struct { + BREvent base; + BREthereumEWM ewm; + BREthereumAddress address; + BREthereumSyncInterestSet interests; + uint64_t blockStart; + uint64_t blockStop; +} BREthereumHandleGetBlocksEvent; + +static void +ewmHandleGetBlocksEventDispatcher(BREventHandler ignore, + BREthereumHandleGetBlocksEvent *event) { + ewmHandleGetBlocks(event->ewm, event->address, event->interests, event->blockStart, event->blockStop); +} + +BREventType handleGetBlocksEventType = { + "EWM: Handle GetBlocks Event", + sizeof (BREthereumHandleGetBlocksEvent), + (BREventDispatcher) ewmHandleGetBlocksEventDispatcher +}; + +extern void +ewmSignalGetBlocks (BREthereumEWM ewm, + BREthereumAddress address, + BREthereumSyncInterestSet interests, + uint64_t blockStart, + uint64_t blockStop) { + BREthereumHandleGetBlocksEvent event = { { NULL, &handleGetBlocksEventType }, ewm, address, interests, blockStart, blockStop }; + eventHandlerSignalEvent(ewm->handlerForMain, (BREvent*) &event); +} // ============================================================================================== // @@ -380,6 +414,8 @@ const BREventType *handlerForMainEventTypes[] = { &handleLogEventType, &handleSaveBlocksEventType, &handleSaveNodesEventType, - &handleSyncEventType + &handleSyncEventType, + &handleGetBlocksEventType, + }; const unsigned int handlerForMainEventTypesCount = 10; diff --git a/ethereum/ewm/BREthereumEWMPrivate.h b/ethereum/ewm/BREthereumEWMPrivate.h index 259f6815b..70fe14cbd 100644 --- a/ethereum/ewm/BREthereumEWMPrivate.h +++ b/ethereum/ewm/BREthereumEWMPrivate.h @@ -69,11 +69,6 @@ struct BREthereumEWMRecord { */ BREthereumType type; - /** - * The SyncMode of this EWM - */ - BREthereumSyncMode syncMode; - /** * The network */ @@ -311,6 +306,22 @@ ewmSignalSync (BREthereumEWM ewm, uint64_t blockNumberCurrent, uint64_t blockNumberStop); +// +// Signal/Handle Get Blocks (BCS Callback) +// +extern void +ewmHandleGetBlocks (BREthereumEWM ewm, + BREthereumAddress address, + BREthereumSyncInterestSet interests, + uint64_t blockStart, + uint64_t blockStop); +extern void +ewmSignalGetBlocks (BREthereumEWM ewm, + BREthereumAddress address, + BREthereumSyncInterestSet interests, + uint64_t blockStart, + uint64_t blockStop); + /// /// MARK: - Handler For Main /// diff --git a/ethereum/ewm/testEwm.c b/ethereum/ewm/testEwm.c index df33001dd..c52ba03c6 100644 --- a/ethereum/ewm/testEwm.c +++ b/ethereum/ewm/testEwm.c @@ -203,6 +203,64 @@ clientGetLogs (BREthereumClientContext context, free (address); } +static void +clientGetBlocks (BREthereumClientContext context, + BREthereumEWM ewm, + const char *address, + int interests, + uint64_t blockNumberStart, + uint64_t blockNumberStop, + int rid) { + BRArrayOf(uint64_t) blockNumbers; + array_new (blockNumbers, 10); + + if (0 == strcasecmp (address, "0xb302B06FDB1348915599D21BD54A06832637E5E8")) { + if (syncInterestMatch(interests, CLIENT_GET_BLOCKS_LOGS_AS_TARGET)) { + array_add (blockNumbers, 4847049); + array_add (blockNumbers, 4847152); + array_add (blockNumbers, 4894677); + array_add (blockNumbers, 4965538); + array_add (blockNumbers, 4999850); + array_add (blockNumbers, 5029844); + // array_add (blockNumbers, 5705175); + } + + if (syncInterestMatch(interests, CLIENT_GET_BLOCKS_LOGS_AS_SOURCE)) { + array_add (blockNumbers, 5705175); + } + + if (syncInterestMatch(interests, CLIENT_GET_BLOCKS_TRANSACTIONS_AS_TARGET)) { + array_add (blockNumbers, 4894027); + array_add (blockNumbers, 4908682); + array_add (blockNumbers, 4991227); + } + + if (syncInterestMatch(interests, CLIENT_GET_BLOCKS_TRANSACTIONS_AS_SOURCE)) { + array_add (blockNumbers, 4894330); + array_add (blockNumbers, 4894641); + array_add (blockNumbers, 4894677); + + array_add (blockNumbers, 4903993); + array_add (blockNumbers, 4906377); + array_add (blockNumbers, 4997449); + + array_add (blockNumbers, 4999850); + array_add (blockNumbers, 4999875); + array_add (blockNumbers, 5000000); + + // TODO: When arrives at BCS - don't request unless the node is up-to-date! + array_add (blockNumbers, 5705175); + } + } + else { + array_add (blockNumbers, blockNumberStart); + array_add (blockNumbers, (blockNumberStart + blockNumberStop) / 2); + array_add (blockNumbers, blockNumberStop); + } + + ethereumClientAnnounceBlocks (ewm, rid, blockNumbers); +} + static void clientGetBlockNumber (BREthereumClientContext context, BREthereumEWM ewm, @@ -232,6 +290,48 @@ clientGetTokens (BREthereumClientContext context, NULL, NULL, 0); + + // For 0xb302B06FDB1348915599D21BD54A06832637E5E8 + ethereumClientAnnounceToken(ewm, + "0x68e14bb5a45b9681327e16e528084b9d962c1a39", + "CAT", + "CAT Token", + "", + 18, + NULL, + NULL, + 0); + + ethereumClientAnnounceToken(ewm, + "0x1234567461d3f8db7496581774bd869c83d51c93", + "bitclave", + "bitclave", + "", + 18, + NULL, + NULL, + 0); + + ethereumClientAnnounceToken(ewm, + "0xb3bd49e28f8f832b8d1e246106991e546c323502", + "GMT", + "GMT", + "", + 18, + NULL, + NULL, + 0); + + ethereumClientAnnounceToken(ewm, + "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0", + "EOS", + "EOS", + "", + 18, + NULL, + NULL, + 0); + } // @@ -421,6 +521,7 @@ static BREthereumClient client = { clientSubmitTransaction, clientGetTransactions, clientGetLogs, + clientGetBlocks, clientGetTokens, clientGetBlockNumber, clientGetNonce, @@ -668,7 +769,9 @@ runSyncTest (unsigned int durationInSeconds, client.context = (JsonRpcTestContext) calloc (1, sizeof (struct JsonRpcTestContextRecord)); - char *paperKey = "0x8975dbc1b8f25ec994815626d070899dda896511"; // "boring head harsh green empty clip fatal typical found crane dinner timber"; +// char *paperKey = "boring head harsh green empty clip fatal typical found crane dinner timber"; +// char *paperKey = "0x8975dbc1b8f25ec994815626d070899dda896511"; + char *paperKey = "0xb302B06FDB1348915599D21BD54A06832637E5E8"; alarmClockCreateIfNecessary (1); BRArrayOf(BREthereumPersistData) blocks = (restart ? savedBlocks : NULL); @@ -696,7 +799,7 @@ runSyncTest (unsigned int durationInSeconds, } } - BREthereumEWM ewm = ethereumCreate(ethereumMainnet, paperKey, EWM_USE_LES, SYNC_MODE_FULL_BLOCKCHAIN, client, + BREthereumEWM ewm = ethereumCreate(ethereumMainnet, paperKey, EWM_USE_LES, SYNC_MODE_PRIME_WITH_ENDPOINT, client, nodes, blocks, transactions, diff --git a/ethereum/les/BREthereumNodeEndpoint.c b/ethereum/les/BREthereumNodeEndpoint.c index 45ad422a2..022aacb66 100644 --- a/ethereum/les/BREthereumNodeEndpoint.c +++ b/ethereum/les/BREthereumNodeEndpoint.c @@ -532,7 +532,7 @@ const char *bootstrapLCLEnodes[] = { // Localhost - Parity // "enode://30af157f9105700422655d81d269575c0b1d63caeb90863f70cb2b92d89356ac4378cabaa4e2c84c9fdf1571264bcf22fd4bb6148f65f84c9e5f37dcc8a46a7f@127.0.0.1:30303", // HDD Archive // "enode://9058392a81f6f3df662cd2dc5e62d3529f08bc68a69025937a831bd6734e9830bffde87485989ebb4b5588771fc877e34b7be82251cf2d48ae2a1a784b6b58fc@127.0.0.1:30303", // SSD Full - "enode://4483ac6134c85ecbd31d14168f1c97b82bdc45c442e81277f52428968de41add46549f8d6c9c8c3432f3b8834b018c350ac37d87d70d67e599f42f68a96717fc@127.0.0.1:30303", // SSD Archive + "enode://4483ac6134c85ecbd31d14168f1c97b82bdc45c442e81277f52428968de41add46549f8d6c9c8c3432f3b8834b018c350ac37d87d70d67e599f42f68a96717fc@127.0.0.1:30304", // SSD Archive // "enode://8ebe6a85d46737451c8bd9423f37dcb117af7316bbce1643856feeaf9f81a792ff09029e9ab1796b193eb477f938af3465f911574c57161326b71aaf0221f341@127.0.0.1:30303", #endif