{"id":302848,"date":"2020-04-30T21:00:44","date_gmt":"2020-04-30T21:00:44","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=302848"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=302848","title":{"rendered":"\u041a\u0430\u043a \u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u0431\u043b\u043e\u043a\u0438 \u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u0441\u0432\u043e\u0435\u043c \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0435 \u043d\u0430 Go"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/499930\/\">\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0441\u0447\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u044f\u0441\u044f \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 3 \u0432\u0430\u0436\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430: <\/p>\n<ul>\n<li> \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0431\u043b\u043e\u043a\u0430<\/li>\n<li> \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/li>\n<li> \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442 \u0431\u043b\u043e\u043a\u0438 \u0432 \u0411\u0414 \u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442 \u0438\u0445 \u0442\u0430\u043c \u043f\u043e \u0438\u0445 \u0445\u0435\u0448\u0443 \u0438\u043b\u0438 \u0432\u044b\u0441\u043e\u0442\u0435 (\u0438\u043b\u0438 \u0435\u0449\u0435 \u043a\u0430\u043a \u043d\u0438\u0431\u0443\u0434\u044c).<\/li>\n<\/ul>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/633\/90e\/12c\/63390e12c9217c81913a097b1474cee7.jpg\" alt=\"image\"><br \/>  <a name=\"habracut\"><\/a><br \/>  \u042d\u0442\u043e \u0432\u0442\u043e\u0440\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d \u0434\u043b\u044f \u043f\u0440\u043e\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u043f\u0435\u0440\u0432\u0430\u044f <a href=\"https:\/\/habr.com\/ru\/post\/499732\/\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  \u0412\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043d\u0435 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043a \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044d\u0442\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c: \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0430 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 LevelDB, \u043d\u043e \u043d\u0438 \u0447\u0435\u0433\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e, \u0441\u043a\u0430\u0436\u0435\u043c \u0442\u0443 \u0436\u0435 MySQL. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439: <a href=\"https:\/\/github.com\/Rusldv\/bcstartup\/blob\/master\/transaction\/builder.go\" rel=\"nofollow\">github.com\/Rusldv\/bcstartup\/blob\/master\/transaction\/builder.go<\/a><\/p>\n<p>  \u0412\u043e\u0442 \u0435\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"go\">type TX struct { \tDataType byte\t\t \tTxHash string  \tTxType byte\t \tTimestamp int64\t\t \tINs []TxIn \tOUTs []TxOut }  type TxIn struct { \tThatTxHash string \tTxOutN int \tByteCode string }  type TxOut struct { \tValue int \tByteCode string }<\/code><\/pre>\n<p>  \u0412 TX \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 (\u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 2), \u0445\u0435\u0448 \u044d\u0442\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0442\u0438\u043f \u0441\u0430\u043c\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043c\u0435\u0442\u043a\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u0445\u043e\u0434\u044b \u0438 \u0432\u044b\u0445\u043e\u0434\u044b. \u0412\u0445\u043e\u0434\u044b TxIn \u0445\u0440\u0430\u043d\u044f\u0442 \u0445\u0435\u0448 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f, \u043d\u043e\u043c\u0435\u0440 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0445\u043e\u0434\u0430 \u0438 \u0431\u0430\u0439\u0442\u043a\u043e\u0434, \u0430 \u0432\u044b\u0445\u043e\u0434\u044b TxOut \u0445\u0440\u0430\u043d\u044f\u0442, \u043a\u0430\u043a\u043e\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0442\u043e\u0436\u0435 \u0431\u0430\u0439\u0442\u043a\u043e\u0434.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043d\u0430\u0434 \u0441\u0432\u043e\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f, \u0442.\u0435. \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<p>  \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0441\u043b\u0443\u0436\u0438\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f transaction.NewTransaction(txtype byte) *TX.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 AddTxIn(thattxhash []byte, txoutn int, code []byte) (*TxIn, error) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0445\u043e\u0434 \u043a \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 AddTxOut(value int, data []byte) (*TxOut, error) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u0445\u043e\u0434 \u043a \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 ToBytes() []byte \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0432 \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0439 \u0441\u0440\u0435\u0437.<\/p>\n<p>  \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f preByteHash(bytes []byte) string \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432 Build() \u0438 Check() \u0434\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0445\u0435\u0448\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0441 \u0445\u0435\u0448\u0430\u043c\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 \u0438\u0437 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 JavaScript.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 Build() \u0437\u0430\u0434\u0430\u0435\u0442 \u0445\u0435\u0448 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: tx.TxHash = preByteHash(tx.ToBytes()).<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 ToJSON() string \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0432 JSON \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 FromJSON(data []byte) error \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438\u0437 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 JSON, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432 \u0432\u0438\u0434\u0435 \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u0441\u043b\u0430\u0439\u0441\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 Check() bool \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0445\u0435\u0448 \u0438\u0437 \u043f\u043e\u043b\u044f \u0445\u0435\u0448\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0441 \u0445\u0435\u0448\u0435\u043c, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 (\u0431\u0435\u0437 \u0443\u0447\u0435\u0442\u0430 \u043f\u043e\u043b\u044f \u0445\u0435\u0448\u0430).<\/p>\n<p>  \u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 \u0431\u043b\u043e\u043a: <a href=\"https:\/\/github.com\/Rusldv\/bcstartup\/blob\/master\/block\/builder.go\" rel=\"nofollow\">github.com\/Rusldv\/bcstartup\/blob\/master\/block\/builder.go<\/a><\/p>\n<p>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u043b\u043e\u043a\u0430 \u0431\u043e\u043b\u0435\u0435 \u043e\u0431\u044a\u0435\u043c\u043d\u0430\u044f:<\/p>\n<pre><code class=\"go\">type Block struct { \tDataType byte\t\t\t\t \tBlockHeight int\t\t\t\t\t         Timestamp int64\t\t\t\t          HeaderSize int\t\t\t\t\t         PrevBlockHash string\t\t\t\t          SelfBlockHash string\t\t\t \tTxsHash string\t\t\t \tMerkleRoot string \tCreatorPublicKey string\t\t\t \tCreatorSig string \tVersion int \tTxsN int \tTxs []transaction.TX }<\/code><\/pre>\n<p>  DataType \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e \u043d\u0435\u043c\u0443 \u043d\u043e\u0434\u0430 \u0438 \u043e\u0442\u043b\u0435\u0447\u0430\u0435\u0442 \u0431\u043b\u043e\u043a \u043e\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u0431\u043b\u043e\u043a\u0430 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e 1.<\/p>\n<p>  BlockHeight \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u044b\u0441\u043e\u0442\u0443 \u0431\u043b\u043e\u043a\u0430.<br \/>  Timestamp \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043c\u0435\u0442\u043a\u0443.<br \/>  HeaderSize \u0440\u0430\u0437\u043c\u0435\u0440 \u0431\u043b\u043e\u043a\u0430 \u0432 \u0431\u0430\u0439\u0442\u0430\u0445.<br \/>  PrevBlockHash \u0445\u0435\u0448 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430, \u0430 SelfBlockHash \u2014 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e.<br \/>  TxsHash \u2014 \u044d\u0442\u043e \u043e\u0431\u0449\u0438\u0439 \u0445\u0435\u0448 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439.<br \/>  MerkleRoot \u2014 \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430 \u041c\u0435\u0440\u043a\u043b\u0430.<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0432 \u043f\u043e\u043b\u044f\u0445 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044f \u0431\u043b\u043e\u043a\u0430, \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044f, \u0432\u0435\u0440\u0441\u0438\u044f \u0431\u043b\u043e\u043a\u0430, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0432 \u0431\u043b\u043e\u043a\u0435 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0430\u043c\u0438 \u044d\u0442\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b:<br \/>  \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f block.NewBlock(): NewBlock(prevBlockHash string, height int) *Block, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0445\u0435\u0448 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u0438 \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u0432 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0435. \u0422\u0430\u043a\u0436\u0435 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u0438\u043f \u0431\u043b\u043e\u043a\u0430 \u0438\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u043f\u0430\u043a\u0435\u0442\u0430 types: <\/p>\n<pre><code class=\"go\">b.DataType = types.BLOCK_TYPE.<\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 AddTx(tx *transaction.TX) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0432 \u0431\u043b\u043e\u043a.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 Build() \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043f\u043e\u043b\u044f \u0431\u043b\u043e\u043a\u0430 \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0435\u0433\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0445\u0435\u0448.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 ToBytesHeader() []byte \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0431\u043b\u043e\u043a\u0430 (\u0431\u0435\u0437 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439) \u0432 \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0439 \u0441\u043b\u0430\u0439\u0441.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 ToJSON() string \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u0431\u043b\u043e\u043a \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 JSON \u0432 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 FromJSON(data []byte) error \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 JSON \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 Check() bool \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0445\u0435\u0448 \u0431\u043b\u043e\u043a\u0430 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0432 \u043f\u043e\u043b\u0435 \u0445\u0435\u0448\u0430 \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 GetTxsHash() string \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u0449\u0438\u0439 \u0445\u0435\u0448 \u0432\u0441\u0435\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0432 \u0431\u043b\u043e\u043a\u0435.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 GetMerkleRoot() \u0437\u0430\u0434\u0430\u0435\u0442 \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430 \u041c\u0435\u0440\u043a\u043b\u0430 \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0432 \u0431\u043b\u043e\u043a\u0435.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 Sign(privk string) \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0431\u043b\u043e\u043a \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c \u043a\u043b\u044e\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044f \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 SetHeight(height int) \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0441\u043e\u0442\u0443 \u0431\u043b\u043e\u043a\u0430 \u0432 \u043f\u043e\u043b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 GetHeight() int \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u044b\u0441\u043e\u0442\u0443 \u0431\u043b\u043e\u043a\u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c \u043f\u043e\u043b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 ToGOBBytes() []byte \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0431\u043b\u043e\u043a \u0432 GOB \u0444\u043e\u0440\u043c\u0430\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u0432\u0438\u0434\u0435 \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u0441\u043b\u0430\u0439\u0441\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 FromGOBBytes(data []byte) error \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0430 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0431\u043b\u043e\u043a\u0430 \u0438\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u0441\u043b\u0430\u0439\u0441\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 GOB.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 GetHash() string \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0445\u0435\u0448 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 GetPrevHash() string \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0445\u0435\u0448 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 SetPublicKey(pubk string) \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432 \u0431\u043b\u043e\u043a \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044f \u0431\u043b\u043e\u043a\u0430.<\/p>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Block \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u043e \u0441\u0435\u0442\u0438 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 LevelDB.<\/p>\n<p>  \u0417\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 blockchain: <a href=\"https:\/\/github.com\/Rusldv\/bcstartup\/tree\/master\/blockchain\" rel=\"nofollow\">github.com\/Rusldv\/bcstartup\/tree\/master\/blockchain<\/a><\/p>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u043b\u043e\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IBlock:<\/p>\n<pre><code class=\"go\">type IGOBBytes interface { \tToGOBBytes() []byte \tFromGOBBytes(data []byte) error }  type IBlock interface { \tIGOBBytes \tGetHash() string \tGetPrevHash() string \tGetHeight() int \tCheck() bool  }<\/code><\/pre>\n<p>  \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 init():   <\/p>\n<pre><code class=\"go\">db, err = leveldb.OpenFile(BLOCKCHAIN_DB_DEBUG, nil).<\/code><\/pre>\n<p>  CloseDB() \u044d\u0442\u043e \u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u0434\u043b\u044f db.Cloce() \u2014 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u043f\u0430\u043a\u0435\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0411\u0414.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f SetTargetBlockHash(hash string) error \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432 \u0411\u0414 \u0445\u0435\u0448 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u0441 \u043a\u043b\u044e\u0447\u0435\u043c \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043e\u0439 BLOCK_HASH.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f GetTargetBlockHash() (string, error) \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0445\u0435\u0448 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430, \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0439\u0441\u044f \u0432 \u0411\u0414.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f SetTargetBlockHeight(height int) error \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432 \u0411\u0414 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0441\u043e\u0442\u044b \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0430 \u0434\u043b\u044f \u043d\u043e\u0434\u044b \u0441 \u043a\u043b\u044e\u0447\u0435\u043c \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043e\u0439 BLOCK_HEIGHT.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f GetTargetBlockHeight() (int, error) \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u044b\u0441\u043e\u0442\u0443 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0430 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u043e\u0434\u044b, \u0445\u0440\u0430\u043d\u0438\u043c\u0443\u044e \u0432 \u0411\u0414.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f CheckBlock(block IBlock) bool \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0431\u043b\u043e\u043a\u0430 \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u0432 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f AddBlock(block IBlock) error \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u043b\u043e\u043a \u0432 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0431\u043b\u043e\u043a\u043e\u0432 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0444\u0430\u0439\u043b\u0435 explore.go \u043f\u0430\u043a\u0435\u0442\u0430 blockchain:<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f GetBlockByHash(hash string) (*block.Block, error) \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u043b\u043e\u043a\u0430, \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0442\u0443\u0434\u0430 \u0431\u043b\u043e\u043a \u0438\u0437 \u0411\u0414 \u0445\u0435\u0448 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430 \u043d\u0435\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0433\u0435\u043d\u0435\u0437\u0438\u0441\u0430 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 Genesis() error \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 genesis.go \u043f\u0430\u043a\u0435\u0442\u0430 blockchain.<\/p>\n<p>  \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e \u043a \u043d\u043e\u0434\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 WebSocket.<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/499930\/\"> https:\/\/habr.com\/ru\/post\/499930\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/499930\/\">\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0441\u0447\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u044f\u0441\u044f \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 3 \u0432\u0430\u0436\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430: <\/p>\n<ul>\n<li> \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0431\u043b\u043e\u043a\u0430<\/li>\n<li> \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/li>\n<li> \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442 \u0431\u043b\u043e\u043a\u0438 \u0432 \u0411\u0414 \u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442 \u0438\u0445 \u0442\u0430\u043c \u043f\u043e \u0438\u0445 \u0445\u0435\u0448\u0443 \u0438\u043b\u0438 \u0432\u044b\u0441\u043e\u0442\u0435 (\u0438\u043b\u0438 \u0435\u0449\u0435 \u043a\u0430\u043a \u043d\u0438\u0431\u0443\u0434\u044c).<\/li>\n<\/ul>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/633\/90e\/12c\/63390e12c9217c81913a097b1474cee7.jpg\" alt=\"image\">  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-302848","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/302848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=302848"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/302848\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=302848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=302848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=302848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}