Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
Add feePayer parameter to build_transaction method
Browse files Browse the repository at this point in the history
  • Loading branch information
siy committed Jun 22, 2021
1 parent ea50a49 commit 6ec3b96
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import com.radixdlt.api.service.ActionParserService;
import com.radixdlt.api.service.SubmissionService;
import com.radixdlt.consensus.HashSigner;
import com.radixdlt.consensus.bft.Self;
import com.radixdlt.identifiers.AID;
import com.radixdlt.identifiers.REAddr;
import com.radixdlt.qualifier.LocalSigner;
import com.radixdlt.utils.functional.Result;

Expand All @@ -44,18 +46,21 @@ public class AccountHandler {
private final SubmissionService submissionService;
private final ActionParserService actionParserService;
private final HashSigner hashSigner;
private final REAddr account;

@Inject
public AccountHandler(
AccountInfoService accountService,
SubmissionService submissionService,
ActionParserService actionParserService,
@LocalSigner HashSigner hashSigner
@LocalSigner HashSigner hashSigner,
@Self REAddr account
) {
this.accountService = accountService;
this.submissionService = submissionService;
this.actionParserService = actionParserService;
this.hashSigner = hashSigner;
this.account = account;
}

public JSONObject handleAccountGetInfo(JSONObject request) {
Expand All @@ -81,7 +86,7 @@ private Result<AID> parseSignSubmit(
) {
return actionParserService.parse(actions)
.flatMap(steps -> submissionService.oneStepSubmit(
steps, message, hashSigner, disableResourceAllocationAndDestroy
account, steps, message, hashSigner, disableResourceAllocationAndDestroy
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.radixdlt.crypto.ECPublicKey;
import com.radixdlt.crypto.HashUtils;
import com.radixdlt.identifiers.AID;
import com.radixdlt.identifiers.AccountAddress;
import com.radixdlt.identifiers.REAddr;
import com.radixdlt.utils.functional.Result;

import java.util.List;
Expand Down Expand Up @@ -57,12 +59,13 @@ public ConstructionHandler(SubmissionService submissionService, ActionParserServ
public JSONObject handleConstructionBuildTransaction(JSONObject request) {
return withRequiredParameters(
request,
List.of("actions"),
List.of("actions", "feePayer"),
List.of("message", "disableResourceAllocationAndDestroy"),
params ->
safeArray(params, "actions")
.flatMap(actions -> actionParserService.parse(actions)
allOf(safeArray(params, "actions"), account(params))
.flatMap((actions, feePayer) -> actionParserService.parse(actions)
.flatMap(steps -> submissionService.prepareTransaction(
feePayer,
steps,
optString(params, "message"),
params.optBoolean("disableResourceAllocationAndDestroy")))
Expand Down Expand Up @@ -122,4 +125,9 @@ private static Result<AID> parseTxId(JSONObject params) {
private static JSONObject formatTxId(AID txId) {
return jsonObject().put("txID", txId);
}

private static Result<REAddr> account(JSONObject params) {
return safeString(params, "feePayer")
.flatMap(AccountAddress::parseFunctional);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private Result<JSONObject> sendTokens(REAddr destination) {

tokensToSend.forEach(rri -> steps.add(transfer(destination, rri)));

return submissionService.oneStepSubmit(steps, empty(), hashSigner, false)
return submissionService.oneStepSubmit(account, steps, empty(), hashSigner, false)
.map(FaucetHandler::formatTxId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@
import com.radixdlt.utils.functional.Result;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static com.radixdlt.api.data.ApiErrors.UNABLE_TO_PREPARE_TX;
import static com.radixdlt.atom.actions.ActionErrors.DIFFERENT_SOURCE_ADDRESSES;
import static com.radixdlt.atom.actions.ActionErrors.EMPTY_TRANSACTIONS_NOT_SUPPORTED;
import static com.radixdlt.atom.actions.ActionErrors.SUBMISSION_FAILURE;
import static com.radixdlt.atom.actions.ActionErrors.TRANSACTION_ADDRESS_DOES_NOT_MATCH;

Expand All @@ -69,32 +65,13 @@ public SubmissionService(
}

public Result<PreparedTransaction> prepareTransaction(
List<TransactionAction> steps, Optional<String> message, boolean disableResourceAllocAndDestroy
REAddr address, List<TransactionAction> steps, Optional<String> message, boolean disableResourceAllocAndDestroy
) {
var addresses = steps.stream()
.map(TransactionAction::getFrom)
.filter(Objects::nonNull)
.collect(Collectors.toSet());

if (addresses.size() != 1) {
return addresses.size() == 0
? EMPTY_TRANSACTIONS_NOT_SUPPORTED.result()
: DIFFERENT_SOURCE_ADDRESSES.result();
}

var addr = addresses.iterator().next();

return Result.wrap(
UNABLE_TO_PREPARE_TX,
() -> {
var txnConstructionRequest = toConstructionRequest(
addr, steps, message, disableResourceAllocAndDestroy
);

return radixEngine.construct(txnConstructionRequest)
.buildForExternalSign()
.map(this::toPreparedTx);
}
() -> radixEngine.construct(toConstructionRequest(address, steps, message, disableResourceAllocAndDestroy))
.buildForExternalSign()
.map(this::toPreparedTx)
);
}

Expand Down Expand Up @@ -151,9 +128,10 @@ private Txn buildTxn(byte[] blob, ECDSASignature recoverable) {
}

public Result<AID> oneStepSubmit(
List<TransactionAction> steps, Optional<String> message, HashSigner signer, boolean disableResourceAllocAndDestroy
REAddr address, List<TransactionAction> steps,
Optional<String> message, HashSigner signer, boolean disableResourceAllocAndDestroy
) {
return prepareTransaction(steps, message, disableResourceAllocAndDestroy)
return prepareTransaction(address, steps, message, disableResourceAllocAndDestroy)
.map(prepared -> buildTxn(prepared.getBlob(), signer.sign(prepared.getHashToSign())))
.flatMap(this::submit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.radixdlt.crypto.ECPublicKey;
import com.radixdlt.crypto.HashUtils;
import com.radixdlt.identifiers.AID;
import com.radixdlt.identifiers.REAddr;
import com.radixdlt.identifiers.ValidatorAddress;
import com.radixdlt.utils.functional.Result;

Expand All @@ -54,7 +55,8 @@ public class AccountHandlerTest {
private final HashSigner hashSigner = keyPair::sign;

private final AccountHandler handler = new AccountHandler(
accountService, submissionService, actionParserService, hashSigner
accountService, submissionService, actionParserService,
hashSigner, REAddr.ofPubKeyAccount(keyPair.getPublicKey())
);

@Test
Expand Down Expand Up @@ -95,7 +97,7 @@ public void testHandleAccountGetInfo() {
public void testHandleAccountSubmitTransactionSingleStep() {
var aid = AID.from(HashUtils.random256().asBytes());

when(submissionService.oneStepSubmit(any(), any(), any(), eq(false)))
when(submissionService.oneStepSubmit(any(), any(), any(), any(), eq(false)))
.thenReturn(Result.ok(aid));

var actions = jsonArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@
public class ConstructionHandlerTest {
private static final ECPublicKey PUB_KEY = ECKeyPair.generateNew().getPublicKey();
private static final REAddr ACCOUNT_ADDR = REAddr.ofPubKeyAccount(PUB_KEY);
private static final String ADDRESS = AccountAddress.of(ACCOUNT_ADDR);
private static final ECPublicKey V1 = ECKeyPair.generateNew().getPublicKey();
private static final ECPublicKey V2 = ECKeyPair.generateNew().getPublicKey();
private static final ECPublicKey V3 = ECKeyPair.generateNew().getPublicKey();
private static final String FEE_PAYER = AccountAddress.of(ACCOUNT_ADDR);

private final RriParser rriParser = mock(RriParser.class);
private final SubmissionService submissionService = mock(SubmissionService.class);
Expand All @@ -70,7 +67,7 @@ public class ConstructionHandlerTest {
public void testBuildTransactionPositional() {
var prepared = PreparedTransaction.create(randomBytes(), randomBytes(), UInt256.TEN);

when(submissionService.prepareTransaction(any(), any(), eq(false)))
when(submissionService.prepareTransaction(any(), any(), any(), eq(false)))
.thenReturn(Result.ok(prepared));

var actions = jsonArray()
Expand All @@ -81,6 +78,7 @@ public void testBuildTransactionPositional() {
);
var params = jsonArray()
.put(actions)
.put(FEE_PAYER)
.put("message");

var response = handler.handleConstructionBuildTransaction(requestWith(params));
Expand All @@ -98,7 +96,7 @@ public void testBuildTransactionPositional() {
public void testBuildTransactionNamed() {
var prepared = PreparedTransaction.create(randomBytes(), randomBytes(), UInt256.TEN);

when(submissionService.prepareTransaction(any(), any(), eq(false)))
when(submissionService.prepareTransaction(any(), any(), any(), eq(false)))
.thenReturn(Result.ok(prepared));

var actions = jsonArray()
Expand All @@ -109,6 +107,7 @@ public void testBuildTransactionNamed() {
);
var params = jsonObject()
.put("actions", actions)
.put("feePayer", FEE_PAYER)
.put("message", "message");

var response = handler.handleConstructionBuildTransaction(requestWith(params));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void testPrepareTransaction() throws Exception {
)
);

var result = submissionService.prepareTransaction(steps, Optional.of("message"), false);
var result = submissionService.prepareTransaction(acct, steps, Optional.of("message"), false);

result
.onFailureDo(Assert::fail)
Expand Down Expand Up @@ -282,6 +282,6 @@ private Result<PreparedTransaction> buildTransaction() throws TxBuilderException
TransactionAction.transfer(ALICE_ACCT, BOB_ACCT, UInt256.FOUR, nativeToken)
);

return submissionService.prepareTransaction(steps, Optional.of("message"), false);
return submissionService.prepareTransaction(acct, steps, Optional.of("message"), false);
}
}

0 comments on commit 6ec3b96

Please sign in to comment.