Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import foundation.icon.jsonrpc.Address;
import foundation.icon.jsonrpc.IconJsonModule;
import foundation.icon.jsonrpc.JsonrpcClient;
import foundation.icon.jsonrpc.SendTransactionParamSerializer;
import foundation.icon.jsonrpc.TypeReference;
import foundation.icon.jsonrpc.model.CallData;
import foundation.icon.jsonrpc.model.CallParam;
import foundation.icon.jsonrpc.model.DeployData;
import foundation.icon.jsonrpc.model.Hash;
import foundation.icon.jsonrpc.model.SendTransactionParam;
import foundation.icon.jsonrpc.model.TransactionParam;
import foundation.icon.jsonrpc.model.TransactionResult;
import foundation.icon.jsonrpc.*;
import foundation.icon.jsonrpc.model.*;
import score.UserRevertedException;

import java.io.File;
Expand All @@ -40,20 +30,15 @@
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DefaultScoreClient extends JsonrpcClient {
public static final Address ZERO_ADDRESS = new Address("cx0000000000000000000000000000000000000000");
public static final BigInteger DEFAULT_STEP_LIMIT = new BigInteger("9502f900",16);
public static final BigInteger DEFAULT_STEP_LIMIT = new BigInteger("9502f900", 16);
public static final long DEFAULT_RESULT_RETRY_WAIT = 1000;
public static final long DEFAULT_RESULT_TIMEOUT = 10000;

Expand Down Expand Up @@ -91,28 +76,48 @@ public DefaultScoreClient(DefaultScoreClient client) {
this.address = client._address();
this.stepLimit = client._stepLimit();
this.resultTimeout = client._resultTimeout();
this.resultRetryWait = client._resultRetryWait();
}

static void initialize(JsonrpcClient client) {
client.mapper().registerModule(new IconJsonModule());
client.mapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

public static DefaultScoreClient _deploy(String url, String nid, String keyStorePath, String keyStorePassword, String scoreFilePath, Map<String, Object> params) {
public static DefaultScoreClient _deploy(String url, String nid, String keyStorePath, String keyStorePassword,
String scoreFilePath, Map<String, Object> params) {
return _deploy(url, nid(nid), wallet(keyStorePath, keyStorePassword), scoreFilePath, params);
}

public static DefaultScoreClient _deploy(String url, BigInteger nid, Wallet wallet, String scoreFilePath, Map<String, Object> params) {
public static DefaultScoreClient _deploy(String url, BigInteger nid, Wallet wallet, String scoreFilePath,
Map<String, Object> params) {
return _deploy(url, nid, DEFAULT_STEP_LIMIT, wallet, scoreFilePath, params);
}

public static DefaultScoreClient _deploy(String url, BigInteger nid, BigInteger stepLimit, Wallet wallet, String scoreFilePath, Map<String, Object> params) {
public static DefaultScoreClient _deploy(String url, BigInteger nid, BigInteger stepLimit, Wallet wallet,
String scoreFilePath, Map<String, Object> params) {
JsonrpcClient client = new JsonrpcClient(url);
initialize(client);
Address address = deploy(client, nid, wallet, stepLimit, ZERO_ADDRESS, scoreFilePath, params, DEFAULT_RESULT_TIMEOUT);
Address address = deploy(client, nid, wallet, stepLimit, ZERO_ADDRESS, scoreFilePath, params,
DEFAULT_RESULT_TIMEOUT);
return new DefaultScoreClient(url, nid, stepLimit, wallet, address);
}

public static DefaultScoreClient _getDeploymentResult(String url, BigInteger nid, Wallet wallet, Hash hash) {
JsonrpcClient client = new JsonrpcClient(url);
initialize(client);
TransactionResult txr = result(client, hash, DEFAULT_RESULT_TIMEOUT);
Address address = txr.getScoreAddress();
return new DefaultScoreClient(url, nid, wallet, address);
}

public static Hash _getDeploymentHash(String url, BigInteger nid, Wallet wallet, String scoreFilePath, Map<String
, Object> params) {
JsonrpcClient client = new JsonrpcClient(url);
initialize(client);
return getDeploymentHash(client, nid, wallet, DEFAULT_STEP_LIMIT, ZERO_ADDRESS, scoreFilePath, params);
}

public void _update(String scoreFilePath, Map<String, Object> params) {
deploy(this, nid, wallet, DEFAULT_STEP_LIMIT, address, scoreFilePath, params, DEFAULT_RESULT_TIMEOUT);
}
Expand Down Expand Up @@ -219,8 +224,9 @@ public static DefaultScoreClient of(String prefix, Properties properties, Map<St
String scoreFilePath = scoreFilePath(prefix, properties);
Map<String, Object> deployParams = params(prefix, properties, params);
if (address == null) {
System.out.printf("deploy prefix: %s, url: %s, nid: %s, stepLimit: %s, keyStorePath: %s, scoreFilePath: %s, params: %s%n",
prefix, url, nid, stepLimit, wallet != null ? wallet.getAddress() : wallet, scoreFilePath, deployParams);
System.out.printf("deploy prefix: %s, url: %s, nid: %s, stepLimit: %s, keyStorePath: %s, scoreFilePath: " +
"%s, params: %s%n", prefix, url, nid, stepLimit, wallet != null ? wallet.getAddress() : wallet,
scoreFilePath, deployParams);
return _deploy(url, nid, stepLimit, wallet, scoreFilePath, deployParams);
} else {
System.out.printf("prefix: %s, url: %s, nid: %s, stepLimit: %s, wallet: %s, address: %s%n",
Expand All @@ -239,15 +245,15 @@ public static String url(Properties properties) {
}

public static String url(String prefix, Properties properties) {
return properties.getProperty(prefix+"url");
return properties.getProperty(prefix + "url");
}

public static BigInteger nid(Properties properties) {
return nid("", properties);
}

public static BigInteger nid(String prefix, Properties properties) {
return nid(properties.getProperty(prefix+"nid"));
return nid(properties.getProperty(prefix + "nid"));
}

public static BigInteger nid(String nid) {
Expand All @@ -265,7 +271,7 @@ public static BigInteger stepLimit(Properties properties) {
}

public static BigInteger stepLimit(String prefix, Properties properties) {
return stepLimit(properties.getProperty(prefix+"stepLimit"));
return stepLimit(properties.getProperty(prefix + "stepLimit"));
}

public static BigInteger stepLimit(String stepLimit) {
Expand All @@ -283,15 +289,15 @@ public static Wallet wallet(Properties properties) {
}

public static Wallet wallet(String prefix, Properties properties) {
String keyStore = properties.getProperty(prefix+"keyStore");
String keyStore = properties.getProperty(prefix + "keyStore");
if (keyStore == null || keyStore.isEmpty()) {
return null;
}
String keyPassword = properties.getProperty(prefix+"keyPassword");
String keyPassword = properties.getProperty(prefix + "keyPassword");
if (keyPassword == null || keyPassword.isEmpty()) {
String keySecret = properties.getProperty(prefix+"keySecret");
String keySecret = properties.getProperty(prefix + "keySecret");
try {
System.out.println("using keySecret "+keySecret);
System.out.println("using keySecret " + keySecret);
keyPassword = Files.readString(Path.of(keySecret));
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -301,7 +307,7 @@ public static Wallet wallet(String prefix, Properties properties) {
}

public static Wallet wallet(String keyStorePath, String keyStorePassword) {
System.out.println("load wallet "+keyStorePath);
System.out.println("load wallet " + keyStorePath);
return Wallet.load(keyStorePassword, new File(keyStorePath));
}

Expand All @@ -310,7 +316,7 @@ public static Address address(Properties properties) {
}

public static Address address(String prefix, Properties properties) {
String address = properties.getProperty(prefix+"address");
String address = properties.getProperty(prefix + "address");
if (address == null || address.isEmpty()) {
return null;
}
Expand All @@ -326,17 +332,15 @@ public static boolean isUpdate(Properties properties) {
}

public static boolean isUpdate(String prefix, Properties properties) {
return Boolean.parseBoolean(
(String)properties.getOrDefault(prefix+"isUpdate",
Boolean.FALSE.toString()));
return Boolean.parseBoolean((String) properties.getOrDefault(prefix + "isUpdate", Boolean.FALSE.toString()));
}

public static String scoreFilePath(Properties properties) {
return scoreFilePath("", properties);
}

public static String scoreFilePath(String prefix, Properties properties) {
return properties.getProperty(prefix+"scoreFilePath");
return properties.getProperty(prefix + "scoreFilePath");
}

public static Map<String, Object> params(Properties properties) {
Expand All @@ -348,16 +352,16 @@ public static Map<String, Object> params(String prefix, Properties properties) {
}

public static Map<String, Object> params(String prefix, Properties properties, Map<String, Object> overwrite) {
String paramsKey = prefix+"params.";
String paramsKey = prefix + "params.";
Map<String, Object> params = new HashMap<>();
for(Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = ((String)entry.getKey());
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = ((String) entry.getKey());
if (key.startsWith(paramsKey)) {
params.put(key.substring(paramsKey.length()), entry.getValue());
}
}
if (overwrite != null) {
for(Map.Entry<String, Object> entry : overwrite.entrySet()) {
for (Map.Entry<String, Object> entry : overwrite.entrySet()) {
params.put(entry.getKey(), entry.getValue());
}
}
Expand Down Expand Up @@ -417,7 +421,7 @@ static Hash sendTransaction(JsonrpcClient client, Wallet wallet, SendTransaction
}

static void waitForResult(long millis, Hash txh) {
System.out.println("wait for "+txh);
System.out.println("wait for " + txh);
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
Expand All @@ -429,29 +433,44 @@ public static TransactionResult send(
JsonrpcClient client, BigInteger nid, Wallet wallet, BigInteger stepLimit, Address address,
BigInteger valueForPayable, String method, Map<String, Object> params,
long timeout) {
return send(client, nid, wallet, stepLimit, address, valueForPayable, method, params, timeout, DEFAULT_RESULT_RETRY_WAIT);
return send(client, nid, wallet, stepLimit, address, valueForPayable, method, params, timeout,
DEFAULT_RESULT_RETRY_WAIT);
}

public static TransactionResult send(
JsonrpcClient client, BigInteger nid, Wallet wallet, BigInteger stepLimit, Address address,
BigInteger valueForPayable, String method, Map<String, Object> params,
long timeout, long resultRetryWait) {
SendTransactionParam tx = new SendTransactionParam(nid, address, valueForPayable, "call", callData(method, params));
SendTransactionParam tx = new SendTransactionParam(nid, address, valueForPayable, "call", callData(method,
params));
tx.setStepLimit(stepLimit);
Hash txh = sendTransaction(client, wallet, tx);
waitForResult(resultRetryWait*2, txh);
waitForResult(resultRetryWait * 2, txh);
return result(client, txh, timeout, resultRetryWait);
}

public static Address deploy(
JsonrpcClient client, BigInteger nid, Wallet wallet, BigInteger stepLimit, Address address,
String scoreFilePath, Map<String, Object> params,
long timeout) {
return deploy(client, nid, wallet, stepLimit, address, scoreFilePath, params, timeout, DEFAULT_RESULT_RETRY_WAIT);
return deploy(client, nid, wallet, stepLimit, address, scoreFilePath, params, timeout,
DEFAULT_RESULT_RETRY_WAIT);
}

public static Address deploy(
JsonrpcClient client, BigInteger nid, Wallet wallet, BigInteger stepLimit, Address address,
String scoreFilePath, Map<String, Object> params,
long timeout, long resultRetryWait) {
Hash txh = getDeploymentHash(client, nid, wallet, stepLimit, address, scoreFilePath, params);
waitForResult(resultRetryWait * 2, txh);
TransactionResult txr = result(client, txh, timeout, resultRetryWait);
System.out.println("SCORE address: " + txr.getScoreAddress());
return txr.getScoreAddress();
}

public static Hash getDeploymentHash(
JsonrpcClient client, BigInteger nid, Wallet wallet, BigInteger stepLimit, Address address,
String scoreFilePath, Map<String, Object> params) {
byte[] content;
try {
content = Files.readAllBytes(Path.of(scoreFilePath));
Expand All @@ -466,13 +485,11 @@ public static Address deploy(
} else {
throw new RuntimeException("not supported score file");
}
SendTransactionParam tx = new SendTransactionParam(nid, address,null,"deploy", new DeployData(contentType, content, params));

SendTransactionParam tx = new SendTransactionParam(nid, address, null, "deploy", new DeployData(contentType,
content, params));
tx.setStepLimit(stepLimit);
Hash txh = sendTransaction(client, wallet, tx);
waitForResult(resultRetryWait*2, txh);
TransactionResult txr = result(client, txh, timeout, resultRetryWait);
System.out.println("SCORE address: "+txr.getScoreAddress());
return txr.getScoreAddress();
return sendTransaction(client, wallet, tx);
}

public static TransactionResult transfer(
Expand All @@ -492,18 +509,19 @@ public static TransactionResult transfer(
}
tx.setStepLimit(stepLimit);
Hash txh = sendTransaction(client, wallet, tx);
waitForResult(resultRetryWait*2, txh);
waitForResult(resultRetryWait * 2, txh);
return result(client, txh, timeout, resultRetryWait);
}

public static TransactionResult result(JsonrpcClient client, Hash txh, long timeout) {
return result(client, txh, timeout, DEFAULT_RESULT_RETRY_WAIT);
}

public static TransactionResult result(JsonrpcClient client, Hash txh, long timeout, long resultRetryWait) {
Map<String, Object> params = Map.of("txHash", txh);
long etime = System.currentTimeMillis() + timeout;
TransactionResult txr = null;
while(txr == null) {
while (txr == null) {
try {
txr = client.request(TransactionResult.class, "icx_getTransactionResult", params);
} catch (JsonrpcClient.JsonrpcError e) {
Expand Down Expand Up @@ -541,10 +559,10 @@ public static <T> T lastBlock(JsonrpcClient client, Class<T> blockType) {
}

public static <T> List<T> eventLogs(TransactionResult txr,
String signature,
Address scoreAddress,
Function<TransactionResult.EventLog, T> mapperFunc,
Predicate<T> filter) {
String signature,
Address scoreAddress,
Function<TransactionResult.EventLog, T> mapperFunc,
Predicate<T> filter) {
Predicate<TransactionResult.EventLog> predicate =
(el) -> el.getIndexed().get(0).equals(signature);
if (scoreAddress != null) {
Expand All @@ -553,7 +571,7 @@ public static <T> List<T> eventLogs(TransactionResult txr,
Stream<T> stream = txr.getEventLogs().stream()
.filter(predicate)
.map(mapperFunc);
if(filter != null) {
if (filter != null) {
stream = stream.filter(filter);
}
return stream.collect(Collectors.toList());
Expand Down