forked from mrsmkl/eth-patricia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testTR.js
96 lines (77 loc) · 3.85 KB
/
testTR.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const Web3 = require("../web3.js/packages/web3")
const web3 = new Web3()
const fs = require("fs")
const RLP = require('rlp')
const util = require('ethereumjs-util')
const txProof = require("./txProof")
const EthereumTx = require('ethereumjs-tx')
function tx2rlp(tx) {
const signedSiblingTx = new EthereumTx(squanchTx(tx))
return signedSiblingTx.serialize()
}
function tx2rlp(tx) {
const signedSiblingTx = new EthereumTx(squanchTx(tx))
console.log(signedSiblingTx)
return "0x" + Buffer.from(RLP.encode(signedSiblingTx.raw)).toString("hex")
}
function squanchTx(tx) {
console.log(tx)
tx.gas = '0x' + parseInt(tx.gas).toString(16)
tx.gasPrice = '0x' + parseInt(tx.gasPrice).toString(16)
tx.value = '0x' + parseInt(tx.value).toString(16) || '0'
tx.data = tx.input
return tx
}
var host = process.argv[2] || "localhost"
// web3.setProvider(new web3.providers.WebsocketProvider('ws://' + host + ':8546'))
web3.setProvider(new web3.providers.HttpProvider('http://' + host + ':8545'))
const dir = "./compiled/"
var send_opt
async function createContract(name, args) {
var code = "0x" + fs.readFileSync(dir + name + ".bin")
var abi = JSON.parse(fs.readFileSync(dir + name + ".abi"))
return new web3.eth.Contract(abi).deploy({data: code, arguments:args}).send(send_opt)
}
function hash(dta) {
return util.sha3(Buffer.from(dta.substr(2),"hex"))
}
async function doDeploy() {
var accts = await web3.eth.getAccounts()
send_opt = {gas:4700000, from:accts[0]}
var store = await createContract("Blockchain")
console.log("Contract at", store.options.address)
var my_tx = await store.methods.storeHashes(20).send(send_opt)
console.log("storing hashes", my_tx)
var bnum = await web3.eth.getBlockNumber()
var blk = await web3.eth.getBlock(bnum)
var bheader = await web3.eth.getBlockHeader(blk.hash)
console.log("current block", blk)
console.log("Checking hash", hash(bheader))
var tx = await store.methods.storeHashes(20).send(send_opt)
console.log("storing hashes again", tx.status)
var tx = await store.methods.storeHeader(bnum, bheader).send(send_opt)
console.log("storing header", tx.status)
/*
var lst = await txProof.build(2, blk, web3)
var proof_rlp = RLP.encode(lst)
console.log("proof for transaction", lst, proof_rlp, "... its length is", proof_rlp.length)
console.log("checking hash", util.sha3(RLP.encode(lst[0])), util.sha3(""))
console.log(await store.methods.transactionDebug("0x"+util.sha3("").toString("hex"), 2, "0x"+proof_rlp.toString("hex"), bnum).call(send_opt))
var tx = await store.methods.transactionInBlock("0x"+util.sha3("").toString("hex"), 2, "0x"+proof_rlp.toString("hex"), bnum).send(send_opt)
console.log("Proving transaction in block", tx.status)
*/
var lst = await txProof.build(my_tx.transactionIndex, blk, web3)
var proof_rlp = RLP.encode(lst)
console.log("proof for transaction", lst, proof_rlp, "... its length is", proof_rlp.length)
console.log("checking hash", util.sha3(RLP.encode(lst[0])))
console.log(await store.methods.transactionDebug(my_tx.transactionHash, my_tx.transactionIndex, "0x"+proof_rlp.toString("hex"), bnum).call(send_opt))
var tx = await store.methods.transactionInBlock(my_tx.transactionHash, my_tx.transactionIndex, "0x"+proof_rlp.toString("hex"), bnum).send(send_opt)
console.log("Proving transaction in block", tx.status)
var my_tx2 = await web3.eth.getTransaction(blk.transactions[my_tx.transactionIndex])
var tx_rlp = tx2rlp(my_tx2)
var tx = await store.methods.storeTransaction(tx_rlp).send(send_opt)
console.log("Adding transaction", tx.status, tx_rlp, hash(tx_rlp))
console.log(await store.methods.trInfo("0x"+hash(tx_rlp).toString("hex")).call(send_opt))
console.log(await store.methods.transactionSender(bnum, my_tx.transactionIndex).call(send_opt))
}
doDeploy()