Skip to content

Commit

Permalink
Check amount and fee range when checking payload
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Apr 1, 2020
1 parent 541763c commit 8c03d0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
1 change: 0 additions & 1 deletion chain/store/stateGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func (cs *ChainStore) spendTransaction(states *StateDB, txn *transaction.Transac

func (cs *ChainStore) GenerateStateRoot(ctx context.Context, b *block.Block, genesisBlockInitialized, needBeCommitted bool) (common.Uint256, error) {
_, root, err := cs.generateStateRoot(ctx, b, genesisBlockInitialized, needBeCommitted)

return root, err
}

Expand Down
46 changes: 20 additions & 26 deletions chain/txValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"
"regexp"
"sync"

Expand Down Expand Up @@ -33,8 +32,8 @@ func VerifyTransaction(txn *transaction.Transaction, height uint32) error {
return fmt.Errorf("[VerifyTransaction] %v", err)
}

if err := CheckTransactionFee(txn); err != nil {
return fmt.Errorf("[VerifyTransaction] %v", err)
if err := CheckAmount(txn.UnsignedTx.Fee); err != nil {
return fmt.Errorf("[VerifyTransaction] fee %v", err)
}

if err := CheckTransactionNonce(txn); err != nil {
Expand Down Expand Up @@ -65,13 +64,13 @@ func CheckTransactionSize(txn *transaction.Transaction) error {
return nil
}

func CheckTransactionFee(txn *transaction.Transaction) error {
if checkAmountPrecise(Fixed64(txn.UnsignedTx.Fee), 8) {
return errors.New("the precision of fee is incorrect")
func CheckAmount(amount int64) error {
if amount < 0 {
return fmt.Errorf("amount %d is less than 0", amount)
}

if txn.UnsignedTx.Fee < 0 {
return errors.New("tx fee should be greater than 0")
if amount > config.InitialIssueAmount+config.TotalMiningRewards {
return fmt.Errorf("amount %d is greater than max supply", amount)
}

return nil
Expand All @@ -89,10 +88,6 @@ func CheckTransactionAttribute(txn *transaction.Transaction) error {
return nil
}

func checkAmountPrecise(amount Fixed64, precision byte) bool {
return amount.GetData()%int64(math.Pow(10, 8-float64(precision))) != 0
}

func verifyPubSubTopic(topic string, height uint32) error {
regexPattern := config.AllowSubscribeTopicRegex.GetValueAtHeight(height)
match, err := regexp.MatchString(regexPattern, topic)
Expand Down Expand Up @@ -124,8 +119,8 @@ func CheckTransactionPayload(txn *transaction.Transaction, height uint32) error
return errors.New("invalid sender")
}

if checkAmountPrecise(Fixed64(pld.Amount), 8) {
return errors.New("the precision of amount is incorrect")
if err = CheckAmount(pld.Amount); err != nil {
return err
}
case pb.TRANSFER_ASSET_TYPE:
pld := payload.(*pb.TransferAsset)
Expand All @@ -138,12 +133,8 @@ func CheckTransactionPayload(txn *transaction.Transaction, height uint32) error
return errors.New("illegal transaction sender")
}

if checkAmountPrecise(Fixed64(pld.Amount), 8) {
return errors.New("the precision of amount is incorrect")
}

if pld.Amount < 0 {
return errors.New("transfer amount error")
if err = CheckAmount(pld.Amount); err != nil {
return err
}
case pb.SIG_CHAIN_TXN_TYPE:
case pb.REGISTER_NAME_TYPE:
Expand All @@ -153,6 +144,9 @@ func CheckTransactionPayload(txn *transaction.Transaction, height uint32) error

pld := payload.(*pb.RegisterName)
if !config.LegacyNameService.GetValueAtHeight(height) {
if err = CheckAmount(pld.RegistrationFee); err != nil {
return err
}
if Fixed64(pld.RegistrationFee) < Fixed64(config.MinNameRegistrationFee) {
return fmt.Errorf("registration fee %s is lower than MinNameRegistrationFee %d", string(pld.Registrant), config.MinNameRegistrationFee)
}
Expand Down Expand Up @@ -218,6 +212,10 @@ func CheckTransactionPayload(txn *transaction.Transaction, height uint32) error
return fmt.Errorf("decode pubkey error: %v", err)
}

if err = CheckAmount(pld.RegistrationFee); err != nil {
return err
}

if Fixed64(pld.RegistrationFee) < Fixed64(config.MinGenIDRegistrationFee) {
return errors.New("registration fee is lower than MinGenIDRegistrationFee")
}
Expand All @@ -238,12 +236,8 @@ func CheckTransactionPayload(txn *transaction.Transaction, height uint32) error
return errors.New("illegal transaction sender")
}

if checkAmountPrecise(Fixed64(pld.Amount), 8) {
return errors.New("The precision of amount is incorrect")
}

if pld.Amount < 0 {
return errors.New("transfer amount error")
if err = CheckAmount(pld.Amount); err != nil {
return err
}

if pld.TxnExpiration > pld.NanoPayExpiration {
Expand Down

0 comments on commit 8c03d0f

Please sign in to comment.