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

[SC] Support custom InvokeByName method to call methods explicitly rather than using Invoke #542

Open
wants to merge 6 commits into
base: release/1.1.0.0
Choose a base branch
from

Conversation

quantumagi
Copy link
Contributor

@quantumagi quantumagi commented May 5, 2021

This first draft of the PR is to gather some initial opinions/suggestions on the approach, thereafter test cases can be added.

The main motivation is that using reflection for each method call is bad / slow.

Allows a special custom method (InvokeByName) to be defined on the smart contract for mapping method names and arguments to methods instead of relying on reflection to find the appropriate method. Example:

        public object InvokeByName(string methodName, object[] parameters)
        {
            uint version = 1; // this.Version;

            switch (methodName)
            {
                case nameof(DataStorageContract.AddData):
                    if (parameters.Length == 3)
                    {
                        var result = this.AddData(parameters[0] as string[], parameters[1] as string, parameters[2] as string);
                        return result;
                    }

                    if (parameters.Length == 4)
                    {
                        var result = this.AddData(parameters[0] as string[], parameters[1] as string, parameters[2] as string, (bool)parameters[3]);
                        return result;
                    }

                    throw new TargetParameterCountException($"Method {methodName} overload with {parameters.Length} params does not exist on type {nameof(DataStorageContract)} v{version}");

                case nameof(DataStorageContract.PersistComplexType):
                    var txRawHex = parameters[0] as string;

                    var tx = Transaction.Parse(txRawHex, RawFormat.Satoshi);

                    this.PersistComplexType(tx);

                    return DispatchResult.Void;

                default:
                    throw new MissingMethodException($"Method {methodName} does not exist on type {nameof(DataStorageContract)} v{version}");
            }
        }

@quantumagi quantumagi requested a review from YakupIpek May 5, 2021 07:42
@quantumagi quantumagi changed the title [SC] Support custom method resolution [SC] Support custom InvokeByName method to call methods explicitly rather than using Invoke May 5, 2021
@quantumagi quantumagi requested a review from fassadlr May 5, 2021 09:24
@quantumagi quantumagi mentioned this pull request May 6, 2021
@quantumagi quantumagi requested a review from noescape00 May 10, 2021 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant