core: simplify GetTransactionResults() a bit

Make less movements.
This commit is contained in:
Roman Khimov 2020-03-06 19:26:39 +03:00
parent 0e8ff558d1
commit fe4916f691

View file

@ -1641,36 +1641,24 @@ func (bc *Blockchain) verifyResults(t *transaction.Transaction) error {
// GetTransactionResults returns the transaction results aggregate by assetID. // GetTransactionResults returns the transaction results aggregate by assetID.
// Golang of GetTransationResults method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L207) // Golang of GetTransationResults method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L207)
func (bc *Blockchain) GetTransactionResults(t *transaction.Transaction) []*transaction.Result { func (bc *Blockchain) GetTransactionResults(t *transaction.Transaction) []*transaction.Result {
var tempResults []*transaction.Result
var results []*transaction.Result var results []*transaction.Result
tempGroupResult := make(map[util.Uint256]util.Fixed8) tempResult := make(map[util.Uint256]util.Fixed8)
references, err := bc.References(t) references, err := bc.References(t)
if err != nil { if err != nil {
return nil return nil
} }
for _, inout := range references { for _, inout := range references {
tempResults = append(tempResults, &transaction.Result{ c := tempResult[inout.Out.AssetID]
AssetID: inout.Out.AssetID, tempResult[inout.Out.AssetID] = c.Add(inout.Out.Amount)
Amount: inout.Out.Amount,
})
} }
for _, output := range t.Outputs { for _, output := range t.Outputs {
tempResults = append(tempResults, &transaction.Result{ c := tempResult[output.AssetID]
AssetID: output.AssetID, tempResult[output.AssetID] = c.Sub(output.Amount)
Amount: -output.Amount,
})
}
for _, r := range tempResults {
if amount, ok := tempGroupResult[r.AssetID]; ok {
tempGroupResult[r.AssetID] = amount.Add(r.Amount)
} else {
tempGroupResult[r.AssetID] = r.Amount
}
} }
results = []*transaction.Result{} // this assignment is necessary. (Most of the time amount == 0 and results is the empty slice.) results = []*transaction.Result{} // this assignment is necessary. (Most of the time amount == 0 and results is the empty slice.)
for assetID, amount := range tempGroupResult { for assetID, amount := range tempResult {
if amount != util.Fixed8(0) { if amount != util.Fixed8(0) {
results = append(results, &transaction.Result{ results = append(results, &transaction.Result{
AssetID: assetID, AssetID: assetID,