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

[Neo Rpc Methods] fix contract rpc methods parameters #3485

Closed
wants to merge 18 commits into from

Conversation

Jim8y
Copy link
Contributor

@Jim8y Jim8y commented Sep 11, 2024

Description

This pr updates the rpc plugin contract related methods.

Fixes # (issue)

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • TestInvokeFunction
  • TestInvokeScript
  • TestTraverseIterator
  • TestGetUnclaimedGas

Test Configuration:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

shargon
shargon previously approved these changes Sep 11, 2024
cschuchardt88
cschuchardt88 previously approved these changes Sep 11, 2024
src/Plugins/RpcServer/ParameterConverter.cs Show resolved Hide resolved
/// <param name="useDiagnostic">Optional. Flag to enable diagnostic information.</param>
/// <returns>A JToken containing the result of the invocation.</returns>
[RpcMethodWithParams]
protected internal virtual JToken InvokeFunction(string scriptHash, string operation, ContractParameter[] args = null, Signer[] signers = null, bool useDiagnostic = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour diverges from an old one. This method should accept the list of signers with witnesses as the fourth parameter, not only the list of signers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It never worked. And not bing of any use at anywhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And not bing of any use at anywhere.

You don't know that. It's an external API and it's a very widely used one. Your changes can easily break real applications. Refactorings must not break compatibility.

src/Plugins/RpcServer/RpcServer.SmartContract.cs Outdated Show resolved Hide resolved
@Jim8y Jim8y dismissed stale reviews from cschuchardt88 and shargon via 656bee9 September 13, 2024 04:04

namespace Neo.Plugins.RpcServer.Model;

public class SignerOrWitness
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make a base class for common stuff? Than two different classes one, being Signer and other being Witness. This way we can get Signer attributes like Rules or other properties that separate the two classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it work first, optimize it later. I think you are better than me to make the code more elegent.

Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/Plugins/RpcServer/Model/SignerOrWitness.cs Outdated Show resolved Hide resolved
src/Plugins/RpcServer/ParameterConverter.cs Show resolved Hide resolved
src/Plugins/RpcServer/ParameterConverter.cs Show resolved Hide resolved
@Jim8y
Copy link
Contributor Author

Jim8y commented Sep 14, 2024

@AnnaShaleva please check latest update to make it SignerWithWitness, @chenzhitong please update the document accordingly, @superboyiii may you please test this pr? expecially the InvokeFunction and InvokeScript that may contain signer and/or witness.

throw new RpcException(CreateInvalidParamError<Signer>(token));
}
}
throw new RpcException(CreateInvalidParamError<Signer>(token));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would adding an else statement here make it clearer?

