[NOD-822] Don't return rule errors from utxoset code (#693)

* [NOD-822] Remove rule errors from the UTXO diff code

* [NOD-822] Rename applyTransactions -> applyAndVerifyBlockTransactionsToPastUTXO

* [NOD-822] Fix comment
This commit is contained in:
Ori Newman 2020-04-07 12:45:12 +03:00 committed by GitHub
parent 3c4a80f16d
commit df934990d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 17 deletions

View File

@ -1058,11 +1058,16 @@ func (dag *BlockDAG) meldVirtualUTXO(newVirtualUTXODiffSet *DiffUTXOSet) error {
return newVirtualUTXODiffSet.meldToBase() return newVirtualUTXODiffSet.meldToBase()
} }
func (node *blockNode) diffFromTxs(pastUTXO UTXOSet, transactions []*util.Tx) (*UTXODiff, error) { // applyAndVerifyBlockTransactionsToPastUTXO applies a block's transactions to its
// given past UTXO, and verifies that there are no double spends with its past.
func applyAndVerifyBlockTransactionsToPastUTXO(pastUTXO UTXOSet, blockTransactions []*util.Tx) (UTXOSet, error) {
diff := NewUTXODiff() diff := NewUTXODiff()
for _, tx := range transactions { for _, tx := range blockTransactions {
txDiff, err := pastUTXO.diffFromTx(tx.MsgTx(), UnacceptedBlueScore) txDiff, err := pastUTXO.diffFromTx(tx.MsgTx(), UnacceptedBlueScore)
if errors.Is(err, errUTXOMissingTxOut) {
return nil, ruleError(ErrMissingTxOut, err.Error())
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1072,7 +1077,7 @@ func (node *blockNode) diffFromTxs(pastUTXO UTXOSet, transactions []*util.Tx) (*
} }
} }
return diff, nil return pastUTXO.WithDiff(diff)
} }
// verifyAndBuildUTXO verifies all transactions in the given block and builds its UTXO // verifyAndBuildUTXO verifies all transactions in the given block and builds its UTXO
@ -1096,11 +1101,7 @@ func (node *blockNode) verifyAndBuildUTXO(dag *BlockDAG, transactions []*util.Tx
return nil, nil, nil, nil, err return nil, nil, nil, nil, err
} }
diffFromTxs, err := node.diffFromTxs(pastUTXO, transactions) utxo, err := applyAndVerifyBlockTransactionsToPastUTXO(pastUTXO, transactions)
if err != nil {
return nil, nil, nil, nil, err
}
utxo, err := pastUTXO.WithDiff(diffFromTxs)
if err != nil { if err != nil {
return nil, nil, nil, nil, err return nil, nil, nil, nil, err
} }

View File

@ -293,8 +293,8 @@ func (d *UTXODiff) withDiffInPlace(diff *UTXODiff) error {
} }
if d.toRemove.contains(outpoint) { if d.toRemove.contains(outpoint) {
// If already exists - this is an error // If already exists - this is an error
return ruleError(ErrWithDiff, fmt.Sprintf( return errors.Errorf(
"withDiffInPlace: outpoint %s both in d.toRemove and in diff.toRemove", outpoint)) "withDiffInPlace: outpoint %s both in d.toRemove and in diff.toRemove", outpoint)
} }
// If not exists neither in toAdd nor in toRemove - add to toRemove // If not exists neither in toAdd nor in toRemove - add to toRemove
@ -305,9 +305,9 @@ func (d *UTXODiff) withDiffInPlace(diff *UTXODiff) error {
if d.toRemove.containsWithBlueScore(outpoint, entryToAdd.blockBlueScore) { if d.toRemove.containsWithBlueScore(outpoint, entryToAdd.blockBlueScore) {
// If already exists in toRemove with the same blueScore - remove from toRemove // If already exists in toRemove with the same blueScore - remove from toRemove
if d.toAdd.contains(outpoint) && !diff.toRemove.contains(outpoint) { if d.toAdd.contains(outpoint) && !diff.toRemove.contains(outpoint) {
return ruleError(ErrWithDiff, fmt.Sprintf( return errors.Errorf(
"withDiffInPlace: outpoint %s both in d.toAdd and in diff.toAdd with no "+ "withDiffInPlace: outpoint %s both in d.toAdd and in diff.toAdd with no "+
"corresponding entry in diff.toRemove", outpoint)) "corresponding entry in diff.toRemove", outpoint)
} }
d.toRemove.remove(outpoint) d.toRemove.remove(outpoint)
continue continue
@ -316,8 +316,8 @@ func (d *UTXODiff) withDiffInPlace(diff *UTXODiff) error {
(existingEntry.blockBlueScore == entryToAdd.blockBlueScore || (existingEntry.blockBlueScore == entryToAdd.blockBlueScore ||
!diff.toRemove.containsWithBlueScore(outpoint, existingEntry.blockBlueScore)) { !diff.toRemove.containsWithBlueScore(outpoint, existingEntry.blockBlueScore)) {
// If already exists - this is an error // If already exists - this is an error
return ruleError(ErrWithDiff, fmt.Sprintf( return errors.Errorf(
"withDiffInPlace: outpoint %s both in d.toAdd and in diff.toAdd", outpoint)) "withDiffInPlace: outpoint %s both in d.toAdd and in diff.toAdd", outpoint)
} }
// If not exists neither in toAdd nor in toRemove, or exists in toRemove with different blueScore - add to toAdd // If not exists neither in toAdd nor in toRemove, or exists in toRemove with different blueScore - add to toAdd
@ -406,6 +406,8 @@ type UTXOSet interface {
Get(outpoint wire.Outpoint) (*UTXOEntry, bool) Get(outpoint wire.Outpoint) (*UTXOEntry, bool)
} }
var errUTXOMissingTxOut = errors.New("missing transaction output in the utxo set")
// diffFromTx is a common implementation for diffFromTx, that works // diffFromTx is a common implementation for diffFromTx, that works
// for both diff-based and full UTXO sets // for both diff-based and full UTXO sets
// Returns a diff that is equivalent to provided transaction, // Returns a diff that is equivalent to provided transaction,
@ -421,9 +423,8 @@ func diffFromTx(u UTXOSet, tx *wire.MsgTx, acceptingBlueScore uint64) (*UTXODiff
return nil, err return nil, err
} }
} else { } else {
return nil, ruleError(ErrMissingTxOut, fmt.Sprintf( return nil, errors.Wrapf(errUTXOMissingTxOut, "Transaction %s is invalid because it spends "+
"Transaction %s is invalid because spends outpoint %s that is not in utxo set", "outpoint %s that is not in utxo set", tx.TxID(), txIn.PreviousOutpoint)
tx.TxID(), txIn.PreviousOutpoint))
} }
} }
} }