Skip to content

Conversation

BonelessImpl
Copy link
Contributor

For simulation and testing purposes, there is currently no way to get the would-be logged DIP-4 event.

This PR does not change the current functionality of events, as emit() does what it does before, but it extracts the creation of the json event into a function to_json().

@dj8yfo
Copy link
Contributor

dj8yfo commented May 27, 2025

@BonelessImpl can you fix tests?

@BonelessImpl
Copy link
Contributor Author

@BonelessImpl can you fix tests?

Will do. The tests depend on going from json to strings directly, but because serde_json::Value stores the elements inside of it as a map, it can change the order of the values inside of it. I will fix them and make them depend on json objects and values, instead of just strings.

Copy link

codecov bot commented May 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.31%. Comparing base (639fa9b) to head (a5924f3).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1360      +/-   ##
==========================================
+ Coverage   80.29%   80.31%   +0.02%     
==========================================
  Files         104      104              
  Lines       15243    15264      +21     
==========================================
+ Hits        12239    12260      +21     
  Misses       3004     3004              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BonelessImpl
Copy link
Contributor Author

@BonelessImpl can you fix tests?

Tests work now.

@BonelessImpl
Copy link
Contributor Author

Btw, I recommend enabling the preserve_order feature in serde_json, since this is a blockchain and order matters. My tests make order not matter anymore.

Copy link
Collaborator

@frol frol left a comment

Choose a reason for hiding this comment

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

@race-of-sloths score 3

@BonelessImpl Thank you for the contribution!

@race-of-sloths
Copy link

@frol Thank you for calling!

@BonelessImpl Thank you for the contribution! Join Race of Sloths by simply mentioning me in your comment/PRs description and start collecting Sloth Points through contributions to open source projects.

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths and receive a reward
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

Comment on lines 721 to 727
::near_sdk::serde_json::to_value(event)
.unwrap_or_else(|_| ::near_sdk::env::abort())
}

pub fn emit(&self) {
let json = self.to_json();
let json_str = ::near_sdk::serde_json::to_string(&json)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, on the second look I realized that this adds unnecessary double serialization - first to Value and then to String. This means that we will be able to generate 2x fewer events in a single call. It was a critical requirement to generate as many events in 300TGas as possible.

Please, refactor it so there is no unnecessary performance penalty. Bonus points for having a benchmark test for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, on the second look I realized that this adds unnecessary double serialization - first to Value and then to String. This means that we will be able to generate 2x fewer events in a single call. It was a critical requirement to generate as many events in 300TGas as possible.

Please, refactor it so there is no unnecessary performance penalty. Bonus points for having a benchmark test for it.

You're correct that it serializes twice. The problem there is that the EventBuilder type, which builds the json, is an opaque type that can't be taken outside the function that creates it. I will see what Rust wizardry I can do to make it happen only once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, there is no solution that will provide a zero cost abstraction. Especially one that doesn't do an extra heap allocation. So, a simpler solution is to just copy the implementation from emit(), and add tests that ensure that both emit the same result, whether json or not.

@BonelessImpl BonelessImpl marked this pull request as draft May 30, 2025 07:21
@BonelessImpl BonelessImpl marked this pull request as ready for review May 30, 2025 07:40
@BonelessImpl
Copy link
Contributor Author

PR is ready for another review. Thank you guys!

Copy link
Collaborator

@frol frol left a comment

Choose a reason for hiding this comment

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

@dj8yfo I also don't know a better solution from the top of my head. I don't like the duplication, but I won't block this PR for this reason - I hope dead code elimination in Rust compiler is good enough to remove the unused code.

@frol
Copy link
Collaborator

frol commented May 30, 2025

@race-of-sloths score 5

@race-of-sloths
Copy link

@frol Thank you for calling!

@BonelessImpl Thank you for the contribution! Join Race of Sloths by simply mentioning me in your comment/PRs description and start collecting Sloth Points through contributions to open source projects.

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths and receive a reward
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

@frol frol merged commit 985c16b into near:master May 30, 2025
56 of 57 checks passed
@github-project-automation github-project-automation bot moved this from NEW❗ to Shipped 🚀 in DevTools May 30, 2025
@frol frol mentioned this pull request May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Shipped 🚀
Development

Successfully merging this pull request may close these issues.

4 participants