diff --git a/Swift/BRCore.xcodeproj/project.pbxproj b/Swift/BRCore.xcodeproj/project.pbxproj index e6540ce5c..b782ed131 100644 --- a/Swift/BRCore.xcodeproj/project.pbxproj +++ b/Swift/BRCore.xcodeproj/project.pbxproj @@ -1346,16 +1346,16 @@ 3C97E24422416AB1003FD88F /* BRCryptoAmount.c */, 3C97E254224170B9003FD88F /* BRCryptoAccount.h */, 3C97E255224170B9003FD88F /* BRCryptoAccount.c */, - CE179CA6233A4FF400633B97 /* BRCryptoCipher.c */, CE179CA5233A4FF400633B97 /* BRCryptoCipher.h */, - CE5E43C223390FBB001E9238 /* BRCryptoCoder.c */, + CE179CA6233A4FF400633B97 /* BRCryptoCipher.c */, CE5E43C323390FBB001E9238 /* BRCryptoCoder.h */, - CE5E43BE233902A4001E9238 /* BRCryptoHasher.c */, + CE5E43C223390FBB001E9238 /* BRCryptoCoder.c */, CE5E43BF233902A4001E9238 /* BRCryptoHasher.h */, + CE5E43BE233902A4001E9238 /* BRCryptoHasher.c */, 3C1D1F2322F0F1740028B20C /* BRCryptoKey.h */, 3C1D1F2422F0F1740028B20C /* BRCryptoKey.c */, - CE5E43C6233930F1001E9238 /* BRCryptoSigner.c */, CE5E43C7233930F1001E9238 /* BRCryptoSigner.h */, + CE5E43C6233930F1001E9238 /* BRCryptoSigner.c */, 3C97E258224170EC003FD88F /* BRCryptoAddress.h */, 3C97E259224170EC003FD88F /* BRCryptoAddress.c */, 3CCC61E9228C7FB500C0A13E /* BRCryptoFeeBasis.h */, diff --git a/crypto/BRCryptoFeeBasis.c b/crypto/BRCryptoFeeBasis.c index e387522bc..328afafd1 100644 --- a/crypto/BRCryptoFeeBasis.c +++ b/crypto/BRCryptoFeeBasis.c @@ -11,7 +11,7 @@ #include #include "BRCryptoFeeBasis.h" -#include "ethereum/ewm/BREthereumBase.h" +#include "ethereum/BREthereum.h" #include "generic/BRGeneric.h" #include "BRCryptoPrivate.h" diff --git a/crypto/BRCryptoTransfer.c b/crypto/BRCryptoTransfer.c index 207853f1e..b3d8bcc95 100644 --- a/crypto/BRCryptoTransfer.c +++ b/crypto/BRCryptoTransfer.c @@ -17,9 +17,7 @@ #include "support/BRAddress.h" #include "bitcoin/BRWallet.h" #include "bitcoin/BRTransaction.h" -#include "ethereum/util/BRUtil.h" #include "ethereum/BREthereum.h" -#include "ethereum/ewm/BREthereumTransfer.h" /** * @@ -46,6 +44,7 @@ struct BRCryptoTransferRecord { uint64_t recv; } btc; struct { + BREthereumEWM ewm; BREthereumTransfer tid; BREthereumAddress accountAddress; } eth; @@ -199,10 +198,11 @@ cryptoTransferCreateAsETH (BRCryptoUnit unit, BREthereumTransfer tid, BRCryptoFeeBasis feeBasisEstimated) { BRCryptoTransfer transfer = cryptoTransferCreateInternal (BLOCK_CHAIN_TYPE_ETH, unit, unitForFee); + transfer->u.eth.ewm = ewm; transfer->u.eth.tid = tid; - transfer->sourceAddress = cryptoAddressCreateAsETH (transferGetSourceAddress (tid)); - transfer->targetAddress = cryptoAddressCreateAsETH (transferGetTargetAddress (tid)); + transfer->sourceAddress = cryptoAddressCreateAsETH (ewmTransferGetSource (ewm, tid)); + transfer->targetAddress = cryptoAddressCreateAsETH (ewmTransferGetTarget (ewm, tid)); // cache the values that require the ewm BREthereumAccount account = ewmGetAccount (ewm); @@ -234,7 +234,7 @@ cryptoTransferCreateAsETH (BRCryptoUnit unit, // Thus: if `feeBasisEstimated` is NULL, we'll take the ETH fee basis (as the best we have). // Get the ETH feeBasis, in the event that we need it. - BREthereumFeeBasis ethFeeBasis = transferGetFeeBasis(tid); + BREthereumFeeBasis ethFeeBasis = ewmTransferGetFeeBasis (ewm, tid); transfer->feeBasisEstimated = (NULL == feeBasisEstimated ? cryptoFeeBasisCreateAsETH (unitForFee, @@ -328,7 +328,8 @@ cryptoTransferGetAmountAsSign (BRCryptoTransfer transfer, BRCryptoBoolean isNega } case BLOCK_CHAIN_TYPE_ETH: { - BREthereumAmount ethAmount = transferGetAmount(transfer->u.eth.tid); + BREthereumAmount ethAmount = ewmTransferGetAmount (transfer->u.eth.ewm, + transfer->u.eth.tid); switch (amountGetType(ethAmount)) { case AMOUNT_ETHER: amount = cryptoAmountCreate (transfer->unit, @@ -509,10 +510,11 @@ cryptoTransferGetDirection (BRCryptoTransfer transfer) { return CRYPTO_TRANSFER_RECEIVED; } case BLOCK_CHAIN_TYPE_ETH: { - BREthereumTransfer tid =transfer->u.eth.tid; + BREthereumEWM ewm = transfer->u.eth.ewm; + BREthereumTransfer tid = transfer->u.eth.tid; - BREthereumAddress source = transferGetSourceAddress (tid); - BREthereumAddress target = transferGetTargetAddress (tid); + BREthereumAddress source = ewmTransferGetSource (ewm, tid); + BREthereumAddress target = ewmTransferGetTarget (ewm, tid); BREthereumBoolean accountIsSource = addressEqual (source, transfer->u.eth.accountAddress); BREthereumBoolean accountIsTarget = addressEqual (target, transfer->u.eth.accountAddress); @@ -563,9 +565,10 @@ cryptoTransferGetHash (BRCryptoTransfer transfer) { : cryptoHashCreateAsBTC (hash)); } case BLOCK_CHAIN_TYPE_ETH: { - BREthereumTransfer tid =transfer->u.eth.tid; + BREthereumEWM ewm = transfer->u.eth.ewm; + BREthereumTransfer tid = transfer->u.eth.tid; - BREthereumHash hash = transferGetOriginatingTransactionHash (tid); + BREthereumHash hash = ewmTransferGetOriginatingTransactionHash (ewm, tid); return (ETHEREUM_BOOLEAN_TRUE == hashEqual(hash, hashCreateEmpty()) ? NULL : cryptoHashCreateAsETH (hash)); diff --git a/crypto/BRCryptoWallet.c b/crypto/BRCryptoWallet.c index 3e521dbec..a70c13059 100644 --- a/crypto/BRCryptoWallet.c +++ b/crypto/BRCryptoWallet.c @@ -15,12 +15,11 @@ #include "BRCryptoBase.h" #include "BRCryptoPrivate.h" -#include "generic/BRGeneric.h" #include "bitcoin/BRWallet.h" #include "bitcoin/BRWalletManager.h" #include "ethereum/BREthereum.h" -#include "ethereum/ewm/BREthereumTransfer.h" +#include "generic/BRGeneric.h" /** * diff --git a/crypto/BRCryptoWalletManagerClient.c b/crypto/BRCryptoWalletManagerClient.c index 83ef76479..1bb2875f0 100644 --- a/crypto/BRCryptoWalletManagerClient.c +++ b/crypto/BRCryptoWalletManagerClient.c @@ -20,7 +20,6 @@ #include "bitcoin/BRWalletManager.h" #include "ethereum/BREthereum.h" -#include "ethereum/ewm/BREthereumTransfer.h" #include "support/BRBase.h" #include "support/BRSyncMode.h" @@ -1280,7 +1279,7 @@ cwmTransactionEventAsETH (BREthereumClientContext context, BRCryptoTransferState oldState = cryptoTransferGetState (transfer); - BREthereumFeeBasis ethFeeBasis = transferGetFeeBasis (tid); + BREthereumFeeBasis ethFeeBasis = ewmTransferGetFeeBasis (ewm, tid); BRCryptoUnit unit = cryptoTransferGetUnitForFee(transfer); BRCryptoFeeBasis feeBasisConfirmed = cryptoFeeBasisCreateAsETH (unit, diff --git a/ethereum/base/BREthereumFeeBasis.c b/ethereum/base/BREthereumFeeBasis.c index fb21ac1b9..9fca4d76d 100644 --- a/ethereum/base/BREthereumFeeBasis.c +++ b/ethereum/base/BREthereumFeeBasis.c @@ -19,6 +19,16 @@ feeBasisCreate (BREthereumGas limit, }; } +extern BREthereumGas +feeBasisGetGasLimit (BREthereumFeeBasis basis) { + return (FEE_BASIS_GAS == basis.type ? basis.u.gas.limit : gasCreate(0)); +} + +extern BREthereumGasPrice +feeBasisGetGasPrice (BREthereumFeeBasis basis) { + return (FEE_BASIS_GAS == basis.type ? basis.u.gas.price : gasPriceCreate(etherCreateZero())); +} + extern BREthereumEther feeBasisGetFee (BREthereumFeeBasis feeBasis, int *overflow) { // BREthereumBoolean *overflow = 0; diff --git a/ethereum/base/BREthereumFeeBasis.h b/ethereum/base/BREthereumFeeBasis.h index 188159c7e..faa7064a2 100644 --- a/ethereum/base/BREthereumFeeBasis.h +++ b/ethereum/base/BREthereumFeeBasis.h @@ -32,6 +32,12 @@ extern BREthereumFeeBasis feeBasisCreate (BREthereumGas limit, BREthereumGasPrice price); +extern BREthereumGas +feeBasisGetGasLimit (BREthereumFeeBasis basis); + +extern BREthereumGasPrice +feeBasisGetGasPrice (BREthereumFeeBasis basis); + extern BREthereumEther feeBasisGetFee (BREthereumFeeBasis feeBasis, int *overflow); diff --git a/ethereum/ewm/BREthereumEWM.c b/ethereum/ewm/BREthereumEWM.c index 51046c9ad..79b354673 100644 --- a/ethereum/ewm/BREthereumEWM.c +++ b/ethereum/ewm/BREthereumEWM.c @@ -50,7 +50,7 @@ ewmPeriodicDispatcher (BREventHandler handler, /* Forward Implementation */ -/// MARK: - Transaction File Service +/// MARK: - File Service, Initial Load static BRSetOf(BREthereumTransaction) initialTransactionsLoad (BREthereumEWM ewm) { @@ -62,8 +62,6 @@ initialTransactionsLoad (BREthereumEWM ewm) { return transactions; } -/// MARK: - Log File Service - static BRSetOf(BREthereumLog) initialLogsLoad (BREthereumEWM ewm) { BRSetOf(BREthereumLog) logs = BRSetNew(logHashValue, logHashEqual, EWM_INITIAL_SET_SIZE_DEFAULT); @@ -115,7 +113,6 @@ initialWalletsLoad (BREthereumEWM ewm) { return states; } - /** * * @@ -618,7 +615,7 @@ extern BREthereumBoolean ewmConnect(BREthereumEWM ewm) { BREthereumBoolean result = ETHEREUM_BOOLEAN_FALSE; - ewmLock (ewm); + pthread_mutex_lock(&ewm->lock); BREthereumEWMState oldState = ewm->state; BREthereumEWMState newState = ewm->state; @@ -655,7 +652,7 @@ ewmConnect(BREthereumEWM ewm) { { .changed = { oldState, newState }} }); - ewmUnlock (ewm); + pthread_mutex_unlock (&ewm->lock); return result; } @@ -670,7 +667,7 @@ extern BREthereumBoolean ewmDisconnect (BREthereumEWM ewm) { BREthereumBoolean result = ETHEREUM_BOOLEAN_FALSE; - ewmLock (ewm); + pthread_mutex_lock(&ewm->lock); BREthereumEWMState oldState = ewm->state; BREthereumEWMState newState = ewm->state; @@ -727,7 +724,7 @@ ewmDisconnect (BREthereumEWM ewm) { { .changed = { oldState, newState }} }); - ewmUnlock(ewm); + pthread_mutex_unlock (&ewm->lock); return result; } @@ -736,7 +733,7 @@ extern BREthereumBoolean ewmIsConnected (BREthereumEWM ewm) { BREthereumBoolean result = ETHEREUM_BOOLEAN_FALSE; - ewmLock (ewm); + pthread_mutex_lock(&ewm->lock); if (EWM_STATE_CONNECTED == ewm->state || EWM_STATE_SYNCING == ewm->state) { switch (ewm->mode) { @@ -752,7 +749,7 @@ ewmIsConnected (BREthereumEWM ewm) { } } - ewmUnlock (ewm); + pthread_mutex_unlock (&ewm->lock); return result; } @@ -764,34 +761,33 @@ ewmAssertRecovery (BREthereumEWM ewm) { extern BREthereumNetwork ewmGetNetwork (BREthereumEWM ewm) { - return ewm->network; + return ewm->network; // constant } extern BREthereumAccount ewmGetAccount (BREthereumEWM ewm) { - return ewm->account; + return ewm->account; // constant } extern char * ewmGetAccountPrimaryAddress(BREthereumEWM ewm) { - return accountGetPrimaryAddressString(ewmGetAccount(ewm)); + return accountGetPrimaryAddressString(ewmGetAccount(ewm)); // constant } extern BRKey // key.pubKey ewmGetAccountPrimaryAddressPublicKey(BREthereumEWM ewm) { - return accountGetPrimaryAddressPublicKey(ewmGetAccount(ewm)); + return accountGetPrimaryAddressPublicKey(ewmGetAccount(ewm)); // constant } extern BRKey ewmGetAccountPrimaryAddressPrivateKey(BREthereumEWM ewm, const char *paperKey) { - return accountGetPrimaryAddressPrivateKey (ewmGetAccount(ewm), paperKey); + return accountGetPrimaryAddressPrivateKey (ewmGetAccount(ewm), paperKey); // constant } -/// -/// Sync -/// +/// MARK: - Sync + typedef struct { BREthereumEWM ewm; uint64_t begBlockNumber; @@ -993,15 +989,7 @@ ewmSyncToDepth (BREthereumEWM ewm, } } -extern void -ewmLock (BREthereumEWM ewm) { - pthread_mutex_lock (&ewm->lock); -} - -extern void -ewmUnlock (BREthereumEWM ewm) { - pthread_mutex_unlock (&ewm->lock); -} +/// MARK: - Mode extern BRSyncMode ewmGetMode (BREthereumEWM ewm) { @@ -1108,66 +1096,12 @@ ewmUpdateMode (BREthereumEWM ewm, /// MARK: - Blocks -#if defined (NEVER_DEFINED) -extern BREthereumBlock -ewmLookupBlockByHash(BREthereumEWM ewm, - const BREthereumHash hash) { - BREthereumBlock block = NULL; - - pthread_mutex_lock(&ewm->lock); - for (int i = 0; i < array_count(ewm->blocks); i++) - if (ETHEREUM_COMPARISON_EQ == hashCompare(hash, blockGetHash(ewm->blocks[i]))) { - block = ewm->blocks[i]; - break; - } - pthread_mutex_unlock(&ewm->lock); - return block; -} - -extern BREthereumBlock -ewmLookupBlock(BREthereumEWM ewm, - BREthereumBlockId bid) { - BREthereumBlock block = NULL; - - pthread_mutex_lock(&ewm->lock); - block = (0 <= bid && bid < array_count(ewm->blocks) - ? ewm->blocks[bid] - : NULL); - pthread_mutex_unlock(&ewm->lock); - return block; -} - -extern BREthereumBlockId -ewmLookupBlockId (BREthereumEWM ewm, - BREthereumBlock block) { - BREthereumBlockId bid = -1; - - pthread_mutex_lock(&ewm->lock); - for (int i = 0; i < array_count(ewm->blocks); i++) - if (block == ewm->blocks[i]) { - bid = i; - break; - } - pthread_mutex_unlock(&ewm->lock); - return bid; -} - -extern BREthereumBlockId -ewmInsertBlock (BREthereumEWM ewm, - BREthereumBlock block) { - BREthereumBlockId bid = -1; - pthread_mutex_lock(&ewm->lock); - array_add(ewm->blocks, block); - bid = (BREthereumBlockId) (array_count(ewm->blocks) - 1); - pthread_mutex_unlock(&ewm->lock); - ewmSignalBlockEvent(ewm, bid, BLOCK_EVENT_CREATED, SUCCESS, NULL); - return bid; -} -#endif - extern uint64_t ewmGetBlockHeight(BREthereumEWM ewm) { - return ewm->blockHeight; + pthread_mutex_lock(&ewm->lock); + uint64_t height = ewm->blockHeight; + pthread_mutex_unlock(&ewm->lock); + return height; } extern void @@ -1186,129 +1120,8 @@ ewmUpdateBlockHeight(BREthereumEWM ewm, } /// MARK: - Transfers - -#if defined (NEVER_DEFINED) -extern BREthereumTransfer -ewmLookupTransfer (BREthereumEWM ewm, - BREthereumTransfer transfer) { - BREthereumTransfer transfer = NULL; - - pthread_mutex_lock(&ewm->lock); - transfer = (0 <= tid && tid < array_count(ewm->transfers) - ? ewm->transfers[tid] - : NULL); - pthread_mutex_unlock(&ewm->lock); - return transfer; -} - -extern BREthereumTransfer -ewmLookupTransferByHash (BREthereumEWM ewm, - const BREthereumHash hash) { - BREthereumTransfer transfer = NULL; - - pthread_mutex_lock(&ewm->lock); - for (int i = 0; i < array_count(ewm->transfers); i++) - if (ETHEREUM_COMPARISON_EQ == hashCompare(hash, transferGetHash(ewm->transfers[i]))) { - transfer = ewm->transfers[i]; - break; - } - pthread_mutex_unlock(&ewm->lock); - return transfer; -} - -extern BREthereumTransferId -ewmLookupTransferId (BREthereumEWM ewm, - BREthereumTransfer transfer) { - BREthereumTransfer transfer = -1; - - pthread_mutex_lock(&ewm->lock); - for (int i = 0; i < array_count(ewm->transfers); i++) - if (transfer == ewm->transfers[i]) { - tid = i; - break; - } - pthread_mutex_unlock(&ewm->lock); - return tid; -} - -extern BREthereumTransferId -ewmInsertTransfer (BREthereumEWM ewm, - BREthereumTransfer transfer) { - BREthereumTransfer transfer; - - pthread_mutex_lock(&ewm->lock); - array_add (ewm->transfers, transfer); - tid = (BREthereumTransferId) (array_count(ewm->transfers) - 1); - pthread_mutex_unlock(&ewm->lock); - - return tid; -} - -extern void -ewmDeleteTransfer (BREthereumEWM ewm, - BREthereumTransfer transfer) { - BREthereumTransfer transfer = ewm->transfers[tid]; - if (NULL == transfer) return; - - // Remove from any (and all - should be but one) wallet - for (int wid = 0; wid < array_count(ewm->wallets); wid++) - if (walletHasTransfer(ewm->wallets[wid], transfer)) { - walletUnhandleTransfer(ewm->wallets[wid], transfer); - ewmSignalTransferEvent(ewm, wid, tid, TRANSFER_EVENT_DELETED, SUCCESS, NULL); - } - - // Null the ewm's `tid` - MUST NOT array_rm() as all `tid` holders will be dead. - ewm->transfers[tid] = NULL; - transferRelease(transfer); -} -#endif - /// MARK: - Wallets -#if defined (NEVER_DEFINED) -extern BREthereumWallet -ewmLookupWallet(BREthereumEWM ewm, - BREthereumWalletId wid) { - BREthereumWallet wallet = NULL; - - pthread_mutex_lock(&ewm->lock); - wallet = (0 <= wid && wid < array_count(ewm->wallets) - ? ewm->wallets[wid] - : NULL); - pthread_mutex_unlock(&ewm->lock); - return wallet; -} - -extern BREthereumWalletId -ewmLookupWalletId(BREthereumEWM ewm, - BREthereumWallet wallet) { - BREthereumWalletId wid = -1; - - pthread_mutex_lock(&ewm->lock); - for (int i = 0; i < array_count (ewm->wallets); i++) - if (wallet == ewm->wallets[i]) { - wid = i; - break; - } - pthread_mutex_unlock(&ewm->lock); - return wid; -} - -extern BREthereumWallet -ewmLookupWalletByTransfer (BREthereumEWM ewm, - BREthereumTransfer transfer) { - BREthereumWallet wallet = NULL; - pthread_mutex_lock(&ewm->lock); - for (int i = 0; i < array_count (ewm->wallets); i++) - if (walletHasTransfer(ewm->wallets[i], transfer)) { - wallet = ewm->wallets[i]; - break; - } - pthread_mutex_unlock(&ewm->lock); - return wallet; -} -#endif - extern void ewmInsertWallet (BREthereumEWM ewm, BREthereumWallet wallet) { @@ -1321,9 +1134,6 @@ ewmInsertWallet (BREthereumEWM ewm, pthread_mutex_unlock(&ewm->lock); } -// -// Wallet (Actions) -// extern BREthereumWallet * ewmGetWallets (BREthereumEWM ewm) { pthread_mutex_lock(&ewm->lock); @@ -1340,14 +1150,17 @@ ewmGetWallets (BREthereumEWM ewm) { return wallets; } -extern unsigned int +extern size_t ewmGetWalletsCount (BREthereumEWM ewm) { - return (unsigned int) array_count(ewm->wallets); + pthread_mutex_lock(&ewm->lock); + size_t count = array_count(ewm->wallets); + pthread_mutex_unlock(&ewm->lock); + return count; } extern BREthereumWallet ewmGetWallet(BREthereumEWM ewm) { - return ewm->walletHoldingEther; + return ewm->walletHoldingEther; // constant } extern BREthereumWallet @@ -1381,9 +1194,7 @@ ewmWalletCreateTransfer(BREthereumEWM ewm, BREthereumTransfer transfer = NULL; pthread_mutex_lock(&ewm->lock); - transfer = walletCreateTransfer(wallet, addressCreate(recvAddress), amount); - pthread_mutex_unlock(&ewm->lock); // Transfer DOES NOT have a hash yet because it is not signed; but it is inserted in the @@ -1407,14 +1218,12 @@ ewmWalletCreateTransferGeneric(BREthereumEWM ewm, BREthereumTransfer transfer = NULL; pthread_mutex_lock(&ewm->lock); - transfer = walletCreateTransferGeneric(wallet, addressCreate(recvAddress), amount, gasPrice, gasLimit, data); - pthread_mutex_unlock(&ewm->lock); // Transfer DOES NOT have a hash yet because it is not signed; but it is inserted in the @@ -1436,9 +1245,7 @@ ewmWalletCreateTransferWithFeeBasis (BREthereumEWM ewm, BREthereumTransfer transfer = NULL; pthread_mutex_lock(&ewm->lock); - { - transfer = walletCreateTransferWithFeeBasis (wallet, addressCreate(recvAddress), amount, feeBasis); - } + transfer = walletCreateTransferWithFeeBasis (wallet, addressCreate(recvAddress), amount, feeBasis); pthread_mutex_unlock(&ewm->lock); // Transfer DOES NOT have a hash yet because it is not signed; but it is inserted in the @@ -1456,7 +1263,10 @@ ewmWalletEstimateTransferFee(BREthereumEWM ewm, BREthereumWallet wallet, BREthereumAmount amount, int *overflow) { - return walletEstimateTransferFee(wallet, amount, overflow); + pthread_mutex_lock(&ewm->lock); + BREthereumEther fee = walletEstimateTransferFee(wallet, amount, overflow); + pthread_mutex_unlock(&ewm->lock); + return fee; } extern BREthereumEther @@ -1466,7 +1276,10 @@ ewmWalletEstimateTransferFeeForBasis(BREthereumEWM ewm, BREthereumGasPrice price, BREthereumGas gas, int *overflow) { - return walletEstimateTransferFeeDetailed (wallet, amount, price, gas, overflow); + pthread_mutex_lock(&ewm->lock); + BREthereumEther fee = walletEstimateTransferFeeDetailed (wallet, amount, price, gas, overflow); + pthread_mutex_unlock(&ewm->lock); + return fee; } extern void @@ -1497,7 +1310,9 @@ extern BREthereumBoolean ewmWalletCanCancelTransfer (BREthereumEWM ewm, BREthereumWallet wallet, BREthereumTransfer oldTransfer) { + pthread_mutex_lock(&ewm->lock); BREthereumTransaction oldTransaction = transferGetOriginatingTransaction(oldTransfer); + pthread_mutex_unlock(&ewm->lock); // TODO: Something about the 'status' (not already cancelled, etc) return AS_ETHEREUM_BOOLEAN (NULL != oldTransaction); @@ -1507,6 +1322,7 @@ extern BREthereumTransfer // status, error ewmWalletCreateTransferToCancel(BREthereumEWM ewm, BREthereumWallet wallet, BREthereumTransfer oldTransfer) { + pthread_mutex_lock(&ewm->lock); BREthereumTransaction oldTransaction = transferGetOriginatingTransaction(oldTransfer); assert (NULL != oldTransaction); @@ -1534,6 +1350,8 @@ ewmWalletCreateTransferToCancel(BREthereumEWM ewm, ? TRANSFER_BASIS_TRANSACTION : TRANSFER_BASIS_LOG)); walletHandleTransfer(wallet, transfer); + pthread_mutex_unlock(&ewm->lock); + return transfer; } @@ -1541,7 +1359,9 @@ extern BREthereumBoolean ewmWalletCanReplaceTransfer (BREthereumEWM ewm, BREthereumWallet wid, BREthereumTransfer oldTransfer) { + pthread_mutex_lock(&ewm->lock); BREthereumTransaction oldTransaction = transferGetOriginatingTransaction(oldTransfer); + pthread_mutex_unlock(&ewm->lock); // TODO: Something about the 'status' (not already replaced, etc) return AS_ETHEREUM_BOOLEAN (NULL != oldTransaction); @@ -1555,6 +1375,7 @@ ewmWalletCreateTransferToReplace (BREthereumEWM ewm, BREthereumBoolean updateGasPrice, BREthereumBoolean updateGasLimit, BREthereumBoolean updateNonce) { + pthread_mutex_lock(&ewm->lock); BREthereumTransaction oldTransaction = transferGetOriginatingTransaction(oldTransfer); assert (NULL != oldTransaction); @@ -1602,6 +1423,7 @@ ewmWalletCreateTransferToReplace (BREthereumEWM ewm, ? TRANSFER_BASIS_TRANSACTION : TRANSFER_BASIS_LOG)); walletHandleTransfer(wallet, transfer); + pthread_mutex_unlock(&ewm->lock); return transfer; } @@ -1669,13 +1491,16 @@ ewmWalletGetTransferCount(BREthereumEWM ewm, extern BREthereumToken ewmWalletGetToken (BREthereumEWM ewm, BREthereumWallet wallet) { - return walletGetToken(wallet); + return walletGetToken(wallet); // constant } extern BREthereumAmount ewmWalletGetBalance(BREthereumEWM ewm, BREthereumWallet wallet) { - return walletGetBalance(wallet); + pthread_mutex_lock(&ewm->lock); + BREthereumAmount balance = walletGetBalance(wallet); + pthread_mutex_unlock(&ewm->lock); + return balance; } @@ -1683,21 +1508,30 @@ extern BREthereumGas ewmWalletGetGasEstimate(BREthereumEWM ewm, BREthereumWallet wallet, BREthereumTransfer transfer) { - return transferGetGasEstimate(transfer); + pthread_mutex_lock(&ewm->lock); + BREthereumGas gas = transferGetGasEstimate(transfer); + pthread_mutex_unlock(&ewm->lock); + return gas; } extern BREthereumGas ewmWalletGetDefaultGasLimit(BREthereumEWM ewm, BREthereumWallet wallet) { - return walletGetDefaultGasLimit(wallet); + pthread_mutex_lock(&ewm->lock); + BREthereumGas gas = walletGetDefaultGasLimit(wallet); + pthread_mutex_unlock(&ewm->lock); + return gas; } extern void ewmWalletSetDefaultGasLimit(BREthereumEWM ewm, BREthereumWallet wallet, BREthereumGas gasLimit) { + pthread_mutex_lock(&ewm->lock); walletSetDefaultGasLimit(wallet, gasLimit); + pthread_mutex_unlock(&ewm->lock); + ewmSignalWalletEvent(ewm, wallet, (BREthereumWalletEvent) { @@ -1709,14 +1543,20 @@ ewmWalletSetDefaultGasLimit(BREthereumEWM ewm, extern BREthereumGasPrice ewmWalletGetDefaultGasPrice(BREthereumEWM ewm, BREthereumWallet wallet) { - return walletGetDefaultGasPrice(wallet); + pthread_mutex_lock(&ewm->lock); + BREthereumGasPrice price = walletGetDefaultGasPrice(wallet); + pthread_mutex_unlock(&ewm->lock); + return price; } extern void ewmWalletSetDefaultGasPrice(BREthereumEWM ewm, BREthereumWallet wallet, BREthereumGasPrice gasPrice) { + pthread_mutex_lock(&ewm->lock); walletSetDefaultGasPrice(wallet, gasPrice); + pthread_mutex_unlock(&ewm->lock); + ewmSignalWalletEvent(ewm, wallet, (BREthereumWalletEvent) { @@ -1739,8 +1579,6 @@ extern void ewmHandleGasPrice (BREthereumEWM ewm, BREthereumWallet wallet, BREthereumGasPrice gasPrice) { - pthread_mutex_lock(&ewm->lock); - walletSetDefaultGasPrice(wallet, gasPrice); ewmSignalWalletEvent(ewm, @@ -1749,11 +1587,9 @@ ewmHandleGasPrice (BREthereumEWM ewm, WALLET_EVENT_DEFAULT_GAS_PRICE_UPDATED, SUCCESS }); - - pthread_mutex_unlock(&ewm->lock); } - +#if defined (NEVER_DEFINED) /** * Handle a `gasEstimate` for `transaction` in `wallet` * @@ -1767,8 +1603,6 @@ ewmHandleGasEstimate (BREthereumEWM ewm, BREthereumWallet wallet, BREthereumTransfer transfer, BREthereumGas gasEstimate) { - pthread_mutex_lock(&ewm->lock); - transferSetGasEstimate(transfer, gasEstimate); ewmSignalTransferEvent(ewm, @@ -1778,11 +1612,8 @@ ewmHandleGasEstimate (BREthereumEWM ewm, TRANSFER_EVENT_GAS_ESTIMATE_UPDATED, SUCCESS }); - - pthread_mutex_unlock(&ewm->lock); - } - +#endif // ============================================================================================== // // LES(BCS)/BRD Handlers @@ -1834,20 +1665,14 @@ ewmHandleBlockChain (BREthereumEWM ewm, extern void ewmHandleAccountState (BREthereumEWM ewm, BREthereumAccountState accountState) { - pthread_mutex_lock(&ewm->lock); - eth_log("EWM", "AccountState: Nonce: %" PRIu64, accountState.nonce); - ewmHandleAnnounceNonce (ewm, accountGetPrimaryAddress(ewm->account), accountState.nonce, 0); ewmSignalBalance(ewm, amountCreateEther(accountState.balance)); - pthread_mutex_unlock(&ewm->lock); } extern void ewmHandleBalance (BREthereumEWM ewm, BREthereumAmount amount) { - pthread_mutex_lock(&ewm->lock); - BREthereumWallet wallet = (AMOUNT_ETHER == amountGetType(amount) ? ewmGetWallet(ewm) : ewmGetWalletHoldingToken(ewm, amountGetToken (amount))); @@ -1873,7 +1698,6 @@ ewmHandleBalance (BREthereumEWM ewm, free (amountAsString); } } - pthread_mutex_unlock(&ewm->lock); } static int @@ -2320,7 +2144,7 @@ ewmHandleGetBlocks (BREthereumEWM ewm, // // Periodic Dispatcher // -extern void +static void ewmUpdateWalletBalance(BREthereumEWM ewm, BREthereumWallet wallet) { @@ -2615,11 +2439,13 @@ ewmTransferFillRawData (BREthereumEWM ewm, uint8_t **bytesPtr, size_t *bytesCountPtr) { assert (NULL != bytesCountPtr && NULL != bytesPtr); + pthread_mutex_lock (&ewm->lock); assert (walletHasTransfer(wallet, transfer)); BREthereumTransaction transaction = transferGetOriginatingTransaction (transfer); assert (NULL != transaction); assert (ETHEREUM_BOOLEAN_IS_TRUE (transactionIsSigned(transaction))); + pthread_mutex_unlock (&ewm->lock); BRRlpItem item = transactionRlpEncode(transaction, ewm->network, @@ -2641,9 +2467,11 @@ ewmTransferGetRawDataHexEncoded(BREthereumEWM ewm, BREthereumTransfer transfer, const char *prefix) { assert (walletHasTransfer(wallet, transfer)); - + + pthread_mutex_lock (&ewm->lock); BREthereumTransaction transaction = transferGetOriginatingTransaction (transfer); - + pthread_mutex_unlock (&ewm->lock); + return (NULL == transaction ? NULL : transactionGetRlpHexEncoded (transaction, ewm->network, @@ -2651,7 +2479,7 @@ ewmTransferGetRawDataHexEncoded(BREthereumEWM ewm, ? RLP_TYPE_TRANSACTION_SIGNED : RLP_TYPE_TRANSACTION_UNSIGNED), prefix)); - } +} /// MARK: - Transfer @@ -2670,13 +2498,19 @@ ewmTransferGetSource (BREthereumEWM ewm, extern BREthereumHash ewmTransferGetIdentifier(BREthereumEWM ewm, BREthereumTransfer transfer) { - return transferGetIdentifier (transfer); + pthread_mutex_lock (&ewm->lock); + BREthereumHash identifier = transferGetIdentifier (transfer); + pthread_mutex_unlock (&ewm->lock); + return identifier; } extern BREthereumHash ewmTransferGetOriginatingTransactionHash(BREthereumEWM ewm, BREthereumTransfer transfer) { - return transferGetOriginatingTransactionHash(transfer); + pthread_mutex_lock (&ewm->lock); + BREthereumHash hash = transferGetOriginatingTransactionHash(transfer); + pthread_mutex_unlock (&ewm->lock); + return hash; } extern char * @@ -2718,10 +2552,19 @@ ewmTransferGetGasLimit(BREthereumEWM ewm, return feeBasisGetGasLimit(transferGetFeeBasis(transfer)); } +extern BREthereumFeeBasis +ewmTransferGetFeeBasis (BREthereumEWM ewm, + BREthereumTransfer transfer) { + return transferGetFeeBasis (transfer); +} + extern uint64_t ewmTransferGetNonce(BREthereumEWM ewm, BREthereumTransfer transfer) { - return transferGetNonce(transfer); + pthread_mutex_lock (&ewm->lock); + uint64_t nonce = transferGetNonce(transfer); + pthread_mutex_unlock (&ewm->lock); + return nonce; } extern BREthereumBoolean @@ -2732,12 +2575,16 @@ ewmTransferExtractStatusIncluded (BREthereumEWM ewm, uint64_t *blockTransactionIndex, uint64_t *blockTimestamp, BREthereumGas *gasUsed) { - return AS_ETHEREUM_BOOLEAN (transferExtractStatusIncluded (transfer, - blockHash, - blockNumber, - blockTransactionIndex, - blockTimestamp, - gasUsed)); + pthread_mutex_lock (&ewm->lock); + int included = transferExtractStatusIncluded (transfer, + blockHash, + blockNumber, + blockTransactionIndex, + blockTimestamp, + gasUsed); + pthread_mutex_unlock (&ewm->lock); + + return AS_ETHEREUM_BOOLEAN (included); } extern BREthereumHash @@ -2810,31 +2657,35 @@ ewmTransferIsConfirmed(BREthereumEWM ewm, extern BREthereumBoolean ewmTransferIsSubmitted(BREthereumEWM ewm, BREthereumTransfer transfer) { - return AS_ETHEREUM_BOOLEAN(ETHEREUM_BOOLEAN_IS_TRUE(transferHasStatus(transfer, TRANSFER_STATUS_SUBMITTED)) || - ETHEREUM_BOOLEAN_IS_TRUE(transferHasStatusOrTwo(transfer, - TRANSFER_STATUS_INCLUDED, - TRANSFER_STATUS_ERRORED))); + return AS_ETHEREUM_BOOLEAN (ETHEREUM_BOOLEAN_IS_TRUE (transferHasStatus (transfer, TRANSFER_STATUS_SUBMITTED)) || + ETHEREUM_BOOLEAN_IS_TRUE (transferHasStatusOrTwo (transfer, + TRANSFER_STATUS_INCLUDED, + TRANSFER_STATUS_ERRORED))); } extern char * ewmTransferStatusGetError (BREthereumEWM ewm, BREthereumTransfer transfer) { - if (TRANSFER_STATUS_ERRORED == transferGetStatus(transfer)) { - char *reason; + char *reason = NULL; + + pthread_mutex_lock (&ewm->lock); + if (TRANSFER_STATUS_ERRORED == transferGetStatus(transfer)) transferExtractStatusError (transfer, &reason); - return reason; - } - else return NULL; + pthread_mutex_unlock (&ewm->lock); + + return reason; } extern int ewmTransferStatusGetErrorType (BREthereumEWM ewm, BREthereumTransfer transfer) { - BREthereumTransactionErrorType type; + BREthereumTransactionErrorType type = (BREthereumTransactionErrorType) -1; + + pthread_mutex_lock (&ewm->lock); + transferExtractStatusErrorType (transfer, &type); + pthread_mutex_unlock (&ewm->lock); - return (transferExtractStatusErrorType (transfer, &type) - ? type - : (int ) -1); + return type; } extern BREthereumBoolean @@ -2859,7 +2710,12 @@ ewmTransferGetFee(BREthereumEWM ewm, BREthereumTransfer transfer, int *overflow) { assert (NULL != transfer); - return transferGetFee(transfer, overflow); + + pthread_mutex_lock (&ewm->lock); + BREthereumEther fee = transferGetFee(transfer, overflow); + pthread_mutex_unlock (&ewm->lock); + + return fee; } /// MARK: - Amount @@ -2921,6 +2777,7 @@ ewmTransferDelete (BREthereumEWM ewm, if (NULL == transfer) return; // Remove from any (and all - should be but one) wallet + pthread_mutex_lock (&ewm->lock); for (int wid = 0; wid < array_count(ewm->wallets); wid++) { BREthereumWallet wallet = ewm->wallets[wid]; if (walletHasTransfer(wallet, transfer)) { @@ -2933,6 +2790,7 @@ ewmTransferDelete (BREthereumEWM ewm, } // Null the ewm's `tid` - MUST NOT array_rm() as all `tid` holders will be dead. transferRelease(transfer); + pthread_mutex_unlock (&ewm->lock); } extern BREthereumToken diff --git a/ethereum/ewm/BREthereumEWM.h b/ethereum/ewm/BREthereumEWM.h index 92f357e47..fc6cdeb5d 100644 --- a/ethereum/ewm/BREthereumEWM.h +++ b/ethereum/ewm/BREthereumEWM.h @@ -121,12 +121,6 @@ ewmSyncToDepth (BREthereumEWM ewm, BREthereumBoolean pendExistingTransfers, BRSyncDepth depth); -extern void -ewmLock (BREthereumEWM ewm); - -extern void -ewmUnlock (BREthereumEWM ewm); - extern BRSyncMode ewmGetMode (BREthereumEWM ewm); @@ -137,16 +131,12 @@ ewmUpdateMode (BREthereumEWM ewm, extern uint64_t ewmGetBlockHeight (BREthereumEWM ewm); -extern void -ewmUpdateBlockHeight(BREthereumEWM ewm, - uint64_t blockHeight); - /// MARK: - Wallets extern BREthereumWallet * ewmGetWallets (BREthereumEWM ewm); -extern unsigned int +extern size_t ewmGetWalletsCount (BREthereumEWM ewm); extern BREthereumWallet @@ -166,10 +156,6 @@ extern BREthereumAmount ewmWalletGetBalance(BREthereumEWM ewm, BREthereumWallet wallet); -extern void -ewmUpdateWalletBalance(BREthereumEWM ewm, - BREthereumWallet wallet); - extern BREthereumGas ewmWalletGetGasEstimate(BREthereumEWM ewm, BREthereumWallet wallet, @@ -335,6 +321,10 @@ extern BREthereumGas ewmTransferGetGasLimit(BREthereumEWM ewm, BREthereumTransfer transfer); +extern BREthereumFeeBasis +ewmTransferGetFeeBasis (BREthereumEWM ewm, + BREthereumTransfer transfer); + extern uint64_t ewmTransferGetNonce(BREthereumEWM ewm, BREthereumTransfer transfer); @@ -490,10 +480,6 @@ ewmCreateToken (BREthereumEWM ewm, extern uint64_t ewmGetBlockHeight(BREthereumEWM ewm); -extern void -ewmUpdateBlockHeight(BREthereumEWM ewm, - uint64_t blockHeight); - extern const char * ewmTransferGetRawDataHexEncoded(BREthereumEWM ewm, BREthereumWallet wallet, diff --git a/ethereum/ewm/BREthereumEWMClient.c b/ethereum/ewm/BREthereumEWMClient.c index 5ccbc3982..108e3cc15 100644 --- a/ethereum/ewm/BREthereumEWMClient.c +++ b/ethereum/ewm/BREthereumEWMClient.c @@ -64,10 +64,9 @@ ewmHandleAnnounceBalance (BREthereumEWM ewm, BREthereumWallet wallet, UInt256 value, int rid) { - BREthereumAmount amount = - (AMOUNT_ETHER == walletGetAmountType(wallet) - ? amountCreateEther(etherCreate(value)) - : amountCreateToken(createTokenQuantity(walletGetToken(wallet), value))); + BREthereumAmount amount = (AMOUNT_ETHER == walletGetAmountType(wallet) + ? amountCreateEther(etherCreate(value)) + : amountCreateToken(createTokenQuantity(walletGetToken(wallet), value))); ewmSignalBalance(ewm, amount); } @@ -136,10 +135,12 @@ ewmUpdateGasPrice (BREthereumEWM ewm, switch (ewm->mode) { case SYNC_MODE_BRD_ONLY: case SYNC_MODE_BRD_WITH_P2P_SEND: { + pthread_mutex_lock (&ewm->lock); ewm->client.funcGetGasPrice (ewm->client.context, ewm, wallet, ++ewm->requestId); + pthread_mutex_unlock (&ewm->lock); break; } @@ -194,6 +195,8 @@ ewmGetGasEstimate (BREthereumEWM ewm, switch (ewm->mode) { case SYNC_MODE_BRD_ONLY: case SYNC_MODE_BRD_WITH_P2P_SEND: { + pthread_mutex_lock (&ewm->lock); + // This will be ZERO if transaction amount is in TOKEN. BREthereumEther amountInEther = transferGetEffectiveAmountInEther(transfer); BREthereumFeeBasis feeBasis = transferGetFeeBasis (transfer); @@ -216,6 +219,7 @@ ewmGetGasEstimate (BREthereumEWM ewm, price, data, ++ewm->requestId); + pthread_mutex_unlock (&ewm->lock); free (from); free (to); @@ -310,8 +314,8 @@ ewmHandlGasEstimateFailure (BREthereumEWM ewm, */ extern void ewmHandleAnnounceBlockNumber (BREthereumEWM ewm, - uint64_t blockNumber, - int rid) { + uint64_t blockNumber, + int rid) { ewmUpdateBlockHeight(ewm, blockNumber); } @@ -353,6 +357,7 @@ ewmHandleAnnounceNonce (BREthereumEWM ewm, BREthereumAddress address, uint64_t newNonce, int rid) { + pthread_mutex_lock (&ewm->lock); uint64_t oldNonce = accountGetAddressNonce (ewm->account, address); if (oldNonce != newNonce) { // This may not change the nonce @@ -361,6 +366,7 @@ ewmHandleAnnounceNonce (BREthereumEWM ewm, if (oldNonce != accountGetAddressNonce (ewm->account, address)) ewmHandleSaveWallet (ewm, ewmGetWallet(ewm), CLIENT_CHANGE_UPD); } + pthread_mutex_unlock (&ewm->lock); } // ============================================================================================== @@ -606,10 +612,10 @@ ewmAnnounceLogComplete (BREthereumEWM ewm, extern BREthereumStatus ewmAnnounceBlocks (BREthereumEWM ewm, - int id, - // const char *strBlockHash, - int blockNumbersCount, - uint64_t *blockNumbers) { // BRArrayOf(const char *) strBlockNumbers ?? + int id, + // const char *strBlockHash, + int blockNumbersCount, + uint64_t *blockNumbers) { assert (SYNC_MODE_P2P_ONLY == ewm->mode || SYNC_MODE_P2P_WITH_BRD_SYNC == ewm->mode); // into bcs... @@ -621,7 +627,6 @@ ewmAnnounceBlocks (BREthereumEWM ewm, return SUCCESS; } - // ============================================================================================== // // Submit Transaction @@ -643,6 +648,7 @@ ewmWalletSubmitTransfer(BREthereumEWM ewm, // assert: wallet-has-transfer // assert: signed // assert: originatingTransaction + pthread_mutex_lock (&ewm->lock); BREthereumTransaction transaction = transferGetOriginatingTransaction(transfer); BREthereumBoolean isSigned = transactionIsSigned (transaction); @@ -678,6 +684,7 @@ ewmWalletSubmitTransfer(BREthereumEWM ewm, bcsSendTransaction(ewm->bcs, transaction); break; } + pthread_mutex_unlock (&ewm->lock); } extern void @@ -735,7 +742,7 @@ ewmAnnounceSubmitTransfer (BREthereumEWM ewm, BREthereumHash hash = hashCreate(strHash); // We announce a submitted transfer => there is an originating transaction. if (ETHEREUM_BOOLEAN_IS_TRUE (hashEqual (hash, EMPTY_HASH_INIT)) - || ETHEREUM_BOOLEAN_IS_FALSE (hashEqual (hash, transferGetOriginatingTransactionHash (transfer)))) + || ETHEREUM_BOOLEAN_IS_FALSE (hashEqual (hash, ewmTransferGetOriginatingTransactionHash (ewm, transfer)))) return ERROR_TRANSACTION_HASH_MISMATCH; } @@ -766,14 +773,14 @@ ewmUpdateTokens (BREthereumEWM ewm) { 18, NULL, NULL); - ewmAnnounceToken(ewm, rid, - "0x722dd3f80bac40c951b51bdd28dd19d435762180", - "TST", - "Test Standard Token", - "TeST Standard Token (TST) for TeSTing (TST)", - 18, - NULL, - NULL); + ewmAnnounceToken (ewm, rid, + "0x722dd3f80bac40c951b51bdd28dd19d435762180", + "TST", + "Test Standard Token", + "TeST Standard Token (TST) for TeSTing (TST)", + 18, + NULL, + NULL); ewmAnnounceTokenComplete (ewm, rid, ETHEREUM_BOOLEAN_TRUE); } @@ -980,227 +987,3 @@ ewmHandleEWMEvent(BREthereumEWM ewm, ewm, event); } - - - -#if 0 // Transaction -BREthereumTransactionId tid = -1; -BREthereumAddress primaryAddress = accountGetPrimaryAddress(ewm->account); - -assert (ETHEREUM_BOOLEAN_IS_TRUE(addressEqual(primaryAddress, bundle->from)) - || ETHEREUM_BOOLEAN_IS_TRUE(addressEqual(primaryAddress, bundle->to))); - -// primaryAddress is either the transaction's `source` or `target`. -BREthereumBoolean isSource = addressEqual(primaryAddress, bundle->from); - -BREthereumWalletId wid = ewmGetWallet(ewm); -BREthereumWallet wallet = ewmLookupWallet(ewm, wid); - -BREthereumBlock block = ewmLookupBlockByHash(ewm, bundle->blockHash); -block = blockCreateMinimal(bundle->blockHash, bundle->blockNumber, bundle->blockTimestamp); -ewmSignalBlockEvent(ewm, ewmInsertBlock(ewm, block), - BLOCK_EVENT_CREATED, - SUCCESS, NULL); - -// Look for a pre-existing transaction -BREthereumTransaction transaction = walletGetTransactionByHash(wallet, bundle->hash); - -// If we did not have a transaction for 'hash' it might be (might likely be) a newly submitted -// transaction that we are holding but that doesn't have a hash yet. This will *only* apply -// if we are the source. -if (NULL == transaction && ETHEREUM_BOOLEAN_IS_TRUE(isSource)) -transaction = walletGetTransactionByNonce(wallet, primaryAddress, bundle->nonce); - -// If we still don't have a transaction (with 'hash' or 'nonce'); then create one. -if (NULL == transaction) { - BREthereumAddress sourceAddr = (ETHEREUM_BOOLEAN_IS_TRUE(isSource) ? primaryAddress : bundle->from); - BREthereumAddress targetAddr = (ETHEREUM_BOOLEAN_IS_TRUE(isSource) ? bundle->to : primaryAddress); - - // Get the amount; this will be '0' if this is a token transfer - BREthereumAmount amount = amountCreateEther(etherCreate(bundle->amount)); - - // Easily extract the gasPrice and gasLimit. - BREthereumGasPrice gasPrice = gasPriceCreate(etherCreate(bundle->gasPrice)); - - BREthereumGas gasLimit = gasCreate(bundle->gasLimit); - - // Finally, get ourselves a transaction. - transaction = transactionCreate(sourceAddr, - targetAddr, - amount, - gasPrice, - gasLimit, - bundle->nonce); - // With a new transaction: - // - // a) add to the ewm - tid = ewmInsertTransaction(ewm, transaction); - // - // b) add to the wallet - walletHandleTransaction(wallet, transaction); - // - // c) announce the wallet update - ewmSignalTransactionEvent(ewm, wid, tid, - TRANSACTION_EVENT_CREATED, - SUCCESS, NULL); - // - // d) announce as submitted (=> there is a hash, submitted by 'us' or 'them') - walletTransactionSubmitted(wallet, transaction, bundle->hash); - -} - -if (-1 == tid) -tid = ewmLookupTransactionId(ewm, transaction); - -BREthereumGas gasUsed = gasCreate(bundle->gasUsed); -// TODO: Process 'state' properly - errors? - -// Get the current status. -BREthereumTransactionStatus status = transactionGetStatus(transaction); - -// Update the status as blocked -if (TRANSACTION_STATUS_INCLUDED != status.type) -walletTransactionIncluded(wallet, transaction, gasUsed, - blockGetHash(block), - blockGetNumber(block), - bundle->blockTransactionIndex); - -// Announce a transaction event. If already 'BLOCKED', then update CONFIRMATIONS. -ewmSignalTransactionEvent(ewm, wid, tid, - (TRANSACTION_STATUS_INCLUDED == status.type - ? TRANSACTION_EVENT_BLOCK_CONFIRMATIONS_UPDATED - : TRANSACTION_EVENT_BLOCKED), - SUCCESS, - NULL); -#endif - - -#if 0 // Log -BREthereumTransactionId tid = -1; - -pthread_mutex_lock(&ewm->lock); - -// Token of interest -BREthereumToken token = tokenLookupByAddress(bundle->contract); -if (NULL == token) { pthread_mutex_unlock(&ewm->lock); return; } // uninteresting token - -// Event of interest -BREthereumContractEvent event = contractLookupEventForTopic (contractERC20, bundle->arrayTopics[0]); -if (NULL == event || event != eventERC20Transfer) { pthread_mutex_unlock(&ewm->lock); return; }; // uninteresting event - -BREthereumBlock block = NULL; -// BREthereumBlock block = ewmLookupBlockByHash(ewm, bundle->blockHash); -// block = blockCreateMinimal(bundle->blockHash, bundle->blockNumber, bundle->blockTimestamp); -// ewmSignalBlockEvent(ewm, ewmInsertBlock(ewm, block), -// BLOCK_EVENT_CREATED, -// SUCCESS, NULL); - -// Wallet for token -BREthereumWalletId wid = (NULL == token - ? ewmGetWallet(ewm) - : ewmGetWalletHoldingToken(ewm, token)); -BREthereumWallet wallet = ewmLookupWallet(ewm, wid); - -// Existing transaction -BREthereumTransaction transaction = walletGetTransactionByHash(wallet, bundle->hash); - -BREthereumGasPrice gasPrice = gasPriceCreate(etherCreate(bundle->gasPrice)); -BREthereumGas gasUsed = gasCreate(bundle->gasUsed); - - -// Create a token transaction -if (NULL == transaction) { - - // Parse the topic data - we fake it becasue we 'know' topics indices - BREthereumAddress sourceAddr = - addressCreate(eventERC20TransferDecodeAddress(event, bundle->arrayTopics[1])); - - BREthereumAddress targetAddr = - addressCreate(eventERC20TransferDecodeAddress(event, bundle->arrayTopics[2])); - - BRCoreParseStatus status = CORE_PARSE_OK; - - BREthereumAmount amount = - amountCreateToken(createTokenQuantity(token, eventERC20TransferDecodeUInt256(event, - bundle->data, - &status))); - - transaction = transactionCreate(sourceAddr, targetAddr, amount, gasPrice, gasUsed, 0); - - // With a new transaction: - // - // a) add to the ewm - tid = ewmInsertTransaction(ewm, transaction); - // - // b) add to the wallet - walletHandleTransaction(wallet, transaction); - // - // c) announce the wallet update - ewmSignalTransactionEvent(ewm, wid, tid, TRANSACTION_EVENT_CREATED, SUCCESS, NULL); - - // - // d) announce as submitted. - walletTransactionSubmitted(wallet, transaction, bundle->hash); - -} - -if (-1 == tid) -tid = ewmLookupTransactionId(ewm, transaction); - -// TODO: Process 'state' properly - errors? - -// Get the current status. -BREthereumTransactionStatus status = transactionGetStatus(transaction); - -// Update the status as blocked -if (TRANSACTION_STATUS_INCLUDED != status.type) -walletTransactionIncluded(wallet, transaction, gasUsed, - blockGetHash(block), - blockGetNumber(block), - bundle->blockTransactionIndex); - -// Announce a transaction event. If already 'BLOCKED', then update CONFIRMATIONS. -ewmSignalTransactionEvent(ewm, wid, tid, - (TRANSACTION_STATUS_INCLUDED == status.type - ? TRANSACTION_EVENT_BLOCK_CONFIRMATIONS_UPDATED - : TRANSACTION_EVENT_BLOCKED), - SUCCESS, - NULL); - -// Hmmmm... -pthread_mutex_unlock(&ewm->lock); -#endif - - -#if 0 // token -static int -ewmDataIsEmpty (BREthereumEWM ewm, const char *data) { - return NULL == data || 0 == strcmp ("", data) || 0 == strcmp ("0x", data); -} - -// If `data` is anything besides "0x", then we have a contract function call. At that point -// it seems we need to process `data` to extract the 'function + args' and then, if the -// function is 'transfer() token' we can then and only then conclude that we have a token - -if (ewmDataIsEmpty(ewm, data)) return NULL; - -// There is contract data; see if it is a ERC20 function. -BREthereumContractFunction function = contractLookupFunctionForEncoding(contractERC20, data); - -// Not an ERC20 token -if (NULL == function) return NULL; - -// See if we have an existing token. -BREthereumToken token = tokenLookup(target); -if (NULL == token) token = tokenLookup(contract); - -// We found a token... -if (NULL != token) return token; - -// ... we didn't find a token - we should create is dynamically. -fprintf (stderr, "Ignoring transaction for unknown ERC20 token at '%s'", target); -return NULL; -#endif - - - diff --git a/ethereum/ewm/BREthereumEWMPrivate.h b/ethereum/ewm/BREthereumEWMPrivate.h index fd3ae006a..08eb450bb 100644 --- a/ethereum/ewm/BREthereumEWMPrivate.h +++ b/ethereum/ewm/BREthereumEWMPrivate.h @@ -52,6 +52,10 @@ extern void ewmInsertWallet (BREthereumEWM ewm, BREthereumWallet wallet); +extern void +ewmUpdateBlockHeight(BREthereumEWM ewm, + uint64_t blockHeight); + // // EWM // diff --git a/ethereum/ewm/BREthereumTransfer.h b/ethereum/ewm/BREthereumTransfer.h index 1a69e79c1..8b71469cf 100644 --- a/ethereum/ewm/BREthereumTransfer.h +++ b/ethereum/ewm/BREthereumTransfer.h @@ -25,16 +25,6 @@ extern "C" { #define TRANSACTION_NONCE_IS_NOT_ASSIGNED UINT64_MAX -static inline BREthereumGas -feeBasisGetGasLimit (BREthereumFeeBasis basis) { - return (FEE_BASIS_GAS == basis.type ? basis.u.gas.limit : gasCreate(0)); -} - -static inline BREthereumGasPrice -feeBasisGetGasPrice (BREthereumFeeBasis basis) { - return (FEE_BASIS_GAS == basis.type ? basis.u.gas.price : gasPriceCreate(etherCreateZero())); -} - typedef enum { TRANSFER_BASIS_TRANSACTION, TRANSFER_BASIS_LOG