Skip to content

Commit

Permalink
Implement Assert
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Sep 10, 2020
1 parent 3bc19bc commit 06fcb85
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions constructor/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (w *Worker) invokeWorker(
return w.FindBalanceWorker(ctx, dbTx, input)
case job.RandomNumber:
return RandomNumberWorker(input)
case job.Assert:
return "", AssertWorker(input)
default:
return "", fmt.Errorf("%w: %s", ErrInvalidActionType, action)
}
Expand Down Expand Up @@ -615,3 +617,25 @@ func (w *Worker) FindBalanceWorker(
// and should just return unsatisfiable.
return "", ErrUnsatisfiable
}

// AssertWorker checks if an input is < 0.
func AssertWorker(rawInput string) error {
// We unmarshal the input here to handle string
// unwrapping automatically.
var input string
err := job.UnmarshalInput([]byte(rawInput), &input)
if err != nil {
return fmt.Errorf("%w: %s", ErrInvalidInput, err.Error())
}

val, err := types.BigInt(input)
if err != nil {
return fmt.Errorf("%w: %s", ErrInvalidInput, err.Error())
}

if val.Sign() < 0 {
return fmt.Errorf("%w: %s < 0", ErrActionFailed, val.String())
}

return nil
}
30 changes: 30 additions & 0 deletions constructor/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,10 @@ func TestJob_ComplicatedTransfer(t *testing.T) {
Input: `{"minimum":"10", "maximum":"100"}`,
OutputPath: "rand_number",
},
{
Type: job.Assert,
Input: `{{rand_number}}`,
},
{
Type: job.PrintMessage,
Input: `{"random_number": {{rand_number}}}`,
Expand Down Expand Up @@ -1195,6 +1199,32 @@ func TestJob_Failures(t *testing.T) {
expectedErr: ErrInvalidActionType,
helper: &mocks.Helper{},
},
"assertion invalid input": {
scenario: &job.Scenario{
Name: "create_address",
Actions: []*job.Action{
{
Type: "assert",
Input: `"hello"`,
},
},
},
expectedErr: ErrInvalidInput,
helper: &mocks.Helper{},
},
"failed assertion": {
scenario: &job.Scenario{
Name: "create_address",
Actions: []*job.Action{
{
Type: "assert",
Input: `"-1"`,
},
},
},
expectedErr: ErrActionFailed,
helper: &mocks.Helper{},
},
"invalid json": {
scenario: &job.Scenario{
Name: "create_address",
Expand Down

0 comments on commit 06fcb85

Please sign in to comment.