SlideShare ist ein Scribd-Unternehmen logo
1 von 65
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

ドメイン駆動設計 本格入門
ドメイン駆動設計 本格入門ドメイン駆動設計 本格入門
ドメイン駆動設計 本格入門増田 亨
 
Go言語のスライスを理解しよう
Go言語のスライスを理解しようGo言語のスライスを理解しよう
Go言語のスライスを理解しようYasutaka Kawamoto
 
近ごろサイボウズで流行ってる「敷居が低いLT」の話
近ごろサイボウズで流行ってる「敷居が低いLT」の話近ごろサイボウズで流行ってる「敷居が低いLT」の話
近ごろサイボウズで流行ってる「敷居が低いLT」の話Ko Kazaana
 
[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida
[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida
[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi YoshidaInsight Technology, Inc.
 
イミュータブルデータモデルの極意
イミュータブルデータモデルの極意イミュータブルデータモデルの極意
イミュータブルデータモデルの極意Yoshitaka Kawashima
 
関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐり関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐりKazuyuki TAKASE
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -onozaty
 
データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3
データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3 データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3
データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3 Hiroshi Ito
 
DevOpsにおけるAnsibleの立ち位置と使い所
DevOpsにおけるAnsibleの立ち位置と使い所DevOpsにおけるAnsibleの立ち位置と使い所
DevOpsにおけるAnsibleの立ち位置と使い所Hidetoshi Hirokawa
 
本当は恐ろしい分散システムの話
本当は恐ろしい分散システムの話本当は恐ろしい分散システムの話
本当は恐ろしい分散システムの話Kumazaki Hiroki
 
DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!kwatch
 
ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計Tadayoshi Sato
 
2022_sakura-yube_ddd.pdf
2022_sakura-yube_ddd.pdf2022_sakura-yube_ddd.pdf
2022_sakura-yube_ddd.pdftoshiki kawai
 
イミュータブルデータモデル(世代編)
イミュータブルデータモデル(世代編)イミュータブルデータモデル(世代編)
イミュータブルデータモデル(世代編)Yoshitaka Kawashima
 
ドメイン駆動設計 ( DDD ) をやってみよう
ドメイン駆動設計 ( DDD ) をやってみようドメイン駆動設計 ( DDD ) をやってみよう
ドメイン駆動設計 ( DDD ) をやってみよう増田 亨
 
脱RESTful API設計の提案
脱RESTful API設計の提案脱RESTful API設計の提案
脱RESTful API設計の提案樽八 仲川
 
SPAセキュリティ入門~PHP Conference Japan 2021
SPAセキュリティ入門~PHP Conference Japan 2021SPAセキュリティ入門~PHP Conference Japan 2021
SPAセキュリティ入門~PHP Conference Japan 2021Hiroshi Tokumaru
 
金勘定のためのBigDecimalそしてMoney and Currency API
金勘定のためのBigDecimalそしてMoney and Currency API金勘定のためのBigDecimalそしてMoney and Currency API
金勘定のためのBigDecimalそしてMoney and Currency APITaku Miyakawa
 

Was ist angesagt? (20)

Tackling Complexity
Tackling ComplexityTackling Complexity
Tackling Complexity
 
ドメイン駆動設計 本格入門
ドメイン駆動設計 本格入門ドメイン駆動設計 本格入門
ドメイン駆動設計 本格入門
 
Go言語のスライスを理解しよう
Go言語のスライスを理解しようGo言語のスライスを理解しよう
Go言語のスライスを理解しよう
 
近ごろサイボウズで流行ってる「敷居が低いLT」の話
近ごろサイボウズで流行ってる「敷居が低いLT」の話近ごろサイボウズで流行ってる「敷居が低いLT」の話
近ごろサイボウズで流行ってる「敷居が低いLT」の話
 
[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida
[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida
[A31]AWS上でOracleを利用するためのはじめの一歩!by Masatoshi Yoshida
 
イミュータブルデータモデルの極意
イミュータブルデータモデルの極意イミュータブルデータモデルの極意
イミュータブルデータモデルの極意
 
関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐり関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐり
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
 
データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3
データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3 データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3
データ履歴管理のためのテンポラルデータモデルとReladomoの紹介 #jjug_ccc #ccc_g3
 
DevOpsにおけるAnsibleの立ち位置と使い所
DevOpsにおけるAnsibleの立ち位置と使い所DevOpsにおけるAnsibleの立ち位置と使い所
DevOpsにおけるAnsibleの立ち位置と使い所
 
本当は恐ろしい分散システムの話
本当は恐ろしい分散システムの話本当は恐ろしい分散システムの話
本当は恐ろしい分散システムの話
 
DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!
 
ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計
 
At least onceってぶっちゃけ問題の先送りだったよね #kafkajp
At least onceってぶっちゃけ問題の先送りだったよね #kafkajpAt least onceってぶっちゃけ問題の先送りだったよね #kafkajp
At least onceってぶっちゃけ問題の先送りだったよね #kafkajp
 
2022_sakura-yube_ddd.pdf
2022_sakura-yube_ddd.pdf2022_sakura-yube_ddd.pdf
2022_sakura-yube_ddd.pdf
 
イミュータブルデータモデル(世代編)
イミュータブルデータモデル(世代編)イミュータブルデータモデル(世代編)
イミュータブルデータモデル(世代編)
 
ドメイン駆動設計 ( DDD ) をやってみよう
ドメイン駆動設計 ( DDD ) をやってみようドメイン駆動設計 ( DDD ) をやってみよう
ドメイン駆動設計 ( DDD ) をやってみよう
 
脱RESTful API設計の提案
脱RESTful API設計の提案脱RESTful API設計の提案
脱RESTful API設計の提案
 
SPAセキュリティ入門~PHP Conference Japan 2021
SPAセキュリティ入門~PHP Conference Japan 2021SPAセキュリティ入門~PHP Conference Japan 2021
SPAセキュリティ入門~PHP Conference Japan 2021
 
金勘定のためのBigDecimalそしてMoney and Currency API
金勘定のためのBigDecimalそしてMoney and Currency API金勘定のためのBigDecimalそしてMoney and Currency API
金勘定のためのBigDecimalそしてMoney and Currency API
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
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
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
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
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 

Kürzlich hochgeladen (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
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
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
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...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
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...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 

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