Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tx_gas_price and optimize balance helpInfo #823

Merged
merged 14 commits into from
Feb 2, 2024
Merged
5 changes: 5 additions & 0 deletions src/main/java/console/command/JlineUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ private static List<Completer> generateComplters(Client client) {
new StringsCompleter(command),
new StringsCompleter(SystemConfigService.TX_GAS_LIMIT),
new StringsCompleterIgnoreCase()));
completers.add(
new ArgumentCompleter(
new StringsCompleter(command),
new StringsCompleter(SystemConfigService.TX_GAS_PRICE),
new StringsCompleterIgnoreCase()));
completers.add(
new ArgumentCompleter(
new StringsCompleter(command),
Expand Down
86 changes: 43 additions & 43 deletions src/main/java/console/command/category/BalanceOpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,10 @@
import java.util.stream.Collectors;

public class BalanceOpCommand extends BasicCategoryCommand {
public static final CommandInfo GET_BALANCE =
new CommandInfo(
"getBalance",
"Get balance of the account",
HelpInfo::getBalanceHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().getBalance(params),
1,
1);
public static final CommandInfo ADD_BALANCE =
new CommandInfo(
"addBalance",
"Add balance to the account",
"Add balance to account. Only balanceGovernor can use it.",
HelpInfo::addBalanceHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().addBalance(params),
Expand All @@ -32,17 +23,16 @@ public class BalanceOpCommand extends BasicCategoryCommand {
public static final CommandInfo SUB_BALANCE =
new CommandInfo(
"subBalance",
"Sub balance from the account",
"Sub balance from account. Only balanceGovernor can use it",
HelpInfo::subBalanceHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().subBalance(params),
2,
3);

public static final CommandInfo TRANSFER_FROM =
new CommandInfo(
"transferBalance",
"Transfer balance from one account to another",
"Transfer balance from one account to another. Only balanceGovernor can use it",
HelpInfo::transferBalanceHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().transferBalance(params),
Expand All @@ -51,7 +41,7 @@ public class BalanceOpCommand extends BasicCategoryCommand {
public static final CommandInfo REGISTER_CALLER =
new CommandInfo(
"registerBalanceGovernor",
"Register the account to balanceGovernor",
"Register an account as balanceGovernor. Only Governor accounts can use it.",
HelpInfo::registerBalanceGovernorHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().registerBalanceGovernor(params),
Expand All @@ -60,46 +50,26 @@ public class BalanceOpCommand extends BasicCategoryCommand {
public static final CommandInfo UNREGISTER_CALLER =
new CommandInfo(
"unregisterBalanceGovernor",
"Unregister the account from balanceGovernor",
"Unregister an account from balanceGovernor. Only governor account can use it",
HelpInfo::unregisterBalanceGovernorHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer
.getPrecompiledFace()
.unregisterBalanceGovernor(params),
1,
1);
public static final CommandInfo LIST_CALLER =

public static final CommandInfo GET_BALANCE =
new CommandInfo(
"listBalanceGovernor",
"List all registered balanceGovernor",
HelpInfo::listBalanceGovernorHelp,
"getBalance",
"Get balance of the account",
HelpInfo::getBalanceHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().listBalanceGovernor(),
0,
0);

consoleInitializer.getPrecompiledFace().getBalance(params),
1,
1);
protected static final Map<String, CommandInfo> commandToCommandInfo = new HashMap<>();

static {
Field[] fields = BalanceOpCommand.class.getDeclaredFields();
for (Field field : fields) {
if (field.getType().equals(CommandInfo.class)) {
try {
CommandInfo constantCommandInfo = (CommandInfo) field.get(null);
commandToCommandInfo.put(constantCommandInfo.getCommand(), constantCommandInfo);
if (constantCommandInfo.getOptionCommand() != null) {
List<String> subCommandList = constantCommandInfo.getOptionCommand();
for (String s : subCommandList) {
commandToCommandInfo.put(s, constantCommandInfo);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}

public BalanceOpCommand() {
super(CommandType.BALANCE_PRECOMPILED_OP);
}
Expand Down Expand Up @@ -129,4 +99,34 @@ public List<String> getAllCommand(boolean isWasm, boolean isAuthOpen) {
public Map<String, CommandInfo> getAllCommandInfo(boolean isWasm) {
return commandToCommandInfo;
}

public static final CommandInfo LIST_CALLER =
new CommandInfo(
"listBalanceGovernor",
"List all registered balanceGovernor",
HelpInfo::listBalanceGovernorHelp,
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().listBalanceGovernor(),
0,
0);

static {
Field[] fields = BalanceOpCommand.class.getDeclaredFields();
for (Field field : fields) {
if (field.getType().equals(CommandInfo.class)) {
try {
CommandInfo constantCommandInfo = (CommandInfo) field.get(null);
commandToCommandInfo.put(constantCommandInfo.getCommand(), constantCommandInfo);
if (constantCommandInfo.getOptionCommand() != null) {
List<String> subCommandList = constantCommandInfo.getOptionCommand();
for (String s : subCommandList) {
commandToCommandInfo.put(s, constantCommandInfo);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public Map<String, CommandInfo> getAllCommandInfo(boolean isWasm) {
.getPrecompiledFace()
.setSystemConfigByKey(consoleInitializer, params),
2,
2);
3);

public static final CommandInfo GET_SYSTEM_CONFIG_BY_KEY =
new CommandInfo(
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ public static void systemConfigHelper() {
" -- the value of tx_gas_limit "
+ Common.TX_GAS_LIMIT_RANGE
+ "(default 3000000000).");
System.out.println(
" -- the value of tx_gas_price "
+ Common.TX_GAS_PRICE_RANGE
+ "(Users can specify the unit of gasPrice. The default unit is wei. support \"wei\", \"kwei\", \"mwei\", \"gwei\", \"szabo\", \"finney\", \"ether\", \"kether\", \"mether\", \"gether\").");
System.out.println(
" -- the value of "
+ SystemConfigService.CONSENSUS_PERIOD
Expand Down Expand Up @@ -475,7 +479,8 @@ public static void getBalanceHelp() {

public static void addBalanceHelp() {
System.out.println("Add balance to the specified account");
System.out.println("Usage: \naddBalance accountAddress amount");
System.out.println(
"Usage: \nOnly balanceGovernor can use it, addBalance accountAddress amount [unit].");
System.out.println("* accountAddress -- The address of the account.");
System.out.println("* amount -- The amount of token to add.");
System.out.println(
Expand All @@ -486,7 +491,8 @@ public static void addBalanceHelp() {

public static void subBalanceHelp() {
System.out.println("Sub balance from the specified account");
System.out.println("Usage: \nsubBalance accountAddress amount");
System.out.println(
"Usage: \nOnly balanceGovernor can use it, subBalance accountAddress amount [unit].");
System.out.println("* accountAddress -- The address of the account.");
System.out.println("* amount -- The amount of token to sub.");
System.out.println(
Expand All @@ -497,7 +503,8 @@ public static void subBalanceHelp() {

public static void transferBalanceHelp() {
System.out.println("Transfer token from address A to address B");
System.out.println("Usage: \ntransferBalance fromAddress toAddress amount");
System.out.println(
"Usage: \nOnly balanceGovernor can use it, transferBalance fromAddress toAddress amount [unit].");
System.out.println("* fromAddress -- The address of the sender.");
System.out.println("* toAddress -- The address of the receiver.");
System.out.println("* amount -- The amount of token to transfer.");
Expand All @@ -509,15 +516,17 @@ public static void transferBalanceHelp() {

public static void registerBalanceGovernorHelp() {
System.out.println("Register the specified account to balanceGovernor");
System.out.println("Usage: \nregisterBalanceGovernor accountAddress");
System.out.println(
"Usage: \nOnly governor account can use it, registerBalanceGovernor accountAddress.");
System.out.println("* accountAddress -- The address of the account.");
System.out.println("[Note]: The caller must be a contract address.");
System.out.println("[Note]: The request initiator account must be governor.");
}

public static void unregisterBalanceGovernorHelp() {
System.out.println("Unregister the specified account from balanceGovernor");
System.out.println("Usage: \nunregisterBalanceGovernor to accountAddress");
System.out.println(
"Usage: \nOnly governor account can use it, unregisterBalanceGovernor to accountAddress.");
System.out.println("* accountAddress -- The address of the account.");
System.out.println("[Note]: The caller must be a contract address.");
System.out.println("[Note]: The request initiator account must be governor.");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/console/common/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private Common() {}
// SystemConfig key
public static final String TX_COUNT_LIMIT = "tx_count_limit";
public static final String TX_GAS_LIMIT = "tx_gas_limit";
public static final String TX_GAS_PRICE = "tx_gas_price";
public static final String CONSENSUS_LEADER_PERIOD = "consensus_leader_period";
public static final String COMPATIBILITY_VERSION = "compatibility_version";
public static final String AUTH_CHECK_STATUS = "auth_check_status";
Expand All @@ -25,6 +26,7 @@ private Common() {}
Arrays.asList(
SystemConfigService.TX_COUNT_LIMIT,
SystemConfigService.TX_GAS_LIMIT,
SystemConfigService.TX_GAS_PRICE,
SystemConfigService.CONSENSUS_PERIOD,
SystemConfigService.COMPATIBILITY_VERSION,
SystemConfigService.AUTH_STATUS));
Expand All @@ -44,6 +46,7 @@ private Common() {}
public static final String DEPLOY_LOG_INTEGER_RANGE = "from 1 to 100";
public static final String TX_GAS_LIMIT_RANGE = "must be greater than 100000";
public static final String SYS_CONFIG_RANGE = "must be greater or equal to 1";
public static final String TX_GAS_PRICE_RANGE = "must be >= 0";
public static final String AUTH_CHECK_DESC =
"means whether to check auth when deploy/call contract, if value>0, check auth, otherwise not check auth.";
public static final String COMPATIBILITY_VERSION_DESC =
Expand Down
77 changes: 76 additions & 1 deletion src/main/java/console/precompiled/PrecompiledImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import console.precompiled.model.CRUDParseUtils;
import console.precompiled.model.Table;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -120,6 +121,13 @@ public void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[]
throws Exception {
String key = params[1];
String value = params[2];
if (params.length > 3 && key.equals(SystemConfigService.TX_GAS_PRICE)) {
Convert.Unit unit = Convert.Unit.fromString(params[3]);
BigDecimal weiValue = Convert.toWei(value, unit);
System.out.println("weiValue: " + weiValue);
value = weiValue.toBigIntegerExact().toString();
System.out.println("value: " + value);
}
RetCode retCode = this.systemConfigService.setValueByKey(key, value);
ConsoleUtils.printJson(retCode.toString());
if (key.equals(SystemConfigService.COMPATIBILITY_VERSION)
Expand Down Expand Up @@ -751,8 +759,12 @@ public void fixBFS(String[] params) throws Exception {
@Override
public void getBalance(String[] params) throws Exception {
String address = params[1];
if (!AddressUtils.isValidAddress(address)) {
System.out.println("Invalid address: " + address);
return;
}
BigInteger result = this.balanceService.getBalance(address);
System.out.println("balance: " + result);
System.out.println("balance: " + result + " wei");
}

@Override
Expand All @@ -763,6 +775,23 @@ public void addBalance(String[] params) throws Exception {
if (params.length > 3) {
unit = Convert.Unit.fromString(params[3]);
}
if (!AddressUtils.isValidAddress(address)) {
System.out.println("Invalid address: " + address);
return;
}
if (!ConsoleUtils.isValidNumber(amount)) {
System.out.println("Invalid amount: " + amount);
return;
}
BigDecimal value = new BigDecimal(amount);
if (value.compareTo(BigDecimal.ZERO) < 0) {
System.out.println("Invalid amount: " + amount);
return;
}
if (value.compareTo(BigDecimal.ZERO) == 0) {
System.out.println("Amount is zero, no need to addBalance.");
return;
}
RetCode retCode = this.balanceService.addBalance(address, amount, unit);

logger.info("addBalance: {}, retCode {}", address, retCode);
Expand All @@ -786,6 +815,23 @@ public void subBalance(String[] params) throws Exception {
if (params.length > 3) {
unit = Convert.Unit.fromString(params[3]);
}
if (!AddressUtils.isValidAddress(address)) {
System.out.println("Invalid address: " + address);
return;
}
if (!ConsoleUtils.isValidNumber(amount)) {
System.out.println("Invalid amount: " + amount);
return;
}
BigDecimal value = new BigDecimal(amount);
if (value.compareTo(BigDecimal.ZERO) < 0) {
System.out.println("Invalid amount: " + amount);
return;
}
if (value.compareTo(BigDecimal.ZERO) == 0) {
System.out.println("Amount is zero, no need to subBalance.");
return;
}
RetCode retCode = this.balanceService.subBalance(address, amount, unit);

logger.info("subBalance: {}, retCode {}", address, retCode);
Expand All @@ -809,6 +855,27 @@ public void transferBalance(String[] params) throws Exception {
if (params.length > 4) {
unit = Convert.Unit.fromString(params[4]);
}
if (!AddressUtils.isValidAddress(from)) {
System.out.println("Invalid from address: " + from);
return;
}
if (!AddressUtils.isValidAddress(to)) {
System.out.println("Invalid to address: " + to);
return;
}
if (!ConsoleUtils.isValidNumber(amount)) {
System.out.println("Invalid amount: " + amount);
return;
}
BigDecimal value = new BigDecimal(amount);
if (value.compareTo(BigDecimal.ZERO) < 0) {
System.out.println("Invalid amount: " + amount);
return;
}
if (value.compareTo(BigDecimal.ZERO) == 0) {
System.out.println("Amount is zero, no need to transferBalance.");
return;
}
RetCode retCode = this.balanceService.transfer(from, to, amount, unit);

logger.info("transferBalance: {}, retCode {}", from, retCode);
Expand All @@ -834,6 +901,10 @@ public void transferBalance(String[] params) throws Exception {
@Override
public void registerBalanceGovernor(String[] params) throws Exception {
String address = params[1];
if (!AddressUtils.isValidAddress(address)) {
System.out.println("Invalid address: " + address);
return;
}
RetCode retCode = this.balanceService.registerCaller(address);

logger.info("registerBalanceGovernor: {}, retCode {}", address, retCode);
Expand All @@ -850,6 +921,10 @@ public void registerBalanceGovernor(String[] params) throws Exception {
@Override
public void unregisterBalanceGovernor(String[] params) throws Exception {
String address = params[1];
if (!AddressUtils.isValidAddress(address)) {
System.out.println("Invalid address: " + address);
return;
}
RetCode retCode = this.balanceService.unregisterCaller(address);

logger.info("unregisterBalanceGovernor: {}, retCode {}", address, retCode);
Expand Down
Loading