Blockchain: Transactions, Blocks and Adding DataIn this demonstration we will examine blocks, transactions and writing data: In Blockchain we create transactions and these are then captured by miners by certain periods and then the miners compete to create a new hash, and which creates a new block: With Ethereum we can add data onto the Blockchain with a transaction. Let's stop the miners and add the string "hello" to the Blockchain. For this we create a variable named myData, and then use sendTransaction(): > miner.stop() true > var myData="0x68656c6c6f"; undefined > eth.sendTransaction({from:eth.accounts[0], gas:3141592, data: myData}) Error: authentication needed: password or unlock at web3.js:3104:20 at web3.js:6191:15 at web3.js:5004:36 at anonymous:1:2 > personal.unlockAccount(web3.eth.accounts[0],'Qwerty') true > eth.sendTransaction({from:eth.accounts[0], gas:3141592, data: myData}) "0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369" We now have a transaction number, so let's examine it:
> eth.getTransaction("0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369")
{
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xc7552f45deb093cafb47286a0bc9415845ca3735",
gas: 3141592,
gasPrice: 18000000000,
hash: "0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369",
input: "0x68656c6c6f",
nonce: 74,
r: "0x8b918d7e489450991f99db45717c38e0a53d4f96a9b328668f3efdf070be80df",
s: "0x7554476d9abd3924c14835d592120b3b6212ed3b4b9eda475da8c563e6a24e3",
to: null,
transactionIndex: 0,
v: "0x41",
value: 0
}
We can see the input property defines the data ("hello"), but there is no block number as it hasn't been mined yet. So let's start the miner:
> miner.start()
null
The miner doesn't pick it up straight away, as the blockNumber is still null:
> eth.getTransaction("0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369")
{
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xc7552f45deb093cafb47286a0bc9415845ca3735",
gas: 3141592,
gasPrice: 18000000000,
hash: "0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369",
input: "0x68656c6c6f",
nonce: 74,
r: "0x8b918d7e489450991f99db45717c38e0a53d4f96a9b328668f3efdf070be80df",
s: "0x7554476d9abd3924c14835d592120b3b6212ed3b4b9eda475da8c563e6a24e3",
to: null,
transactionIndex: 0,
v: "0x41",
value: 0
}
But eventually we see it:
> eth.getTransaction("0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369")
{
blockHash: "0x3709ba4981574413e4c5af775f586fa89d3a73060356c0f59ab5143524e75caf
",
blockNumber: 5134,
from: "0xc7552f45deb093cafb47286a0bc9415845ca3735",
gas: 3141592,
gasPrice: 18000000000,
hash: "0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369",
input: "0x68656c6c6f",
nonce: 74,
r: "0x8b918d7e489450991f99db45717c38e0a53d4f96a9b328668f3efdf070be80df",
s: "0x7554476d9abd3924c14835d592120b3b6212ed3b4b9eda475da8c563e6a24e3",
to: null,
transactionIndex: 0,
v: "0x41",
value: 0
}
We see now that that the transaction is with Block 5134, so let's examine that block:
> web3.eth.getBlock(5134)
{
difficulty: 958134,
extraData: "0xd983010606846765746887676f312e382e338777696e646f7773",
gasLimit: 4712388,
gasUsed: 53343,
hash: "0x3709ba4981574413e4c5af775f586fa89d3a73060356c0f59ab5143524e75caf",
logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000",
miner: "0xc7552f45deb093cafb47286a0bc9415845ca3735",
mixHash: "0x2760004c46842240f6cf8cd888cf28d94dc1764178d82fa06c2d25361909b82d",
nonce: "0x721e33400bb8c24b",
number: 5134,
parentHash: "0xdc6254b48f8379062f8a56943ed148649022c4adb5025ffb0b98b5f22206bb8a",
receiptsRoot: "0xc251f3128f854445a2d16fb1936824836d137b951e43bb330dcc3272e6fda9ec",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 630,
stateRoot: "0x0667d97ebd8090ee546863505a2439adcaa3788ab6e851410c9b696a0060b39a",
timestamp: 1498920608,
totalDifficulty: 2428672816,
transactions: ["0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369"],
transactionsRoot: "0xea5082474a5a95aa28668a1ca465ffb5d1e93a2d16a2cb74b47d85ce22bfd742",
uncles: []
}
We can see there is only one transaction there (0x88...), which is the one where we added "hello". Now let's view the transaction:
> web3.eth.getBlock(5134,true).transactions[0]
{
blockHash: "0x3709ba4981574413e4c5af775f586fa89d3a73060356c0f59ab5143524e75caf",
blockNumber: 5134,
from: "0xc7552f45deb093cafb47286a0bc9415845ca3735",
gas: 3141592,
gasPrice: 18000000000,
hash: "0x88add08d4f88af17ba03f68d675d57be39f43e27bd96002bdcec23e396d0a369",
input: "0x68656c6c6f",
nonce: 74,
r: "0x8b918d7e489450991f99db45717c38e0a53d4f96a9b328668f3efdf070be80df",
s: "0x7554476d9abd3924c14835d592120b3b6212ed3b4b9eda475da8c563e6a24e3",
to: null,
transactionIndex: 0,
v: "0x41",
value: 0
}
And finally view the data we added to the Blockchain:
> web3.eth.getBlock(5134,true).transactions[0].input
"0x68656c6c6f"
|