Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Intent vs Observed Operations and Signers #78

Merged
merged 5 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions parser/intent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

import (
"fmt"
"log"

"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/types"
)

// ExpectedOperation returns an error if an observed operation
// differs from the intended operation. An operation is considered
// to be different from the intent if the AccountIdentifier,
// Amount, or Type has changed.
func ExpectedOperation(intent *types.Operation, observed *types.Operation) error {
if types.Hash(intent.Account) != types.Hash(observed.Account) {
return fmt.Errorf(
"intended account %s did not match observed account %s",
types.PrettyPrintStruct(intent.Account),
types.PrettyPrintStruct(observed.Account),
)
}

if types.Hash(intent.Amount) != types.Hash(observed.Amount) {
return fmt.Errorf(
"intended amount %s did not match observed amount %s",
types.PrettyPrintStruct(intent.Amount),
types.PrettyPrintStruct(observed.Amount),
)
}

if intent.Type != observed.Type {
return fmt.Errorf(
"intended type %s did not match observed type %s",
intent.Type,
observed.Type,
)
}

return nil
}

// ExpectedOperations returns an error if a slice of intended
// operations differ from observed operations. Optionally,
// it is possible to error if any extra observed opertions
// are found.
func ExpectedOperations(
intent []*types.Operation,
observed []*types.Operation,
errExtra bool,
) error {
matches := make(map[int]struct{})
unmatched := []*types.Operation{}

for _, obs := range observed {
foundMatch := false
for i, in := range intent {
if _, exists := matches[i]; exists {
continue
}

if ExpectedOperation(in, obs) == nil {
matches[i] = struct{}{}
foundMatch = true
break
}
}

if !foundMatch {
if errExtra {
return fmt.Errorf(
"found extra operation %s",
types.PrettyPrintStruct(obs),
)
}
unmatched = append(unmatched, obs)
}
}

for i := 0; i < len(intent); i++ {
if _, exists := matches[i]; !exists {
return fmt.Errorf(
"could not find operation with intent %s in observed",
types.PrettyPrintStruct(intent[i]),
)
}
}

if len(unmatched) > 0 {
log.Printf("found extra operations: %s\n", types.PrettyPrintStruct(unmatched))
}

return nil
}

// ExpectedSigners returns an error if a slice of SigningPayload
// has different signers than what was observed (typically populated
// using the signers returned from parsing a transaction).
func ExpectedSigners(intent []*types.SigningPayload, observed []string) error {
// De-duplicate required signers (ex: multiple UTXOs from same address)
intendedSigners := make(map[string]struct{})
for _, payload := range intent {
intendedSigners[payload.Address] = struct{}{}
}

if err := asserter.StringArray("observed signers", observed); err != nil {
return fmt.Errorf("%w: found duplicate signer", err)
}

// Could exist here if len(intent) != len(observed) but
// more useful to print out a detailed error message.
seenSigners := make(map[string]struct{})
unmatched := []string{} // observed
for _, signer := range observed {
_, exists := intendedSigners[signer]
if !exists {
unmatched = append(unmatched, signer)
} else {
seenSigners[signer] = struct{}{}
}
}

for k := range intendedSigners {
if _, exists := seenSigners[k]; !exists {
return fmt.Errorf(
"could not find match for intended signer %s",
k,
)
}
}

if len(unmatched) != 0 {
return fmt.Errorf(
"found unexpected signers: %s",
types.PrettyPrintStruct(unmatched),
)
}

return nil
}
Loading