use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.
the class RoundManager method getNextRoundByExpectedRound.
private MeetingRound getNextRoundByExpectedRound(BlockExtendsData roundData) {
BlockHeader startBlockHeader = chain.getEndBlockHeader();
long roundIndex = roundData.getRoundIndex();
long roundStartTime = roundData.getRoundStartTime();
if (startBlockHeader.getHeight() != 0L) {
// if(roundData.getConsensusMemberCount() == roundData.getPackingIndexOfRound()) {
// roundIndex += 1;
// roundStartTime = roundData.getRoundEndTime();
// }
startBlockHeader = getFirstBlockHeightOfPreRoundByRoundIndex(roundIndex);
}
return calculationRound(startBlockHeader, roundIndex, roundStartTime);
}
use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.
the class RoundManager method getBlockCountMap.
private Map<String, Integer> getBlockCountMap(long roundStart, long roundEnd) {
List<BlockHeader> blockHeaderList = chain.getAllBlockHeaderList();
Map<String, Integer> map = new HashMap<>();
for (int i = blockHeaderList.size() - 1; i >= 0; i--) {
BlockHeader blockHeader = blockHeaderList.get(i);
BlockExtendsData roundData = new BlockExtendsData(blockHeader.getExtend());
if (roundData.getRoundIndex() > roundEnd) {
continue;
}
if (roundData.getRoundIndex() < roundStart) {
break;
}
Integer count = map.get(blockHeader.getPackingAddressStr());
if (null == count) {
count = 0;
}
map.put(blockHeader.getPackingAddressStr(), count + 1);
}
return map;
}
use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.
the class Chain method rollbackBlock.
public BlockHeader rollbackBlock() {
blockList.remove(blockList.size() - 1);
BlockHeader header;
if (blockHeaderList.size() == 1) {
header = blockHeaderList.remove(blockHeaderList.size() - 1);
this.startBlockHeader = null;
this.endBlockHeader = null;
} else {
BlockHeader preHeader = blockHeaderList.get(blockHeaderList.size() - 2);
header = blockHeaderList.remove(blockHeaderList.size() - 1);
if (header.getPreHash().equals(preHeader.getHash())) {
this.endBlockHeader = preHeader;
} else {
this.endBlockHeader = blockHeaderList.get(blockHeaderList.size() - 1);
}
}
return header;
}
use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.
the class VMContext method getBlockHeader.
/**
* @param height
* @return
* @throws NulsException
* @throws IOException
*/
public BlockHeaderDto getBlockHeader(long height) throws NulsException, IOException {
if (height < 0L) {
return null;
}
Result<BlockHeader> blockHeaderResult = blockService.getBlockHeader(height);
if (blockHeaderResult == null || blockHeaderResult.getData() == null) {
return null;
}
BlockHeaderDto header = new BlockHeaderDto(blockHeaderResult.getData());
return header;
}
use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.
the class CreateContractTxProcessor method onCommit.
@Override
public Result onCommit(CreateContractTransaction tx, Object secondaryData) {
ContractResult contractResult = tx.getContractResult();
contractService.saveContractExecuteResult(tx.getHash(), contractResult);
CreateContractData txData = tx.getTxData();
byte[] contractAddress = txData.getContractAddress();
byte[] sender = txData.getSender();
String senderStr = AddressTool.getStringAddressByBytes(sender);
String contractAddressStr = AddressTool.getStringAddressByBytes(contractAddress);
// 移除未确认的创建合约交易
contractTxService.removeLocalUnconfirmedCreateContractTransaction(senderStr, contractAddressStr, contractResult);
// 执行失败的合约直接返回
if (!contractResult.isSuccess()) {
return Result.getSuccess();
}
NulsDigestData hash = tx.getHash();
long blockHeight = tx.getBlockHeight();
long bestBlockHeight = NulsContext.getInstance().getBestHeight();
ContractAddressInfoPo info = new ContractAddressInfoPo();
info.setContractAddress(contractAddress);
info.setSender(sender);
try {
info.setCreateTxHash(hash.serialize());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
info.setCreateTime(tx.getTime());
info.setBlockHeight(blockHeight);
// byte[] stateRoot = contractResult.getStateRoot();
boolean isNrc20Contract = contractResult.isNrc20();
boolean acceptDirectTransfer = contractResult.isAcceptDirectTransfer();
info.setAcceptDirectTransfer(acceptDirectTransfer);
info.setNrc20(isNrc20Contract);
// 获取 token tracker
if (isNrc20Contract) {
BlockHeader blockHeader = tx.getBlockHeader();
byte[] newestStateRoot = blockHeader.getStateRoot();
// NRC20 token 标准方法获取名称数据
ProgramResult programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_NAME, null, null);
if (programResult.isSuccess()) {
String tokenName = programResult.getResult();
info.setNrc20TokenName(tokenName);
}
programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_SYMBOL, null, null);
if (programResult.isSuccess()) {
String symbol = programResult.getResult();
info.setNrc20TokenSymbol(symbol);
}
programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_DECIMALS, null, null);
if (programResult.isSuccess()) {
String decimals = programResult.getResult();
if (StringUtils.isNotBlank(decimals)) {
try {
info.setDecimals(new BigInteger(decimals).longValue());
} catch (Exception e) {
Log.error("Get nrc20 decimals error.", e);
// skip it
}
}
}
programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_TOTAL_SUPPLY, null, null);
if (programResult.isSuccess()) {
String totalSupply = programResult.getResult();
if (StringUtils.isNotBlank(totalSupply)) {
try {
info.setTotalSupply(new BigInteger(totalSupply));
} catch (Exception e) {
Log.error("Get nrc20 totalSupply error.", e);
// skip it
}
}
}
// 刷新创建者的token余额
vmHelper.refreshTokenBalance(newestStateRoot, info, senderStr, contractAddressStr);
// 处理合约事件
vmHelper.dealEvents(newestStateRoot, tx, contractResult, info);
}
Result result = contractAddressStorageService.saveContractAddress(contractAddress, info);
return result;
}
Aggregations