Lecture 6 - Block
Lecture 6 - Block
Introduction to SpartanGold
Prepared by:
Dr. Hossam Mahmoud Moftah
Faculty of Computers and Artificial Intelligence
Beni-Suef University
What is SpartanGold?
• It is a blockchain implementation written in JavaScript
patterned after Bitcoin.
• Several features of Bitcoin’s design are simplified or
eliminated, though those features may be added in easily.
• SpartanGold supports a single-threaded mode where
miners communicate via JavaScript events, and a multi-
process mode where miners and clients send messages
over TCP/IP.
• While the former mode (single-threaded) simplifies testing
and can provide cleaner demonstrations, the latter mode
(multi-process) provides a more realistic experience.
Prof. Tom Austin (Thomas H. Austin) , San José State University
SpartanGold: A Blockchain for Education, Experimentation, and Rapid Prototyping
April 2021: Communications in Computer and Information Science
DOI: 10.1007/978-3-030-72725-3_9
SpartanGold npm module
https://nodejs.org/
https://www.w3schools.com/
Similarities to Bitcoin
Bitcoin: SpartanGold:
• Transactions stored in a • Transactions stored in a
Merkle tree map
• Bitcoin script • No scripting language
• UTXO-based model • Account-based model
• Proof-of-work target • Proof-of-work target
adjusts over time fixed
• Fixed block size • No block size limit
https://www.nervos.org/
How Does the UTXO Model Work?
• UTXOs work similarly to cash, where each UTXO is like a unique fiat paper
bill that users can spend. Each user in UTXO-based blockchains can keep
track of its balance by adding up the cryptocurrencies in their possession.
• In UTXO-based blockchains, the $20 bill and the $10 change would be
represented as two separate UTXOs. So, in Bob’s case, his cryptocurrency
account balance is just a sum of his UTXOs, just like his physical wallet is
just a sum of all the different bills that he puts into his wallet.
https://www.nervos.org/
How Does the Account-Based Model Work?
• The bookkeeping in the account model works just like bank accounts, where
money transfers are recorded as debits and credits on different users'
accounts on the bank's ledger. For example, when Dave wants to send $10 to
Bob, the bank credits or deducts $10 from Dave's account and debits or adds
$10 to Bob's name in its ledger.
https://www.nervos.org/
Running insingle-threaded mode
$ node driver.js
Starting simulation. This may take a moment...
Initial balances:
Alice has 233 gold.
Bob has 99 gold.
Charlie has 67 gold.
Minnie has 400 gold.
Mickey has 300 gold.
Donald has 0 gold.
Alice is transfering 40 gold to zy2sIPBlf9PeM36D/
j0SyTznb8c3ESsDekNlZtSi06s=
Minnie: found proof for block 1: 5660
Minnie: cutting over to new chain.
Mickey: cutting over to new chain.
…
Your choice:
Source: Prof. Tom Austin, San José State University
Lab, part 1:
Experiment with SpartanGold
• Install with:
npm install spartan-gold
• Download singleThreadedExample.js, tcpMiner.js,
minnie.json, and mickey.json from the website.
• Experiment with single-threaded and multi-process mode.
• sign(privKey)
• validSignature()
• sufficientFunds(block)
– Pass in most recently confirmed block
– Returns true if client has enough gold for
transaction
• totalOutput()
– Sum of all outputs plus the transaction
fee
• isGenesisBlock()
– Genesis block has special rules.
• hasValidProof()
• balanceOf(addr): Gold available for specified client
• totalRewards(): Transaction fees + coinbase reward
• contains(tx): True if transaction is in current block
• addTransaction(tx)
– Overridden in BuggyClient for lab
• serialize(): Converts block to string form
– some fields are omitted
• rerun(prevBlock): Described next slide…
• Posts transactions
• Stores public/private keys
• Tracks blocks
– Listens for new blocks
– Verifies block validity
– Requests missing blocks
– Tracks last confirmed block
• initialize():
– set up listeners and begin mining
• findProof():
– searches for a valid PoW
• addTransaction(tx):
– Invoked on
Blockchain.POST_TRANSACTION
message
• deserializeBlock(s):
– converts string to instance of Block class
• makeBlock(…):
– Equivalent to new Block(…),
except that appropriate
subclass is above.
• makeTransaction(…):
– Equivalent to new
Transaction(…), except …
• makeGenesis(cfg): next slide…
Source: Prof. Tom Austin, San José State University
makeGenesis
• register(…clientList)
– Adds clients to list of known clients
• recognizes(client)
• sendMessage(address, msg,
o)
– address: client to send message to
– msg: name of the event
– o: payload of the message
• broadcast(msg, o)
– Calls sendMessage to all known
clients
Source: Prof. Tom Austin, San José State University
Lab, part 2: Replay attack