From dd0dd2d03111369d4be67ecea694288e8c152baa Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 30 Sep 2019 17:37:04 +0300 Subject: [PATCH] core: add claim-specific GetScriptHashesForVerifying() Claim transactions have different logic in C# node, so we need to implement it too. It's not the most elegant way to fix it, but let's make it work first and then refactor if and where needed. Fixes verification of Claim transactions. --- pkg/core/blockchain.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index b9720a261..88bf4e5d8 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -883,10 +883,41 @@ func (bc *Blockchain) GetTransationResults(t *transaction.Transaction) []*transa return results } +// GetScriptHashesForVerifyingClaim returns all ScriptHashes of Claim transaction +// which has a different implementation from generic GetScriptHashesForVerifying. +func (bc *Blockchain) GetScriptHashesForVerifyingClaim(t *transaction.Transaction) ([]util.Uint160, error) { + hashes := make([]util.Uint160, 0) + + claim := t.Data.(*transaction.ClaimTX) + clGroups := make(map[util.Uint256][]*transaction.Input) + for _, in := range claim.Claims { + clGroups[in.PrevHash] = append(clGroups[in.PrevHash], in) + } + for group, inputs := range clGroups { + refTx, _, err := bc.GetTransaction(group) + if err != nil { + return nil, err + } + for _, input := range inputs { + if len(refTx.Outputs) <= int(input.PrevIndex) { + return nil, fmt.Errorf("wrong PrevIndex reference") + } + hashes = append(hashes, refTx.Outputs[input.PrevIndex].ScriptHash) + } + } + if len(hashes) > 0 { + return hashes, nil + } + return nil, fmt.Errorf("no hashes found") +} + // GetScriptHashesForVerifying returns all the ScriptHashes of a transaction which will be use // to verify whether the transaction is bonafide or not. // Golang implementation of GetScriptHashesForVerifying method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L190) func (bc *Blockchain) GetScriptHashesForVerifying(t *transaction.Transaction) ([]util.Uint160, error) { + if t.Type == transaction.ClaimType { + return bc.GetScriptHashesForVerifyingClaim(t) + } references := bc.References(t) if references == nil { return nil, errors.New("Invalid operation")