Skip to content

Commit

Permalink
Patches for rate usage
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
JamesSmartCell committed Dec 7, 2022
1 parent 81972c6 commit 8acd3f7
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 22 deletions.
13 changes: 13 additions & 0 deletions app/src/main/cpp/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getSecondaryInfuraKey( JN
#endif
}

JNIEXPORT jstring JNICALL
Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getTertiaryInfuraKey( JNIEnv* env, jobject thiz )
{
#if (HAS_KEYS == 1)
return getDecryptedKey(env, tertiaryInfuraKey);
#elif (HAS_INFURA == 1)
return (*env)->NewStringUTF(env, IFKEY);
#else
const jstring key = "da3717f25f824cc1baa32d812386d93f";
return (*env)->NewStringUTF(env, key);
#endif
}

JNIEXPORT jstring JNICALL
Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getKlaytnKey( JNIEnv* env, jobject thiz )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ public static List<CoinGeckoTicker> buildTickerList(String jsonData, String curr
fiatPrice = obj.getDouble(currencyIsoSymbol.toLowerCase());
fiatChangeStr = obj.getString(currencyIsoSymbol.toLowerCase() + "_24h_change");
}
else
else if (obj.has("usd"))
{
fiatPrice = obj.getDouble("usd") * currentConversionRate;
fiatChangeStr = obj.getString("usd_24h_change");
}
else
{
continue; //handle empty/corrupt returns
}

res.add(new CoinGeckoTicker(address, fiatPrice, getFiatChange(fiatChangeStr)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ public abstract class EthereumNetworkBase implements EthereumNetworkRepositoryTy
// Add deprecated testnet IDs here
));

private static final String INFURA_ENDPOINT = ".infura.io/v3/";

@Override
public String getDappBrowserRPC(long chainId)
{
NetworkInfo info = getNetworkByChain(chainId);

if (info == null)
{
return "";
}
else if (chainId == MAINNET_ID)
{
int index = info.rpcServerUrl.indexOf(INFURA_ENDPOINT);
return info.rpcServerUrl.substring(0, index + INFURA_ENDPOINT.length()) + keyProvider.getTertiaryInfuraKey();
}
else if (info.backupNodeUrl != null)
{
return info.backupNodeUrl;
}
else
{
return info.rpcServerUrl;
}
}

// for reset built-in network
private static final LongSparseArray<NetworkInfo> builtinNetworkMap = new LongSparseArray<NetworkInfo>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.alphawallet.app.entity.NetworkInfo;
import com.alphawallet.app.entity.Wallet;
import com.alphawallet.app.entity.tokens.Token;
import com.alphawallet.app.repository.entity.RealmToken;

import org.web3j.protocol.Web3j;

