From 524ea383d86a505174acae5411e209bb9216a88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Jos=C3=A9=20D=C3=A1vila?= Date: Wed, 8 Nov 2023 12:50:50 -0400 Subject: [PATCH] - fix: not allow to send tokens with "to" and "from" address equals (#1186) - update genesis --- vochain/account_test.go | 27 +++++++++++++++++++++++++++ vochain/genesis/genesis.go | 4 ++-- vochain/transaction/tokens_tx.go | 6 ++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/vochain/account_test.go b/vochain/account_test.go index 3bf15654a..f6d1ebd76 100644 --- a/vochain/account_test.go +++ b/vochain/account_test.go @@ -594,6 +594,33 @@ func TestSendTokensTx(t *testing.T) { qt.Assert(t, fromAcc.Balance, qt.Equals, uint64(890)) } +func TestSendTokensTxToTheSameAccount(t *testing.T) { + app := TestBaseApplication(t) + + signer := ethereum.SignKeys{} + err := signer.Generate() + qt.Assert(t, err, qt.IsNil) + + app.State.SetAccount(state.BurnAddress, &state.Account{}) + + err = app.State.SetTxBaseCost(models.TxType_SEND_TOKENS, 10) + qt.Assert(t, err, qt.IsNil) + + err = app.State.CreateAccount(signer.Address(), "ipfs://", [][]byte{}, 0) + qt.Assert(t, err, qt.IsNil) + + err = app.State.MintBalance(&vochaintx.TokenTransfer{ + ToAddress: signer.Address(), + Amount: 1000, + }) + qt.Assert(t, err, qt.IsNil) + testCommitState(t, app) + + err = testSendTokensTx(t, &signer, app, signer.Address(), 89, 0) + qt.Assert(t, err, qt.IsNotNil) + +} + func testSendTokensTx(t *testing.T, signer *ethereum.SignKeys, app *BaseApplication, diff --git a/vochain/genesis/genesis.go b/vochain/genesis/genesis.go index 6ce387763..2ac884580 100644 --- a/vochain/genesis/genesis.go +++ b/vochain/genesis/genesis.go @@ -41,8 +41,8 @@ var Genesis = map[string]Vochain{ } var devGenesis = Doc{ - GenesisTime: time.Date(2023, time.October, 31, 1, 0, 0, 0, time.UTC), - ChainID: "vocdoni/DEV/28", + GenesisTime: time.Date(2023, time.November, 8, 1, 0, 0, 0, time.UTC), + ChainID: "vocdoni/DEV/29", ConsensusParams: &ConsensusParams{ Block: BlockParams{ MaxBytes: 2097152, diff --git a/vochain/transaction/tokens_tx.go b/vochain/transaction/tokens_tx.go index 352b9c6f8..263db767e 100644 --- a/vochain/transaction/tokens_tx.go +++ b/vochain/transaction/tokens_tx.go @@ -1,6 +1,7 @@ package transaction import ( + "bytes" "encoding/binary" "fmt" @@ -30,6 +31,7 @@ func (t *TransactionHandler) SendTokensTxCheck(vtx *vochaintx.Tx) error { if len(tx.To) == 0 { return fmt.Errorf("invalid to address") } + pubKey, err := ethereum.PubKeyFromSignature(vtx.SignedBody, vtx.Signature) if err != nil { return fmt.Errorf("cannot extract public key from vtx.Signature: %w", err) @@ -46,6 +48,10 @@ func (t *TransactionHandler) SendTokensTxCheck(vtx *vochaintx.Tx) error { ) } txToAddress := common.BytesToAddress(tx.To) + if bytes.Equal(txFromAddress.Bytes(), txToAddress.Bytes()) { + return fmt.Errorf("to and from address are equal") + } + toTxAccount, err := t.state.GetAccount(txToAddress, false) if err != nil { return fmt.Errorf("cannot get to account: %w", err)