Skip to content

[N4] unwrap StorageKey#4358

Closed
shargon wants to merge 1 commit intoneo-project:masterfrom
shargon:n4-unwrap-key
Closed

[N4] unwrap StorageKey#4358
shargon wants to merge 1 commit intoneo-project:masterfrom
shargon:n4-unwrap-key

Conversation

@shargon
Copy link
Member

@shargon shargon commented Dec 2, 2025

Description

Required for #4348

This pull request introduces a new parsing capability to the KeyBuilder class in the smart contract library, allowing for extraction of key components from a byte array, and adds corresponding unit tests to verify this functionality. The most important changes are grouped below by theme.

KeyBuilder enhancements

  • Added a new internal static method TryParse to KeyBuilder that parses a byte array into its constituent id, prefix, and value fields, supporting multiple value types. This improves the ability to decompose storage keys for inspection or validation.
  • Implemented a helper method ReadStruct for safely reading value types from a byte span using marshaling.
  • Removed the unused constant PrefixLength from KeyBuilder, simplifying the class.

Unit testing

  • Added a new test method TestTryParse to UT_KeyBuilder.cs to verify correct parsing of various key formats and error conditions for the new TryParse method.
  • Updated test imports to include System.Reflection, supporting new test logic.

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?

  • Unit Testing
  • Run Application
  • Local Computer Tests
  • No Testing

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

@github-actions github-actions bot added the N4 label Dec 2, 2025
Span<byte> span = data.AsSpan(offset, size);

if (BitConverter.IsLittleEndian)
span.Reverse();
Copy link
Member Author

Choose a reason for hiding this comment

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

@erikzhang we loose performance here, because in regular nodes we always need to reverse each key, before the change this was faster

Copy link
Member

Choose a reason for hiding this comment

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

Have you tested it? Approximately how much performance was lost?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't have a benchmark. it depends a lot on the key size, but previously an integer was written directly inverted, and now it requires an extra loop to reverse it. Also, I think a string gets reversed, and it shouldn't; only an integer should be reversed.

Copy link
Member

Choose a reason for hiding this comment

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

string is not a value type.

@erikzhang
Copy link
Member

If it is used for #4348, I think it's easy to extract Offset from the Value.

@shargon shargon closed this Dec 2, 2025
@shargon shargon deleted the n4-unwrap-key branch December 2, 2025 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants