NEO Development(4): All About Timestamp - 區塊鏈上的時間戳記
Timestamp on Blockchain
在智能合約的編程中,有一個十分有趣的面向就是在於「時間點」的計算。我們都知道,區塊鏈是個分散式系統,而且是去中心化的,也就是說並不存在一個中心伺服器為每一筆交易提交的時間蓋下郵戳。在區塊鏈世界裡,我們不能相信各個節點所聲稱的交易時間點,因為交易可以由任何人提交,所以任何人也可以偽造交易時間點。例如我們要寫一個下賭注的智能合約,要是大家都在比賽結束了才提交賭注,還說,「明明我早上午點就下注了,賭勇士122-103贏騎士。」那不就天下大亂了嗎?
所以,我們不能相信節點所說的時間點。那們我們該相信什麼呢?
在區塊鏈世界裡,我們唯一相信的只有區塊上的信息。也就是說,我們只能透過「包含這筆交易的區塊高度」來判斷事情發生的先後以及時間點。也就是說如果你手續費付的少、交易塞車了一天,那麼不論你多早提交交易,這筆交易的最終時間都會被判斷在「塞車後」成功寫上區塊鏈的時間點。也就是說,區塊鏈無法精準判斷時間先後,這算是為了安全性而帶來的小小缺點。
TimeStamp 的使用
因此在智能合約的開發過程中,我們不能相一般寫程式一樣輕鬆的使用datetime.now()
來知道現在的時間點,我們必須要先知道「最新的區塊高度」,接著才能換算「現在時間」。這個時間點固然不精準了,但是是在區塊鏈中唯一可以被信任的用法。
以下就是我簡單的透過neo-python
寫的「獲取現在時間點」的程式。邏輯非常之簡單,如果了解前面說的話又懂邊程的話,看起來應該十分輕鬆。
以上的程式也可以在我的GitHub - 智能合約筆記 裡面找到:https://github.com/antoncoding/neo-smart-contracts/blob/master/sample-timestamp.py
實際執行
實際把這個合約編譯便放到private-net上面執行,可以透過neo-python
執行下列指令:
build {path}/smart-contracts/sample-timestamp.py test 02 02 True True
若是想從頭看看智能合約的架設部屬,可以參考我之前的文章:neo-python: 在NEO上部屬第一個智能合約
函式回給我們的值是1528088393
,這就是我們一般程式中常用到的Timestamp格式了。可以利用線上工具看一下這個Timestamp所對應到的時間,確定是合理的。
補充一下,這個timestamp意義是從1970年一月一號到現在的秒數,因此單位是秒。所以如果要設定「一天後」的時間點,只要使用這個Timestamp加上86400秒,就是隔天囉!
小小結
大概就是這樣,簡單的一個邏輯,了解之後就會發現要處理時間是很簡單的!
I would like to know what it say about NEO i try to understand but is hard to find some info :(
Hi @criptraders, glad that you tried to read my post.
It's about how to deal with "Time" on smart contracts. Since we cannot trust timestamps provided by nodes, the only way to get timestamp of a transaction is to get the timestamp of the block containing the transaction.
So I provided a sample code about how to implement this in
neo-python
. If you're interested in smart contract development, I encourage you to look into it, it's easy to use, and I think NEO does have several advantages over Ethereum.thanks for reply now i have some idea about the post actually i have deployed a smart contract on ethereum classic, but i would like to learn about NEO, thanks a lot
wow, good to know another Dapp developer! Happy hacking lol!
Really nice for us that can understand only english.
你那里天气如何?才子,才女,你参加月旦评了吗?这可是我们中文区最大的活动之一啊。倘若你不喜欢我的留言,请回复“取消”。
Congratulations,
you just received a 36.08% upvote from @steemhq - Community Bot!
Wanna join and receive free upvotes yourself?
![](https://steemitimages.com/0x0/https://www.steemhq.com/steemhq_voting.gif)
Vote for
steemhq.witness
on Steemit or directly on SteemConnect and join the Community Witness.This service was brought to you by SteemHQ.com
Congratulations @antonsteemit! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
You got a 16.67% upvote from @voteme courtesy of @bidseption! For next round, send minimum 0.01 SBD to bid for upvote.
Do you know, you can also earn daily passive income simply by delegating your Steem Power to voteme by clicking following links: 10SP, 25SP, 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
You got a 54.79% upvote from @brupvoter courtesy of @antonsteemit!