From e4ba715568c400163b9579319560aa064cdce2a0 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Thu, 5 Sep 2024 14:52:24 +0200 Subject: [PATCH] fixup --- vochain/account_test.go | 40 +++++++++++++++++++++++++++++- vochain/transaction/transaction.go | 6 ++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/vochain/account_test.go b/vochain/account_test.go index 52d87dbd1..3cb155959 100644 --- a/vochain/account_test.go +++ b/vochain/account_test.go @@ -74,7 +74,7 @@ func setupTestBaseApplicationAndSigners(t *testing.T, } func TestSetAccountTx(t *testing.T) { - app, signers, err := setupTestBaseApplicationAndSigners(t, 10) + app, signers, err := setupTestBaseApplicationAndSigners(t, 11) qt.Assert(t, err, qt.IsNil) // set account 0 qt.Assert(t, @@ -578,6 +578,44 @@ func TestSetAccountTx(t *testing.T) { qt.Assert(t, signer9Account.Balance, qt.DeepEquals, uint64(21)) qt.Assert(t, signer9Account.Nonce, qt.DeepEquals, uint32(2)) qt.Assert(t, signer9Account.InfoURI, qt.CmpEquals(), infoURI4) + + // now we init signers[10] with 1000 tokens from faucet (signers[0]) + // resulting balance should be 800 (because of the 200 txcost of createAccount) + // then "transfer" 700 of those to signers[9] via a faucet package issued by signers[10] + // used to setAccount + faucetPkg, err = GenerateFaucetPackage(signers[0], signers[10].Address(), 1000) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, testSetAccountTx(t, + signers[10], signers[10].Address(), faucetPkg, app, "", uint32(0), true), + qt.IsNil, + ) + signer10Account, err := app.State.GetAccount(signers[10].Address(), false) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, signer10Account.Balance, qt.DeepEquals, uint64(1000-200)) + + signer9Account, err = app.State.GetAccount(signers[9].Address(), false) + qt.Assert(t, err, qt.IsNil) + oldBalance := signer9Account.Balance + + faucetPkg, err = GenerateFaucetPackage(signers[10], signers[9].Address(), 1000-200+1) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, testSetAccountTx(t, + signers[9], signers[9].Address(), faucetPkg, app, "ipfs://test", uint32(2), false), + qt.ErrorMatches, regexp.MustCompile(".*issuer address does not have enough balance.*"), + ) + + faucetPkg, err = GenerateFaucetPackage(signers[10], signers[9].Address(), 1000-200-50) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, testSetAccountTx(t, + signers[9], signers[9].Address(), faucetPkg, app, "ipfs://test", uint32(2), false), + qt.IsNil, + ) + signer10Account, err = app.State.GetAccount(signers[10].Address(), false) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, signer10Account.Balance, qt.DeepEquals, uint64(50)) + signer9Account, err = app.State.GetAccount(signers[9].Address(), false) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, signer9Account.Balance, qt.DeepEquals, uint64(oldBalance+1000-200-50-200)) } func testSetAccountTx(t *testing.T, diff --git a/vochain/transaction/transaction.go b/vochain/transaction/transaction.go index 783ab3bfc..f8884ef0c 100644 --- a/vochain/transaction/transaction.go +++ b/vochain/transaction/transaction.go @@ -612,7 +612,7 @@ func (t *TransactionHandler) checkAccountCanPayCost(txType models.TxType, vtx *v return txSenderAcc, &txSenderAddress, nil } -// checkFaucetPackageCanPayCost checks if the txFaucetPackage is a valid Faucet package issued for txSenderAddress and the account signing the faucet package has +// checkFaucetPackageAndTransfer checks if the txFaucetPackage is a valid Faucet package issued for txSenderAddress and the account signing the faucet package has // enough funds for paying for the txCost. // If forCommit is true, the faucet package balance is transferred to the txSender account. // Returns false and no error if the faucet package does not exist. If it exists but there is an issue, returns false and the error. @@ -661,11 +661,11 @@ func (t *TransactionHandler) checkFaucetPackageAndTransfer(txFaucetPackage *mode if used { return false, fmt.Errorf("faucet payload %x already used", keyHash) } - if issuerAcc.Balance < faucetPayload.Amount+txCost { + if issuerAcc.Balance < faucetPayload.Amount { return false, fmt.Errorf( "issuer address does not have enough balance %d, required %d", issuerAcc.Balance, - faucetPayload.Amount+txCost, + faucetPayload.Amount, ) }