From a75c46b64613e898b3090ac2677ea4e0840842b1 Mon Sep 17 00:00:00 2001 From: Michael Tinker Date: Thu, 7 Dec 2023 16:40:08 -0600 Subject: [PATCH] Make transferTo() a no-op on same from/to accounts Signed-off-by: Michael Tinker --- .../token/impl/api/TokenServiceApiImpl.java | 13 ++++++++----- .../impl/test/api/TokenServiceApiImplTest.java | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/api/TokenServiceApiImpl.java b/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/api/TokenServiceApiImpl.java index 9a6685fef1b0..cfd5af2d234e 100644 --- a/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/api/TokenServiceApiImpl.java +++ b/hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/api/TokenServiceApiImpl.java @@ -274,11 +274,14 @@ public void transferFromTo(@NonNull AccountID fromId, @NonNull AccountID toId, l throw new IllegalArgumentException( "Overflow on transfer of " + amount + " tinybars from " + fromId + " to " + toId); } - accountStore.put(from.copyBuilder() - .tinybarBalance(from.tinybarBalance() - amount) - .build()); - accountStore.put( - to.copyBuilder().tinybarBalance(to.tinybarBalance() + amount).build()); + if (!from.accountIdOrThrow().equals(to.accountIdOrThrow())) { + accountStore.put(from.copyBuilder() + .tinybarBalance(from.tinybarBalance() - amount) + .build()); + accountStore.put(to.copyBuilder() + .tinybarBalance(to.tinybarBalance() + amount) + .build()); + } } @Override diff --git a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/api/TokenServiceApiImplTest.java b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/api/TokenServiceApiImplTest.java index 32578353f97f..19f025a06ef0 100644 --- a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/api/TokenServiceApiImplTest.java +++ b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/api/TokenServiceApiImplTest.java @@ -322,6 +322,20 @@ void transfersRequestedValue() { assertEquals(23L, postTransferReceiver.tinybarBalance()); } + @Test + void identicalFromToIsNoop() { + accountStore.put(Account.newBuilder() + .accountId(CONTRACT_ACCOUNT_ID) + .tinybarBalance(123L) + .smartContract(true) + .build()); + + subject.transferFromTo(CONTRACT_ACCOUNT_ID, CONTRACT_ACCOUNT_ID, 23L); + + final var postTransfer = requireNonNull(accountState.get(CONTRACT_ACCOUNT_ID)); + assertEquals(123L, postTransfer.tinybarBalance()); + } + @Test void refusesToTransferNegativeAmount() { assertThrows(