Skip to content

Commit

Permalink
Fix TransactionCheck bug and optimize performence
Browse files Browse the repository at this point in the history
Signed-off-by: gdmmx <[email protected]>
  • Loading branch information
gdmmx committed Nov 16, 2018
1 parent 37dfdfc commit 7e72efb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
6 changes: 3 additions & 3 deletions core/ledger/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func TransactionCheck(block *Block) error {
if errCode := tx.VerifyTransaction(txn); errCode != ErrNoError {
return errors.New("transaction sanity check failed")
}
if errCode := tx.VerifyTransactionWithBlock(TransactionArray(block.Transactions)); errCode != ErrNoError {
return errors.New("transaction block check failed")
}
if errCode := tx.VerifyTransactionWithLedger(txn); errCode != ErrNoError {
return errors.New("transaction history check failed")
}
}
if errCode := tx.VerifyTransactionWithBlock(TransactionArray(block.Transactions)); errCode != ErrNoError {
return errors.New("transaction block check failed")
}

return nil
}
Expand Down
15 changes: 10 additions & 5 deletions core/transaction/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ func (tp *TxnPool) AppendTxnPool(txn *Transaction) ErrCode {
log.Info("Transaction verification failed", txn.Hash())
return errCode
}
if errCode := VerifyTransactionWithBlock(TransactionMap(tp.txnList)); errCode != ErrNoError {
log.Info("Transaction verification with block failed", txn.Hash())
return errCode
}
if errCode := VerifyTransactionWithLedger(txn); errCode != ErrNoError {
log.Info("Transaction verification with ledger failed", txn.Hash())
return errCode
Expand All @@ -72,7 +68,16 @@ func (tp *TxnPool) AppendTxnPool(txn *Transaction) ErrCode {
}

//add the transaction to process scope
tp.addtxnList(txn)
if tp.addtxnList(txn) {
// Check duplicate UTXO reference after append successful
if errCode := VerifyTransactionWithBlock(TransactionMap(tp.txnList)); errCode != ErrNoError {
log.Info("Transaction verification with block failed", txn.Hash())
tp.deltxnList(txn) // Revert previous append action
return errCode
}
} else {
return ErrDuplicatedTx // Don't broadcast this txn if hash duplicated
}

return ErrNoError
}
Expand Down
14 changes: 6 additions & 8 deletions core/transaction/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,13 @@ func VerifyTransactionWithLedger(Tx *Transaction) ErrCode {

//validate the transaction of duplicate UTXO input
func CheckDuplicateInput(tx *Transaction) error {
if len(tx.Inputs) == 0 {
return nil
}
for i, utxoin := range tx.Inputs {
for j := 0; j < i; j++ {
if utxoin.ReferTxID == tx.Inputs[j].ReferTxID && utxoin.ReferTxOutputIndex == tx.Inputs[j].ReferTxOutputIndex {
return errors.New("invalid transaction")
}
dupMap := make(map[string]struct{})
for _, utxoin := range tx.Inputs {
k := utxoin.ToString()
if _, ok := dupMap[k]; ok { // ok means duplicate
return errors.New("invalid transaction")
}
dupMap[k] = struct{}{}
}
return nil
}
Expand Down
6 changes: 1 addition & 5 deletions db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,7 @@ func (cs *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block) (uint
func (cs *ChainStore) IsTxHashDuplicate(txhash Uint256) bool {
prefix := []byte{byte(DATA_Transaction)}
_, err_get := cs.st.Get(append(prefix, txhash.ToArray()...))
if err_get != nil {
return false
} else {
return true
}
return err_get == nil
}

func (cs *ChainStore) IsDoubleSpend(tx *tx.Transaction) bool {
Expand Down

0 comments on commit 7e72efb

Please sign in to comment.