Coder Social home page Coder Social logo

bitcoiner-intro-to-rust's Introduction

The Bitcoiner's Introduction to Rust

Learn Rust the Hard Bitcoin Way

Goal

Learn Rust and review Bitcoin fundamentals by building a command-line program.

Project

We are going to build a relatively small command line program that decodes a raw transaction. Don't worry if you don't exactly know what that means, we'll review all the basic Bitcoin concepts and walk you through it step by step. If you are familiar with bitcoin-cli, this is the equivalent of bitcoin-cli decoderawtransaction [raw transaction hex].

Prerequisites

Ideally, you have some experience with programming and have read Mastering Bitcoin. This is not required, but it will make the learning process a lot smoother.

Why Rust

Rust is becoming very popular for new Bitcoin projects. Some examples of popular open source Bitcoin projects being built in Rust include Rust-Bitcoin, BDK, LDK, and Fedimint. But it's not just Bitcoin. For 8 years in a row, Rust topped the charts as the most desired programming language in Stack Overflow's annual developer survey (https://github.blog/2023-08-30-why-rust-is-the-most-admired-language-among-developers/). Why is that? Well, Rust allows developers to write systems-level programs in a way that is extremely performant, memory-safe, reliable and readable. It has great documentation, tooling, and a large and growing ecosystem. These are all very important features, especially for an important protocol like Bitcoin which will ultimately run the entire world's money system!

But Rust is Hard

It's not as difficult as you might think. It just takes some time to get familiar with a new way of doing things. At first, it might feel more restrictive and tedious to write Rust code, but this is because the language makes certain tradeoffs that yield many long-term benefits in terms of performance, safety, effectiveness and readability. The compiler forces you to reason more carefully about your program and what it's actually doing under the hood. My hope is that by the end of this project you come to appreciate Rust and feel more confident writing Rust programs. I transitioned from Ruby and Javascript to Rust and I can't see myself ever going back!

What You Will Learn

By the end of this project, you will have understood the fundamental concepts of Rust such as types, data structures, references, stack and the heap, traits, error handling and more. We will also review some basic aspects of the Bitcoin protocol, specifically the components of a transaction. In future courses, we will dive deeper into the Bitcoin protocol and write more complex programs, exploring the full extent of Bitcoin's capabilities.

Table of Contents

bitcoiner-intro-to-rust's People

Contributors

sb1752 avatar edilmedeiros avatar bblackwo avatar gimballock avatar green-john avatar jsahagun91 avatar vnprc avatar

Stargazers

 avatar Matuu avatar Vaz avatar abhishake bansal avatar  avatar ifuensan avatar Jonas de Souza avatar 0tuedon avatar 我心里危险的东西 avatar Novaly avatar Otávio avatar Armando CD avatar Alex Lewin avatar benny b avatar dunxen avatar Shishir Kushwaha avatar Piit avatar William Mbotta avatar Aleksandr Volkov avatar Matthew Ramsden avatar Fernando Parisotto avatar  avatar Vasu Khanna avatar  avatar Somil avatar Joao Leal avatar Antonio Quental avatar Matt McGivney avatar Anderson Juhasc avatar Luis Schwab avatar Qurum avatar Nobu avatar Ankur Ambar Mishra avatar  avatar  avatar  avatar  avatar Darrell avatar Marcos Alejandro avatar Jeff Walker avatar Hunter Beast avatar Will Clark avatar 22388o⚡️  avatar  avatar nully0x avatar Masud avatar Darius Parvin avatar Corey avatar Jonas avatar Kumulynja avatar ion- avatar mplsgrant avatar

Watchers

 avatar Bala avatar  avatar

bitcoiner-intro-to-rust's Issues

Wrong output in Chapter 14

This is trying to decode the string in https://github.com/sb1752/bitcoiner-intro-to-rust/blob/8f0c2c87ac2ea8aa51d5fdf989dbfe8444a089af/14_json_serialization.md?plain=1#L105C28-L105C770.

    let transaction_hex = "010000000242d5c1d6f7308bbe95c0f6e1301dd73a8da77d2155b0773bc297ac47f9cd7380010000006a4730440220771361aae55e84496b9e7b06e0a53dd122a1425f85840af7a52b20fa329816070220221dd92132e82ef9c133cb1a106b64893892a11acf2cfa1adb7698dcdc02f01b0121030077be25dc482e7f4abad60115416881fe4ef98af33c924cd8b20ca4e57e8bd5feffffff75c87cc5f3150eefc1c04c0246e7e0b370e64b17d6226c44b333a6f4ca14b49c000000006b483045022100e0d85fece671d367c8d442a96230954cdda4b9cf95e9edc763616d05d93e944302202330d520408d909575c5f6976cc405b3042673b601f4f2140b2e4d447e671c47012103c43afccd37aae7107f5a43f5b7b223d034e7583b77c8cd1084d86895a7341abffeffffff02ebb10f00000000001976a9144ef88a0b04e3ad6d1888da4be260d6735e0d308488ac508c1e000000000017a91476c0c8f2fc403c5edaea365f6a284317b9cdf7258700000000";

Manual inspection say the first input should have txid = 42d5c1d6f7308bbe95c0f6e1301dd73a8da77d2155b0773bc297ac47f9cd7380 (in reverse...)

Original text: https://github.com/sb1752/bitcoiner-intro-to-rust/blob/8f0c2c87ac2ea8aa51d5fdf989dbfe8444a089af/14_json_serialization.md?plain=1#L185C1-L198C2

Inputs: [
  {
    "txid": "f8c693771b2992a11b53c045369ab31a920de1d921ff3c148a9d0487c8f90baf",
    "output_index": 16,
    "script": "483045022100904a2e0e8f597fc1cc271b6294b097a6edc952e30c453e3530f92492749769a8022018464c225b03c28791af06bc7fed129dcaaeff9ec8135ada1fb11762ce081ea9014104da289192b0845d5b89ce82665d88ac89d757cfc5fd997b1de8ae47f7780ce6a32207583b7458d1d2f3fd6b3a3b842aea9eb789e2bea57b03d40e684d8e1e0569",
    "sequence": 4294967295
  },
  {
    "txid": "e51d2177332baff9cfbbc08427cf0d85d28afdc81411cdbb84f40c95858b080d",
    "output_index": 1,
    "script": "4830450220369df7d42795239eabf9d41aee75e3ff20521754522bd067890f8eedf6044c6d0221009acfbd88d51d842db87ab990a48bed12b1f816e95502d0198ed080de456a988d014104e0ec988a679936cea80a88e6063d62dc85182e548a535faecd6e569fb565633de5b4e83d5a11fbad8b01908ce71e0374b006d84694b06f10bdc153ca58a53f87",
    "sequence": 4294967295
  }
]

My output:

Version: 1
Inputs [
  {
    "txid": "8073cdf947ac97c23b77b055217da78d3ad71d30e1f6c095be8b30f7d6c1d542",
    "output_index": 1,
    "script_sig": "4730440220771361aae55e84496b9e7b06e0a53dd122a1425f85840af7a52b20fa329816070220221dd92132e82ef9c133cb1a106b64893892a11acf2cfa1adb7698dcdc02f01b0121030077be25dc482e7f4abad60115416881fe4ef98af33c924cd8b20ca4e57e8bd5",
    "sequence": 4294967294
  },
  {
    "txid": "9cb414caf4a633b3446c22d6174be670b3e0e746024cc0c1ef0e15f3c57cc875",
    "output_index": 0,
    "script_sig": "483045022100e0d85fece671d367c8d442a96230954cdda4b9cf95e9edc763616d05d93e944302202330d520408d909575c5f6976cc405b3042673b601f4f2140b2e4d447e671c47012103c43afccd37aae7107f5a43f5b7b223d034e7583b77c8cd1084d86895a7341abf",
    "sequence": 4294967294
  }
]

I'm fixing this in a PR, but wanted to check first.

CompactSize includes 0

Technically, the compactSize includes 0, but we start at 1 in lesson 10 to avoid 0 inputs.

Should make this more clear as it will create confusion later on in chapter 22. Perhaps can simply check if there are 0 inputs and then panic! early on and fix the compactSize function implementation.

Rename script_sig variables

Inputs have a scriptSig field which was named script in the code examples.
Probably better to rename them all to script_sig.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.