Skip to content

Commit

Permalink
Set UnlModify.account to ACCOUNT_ZERO to avoid deserialization issues (
Browse files Browse the repository at this point in the history
…#294)

* set UnlModify.account to ACCOUNT_ZERO
  • Loading branch information
nkramer44 authored Aug 3, 2022
1 parent ba64aed commit 9dcbbfa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -30,14 +30,20 @@
import org.xrpl.xrpl4j.model.client.ledger.LedgerRequestParams;
import org.xrpl.xrpl4j.model.client.ledger.LedgerResult;
import org.xrpl.xrpl4j.model.client.transactions.TransactionResult;
import org.xrpl.xrpl4j.model.transactions.Transaction;
import org.xrpl.xrpl4j.model.transactions.UnlModify;
import org.xrpl.xrpl4j.tests.environment.MainnetEnvironment;

import java.util.Optional;

/**
* These tests ensure {@link LedgerResult}s can be constructed from all of the different JSON responses
* rippled sends back.
*/
public class LedgerResultIT extends AbstractIT {

final XrplClient mainnetClient = new MainnetEnvironment().getXrplClient();

@Test
void getValidatedLedgerResult() throws JsonRpcClientErrorException {
final LedgerResult ledgerResult = xrplClient.ledger(LedgerRequestParams.builder()
Expand Down Expand Up @@ -95,8 +101,6 @@ void getClosedLedgerResult() throws JsonRpcClientErrorException {

@Test
void getLedgerResult() throws JsonRpcClientErrorException {

final XrplClient mainnetClient = new MainnetEnvironment().getXrplClient();
final LedgerResult ledgerResult = mainnetClient.ledger(LedgerRequestParams.builder()
.ledgerSpecifier(LedgerSpecifier.VALIDATED)
.transactions(true)
Expand All @@ -114,4 +118,27 @@ void getLedgerResult() throws JsonRpcClientErrorException {
assertThat(ledgerResult.ledger().closeTimeHuman()).isNotEmpty();
assertThat(ledgerResult.ledger().parentCloseTime()).isNotEmpty();
}

/**
* This test ensures that <a href="https://github.com/XRPLF/xrpl4j/issues/288">xrpl4j-288</a> has been fixed.
* This issue states that {@link LedgerResult}s which contain {@link org.xrpl.xrpl4j.model.transactions.UnlModify}
* transactions fail to get deserialized and therefore calls to {@link XrplClient#ledger(LedgerRequestParams)}
* fail. This test queries the ledgers found in the issue make sure xrpl4j can deserialize those ledgers.
*/
@Test
void getLedgerResultWithTransactionsForLedgerWithUnlModify() throws JsonRpcClientErrorException {
LedgerResult ledgerResult = mainnetClient.ledger(
LedgerRequestParams.builder()
.ledgerSpecifier(LedgerSpecifier.of(73151744))
.transactions(true)
.build()
);

Optional<? extends Transaction> foundUnlModify = ledgerResult.ledger().transactions().stream()
.map(TransactionResult::transaction)
.filter(transaction -> UnlModify.class.isAssignableFrom(transaction.getClass()))
.findFirst();

assertThat(foundUnlModify).isNotEmpty().get().extracting("account").isEqualTo(UnlModify.ACCOUNT_ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
@JsonDeserialize(as = ImmutableUnlModify.class)
public interface UnlModify extends Transaction {

Address ACCOUNT_ZERO = Address.of("rrrrrrrrrrrrrrrrrrrrrhoLvTp");

/**
* Construct a builder for this class.
*
Expand All @@ -28,6 +30,23 @@ static ImmutableUnlModify.Builder builder() {
}


/**
* This field is overridden in this class because of a bug in rippled that causes this field to be missing
* in API responses. In other pseudo-transactions such as {@link SetFee} and {@link EnableAmendment}, the rippled
* API sets the {@code account} field to a special XRPL address called ACCOUNT_ZERO, which is the base58
* encoding of the number zero. Because rippled does not set the {@code account} field of the {@link UnlModify}
* pseudo-transaction, this override will always set the field to ACCOUNT_ZERO to avoid deserialization issues
* and to be consistent with other pseudo-transactions.
*
* @return Always returns ACCOUNT_ZERO, which is the base58 encoding of the number zero.
*/
@Override
@JsonProperty("Account")
@Value.Derived
default Address account() {
return ACCOUNT_ZERO;
}

/**
* The {@link org.xrpl.xrpl4j.model.client.common.LedgerIndex} where this pseudo-transaction appears.
* This distinguishes the pseudo-transaction from other occurrences of the same change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class UnlModifyTest {
public void testBuilder() {
String validator = "EDB6FC8E803EE8EDC2793F1EC917B2EE41D35255618DEB91D3F9B1FC89B75D4539";
UnlModify unlModify = UnlModify.builder()
.account(Address.of("rrrrrrrrrrrrrrrrrrrrrhoLvTp"))
.fee(XrpCurrencyAmount.ofDrops(12))
.sequence(UnsignedInteger.valueOf(2470665))
.signingPublicKey("")
Expand All @@ -23,7 +22,7 @@ public void testBuilder() {
.build();

assertThat(unlModify.transactionType()).isEqualTo(TransactionType.UNL_MODIFY);
assertThat(unlModify.account()).isEqualTo(Address.of("rrrrrrrrrrrrrrrrrrrrrhoLvTp"));
assertThat(unlModify.account()).isEqualTo(UnlModify.ACCOUNT_ZERO);
assertThat(unlModify.fee().value()).isEqualTo(UnsignedLong.valueOf(12));
assertThat(unlModify.sequence()).isEqualTo(UnsignedInteger.valueOf(2470665));
assertThat(unlModify.ledgerSequence()).isEqualTo(LedgerIndex.of(UnsignedInteger.valueOf(67850752)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class UnlModifyJsonTests extends AbstractJsonTest {
@Test
public void testJson() throws JsonProcessingException, JSONException {
UnlModify unlModify = UnlModify.builder()
.account(Address.of("rrrrrrrrrrrrrrrrrrrrrhoLvTp"))
.fee(XrpCurrencyAmount.ofDrops(12))
.sequence(UnsignedInteger.valueOf(2470665))
.signingPublicKey("")
Expand All @@ -25,7 +24,7 @@ public void testJson() throws JsonProcessingException, JSONException {
.build();

String json = "{" +
"\"Account\":\"rrrrrrrrrrrrrrrrrrrrrhoLvTp\"," +
"\"Account\":\"" + UnlModify.ACCOUNT_ZERO + "\"," +
"\"Fee\":\"12\"," +
"\"LedgerSequence\":67850752," +
"\"Sequence\":2470665," +
Expand Down

0 comments on commit 9dcbbfa

Please sign in to comment.