Note: Part 2 of this piece can be foundhere.

Although some think blockchain is a solution waiting for problems, there’s no doubt that this novel technology is a marvel of computing. But, what exactly is a blockchain?

Blockchain

a digital ledger in which transactions made in bitcoin or another cryptocurrency are recorded chronologically and publicly.

In more general terms, it’s a public database where new data are stored in a container called a block and are added to an immutable chain (hence blockchain) with data added in the past. In the case of Bitcoin and other cryptocurrencies, these data are groups of transactions. But, the data can be of any type, of course.

Blockchain technology has given rise to new, fully digital currencies like Bitcoin and Litecoin that aren’t issued or managed by a central authority. This brings new freedom to individuals who believe that today’s banking systems are a scam or subject to failure. Blockchain has also revolutionized distributed computing in the form of technologies like Ethereum, which has introduced interesting concepts like smart contracts.

In this article, I’ll make a simple blockchain in less than 50 lines of Python 2 code. It’ll be called SnakeCoin.

We’ll start by first defining what our blocks will look like. In blockchain, each block is stored with a timestamp and, optionally, an index. In SnakeCoin, we’re going to store both. And to help ensure integrity throughout the blockchain, each block will have a self-identifying hash. Like Bitcoin, each block’s hash will be a cryptographic hash of the block’s index, timestamp, data, and the hash of the previous block’s hash. Oh, and the data can be anything you want.

Awesome! We have our block structure, but we’re creating a blockchain. We need to start adding blocks to the actual chain. As I mentioned earlier, each block requires information from the previous block. But with that being said, a question arises: how does the first block in the blockchain get there? Well, the first block, or genesis block, is a special block. In many cases, it’s added manually or has unique logic allowing it to be added.

We’ll create a function that simply returns a genesis block to make things easy. This block is of index 0, and it has an arbitrary data value and an arbitrary value in the “previous hash” parameter.

Now that we’re able to create a genesis block, we need a function that will generate succeeding blocks in the blockchain. This function will take the previous block in the chain as a parameter, create the data for the block to be generated, and return the new block with its appropriate data. When new blocks hash information from previous blocks, the integrity of the blockchain increases with each new block. If we didn’t do this, it would be easier for an outside party to “change the past” and replace our chain with an entirely new one of their own. This chain of hashes acts as cryptographic proof and helps ensure that once a block is added to the blockchain it cannot be replaced or removed.

That’s the majority of the hard work. Now, we can create our blockchain! In our case, the blockchain itself is a simple Python list. The first element of the list is the genesis block. And of course, we need to add the succeeding blocks. Because SnakeCoin is the tiniest blockchain, we’ll only add 20 new blocks. We can do this with a for loop.

Let’s test what we’ve made so far.

Don’t worry. It goes up to 20.

There we go! Our blockchain works. If you want to see more information in the console, you could edit the complete source file and print each block’s timestamp or data.

That’s about all that SnakeCoin has to offer. To make SnakeCoin scale to the size of today’s production blockchains, we’d have to add more features like a server layer to track changes to the chain on multiple machines and a proof-of-work algorithm to limit the amount of blocks added in a given time period.

If you’d like to get more technical, you can view the original Bitcoin whitepaper here. Best of luck and happy hacking!

https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b

Let’s Build the Tiniest Blockchain In Less Than 50 Lines of Python (Part I)相关推荐

  1. How to Build Your Own Blockchain Part 4.1 — Bitcoin Proof of Work Difficulty Explained

    If you're wondering why this is part 4.1 instead of part 4, and why I'm not talking about continuing ...

  2. How to Build Your Own Blockchain Part 4.2 — Ethereum Proof of Work Difficulty Explained

    We're back at it in the Proof of Work difficulty spectrum, this time going through how Ethereum's di ...

  3. python开发一个区块链只需40多行

    40多行python代码开发一个区块链?可信吗?我们将通过Python 2动手开发实现一个迷你区块链来帮你真正理解区块链技术的核心原理.python开发区块链的源代码保存在Github. 尽管有人认为 ...

  4. 2018年python薪资_最好的Python:2017和2018年至今我最喜欢的文章集

    2018年python薪资 我打算发布此收藏集 (My intention with publishing this collection) Last year I only used Medium ...

  5. 从入门到进阶,史上最全Python精华文章合集

    我将这些文章分为 10 类--顺便一提,这也恰好反映出 Python 的多用途.多目标的性质. 这些类别如下: Python 常规编程 Python 性能优化 Python 开发环境及 DevOps ...

  6. 3DSlicer22:Module-ExtensionWizard Build Install

    1.前言 Slicer模块典型包含几种不同类型的模块,如CMake文件.Source文件.资源文件. 在大多数情况下,为了方便编译,文件名称和文件中文本字符串的名称需要保持一致. ExtensionW ...

  7. Go学习之编码实现区块链 - 【blockchain】

    首先,感谢Coral Health的<Code your own blockchain in less than 200 lines of Go!>所提供的Go实现简单区块链一文,鉴于本土 ...

  8. Android7.1.2源码解析系列】Android编译系统翻译------Android_Build_System(/build/core/build-system.html)

    Android编译系统 状态:草稿(2006年5月18日) 正文: 目的: 编译android系统工作的主要目的在于: (1)使得依赖关系工作时更加的值得信赖,即当文件需要重新编译时可以被正确的执行: ...

  9. 解决Unity3D 创建UWP应用出现Exception: Failed to build Visual Studio project using arguments

    解决Unity3D 创建UWP应用出现Exception: Failed to build Visual Studio project using arguments 解决方案在下方 本人在使用Uni ...

最新文章

  1. LIVE 预告 | 牛津胡庆拥:学习理解大规模点云
  2. Homebrew 1.9发布,将支持Linux与Windows 10
  3. Class,表名,表单参数名关系
  4. 破解时的数据约束性和winhex操作失败
  5. [物理学与PDEs]第1章习题14 求解 rot 方程
  6. centos7配置时间同步服务器
  7. 最通俗PLC教程—源自Koyo光洋PLC自学总结(2)
  8. unity 彩带粒子_unity游戏缤纷五彩纸屑粒子特效Confetti FX 1.0
  9. 服务器2003如何修复0,Windows Server 2003 SP2补丁无法安装解决方法(密钥无效)
  10. uniapp延迟执行_【 vue / uniapp 】延迟加载数据的特殊处理
  11. 即食花胶的功效与作用有哪些?
  12. 谁该承担中国SaaS贫瘠生态的一点责任
  13. HTML textarea 标签的 wrap 属性
  14. LM358与TL431验证
  15. 狄利克雷卷积_莫比乌斯反演及狄利克雷卷积
  16. 04_LATTICE入门篇之仿真
  17. 【Bootstrap】(二)Bootstrap 网格系统
  18. 课题五 数控镗铣削加工工艺分析
  19. 三种方法部署YUM软件仓库
  20. 【22/03】为什么要学git分布式版本控制软件及基本操作

热门文章

  1. php pdo操作数据库
  2. UVA 10564 计数DP
  3. JAVA实现拼图游戏
  4. 操作系统安装与优化:chapter8 虚拟机
  5. ubuntu 搜索文件方法(find命令)
  6. mySQL(关系型数据库管理系统)编辑
  7. PSO-LSSVM算法及其MATLAB代码
  8. 学习笔记(一)——图像的灰度级和动态范围
  9. 【14】全歌王歌后合集
  10. [云炬创业基础笔记]第十章企业的利润计划测试2