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

Feature request: Unique time.Now() per transaction #1709

Open
jefft0 opened this issue Feb 29, 2024 · 2 comments
Open

Feature request: Unique time.Now() per transaction #1709

jefft0 opened this issue Feb 29, 2024 · 2 comments
Assignees

Comments

@jefft0
Copy link
Contributor

jefft0 commented Feb 29, 2024

Description

I understand that time.Now() returns the same value for every call within a transaction (the block time). But there can be multiple transactions in a block. It would be useful for time.Now() to return different values for each transaction. (For example each call to r/demo/boards to post a message would give a unique message timestamp.) I tried the following goland code which calls time.Now() twice in quick succession:

func main() {
	t1 := time.Now()
	t2 := time.Now()
	fmt.Println(t1.String())
	fmt.Println(t2.String())
}

It prints:

2024-02-29 14:05:33.265165 +0100 CET m=+0.000095043
2024-02-29 14:05:33.265165 +0100 CET m=+0.000095126

As you can see, even this returns unique values. Go developers may expect this to some extent.

Possible implementation

On a dev call, @moul suggested making each transaction offset the value of time.Now() using the transaction index within the block. For example, timestamp + tx.index * 100us . (The example value of 100us should be configured based on block time and max transactions per block so that a time.Now() value won't overlap with the next block.)

@moul
Copy link
Member

moul commented Mar 1, 2024

Just to make sure that we're aligned, here is what I expect:

TX1

func CallMe() {
	t1 := time.Now()
	t2 := time.Now()
	fmt.Println(t1.String())
	fmt.Println(t2.String())
}
// Output:
// 2024-02-29 14:05:33.265165 +0100 CET m=+0.000095043
// 2024-02-29 14:05:33.265165 +0100 CET m=+0.000095043

TX2:

func CallMe() {
	t1 := time.Now()
	t2 := time.Now()
	fmt.Println(t1.String())
	fmt.Println(t2.String())
}
// Output:
// 2024-02-29 14:05:33.265165 +0100 CET m=+0.000095126
// 2024-02-29 14:05:33.265165 +0100 CET m=+0.000095126

The key is not within a single TX but between independent TXs. Within a single TX, consistency is currently preferred.

@Kouteki
Copy link
Contributor

Kouteki commented Sep 16, 2024

We won't be handling this feature request before the mainnet launch, primarily due to the bandwidth issues. Will keep open and possibly revisit in the future.

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

Successfully merging a pull request may close this issue.

4 participants