aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaran <maran.hidskes@gmail.com>2014-03-31 18:54:37 +0800
committerMaran <maran.hidskes@gmail.com>2014-03-31 18:54:37 +0800
commit5f49a659c36dbfb8c330ddc3d4565c19a9a936b5 (patch)
tree61e4c356238474e6c7e9965755e4a37a95a19f18
parent6253d109389d49e47772597de24cd11874b91338 (diff)
downloaddexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.tar
dexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.tar.gz
dexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.tar.bz2
dexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.tar.lz
dexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.tar.xz
dexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.tar.zst
dexon-5f49a659c36dbfb8c330ddc3d4565c19a9a936b5.zip
More blockchain testing
-rw-r--r--ethchain/block_chain.go13
-rw-r--r--ethchain/block_chain_test.go35
-rw-r--r--ethutil/rlp_test.go10
-rw-r--r--ethutil/trie_test.go2
4 files changed, 51 insertions, 9 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go
index 0e3601a4b..8c03eec38 100644
--- a/ethchain/block_chain.go
+++ b/ethchain/block_chain.go
@@ -126,6 +126,7 @@ func (bc *BlockChain) FindCanonicalChain(blocks []*Block, commonBlockHash []byte
log.Println("[CHAIN] We have found the common parent block, breaking")
break
}
+ log.Println("Checking incoming blocks:")
chainDifficulty.Add(chainDifficulty, bc.CalculateBlockTD(block))
}
@@ -139,13 +140,20 @@ func (bc *BlockChain) FindCanonicalChain(blocks []*Block, commonBlockHash []byte
log.Println("[CHAIN] We have found the common parent block, breaking")
break
}
- log.Println("CHECKING BLOGK:", i)
+ anOtherBlock := bc.GetBlock(block.PrevHash)
+ if anOtherBlock == nil {
+ // We do not want to count the genesis block for difficulty since that's not being sent
+ log.Println("[CHAIN] At genesis block, breaking")
+ break
+ }
+ log.Printf("CHECKING OUR OWN BLOCKS: %x", block.Hash())
+ log.Printf("%x", bc.GenesisBlock().Hash())
curChainDifficulty.Add(curChainDifficulty, bc.CalculateBlockTD(block))
}
log.Println("[CHAIN] Current chain difficulty:", curChainDifficulty)
if chainDifficulty.Cmp(curChainDifficulty) == 1 {
- log.Println("[CHAIN] The incoming Chain beat our asses, resetting")
+ log.Printf("[CHAIN] The incoming Chain beat our asses, resetting to block: %x", commonBlockHash)
bc.ResetTillBlockHash(commonBlockHash)
return false
} else {
@@ -165,6 +173,7 @@ func (bc *BlockChain) ResetTillBlockHash(hash []byte) error {
// END TODO
bc.Ethereum.StateManager().PrepareDefault(returnTo)
+
err := ethutil.Config.Db.Delete(lastBlock.Hash())
if err != nil {
return err
diff --git a/ethchain/block_chain_test.go b/ethchain/block_chain_test.go
index 736247e83..30eb62266 100644
--- a/ethchain/block_chain_test.go
+++ b/ethchain/block_chain_test.go
@@ -78,15 +78,38 @@ func (tm *TestManager) CreateChain2() error {
return err
}
-func TestBlockChainReorg(t *testing.T) {
+func TestNegativeBlockChainReorg(t *testing.T) {
+ // We are resetting the database between creation so we need to cache our information
+ testManager2 := NewTestManager()
+ testManager2.CreateChain2()
+ tm2Blocks := testManager2.Blocks
+
testManager := NewTestManager()
testManager.CreateChain1()
+ oldState := testManager.BlockChain().CurrentBlock.State()
+
+ if testManager.BlockChain().FindCanonicalChain(tm2Blocks, testManager.BlockChain().GenesisBlock().Hash()) != true {
+ t.Error("I expected TestManager to have the longest chain, but it was TestManager2 instead.")
+ }
+ if testManager.BlockChain().CurrentBlock.State() != oldState {
+ t.Error("I expected the top state to be the same as it was as before the reorg")
+ }
+
+}
+
+func TestPositiveBlockChainReorg(t *testing.T) {
+ testManager := NewTestManager()
+ testManager.CreateChain1()
+ tm1Blocks := testManager.Blocks
+
testManager2 := NewTestManager()
testManager2.CreateChain2()
+ oldState := testManager2.BlockChain().CurrentBlock.State()
- // This fails because we keep resetting the DB
- block := testManager.BlockChain().GetBlock(testManager.BlockChain().CurrentBlock.PrevHash)
- fmt.Println(block)
- //testManager.BlockChain().FindCanonicalChain(testManager2.Blocks, testManager.BlockChain().GenesisBlock().Hash())
-
+ if testManager2.BlockChain().FindCanonicalChain(tm1Blocks, testManager.BlockChain().GenesisBlock().Hash()) == true {
+ t.Error("I expected TestManager to have the longest chain, but it was TestManager2 instead.")
+ }
+ if testManager2.BlockChain().CurrentBlock.State() == oldState {
+ t.Error("I expected the top state to have been modified but it was not")
+ }
}
diff --git a/ethutil/rlp_test.go b/ethutil/rlp_test.go
index 2a58bfc0f..ce2535663 100644
--- a/ethutil/rlp_test.go
+++ b/ethutil/rlp_test.go
@@ -2,6 +2,7 @@ package ethutil
import (
"bytes"
+ "fmt"
"math/big"
"reflect"
"testing"
@@ -55,6 +56,15 @@ func TestValue(t *testing.T) {
}
}
+func TestEncodeDecodeMaran(t *testing.T) {
+ b := NewValue([]interface{}{"dog", 15, []interface{}{"cat", "cat", []interface{}{}}, 1024, "tachikoma"})
+ a := b.Encode()
+ fmt.Println("voor maran", a)
+ f, i := Decode(a, 0)
+ fmt.Println("voor maran 2", f)
+ fmt.Println(i)
+}
+
func TestEncode(t *testing.T) {
strRes := "\x83dog"
bytes := Encode("dog")
diff --git a/ethutil/trie_test.go b/ethutil/trie_test.go
index 79e5de921..0be512d9f 100644
--- a/ethutil/trie_test.go
+++ b/ethutil/trie_test.go
@@ -1,7 +1,7 @@
package ethutil
import (
- "fmt"
+ _ "fmt"
"reflect"
"testing"
)