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

feat: validate_timestamp implementation #26

Merged
merged 2 commits into from
Aug 5, 2024

Conversation

TAdev0
Copy link
Collaborator

@TAdev0 TAdev0 commented Aug 4, 2024

This PR implements validate_timestamp functions, which checks that the proposed timestamp for the new block is strictly greater than the median of the previous 11 timestamps.

My implementation is simplified compared to ZeroSync

ZeroSync uses cairo zero hints to retrieve the last 11 timestamps by reading 11 contiguous memory cells in the VM, and then sorts the new array of 11 elements in order to find the median. I think this step can be skipped, it doesn't seem necessary to sort anything in our implementation as prev_timestamps in ChainState is necessary already sorted right?

also, what happens if prev_timestamps contains only a few values or even none (for example when syncing the very first blocks)? I'm assuming there are at least 6 values (11 - 5 = 6 to get the mean) , implementation will break if its not the case. But i guess for ZeroSync its the same? Trying to read 11 consecutives values in vm memory if there are less than 11 will fail with reading unknow value error

Copy link

vercel bot commented Aug 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
raito ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 5, 2024 9:18am

@TAdev0
Copy link
Collaborator Author

TAdev0 commented Aug 4, 2024

yeah, ZeroSync uses this internal function:

// Compute the median value of an array of 11 timestamps
//
func compute_timestamps_median{range_check_ptr}(timestamp_array: felt*) -> felt {
    alloc_locals;
    // Compute the median using a hint
    local median;
    %{
        timestamps = []
        for i in range(ids.TIMESTAMP_COUNT):
            timestamps.append( memory[ids.timestamp_array + i] )
        timestamps.sort()
        ids.median = timestamps[ids.TIMESTAMP_MEDIAN_INDEX]
    %}
    verify_timestamps_median(timestamp_array, median);
    return median;
}

which implies that timestamp_array pointer points to an array of 11 elements

@maciejka maciejka removed the request for review from b-j-roberts August 5, 2024 08:28
Copy link
Collaborator

@maciejka maciejka left a comment

Choose a reason for hiding this comment

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

Chain state will be verified by the zk proof so it makes sense to assume it is valid, i.e. timestamps are sorted and of valid length.
Just small stylistic issue.

@maciejka maciejka merged commit 2e0e8c7 into keep-starknet-strange:main Aug 5, 2024
5 checks passed
@maciejka
Copy link
Collaborator

maciejka commented Aug 5, 2024

@TAdev0 please join project tg group: https://t.me/RaitoStarknet

@TAdev0 TAdev0 deleted the validate_timestamp branch August 13, 2024 15:06
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.

2 participants