AccountsManager { public void transferFunds(Accounts accounts, String senderAddress, String receiverAddress, BigInteger value, BigInteger nonce, ShardOperation operation) throws IOException, ClassNotFoundException { logger.traceEntry("params: {} {} {} {} {}", accounts, senderAddress, receiverAddress, value, nonce); Util.check(accounts != null, "accounts!=null"); Util.check(!(senderAddress == null || senderAddress.isEmpty()), "senderAddress!=null"); Util.check(!(receiverAddress == null || receiverAddress.isEmpty()), "receiverAddress!=null"); Util.check(value.compareTo(BigInteger.ZERO) >= 0, "value>=0"); Util.check(nonce.compareTo(BigInteger.ZERO) >= 0, "nonce>=0"); if (operation.isCheckSource()) { if (!(hasFunds(accounts, senderAddress, value) && hasCorrectNonce(accounts, senderAddress, nonce))) { IllegalArgumentException ex = new IllegalArgumentException("Validation of Sender Account failed!"); logger.throwing(ex); throw ex; } } if (operation.isCheckTarget()) { AccountAddress receiver = AccountAddress.fromHexString(receiverAddress); AccountState receiverAccountState = AppServiceProvider.getAccountStateService().getOrCreateAccountState(receiver, accounts); logger.trace("Transfer asset > adding"); receiverAccountState.setBalance(receiverAccountState.getBalance().add(value)); AppServiceProvider.getAccountStateService().setAccountState(receiver, receiverAccountState, accounts); } if (operation.isCheckSource()) { AccountAddress sender = AccountAddress.fromHexString(senderAddress); AccountState senderAccountState = AppServiceProvider.getAccountStateService().getOrCreateAccountState(sender, accounts); logger.trace("Transfer asset > substracting"); senderAccountState.setBalance(senderAccountState.getBalance().subtract(value)); logger.trace("Transfer asset > increasing sender nonce"); senderAccountState.setNonce(senderAccountState.getNonce().add(BigInteger.ONE)); logger.trace("Transfer asset > saving"); AppServiceProvider.getAccountStateService().setAccountState(sender, senderAccountState, accounts); } logger.traceExit(); }  static AccountsManager instance(); Boolean hasFunds(Accounts accounts, String addressString, BigInteger value); Boolean hasCorrectNonce(Accounts accounts, String addressString, BigInteger nonce); void transferFunds(Accounts accounts, String senderAddress, String receiverAddress, BigInteger value, BigInteger nonce, ShardOperation operation);  }
@Test public void testTransferFundsWithNullAccountsShouldThrowException() throws IOException, ClassNotFoundException { expected(IllegalArgumentException.class, "accounts!=null"); accountsManager.transferFunds(null, "Sender", "Receiver", BigInteger.TEN, BigInteger.TEN, operation); }
@Test public void testTransferFundsWithNullSenderAddressShouldThrowException() throws IOException, ClassNotFoundException { expected(IllegalArgumentException.class, "senderAddress!=null"); accountsManager.transferFunds(accounts, null, "Receiver", BigInteger.TEN, BigInteger.TEN, operation); }
@Test public void testTransferFundsWithNullReceiverAddressShouldThrowException() throws IOException, ClassNotFoundException { expected(IllegalArgumentException.class, "receiverAddress!=null"); accountsManager.transferFunds(accounts, "Sender", "", BigInteger.TEN, BigInteger.TEN, operation); }
@Test public void testTransferFundsWithNegativeValueShouldThrowException() throws IOException, ClassNotFoundException { expected(IllegalArgumentException.class, "value>=0"); accountsManager.transferFunds(accounts, "Sender", "Receiver", BigInteger.valueOf(-1), BigInteger.TEN, operation); }
@Test public void testTransferFundsWithNegativeNonceShouldThrowException() throws IOException, ClassNotFoundException { expected(IllegalArgumentException.class, "nonce>=0"); accountsManager.transferFunds(accounts, "Sender", "Receiver", BigInteger.TEN, BigInteger.valueOf(-1), operation); }
@Test public void testTransferFunds() throws IOException, ClassNotFoundException { AccountAddress senderAddress = AccountAddress.fromHexString("Sender"); AccountState senderAccountState = AppServiceProvider.getAccountStateService().getOrCreateAccountState(senderAddress, accounts); AccountAddress receiverAddress = AccountAddress.fromHexString("Receiver"); senderAccountState.setBalance(BigInteger.TEN); AppServiceProvider.getAccountStateService().setAccountState(senderAddress, senderAccountState, accounts); accountsManager.transferFunds(accounts, "Sender", "Receiver", BigInteger.ONE, BigInteger.ZERO, operation); senderAccountState = AppServiceProvider.getAccountStateService().getOrCreateAccountState(senderAddress, accounts); Assert.assertTrue(senderAccountState.getBalance().longValue() == 9); AccountState receiverAccountState = AppServiceProvider.getAccountStateService().getOrCreateAccountState(receiverAddress, accounts); Assert.assertTrue(receiverAccountState.getBalance().longValue() == 1); Assert.assertTrue(senderAccountState.getNonce().longValue() == 1); }