Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Developer Primer on Blockchain

A Developer Primer on Blockchain

Come find out how you can build a peer-to-peer distributed ledger forged by consensus. Understand through code how to create “smart contracts” and develop a different type of transactional application that establishes trust, accountability and transparency, while streamlining business processes and legal constraints.

Martin Beeby

April 09, 2018
Tweet

More Decks by Martin Beeby

Other Decks in Technology

Transcript

  1. Martin Beeby @thebeebs Transferring Money between them is now harder

    Easy to lose track of who owes what Lets build a database MONEY IS A PAIN
  2. Martin Beeby @thebeebs Martin Controls it Each cake sale is

    a transaction The seller tells martin about the transaction. He records it CREATE A DATABASE
  3. Martin Beeby @thebeebs Hard to keep in sync Hard to

    agree Hard to reach consensus PEOPLE COULD STILL DEFRAUD
  4. Martin Beeby @thebeebs PUBLIC AND PRIVATES DONALD MARTIN Address 1

    Address 2 Address 3 Address 6 Address 5 Private Key Private Key Private Key Private Key CHRISTER EDWARD
  5. Martin Beeby @thebeebs Sender Amount Receiver Signed Address 1 5

    Address 2 110101010 Address 2 4 Address 3 010101010 Address 3 3 Address 1 010011010 TRANSACTIONS
  6. Martin Beeby @thebeebs Listen for transactions and validates them Order

    transactions into a block Creates and hashes blocks solving a maths problem related MINERS LISTEN FOR TRANSACTIONS Sender Receiver Signed Address 1 5 Address 2 110101010 Address 2 4 Address 3 010101010 Address 3 3 Address 1 010011010
  7. Martin Beeby @thebeebs Solve a complex problem involving the hashing

    of the block Would be very expensive to try and present incorrect blocks Miners on bitcoin spend huge sums of money on electric PROOF OF WORK Jargon: A Consensus Algorithm
  8. Martin Beeby @thebeebs Proof of Stake Proof of Elapsed Time

    Byzantine fault tolerant OTHER CONSENSUS ALGORITHM
  9. Martin Beeby @thebeebs HASHING In west Philadelphia born and raised

    on the play ground was where I spent most of my days. Chilling 446F09C5D7DB665A 851077054EBC7067 56E372A32C8753F0 428B6671CA6CE449 In west Trondheim born and raised on the play ground was where I spent most of my days. Chilling 223E8E360399E82A C97B37A73FA1F532 071F03D8A174FA56 6D44F1770CC37D9A SHA256
  10. Martin Beeby @thebeebs Previous Hash of the block All the

    transactions to create a block Hash the block to create a unique Hash that represents everything YOU CANT CHANGE A BLOCK
  11. Martin Beeby @thebeebs A list of transactions grouped into blocks

    Blocks chained together by knowing the previous hash Nothing can be altered, the slightest modification would be obvious A BLOCK CHAIN IS Jargon: Merkle Tree
  12. Martin Beeby @thebeebs Christer decides to run an incentive. Every

    100th cake customer get a reward. Contract Int x AddAddress(address) x = x + 1; If (x =100){ address.transfer(reward) } A PSUEDO CONTRACT
  13. Martin Beeby @thebeebs The contract code is visible on the

    blockchain It can’t be changed by anyone All the calls are public and everyone can see the cake competition is fair ITS TRANSPARENT
  14. Martin Beeby @thebeebs Bitcoin is great when you have zero

    trust in the nodes If you have no trust Proof of Work is a good compromise But the electricity cost is the compromise LEVELS OF TRUST
  15. Martin Beeby @thebeebs You have trust and want to have

    decentralised consensus You want to see all the transitions and have distributed consensus You want to add and remove members. ENTERPRISE TRUST ISSUES CAN BE DIFFERENT
  16. Martin Beeby @thebeebs Private and Permissioned Membership Service Provider (MSP)

    Pluggable: MSP/Consensus/Ledger Format HYPERLEDGER FABRIC Channels
  17. Martin Beeby @thebeebs THE TUNA SUPPLY CHAIN From Sea to

    Table Lots of Fraud Could benefit from Transparency
  18. Martin Beeby @thebeebs TUNA SUPPLY CHAIN Sarah is the fisherman

    who sustainably and legally catches tuna. Regulators verify that the tuna has been legally/sustainably caught. Miriam is a restaurant owner who wants legal/sustainable tuna Carl is another restaurant owner fisherman Sarah can sell tuna to.
  19. Martin Beeby @thebeebs Sarah sells at different prices Privacy around

    deals Carl shouldn’t see Miriams price DEALS NEED TO BE PRIVATE
  20. Martin Beeby @thebeebs type Tuna struct { Vessel string ‘json:"vessel"’

    Datetime string ‘json:"datetime"’ Location string ‘json:"location"’ Holder string ‘json:"holder"’ } TUNA STRUCT
  21. Martin Beeby @thebeebs func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {

    tuna := []Tuna{ Tuna{Vessel: "923F", Location: "67.0006, -70.5476", Timestamp: "1504054225", Holder: "Miriam"}, Tuna{Vessel: "M83T", Location: "91.2395, -49.4594", Timestamp: "1504057825", Holder: "Dave"}, Tuna{Vessel: "T012", Location: "58.0148, 59.01391", Timestamp: "1493517025", Holder: "Igor"}, Tuna{Vessel: "P490", Location: "-45.0945, 0.7949", Timestamp: "1496105425", Holder: "Amalea"}, Tuna{Vessel: "S439", Location: "-107.6043, 19.5003", Timestamp: "1493512301", Holder: "Rafa"}, Tuna{Vessel: "J205", Location: "-155.2304, -15.8723", Timestamp: "1494117101", Holder: "Shen"}, Tuna{Vessel: "S22L", Location: "103.8842, 22.1277", Timestamp: "1496104301", Holder: "Leila"}, Tuna{Vessel: "EI89", Location: "-132.3207, -34.0983", Timestamp: "1485066691", Holder: "Yuan"}, Tuna{Vessel: "129R", Location: "153.0054, 12.6429", Timestamp: "1485153091", Holder: "Carlo"}, Tuna{Vessel: "49W4", Location: "51.9435, 8.2735", Timestamp: "1487745091", Holder: "Fatima"}, } i := 0 for i < len(tuna) { fmt.Println("i is ", i) tunaAsBytes, _ := json.Marshal(tuna[i]) APIstub.PutState(strconv.Itoa(i+1), tunaAsBytes) fmt.Println("Added", tuna[i]) i = i + 1 } return shim.Success(nil) } INIT
  22. Martin Beeby @thebeebs func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {

    // Retrieve the requested Smart Contract function and arguments function, args := APIstub.GetFunctionAndParameters() // Route to the appropriate handler function to interact with the ledger appropriately if function == "queryTuna" { return s.queryTuna(APIstub, args) } else if function == "initLedger" { return s.initLedger(APIstub) } else if function == "recordTuna" { return s.recordTuna(APIstub, args) } else if function == "queryAllTuna" { return s.queryAllTuna(APIstub) } else if function == "changeTunaHolder" { return s.changeTunaHolder(APIstub, args) } return shim.Error("Invalid Smart Contract function name.") } INVOKE
  23. Martin Beeby @thebeebs func (s *SmartContract) queryTuna(APIstub shim.ChaincodeStubInterface, args []string)

    sc.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 1") } tunaAsBytes, _ := APIstub.GetState(args[0]) if tunaAsBytes == nil { return shim.Error(“Could not locate tuna”) } return shim.Success(tunaAsBytes) } QUERYTUNA
  24. Martin Beeby @thebeebs RECORDTUNA func (s *SmartContract) recordTuna(APIstub shim.ChaincodeStubInterface, args

    []string) sc.Response { if len(args) != 5 { return shim.Error("Incorrect number of arguments. Expecting 5") } var tuna = Tuna{ Vessel: args[1], Location: args[2], Timestamp: args[3], Holder: args[4]} tunaAsBytes, _ := json.Marshal(tuna) err := APIstub.PutState(args[0], tunaAsBytes) if err != nil { return shim.Error(fmt.Sprintf("Failed to record tuna catch: %s", args[0])) } return shim.Success(nil) }
  25. Martin Beeby @thebeebs func (s *SmartContract) changeTunaHolder(APIstub shim.ChaincodeStubInterface, args []string)

    sc.Response { if len(args) != 2 { return shim.Error("Incorrect number of arguments. Expecting 2") } tunaAsBytes, _ := APIstub.GetState(args[0]) if tunaAsBytes != nil { return shim.Error("Could not locate tuna") } tuna := Tuna{} json.Unmarshal(tunaAsBytes, &tuna) // Normally check that the specified argument is a valid holder of tuna but here we are skipping this check for this example. tuna.Holder = args[1] tunaAsBytes, _ = json.Marshal(tuna) err := APIstub.PutState(args[0], tunaAsBytes) if err != nil { return shim.Error(fmt.Sprintf("Failed to change tuna holder: %s", args[0])) } return shim.Success(nil) } CHANGETUNAHOLDER
  26. Martin Beeby @thebeebs CALLING SOME CODE // changeTunaHolder - requires

    2 argument var request = { chaincodeId:’tuna-app’, fcn: 'changeTunaHolder', args: ['1', 'Alex'], chainId: 'mychannel', txId: tx_id }; return channel.sendTransactionProposal(request);