Skip to content

Commit

Permalink
- fix: not allow to send tokens with "to" and "from" address equals (#…
Browse files Browse the repository at this point in the history
…1186)

- update genesis
  • Loading branch information
mariajdab authored Nov 8, 2023
1 parent a2ab04f commit 524ea38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
27 changes: 27 additions & 0 deletions vochain/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions vochain/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions vochain/transaction/tokens_tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transaction

import (
"bytes"
"encoding/binary"
"fmt"

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 524ea38

Please sign in to comment.