SlideShare une entreprise Scribd logo
1  sur  65
Télécharger pour lire hors ligne
Scala
2018/11/10 Scala Summit 2018
• @itohiro73
• FOLIO
• Reladomo in Scala Scala
Summit 2017
• 2009 Satoshi Nakamoto
Bitcoin https://bitcoin.org/bitcoin.pdf
• Bitcoin
•
• 2017
•
•
•
🙋
👍
0
🤔
•
✓
• Proof of Work
✓
✓nonse Proof of Work
✓mining
•
•
•
•
import java.math.BigInteger
import java.security.MessageDigest
object HashFunction {
val MD_SHA256 = MessageDigest.getInstance("SHA-256")
def sha256(input: String): String =
{
String.format("%064x", new BigInteger(1,
MD_SHA256.digest(input.getBytes("UTF-8"))))
}
}
scala> HashFunction.sha256("a")
res0: String =
ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785
afee48bb
scala> HashFunction.sha256("test")
res1: String =
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15
b0f00a08
scala> HashFunction.sha256("same input")
res2: String =
c2f991739d5824b4e1d8bafaffb735b9e4061f801d82c4aaf57aea02
495f750c
scala> HashFunction.sha256("same input")
res3: String =
c2f991739d5824b4e1d8bafaffb735b9e4061f801d82c4aaf57aea02
495f750c
??? =
f0157537806d0ec6fd86455f6603bbaf1efed378798bcb43377456bc
6a8bfd78
scala> HashFunction.sha256("random input1")
res5: String =
3ef129a908f6b5e0c11cade17bb27691f6bd8cfffbb18622448a5fb229ca724d
scala> HashFunction.sha256("random input2")
res6: String =
dac708dccfe5ccbf009c9e5cd43ee9bf592abd0c7c04b421d8fe79f7518784c3
scala> HashFunction.sha256("random input3")
res7: String =
30b9b0875d568d926cf653f2360e95c86e8b798574a0ec95ceb7ef43cc9768ad
class Block(val timestamp: LocalDateTime, val data:
String, val previousHash: String) {
val hash = calculateHash()
def calculateHash(): String = {
val value = timestamp.toString + data + previousHash
HashFunction.sha256(value)
}
}
override def toString: String = {
"{ntTimestamp: " + this.timestamp +
"ntData: " + this.data +
"ntPrevious Hash: " + this.previousHash +
"ntHash:" + this.hash +
"n}"
}
class Blockchain(){
var chain = Seq[Block](createGenesisBlock())
def createGenesisBlock(): Block = {
new Block(LocalDateTime.now(), "A genesis block")
}
def addBlock(block: Block): Unit = {
this.chain = this.chain :+ block
}
def getLastBlock(): Block = {
this.chain.last
}
}
override def toString: String = {
this.chain.mkString("n")
}
def isChainValid(): Boolean = {
for(i <- 1 until this.chain.length) {
val currentBlock = this.chain(i)
val previousBlock = this.chain(i-1)
if(currentBlock.hash != currentBlock.calculateHash())
return false
if(currentBlock.previousHash != previousBlock.hash)
return false
}
return true
}
•Bitcoin Proof of
Work
•Proof of Work
0
✓ Satoshi Nakamoto
•
• nonce
• mining
• =difficulty
✓Bitcoin 10 difficulty
• nonce
• nonce
class Block(val timestamp: LocalDateTime, val data: String, val previousHash: String = "") {
var hash = calculateHash()
var nonce = 0
def calculateHash(): String = {
val value = timestamp.toString + data + previousHash + nonce
HashFunction.sha256(value)
}
def mineBlock(difficulty: Int): Unit = {
while(this.hash.substring(0, difficulty) != Array.fill(difficulty)("0").mkString ) {
this.nonce += 1
this.hash = calculateHash()
}
println("Block mined: " + this.hash)
}
class Blockchain {
var chain = Seq[Block](createGenesisBlock())
val difficulty = 4
def createGenesisBlock(): Block = {
new Block(LocalDateTime.now(), "A genesis block.")
}
def addBlock(block: Block): Unit = {
block.mineBlock(difficulty)
this.chain = this.chain :+ block
}
•nonce
Proof of Work
•Blockchain Block
class Transaction(val fromAddress: String, val
toAddress: String, val amount: Double) {
override def toString: String = {
"From Address: " + fromAddress + ", To Address: " +
toAddress + ", Amount: " + amount
}
}
import java.time.LocalDateTime
class Block(val timestamp: LocalDateTime, val transactions: Seq[Transaction], val previousHash: String = "") {
var hash = this.calculateHash()
var nonce = 0
def calculateHash(): String = {
val value = this.timestamp + this.transactions.mkString + this.previousHash + this.nonce
HashFunction.sha256(value)
}
def mineBlock(difficulty: Int): Unit = {
while(this.hash.substring(0, difficulty) != Array.fill(difficulty)("0").mkString) {
this.nonce += 1
this.hash = this.calculateHash()
}
}
override def toString: String = {
"{ntTimestamp: " + this.timestamp +
"ntTransaction: " + this.transactions.mkString("ntt{nttt", "nttt", "ntt}") +
"ntPrevious Hash: " + this.previousHash +
"ntHash: " + this.hash +
"ntNonse: " + this.nonce +
"n}"
}
class Blockchain {
var chain = Seq[Block](createGenesisBlock())
val difficulty = 4
var pendingTransactions = Seq[Transaction]()
val miningReward = 100
def createGenesisBlock(): Block = {
new Block(LocalDateTime.now(), Seq(new
Transaction("", "Initial Pool Address", 500)))
}
def addTransaction(transaction: Transaction): Unit = {
this.pendingTransactions = this.pendingTransactions :+ transaction
}
def minePendingTransactions(rewardAddress: String): Unit = {
val newBlock = new Block(LocalDateTime.now(), this.pendingTransactions)
newBlock.mineBlock(this.difficulty)
println("Block successfully mined: " + newBlock)
this.chain = this.chain :+ newBlock
this.pendingTransactions = Seq[Transaction](new Transaction("Reward",
rewardAddress, this.miningReward))
}
def getBalanceOfAddress(address: String): Double = {
var balance = 0.0
this.chain.flatMap(block =>
block.transactions).foreach(transaction => {
if(transaction.fromAddress == address)
balance -= transaction.amount
if(transaction.toAddress == address)
balance += transaction.amount
})
balance
}
•
✓
• Proof of Work
✓
✓nonse Proof of Work
✓mining
https://github.com/itohiro73/blockchain-scala
Satoshi Nakamoto (2009) Bitcoin: A Peer-to-Peer Electronic Cash System
2017
2018
Building a blockchain with Javascript

Contenu connexe

Tendances

Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜
Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜
Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜Takahiro Inoue
 
ダブル配列の実装方法
ダブル配列の実装方法ダブル配列の実装方法
ダブル配列の実装方法Higashiyama Masahiko
 
オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選Takuya Ueda
 
ドメイン駆動設計(DDD)の実践Part2
ドメイン駆動設計(DDD)の実践Part2ドメイン駆動設計(DDD)の実践Part2
ドメイン駆動設計(DDD)の実践Part2増田 亨
 
WikipediaからのSolr用類義語辞書の自動生成
WikipediaからのSolr用類義語辞書の自動生成WikipediaからのSolr用類義語辞書の自動生成
WikipediaからのSolr用類義語辞書の自動生成Koji Sekiguchi
 
ドメインオブジェクトの見つけ方・作り方・育て方
ドメインオブジェクトの見つけ方・作り方・育て方ドメインオブジェクトの見つけ方・作り方・育て方
ドメインオブジェクトの見つけ方・作り方・育て方増田 亨
 
ドメイン駆動設計に15年取り組んでわかったこと
ドメイン駆動設計に15年取り組んでわかったことドメイン駆動設計に15年取り組んでわかったこと
ドメイン駆動設計に15年取り組んでわかったこと増田 亨
 
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~Miki Shimogai
 
毎日が越境だ!
毎日が越境だ!毎日が越境だ!
毎日が越境だ!増田 亨
 
DDDのモデリングとは何なのか、 そしてどうコードに落とすのか
DDDのモデリングとは何なのか、 そしてどうコードに落とすのかDDDのモデリングとは何なのか、 そしてどうコードに落とすのか
DDDのモデリングとは何なのか、 そしてどうコードに落とすのかKoichiro Matsuoka
 
Where狙いのキー、order by狙いのキー
Where狙いのキー、order by狙いのキーWhere狙いのキー、order by狙いのキー
Where狙いのキー、order by狙いのキーyoku0825
 
Goの時刻に関するテスト
Goの時刻に関するテストGoの時刻に関するテスト
Goの時刻に関するテストKentaro Kawano
 
「多要素認証」と言っても色々あるんです
「多要素認証」と言っても色々あるんです「多要素認証」と言っても色々あるんです
「多要素認証」と言っても色々あるんですIIJ
 
イミュータブルデータモデルの極意
イミュータブルデータモデルの極意イミュータブルデータモデルの極意
イミュータブルデータモデルの極意Yoshitaka Kawashima
 
カラムストアインデックス 最初の一歩
カラムストアインデックス 最初の一歩カラムストアインデックス 最初の一歩
カラムストアインデックス 最初の一歩Masayuki Ozawa
 
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考えるGoのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考えるpospome
 
3週連続DDDその3 ドメイン駆動設計 戦略的設計
3週連続DDDその3  ドメイン駆動設計 戦略的設計3週連続DDDその3  ドメイン駆動設計 戦略的設計
3週連続DDDその3 ドメイン駆動設計 戦略的設計増田 亨
 
Apache Avro vs Protocol Buffers
Apache Avro vs Protocol BuffersApache Avro vs Protocol Buffers
Apache Avro vs Protocol BuffersSeiya Mizuno
 

Tendances (20)

Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜
Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜
Map Reduce 〜入門編:仕組みの理解とアルゴリズムデザイン〜
 
ダブル配列の実装方法
ダブル配列の実装方法ダブル配列の実装方法
ダブル配列の実装方法
 
オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選
 
ドメイン駆動設計(DDD)の実践Part2
ドメイン駆動設計(DDD)の実践Part2ドメイン駆動設計(DDD)の実践Part2
ドメイン駆動設計(DDD)の実践Part2
 
WikipediaからのSolr用類義語辞書の自動生成
WikipediaからのSolr用類義語辞書の自動生成WikipediaからのSolr用類義語辞書の自動生成
WikipediaからのSolr用類義語辞書の自動生成
 
ドメインオブジェクトの見つけ方・作り方・育て方
ドメインオブジェクトの見つけ方・作り方・育て方ドメインオブジェクトの見つけ方・作り方・育て方
ドメインオブジェクトの見つけ方・作り方・育て方
 
ドメイン駆動設計に15年取り組んでわかったこと
ドメイン駆動設計に15年取り組んでわかったことドメイン駆動設計に15年取り組んでわかったこと
ドメイン駆動設計に15年取り組んでわかったこと
 
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
 
毎日が越境だ!
毎日が越境だ!毎日が越境だ!
毎日が越境だ!
 
Spring と TDD
Spring と TDDSpring と TDD
Spring と TDD
 
DDDのモデリングとは何なのか、 そしてどうコードに落とすのか
DDDのモデリングとは何なのか、 そしてどうコードに落とすのかDDDのモデリングとは何なのか、 そしてどうコードに落とすのか
DDDのモデリングとは何なのか、 そしてどうコードに落とすのか
 
Where狙いのキー、order by狙いのキー
Where狙いのキー、order by狙いのキーWhere狙いのキー、order by狙いのキー
Where狙いのキー、order by狙いのキー
 
Goの時刻に関するテスト
Goの時刻に関するテストGoの時刻に関するテスト
Goの時刻に関するテスト
 
「多要素認証」と言っても色々あるんです
「多要素認証」と言っても色々あるんです「多要素認証」と言っても色々あるんです
「多要素認証」と言っても色々あるんです
 
pg_trgmと全文検索
pg_trgmと全文検索pg_trgmと全文検索
pg_trgmと全文検索
 
イミュータブルデータモデルの極意
イミュータブルデータモデルの極意イミュータブルデータモデルの極意
イミュータブルデータモデルの極意
 
カラムストアインデックス 最初の一歩
カラムストアインデックス 最初の一歩カラムストアインデックス 最初の一歩
カラムストアインデックス 最初の一歩
 
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考えるGoのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
 
3週連続DDDその3 ドメイン駆動設計 戦略的設計
3週連続DDDその3  ドメイン駆動設計 戦略的設計3週連続DDDその3  ドメイン駆動設計 戦略的設計
3週連続DDDその3 ドメイン駆動設計 戦略的設計
 
Apache Avro vs Protocol Buffers
Apache Avro vs Protocol BuffersApache Avro vs Protocol Buffers
Apache Avro vs Protocol Buffers
 

Similaire à Scalaで実装してみる簡易ブロックチェーン

The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196Mahmoud Samir Fayed
 
Importing Data into Neo4j quickly and easily - StackOverflow
Importing Data into Neo4j quickly and easily - StackOverflowImporting Data into Neo4j quickly and easily - StackOverflow
Importing Data into Neo4j quickly and easily - StackOverflowNeo4j
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopSaša Tatar
 
Graph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easilyGraph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easilyMark Needham
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31Mahmoud Samir Fayed
 
Scala introduction
Scala introductionScala introduction
Scala introductionvito jeng
 
GraphConnect Europe 2016 - Importing Data - Mark Needham, Michael Hunger
GraphConnect Europe 2016 - Importing Data - Mark Needham, Michael HungerGraphConnect Europe 2016 - Importing Data - Mark Needham, Michael Hunger
GraphConnect Europe 2016 - Importing Data - Mark Needham, Michael HungerNeo4j
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 28 of 180
The Ring programming language version 1.5.1 book - Part 28 of 180The Ring programming language version 1.5.1 book - Part 28 of 180
The Ring programming language version 1.5.1 book - Part 28 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 49 of 210
The Ring programming language version 1.9 book - Part 49 of 210The Ring programming language version 1.9 book - Part 49 of 210
The Ring programming language version 1.9 book - Part 49 of 210Mahmoud Samir Fayed
 
Mary Had a Little λ (QCon)
Mary Had a Little λ (QCon)Mary Had a Little λ (QCon)
Mary Had a Little λ (QCon)Stephen Chin
 
The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212Mahmoud Samir Fayed
 
はじめてのUnitTest XCTestに触れて
はじめてのUnitTest XCTestに触れてはじめてのUnitTest XCTestに触れて
はじめてのUnitTest XCTestに触れてKenji Tanaka
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingWebinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingMongoDB
 
Blockchain com JavaScript
Blockchain com JavaScriptBlockchain com JavaScript
Blockchain com JavaScriptBeto Muniz
 
BlockChain implementation by python
BlockChain implementation by pythonBlockChain implementation by python
BlockChain implementation by pythonwonyong hwang
 
Iniciando com jquery
Iniciando com jqueryIniciando com jquery
Iniciando com jqueryDanilo Sousa
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 40 of 180
The Ring programming language version 1.5.1 book - Part 40 of 180The Ring programming language version 1.5.1 book - Part 40 of 180
The Ring programming language version 1.5.1 book - Part 40 of 180Mahmoud Samir Fayed
 

Similaire à Scalaで実装してみる簡易ブロックチェーン (20)

The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
 
Importing Data into Neo4j quickly and easily - StackOverflow
Importing Data into Neo4j quickly and easily - StackOverflowImporting Data into Neo4j quickly and easily - StackOverflow
Importing Data into Neo4j quickly and easily - StackOverflow
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loop
 
Graph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easilyGraph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easily
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
GraphConnect Europe 2016 - Importing Data - Mark Needham, Michael Hunger
GraphConnect Europe 2016 - Importing Data - Mark Needham, Michael HungerGraphConnect Europe 2016 - Importing Data - Mark Needham, Michael Hunger
GraphConnect Europe 2016 - Importing Data - Mark Needham, Michael Hunger
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
 
The Ring programming language version 1.5.1 book - Part 28 of 180
The Ring programming language version 1.5.1 book - Part 28 of 180The Ring programming language version 1.5.1 book - Part 28 of 180
The Ring programming language version 1.5.1 book - Part 28 of 180
 
The Ring programming language version 1.9 book - Part 49 of 210
The Ring programming language version 1.9 book - Part 49 of 210The Ring programming language version 1.9 book - Part 49 of 210
The Ring programming language version 1.9 book - Part 49 of 210
 
Mary Had a Little λ (QCon)
Mary Had a Little λ (QCon)Mary Had a Little λ (QCon)
Mary Had a Little λ (QCon)
 
The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212
 
はじめてのUnitTest XCTestに触れて
はじめてのUnitTest XCTestに触れてはじめてのUnitTest XCTestに触れて
はじめてのUnitTest XCTestに触れて
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingWebinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
 
Blockchain com JavaScript
Blockchain com JavaScriptBlockchain com JavaScript
Blockchain com JavaScript
 
BlockChain implementation by python
BlockChain implementation by pythonBlockChain implementation by python
BlockChain implementation by python
 
Iniciando com jquery
Iniciando com jqueryIniciando com jquery
Iniciando com jquery
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210
 
The Ring programming language version 1.5.1 book - Part 40 of 180
The Ring programming language version 1.5.1 book - Part 40 of 180The Ring programming language version 1.5.1 book - Part 40 of 180
The Ring programming language version 1.5.1 book - Part 40 of 180
 

Dernier

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 

Dernier (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 

Scalaで実装してみる簡易ブロックチェーン

  • 2. • @itohiro73 • FOLIO • Reladomo in Scala Scala Summit 2017
  • 3.
  • 4. • 2009 Satoshi Nakamoto Bitcoin https://bitcoin.org/bitcoin.pdf • Bitcoin • • 2017
  • 6.
  • 7.
  • 8.
  • 10.
  • 11.
  • 12. 👍
  • 13.
  • 14. 0
  • 15. 🤔
  • 16.
  • 17.
  • 18. • ✓ • Proof of Work ✓ ✓nonse Proof of Work ✓mining
  • 19.
  • 21. import java.math.BigInteger import java.security.MessageDigest object HashFunction { val MD_SHA256 = MessageDigest.getInstance("SHA-256") def sha256(input: String): String = { String.format("%064x", new BigInteger(1, MD_SHA256.digest(input.getBytes("UTF-8")))) } }
  • 22. scala> HashFunction.sha256("a") res0: String = ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785 afee48bb scala> HashFunction.sha256("test") res1: String = 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15 b0f00a08
  • 23. scala> HashFunction.sha256("same input") res2: String = c2f991739d5824b4e1d8bafaffb735b9e4061f801d82c4aaf57aea02 495f750c scala> HashFunction.sha256("same input") res3: String = c2f991739d5824b4e1d8bafaffb735b9e4061f801d82c4aaf57aea02 495f750c
  • 25. scala> HashFunction.sha256("random input1") res5: String = 3ef129a908f6b5e0c11cade17bb27691f6bd8cfffbb18622448a5fb229ca724d scala> HashFunction.sha256("random input2") res6: String = dac708dccfe5ccbf009c9e5cd43ee9bf592abd0c7c04b421d8fe79f7518784c3 scala> HashFunction.sha256("random input3") res7: String = 30b9b0875d568d926cf653f2360e95c86e8b798574a0ec95ceb7ef43cc9768ad
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31. class Block(val timestamp: LocalDateTime, val data: String, val previousHash: String) { val hash = calculateHash() def calculateHash(): String = { val value = timestamp.toString + data + previousHash HashFunction.sha256(value) } }
  • 32. override def toString: String = { "{ntTimestamp: " + this.timestamp + "ntData: " + this.data + "ntPrevious Hash: " + this.previousHash + "ntHash:" + this.hash + "n}" }
  • 33. class Blockchain(){ var chain = Seq[Block](createGenesisBlock()) def createGenesisBlock(): Block = { new Block(LocalDateTime.now(), "A genesis block") } def addBlock(block: Block): Unit = { this.chain = this.chain :+ block } def getLastBlock(): Block = { this.chain.last } }
  • 34. override def toString: String = { this.chain.mkString("n") }
  • 35. def isChainValid(): Boolean = { for(i <- 1 until this.chain.length) { val currentBlock = this.chain(i) val previousBlock = this.chain(i-1) if(currentBlock.hash != currentBlock.calculateHash()) return false if(currentBlock.previousHash != previousBlock.hash) return false } return true }
  • 36.
  • 37.
  • 38.
  • 39.
  • 40. •Bitcoin Proof of Work •Proof of Work 0 ✓ Satoshi Nakamoto
  • 41. • • nonce • mining • =difficulty ✓Bitcoin 10 difficulty
  • 42.
  • 44. class Block(val timestamp: LocalDateTime, val data: String, val previousHash: String = "") { var hash = calculateHash() var nonce = 0 def calculateHash(): String = { val value = timestamp.toString + data + previousHash + nonce HashFunction.sha256(value) } def mineBlock(difficulty: Int): Unit = { while(this.hash.substring(0, difficulty) != Array.fill(difficulty)("0").mkString ) { this.nonce += 1 this.hash = calculateHash() } println("Block mined: " + this.hash) }
  • 45. class Blockchain { var chain = Seq[Block](createGenesisBlock()) val difficulty = 4 def createGenesisBlock(): Block = { new Block(LocalDateTime.now(), "A genesis block.") } def addBlock(block: Block): Unit = { block.mineBlock(difficulty) this.chain = this.chain :+ block }
  • 46.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56. class Transaction(val fromAddress: String, val toAddress: String, val amount: Double) { override def toString: String = { "From Address: " + fromAddress + ", To Address: " + toAddress + ", Amount: " + amount } }
  • 57. import java.time.LocalDateTime class Block(val timestamp: LocalDateTime, val transactions: Seq[Transaction], val previousHash: String = "") { var hash = this.calculateHash() var nonce = 0 def calculateHash(): String = { val value = this.timestamp + this.transactions.mkString + this.previousHash + this.nonce HashFunction.sha256(value) } def mineBlock(difficulty: Int): Unit = { while(this.hash.substring(0, difficulty) != Array.fill(difficulty)("0").mkString) { this.nonce += 1 this.hash = this.calculateHash() } } override def toString: String = { "{ntTimestamp: " + this.timestamp + "ntTransaction: " + this.transactions.mkString("ntt{nttt", "nttt", "ntt}") + "ntPrevious Hash: " + this.previousHash + "ntHash: " + this.hash + "ntNonse: " + this.nonce + "n}" }
  • 58. class Blockchain { var chain = Seq[Block](createGenesisBlock()) val difficulty = 4 var pendingTransactions = Seq[Transaction]() val miningReward = 100 def createGenesisBlock(): Block = { new Block(LocalDateTime.now(), Seq(new Transaction("", "Initial Pool Address", 500))) }
  • 59. def addTransaction(transaction: Transaction): Unit = { this.pendingTransactions = this.pendingTransactions :+ transaction } def minePendingTransactions(rewardAddress: String): Unit = { val newBlock = new Block(LocalDateTime.now(), this.pendingTransactions) newBlock.mineBlock(this.difficulty) println("Block successfully mined: " + newBlock) this.chain = this.chain :+ newBlock this.pendingTransactions = Seq[Transaction](new Transaction("Reward", rewardAddress, this.miningReward)) }
  • 60. def getBalanceOfAddress(address: String): Double = { var balance = 0.0 this.chain.flatMap(block => block.transactions).foreach(transaction => { if(transaction.fromAddress == address) balance -= transaction.amount if(transaction.toAddress == address) balance += transaction.amount }) balance }
  • 61.
  • 62.
  • 63. • ✓ • Proof of Work ✓ ✓nonse Proof of Work ✓mining
  • 65. Satoshi Nakamoto (2009) Bitcoin: A Peer-to-Peer Electronic Cash System 2017 2018 Building a blockchain with Javascript