Skip to content

Commit

Permalink
Merge pull request #259 from nervosnetwork/rc/v0.26.0
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.26.0
  • Loading branch information
duanyytop authored Dec 14, 2019
2 parents 06b7eec + 7a5b2cb commit f1dbc8e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 141 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [v0.26.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.26.0-rc2...v0.26.0) (2019-12-14)

### Refactor

* Remove useless code ([557632a](https://github.com/nervosnetwork/ckb-sdk-java/commit/557632aef474d7ff310a83e292f117b0502dbdba))

# [v0.26.0-rc2](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.25.0...v0.26.0-rc2) (2019-12-8)

### Feature
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ allprojects {
targetCompatibility = 1.8

group 'org.nervos.ckb'
version '0.26.0-rc2'
version '0.26.0'

apply plugin: 'java'

Expand Down Expand Up @@ -113,7 +113,7 @@ configure(subprojects.findAll { it.name != 'tests' }) {
publications {
mavenJava(MavenPublication) {
groupId 'org.nervos.ckb'
version '0.26.0-rc2'
version '0.26.0'
from components.java
}
}
Expand Down
143 changes: 4 additions & 139 deletions example/src/main/java/org/nervos/ckb/transaction/CellCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import java.math.BigInteger;
import java.util.*;
import org.nervos.ckb.service.Api;
import org.nervos.ckb.system.SystemContract;
import org.nervos.ckb.system.type.SystemScriptCell;
import org.nervos.ckb.type.Witness;
import org.nervos.ckb.type.cell.*;
import org.nervos.ckb.type.cell.CellInput;
import org.nervos.ckb.type.cell.CellOutput;
import org.nervos.ckb.type.cell.CellOutputWithOutPoint;
import org.nervos.ckb.type.cell.CellWithStatus;
import org.nervos.ckb.type.transaction.Transaction;
import org.nervos.ckb.utils.Calculator;
import org.nervos.ckb.utils.Numeric;
Expand All @@ -32,142 +33,6 @@ public CellCollector(Api api) {
this.skipDataAndType = true;
}

public CollectResult collectInputs(
List<String> addresses,
List<CellOutput> cellOutputs,
BigInteger feeRate,
int initialLength,
List<CellDep> cellDeps,
List<String> outputsData,
List<String> headerDeps)
throws IOException {

List<String> lockHashes = new ArrayList<>();
for (String address : addresses) {
AddressParseResult addressParseResult = AddressParser.parse(address);
lockHashes.add(addressParseResult.script.computeHash());
}
List<String> cellOutputsData = new ArrayList<>();
for (int i = 0; i < cellOutputs.size() - 1; i++) {
BigInteger size = cellOutputs.get(i).occupiedCapacity("0x");
if (size.compareTo(Numeric.toBigInt(cellOutputs.get(i).capacity)) > 0) {
throw new IOException("Cell output byte size must not be bigger than capacity");
}
cellOutputsData.add("0x");
}
SystemScriptCell systemScriptCell = SystemContract.getSystemSecpCell(api);
cellOutputsData.add("0x");

if (outputsData != null && outputsData.size() > 0) {
cellOutputsData = outputsData;
}

List<CellDep> cellDepList =
Collections.singletonList(new CellDep(systemScriptCell.outPoint, CellDep.DEP_GROUP));
if (cellDeps != null && cellDeps.size() > 0) {
cellDepList = cellDeps;
}

List<String> headerDepList = Collections.emptyList();
if (headerDeps != null && headerDeps.size() > 0) {
headerDepList = headerDeps;
}

Transaction transaction =
new Transaction(
"0",
cellDepList,
headerDepList,
Collections.emptyList(),
cellOutputs,
cellOutputsData,
Collections.emptyList());

BigInteger inputsCapacity = BigInteger.ZERO;
List<CellInput> cellInputs = new ArrayList<>();
Map<String, List<CellInput>> lockInputsMap = new HashMap<>();
for (String lockHash : lockHashes) {
lockInputsMap.put(lockHash, new ArrayList<>());
}
List witnesses = new ArrayList<>();

CellOutput changeOutput = cellOutputs.get(cellOutputs.size() - 1);

BigInteger needCapacity = BigInteger.ZERO;
for (CellOutput cellOutput : cellOutputs) {
needCapacity = needCapacity.add(Numeric.toBigInt(cellOutput.capacity));
}
List<CellOutputWithOutPoint> cellOutputList;
for (int index = 0; index < lockHashes.size(); index++) {
long toBlockNumber = api.getTipBlockNumber().longValue();
long fromBlockNumber = 1;

while (fromBlockNumber <= toBlockNumber
&& inputsCapacity.compareTo(needCapacity.add(calculateTxFee(transaction, feeRate))) < 0) {
long currentToBlockNumber = Math.min(fromBlockNumber + 100, toBlockNumber);
cellOutputList =
api.getCellsByLockHash(
lockHashes.get(index),
BigInteger.valueOf(fromBlockNumber).toString(),
BigInteger.valueOf(currentToBlockNumber).toString());
for (CellOutputWithOutPoint cellOutputWithOutPoint : cellOutputList) {
if (skipDataAndType) {
CellWithStatus cellWithStatus = api.getLiveCell(cellOutputWithOutPoint.outPoint, true);
String outputsDataContent = cellWithStatus.cell.data.content;
CellOutput cellOutput = cellWithStatus.cell.output;
if ((!Strings.isEmpty(outputsDataContent) && !"0x".equals(outputsDataContent))
|| cellOutput.type != null) {
continue;
}
}
CellInput cellInput = new CellInput(cellOutputWithOutPoint.outPoint, "0x0");
inputsCapacity = inputsCapacity.add(Numeric.toBigInt(cellOutputWithOutPoint.capacity));
List<CellInput> cellInputList = lockInputsMap.get(lockHashes.get(index));
cellInputList.add(cellInput);
cellInputs.add(cellInput);
witnesses.add("0x");
transaction.inputs = cellInputs;
transaction.witnesses = witnesses;
BigInteger sumNeedCapacity =
needCapacity
.add(calculateTxFee(transaction, feeRate))
.add(calculateOutputSize(changeOutput));
if (inputsCapacity.compareTo(sumNeedCapacity) > 0) {
// update witness of group first element
int witnessIndex = 0;
for (String lockHash : lockHashes) {
if (lockInputsMap.get(lockHash).size() == 0) break;
witnesses.set(witnessIndex, new Witness(NumberUtils.getZeros(initialLength)));
witnessIndex += lockInputsMap.get(lockHash).size();
}
transaction.witnesses = witnesses;
// calculate sum need capacity again
sumNeedCapacity =
needCapacity
.add(calculateTxFee(transaction, feeRate))
.add(calculateOutputSize(changeOutput));
if (inputsCapacity.compareTo(sumNeedCapacity) > 0) {
break;
}
}
}
fromBlockNumber = currentToBlockNumber + 1;
}
}
if (inputsCapacity.compareTo(needCapacity.add(calculateTxFee(transaction, feeRate))) < 0) {
throw new IOException("Capacity not enough!");
}
BigInteger changeCapacity =
inputsCapacity.subtract(needCapacity.add(calculateTxFee(transaction, feeRate)));
List<CellsWithAddress> cellsWithAddresses = new ArrayList<>();
for (Map.Entry<String, List<CellInput>> entry : lockInputsMap.entrySet()) {
cellsWithAddresses.add(
new CellsWithAddress(
entry.getValue(), addresses.get(lockHashes.indexOf(entry.getKey()))));
}
return new CollectResult(cellsWithAddresses, Numeric.toHexStringWithPrefix(changeCapacity));
}

public CollectResult collectInputs(
List<String> addresses, Transaction tx, BigInteger feeRate, int initialLength)
throws IOException {
Expand Down

0 comments on commit f1dbc8e

Please sign in to comment.