Suppose I am running a gallery and ask participants to submit there paintings before a specific date. After passing that date participant should not be able to submit their entry. How can I implement such functionality?
2 Answers
There are two ways to do this:
1) Limit the deadline by the block number.
require(block.number < 7169670); // Where 7169670 is a block number.
2) Limit the deadline by the current timestamp.
require(now < 1549219795); // Where 1549219795 is the current unix timestamp.
There are advantages for both. It is not recommended to use block.number
for checks that are in a long time, as there is variance in timing of blocks and other factors such as the ice age.
now
is preferred, as it is generally more accurate. With each mined block, miners include a timestamp of the current time. There are rules which limit their ability to manipulate this time (for example, it cannot be prior to the previous block's timestamp, nor can it be too much longer than it).
-
Why to use
block.number
is not recommended to use for checks in a long time? @shane– alperCommented Oct 4, 2019 at 9:50 -
It was recommended to not use
block.number
back when the difficulty bomb existed. There were a number of contracts (usually ICOs), that usedblock.number
and expected to start their ICO at a certain time. When the difficulty bomb happened, these start times got pushed back because of the longer block times. Thus, the reason to not useblock.nunber
is because the variance in block times in the short term (<infinity) is too large to accurately predict. Commented Oct 4, 2019 at 14:46 -
How about if it is network is proof-of-authorithy network and block time is fixed seconds such as 15 seconds?– alperCommented Oct 4, 2019 at 15:19
-
I would say that is fine. Additionally, it should be fine for certain situations where precise timing isn't necessary. Commented Oct 4, 2019 at 15:26
error DeadlinePassed();
struct Gallery {
uint deadline
uint Goal
} //initialize struct
mapping(unit => Gallery) public gallery;
modifier isDatePassed(unit _id) {
if (block.timestamp > Gallery[_id] ) {
revert DeadlinePassed();
}
_;
}
let me know if this works i just used my imagination