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 VM: FIX] the GetString() methods of bytestring requires strictUTF8 #3110

Merged
merged 20 commits into from
Feb 8, 2024

Conversation

Jim8y
Copy link
Contributor

@Jim8y Jim8y commented Jan 29, 2024

Description

The GetString methods of StackItem requires strick UTF8.

Type of change

  • 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?

  • TestToStringInvalidUTF8

Test Configuration:
dotnet test

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

@@ -45,7 +45,7 @@ public override string ToString()
StackItemType.Pointer => $"({((Pointer)p).Position})",
StackItemType.Boolean => $"({p.GetBoolean()})",
StackItemType.Integer => $"({p.GetInteger()})",
StackItemType.ByteString => $"(\"{p.GetString()}\")",
StackItemType.ByteString => p.GetSpan().ToArray().TryGetString(out var str) ? $"(\"{str}\")" : $"(\"{Convert.ToBase64String(p.GetSpan())}\")",
Copy link
Contributor

Choose a reason for hiding this comment

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

Mixing base64 and strings (that can be valid base64 as well) is not a good idea to me, how can I differentiate 41414141 from 000000 then?

This also is an incompatible change.

Copy link
Contributor Author

@Jim8y Jim8y Jan 29, 2024

Choose a reason for hiding this comment

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

Mixing base64 and strings (that can be valid base64 as well) is not a good idea to me, how can I differentiate 41414141 from 000000 then?

This also is an incompatible change.

