|
| 1 | +// Copyright (c) 2025 The VeChainThor developers |
| 2 | + |
| 3 | +// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying |
| 4 | +// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> |
| 5 | + |
| 6 | +package comm |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "math/big" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/ethereum/go-ethereum/rlp" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "github.com/vechain/thor/v2/comm/proto" |
| 19 | + "github.com/vechain/thor/v2/genesis" |
| 20 | + "github.com/vechain/thor/v2/p2p" |
| 21 | + "github.com/vechain/thor/v2/p2p/discover" |
| 22 | + "github.com/vechain/thor/v2/test/testchain" |
| 23 | + "github.com/vechain/thor/v2/thor" |
| 24 | + "github.com/vechain/thor/v2/tx" |
| 25 | + "github.com/vechain/thor/v2/txpool" |
| 26 | +) |
| 27 | + |
| 28 | +func TestHandleRPC_MsgNewTx(t *testing.T) { |
| 29 | + chain, err := testchain.NewWithFork(&thor.SoloFork, 180) |
| 30 | + require.NoError(t, err) |
| 31 | + repo := chain.Repo() |
| 32 | + |
| 33 | + pool := txpool.New(repo, chain.Stater(), txpool.Options{ |
| 34 | + Limit: 10000, |
| 35 | + LimitPerAccount: 16, |
| 36 | + MaxLifetime: 10 * time.Minute, |
| 37 | + }, &thor.SoloFork) |
| 38 | + defer pool.Close() |
| 39 | + |
| 40 | + comm := New(repo, pool) |
| 41 | + peer := newPeer(p2p.NewPeer(discover.NodeID{}, "test", nil), stubMsgReadWriter{}) |
| 42 | + |
| 43 | + to, _ := thor.ParseAddress("0x7567d83b7b8d80addcb281a71d54fc7b3364ffed") |
| 44 | + chainTag := repo.ChainTag() |
| 45 | + testTx := tx.NewBuilder(tx.TypeLegacy). |
| 46 | + ChainTag(chainTag). |
| 47 | + BlockRef(tx.NewBlockRef(0)). |
| 48 | + Expiration(100). |
| 49 | + GasPriceCoef(0). |
| 50 | + Gas(21000). |
| 51 | + Nonce(1). |
| 52 | + Clause(tx.NewClause(&to).WithValue(big.NewInt(1))). |
| 53 | + Build() |
| 54 | + testTx = tx.MustSign(testTx, genesis.DevAccounts()[0].PrivateKey) |
| 55 | + txHash := testTx.Hash() |
| 56 | + txID := testTx.ID() |
| 57 | + |
| 58 | + txData, err := rlp.EncodeToBytes(testTx) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + t.Run("valid transaction", func(t *testing.T) { |
| 62 | + writeCalled := false |
| 63 | + var writtenData any |
| 64 | + |
| 65 | + write := func(data any) { |
| 66 | + writeCalled = true |
| 67 | + writtenData = data |
| 68 | + } |
| 69 | + |
| 70 | + msg := &p2p.Msg{ |
| 71 | + Code: proto.MsgNewTx, |
| 72 | + Size: uint32(len(txData)), |
| 73 | + Payload: bytes.NewReader(txData), |
| 74 | + } |
| 75 | + |
| 76 | + txsToSync := &txsToSync{} |
| 77 | + |
| 78 | + err := comm.handleRPC(peer, msg, write, txsToSync) |
| 79 | + assert.NoError(t, err) |
| 80 | + |
| 81 | + assert.True(t, peer.IsTransactionKnown(txHash), "transaction should be marked on peer") |
| 82 | + |
| 83 | + addedTx := pool.Get(txID) |
| 84 | + assert.NotNil(t, addedTx, "transaction should be added to pool") |
| 85 | + if addedTx != nil { |
| 86 | + assert.Equal(t, testTx.Hash(), addedTx.Hash(), "added transaction should match") |
| 87 | + } |
| 88 | + |
| 89 | + assert.True(t, writeCalled, "write function should be called") |
| 90 | + assert.Equal(t, &struct{}{}, writtenData, "write should be called with empty struct") |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("transaction exceeds size limit", func(t *testing.T) { |
| 94 | + writeCalled := false |
| 95 | + |
| 96 | + write := func(data any) { |
| 97 | + writeCalled = true |
| 98 | + } |
| 99 | + |
| 100 | + msg := &p2p.Msg{ |
| 101 | + Code: proto.MsgNewTx, |
| 102 | + Size: maxTxSize + 1, |
| 103 | + Payload: bytes.NewReader(txData), |
| 104 | + } |
| 105 | + |
| 106 | + txsToSync := &txsToSync{} |
| 107 | + |
| 108 | + err := comm.handleRPC(peer, msg, write, txsToSync) |
| 109 | + assert.Error(t, err) |
| 110 | + assert.Contains(t, err.Error(), "payload size: exceeds limit") |
| 111 | + |
| 112 | + assert.False(t, writeCalled, "write should not be called on error") |
| 113 | + }) |
| 114 | + |
| 115 | + t.Run("decode error", func(t *testing.T) { |
| 116 | + writeCalled := false |
| 117 | + |
| 118 | + write := func(data any) { |
| 119 | + writeCalled = true |
| 120 | + } |
| 121 | + |
| 122 | + invalidData := []byte{0x01, 0x02, 0x03} |
| 123 | + msg := &p2p.Msg{ |
| 124 | + Code: proto.MsgNewTx, |
| 125 | + Size: uint32(len(invalidData)), |
| 126 | + Payload: bytes.NewReader(invalidData), |
| 127 | + } |
| 128 | + |
| 129 | + txsToSync := &txsToSync{} |
| 130 | + |
| 131 | + err := comm.handleRPC(peer, msg, write, txsToSync) |
| 132 | + assert.Error(t, err) |
| 133 | + assert.Contains(t, err.Error(), "decode msg") |
| 134 | + |
| 135 | + assert.False(t, writeCalled, "write should not be called on error") |
| 136 | + }) |
| 137 | + |
| 138 | + t.Run("transaction at size limit boundary", func(t *testing.T) { |
| 139 | + writeCalled := false |
| 140 | + var writtenData any |
| 141 | + |
| 142 | + write := func(data any) { |
| 143 | + writeCalled = true |
| 144 | + writtenData = data |
| 145 | + } |
| 146 | + |
| 147 | + msg := &p2p.Msg{ |
| 148 | + Code: proto.MsgNewTx, |
| 149 | + Size: maxTxSize, |
| 150 | + Payload: bytes.NewReader(txData), |
| 151 | + } |
| 152 | + |
| 153 | + txsToSync := &txsToSync{} |
| 154 | + |
| 155 | + err := comm.handleRPC(peer, msg, write, txsToSync) |
| 156 | + assert.NoError(t, err) |
| 157 | + |
| 158 | + assert.True(t, writeCalled, "write function should be called") |
| 159 | + assert.Equal(t, &struct{}{}, writtenData, "write should be called with empty struct") |
| 160 | + }) |
| 161 | +} |
0 commit comments