Skip to content

Commit

Permalink
feat: add ValidateDynamicLinkInterface to api
Browse files Browse the repository at this point in the history
  • Loading branch information
loloicci committed Mar 28, 2023
1 parent 50aab1f commit e954498
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,26 @@ func CallCallablePoint(
return copyAndDestroyUnmanagedVector(res), copyAndDestroyUnmanagedVector(events), copyAndDestroyUnmanagedVector(attributes), uint64(gasUsed), nil
}

// returns: result, systemerr
//
// result: serialized Option<String> which None means true, Some(e) means false and the reason is e.
func ValidateDynamicLinkInterface(
cache Cache,
checksum []byte,
expectedInterface []byte,
) ([]byte, error) {
cs := makeView(checksum)
defer runtime.KeepAlive(checksum)
ei := makeView(expectedInterface)
defer runtime.KeepAlive(expectedInterface)
errmsg := newUnmanagedVector(nil)
res, err := C.validate_interface(cache.ptr, cs, ei, &errmsg)
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
return nil, errorWithMessage(err, errmsg)
}
return copyAndDestroyUnmanagedVector(res), nil
}

/**** To error module ***/

func errorWithMessage(err error, b C.UnmanagedVector) error {
Expand Down
20 changes: 20 additions & 0 deletions api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,3 +1515,23 @@ func TestCallCallablePoint(t *testing.T) {

require.Equal(t, attrsIn, attributes)
}

func TestValidateDynamicLinkInterafce(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
checksum := createEventsContract(t, cache)

correctInterface := []byte(`[{"name":"add_event_dyn","ty":{"params":["I32","I32","I32"],"results":[]}},{"name":"add_events_dyn","ty":{"params":["I32","I32"],"results":[]}},{"name":"add_attribute_dyn","ty":{"params":["I32","I32","I32"],"results":[]}},{"name":"add_attributes_dyn","ty":{"params":["I32","I32"],"results":[]}}]`)
res, err := ValidateDynamicLinkInterface(cache, checksum, correctInterface)
require.NoError(t, err)
require.Equal(t, []byte(`null`), res)

wrongInterface := []byte(`[{"name":"add_event","ty":{"params":["I32","I32","I32"],"results":[]}},{"name":"add_events","ty":{"params":["I32","I32"],"results":[]}},{"name":"add_attribute","ty":{"params":["I32","I32","I32"],"results":[]}},{"name":"add_attributes","ty":{"params":["I32","I32"],"results":[]}}]`)
res, err = ValidateDynamicLinkInterface(cache, checksum, wrongInterface)
require.NoError(t, err)
require.Contains(t, string(res), `following functions are not implemented`)
require.Contains(t, string(res), `add_event`)
require.Contains(t, string(res), `add_events`)
require.Contains(t, string(res), `add_attribute`)
require.Contains(t, string(res), `add_attributes`)
}

0 comments on commit e954498

Please sign in to comment.