Skip to content

Commit

Permalink
docs: add docs for tx builder and ScriptHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Sep 2, 2023
1 parent d05f5e6 commit 7d05439
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ buildscript {
ext.mockkVersion = "1.13.5"

repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenCentral()
}

Expand Down Expand Up @@ -46,6 +47,7 @@ allprojects {
apply plugin: 'java'

repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenCentral()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public abstract class AbstractTransactionBuilder {
protected List<TransactionInput> inputsDetail = new ArrayList<>();
protected Transaction tx = new Transaction();

/**
* Initialites the transaction builder.
*
* @param configuration This is the bundle of configuration for builder,
* such as fee rate and registerred {@link org.nervos.ckb.transaction.handler.ScriptHandler}.
*/
public AbstractTransactionBuilder(TransactionBuilderConfiguration configuration, Iterator<TransactionInput> availableInputs) {
this.configuration = configuration;
this.availableInputs = availableInputs;
Expand Down Expand Up @@ -94,9 +100,24 @@ protected static long calculateTxFee(Transaction transaction, long feeRate) {
return Calculator.calculateTransactionFee(transaction, feeRate);
}

/**
* Builds the transaction with a default context for script handlers.
*
* This function will call script handlers in {@code TransactionConfiguration} with a
* default context null. Use {@link #build(Object...)} to pass custom contexts.
*
* @see org.nervos.ckb.transaction.handler.ScriptHandler#buildTransaction(AbstractTransactionBuilder, ScriptGroup, Object)
*/
public TransactionWithScriptGroups build() {
return build((Object) null);
}

/**
* Builds the transaction with custom contexts for script handlers.
*
* The contexts will be passed to the registered script handlers in the same order.
*
* @see org.nervos.ckb.transaction.handler.ScriptHandler#buildTransaction(AbstractTransactionBuilder, ScriptGroup, Object)
*/
abstract TransactionWithScriptGroups build(Object... contexts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,58 @@
import org.nervos.ckb.sign.ScriptGroup;
import org.nervos.ckb.transaction.AbstractTransactionBuilder;

/**
* ScriptHandler implements the building logic for various scripts.
*
* These handlers should be registered in the transaction builder via {@link
* org.nervos.ckb.transaction.TransactionBuilderConfiguration}.
*/
public interface ScriptHandler {
/**
* Build the transaction for the script.
*
* This function is called by transaction builder for each script group and each context.
*
* Generally this function should:
*
* <ol>
* <li>Check the scriptGroup to determine whether to proceed with the next steps or skip them.</li>
* <li>Add cell deps for the script.</li>
* <li>
* Prefill the witness for the script so the transaction size will not
* increase after signing, which may invalidate the fee calculation.
* </li>
* </ol>
*
* <p>For example:</p>
*
* <pre>{@code
* @Override
* boolean buildTransaction(AbstractTransactionBuilder txBuilder, ScriptGroup scriptGroup, Object context) {
* // Only change the transaction when the script is used.
* if (scriptGroup == null || !isMatched(scriptGroup.getScript())) {
* return false;
* }
* // Add celldeps
* txBuilder.addCellDeps(cellDeps);
*
* // Prefill witness placeholder
* int witnessIndex = scriptGroup.getInputIndices().get(0);
* byte[] dummyWitness = new byte[8];
* txBuilder.setWitness(witnessIndex, WitnessArgs.Type.LOCK, dummyWitness);
*
* return true;
* }
* }</pre>
*
* @param txBuilder The transaction in building.
* @param scriptGroup Transaction builder calls this callback for each script group found in the transaction.
* The script handler is responsoble for determining whether it should handle the given script group.
* @param context This is null passed from {@link org.nervos.ckb.transaction.AbstractTransactionBuilder#build()},
* or provided contexts in {@link org.nervos.ckb.transaction.AbstractTransactionBuilder#build(Object...)}.
* @return {@code true} when the script handler has modified the transaction.
*
*/
boolean buildTransaction(AbstractTransactionBuilder txBuilder, ScriptGroup scriptGroup, Object context);

void init(Network network);
Expand Down

0 comments on commit 7d05439

Please sign in to comment.