Expand Down Expand Up @@ -54,6 +53,7 @@ public interface EthereumNetworkRepositoryType {
void setHasSetNetworkFilters();
boolean isMainNetSelected();
void setActiveMainnet(boolean isMainNet);
String getDappBrowserRPC(long chainId);

void saveCustomRPCNetwork(String networkName, String rpcUrl, long chainId, String symbol, String blockExplorerUrl, String explorerApiUrl, boolean isTestnet, Long oldChainId);
void removeCustomRPCNetwork(long chainId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface KeyProvider

String getSecondaryInfuraKey();

String getTertiaryInfuraKey();

String getRampKey();

String getOpenSeaKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public KeyProviderJNIImpl()

public native String getSecondaryInfuraKey();

public native String getTertiaryInfuraKey();

public native String getBSCExplorerKey();

public native String getAnalyticsKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class TickerService
private static String currentCurrencySymbol;
private static final Map<Long, Long> canUpdate = new ConcurrentHashMap<>();
private static final Map<String, TokenCardMeta> dexGuruQuery = new ConcurrentHashMap<>();
private static long lastTickerUpdate;

@Nullable
private Disposable tickerUpdateTimer;
Expand All @@ -119,11 +120,12 @@ public TickerService(OkHttpClient httpClient, PreferenceRepositoryType sharedPre

resetTickerUpdate();
initCurrency();
lastTickerUpdate = 0;
}

public void updateTickers()
{
if (mainTickerUpdate != null && !mainTickerUpdate.isDisposed())
if (mainTickerUpdate != null && !mainTickerUpdate.isDisposed() && System.currentTimeMillis() > (lastTickerUpdate + DateUtils.MINUTE_IN_MILLIS))
{
return; //do not update if update is currently in progress
}
Expand Down Expand Up @@ -151,6 +153,7 @@ private void tickersUpdated(int tickerCount)
{
Timber.d("Tickers Updated: %s", tickerCount);
mainTickerUpdate = null;
lastTickerUpdate = System.currentTimeMillis();
}

public Single<Double> updateCurrencyConversion()
Expand Down
94 changes: 77 additions & 17 deletions app/src/main/java/com/alphawallet/app/util/ens/EnsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand All @@ -76,6 +78,7 @@ public class EnsResolver implements Resolvable

// Permit number offchain calls for a single contract call.
public static final int LOOKUP_LIMIT = 4;
private static final long ENS_CACHE_TIME_VALIDITY = 10 * (1000*60); //10 minutes

public static final String REVERSE_NAME_SUFFIX = ".addr.reverse";

Expand Down Expand Up @@ -112,11 +115,61 @@ public EnsResolver(Web3j web3j) {
this(web3j, Keys.ADDRESS_LENGTH_IN_HEX);
}

protected ContractAddress obtainOffchainResolverAddr(String ensName) throws Exception
protected ContractAddress obtainOffChainResolverAddress(String ensName) throws Exception
{
return new ContractAddress(chainId, getResolverAddress(ensName));
}

private static class CachedENSRead
{
public final String cachedResult;
public final long cachedResultTime;

public CachedENSRead(String result)
{
cachedResult = result;
cachedResultTime = System.currentTimeMillis();
}

public boolean isValid()
{
return System.currentTimeMillis() < (cachedResultTime + ENS_CACHE_TIME_VALIDITY); //10 minutes cache validity
}
}

//Need to cache results for Resolve
private static final Map<String, CachedENSRead> cachedNameReads = new ConcurrentHashMap<>();

private String cacheKey(String ensName, String addrFunction)
{
return ((ensName != null) ? ensName : "") + "%" + ((addrFunction != null) ? addrFunction : "");
}

private String resolveWithCaching(String ensName, byte[] nameHash, String resolverAddr) throws Exception
{
String dnsEncoded = NameHash.dnsEncode(ensName);
String addrFunction = encodeResolverAddr(nameHash);

CachedENSRead lookupData = cachedNameReads.get(cacheKey(ensName, addrFunction));
String lookupDataHex = lookupData != null ? lookupData.cachedResult : null;

if (lookupData == null || !lookupData.isValid())
{
EthCall result =
resolve(
Numeric.hexStringToByteArray(dnsEncoded),
Numeric.hexStringToByteArray(addrFunction),
resolverAddr);
lookupDataHex = result.isReverted() ? Utils.removeDoubleQuotes(result.getError().getData()) : result.getValue();// .toString();
if (!TextUtils.isEmpty(lookupDataHex) && !lookupDataHex.equals("0x"))
{
cachedNameReads.put(cacheKey(ensName, addrFunction), new CachedENSRead(lookupDataHex));
}
}

return lookupDataHex;
}

/**
* Returns the address of the resolver for the specified node.
*
Expand All @@ -133,24 +186,15 @@ public String resolve(String ensName) throws Exception
try {
if (isValidEnsName(ensName, addressLength))
{
ContractAddress resolverAddress = obtainOffchainResolverAddr(ensName);
ContractAddress resolverAddress = obtainOffChainResolverAddress(ensName);

boolean supportWildcard =
supportsInterface(EnsUtils.ENSIP_10_INTERFACE_ID, resolverAddress.address);
byte[] nameHash = NameHash.nameHashAsBytes(ensName);

String resolvedName;
if (supportWildcard) {
String dnsEncoded = NameHash.dnsEncode(ensName);
String addrFunction = encodeResolverAddr(nameHash);

EthCall result =
resolve(
Numeric.hexStringToByteArray(dnsEncoded),
Numeric.hexStringToByteArray(addrFunction),
resolverAddress.address);

String lookupDataHex = result.isReverted() ? Utils.removeDoubleQuotes(result.getError().getData()) : result.getValue();// .toString();
String lookupDataHex = resolveWithCaching(ensName, nameHash, resolverAddress.address);
resolvedName = resolveOffchain(lookupDataHex, resolverAddress, LOOKUP_LIMIT);
} else {
try {
Expand Down Expand Up @@ -359,7 +403,7 @@ public String reverseResolve(String address) throws Exception
if (WalletUtils.isValidAddress(address, addressLength))
{
String reverseName = Numeric.cleanHexPrefix(address) + REVERSE_NAME_SUFFIX;
ContractAddress resolverAddress = obtainOffchainResolverAddr(reverseName);
ContractAddress resolverAddress = obtainOffChainResolverAddress(reverseName);

byte[] nameHash = NameHash.nameHashAsBytes(reverseName);
String name;
Expand Down Expand Up @@ -438,11 +482,27 @@ public boolean supportsInterface(byte[] interfaceID, String address) throws Exce

public String resolverAddr(byte[] node, String address) throws Exception
{
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC_addr,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
//use caching
String nodeData = Numeric.toHexString(node);
CachedENSRead resolverData = cachedNameReads.get(cacheKey(nodeData, address));
String resolverAddr = resolverData != null ? resolverData.cachedResult : null;

return getContractData(address, function, "");
if (resolverData == null || !resolverData.isValid())
{
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC_addr,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>()
{
}));

resolverAddr = getContractData(address, function, "");
if (!TextUtils.isEmpty(resolverAddr) && resolverAddr.length() > 2)
{
cachedNameReads.put(cacheKey(nodeData, address), new CachedENSRead(resolverAddr));
}
}

return resolverAddr;
}

public String encodeResolverAddr(byte[] node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ public Single<BigInteger> calculateGasEstimate(Wallet wallet, Web3Transaction tr
}
}

// Use the backup node if avail
public String getNetworkNodeRPC(long chainId)
{
return ethereumNetworkRepository.getNetworkByChain(chainId).rpcServerUrl;
return ethereumNetworkRepository.getDappBrowserRPC(chainId);
}

public NetworkInfo getNetworkInfo(long chainId)
Expand Down
9 changes: 9 additions & 0 deletions app/src/test/java/com/alphawallet/app/ENSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public void testResolve() throws Exception {

assertEquals(
ensResolver.resolve("offchainexample.eth"), ("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045").toLowerCase());

//Now test the cache
assertEquals(
ensResolver.resolve("1.offchainexample.eth"), ("0x41563129cdbbd0c5d3e1c86cf9563926b243834d").toLowerCase());
assertEquals(
ensResolver.resolve("1.offchainexample.eth"), ("0x41563129cdbbd0c5d3e1c86cf9563926b243834d").toLowerCase());

assertEquals(
ensResolver.resolve("web3j.eth"), ("0x7bfd522dea355ddee2be3c01dfa4419451759310").toLowerCase());
}

//Temporarily remove - DAS seems to be acting up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void should_getNodeURLByNetworkId_when_use_production_key()
public void should_construct_infura_url_when_getNodeURLByNetworkId_given_production_key()
{
assertThat(EthereumNetworkBase.getNodeURLByNetworkId(1L), equalTo("https://mainnet.infura.io/v3/fake-key-for-testing"));
assertThat(EthereumNetworkBase.getNodeURLByNetworkId(3L), equalTo("https://rpc.ankr.com/eth_ropsten"));
assertThat(EthereumNetworkBase.getNodeURLByNetworkId(5L), equalTo("https://goerli.infura.io/v3/fake-key-for-testing"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public String getSecondaryInfuraKey()
return FAKE_KEY_FOR_TESTING;
}

@Override
public String getTertiaryInfuraKey()
{
return FAKE_KEY_FOR_TESTING;
}

@Override
public String getRampKey()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public String getSecondaryInfuraKey()
return null;
}

@Override
public String getTertiaryInfuraKey()
{
return null;
}

@Override
public String getRampKey()
{
Expand Down

0 comments on commit 8acd3f7

Please sign in to comment.