Skip to content

Commit

Permalink
Merge pull request tronprotocol#680 from tronprotocol/develop-evan-up…
Browse files Browse the repository at this point in the history
…dataccount

Develop evan updataccount
  • Loading branch information
renchenchang authored May 9, 2018
2 parents 7e18e5c + 4f44d81 commit 62536c6
Show file tree
Hide file tree
Showing 14 changed files with 719 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.tron.common.overlay.message.Message;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.Utils;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
Expand Down Expand Up @@ -111,7 +112,7 @@ public static void setAddressPreFixByte(byte addressPreFixByte) {
}

public static boolean addressValid(byte[] address) {
if (address == null || address.length == 0) {
if (ByteUtil.isNullOrZeroArray(address)) {
logger.warn("Warning: Address is empty !!");
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/tron/core/actuator/ActuatorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static List<Actuator> createActuator(TransactionCapsule transactionCapsul

private static Actuator getActuatorByContract(Contract contract, Manager manager) {
switch (contract.getType()) {
case AccountCreateContract:
return new CreateAccountActuator(contract.getParameter(), manager);
case AccountUpdateContract:
return new UpdateAccountActuator(contract.getParameter(), manager);
case TransferContract:
return new TransferActuator(contract.getParameter(), manager);
case TransferAssetContract:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Slf4j
public class CreateAccountActuator extends AbstractActuator {


@Deprecated //Can not create account by api. Need send more than 1 trx , will create account if not exit.
CreateAccountActuator(Any contract, Manager dbManager) {
super(contract, dbManager);
}
Expand Down
71 changes: 55 additions & 16 deletions src/main/java/org/tron/core/actuator/UpdateAccountActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,81 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.tron.core.Wallet;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.capsule.utils.TransactionUtil;
import org.tron.core.db.AccountIndexStore;
import org.tron.core.db.AccountStore;
import org.tron.core.db.Manager;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.protos.Contract.AccountUpdateContract;
import org.tron.protos.Protocol.Transaction.Result.code;

@Slf4j
public class UpdateAccountActuator extends AbstractActuator {

AccountUpdateContract accountUpdateContract;
byte[] accountName;
byte[] ownerAddress;
long fee;

UpdateAccountActuator(Any contract, Manager dbManager) {
super(contract, dbManager);
try {
accountUpdateContract = contract.unpack(AccountUpdateContract.class);
} catch (InvalidProtocolBufferException e) {
logger.error(e.getMessage(), e);
}
accountName = accountUpdateContract.getAccountName().toByteArray();
ownerAddress = accountUpdateContract.getOwnerAddress().toByteArray();
fee = calcFee();
}

@Override
public boolean execute(TransactionResultCapsule result) throws ContractExeException {
long fee = calcFee();
try {
public boolean execute(TransactionResultCapsule ret) {
AccountStore accountStore = dbManager.getAccountStore();
AccountIndexStore accountIndexStore = dbManager.getAccountIndexStore();
AccountCapsule account = accountStore.get(ownerAddress);

AccountUpdateContract accountUpdateContract = contract.unpack(AccountUpdateContract.class);
AccountCapsule account =
dbManager.getAccountStore().get(accountUpdateContract.getOwnerAddress().toByteArray());
account.setAccountName(accountName);
accountStore.put(ownerAddress, account);
accountIndexStore.put(account);

account.setAccountName(accountUpdateContract.getAccountName().toByteArray());
dbManager.getAccountStore().put(accountUpdateContract.getOwnerAddress().toByteArray(),
account);
return true;
} catch (InvalidProtocolBufferException e) {
logger.debug(e.getMessage(), e);
throw new ContractExeException(e.getMessage());
}
ret.setStatus(fee, code.SUCESS);

return true;
}

@Override
public boolean validate() throws ContractValidateException {
// todo validate freq.
if (this.dbManager == null) {
throw new ContractValidateException("No dbManager!");
}
if (accountUpdateContract == null) {
throw new ContractValidateException(
"contract type error,expected type [AccountUpdateContract],real type[" + contract
.getClass() + "]");
}

if (!TransactionUtil.validAccountName(accountName)) {
throw new ContractValidateException("Invalidate accountName");
}
if (!Wallet.addressValid(ownerAddress)) {
throw new ContractValidateException("Invalidate ownerAddress");
}

AccountCapsule account = dbManager.getAccountStore().get(ownerAddress);
if (account == null) {
throw new ContractValidateException("Account has not existed");
}
if (account.getAccountName() != null && !account.getAccountName().isEmpty()) {
throw new ContractValidateException("This account name already exist");
}
if (dbManager.getAccountIndexStore().has(accountName)) {
throw new ContractValidateException("This name has existed");
}

return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/tron/core/capsule/AccountCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public AccountType getType() {
return this.account.getType();
}

public ByteString getAccountName() {
return this.account.getAccountName();
}

public long getBalance() {
return this.account.getBalance();
Expand Down Expand Up @@ -385,5 +388,4 @@ public void setLatestWithdrawTime(long latestWithdrawTime) {
.setLatestWithdrawTime(latestWithdrawTime)
.build();
}

}
3 changes: 1 addition & 2 deletions src/main/java/org/tron/core/capsule/TransactionCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Wallet;
import org.tron.core.db.AccountStore;
import org.tron.core.exception.ValidateBandwidthException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.protos.Contract.AccountUpdateContract;
import org.tron.protos.Contract.AccountCreateContract;
import org.tron.protos.Contract.AccountUpdateContract;
import org.tron.protos.Contract.FreezeBalanceContract;
import org.tron.protos.Contract.ParticipateAssetIssueContract;
import org.tron.protos.Contract.TransferAssetContract;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/tron/core/capsule/utils/TransactionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.ByteUtil;
import org.tron.core.Wallet;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.protos.Contract.TransferContract;
Expand Down Expand Up @@ -49,6 +50,24 @@ private static boolean checkBalance(long totalBalance, long totalSpent) {
return totalBalance == totalSpent;
}

public static boolean validAccountName(byte[] accountName) {
if (ByteUtil.isNullOrZeroArray(accountName)) {
return false;
}
if (accountName.length > 32) {
return false;
}
// b must read able.
for (byte b : accountName) {
if (b < 0x21) {
return false; // 0x21 = '!'
}
if (b > 0x7E) {
return false; // 0x7E = '~'
}
}
return true;
}
/**
* Get sender.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/tron/core/config/DefaultConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public String blockIndex() {
return "block-index";
}

@Bean(name = "account-index")
public String accountIndex() {
return "account-index";
}

@Bean(name = "witness_schedule")
public String witnessSchedule() {
return "witness_schedule";
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/org/tron/core/db/AccountIndexStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.tron.core.db;

import com.google.protobuf.ByteString;
import java.util.Objects;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.BytesCapsule;

@Component
public class AccountIndexStore extends TronStoreWithRevoking<BytesCapsule> {

@Autowired
public AccountIndexStore(@Qualifier("account-index") String dbName) {
super(dbName);
}

private static AccountIndexStore instance;

public static void destroy() {
instance = null;
}

/**
* create fun.
*
* @param dbName the name of database
*/
public static AccountIndexStore create(String dbName) {
if (instance == null) {
synchronized (AccountIndexStore.class) {
if (instance == null) {
instance = new AccountIndexStore(dbName);
}
}
}
return instance;
}

public void put(AccountCapsule accountCapsule) {
put(accountCapsule.getAccountName().toByteArray(),
new BytesCapsule(accountCapsule.getAddress().toByteArray()));
}

public byte[] get(ByteString name) {
BytesCapsule bytesCapsule = get(name.toByteArray());
if (Objects.nonNull(bytesCapsule)) {
return bytesCapsule.getData();
}
return null;
}

@Override
public BytesCapsule get(byte[] key) {
byte[] value = dbSource.getData(key);
if (ArrayUtils.isEmpty(value)) {
return null;
}
return new BytesCapsule(value);
}

@Override
public boolean has(byte[] key) {
byte[] value = dbSource.getData(key);
if (ArrayUtils.isEmpty(value)) {
return false;
}
return true;
}
}
20 changes: 17 additions & 3 deletions src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class Manager {
@Autowired
private BlockIndexStore blockIndexStore;
@Autowired
private AccountIndexStore accountIndexStore;
@Autowired
private WitnessScheduleStore witnessScheduleStore;
@Autowired
private RecentBlockStore recentBlockStore;
Expand Down Expand Up @@ -237,6 +239,7 @@ public void destory() {
DynamicPropertiesStore.destroy();
WitnessScheduleStore.destroy();
BlockIndexStore.destroy();
AccountIndexStore.destroy();
}

@PostConstruct
Expand Down Expand Up @@ -329,6 +332,7 @@ public void initAccount() {
account.getAccountType(),
account.getBalance());
this.accountStore.put(account.getAddress(), accountCapsule);
this.accountIndexStore.put(accountCapsule);
});
}

Expand Down Expand Up @@ -758,7 +762,8 @@ public LinkedList<BlockId> getBlockChainHashesOnFork(final BlockId forkBlockHash
*/
public boolean containBlock(final Sha256Hash blockHash) {
try {
return this.khaosDb.containBlockInMiniStore(blockHash) || blockStore.get(blockHash.getBytes()) != null;
return this.khaosDb.containBlockInMiniStore(blockHash)
|| blockStore.get(blockHash.getBytes()) != null;
} catch (ItemNotFoundException e) {
return false;
} catch (BadItemException e) {
Expand Down Expand Up @@ -1015,10 +1020,10 @@ public long getSyncBeginNumber() {
}

public BlockId getSolidBlockId() {
try{
try {
long num = dynamicPropertiesStore.getLatestSolidifiedBlockNum();
return getBlockIdByNum(num);
}catch (Exception e){
} catch (Exception e) {
return getGenesisBlockId();
}
}
Expand Down Expand Up @@ -1109,11 +1114,20 @@ public void setBlockIndexStore(BlockIndexStore indexStore) {
this.blockIndexStore = indexStore;
}

public AccountIndexStore getAccountIndexStore() {
return this.accountIndexStore;
}

public void setAccountIndexStore(AccountIndexStore indexStore) {
this.accountIndexStore = indexStore;
}

public void closeAllStore() {
System.err.println("******** begin to close db ********");
closeOneStore(accountStore);
closeOneStore(blockStore);
closeOneStore(blockIndexStore);
closeOneStore(accountIndexStore);
closeOneStore(witnessStore);
closeOneStore(witnessScheduleStore);
closeOneStore(assetIssueStore);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/tron/core/services/RpcApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ public void updateWitness(Contract.WitnessUpdateContract request,
responseObserver.onCompleted();
}

@Override
public void updateAccount(Contract.AccountUpdateContract request,
StreamObserver<Transaction> responseObserver) {
try {
responseObserver.onNext(
createTransactionCapsule(request, ContractType.AccountUpdateContract).getInstance());
} catch (ContractValidateException e) {
responseObserver
.onNext(null);
logger.debug("ContractValidateException", e.getMessage());
}
responseObserver.onCompleted();
}

@Override
public void freezeBalance(Contract.FreezeBalanceContract request,
StreamObserver<Transaction> responseObserver) {
Expand Down
Loading

0 comments on commit 62536c6

Please sign in to comment.