core: add double claim verification check

This commit is contained in:
Roman Khimov 2020-02-26 12:48:38 +03:00
parent 29586f2aa7
commit 7bea6e043e
2 changed files with 30 additions and 0 deletions

View file

@ -1150,6 +1150,9 @@ func (bc *Blockchain) verifyTx(t *transaction.Transaction, block *block.Block) e
if transaction.HaveDuplicateInputs(claim.Claims) {
return errors.New("duplicate claims")
}
if bc.dao.IsDoubleClaim(claim) {
return errors.New("double claim")
}
}
return bc.verifyTxWitnesses(t, block)
@ -1171,6 +1174,12 @@ func (bc *Blockchain) isTxStillRelevant(t *transaction.Transaction) bool {
if bc.dao.IsDoubleSpend(t) {
return false
}
if t.Type == transaction.ClaimType {
claim := t.Data.(*transaction.ClaimTX)
if bc.dao.IsDoubleClaim(claim) {
return false
}
}
for i := range t.Scripts {
if !vm.IsStandardContract(t.Scripts[i].VerificationScript) {
recheckWitness = true

View file

@ -562,6 +562,27 @@ func (dao *dao) IsDoubleSpend(tx *transaction.Transaction) bool {
return false
}
// IsDoubleClaim verifies that given claim inputs are not already claimed by another tx.
func (dao *dao) IsDoubleClaim(claim *transaction.ClaimTX) bool {
if len(claim.Claims) == 0 {
return false
}
for _, inputs := range transaction.GroupInputsByPrevHash(claim.Claims) {
prevHash := inputs[0].PrevHash
scs, err := dao.GetSpentCoinState(prevHash)
if err != nil {
return true
}
for _, input := range inputs {
_, ok := scs.items[input.PrevIndex]
if !ok {
return true
}
}
}
return false
}
// Persist flushes all the changes made into the (supposedly) persistent
// underlying store.
func (dao *dao) Persist() (int, error) {