@roman-khimov What? this is only for the ease of debug and unit test,,,, how come it can be incompatible to something? we dont print the executionstack anywhere but unit test (I added it in some pr that not yet merged). But maybe i should add somthing to tell if its base64 or string.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know how exactly it's used in the current codebase, but it's public. If it's debug/test only, then maybe it's OK (can it be used by plugins/other tools?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, cause i originally added this to debug the devpack, currently nowhere in the master branch is using it.

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 29, 2024

Choose a reason for hiding this comment

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

Doesn't matter it's in the main Neo.VM. If it's for tests then add it to the unit test's code. Don't add testing code to the main source. Add ONLY to test projects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thought about it, but we will need it cross all unit tests, including devpack.

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 30, 2024

Choose a reason for hiding this comment

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

Shouldn't put test in main code. It can get mixed up to easily. Especially for a new person and if it's not documented. If it ends up being in main code. It needs to fail, unless running in a test. Just create new library for unit tests functionality that is the same for the test projects. What if for example someone end up getting alot of free gas cause they called a test function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will update.

Copy link
Contributor

Choose a reason for hiding this comment

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

Refs. #3033 (not in any release yet, can be changed/removed). I agree with @cschuchardt88, this shouldn't be widely exposed if it's debug/test only.

@Jim8y Jim8y marked this pull request as draft January 30, 2024 08:31
…ded. As it is a test method and only unit test will need it.
@Jim8y Jim8y marked this pull request as ready for review January 31, 2024 13:16
@Jim8y
Copy link
Contributor Author

Jim8y commented Jan 31, 2024

@shargon @cschuchardt88 please check.

@cschuchardt88
Copy link
Member

cschuchardt88 commented Feb 1, 2024

This isn't a must but all filenames and classes should start with UT_ so we know it a Unit test. So when we are reading tests we know that it is production code or test code functions/classes.

src/Neo.VM/Utility.cs Outdated Show resolved Hide resolved
shargon
shargon previously approved these changes Feb 1, 2024
Copy link
Member

@cschuchardt88 cschuchardt88 left a comment

Choose a reason for hiding this comment

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

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 1, 2024

Like this @Jim8y https://github.com/neo-project/neo/pull/3103/files#diff-4faae5d98adc622a6e3016c1f98c157bd6cbabf32b55440248d30020edfd0b99

Look at filename and class

This one alignes with existing file and tests, maybe update them all together in another pr. but before that, i think we should keep it this way. Take a look at: https://github.com/neo-project/neo/blob/master/tests/Neo.VM.Tests/Helpers/RandomHelper.cs

@cschuchardt88
Copy link
Member

cschuchardt88 commented Feb 1, 2024

Also for Test methods with attribute [TestMethod] or [Fact] you don't need to add UT_ just have the name be a description of what it's doing. for example Test_Blockchain_Invoke_On_ApllicationEngine

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 1, 2024

Also i think we can deal with the name some time later, we should leave these to the editorstyle instead of manually.

@cschuchardt88
Copy link
Member

Like this @Jim8y https://github.com/neo-project/neo/pull/3103/files#diff-4faae5d98adc622a6e3016c1f98c157bd6cbabf32b55440248d30020edfd0b99
Look at filename and class

This one alignes with existing file and tests, maybe update them all together in another pr. but before that, i think we should keep it this way. Take a look at: https://github.com/neo-project/neo/blob/master/tests/Neo.VM.Tests/Helpers/RandomHelper.cs

I understand that. But this change will make life easier for the future or for someone coming in not knowing whether or not its core functioning code or test code they are debugging and reading so time isn't wasted.

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 1, 2024

I understand that name style is a thing, but there are many many rules everywhere, we should not take care of them manually, but use a tool to detect them.

@cschuchardt88
Copy link
Member

I understand that name style is a thing, but there are many many rules everywhere, we should not take care of them manually, but use a tool to detect them.

We can use another .editorconfig to do this. @shargon or @Jim8y maybe you can look into this. Im currently busy with cli product.

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 1, 2024

Surely will, @shargon you going to take this?

Copy link
Member

@shargon shargon left a comment

Choose a reason for hiding this comment

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

@Jim8y What about override ToString in all stack items?

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 6, 2024

@Jim8y What about override ToString in all stack items?

compound types is hard to print, will make the console impossible to trace, cause it will be printed on every opcode

@shargon
Copy link
Member

shargon commented Feb 6, 2024

@Jim8y What about override ToString in all stack items?

compound types is hard to print, will make the console impossible to trace, cause it will be printed on every opcode

It will print only the count, will be the same, but reusable

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 7, 2024

@Jim8y What about override ToString in all stack items?

compound types is hard to print, will make the console impossible to trace, cause it will be printed on every opcode

It will print only the count, will be the same, but reusable

@shargon just tried it out, even if you have to sting for each item, you still need a method for stack to print the stack. Just making it easier to implement, but still need somthing on both devpack side and vm side. Or just add it to the core.

Come on, @shargon and @cschuchardt88 are caring about different things, this will make our work hard, how about we focus on the functionality first? I have kept moving them here there, but they just work the same.

@cschuchardt88
Copy link
Member

Why don't you have a look at IApplicationEngineProvidor its used for tracing vm.

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 7, 2024

Why don't you have a look at IApplicationEngineProvidor its used for tracing vm.

We can access stack, but we can not access the things in the vm test. Have a new test project solely for a stack.print is not necessary. Cause we will either have to add it as reference in the devpack, or publish it, too much for a print.

@cschuchardt88
Copy link
Member

That kind of stack you trying to override?

image

@cschuchardt88
Copy link
Member

image

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 7, 2024

I� need to print the stack, not the stack items, but everything in the stack.

shargon
shargon previously approved these changes Feb 7, 2024
@shargon
Copy link
Member

shargon commented Feb 7, 2024

@shargon just tried it out, even if you have to sting for each item, you still need a method for stack to print the stack. Just making it easier to implement, but still need something on both devpack side and vm side. Or just add it to the core.

We can have both

{
public static string Print(this EvaluationStack stack)
{
return $"[{string.Join(", ", stack.Select(p => $"{p.Type}{p}"))}]";
Copy link
Member

Choose a reason for hiding this comment

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

In my opinion, we should move this to ToString no sense to duplicate code everywhere, and also is useful for debug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so you still need to have one in VM test and one in devpack test, what is the difference.

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't hurt, and is good to override ToString to see something useful during debug, writes, etc

Copy link
Member

Choose a reason for hiding this comment

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

There should a Interface or some way to hook into the VM for override.

Please stop rushing code designs. It clear that no thought is put into the design when making code changes. Copy and Paste isn't the solution. I am not talking to anyone person, but us as a whole. I do it too. But we need to all stop and have plans. I think we should have in the 1st comment of the PR, what the plan is for design when doing design changes. Yes it takes more time, But we need the time to refactor and think it through.

Copy link
Member

Choose a reason for hiding this comment

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

There should a Interface or some way to hook into the VM for override.

This change is not required for core, is only for debugging and testing purpose

@Jim8y
Copy link
Contributor Author

Jim8y commented Feb 7, 2024

Good to go then

shargon
shargon previously approved these changes Feb 7, 2024
@shargon shargon merged commit 3e52756 into neo-project:master Feb 8, 2024
1 of 2 checks passed
@Jim8y Jim8y deleted the fix-tostring branch February 8, 2024 08:49
Jim8y added a commit to Jim8y/neo that referenced this pull request Feb 9, 2024
* 'nullable' of github.com:Jim8y/neo:
  [Neo Fix] fix the store crash issue (neo-project#3124)
  [Neo VM: FIX] the GetString() methods of bytestring requires strictUTF8 (neo-project#3110)
@roman-khimov roman-khimov added this to the v3.7.0 milestone Feb 27, 2024
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.

4 participants