Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EN-6798/tx-integrity-checks #1970

Merged
merged 5 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions process/economics/economicsData.go
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
bogdan-rosianu marked this conversation as resolved.
Show resolved Hide resolved
}

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
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
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
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have wrapped the errors to know which field was bad

}
if len(inTx.tx.SndUserName) > 0 && len(inTx.tx.SndUserName) != inTx.hasher.Size() {
bogdan-rosianu marked this conversation as resolved.
Show resolved Hide resolved
return process.ErrInvalidUserNameLength
}

return inTx.feeHandler.CheckValidityTxValues(inTx.tx)
}
Expand Down
32 changes: 32 additions & 0 deletions process/transaction/interceptedTransaction_test.go
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