Skip to content

Commit

Permalink
Merge pull request #1970 from ElrondNetwork/EN-6798/tx-integrity-checks
Browse files Browse the repository at this point in the history
EN-6798/tx-integrity-checks
  • Loading branch information
LucianMincu committed Jun 17, 2020
2 parents 5fbaf1a + 3f9addb commit c65203c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
8 changes: 8 additions & 0 deletions process/economics/economicsData.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ func (ed *EconomicsData) CheckValidityTxValues(tx process.TransactionWithFeeHand
return process.ErrHigherGasLimitRequiredInTx
}

if len(tx.GetValue().Bytes()) > len(ed.genesisTotalSupply.Bytes()) {
return process.ErrTxValueOutOfBounds
}

if tx.GetValue().Cmp(ed.genesisTotalSupply) > 0 {
return process.ErrTxValueTooBig
}

return nil
}

Expand Down
32 changes: 32 additions & 0 deletions process/economics/economicsData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestEconomicsData_ComputeFeeNoTxData(t *testing.T) {
tx := &transaction.Transaction{
GasPrice: gasPrice,
GasLimit: minGasLimit,
Value: big.NewInt(0),
}

cost := economicsData.ComputeFee(tx)
Expand All @@ -184,6 +185,7 @@ func TestEconomicsData_ComputeFeeWithTxData(t *testing.T) {
GasPrice: gasPrice,
GasLimit: minGasLimit,
Data: []byte(txData),
Value: big.NewInt(0),
}

cost := economicsData.ComputeFee(tx)
Expand All @@ -207,6 +209,7 @@ func TestEconomicsData_TxWithLowerGasPriceShouldErr(t *testing.T) {
tx := &transaction.Transaction{
GasPrice: minGasPrice - 1,
GasLimit: minGasLimit,
Value: big.NewInt(0),
}

err := economicsData.CheckValidityTxValues(tx)
Expand All @@ -226,6 +229,7 @@ func TestEconomicsData_TxWithLowerGasLimitShouldErr(t *testing.T) {
tx := &transaction.Transaction{
GasPrice: minGasPrice,
GasLimit: minGasLimit - 1,
Value: big.NewInt(0),
}

err := economicsData.CheckValidityTxValues(tx)
Expand All @@ -248,6 +252,7 @@ func TestEconomicsData_TxWithHigherGasLimitShouldErr(t *testing.T) {
GasPrice: minGasPrice,
GasLimit: minGasLimit + 1,
Data: []byte("1"),
Value: big.NewInt(0),
}

err := economicsData.CheckValidityTxValues(tx)
Expand All @@ -269,6 +274,7 @@ func TestEconomicsData_TxWithWithEqualGasPriceLimitShouldWork(t *testing.T) {
tx := &transaction.Transaction{
GasPrice: minGasPrice,
GasLimit: minGasLimit,
Value: big.NewInt(0),
}

err := economicsData.CheckValidityTxValues(tx)
Expand All @@ -290,9 +296,35 @@ func TestEconomicsData_TxWithWithMoreGasPriceLimitShouldWork(t *testing.T) {
tx := &transaction.Transaction{
GasPrice: minGasPrice + 1,
GasLimit: minGasLimit + 1,
Value: big.NewInt(0),
}

err := economicsData.CheckValidityTxValues(tx)

assert.Nil(t, err)
}

func TestEconomicsData_TxWithWithMoreValueThanGenesisSupplyShouldError(t *testing.T) {
t.Parallel()

minGasPrice := uint64(500)
minGasLimit := uint64(12)
maxGasLimitPerBlock := minGasLimit + 1
economicsConfig := createDummyEconomicsConfig()
economicsConfig.FeeSettings.MaxGasLimitPerBlock = fmt.Sprintf("%d", maxGasLimitPerBlock)
economicsConfig.FeeSettings.MinGasPrice = fmt.Sprintf("%d", minGasPrice)
economicsConfig.FeeSettings.MinGasLimit = fmt.Sprintf("%d", minGasLimit)
economicsData, _ := economics.NewEconomicsData(economicsConfig)
tx := &transaction.Transaction{
GasPrice: minGasPrice + 1,
GasLimit: minGasLimit + 1,
Value: big.NewInt(0).Add(economicsData.GenesisTotalSupply(), big.NewInt(1)),
}

err := economicsData.CheckValidityTxValues(tx)
assert.Equal(t, err, process.ErrTxValueTooBig)

tx.Value.SetBytes([]byte("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999"))
err = economicsData.CheckValidityTxValues(tx)
assert.Equal(t, err, process.ErrTxValueOutOfBounds)
}
9 changes: 9 additions & 0 deletions process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,12 @@ var ErrNilInterceptorContainer = errors.New("nil interceptor container")

// ErrInvalidChainID signals that an invalid chain ID has been provided
var ErrInvalidChainID = errors.New("invalid chain ID")

// ErrTxValueTooBig signals that transaction value is too big
var ErrTxValueTooBig = errors.New("tx value is too big")

// ErrInvalidUserNameLength signals that provided user name length is invalid
var ErrInvalidUserNameLength = errors.New("invalid user name length")

// ErrTxValueOutOfBounds signals that transaction value is out of bounds
var ErrTxValueOutOfBounds = errors.New("tx value is out of bounds")
1 change: 1 addition & 0 deletions process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ type TransactionWithFeeHandler interface {
GetGasPrice() uint64
GetData() []byte
GetRcvAddr() []byte
GetValue() *big.Int
}

// EconomicsAddressesHandler will return information about economics addresses
Expand Down
6 changes: 6 additions & 0 deletions process/transaction/interceptedTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ func (inTx *InterceptedTransaction) integrity() error {
if inTx.tx.Value.Sign() < 0 {
return process.ErrNegativeValue
}
if len(inTx.tx.RcvUserName) > 0 && len(inTx.tx.RcvUserName) != inTx.hasher.Size() {
return process.ErrInvalidUserNameLength
}
if len(inTx.tx.SndUserName) > 0 && len(inTx.tx.SndUserName) != inTx.hasher.Size() {
return process.ErrInvalidUserNameLength
}

return inTx.feeHandler.CheckValidityTxValues(inTx.tx)
}
Expand Down
32 changes: 32 additions & 0 deletions process/transaction/interceptedTransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,38 @@ func TestInterceptedTransaction_CheckValidityNilValueShouldErr(t *testing.T) {
assert.Equal(t, process.ErrNilValue, err)
}

func TestInterceptedTransaction_CheckValidityInvalidUserNameLength(t *testing.T) {
t.Parallel()

tx := &dataTransaction.Transaction{
Nonce: 1,
Value: big.NewInt(2),
Data: []byte("data"),
GasLimit: 3,
GasPrice: 4,
RcvAddr: recvAddress,
SndAddr: senderAddress,
Signature: sigOk,
RcvUserName: []byte("username"),
}
txi, _ := createInterceptedTxFromPlainTx(tx, createFreeTxFeeHandler())

err := txi.CheckValidity()
assert.Equal(t, process.ErrInvalidUserNameLength, err)

tx.RcvUserName = nil
tx.SndUserName = []byte("username")
txi, _ = createInterceptedTxFromPlainTx(tx, createFreeTxFeeHandler())
err = txi.CheckValidity()
assert.Equal(t, process.ErrInvalidUserNameLength, err)

tx.RcvUserName = []byte("12345678901234567890123456789012")
tx.SndUserName = []byte("12345678901234567890123456789012")
txi, _ = createInterceptedTxFromPlainTx(tx, createFreeTxFeeHandler())
err = txi.CheckValidity()
assert.Nil(t, err)
}

func TestInterceptedTransaction_CheckValidityNilNegativeValueShouldErr(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c65203c

Please sign in to comment.