core: fix and speed up mempool Verify()

First of all, it was wrong, it was not checking for inputs really, it compared
tx hashes for some reason, second, when it did compare inputs it compared only
the PrevIndex part of them which is also wrong.

Also, there is absolutely no reason to go through GetVerifiedTransactions()
here, we don't need this copy of pointers and it can also be outdated by the
time we're to finish our check.

Before:
BenchmarkTXPerformanceTest-4
    5000            485506 ns/op           65886 B/op        409 allocs/op
ok      github.com/CityOfZion/neo-go/integration        3.212s

After:
enchmarkTXPerformanceTest-4
    5000            371104 ns/op           44367 B/op        408 allocs/op
ok      github.com/CityOfZion/neo-go/integration        2.712s
This commit is contained in:
Roman Khimov 2019-12-13 23:23:33 +03:00
parent 35f7f4137e
commit 5d68f88196

View file

@ -287,23 +287,17 @@ func (mp *MemPool) GetVerifiedTransactions() []*transaction.Transaction {
// If yes, the transaction tx is not a valid transaction and the function return false.
// If no, the transaction tx is a valid transaction and the function return true.
func (mp MemPool) Verify(tx *transaction.Transaction) bool {
count := 0
inputs := make([]*transaction.Input, 0)
for _, item := range mp.GetVerifiedTransactions() {
if tx.Hash().Equals(item.Hash()) {
for i := range item.Inputs {
inputs = append(inputs, &item.Inputs[i])
}
}
}
for i := 0; i < len(inputs); i++ {
mp.lock.RLock()
defer mp.lock.RUnlock()
for _, item := range mp.unsortedTxn {
for i := range item.txn.Inputs {
for j := 0; j < len(tx.Inputs); j++ {
if inputs[i].PrevHash.Equals(tx.Inputs[j].PrevHash) {
count++
if item.txn.Inputs[i] == tx.Inputs[j] {
return false
}
}
}
}
return count == 0
return true
}