tag:blogger.com,1999:blog-34268930356417458052024-03-21T01:26:16.158-07:00Cách Mạng 4.0Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-3426893035641745805.post-3411116477079074752021-02-28T19:37:00.001-08:002021-02-28T19:37:16.151-08:00Ai tạo ra Bitcoin?<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl74YbJW5AJ_tW3PK5iph_9iozwZcLLWEOgUS_7ouualIqExMFH0dAfBuoj07T0j7WVdc_tELW8FZkQD6jS0shCyCYreFjibJutgp8eBqYmiQaB1mBtVgXuAOUui5mnPYKNl74LD-31RE/s783/cha-de-bitcoin.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="608" data-original-width="783" height="497" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl74YbJW5AJ_tW3PK5iph_9iozwZcLLWEOgUS_7ouualIqExMFH0dAfBuoj07T0j7WVdc_tELW8FZkQD6jS0shCyCYreFjibJutgp8eBqYmiQaB1mBtVgXuAOUui5mnPYKNl74LD-31RE/w640-h497/cha-de-bitcoin.jpeg" width="640" /></a></div><br /><h3 style="text-align: left;"><br /></h3><h3 style="text-align: left;"> <span style="color: #333333; font-family: "Noto Serif", serif; font-size: 1.1rem; font-weight: bold; text-align: center;">Rất nhiều giả thuyết về danh tính người sáng tạo Bitcoin. Thậm chí, có thuyết cho rằng Satoshi Nakamoto là cách chơi chữ đầu của 4 công ty: Samsung, Toshiba, Nakamichi và Motorola.</span></h3><p style="text-align: left;"><span style="color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px;">Satoshi Nakamoto - cha đẻ của Bitcoin đến nay vẫn là một ẩn số. Thậm chí, người ta vẫn chưa thể biết danh tính này là của một người hay nhóm tổ chức đã tạo ra đồng tiền mã hóa nổi tiếng nhất hiện nay.</span></p><h4 style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; float: left; font-family: "Noto Serif", serif; font-size: 1.8rem; letter-spacing: -1px; line-height: 1.2; margin: 10px 0px; outline: 0px; padding: 30px 108px 10px; text-align: center; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;">"Tôi là nam giới, 37 tuổi sống tại Nhật Bản"</h4><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; clear: both; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 0px 0px 18px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Vì tin rằng thế giới có thể tồn tại một hệ thống giao dịch mà các thành viên không cần tin tưởng lẫn nhau, năm 2007, Satoshi đã khởi tạo ra mã nguồn mở Bitcoin Core (tên cũ Bitcoin-Qt) để công chúng sử dụng đồng thuật toán.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Năm 2008, ông tự xuất bản bài viết <em style="-webkit-font-smoothing: antialiased; background: transparent; border: 0px; box-sizing: border-box; font-size: 17.6px; margin: 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">"Bitcoin: Hệ thống tiền điện tử ngang hàng"</em>, mô tả việc sử dụng mạng lưới ngang hàng sẽ loại bỏ khả năng gian lận dùng hai giao dịch khác nhau để cùng chi tiêu số dư một tài khoản (Double spending).</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Giải pháp chống lại cách gian lận trên là ủy quyền cho bên thứ 3 đáng tin cậy đứng ra làm trung gian, xác minh giao dịch đã được thực hiện hay chưa. Satoshi đã loại bỏ bên thứ 3 này, vốn thông thường là các ngân hàng, tạo ra hệ thống phi tập trung cho giao dịch.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Blockchain ra đời từ đây. Cuốn sổ cái công cộng ghi lại mọi dữ liệu, truyền tải minh bạch, toàn vẹn, không thể thay đổi hay gian lận. Sâu hơn, blockchain là dạng cơ sở dữ liệu phi tập trung, trong đó thông tin được lưu trữ bằng các block, từng block lại được liên kết mã hóa với nhau.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Ngày 9/1/2009, Nakamoto phát hành phần mềm Bitcoin phiên bản 0.1 trên SourceForge và khởi chạy mạng lưới bằng cách đào khối gốc Bitcoin (khối số 0). Phần thưởng cho cha đẻ đồng tiền là 50 BTC.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Đáng chú ý, được nhúng trong giao dịch coinbase của khối là dòng chữ: <em style="-webkit-font-smoothing: antialiased; background: transparent; border: 0px; box-sizing: border-box; font-size: 17.6px; margin: 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">"Ngày 3/9/2009, Thủ tướng sắp đưa ra gói cứu trợ thứ 2 cho các ngân hàng"</em>, đề cập đến bài báo trên tờ <em style="-webkit-font-smoothing: antialiased; background: transparent; border: 0px; box-sizing: border-box; font-size: 17.6px; margin: 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">The Times </em>xuất bản tại Anh quốc ngày hôm đó. Ghi chú này vừa đánh dấu mốc của đồng mã hóa nổi tiếng nhất thế giới, vừa là lời mỉa mai sự bất ổn của các ngân hàng.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Đến cuối 2010, cộng đồng phát triển Bitcoin mất liên lạc hoàn toàn với Satoshi.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Ông đưa cho nhà phát triển Gavin Andresen khóa báo động khi mạng lưới Bitcoin bị tấn công. Khóa này có thể dùng để báo động tới toàn mạng lưới ngừng giao dịch.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Satoshi chưa bao giờ tiết lộ bất kỳ thông tin cá nhân nào trong quá trình phát triển Bitcoin, dù từng có lúc đưa ra bình luận về hệ thống các ngân hàng dự trữ một phần (Fractional reserve banking).</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Trong hồ sơ của Satoshi trên Quỹ P2P, nhân vật này tự nhận là nam giới 37 tuổi sống ở Nhật Bản. Tuy nhiên, với khả năng tiếng Anh như người bản địa, cùng với việc phần mềm Bitcoin không hề có bất kỳ dòng chữ tiếng Nhật nào, nhiều ý kiến cho rằng đây khó có khả năng là một người đang sống ở xứ hoa anh đào.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Bằng chứng là cách Satoshi dùng tiếng Anh bản địa trong cả những nhận xét mã nguồn và các bài đăng trên diễn đàn như "bloody hard", "flat", "maths", "greys" hay "colour". Ngoài ra, việc mỉa mai tiêu đề bài báo của <em style="-webkit-font-smoothing: antialiased; background: transparent; border: 0px; box-sizing: border-box; font-size: 17.6px; margin: 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">The Times</em> cho thấy Satoshi có mối quan tâm đặc biệt đến các vấn đề của Anh quốc.</p><h3 style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; float: left; font-family: "Noto Serif", serif; font-size: 1.8rem; letter-spacing: -1px; line-height: 1.2; margin: 10px 0px; outline: 0px; padding: 30px 108px 10px; text-align: center; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;">Bí ẩn lớn nhất của Bitcoin</h3><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; clear: both; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 0px 0px 18px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Nhà nghiên cứu bảo mật Dan Kaminsky cho rằng Nakamoto có thể là tên của nhóm người. Laszlo Hanyecz, nhà phát triển từng trao đổi email với Satoshi cho biết các bản mã được viết quá tốt, khó có thể là sản phẩm bởi chỉ một cá nhân.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Huyền thoại máy tính đang ngồi tù John McAfee tuyên bố Nakamoto là "một đội gồm 11 người". Bình luận về Satoshi, Gavin Andresen cho rằng "Anh ta là lập trình viên xuất sắc, nhưng khá lỳ lạ".</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Kỹ sư phần mềm người Thụy Sĩ Stefan Thomas đã vẽ biểu đồ thời gian cho 500 bài đăng trên diễn đàn Bitcoin của Satoshi. Kết quả cho thấy hầu như không có bài viết nào trong khoảng thời gian từ 14h-20h Nhật Bản. Trong cả các ngày cuối tuần, Satoshi cũng hoạt động với thời gian biểu như thế.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Rất nhiều người được công chúng điền vào sự ẩn danh của Satoshi. Nổi tiếng nhất là cụ ông người Mỹ gốc Nhật sống tại California Dorian Satoshi Nakamoto, sau khi một bài báo của <em style="-webkit-font-smoothing: antialiased; background: transparent; border: 0px; box-sizing: border-box; font-size: 17.6px; margin: 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">News Week</em> vào tháng 3/2014 cho rằng ông chính là nhà sáng lập Bitcoin.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Bài báo gây xôn xao trong cộng đồng tiền mã hóa vì đây là lần đầu tiên, một ấn phẩm chính thống lên tiếng xác minh danh tính của Satoshi.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Cụ thể, tác giả bài viết Leah McGrath Goodman cho hay trong một cuộc phỏng vấn, ông Dorian trả lời rằng: "Tôi không còn liên quan gì đến Bitcoin nên không thể thảo luận về nó. Nó đã được chuyển giao cho người khác và họ đang chịu trách nhiệm. Tôi không còn mối liên hệ nào nữa". Tuy nhiên, ông Dorian phủ nhận thông tin này.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Ngoài ra, có thể kể đến Nick Szabo, nhà phát triển từng xuất bản bài báo "<em style="-webkit-font-smoothing: antialiased; background: transparent; border: 0px; box-sizing: border-box; font-size: 17.6px; margin: 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">bit gold</em>" năm 1998, được xem như tiền thân của Bitcoin; Hal Finney - nhà nghiên cứu tiền mã hoá từ trước khi Bitcoin ra đời, một trong số những người đầu tiên sử dụng phần mềm Bitcoin.</p><p style="-webkit-font-smoothing: antialiased; background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #333333; font-family: "Noto Serif", serif; font-size: 17.6px; margin: 18px 0px; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline;">Thậm chí, nhiều người cũng tin Elon Musk chính là cái tên đã tạo ra đồng tiền làm sôi sục thị trường thời gian đầu 2021. Cũng giống như cụ ông Dorian, nhà sáng lập SpaceX đã phủ nhận nghi ngờ này.</p><div class="the-article-body" style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; clear: both; color: #333333; float: left; font-family: "Noto Serif", serif; font-size: 1.1rem; left: 460px; line-height: 1.6; margin: 0px 0px 0px -300px; outline: 0px; padding: 0px; position: relative; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;"><p style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; float: none; font-size: 17.6px; margin-bottom: 18px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;"><br /></p><p style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; float: none; font-size: 17.6px; margin-bottom: 18px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;"><br /></p><p style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; float: none; font-size: 17.6px; margin-bottom: 18px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;"><br /></p><p style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; float: none; font-size: 17.6px; margin-bottom: 18px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;"><br /></p><p style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; float: none; font-size: 17.6px; margin-bottom: 18px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; outline: 0px; padding: 0px; text-rendering: geometricprecision; text-size-adjust: 100%; vertical-align: baseline; width: 600px;"><br /></p></div>Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com2tag:blogger.com,1999:blog-3426893035641745805.post-18773450207113182972018-09-07T00:25:00.000-07:002018-09-07T00:25:29.377-07:00Phần 6: Tìm hiểu về Smart Contract và đồng tiền ảo Ethereum<br />
<div class="post-footer d-flex align-items-center justify-content-end mb-2" style="-webkit-text-stroke-width: 0px; align-items: center !important; background-color: white; box-sizing: border-box; color: #292b2c; display: flex !important; font-family: Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; justify-content: flex-end !important; letter-spacing: normal; margin-bottom: 1.5rem !important; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div class="social-sharing mr-1 social-sharing--vertical social-sharing--medium" data-v-699851c4="" style="box-sizing: border-box; font-size: 20px; margin-right: 1rem !important;">
<a class="link link--muted" data-original-title="Share a link to this page on Facebook" data-tippy="" data-v-699851c4="" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fviblo.asia%2Fp%2Fphan-6-tim-hieu-ve-smart-contract-va-dong-tien-ao-ethereum-3Q75w2dBlWb&title=Ph%E1%BA%A7n%206%3A%20T%C3%ACm%20hi%E1%BB%83u%20v%E1%BB%81%20Smart%20Contract%20v%C3%A0%20%C4%91%E1%BB%93ng%20ti%E1%BB%81n%20%E1%BA%A3o%20Ethereum" style="background-color: transparent; box-sizing: border-box; color: inherit; text-decoration: none;" target="_blank" tooltip-placement="bottom"><i aria-hidden="true" class="fa fa-facebook" data-v-699851c4="" style="-webkit-font-smoothing: antialiased; box-sizing: border-box; display: inline-block; font-family: FontAwesome; font-size: inherit; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1; text-rendering: auto;"></i></a></div>
</div>
<br />
<div class="md-contents article-content__body my-2 flex-fill" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #292b2c; flex: 1 1 auto !important; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.75; margin-bottom: 1.5rem !important; margin-top: 1.5rem !important; orphans: 2; overflow: hidden; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/22186f8d-7c8e-4f48-b2c3-06ae1f100255.gif" src="https://images.viblo.asia/22186f8d-7c8e-4f48-b2c3-06ae1f100255.gif" style="border-style: none; box-sizing: border-box; cursor: pointer; display: block; margin: 1.5em auto; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Bài này đang được chỉnh sửa... (Viết vội theo yêu cầu của một anh bạn)</div>
<h1 id="_blockchain-chi-co-tac-dung-giu-tien-0" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.95rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Blockchain chỉ có tác dụng giữ tiền?</h1>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Nối tiếp 5 phần trước, phần này tôi sẽ giới thiệu để các bạn hiểu về khái niệm Smart Contract và đồng tiền ảo Ethereum.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Chúng ta đã hiểu rõ thế nào là tiền ảo, thế nào là giao dịch được bảo mật rất tốt trong Blockchain rồi đúng không?</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Nếu ta hiểu rồi thì sẽ thấy, Blockchain đường như chỉ là một cơ sở dữ liệu để<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">giao dịch</code><span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">mua đi bán lại</code><span> </span>và<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">chứa tiền nong</code><span> </span>là hợp lý. Vì dữ liệu lưu trong các Block của Blockchain theo như 4 phần demo toàn là<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDich</code>. Mà các giao dịch này lại là chuỗi cố định, vì người dùng muốn chắc chắn là chuỗi lưu vào Block phải cố định.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Vậy có vẻ cơ sở dữ liệu Blockchain không có tác dụng gì khác? Bạn nhầm!</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Nếu bây giờ ta muốn lưu thứ gì đó thay đổi được vào một Block thì sao? Hãy tưởng tượng bạn mua bán, nhưng thay vì chuyển tiền, bạn cần trao đổi một giao dịch dưới dạng<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">HỢP ĐỒNG</code>. Nếu là hợp đồng, nghĩa là giao dịch của bạn có chữ<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Nếu-Thì</code>. Nếu thỏa màn điều kiện A, thì tôi trả anh từng này, nếu anh làm tốt hơn, nhanh hơn, thỏa điều kiện B, tôi trả anh mức cao hơn. Hoặc bạn cần trao đổi một loại<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">hợp đồng bảo hiểm</code>. Lúc này hợp đồng ghi: Nếu gãy tay, từng này tiền, nếu liệt nửa người, từng này tiền, nếu không may Die, một cục ngần này tiền. Một khi đã đặt bút ký, thì cả 2 bên bắt đầu thực hiện hợp đồng.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Vậy thì ta thấy là các dữ liệu lưu trong Block lúc này là tiền. nhưng lại kèm theo điều kiện "If-Else". Cần phải có một loại cơ sở dữ liệu lưu trữ được dạng data kỳ dị này. Và cũng cần có một người đứng ra phân giải nếu hợp đồng có tranh chấp kiện tụng.</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
May thay, có người đã sớm nghĩ ra thuật toán cho nó, đó là lý do Smart Contract và đồng tiền ảo Ethereum ra đời.</div>
<h1 id="_smart-contract-1" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.95rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Smart Contract</h1>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Smart Contract chính là cái cục If-Else nằm bên trong một Blockchain, kèm theo mỗi giao dịch. Làm thế nào để có thể kiểm tra được If-Else. Nghe như là phải chạy một đoạn lệnh If-Else ấy nhỉ? Đúng vậy. Smart Contract là một chuỗi, trong đó chứa luôn một đoạn lệnh If-else luôn. Thậm chí là có cả các hàm API trong đây. Để làm gì? để các bên liên quan có thể mở SmartContract và chạy thử đoạn lệnh đó, xem là hiện tại<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">đầu vào</code><span> </span>đã đủ theo tiêu chuẩn<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Hợp đồng</code><span> </span>hay chưa.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Đầu vào ở đây là gì?</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Ờ, cái này có thể là bất kỳ cái gì, vì nó không liên quan đến Blockchain, nó liên quan đến các hàm viết trong API để tính toán xem điều kiện khi nào thỏa mãn. Ví dụ, Smart Contract về bảo hiểm, thì họ sẽ đính kèm API điều kiện hưởng bảo hiểm. Đầu vào sẽ là hóa đơn bệnh viện. Bạn có đủ chấn thương, tự động số tiền sẽ nhảy vào tài khoản của bạn, không cần mất công làm hồ sơ rồi đi đòi.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Lấy thêm một ví dụ nữa. Một công ty muốn kêu gọi vốn đầu tư, họ đặt ra giới hạn là đến 1-1-2019 ra mắt 1 chiếc ô tô bay. Họ cần mỗi người đóng góp 1000usd thì sẽ được mua ô tô đó giá gốc. Góp vốn được 1tr đô thành công thì họ sẽ hứa giao xe đúng hẹn vào ngày 1-1-2019. Nếu k đủ 1tr đô thì họ sẽ trả lại tiền, ai về nhà nấy, họ cũng hủy luôn dự án và coi như chưa ai nói gì nhé. Dự án kiểu này thường được kêu gọi vốn bằng Kickstarter. Một server trung gian để Người góp vốn và người làm dự án phải đổ tiền vào. Bên thứ ba này sẽ giữ tiền của cả 2 bên để hai bên<span> </span><strong style="box-sizing: border-box; font-weight: bolder;">TIN TƯỞNG NHAU</strong>.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Giờ nếu Blockchain tham gia vào. thì SmartContract sẽ là cái dùng để<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">TIN TƯỞNG NHAU</code>. Vì Blockchain và SmartContract luôn luôn công bằng nên rất đáng tin. Và điều kiện đưa vào Contract rất rõ ràng, không ai chạy làng được và không ai sửa được. Vậy nên chắc chắn nó sẽ là nền tảng cho tương lai, khi mà không cần bên thứ 3 trung gian đứng ra bảo lãnh nữa.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Vậy thì công nghệ SmartContract chỉ đơn giản như thế thôi ư, đồng tiền Ethereum đóng vai trò gì giữa Blockchain và SmartContract?</div>
</blockquote>
<h1 id="_ethereum-2" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.95rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Ethereum</h1>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Đồng tiền ảo Ethereum dĩ nhiên là có một chuỗi Blockchain của riêng nó, khác hẳn với công nghệ của đồng tiền BitCoin. Ethereum sinh ra là để hỗ trợ lưu trữ được SmartContract vào Block. Và hơn thế nữa. Ethereum có các hàm API để phục vụ việc thực hiện đầu vào của SmartContract và tính toán SmartContract xem nó đúng hay sai, và trả tiền cho các bên liên quan.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Vậy nếu mỗi đoạn lệnh If-Else đưa vào Blockchain lại phải có một API để xử lý nó riêng biệt, thế thì người dùng sẽ tốn công sức lắm nhỉ?</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Đúng là như vậy. Và Ethereum đã định nghĩa ra một ngôn ngữ mới (có thể gọi nó là ngôn ngữ lập trình mới) là Solidity để giải quyết việc này. Đây là một cú pháp, quy định các điều khoản hợp đồng và cách để xác thực nó, cách để connect API trong một Block.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Người dùng sau khi tạo ra một Block thì viết các truy vấn bằng Solidity để theo dõi Block đó.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
(to be continues....)</div>
</div>
Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com0Hà Nội, Hoàn Kiếm, Hà Nội, Việt Nam21.0277644 105.8341597999999520.968477399999998 105.75347879999995 21.0870514 105.91484079999995tag:blogger.com,1999:blog-3426893035641745805.post-54229616572777614962018-09-07T00:23:00.000-07:002018-09-07T00:23:28.451-07:00Phần 5: Cách bảo mật giao dịch Tiền ảo bằng Ví tiền ảo<br />
<div class="post-footer d-flex align-items-center justify-content-end mb-2" style="-webkit-text-stroke-width: 0px; align-items: center !important; background-color: white; box-sizing: border-box; color: #292b2c; display: flex !important; font-family: Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; justify-content: flex-end !important; letter-spacing: normal; margin-bottom: 1.5rem !important; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div class="social-sharing mr-1 social-sharing--vertical social-sharing--medium" data-v-699851c4="" style="box-sizing: border-box; font-size: 20px; margin-right: 1rem !important;">
<a class="link link--muted" data-original-title="Share a link to this page on Facebook" data-tippy="" data-v-699851c4="" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fviblo.asia%2Fp%2Fphan-5-cach-bao-mat-giao-dich-tien-ao-bang-vi-tien-ao-OeVKB43ylkW&title=Ph%E1%BA%A7n%205%3A%20C%C3%A1ch%20b%E1%BA%A3o%20m%E1%BA%ADt%20giao%20d%E1%BB%8Bch%20Ti%E1%BB%81n%20%E1%BA%A3o%20b%E1%BA%B1ng%20V%C3%AD%20ti%E1%BB%81n%20%E1%BA%A3o" style="background-color: transparent; box-sizing: border-box; color: inherit; text-decoration: none;" target="_blank" tooltip-placement="bottom"><i aria-hidden="true" class="fa fa-facebook" data-v-699851c4="" style="-webkit-font-smoothing: antialiased; box-sizing: border-box; display: inline-block; font-family: FontAwesome; font-size: inherit; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1; text-rendering: auto;"></i></a><a class="link link--muted" data-original-title="Share a link to this page on Twitter" data-tippy="" data-v-699851c4="" href="https://twitter.com/intent/tweet?url=https://viblo.asia/p/phan-5-cach-bao-mat-giao-dich-tien-ao-bang-vi-tien-ao-OeVKB43ylkW&text=Ph%E1%BA%A7n%205%3A%20C%C3%A1ch%20b%E1%BA%A3o%20m%E1%BA%ADt%20giao%20d%E1%BB%8Bch%20Ti%E1%BB%81n%20%E1%BA%A3o%20b%E1%BA%B1ng%20V%C3%AD%20ti%E1%BB%81n%20%E1%BA%A3o&hashtags=JavaScript%2CHTML5%2CSecurity%2CBlockchain%2CEthereum%2CCryptocurrency%2CLearn%20blockchain%2CBitcoin" style="background-color: transparent; box-sizing: border-box; color: inherit; margin-left: 1rem; text-decoration: none;" target="_blank" tooltip-placement="bottom"><i aria-hidden="true" class="fa fa-twitter" data-v-699851c4="" style="-webkit-font-smoothing: antialiased; box-sizing: border-box; display: inline-block; font-family: FontAwesome; font-size: inherit; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1; text-rendering: auto;"></i></a></div>
</div>
<br />
<div class="md-contents article-content__body my-2 flex-fill" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #292b2c; flex: 1 1 auto !important; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.75; margin-bottom: 1.5rem !important; margin-top: 1.5rem !important; orphans: 2; overflow: hidden; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/183a908f-269c-4a61-b301-62dd8844a714.gif" src="https://images.viblo.asia/183a908f-269c-4a61-b301-62dd8844a714.gif" style="border-style: none; box-sizing: border-box; cursor: pointer; display: block; margin: 1.5em auto; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Bài này đang được chỉnh sửa nên các bạn đọc trước cho vui thôi nhé...</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Với 4 phần trước, bạn đã tạo được cho mình một đế chế tiền ảo. Người dùng bắt đầu đổ xô đi đào loại tiền ảo MyCoin mà bạn đã xây dựng sẵn, và hí hửng nhặt về các địa chỉ<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">VÍ CÁ NHÂN</code><span> </span>của họ.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Vấn đề dễ nhận thấy nhất là MyCoin không hề bảo mật. Địa chỉ ta thấy chỉ là một chuỗi<span> </span><strong style="box-sizing: border-box; font-weight: bolder;">"Dia Chi Cua Ong A"</strong>,<span> </span><strong style="box-sizing: border-box; font-weight: bolder;">"DiaChiViNhanTienThuong"</strong>. Một hacker nếu sửa đổi bất kỳ bản ghi nào trong chuỗi Blockchain là không hợp lệ vì sai Hash. Nhưng nếu anh ta có Block cuối cùng. Anh ta nhanh tay lấy<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DiaChiViTienThuongCuaBan</code><span> </span>và chuyển hết số tiền vào<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">ViHacker</code><span> </span>và nhét Block mới vào chuỗi MangBlock, thế là nghiễm nhiên giao dịch thành công. Bùm, bạn mất sạch tiền.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Vậy thì làm cách nào để ngăn chặn việc này? Phải có cách nào đó để bảo mật chứ nhỉ?</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Tất nhiên rồi, ta lại phải nghĩ ra vài thuật toán cao siêu để ngăn chặn hacker ăn cắp tiền. Địa chỉ nhận tiền lúc này không thể là một chuỗi nữa. Cần phải mã hóa nó đi.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Nếu bạn chỉ nghĩ đơn giản là mã hóa chuỗi<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DiaChiViTienThuongCuaBan</code><span> </span>thành một chuỗi loằng ngoằng MD5 dạng<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">kj 82y382h ưco2c3 o2hc3o</code><span> </span>và giấu đi để hacker không thể nhìn thấy thì bạn nhầm. Chuỗi địa chỉ của bạn trong Block luôn luôn được hiển thị, không có cách nào mã hóa nó để không ai nhìn thấy được. Bởi vì Blockchain luôn minh bạch, giao dịch phải được kiểm chứng bởi các người dùng khác trong hệ thống, nếu không họ sẽ không đồng ý thêm giao dịch của bạn vào chuỗi Block.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Lúc này ta cần đến thuật toán key-pair (cặp key gồm public key và private key).</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Public key sẽ được dùng để nhét vào các giao dịch trong các Block. Ai cũng nhìn thấy đc. Còn Private Key chỉ bạn biết. Public Key như là Tên Tài Khoản, Private Key là mật khẩu. Khi nối 2 thứ này lại thì sẽ ra được địa chỉ ví tiền thật của bạn.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Vấn đề đã giải quyết xong chưa nhỉ? Vấn đề vẫn còn nguyên. Vì PublicKey và PrivateKey là một cặp, được tạo ra bởi một thuật toán nào đó. Nếu bạn đem nhét PublicKey vào<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDich</code>. Thì hầu như giao dịch nào của bạn trong hệ thống cũng có y hệt một chuỗi. Có chuỗi này, hacker bắt đầu brusforce-attach (tấn công theo kiểu mò mẫn, thử lần lượt đến khi nào đúng thì thôi). Và đến lúc nào đó thì Private Key của bạn vẫn lòi ra. PrivateKey thậm chí có người đặt dài đến 128 ký tự. Nhưng vẫn vô vọng thôi.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Phải có cách cao tay hơn. Đó là mỗi một giao dịch ta phải có một địa chỉ hoàn toàn khác nhau.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
WHAT?! Thế thì làm thế nào để tiền gửi về đúng địa chỉ của ví của ta được?</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Bình tõm. Cái chuỗi ngẫu nhiên nhét vào các<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDich</code><span> </span>thường được gọi là Digital Signature (Chữ ký điện tử). Nhưng chuỗi chữ ký này mỗi giao dịch là khác nhau, bởi vì nó mã hóa privateKey + Data trong chuỗi.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
(chỗ này đang viết tiếp...)</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Chú ý là Địa chỉ ví tiền của bạn đã có. Nhưng giá trị số tiền thật trong ví lại không được lưu trong ví, không hề có con số tổng tiền trong ví lưu ở server nào cả. Toàn bộ giao dịch tăng giảm ví tiền của bạn đều vẫn được lưu ở<span> </span><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">cơ sở dữ liệu</code><span> </span>Blockchain. Và cơ sở dữ liệu này thì cực kỳ bảo mật như bạn đã biết. Chỉ có thể gửi và nhận chứ không sửa xóa được. Muốn tính xem bạn đang có bao nhiêu tiền bạn vẫn phải gọi hàm TinhTienTrongVi() và lục lại toàn bộ giao dịch trên Blockchain để đếm tổng. Bạn hoàn toàn yên tâm là không ai đụng được vào đồng nào của bạn trong ví. Bạn chỉ cần cất thật kỹ privateKey là có thể đắp chăn nằm ngủ.</div>
<h1 id="_ket-luan-0" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.95rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Kết luận</h1>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Như vậy là ta đã hiểu được về địa chỉ ví tiền ảo và tại sao nó lại an toàn đến mưc không ai có thể biết được ai sở hữu và không có cách nào lấy đc tiền từ tài khoản ra khỏi blockchain.</div>
</div>
Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com0Hà Nội, Hoàn Kiếm, Hà Nội, Việt Nam21.0277644 105.8341597999999520.968477399999998 105.75347879999995 21.0870514 105.91484079999995tag:blogger.com,1999:blog-3426893035641745805.post-61254090382077230182018-09-07T00:21:00.000-07:002018-09-07T00:21:34.396-07:00Phần 4: Thử trao đổi tiền MyCoin trong mạng ngang hàng bằng Blockchain<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Ở 3 phần trước ta đã thấy cách đào tiền ảo, cách giao dịch, nhưng chưa thấy rõ cách vận hành của một khối Blockchain trao đi đổi lại giữa các máy người dùng như thế nào.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Bởi vì không hề có server chung gian để xử lý giao dịch và thêm Block vào Blockchain. Mỗi máy người dùng sẽ đóng vai trò là một máy chủ để xử lý chuỗi Blockchain nhận được từ người khác, rồi bạn tạo giao dịch mới hoặc đào hash cho giao dịch tạm hoãn và cập nhật chuỗi Blockchain của bạn, gửi cho những người khác trong mạng.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hình dung nó sẽ giống như thế này<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/98337971-3d48-4c7e-ae62-52c42a205abf.gif" src="https://images.viblo.asia/98337971-3d48-4c7e-ae62-52c42a205abf.gif" style="border-style: none; box-sizing: border-box; cursor: pointer; display: block; margin: 1.5em auto; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Nhưng vấn đề là rất dễ xảy ra tranh chấp. Vì bạn và người A cùng sửa chuỗi Blockchain và cùng đào được Hash, nghĩa là 2 bạn cùng thêm 1 Block mới vào Blockchain, nhưng 2 block của bạn là khác nhau. Vậy phải làm thế nào?</div>
</blockquote>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Lúc này ta phải có thuật toán để <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">nối</code> Block lại.</div>
<h1 id="_tranh-chap-block-trong-block-chain-0" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Tranh chấp Block trong Block chain</h1>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Để xác định được một Block được thêm vào là chuẩn chỉnh hay chưa. Thì hàm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">KiemTraTinhToanVen</code>sẽ cho biết. Nhưng 2 Blockchain toàn vẹn, lại có 2 phần tử cuối cùng khác nhau, nếu lấy một trong 2 Blockchain và bỏ cái kia đi, thì một vài giao dịch đã tự dưng bị mất.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Phần này ta sẽ thử viết Demo để có thể giải quyết được bài toán này.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
(đang viết Demo...)</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Như vậy sau khi giải quyết được bài toán, ta thấy rằng, Blockchain nào dài nhất sẽ được quyền ưu tiên. Vì thời gian Hash mã của nó nhiều nhất, giá trị tiền ảo bên trong nó cũng đang lưu nhiều nhất. Tuy nhiên các khối Block khác từ các người dùng khác vẫn sẽ được nối thêm vào để tránh làm mất dữ liệu.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Tuy nhiên thuật toán ưu tiên Blockchain dài nhất này lại có một nhược điểm chết người. đó là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">51% Attach</code></div>
<h1 id="_51-attach-1" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
51% Attach</h1>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 0.5em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Đây nghĩa là gì?</div>
</blockquote>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Nếu giờ có Hacker nào đó mua một cỗ máy siêu khủng, và số lượng Block ban đầu chưa nhiều, anh ta sẽ thử nhanh tay đào Hash và tạo ra một Blockchain dài hơn tất cả, lúc này anh ta sẽ được quyền chiếm giữ hệ thống.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Tại sao? tại vì anh ta sẽ ép được các máy khác lưu Blockchain đã chỉnh sửa của anh ta. Nếu những người kia muốn thay đổi lại, họ phải đào hash. Trong lúc đó anh này cũng Hash. Đôi bên cứ giằng co như vậy. Và con số 51% có nghĩa là sức mạnh tính toán hash được nhiều hơn tổng khối lượng hash của toàn hệ thống thì sẽ chiếm được hệ thống.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Dĩ nhiên là điều này khó xảy ra. Vì sức mạnh tính toán sẽ là tổng của toàn bộ người chơi trong hệ thống. Nếu hệ thống lớn như BitCoin thì hàng triệu người cùng hì hụi đào, không một máy cá nhân hoặc một công ty nào đủ điện để chiến đấu với từng ấy người.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Nhưng biết đâu máy tính lượng tử ra đời với khả năng tính hàng triệu tỉ phép tính giây sẽ cho phép Hash nhanh như điện, lúc đó mới đáng lo.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Cũng có một số ý kiến cho rằng, nếu hack được một đồng tiền bằng phương pháp 51% attach, toàn bộ người chơi trong hệ thống sẽ biết, và họ sẽ từ bỏ đồng tiền đó để nó từ từ chết đi, cho giảm giá trị, nên việc hack này sẽ không có nhiều người ham hố. Bạn thì bạn nghĩ sao?</div>
<h1 id="_ket-luan-2" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Kết luận</h1>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Bạn hãy cùng comment các sũy nghĩ của bạn về Blockchain và các đường link hay khác để các bạn khác tham khảo với nhé!</div>
Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com1Hà Nội, Hoàn Kiếm, Hà Nội, Việt Nam21.0277644 105.8341597999999520.968477399999998 105.75347879999995 21.0870514 105.91484079999995tag:blogger.com,1999:blog-3426893035641745805.post-6845850904411278572018-09-07T00:18:00.000-07:002018-09-07T00:18:07.794-07:00Phần 3: Tự tạo ra một đồng tiền ảo Bitcoin của mình<h1 id="_tien-ao-la-gi-0" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Tiền ảo là gì?</h1>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Ở 2 phần trước ta đã hiểu thế nào là Blockchain, và thế nào là đào Hash để ngăn chặn giao dịch ảo. Tuy nhiên Blockchain của chúng ta vì quá khó để tạo giao dịch. Nó trở nên hoàn toàn kém hấp dẫn và chả ai đi bỏ thời gian ra đào Hash rồi nhét data vào chuỗi của chúng ta làm gì. Chúng ta cần tạo ra cái gì đó để kích cầu.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Lúc này thì khái niệm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">TIỀN ẢO</code> mới ra đời. Tiền ảo là một <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">PHẦN THƯỞNG</code> dành cho người nào bỏ công sức để đào hash, để xác thực các giao dịch và thêm thành công các Block vào chuỗi của chúng ta. Ban đầu, lượng tiền ảo (hay tiền thưởng) đào được là rất ít, dẫn đến có sự cạnh tranh, sự trao đi đổi lại (trading) lấy tiền thật. Dần dần đồng tiền của ta tạo ra sẽ lên giá.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Với suy nghĩ đơn giản như vậy, ta hãy bắt tay vào sửa file html và tạo nên đồng tiền ảo đầu tiên của mình.</div>
<h1 id="_thuat-toan-de-tao-ra-mot-dong-tien-ao-1" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Thuật toán để tạo ra một đồng tiền ảo</h1>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Để tạo ra một đồng tiền ảo hoàn chỉnh, ta cần cho phép giao dịch bằng đồng tiền ảo đó. Ở ví dụ trước ta đã tạo ra một dạng <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DuLieu</code> có thể chứa giao dịch. Nhưng để chính thức hỗ trợ giao dịch, ta tạo một class mới dùng để chứa giao dịch như sau:</div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class="language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/*=== ĐÂY LÀ CLASS MÔ TẢ CẤU TRÚC CỦA MỘT GIAO DỊCH ===*/</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">GiaoDich</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Các thông tin của một giao dịch vẫn cơ bản như cũ</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">constructor</span><span class="token punctuation" style="box-sizing: border-box;">(</span>DiaChiGui<span class="token punctuation" style="box-sizing: border-box;">,</span> DiaChiNhan<span class="token punctuation" style="box-sizing: border-box;">,</span> GiaTri<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DiaChiGui <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> DiaChiGui<span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DiaChiNhan <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> DiaChiNhan<span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaTri <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> GiaTri<span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Lúc này trong phần định nghĩa của Blockchain, ta cần tạo thêm 2 biến mới.</div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class="language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/*Phần này mới được thêm vào*/</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaoDichTamHoan <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token punctuation" style="box-sizing: border-box;">[</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Là mảng các giao dịch mới thêm vào mảng Blockchain và chưa được Hash xong.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>TienThuong <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">100</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Là phần thưởng dành cho các miner (người đào hash) cho việc thêm mới thành công mảng GiaoDichTamHoan vào Blockchain.</span>
</code></pre>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Thứ nhất là mảng <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDichTamHoan</code>. Mảng này sẽ chứa toàn bộ các giao dịch mà chưa có mã Hash, đang đợi máy đào để được thêm vào Blockchain. Điều này sẽ cho phép nhiều giao dịch được thêm đồng thời vào Blockchain và cùng nằm trên một Block. Người giao dịch và người đào Block cũng độc lập với nhau.</div>
</blockquote>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Thứ hai là biến <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">TienThuong</code> để lưu giá trị mặc định của khoản tiền thưởng cho <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Người đào coin</code>. Lượng tiền thưởng này như đã nói ở phần trước, sẽ bị giới hạn, bởi vì đến một lúc nào đó không thể tạo thêm Hash => không ai có thêm tiền thưởng nữa. Và như vậy nghĩa là đồng MyCoin của chúng ta có tính chất khan hiếm, y như vàng bạc vậy.</div>
</blockquote>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Trong số các hàm của Blockchain, ta thay hàm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DaoBlock()</code> bằng hàm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DaoTienAo()</code>. Bởi vì một Block lúc này không thể dễ dàng thêm vào Blockchain được nữa, mà phải có ai đó <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">đào</code> Hash cho Block đó để nó đc thêm vào chuỗi.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
<span style="box-sizing: border-box; font-weight: bolder;">Định nghĩa hàm đào tiền ảo như sau:</span></div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class="language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token function" style="box-sizing: border-box; color: #6f42c1;">DaoTienAo</span><span class="token punctuation" style="box-sizing: border-box;">(</span>DiaChiViNhanTienThuong<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Hàm dùng để đào (thêm mới) một Block vào Blockchain.</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lúc này ta sẽ tạo mới một Block, trong Block này sẽ chứa toàn bộ các giao dịch đã bị tạm hoãn trước đó, do nó chưa được đào và chưa có Hash.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">let</span> block <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Date</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaoDichTamHoan<span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">PhanTuCuoiCung</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
block<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">DaoBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DoKho<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Vẫn phải đào Hash bình thường cho lần này.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">push</span><span class="token punctuation" style="box-sizing: border-box;">(</span>block<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nối phần tử block vào làm phần tử cuối cùng của mảng Blockchain sau khi đã "đào" được.</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Sau khi đã bỏ công ra đào 1 Hash cho giao dịch hiện tại, ta sẽ có quyền được thưởng một phần tiền thưởng cố định sẵn. GiaoDichTamHoan đã được xử lý xong nên có thể xóa nó đi, sau đó ta gán một GiaoDichTamHoan mới, trong đó chuyển lượng tiền ta nhận được vào ví của chính mình.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaoDichTamHoan <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token punctuation" style="box-sizing: border-box;">[</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">GiaoDich</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">null</span><span class="token punctuation" style="box-sizing: border-box;">,</span> DiaChiViNhanTienThuong<span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>TienThuong<span class="token punctuation" style="box-sizing: border-box;">)</span>
<span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Chú ý là chỗ này ta không thể nhận được ngay lượng tiền này trong ví, vì giao dịch chưa được tạo và chưa có Hash. Nên trong Blockchain chưa có bản ghi mới ghi nhận số tiền đã chuyển vào ví nhận tiền thưởng.</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Để nhận được khoản tiền thưởng cho lần đào này. Thì ta phải đợi đến lần đào kế tiếp, giao dịch tạm hoãn này sẽ được khớp lệnh và lúc đó tiền thưởng mới có trong ví.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hàm đào tiền ảo như trong code đã comment, sẽ giúp cho người đào nhét được các giao dịch đang hoãn vào chuỗi. và để lần đào tiếp theo sẽ lấy được giá trị tiền thưởng.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Tại sao lại cần phải lần đào thứ 2 mới lấy được tiền thưởng của lần đào thứ 1. Như thế này thì ăn chặn của người ta à?</div>
</blockquote>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Mục đích của việc này có 2 tác dụng:</div>
<ol style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Để tránh việc người đào "ăn non". Lấy tiền thưởng của Block này đập vào Block kia.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Đó là để khuyến khích đào đào và đào. Người mới tham gia đào, sẽ luôn có một giao dịch chờ sẵn để người đó đào lấy tiền xài.</li>
</ol>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Trong thực tế thì đối với BitCoin, một giao dịch trong Blockchain chỉ được xác nhận khi mà phía sau Block chứa nó đã có thêm 6 Block khác được thêm vào. Lúc này thì ví tiền của bạn mới được cộng hoặc trừ tiền. Và chỉ khi đó thì bạn mới tiếp tục giao dịch tiếp được.</div>
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Khoảng thời gian bạn chờ cho có thêm 6 Block khác được insert vào thường được gọi là thời gian "tiền bị đóng băng" trong hệ thống. Tuy nhiên việc này thường chỉ diễn ra trong vài phút.</div>
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
6 Block giao dịch xong mới được chứng nhận trong ví có tiền thường được gọi là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">cơ chế đồng thuận</code> trong hệ thống. Khi mà có nhiều người cùng đào chuỗi Blockchain chứa Block có tiền của bạn. Họ sẽ gián tiếp xác nhận là giao dịch của bạn đáng tin</div>
</blockquote>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Việc ngăn không cho người đào lấy ngay được tiền thưởng đó là tùy vào từng đồng coin họ có áp dụng hay không. Đối với BitCoin thì là có. Ta hoàn toàn có thể tùy biến thuật toán chỗ này để áp dụng cho đồng MyCoin của ta.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Chú ý nữa đó là mảng <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDichTamHoan</code> sau mỗi một lần đào Hash xong, sẽ nhét toàn bộ mảng này vào chung một Block. Như vậy là có thể có Block chứa tới 100 giao dịch. Ở đây hoàn toàn là do code của ta cho phép việc này. Đối với BitCoin hoặc Etherium, họ chỉ cho phép 2 giao dịch trong cùng một Block thôi. Do đó đào xong Hash, ta phải nhặt 2 <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDich</code> trong mảng <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDichTamHoan</code>để đưa vào Block mới tạo. Máy đào tiền ảo thực tế thì nó sẽ chọn random 2 GiaoDich bất kỳ (tất nhiên là ưu tiên cái giao dịch nhét tiền vào ví của chính mình trong mảng <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDichTamHoan</code> rồi).</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Rồi sau khi có thuật toán <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DaoTienAo</code>. Ta cần một hàm nữa là hàm khởi tạo GiaoDich và nhét vào <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaoDichTamHoan</code></div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class="language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/* - Đây là hàm sẽ tạo ra một giao dịch mới. Đưa giao dịch vào mảng GiaoDichTamHoan. Như vậy là một Block sẽ có thể chứa nhiều giao dịch mà đang đợi Hash để được công nhận chính thức và thêm được vào Blockchain - */</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">TaoGiaoDich</span><span class="token punctuation" style="box-sizing: border-box;">(</span>GiaoDichMoi<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaoDichTamHoan<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">push</span><span class="token punctuation" style="box-sizing: border-box;">(</span>GiaoDichMoi<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Chúng ta cần một hàm nữa đó là hàm tính toán ra số tiền trong ví của mỗi người sau khi trao đổi mua bán bằng tiền ảo của chúng ta.</div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class="language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Chúng ta cũng cần một hàm để kiểm tra được lượng tiền đang có trong một địa chỉ ví nào đó. </span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Mỗi một ví tiền không hề có một con số tổng tiền được lưu trữ lại. Mà việc tính toán số tiền của một địa chỉ ví trong toàn bộ Blockchain ta phải lần tìm lần lượt toàn bộ các giao dịch bên trong Blockchain để kiểm đếm số tiền của một ví. Điều này sẽ rất an toàn và trung thực.</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Một điều quan trọng là tính minh bạch ở đây vì bất kỳ ai khi có địa chỉ ví của bạn cũng sẽ nhìn thấy hết toàn bộ giao dịch bạn đã từng thực hiện trong hệ thống.</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">KiemTraTienTrongVi</span><span class="token punctuation" style="box-sizing: border-box;">(</span>DiaChiVi<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">let</span> TienTrongVi <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">0</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">for</span> <span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">const</span> block <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">of</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Đi duyệt qua toàn bộ các Block trong Blockchain</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">for</span> <span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">const</span> gd <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">of</span> block<span class="token punctuation" style="box-sizing: border-box;">.</span>DanhSachGiaoDich<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Đi duyệt qua toàn bộ các giao dịch trong Block (Vì một Block là 1 mảng các giao dịch)</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">if</span> <span class="token punctuation" style="box-sizing: border-box;">(</span>gd<span class="token punctuation" style="box-sizing: border-box;">.</span>DiaChiGui <span class="token operator" style="box-sizing: border-box; color: #d73a49;">===</span> DiaChiVi<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu địa chỉ gửi là ví tiền này, thì tức là phải trừ ở Ví đi số tiền tương ứng trong giao dịch</span>
TienTrongVi <span class="token operator" style="box-sizing: border-box; color: #d73a49;">-=</span> gd<span class="token punctuation" style="box-sizing: border-box;">.</span>GiaTri<span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">if</span> <span class="token punctuation" style="box-sizing: border-box;">(</span>gd<span class="token punctuation" style="box-sizing: border-box;">.</span>DiaChiNhan <span class="token operator" style="box-sizing: border-box; color: #d73a49;">===</span> DiaChiVi<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu địa chỉ nhận là ví tiền này, thì tức là phải cộng vào Ví số tiền tương ứng trong giao dịch</span>
TienTrongVi <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+=</span> gd<span class="token punctuation" style="box-sizing: border-box;">.</span>GiaTri<span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> TienTrongVi<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Kiểm đếm xong ta sẽ được con số tổng.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Như trong code cũng đã nói, mỗi một ví tiền không hề có một con số tổng tiền được lưu trữ lại ở đâu cả. Mà việc tính toán số tiền của một địa chỉ ví trong toàn bộ Blockchain ta phải lần tìm lần lượt toàn bộ các giao dịch bên trong Blockchain để kiểm đếm số tiền của một ví. Điều này sẽ rất an toàn và trung thực.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Điều quan trọng là tính minh bạch ở đây vì bất kỳ ai khi có địa chỉ ví của bạn cũng sẽ nhìn thấy hết toàn bộ giao dịch bạn đã từng thực hiện trong hệ thống.</div>
<h3 id="_bay-gio-thi-da-co-the-chay-thu-roi-2" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.45rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
<span style="box-sizing: border-box; font-weight: bolder;">Bây giờ thì đã có thể chạy thử rồi.</span></h3>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Các bạn có thể xem DEMO tại link này: <a href="https://chungminhtu.github.io/Blockchain_HTML_Example/example3.html" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">LINK_DEMO</a></div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Download file example3.html tại đây: <a href="https://raw.githubusercontent.com/chungminhtu/Blockchain_HTML_Example/master/example3.html" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">LINK_DOWNLOAD</a></div>
<h1 id="_thuc-te-co-nhu-demo-3" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Thực tế có như DEMO?</h1>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Chúng ta hãy đi xem một trang web thực tế, nơi mà họ thực sự tạo ra một đồng tiền ảo có thể đào được ngay bằng trình duyệt. Bạn có thể đào tiền ảo, trao đổi và giao dịch với mọi người trong mạng trên toàn thế giới. Đồng tiền mà tôi đang nói tới, đó chính là NIMIQ.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hãy vào thử trang web này: <a href="https://nimiq.com/miner/" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">https://nimiq.com/miner/</a> Kích vào <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Connect to Network</code>. Ta sẽ thấy như sau: <img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/d25a9776-725c-4efb-b13d-f3f123fe97e3.gif" src="https://images.viblo.asia/d25a9776-725c-4efb-b13d-f3f123fe97e3.gif" style="border-style: none; box-sizing: border-box; cursor: pointer; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hãy xem kỹ hơn các thông tin mà máy bạn đang đào.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/d483e3cb-5559-4e36-acd0-4bf84cb70767.jpg" src="https://images.viblo.asia/d483e3cb-5559-4e36-acd0-4bf84cb70767.jpg" style="border-style: none; box-sizing: border-box; cursor: pointer; display: block; margin: 1.5em auto; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Chúng ta thấy là quá trình đào được diễn ra một cách tự động, vậy là bạn đã biến laptop của mình thành một con <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Trâu cày</code> tiền ảo rồi đấy.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; letter-spacing: -0.003em; word-break: break-word; word-wrap: break-word;">
Xem lại lịch sử một chút:</div>
<ul style="box-sizing: border-box; color: #686868; margin-bottom: 0px; margin-top: 0.5em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">Vào năm 2009 BitCoin ra đời. Số tiền thưởng của đồng tiền ảo BitCoin là 50 BTC nếu Hash được 1 Block. Đến năm 2012 thì bị giảm 1 nửa còn 25 BTC nếu Hash thành công 1 Block. Và đến năm 2017 thì bị giảm tiếp 1 nửa còn 12.5 BTC/Block. Người ta tính ra là cứ 210.000 Block đào được thì tiền thưởng bị giảm một nửa. Giảm 64 lần là số tiền thưởng sẽ về 0. Và với tốc độ tính toán hiện tại thì khoảng năm 2140 là không còn tiền thưởng để đào nữa. Và hiện nay cả thế giới đang hóng một sự kiện, đó là ngày 31 tháng 5 năm 2020 thì tiền thưởng sẽ bị giảm tiếp một nửa chỉ còn 6.5 BTC/Block mà thôi. Chúng ta có thể xem đếm ngược ở đây cho vui: <a href="http://www.bitcoinblockhalf.com/" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">http://www.bitcoinblockhalf.com</a>. Để xem tỉ giá đào BTC hãy vào đây: <a href="https://bitinfocharts.com/bitcoin/" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">https://bitinfocharts.com/bitcoin/</a></li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">Đồng tiền Ethereum thì không có cơ chế thưởng tiền sau khi đào xong 1 Block. Thay vào đó, mỗi một giao dịch sẽ có một khoản phí nhất định. Và tiền phí này được trả cho máy đào. Hiện tại tính ra thì 1 Block đào được sẽ có giá 3 ETH. Trung bình thì cứ 14.6 giây là đào được 1 Block ETH. Xem thêm tại <a href="https://bitinfocharts.com/ethereum/" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">https://bitinfocharts.com/ethereum/</a></li>
</ul>
</blockquote>
<h1 id="_ket-luan-4" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.95rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Kết luận</h1>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Như vậy là kết thúc bài 3 trong loạt bài về Blockchain và Tiền ảo. Tôi đã lấy ra các thuật toán "đơn giản" nhất để minh họa và giúp bạn dễ hiểu hơn về cách hoạt động của 2 công nghệ đang làm mưa làm gió.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Đây chỉ là những gì sơ khai nhất về ứng dụng Blockchain vào xây dựng thử một loại Tiền ảo. Để có thể hình thành nên cả một đế chế tiền ảo riêng, có những đặc tính riêng biệt nhau. Thì rõ ràng thuật toán của họ phải rất <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">kinh khủng</code></div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Vì Blockchain và Tiền ảo có rất nhiều cái hay, cách thực hiện nó cũng dễ. Nên hiện nay có thể thấy các đồng Coin mới ra đời cứ như mưa. Mỗi quốc gia hoàn toàn có thể tự tạo đồng Coin cho riêng mình.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hạn chế trước mắt của Blockchain và tiền ảo là gì? Đó là mỗi Blockchain và mỗi đồng tiền ảo với thuật toán khác nhau thì hoàn toàn khác nhau và không thể trao đổi tay đôi được. Bạn không thể nào đặt lệnh giao dịch địa chỉ ví chứa BitCoin và lấy đồng Etherium nhét vào được. Vì đơn giản là 2 Blockchain này là hai array chứa thông tin khác hẳn nhau. Bạn bỏ tiền ra mua BitCoin thì không thể trao đổi ngang hàng với Etherium được mà phải dùng trung gian là đổi BTC ra tiền USD chẳng hạn, rồi dùng tiền đó đi mua ETH.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Nhiều dân Trader (người mua đi bán lại) các đồng tiền ảo cũng thấy rằng, họ giao dịch giữa người A với người B nhưng vẫn bị mất một phần nhỏ chi phí giao dịch bị ghi là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">chi phí dành cho máy đào</code>. Giờ thì bạn đã biết là tại sao rồi chứ nhỉ?</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Blockchain và tiền ảo mới đang ở thủa sơ khai của nó. Còn rất nhiều thứ cần được nâng cấp và thuật toán cần được mở rộng. Do đó hãy cùng chờ đợi sự bùng nổ của 2 công nghệ này trong năm 2018 nhé bạn.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Bài này hi vọng nó đã giúp cho bạn có một cái nhìn tổng quan và hiểu biết nhất định để đọc các bài viết khác về Blockchain không bị bỡ ngỡ.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Các bạn hãy comment và share các link tham khảo mà bạn đang đọc về Blockchain và Bitcoin để cùng trao đổi nhé.</div>
Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com0Hà Nội, Hoàn Kiếm, Hà Nội, Việt Nam21.0277644 105.8341597999999520.968477399999998 105.75347879999995 21.0870514 105.91484079999995tag:blogger.com,1999:blog-3426893035641745805.post-38615559934348363202018-09-06T20:46:00.001-07:002018-09-06T20:47:04.595-07:00Phần 2: Tạo ra công nghệ đào tiền ảo<br />
<div class="md-contents article-content__body my-2 flex-fill" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #292b2c; flex: 1 1 auto !important; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.75; margin-bottom: 1.5rem !important; margin-top: 1.5rem !important; orphans: 2; overflow: hidden; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/0f467690-9893-4701-a4f0-2d0e465b1faa.gif" src="https://images.viblo.asia/0f467690-9893-4701-a4f0-2d0e465b1faa.gif" style="border-style: none; box-sizing: border-box; cursor: pointer; display: block; margin: 1.5em auto; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<h2 id="_so-ho-cua-blockchain-hien-tai-0" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.75rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Sơ hở của Blockchain hiện tại</h2>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Tiếp nối phần một, ta đã tìm hiểu về công nghệ Blockchain, về tính phân tán và minh bạch của nó.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Ta cũng thấy là công nghệ vừa tạo ra có nhược điểm là rất dễ bị hack. Bởi vì ta có được giao dịch cuối cùng trong chuỗi, thì lần theo dấu vết lần lượt các Hash của từng phần tử, ta sẽ xâu chuỗi lại được toàn bộ mảng Blockchain. Việc chỉnh sửa bằng cách tính toán lại toàn bộ Hash cho khớp và đồng bộ lúc này chỉ cần vài động tác và data sẽ nghiễm nhiên là toàn vẹn ở tất cả các máy trong mạng ngang hàng.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Có bạn sẽ bảo là có thuật toán để check MangBlock ở máy mình và MangBlock được tải về từ mạng P2P để xem sự khác nhau trước khi đồng bộ. Đúng là có thuật toán đó. Nhưng thuật toán này lại ưu tiên mảng nào "dài hơn" lại là mảng được cân nhắc là mảng đúng. Do đó nó không đủ mạnh để giải quyết vấn nạn hack.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Có một cách hay hơn để ngăn chặn việc này. Đó là giới hạn thời gian Hash được một Blockchain. Ta phải tăng độ khó của thuật toán Hash lên để Hash được một chuỗi sẽ cần rất nhiều thời gian. Điều này cũng có nghĩa là phải sau một thời gian nhất định mới có thể thêm mới 1 Block vào hệ thống. Ví dụ đồng tiền BitCoin thì quy định, sau mỗi 10 phút mới cho phép một bản ghi mới được thêm vào.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Làm điều này để làm gì?</div>
<ol style="box-sizing: border-box; color: #686868; margin-bottom: 0px; margin-top: 0.5em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">Ngăn chặn hệ thống Blockchain bị SPAM, liên tục có người thêm mới giao dịch giả vào hệ thống. Dẫn đến Blockchain bị DDOS, và có thể sập. Dữ liệu thật thì bị lấy đi.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">Ngăn chặn việc làm giả hệ thống, vì thời gian hacker Hash được 100 bản ghi sẽ bằng 100x10 phút = 1000 phút. Do đó hắn sẽ không dễ dàng clone toàn bộ hệ thống và Hash ra mã mới.</li>
</ol>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Khi một người dùng muốn thêm mới một bản ghi vào hệ thống, người đó phải chứng tỏ mình thực sự muốn giao dịch. Và để chứng minh điều đó anh ta cần bỏ tài nguyên của máy ra để tính toán Hash, và sau một thời gian nhất định, anh ta sẽ đăng ký thành công bản ghi nối tiếp (chain) vào Blockchain. Quá trình này người ta gọi là <em style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: bolder;">Proof-Of-Work</strong></em>.</div>
<h2 id="_proof-of-work-1" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.75rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Proof of Work</h2>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Ta hãy thử minh họa bằng cách sửa đổi file Class Block như sau:</div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; display: block; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin: 1em 0px; overflow: auto; padding: 1em; tab-size: 4; text-align: left; white-space: pre; word-break: normal; word-spacing: normal;"><code class="language-js" style="box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; tab-size: 4; text-align: left; white-space: pre; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/*=== ĐÂY LÀ CLASS MÔ TẢ CẤU TRÚC CỦA MỘT BLOCK. MỘT PHẦN TỬ CỦA BLOCKCHAIN ===*/</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">constructor</span><span class="token punctuation" style="box-sizing: border-box;">(</span>NgayGioTao<span class="token punctuation" style="box-sizing: border-box;">,</span> DuLieu<span class="token punctuation" style="box-sizing: border-box;">,</span> HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">''</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Để tạo ra một Block chúng ta cần truyền vào các tham số như sau:</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>NgayGioTao <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> NgayGioTao<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Ngày tháng hiện tại tạo ra Block này</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DuLieu <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> DuLieu<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Dữ liệu sẽ được lưu trong BLock này. Nó có thể là bất cứ cái gì, từ String, Array, Object JSON...</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> HashTruocDo<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lưu trữ Hash của Block ngay trước Block này. Vì các Block sẽ được nối lại thành chuỗi (mảng).</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Mã hóa toàn bộ nội dung của BLock này theo thuật toán SHA256 và lưu lại vào chính Block này.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaTriTuTang <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">0</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//THÊM VÀO Giá trị tự tăng của Block </span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Hàm mã hóa nội dung của toàn bộ Block. Do đó ta cần lấy toàn bộ các thuộc tính của Block đưa vào SHA256 để mã hóa ra một chuỗi.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> CryptoJS<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token constant" style="box-sizing: border-box; color: #24292e;">SHA256</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>NgayTao <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token constant" style="box-sizing: border-box; color: #24292e;">JSON</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">stringify</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DuLieu<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaTriTuTang<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Chỗ này có thêm GiaTriTuTang</span>
<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">toString</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//chú ý: JSON.stringify(this.DuLieu) sẽ convert biến Object this.DuLieu thành chuỗi</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//thay đổi hàm TaoBlock thành hàm DaoBlock</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">DaoBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span>DoKho<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">while</span> <span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">substring</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token number" style="box-sizing: border-box; color: #005cc5;">0</span><span class="token punctuation" style="box-sizing: border-box;">,</span> DoKho<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token operator" style="box-sizing: border-box; color: #d73a49;">!==</span> <span class="token function" style="box-sizing: border-box; color: #6f42c1;">Array</span><span class="token punctuation" style="box-sizing: border-box;">(</span>DoKho <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">join</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token string" style="box-sizing: border-box; color: #032f62;">"0"</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Kiểm tra xem giá trị Hash hiện tại đã đạt đủ số 0 ở đầu tiên như yêu vầu về độ khó đặt ra chưa. Lặp đi lặp lại hàm cho đến khi tìm được giá trị Hash đáp ứng yêu cầu.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>GiaTriTuTang<span class="token operator" style="box-sizing: border-box; color: #d73a49;">++</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tăng giá trị trong Block lên, để Hash mỗi lần sẽ nhận được một giá trị khác nhau. Nếu Hash không có 2 hoặc n số 0 ở đầu thì sẽ không đạt yêu cầu và phải tiếp tục Hash cái khác.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tính toán lại Hash của toàn bộ Block ứng với lần tăng này.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
console<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">log</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token string" style="box-sizing: border-box; color: #032f62;">"Đã đào xong Block: "</span> <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu đã tìm được Hash thì ta coi đấy là một lần "đào" (hashing) thành công.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Trong class Block ta thêm vào một <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">GiaTriTuTang</code>, đây là một biến được dùng khi tính toán Hash.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Vì ta cần phải tăng độ khó mỗi khi Hash, hàm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">TinhToanHash</code> vẫn không đổi, tuy nhiên ta tạo ra một thuật toán đơn giản, đó là không chấp nhận mã Hash tạo ra không đáp ứng yêu cầu. Yêu cầu của ta ở đây là mã Hash phải bắt đầu bằng 2 hoặc 3 hoặc 4 thậm chí 5 số "0" liên tiếp ở đầu.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Vì thuật toán SHA256 không bao giờ thay đổi đầu ra đối với 1 chuỗi đầu vào, bắt buộc phải thay đổi chuỗi đầu vào để tìm ra được Hash ở đầu ra thỏa mãn.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Do đó ta viết một hàm gọi là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DaoBlock</code> để lặp đi lặp lại quá trình "đào" cái mã Hash cho Block hiện tại. Và việc đào này có độ khó phụ thuộc vào biến <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">DoKho</code>. Nếu tăng số chữ số 0 liên tiếp lên thì cần lặp khá lâu mới tìm được chuỗi Hash ưng ý.</div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Ví dụ:</div>
<ul style="box-sizing: border-box; color: #686868; margin-bottom: 0px; margin-top: 0.5em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">DoKho = 2 thì cần 0.001s là tìm đc Hash có 2 chữ số 0 liền nhau ở đầu.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">DoKho = 3 thì cần 0.5s là tìm đc Hash có 3 chữ số 0 liền nhau ở đầu.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">DoKho = 4 thì cần 3s là tìm đc Hash có 4 chữ số 0 liền nhau ở đầu.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0px; word-break: break-word; word-wrap: break-word;">DoKho = 5 thì cần 20s là tìm đc Hash có 5 chữ số 0 liền nhau ở đầu.</li>
</ul>
</blockquote>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Đối với đồng 'tiền ảo BitCoin, thì độ khó của thuật toán được điều chỉnh để làm sao với mỗi 10 phút sẽ có một mã Hash ra đời. Dĩ nhiên là thuật toán của BitCoin không cùi bắp như thuật toán ở đây. Nhưng các bạn đã hình dung ra nó rồi đấy.</div>
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Proof-Of-Work còn được viết tắt là PoW. Khái niệm PoW được áp dụng vào Blockchain tương tự như cơ chế chống SPAM và DDOS 1 trang web bằng ReCapcha, người dùng muốn chứng thực mình không phải robot tự động thì phải bỏ công ra ngồi gõ một chuỗi ký tự lạ.</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Giờ ta sửa lại class Blockchain để đưa thuật toán "Đào' Block vào:</div>
<pre class="language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; display: block; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin: 1em 0px; overflow: auto; padding: 1em; tab-size: 4; text-align: left; white-space: pre; word-break: normal; word-spacing: normal;"><code class="language-js" style="box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; tab-size: 4; text-align: left; white-space: pre; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/*=== ĐÂY LÀ CLASS MÔ TẢ CẤU TRÚC CỦA MỘT BLOCKCHAIN. LÀ MỘT MẢNG CÁC BLOCK ===*/</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Blockchain</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">constructor</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Cấu trúc của Blockchain như sau</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token punctuation" style="box-sizing: border-box;">[</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tạo ra một mảng rỗng để chứa các Block. </span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">push</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token string" style="box-sizing: border-box; color: #032f62;">"01/01/2018"</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"Genesis Block"</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"0"</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tạo phần tử đầu tiên của Blockchain. Đây thường được gọi là Genesis Block, hay chính là "phần tử khởi tạo". Các phần tử tiếp theo sẽ nối tiếp vào phần tử này.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DoKho <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">5</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Thêm độ khó cho Blockchain;</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">PhanTuCuoiCung</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">[</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>length <span class="token operator" style="box-sizing: border-box; color: #d73a49;">-</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy ra phần tử cuối cùng của Blockchain</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">TaoMoiBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span>newBlock<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Hàm dùng để thêm mới một Block vào Blockchain.</span>
newBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">PhanTuCuoiCung</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy Hash của phần tử cuối cùng của mảng và lưu vào HasTruocDo của phần tử này</span>
<span class="token comment" style="box-sizing: border-box; color: #6a737d;">//newBlock.Hash = newBlock.TinhToanHash(); //Lúc này ta không tính toán Hash đơn thuần nữa mà phải "đào" thì mới có Hash cho Block mới.</span>
newBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">DaoBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DoKho<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">push</span><span class="token punctuation" style="box-sizing: border-box;">(</span>newBlock<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nối phần tử newBlock vào làm phần tử cuối cùng của mảng Blockchain sau khi đã "đào" được.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">KiemTraTinhToanVen</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">for</span> <span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">let</span> i <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">;</span> i <span class="token operator" style="box-sizing: border-box; color: #d73a49;"><</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>length<span class="token punctuation" style="box-sizing: border-box;">;</span> i<span class="token operator" style="box-sizing: border-box; color: #d73a49;">++</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">const</span> BlockHienTai <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">[</span>i<span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy ra phần tử ở vị trí hiện tại</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">const</span> BlockTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">[</span>i <span class="token operator" style="box-sizing: border-box; color: #d73a49;">-</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy ra phần tử ở ngay trước vị trí hiện tại</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">if</span> <span class="token punctuation" style="box-sizing: border-box;">(</span>BlockHienTai<span class="token punctuation" style="box-sizing: border-box;">.</span>Hash <span class="token operator" style="box-sizing: border-box; color: #d73a49;">!==</span> BlockHienTai<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Kiểm tra lại Hash của toàn bộ Block hiện tại và Hash đã lưu xem có trùng nhau không.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token boolean" style="box-sizing: border-box; color: #005cc5;">false</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu không trùng tức là Dữ liệu trong Block hiện tạiđã bị chỉnh sửa, hàm KiemTraToanVen sẽ trả về false luôn.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">if</span> <span class="token punctuation" style="box-sizing: border-box;">(</span>BlockHienTai<span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">!==</span> BlockTruocDo<span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy Hash hiện tại và Hash phần tử trước đó đã lưu xem có trùng nhau không.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token boolean" style="box-sizing: border-box; color: #005cc5;">false</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu không trùng tức là Hash của Block hiện tại đã bị chỉnh sửa, hàm KiemTraToanVen sẽ trả về false luôn.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token boolean" style="box-sizing: border-box; color: #005cc5;">true</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu kiểm tra hết toàn bộ trong vòng For mà không vấn đề gì thì tức là Blockchain vẫn toàn vẹn, chưa bị sửa đổi.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Ta cần tạo một biến <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">this.DoKho</code> trong Blockchain. Sau đó ta gọi <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">newBlock.DaoBlock(this.DoKho);</code>để mỗi khi tạo mới Block, ta phải đào mới có mã Hash cho nó.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Giờ thì test thôi nào. Hãy chạy thử LINK_DEMO dưới đây và nhớ bật F12 để xem console.log() ở tab Console ở màn hình Chrome Dev Tools.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/dfce3d3d-bf78-4df4-b647-4f414eb7efa5.gif" src="https://images.viblo.asia/dfce3d3d-bf78-4df4-b647-4f414eb7efa5.gif" style="border-style: none; box-sizing: border-box; cursor: pointer; display: block; margin: 1.5em auto; max-width: 100%; text-align: center; transition: transform 0.3s; vertical-align: baseline; z-index: 1050 !important;" /></div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Khi chạy ta thấy trình duyệt bị đơ một lúc, và một lúc sau mới hiện ra các giao dịch. Cửa sổ console sẽ cho thấy các mã Hash sau khi đào được.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Các bạn có thể xem DEMO tại link này: <a href="https://chungminhtu.github.io/Blockchain_HTML_Example/example2.html" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration: none;" target="_blank">LINK_DEMO</a></div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Download file example2.html tại đây: <a href="https://raw.githubusercontent.com/chungminhtu/Blockchain_HTML_Example/master/example2.html" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration: none;" target="_blank">LINK_DOWNLOAD</a></div>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Có bạn đã hỏi tôi chỗ này:</div>
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Có vẻ như Blockchain và Tiền ảo (Cryptocurrency) thường đi cùng với nhau?</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Đúng là như vậy. Blockchain không thể sống thiếu Cryptocurrency và Cryptocurrency cũng không thể sống mà không có Blockchain, như thể sinh ra đã là của nhau vậy. Blockchain nếu không có quá trình <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">đào tiền ảo</code> bằng các thuật toán Proof-Of-Work thì sẽ không có gì hấp dẫn người dùng, và quá ít người dùng trong hệ thống cùng đào sẽ dẫn đến 1 Hacker có máy tính đủ mạnh có thể đánh bại tất cả. Do đó một công ty nào đó muốn sử dụng Blockchain để làm cơ sở dữ liệu lưu trữ, họ phải liên tục tạo ra sự cạnh tranh và sự hấp dẫn cho những người tham gia xây dựng CSDL Blockchain đó.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Tuy nhiên nhiều đồng tiền ảo hiện nay đã có sự lo ngại là đến một giai đoạn thoái trào, phong trào đào tiền ảo sẽ nguội bớt, và người ta không cắm máy để đào nữa vì phần thưởng ít dần. Lúc này Proof-Of-Work không còn là liều thuốc mạnh. Người ta đang thay thế bằng một thứ mới, đó là Proof-Of-Stake.</div>
<h2 id="_proof-of-stake-2" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.75rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Proof-Of-Stake</h2>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Proof-Of-Stake (viết tắt là PoS) ra đời vì 2 lý do:</div>
<ol style="box-sizing: border-box; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Người dùng dần dần mất đi hứng thú đào tiền ảo vì nó dần đi đến giới hạn không thể đào tiếp nữa. Và vì quá nhiều Blockchain + Tiền ảo mới ra đời, người dùng sẽ đổ xô đi đào đồng tiền mới, bỏ rơi đồng tiền cũ.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Phong trào đào tiền ảo ngày nay đã dẫn đến sự lãng phí điện năng và công suất máy tính vào việc duy nhất là chạy SHA265 lấy mã Hash. Điều này là hệ lụy không đáng có chỉ vì mục đích bảo vệ một chuỗi BlockChain. Giả sử hàng nghìn loại Blockchain mới ra đời thì phải bao nhiêu phần cứng và điện năng mới đủ để bảo vệ và tấn công nó.</li>
</ol>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
PoW sinh ra là để ngăn chặn việc SPAM Blockchain, và cũng chống hack. Vậy thì cách làm của PoS như sau:</div>
<ol style="box-sizing: border-box; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Những người tham gia vào mạng lưới Blockchain không còn gọi là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">Thợ đào mỏ (Miners)</code>nữa mà được gọi là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">Nhân viên kiểm soát (Validators)</code></li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Nhân viên lúc này sẽ phải đánh cược, khi chèn một Block vào Blockchain, anh ta phải hi sinh một ít tiền ảo đang sở hữu ở ví của mình vào đó. Nhiều người cùng chèn Block. Và người nào chịu <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">hi sinh</code> nhiều nhất sẽ là người chiến thắng, chèn được Block thành công và nhận được <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">tiền phí</code> mà mỗi giao dịch đã bỏ ra trả cho người đào.</li>
</ol>
<blockquote style="background-color: #f8f8f8; border-left: 3px solid rgb(84, 136, 199); box-sizing: border-box; margin: 1em 0px 0px; overflow: auto; padding: 0.5rem 1rem;">
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0px; text-align: left; word-break: break-word; word-wrap: break-word;">
Điều này có nghĩa là người đào sẽ <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">đánh cược</code> để có được tiền ảo?</div>
<div style="box-sizing: border-box; color: #686868; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Như này thì người nào càng giàu thì sẽ càng giàu nhanh. Người mới tham gia hệ thống chưa có đồng nào trong tay thì làm sao mà kiếm được lợi nhuận đây?</div>
</blockquote>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Thực tế thì PoS đang trong quá trình <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">chế tạo</code> và còn nhiều ý tưởng của nó chưa được kiểm chứng hết. Đồng tiền Ethereum dự kiến đến 2019 sẽ đưa PoS vào hệ thống thay cho PoW. Chúng ta hãy chờ xem sao.</div>
<h2 id="_ket-luan-3" style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.75rem; font-weight: 700; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Kết luận</h2>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 0.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Như vậy ta đã áp dụng thành công thuật toán <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">ĐÀO</code> cho Blockchain của mình. Tuy nhiên đây không phải <em style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: bolder;">Đào BitCoin</strong></em> mà chỉ là Đào BLock.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Để có thể ứng dụng Blockchain này và tạo ra một đồng tiền ảo BitCoin của riêng mình, ta cần phải tạo ra được "Phần Thưởng" cho người đào Hash. Đó chính là tiền ảo, một dạng tiền thưởng cho người bỏ công ra đào Hash.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Vì mã Hash là giới hạn bởi độ dài số ký tự. Do đó đến một lúc nào đó sẽ không còn tìm được mã Hash nào thỏa mãn yêu cầu đặt ra nữa. Đó chính là điểm mấu chốt tạo ra giá trị của đồng tiền ảo. Người nào bỏ công ra tìm Hash, sẽ nhận được <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Tiền ảo</code>. Và chúng ta cũng sẽ tạo ra một <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">Ví tiền ảo</code> để có thể gửi và nhận BitCoin sau khi đã đào xong.</div>
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
Để tự mình tạo ra một đồng tiền ảo của riêng mình, sử dụng HTML và Javascript, mời các bạn đón đọc phần 3 của Series này nhé.</div>
<hr style="border-bottom-color: initial; border-bottom-style: initial; border-image: initial; border-left-color: initial; border-left-style: initial; border-right-color: initial; border-right-style: initial; border-top-color: rgba(27, 27, 27, 0.1); border-top-style: solid; border-width: 1px 0px 0px; box-sizing: content-box; height: 0px; margin-bottom: 1rem; margin-top: 1rem; overflow: visible;" />
<div style="box-sizing: border-box; color: #1b1b1b; font-size: 1em; font-weight: 400; letter-spacing: -0.003em; margin-bottom: 0px; margin-top: 1.5em; text-align: left; word-break: break-word; word-wrap: break-word;">
<strong style="box-sizing: border-box; font-weight: bolder;">Ghi chú:</strong> Các thuật ngữ chuyên ngành tiếng Anh các bạn có thể biết để tham khảo và đọc tài liệu cho rõ hơn như sau:</div>
<ul style="box-sizing: border-box; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;"><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">DaoBlock</code> trong blockchain thì hàm này thường có tên là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">Mining</code> hoặc <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">MineBlock</code></li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;"><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">GiaTriTuTang</code> trong blockchain thì biến này thường có tên là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">nonce</code></li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;"><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">NgayGioTao</code> trong blockchain thì biến này thường có tên là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">timestamp</code></li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;"><code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">KiemTraTinhToanVen</code> trong blockchain thì hàm này thường có tên là <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">Validate</code> hoặc <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">Accept</code></li>
</ul>
</div>
Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com2Hà Nội, Hoàn Kiếm, Hà Nội, Việt Nam21.0277644 105.8341597999999520.968477399999998 105.75347879999995 21.0870514 105.91484079999995tag:blogger.com,1999:blog-3426893035641745805.post-72230025085308593912018-09-06T20:43:00.000-07:002018-09-06T20:47:57.807-07:00Phần 1: Tự tạo ra một công nghệ Blockchain của riêng mình<h2 id="_cong-nghe-blockchain-0" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.75rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
<img alt="" class="medium-zoom-image" data-zoom-target="https://images.viblo.asia/1600/98337971-3d48-4c7e-ae62-52c42a205abf.gif" src="https://images.viblo.asia/98337971-3d48-4c7e-ae62-52c42a205abf.gif" style="border-style: none; box-sizing: border-box; color: #1b1b1b; display: block; font-size: 18px; font-weight: 400; letter-spacing: -0.054px; margin: 1.5em auto; max-width: 100%; text-align: center; transition: 0.3s; vertical-align: baseline; z-index: 1050;" /></h2>
<h2 id="_cong-nghe-blockchain-0" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.75rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Công nghệ Blockchain</h2>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Blockchain khá đơn giản và dễ hiểu. Đó là một dạng cơ sở dữ liệu nhưng lưu trữ phân tán. Rất khó để chỉnh sửa một bản ghi sau khi đã được thêm vào. Thường được sử dụng như là muốn cuốn sổ giao dịch.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Chúng ta thường nhầm lẫn là chỉ có ngân hàng mới cần chứng chỉ giao dịch không sửa được. Nhưng chúng ta hàng ngày gặp các trường hợp này rất nhiều. Chúng ta cần dữ liệu không thể sửa đổi để tránh bị lừa đảo khi giao dịch tay đôi. Ví dụ: bạn đi thuê nhà, hợp đồng thuê nhà ghi rõ giá tiền thuê hàng tháng, và ngày bắt đầu thuê. Sau đó hợp đồng được viết làm 2 bản và bạn giữ 1 bản, chủ nhà giữ một bản. Nếu một ngày đẹp trời, chủ nhà nói bạn chưa đóng tiền tháng này. Bạn cần có 2 tờ giấy hợp đồng để đối chiếu lại ngày bắt đầu đóng tiền. 2 tờ giấy hợp đồng mà tôi vừa nói là 1 dạng nguyên thủy của dữ liệu phân tán không thể sửa đổi. Và cũng có thể hiểu đó là 2 bản ghi của 1 blockchain. Nó là phân tán vì lưu ở 2 phía người dùng và không cần đến một nơi trung gian thứ 3 để lưu trữ.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Nếu có nhiều hơn 2 người dùng, thì công nghệ Blockchain cũng vẫn không thay đổi cách hoạt động, lúc này 2 hoặc nhiều người dùng trao đổi với nhau thì các giao dịch trong cùng một hệ thống (cùng một mạng) phải được liên kết (nối lại, xích lại, chaining) lại với nhau thành một mảng duy nhất. Và mỗi người trong mạng sẽ có được toàn bộ lịch sử giao dịch của những người khác trước đó. Như vậy nếu có 5 người chơi, thì sẽ có tới 5 bản ghi của lịch sử giao dịch được phân tán trong mạng. Nếu một trong 5 người tự ý sửa đổi bản ghi để đưa cho 4 người còn lại, 4 người kia sẽ biết ngay có gian dối và họ sẽ không chấp nhận.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Đó chính là <span style="box-sizing: border-box; font-weight: bolder;">SỰ PHÂN TÁN</span>, và <span style="box-sizing: border-box; font-weight: bolder;">SỰ MINH BẠCH</span> của công nghệ Blockchain.</div>
<h2 id="_thu-tu-xay-dung-mot-blockchain-hoan-chinh-1" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.75rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Thử tự xây dựng một Blockchain hoàn chỉnh</h2>
<h3 id="_ngay-bay-gio-ta-hay-thu-su-dung-javascript-thuan-va-html-de-tu-xay-dung-mot-cong-nghe-blockchain-xem-sao-2" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.45rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Ngay bây giờ, ta hãy thử sử dụng javascript thuần và HTML để tự xây dựng một công nghệ Blockchain xem sao.</h3>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Để mô tả một Block (phần tử giao dịch) trong Blockchain (mảng các giao dịch), ta cần một class như sau:</div>
<pre class=" language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class=" language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/*=== ĐÂY LÀ CLASS MÔ TẢ CẤU TRÚC CỦA MỘT BLOCK. MỘT PHẦN TỬ CỦA BLOCKCHAIN ===*/</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">constructor</span><span class="token punctuation" style="box-sizing: border-box;">(</span>NgayGioTao<span class="token punctuation" style="box-sizing: border-box;">,</span> DuLieu<span class="token punctuation" style="box-sizing: border-box;">,</span> HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">''</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Để tạo ra một Block chúng ta cần truyền vào các tham số như sau:</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>NgayGioTao <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> NgayGioTao<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Ngày tháng hiện tại tạo ra Block này</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DuLieu <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> DuLieu<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Dữ liệu sẽ được lưu trong BLock này. Nó có thể là bất cứ cái gì, từ String, Array, Object JSON...</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> HashTruocDo<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lưu trữ Hash của Block ngay trước Block này. Vì các Block sẽ được nối lại thành chuỗi (mảng).</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Mã hóa toàn bộ nội dung của BLock này theo thuật toán SHA256 và lưu lại vào chính Block này.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Hàm mã hóa nội dung của toàn bộ Block. Do đó ta cần lấy toàn bộ các thuộc tính của Block đưa vào SHA256 để mã hóa ra một chuỗi.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> CryptoJS<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token constant" style="box-sizing: border-box;">SHA256</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>NgayTao <span class="token operator" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="token constant" style="box-sizing: border-box;">JSON</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">stringify</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>DuLieu<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">toString</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//chú ý: JSON.stringify(this.DuLieu) sẽ convert biến Object this.DuLieu thành chuỗi.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Dữ liệu trong một Block rất đơn giản. Hàm quan trọng nhất trong mỗi Block chính là hàm tính toán giá trị Hash (mã hóa) của toàn bộ Block đó. Vì hàm tính toán Hash cần đến một thuật toán mã hóa mạnh là mã hóa SHA 256 bit, do đó ta cần một thư viện. Ở đây tôi dùng CryptoJS. Tôi sẽ load thư viện này từ cdn:</div>
<pre class=" language-html" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class=" language-html" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"> <span class="token tag" style="box-sizing: border-box; color: #22863a;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box;"><</span>script</span> <span class="token attr-name" style="box-sizing: border-box; color: #6f42c1;">src</span><span class="token attr-value" style="box-sizing: border-box; color: #032f62;"><span class="token punctuation" style="box-sizing: border-box;">=</span><span class="token punctuation" style="box-sizing: border-box;">"</span>https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/core.min.js<span class="token punctuation" style="box-sizing: border-box;">"</span></span><span class="token punctuation" style="box-sizing: border-box;">></span></span><span class="token script language-javascript" style="box-sizing: border-box;"></span><span class="token tag" style="box-sizing: border-box; color: #22863a;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box;"></</span>script</span><span class="token punctuation" style="box-sizing: border-box;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #22863a;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box;"><</span>script</span> <span class="token attr-name" style="box-sizing: border-box; color: #6f42c1;">src</span><span class="token attr-value" style="box-sizing: border-box; color: #032f62;"><span class="token punctuation" style="box-sizing: border-box;">=</span><span class="token punctuation" style="box-sizing: border-box;">"</span>https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/sha256.min.js<span class="token punctuation" style="box-sizing: border-box;">"</span></span><span class="token punctuation" style="box-sizing: border-box;">></span></span><span class="token script language-javascript" style="box-sizing: border-box;"></span><span class="token tag" style="box-sizing: border-box; color: #22863a;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box;"></</span>script</span><span class="token punctuation" style="box-sizing: border-box;">></span></span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Chúng ta cần tạo một class Blockchain để chứa mảng các phần tử Block. Hãy xem đoạn code sau và đọc comment:</div>
<pre class=" language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class=" language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"><span class="token comment" style="box-sizing: border-box; color: #6a737d;">/*=== ĐÂY LÀ CLASS MÔ TẢ CẤU TRÚC CỦA MỘT BLOCKCHAIN. LÀ MỘT MẢNG CÁC BLOCK ===*/</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Blockchain</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">constructor</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Cấu trúc của Blockchain như sau</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token punctuation" style="box-sizing: border-box;">[</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tạo ra một mảng rỗng để chứa các Block. </span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">push</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token string" style="box-sizing: border-box; color: #032f62;">"01/01/2018"</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"Genesis Block"</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"0"</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tạo phần tử đầu tiên của Blockchain. Đây thường được gọi là Genesis Block, hay chính là "phần tử khởi tạo". Các phần tử tiếp theo sẽ nối tiếp vào phần tử này.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">PhanTuCuoiCung</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">[</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>length <span class="token operator" style="box-sizing: border-box; color: #d73a49;">-</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy ra phần tử cuối cùng của Blockchain</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">TaoMoiBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span>newBlock<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Hàm dùng để thêm mới một Block vào Blockchain.</span>
newBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">PhanTuCuoiCung</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy Hash của phần tử cuối cùng của mảng và lưu vào HasTruocDo của phần tử này</span>
newBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>Hash <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> newBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tính toán Hash của chính phần tử này.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">push</span><span class="token punctuation" style="box-sizing: border-box;">(</span>newBlock<span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nối phần tử newBlock vào làm phần tử cuối cùng của mảng Blockchain.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token function" style="box-sizing: border-box; color: #6f42c1;">KiemTraTinhToanVen</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">for</span> <span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">let</span> i <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">;</span> i <span class="token operator" style="box-sizing: border-box; color: #d73a49;"><</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">.</span>length<span class="token punctuation" style="box-sizing: border-box;">;</span> i<span class="token operator" style="box-sizing: border-box; color: #d73a49;">++</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">const</span> BlockHienTai <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">[</span>i<span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy ra phần tử ở vị trí hiện tại</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">const</span> BlockTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">this</span><span class="token punctuation" style="box-sizing: border-box;">.</span>MangBlock<span class="token punctuation" style="box-sizing: border-box;">[</span>i <span class="token operator" style="box-sizing: border-box; color: #d73a49;">-</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">1</span><span class="token punctuation" style="box-sizing: border-box;">]</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy ra phần tử ở ngay trước vị trí hiện tại</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">if</span> <span class="token punctuation" style="box-sizing: border-box;">(</span>BlockHienTai<span class="token punctuation" style="box-sizing: border-box;">.</span>Hash <span class="token operator" style="box-sizing: border-box; color: #d73a49;">!==</span> BlockHienTai<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TinhToanHash</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Kiểm tra lại Hash của toàn bộ Block hiện tại và Hash đã lưu xem có trùng nhau không.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token boolean" style="box-sizing: border-box; color: #005cc5;">false</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu không trùng tức là Dữ liệu trong Block hiện tạiđã bị chỉnh sửa, hàm KiemTraToanVen sẽ trả về false luôn.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">if</span> <span class="token punctuation" style="box-sizing: border-box;">(</span>BlockHienTai<span class="token punctuation" style="box-sizing: border-box;">.</span>HashTruocDo <span class="token operator" style="box-sizing: border-box; color: #d73a49;">!==</span> BlockTruocDo<span class="token punctuation" style="box-sizing: border-box;">.</span>Hash<span class="token punctuation" style="box-sizing: border-box;">)</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Lấy Hash hiện tại và Hash phần tử trước đó đã lưu xem có trùng nhau không.</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token boolean" style="box-sizing: border-box; color: #005cc5;">false</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu không trùng tức là Hash của Block hiện tại đã bị chỉnh sửa, hàm KiemTraToanVen sẽ trả về false luôn.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token keyword" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="token boolean" style="box-sizing: border-box; color: #005cc5;">true</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Nếu kiểm tra hết toàn bộ trong vòng For mà không vấn đề gì thì tức là Blockchain vẫn toàn vẹn, chưa bị sửa đổi.</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hàm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">TaoMoiBlock()</code> là hàm sẽ đảm bảo 2 việc:</div>
<ol style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Tính toán toàn bộ Hash của Block hiện tại và lưu lại.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Lấy Hash của Block cuối cùng để lưu vào biến HashTruocDo của Block hiện tại. Như vậy thì các Block sẽ được nối (chain-xích) lại với nhau thông qua ràng buộc là mã Hash.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Hàm <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">KiemTraTinhToanVen()</code> là một hàm rất quan trọng, đóng vai trò trái tim trong Blockchain. Mỗi khi một người dùng bất kỳ muốn kiểm tra toàn bộ Blockchain hiện tại xem có an toàn hay không để tiếp tục giao dịch. Toàn bộ các Hash trong chuỗi được lấy ra so sánh. Tính toàn vẹn của từng Block và tính liên kết của toàn bộ Block phải được đảm bảo không bị sai mã Hash. Nếu có một trường hợp sai, tức là data đã bị ai đó trong mạng chỉnh sửa.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Như vậy là công nghệ Blockchain tự tạo đã xong, giờ ta có thể bắt đầu test nó:</div>
<pre class=" language-js" style="background-color: #f1f2f3; border-radius: 3px; border: 1px solid rgb(229, 229, 229); box-sizing: border-box; color: #24292e; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 15px; hyphens: none; line-height: 1.45; margin-bottom: 1em; margin-top: 1em; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class=" language-js" style="box-sizing: border-box; direction: ltr; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; hyphens: none; line-height: 1.45; tab-size: 4; word-break: normal; word-spacing: normal;"> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">var</span> MyCoin <span class="token operator" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Blockchain</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Tạo mới một Blockchain để chứa các giao dịch. Ở đây ta có thể coi là lịch sử giao dịch của đồng tiền MyCoin.</span>
MyCoin<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TaoMoiBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token string" style="box-sizing: border-box; color: #032f62;">"02/01/2018"</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token punctuation" style="box-sizing: border-box;">{</span> <span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Thêm một giao dịch mới, Data là một Object chứa nội dung chuyển tiền</span>
NguoiGui<span class="token punctuation" style="box-sizing: border-box;">:</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"Ong A"</span><span class="token punctuation" style="box-sizing: border-box;">,</span>
NguoiNhan<span class="token punctuation" style="box-sizing: border-box;">:</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"Ba B"</span><span class="token punctuation" style="box-sizing: border-box;">,</span>
Tien<span class="token punctuation" style="box-sizing: border-box;">:</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">350</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
MyCoin<span class="token punctuation" style="box-sizing: border-box;">.</span><span class="token function" style="box-sizing: border-box; color: #6f42c1;">TaoMoiBlock</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token keyword" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="token class-name" style="box-sizing: border-box; color: #6f42c1;">Block</span><span class="token punctuation" style="box-sizing: border-box;">(</span><span class="token string" style="box-sizing: border-box; color: #032f62;">"03/01/2018"</span><span class="token punctuation" style="box-sizing: border-box;">,</span> <span class="token punctuation" style="box-sizing: border-box;">{</span><span class="token comment" style="box-sizing: border-box; color: #6a737d;">//Thêm một giao dịch mới, Data là một Object chứa nội dung chuyển tiền</span>
NguoiGui<span class="token punctuation" style="box-sizing: border-box;">:</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"Ba B"</span><span class="token punctuation" style="box-sizing: border-box;">,</span>
NguoiNhan<span class="token punctuation" style="box-sizing: border-box;">:</span> <span class="token string" style="box-sizing: border-box; color: #032f62;">"Ong C"</span><span class="token punctuation" style="box-sizing: border-box;">,</span>
Tien<span class="token punctuation" style="box-sizing: border-box;">:</span> <span class="token number" style="box-sizing: border-box; color: #005cc5;">200</span>
<span class="token punctuation" style="box-sizing: border-box;">}</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">)</span><span class="token punctuation" style="box-sizing: border-box;">;</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Thêm chút mắm muối, ta sẽ được một file html hoàn chỉnh. Bạn có thể lưu lại và bật trực tiếp bằng trình duyệt chrome hoặc firefox.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Các bạn có thể xem DEMO tại link này: <a href="https://chungminhtu.github.io/Blockchain_HTML_Example/example1.html" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">LINK_DEMO</a></div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Sau khi chạy kết quả ta sẽ thấy sự toàn vẹn của dữ liệu đã được kiểm tra đúng. Và dữ liệu MangBlock cũng đã liên kết đúng giữa HashTruocDo và Hash của 2 phần tử liền kề nhau.</div>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Download file <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">example1.html</code> tại đây: <a href="https://raw.githubusercontent.com/chungminhtu/Blockchain_HTML_Example/master/example1.html" style="background-color: transparent; box-sizing: border-box; color: #2b6dad; text-decoration-line: none;" target="_blank">LINK_DOWNLOAD</a></div>
<h2 id="_ket-luan-3" style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 1.75rem; line-height: 1.2; margin: 1.5em 0px 0.6em;">
Kết luận</h2>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 0.5em; word-break: break-word; word-wrap: break-word;">
Chỉ đơn giản như vậy là ta đã tạo thành công một công nghệ Blockchain. Để có thể giao dịch được thì file <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 16px; padding: 3px 5px; word-break: break-word;">example1.html</code> này của chúng ta phải được copy đến các máy cá nhân của người dùng trong mạng. Và giá trị MangBlock phải là cố định và được sync (đồng bộ) bằng mạng ngang hàng P2P để mỗi khi có giao dịch mới ở máy một người, toàn bộ những người khác sẽ Validate (kiểm tra lại tính toàn vẹn những gì được thêm vào) và tiếp tục giao dịch tiếp. Công nghệ Blockchain sơ khai này mới đáp ứng được một số yêu cầu đơn giản của Blockchain:</div>
<ol style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Dữ liệu nằm phân tán.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Dữ liệu và mã nguồn được minh bạch. Người dùng sẽ dễ dàng kiểm tra tính toàn vẹn.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Một khi dữ liệu đã <em style="box-sizing: border-box;">thêm</em> vào hệ thống thì không thể <em style="box-sizing: border-box;">xóa</em> hoặc <em style="box-sizing: border-box;">sửa</em> được nữa. Nếu mất đi một phần tử thì chuỗi của chúng ta không toàn vẹn. Điều này đảm bảo tính minh bạch của toàn hệ thống, không ai có thể tác động vào hệ thống và mọi người đều nhìn thấy rõ dữ liệu một khi đã thêm vào thành công.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Tuy nhiên công nghệ Blockchain vừa tạo vẫn còn một số sơ hở:</div>
<ol style="background-color: white; box-sizing: border-box; color: #292b2c; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; margin-bottom: 0px; margin-top: 1em;">
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Người dùng dễ dàng thêm hàng trăm ngàn giao dịch vào hệ thống mỗi giây. Dẫn đến hệ thống bị <span style="box-sizing: border-box; font-weight: bolder;">SPAM</span> và toàn giao dịch rác. Blockchain dài nhất cũng sẽ được coi là đúng nhất do đó khả năng sai lệch hệ thống.</li>
<li style="box-sizing: border-box; font-size: 1em; margin-bottom: 0.5em; word-break: break-word; word-wrap: break-word;">Hacker dễ dàng clone (copy) toàn bộ biến <code style="background-color: #eeeeee; border-radius: 2px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; padding: 3px 5px; word-break: break-word;">MangBlock</code> rồi sửa một bản ghi, rồi ghi đè toàn bộ MangBlock fake đó vào hệ thống của hắn. Tiếp theo MangBlock đó sẽ được tính toán lại Hash cho thành chuỗi mới và đồng bộ tự động, và ghi đè vào máy của những người còn lại. Dẫn đến sự "tèo" của hệ thống chúng ta khổ công gây dựng.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #1b1b1b; font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; font-size: 18px; letter-spacing: -0.003em; margin-top: 1.5em; word-break: break-word; word-wrap: break-word;">
Để nâng cấp hệ thống vừa xây dựng, chúng ta cần tìm hiểu đến một công nghệ cao siêu hơn. Đó chính là các công nghệ "đào" tiền ảo, hay còn gọi là Mining. Mời các bạn theo dõi tiếp ở phần 2.</div>
Trung Vũhttp://www.blogger.com/profile/17156446365525173708noreply@blogger.com0Hà Nội, Hoàn Kiếm, Hà Nội, Việt Nam21.0277644 105.8341597999999520.968477399999998 105.75347879999995 21.0870514 105.91484079999995