src/Plugins/RpcServer/Model/SignerWithWitness.cs Outdated Show resolved Hide resolved
src/Plugins/RpcServer/Model/SignerWithWitness.cs Outdated Show resolved Hide resolved
AllowedContracts = ((JArray)jObject["allowedcontracts"])?.Select(p => UInt160.Parse(p.AsString())).ToArray() ?? Array.Empty<UInt160>(),
AllowedGroups = ((JArray)jObject["allowedgroups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray() ?? Array.Empty<ECPoint>(),
Rules = ((JArray)jObject["rules"])?.Select(r => WitnessRule.FromJson((JObject)r)).ToArray() ?? Array.Empty<WitnessRule>()
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need a check against the maximum number of parsed contracts, rules and groups, it's 16.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may serialize and deserialize parsed items to ensure validity, exactly how the old code does it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not resolved, we need validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already copied from the original code, if its still not working, i would say it has never worked, and should not be a concern of this pr.

src/Plugins/RpcServer/ParameterConverter.cs Outdated Show resolved Hide resolved
return new[] { signerOrWitness };
}
}
throw new RpcException(RpcError.InvalidParams.WithData($"Invalid SignerOrWitness format: {token}"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

src/Plugins/RpcServer/ParameterConverter.cs Show resolved Hide resolved
src/Plugins/RpcServer/ParameterConverter.cs Show resolved Hide resolved
@Jim8y
Copy link
Contributor Author

Jim8y commented Sep 21, 2024

@AnnaShaleva updated this pr to mock the complete http request process, now should be able to work without miss any detail.

@Jim8y
Copy link
Contributor Author

Jim8y commented Sep 27, 2024

@shargon @superboyiii

shargon
shargon previously approved these changes Sep 27, 2024
@vncoelho vncoelho changed the title [Neo Rpc Methods] fix contact rpc methods parameters [Neo Rpc Methods] fix contract rpc methods parameters Sep 27, 2024
vncoelho
vncoelho previously approved these changes Sep 27, 2024
Copy link
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testes some methods and they are working as expected

@vncoelho
Copy link
Member

However, maybe @superboyiii should do a more extensive tests, these changes affects various methods

@Jim8y
Copy link
Contributor Author

Jim8y commented Sep 28, 2024

However, maybe @superboyiii should do a more extensive tests, these changes affects various methods

I have updated to make it use existing unit test, without updating the unit test logic makes sure that changes in this pr follows the old processing logic.

@Jim8y Jim8y dismissed stale reviews from vncoelho and shargon via 2f50215 September 28, 2024 09:50
Copy link
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably a breaking change to several dapps.
InvokeFunction is a methods that probably many services use.
Perhaps its parameters will now face this modification from array.

Comment on lines +198 to +199
var signers = signerWithWitnesses?.Where(u => u.Signer != null).Select(u => u.Signer).ToArray() ?? [];
var witnesses = signerWithWitnesses?.Where(u => u.Witness != null).Select(u => u.Witness).ToArray() ?? [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks .Where(u => u.Signer != null) and .Where(u => u.Witness != null) are not quite correct because it may lead to unexpected result if e.g. some witness is missing for a signer. These checks may lead to the situation when signers and witnesses have different length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think we have properly defined the behavior of when signers and witnesses have different length. we dont have any logic to process it in previous code.

AllowedContracts = ((JArray)jObject["allowedcontracts"])?.Select(p => UInt160.Parse(p.AsString())).ToArray() ?? Array.Empty<UInt160>(),
AllowedGroups = ((JArray)jObject["allowedgroups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray() ?? Array.Empty<ECPoint>(),
Rules = ((JArray)jObject["rules"])?.Select(r => WitnessRule.FromJson((JObject)r)).ToArray() ?? Array.Empty<WitnessRule>()
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not resolved, we need validation.

Signer[] signers = _params.Count >= 3 ? SignersFromJson((JArray)_params[2], system.Settings) : null;
Witness[] witnesses = _params.Count >= 3 ? WitnessesFromJson((JArray)_params[2]) : null;
Signer[] signers = _params.Count >= 3 ? SignerWithWitness.ParseArray((JArray)_params[2], system.Settings).Where(u => u.Signer != null).Select(u => u.Signer).ToArray() : null;
Witness[] witnesses = _params.Count >= 3 ? SignerWithWitness.ParseArray((JArray)_params[2], system.Settings).Where(u => u.Witness != null).Select(u => u.Witness).ToArray() : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@roman-khimov
Copy link
Contributor

This is probably a breaking change to several dapps.

If it's a breaking change, it MUST NOT be merged. We have exactly zero reasons to break these interfaces. Any refactorings MUST keep compatibility.


namespace Neo.Plugins.RpcServer.Model;

public class SignerWithWitness(Signer? signer, Witness? witness)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignerWithWitness ?
May be WitnessSigner is better?

{
signer = SignerFromJson(jObject, settings);
}
if (jObject.ContainsProperty("invocation") || jObject.ContainsProperty("verification"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"invocation" and "verification" defined as constant values is better?

@Jim8y Jim8y closed this Nov 6, 2024
@shargon
Copy link
Member

shargon commented Nov 6, 2024

Why it was closed @Jim8y ?

@Jim8y
Copy link
Contributor Author

Jim8y commented Nov 6, 2024

Why it was closed @Jim8y ?

Srry i should have mentioned, i am working a new pr to replace this one that introduces minumum change to the existing code base such that it can be easier to review. This pr has made many changes that is not relavent to this pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants