From d2eb6a8db193ff71376554981af35481987402ba Mon Sep 17 00:00:00 2001 From: vivid Date: Sun, 17 Jun 2018 18:23:56 -0700 Subject: [PATCH] add script and js for testing, update difficulty in consensus.go --- consensus/ethash/consensus.go | 4 ++- genesis.json | 12 +++++++++ node0.js | 47 +++++++++++++++++++++++++++++++++++ node0.sh | 7 ++++++ node1.js | 19 ++++++++++++++ node1.sh | 7 ++++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 genesis.json create mode 100644 node0.js create mode 100644 node0.sh create mode 100644 node1.js create mode 100644 node1.sh diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 99eec82211..dab04e88af 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -287,8 +287,10 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent * // CalcDifficulty is the difficulty adjustment algorithm. It returns // the difficulty that a new block should have when created at time // given the parent block's time and difficulty. +// TODO Wei: update the difficulty calculation function to reduce the rate of blck difficulty increment func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { - return CalcDifficulty(chain.Config(), time, parent) + //return CalcDifficulty(chain.Config(), time, parent) + return big.NewInt(0x200) } // CalcDifficulty is the difficulty adjustment algorithm. It returns diff --git a/genesis.json b/genesis.json new file mode 100644 index 0000000000..5423d4bf91 --- /dev/null +++ b/genesis.json @@ -0,0 +1,12 @@ + +{ "config": { + "chainId": 987, + "homesteadBlock": 0, + "eip155Block": 0, + "eip158Block": 0 + }, + "difficulty": "0x200", + "gasLimit": "0x8000000", + "alloc": {} +} +} diff --git a/node0.js b/node0.js new file mode 100644 index 0000000000..b256e08f39 --- /dev/null +++ b/node0.js @@ -0,0 +1,47 @@ +function sleep(milliseconds) { + var start = new Date().getTime(); + for (var i = 0; i < 1e7; i++) { + if ((new Date().getTime() - start) > milliseconds){ + break; + } + } +} + +function interaction() { + personal.newAccount("123456") + personal.newAccount("123456") + acc0 = web3.eth.accounts[0] + acc1 = web3.eth.accounts[1] + console.log("account list: " + eth.accounts) + console.log("acc0 balance: " + web3.fromWei(web3.eth.getBalance(acc0))) + console.log("acc1 balance: " + web3.fromWei(web3.eth.getBalance(acc1))) + + miner.setEtherbase(eth.accounts[0]) + miner.start() + + while (1) { + balance = web3.fromWei(web3.eth.getBalance(acc0)) + if (balance > 0) { + miner.stop() + personal.unlockAccount(acc0, "123456") + eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(1,"ether")}) + miner.start() + // sleep(10000) + // if (balance1 = balance - 1) { + while (1) { + balance1 = web3.fromWei(web3.eth.getBalance(acc1)) + if (balance1 > 0) { + miner.stop() + break + } + } + console.log("acc0 balance: " + web3.fromWei(web3.eth.getBalance(acc0))) + console.log("acc1 balance: " + web3.fromWei(web3.eth.getBalance(acc1))) + // } + break + } + } + console.log(admin.nodeInfo.id) +} + +interaction() diff --git a/node0.sh b/node0.sh new file mode 100644 index 0000000000..0bcfe99db0 --- /dev/null +++ b/node0.sh @@ -0,0 +1,7 @@ +rm -rf /home/vivid/.ethereum/* +echo "main node datadir cleared" +./geth --datadir /home/vivid/.ethereum init genesis.json + +sleep 1s +./geth --datadir /home/vivid/.ethereum --port 30303 --networkid 9876 console + diff --git a/node1.js b/node1.js new file mode 100644 index 0000000000..34cf440edc --- /dev/null +++ b/node1.js @@ -0,0 +1,19 @@ +function sleep(milliseconds) { + var start = new Date().getTime(); + for (var i = 0; i < 1e7; i++) { + if ((new Date().getTime() - start) > milliseconds){ + break; + } + } +} + +function interaction() { + console.log("peer count: " + net.peerCount) + admin.addPeer("enode://a321db2296eb0078fab331d27460456f9c66c478049ee34441595d9c4fc4391af31f24b81fcdf21f28a7e391d07ab4fc550b6e235021abad310992f32d2fc876@127.0.0.1:30303") + sleep(5000) + console.log("peer count: " + net.peerCount) + + +} + +interaction() diff --git a/node1.sh b/node1.sh new file mode 100644 index 0000000000..049d48d747 --- /dev/null +++ b/node1.sh @@ -0,0 +1,7 @@ +rm -rf /home/vivid/peernode/* +echo "peer node datadir cleared" +./geth --datadir /home/vivid/peernode init genesis.json + +sleep 1s +./geth --datadir /home/vivid/peernode --port 30304 --networkid 9876 console +