From 2da367a2bee84d74d1bb0ea1b42d4c22fae486dd Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 26 Jan 2015 10:57:23 -0800 Subject: fix unchecked slice index on tx.From() --- core/transaction_pool.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/transaction_pool.go b/core/transaction_pool.go index d3aec9050..193db45ed 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -109,8 +109,13 @@ func (self *TxPool) Add(tx *types.Transaction) error { } else { to = "[NEW_CONTRACT]" } - - txplogger.Debugf("(t) %x => %s (%v) %x\n", tx.From()[:4], to, tx.Value, tx.Hash()) + var from string + if len(tx.From()) > 0 { + from = ethutil.Bytes2Hex(tx.From()[:4]) + } else { + from = "INVALID" + } + txplogger.Debugf("(t) %x => %s (%v) %x\n", from, to, tx.Value, tx.Hash()) // Notify the subscribers go self.eventMux.Post(TxPreEvent{tx}) -- cgit v1.2.3 From 9446489cf3f2eb4b5237b9355b3975fde2886508 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 17 Feb 2015 22:02:15 -0500 Subject: core: chain manager forking tests --- core/chain_manager_test.go | 276 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) (limited to 'core') diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index bc3a264d1..a357a8b1a 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -3,6 +3,7 @@ package core import ( "bytes" "fmt" + "math/big" "os" "path" "runtime" @@ -13,14 +14,167 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/state" ) +// So we can generate blocks easily +type fakePow struct{} + +func (f fakePow) Search(block pow.Block, stop <-chan struct{}) []byte { return nil } +func (f fakePow) Verify(block pow.Block) bool { return true } +func (f fakePow) GetHashrate() int64 { return 0 } +func (f fakePow) Turbo(bool) {} + func init() { runtime.GOMAXPROCS(runtime.NumCPU()) ethutil.ReadConfig("/tmp/ethtest", "/tmp/ethtest", "ETH") } +func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { + block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), nil, "") + + block.SetUncles(nil) + block.SetTransactions(nil) + block.SetReceipts(nil) + + header := block.Header() + header.Difficulty = CalcDifficulty(block, parent) + header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1) + header.GasLimit = CalcGasLimit(parent, block) + + block.Td = parent.Td + + return block +} + +// Actually make a block by simulating what miner would do +func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database) *types.Block { + addr := ethutil.LeftPadBytes([]byte{byte(i)}, 20) + block := newBlockFromParent(addr, parent) + state := state.New(block.Root(), db) + cbase := state.GetOrNewStateObject(addr) + cbase.SetGasPool(CalcGasLimit(parent, block)) + cbase.AddAmount(BlockReward) + state.Update(ethutil.Big0) + block.SetRoot(state.Root()) + return block +} + +// Make a chain with real blocks +// Runs ProcessWithParent to get proper state roots +func makeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database) types.Blocks { + bman.bc.currentBlock = parent + blocks := make(types.Blocks, max) + for i := 0; i < max; i++ { + block := makeBlock(bman, parent, i, db) + td, err := bman.processWithParent(block, parent) + if err != nil { + fmt.Println("process with parent failed", err) + panic(err) + } + block.Td = td + blocks[i] = block + parent = block + fmt.Printf("New Block: %x\n", block.Hash()) + } + fmt.Println("Done making chain") + return blocks +} + +// Create a new chain manager starting from given block +// Effectively a fork factory +func newChainManager(block *types.Block, eventMux *event.TypeMux, db ethutil.Database) *ChainManager { + bc := &ChainManager{db: db, genesisBlock: GenesisBlock(db), eventMux: eventMux} + if block == nil { + bc.Reset() + } else { + bc.currentBlock = block + bc.td = block.Td + } + return bc +} + +// block processor with fake pow +func newBlockProcessor(db ethutil.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor { + bman := NewBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) + bman.Pow = fakePow{} + return bman +} + +// Make a new canonical chain by running InsertChain +// on result of makeChain +func newCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { + eventMux := &event.TypeMux{} + txpool := NewTxPool(eventMux) + + bman := newBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) + bman.bc.SetProcessor(bman) + parent := bman.bc.CurrentBlock() + if n == 0 { + return bman, nil + } + lchain := makeChain(bman, parent, n, db) + bman.bc.InsertChain(lchain) + return bman, nil +} + +// Test fork of length N starting from block i +func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big.Int)) { + fmt.Println("Testing Fork!") + var b *types.Block = nil + if i > 0 { + b = bman.bc.GetBlockByNumber(uint64(i)) + } + _ = b + // switch databases to process the new chain + db, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + // copy old chain up to i into new db with deterministic canonical + bman2, err := newCanonical(i, db) + if err != nil { + t.Fatal("could not make new canonical in testFork", err) + } + bman2.bc.SetProcessor(bman2) + + parent := bman2.bc.CurrentBlock() + chainB := makeChain(bman2, parent, N, db) + bman2.bc.InsertChain(chainB) + + tdpre := bman.bc.Td() + td, err := testChain(chainB, bman) + if err != nil { + t.Fatal("expected chainB not to give errors:", err) + } + // Compare difficulties + f(tdpre, td) +} + +func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) { + td := new(big.Int) + for _, block := range chainB { + td2, err := bman.bc.processor.Process(block) + if err != nil { + if IsKnownBlockErr(err) { + continue + } + return nil, err + } + block.Td = td2 + td = td2 + + bman.bc.mu.Lock() + { + bman.bc.write(block) + } + bman.bc.mu.Unlock() + } + return td, nil +} + func loadChain(fn string, t *testing.T) (types.Blocks, error) { fh, err := os.OpenFile(path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "_data", fn), os.O_RDONLY, os.ModePerm) if err != nil { @@ -45,6 +199,128 @@ func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t * done <- true } +func TestExtendCanonical(t *testing.T) { + db, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + // make first chain starting from genesis + bman, err := newCanonical(5, db) + if err != nil { + t.Fatal("Could not make new canonical chain:", err) + } + f := func(td1, td2 *big.Int) { + if td2.Cmp(td1) <= 0 { + t.Error("expected chainB to have higher difficulty. Got", td2, "expected more than", td1) + } + } + // Start fork from current height (5) + testFork(t, bman, 5, 1, f) + testFork(t, bman, 5, 2, f) + testFork(t, bman, 5, 5, f) + testFork(t, bman, 5, 10, f) +} + +func TestShorterFork(t *testing.T) { + db, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + // make first chain starting from genesis + bman, err := newCanonical(10, db) + if err != nil { + t.Fatal("Could not make new canonical chain:", err) + } + f := func(td1, td2 *big.Int) { + if td2.Cmp(td1) >= 0 { + t.Error("expected chainB to have lower difficulty. Got", td2, "expected less than", td1) + } + } + // Sum of numbers must be less than 10 + // for this to be a shorter fork + testFork(t, bman, 0, 3, f) + testFork(t, bman, 0, 7, f) + testFork(t, bman, 1, 1, f) + testFork(t, bman, 1, 7, f) + testFork(t, bman, 5, 3, f) + testFork(t, bman, 5, 4, f) +} + +func TestLongerFork(t *testing.T) { + db, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + // make first chain starting from genesis + bman, err := newCanonical(10, db) + if err != nil { + t.Fatal("Could not make new canonical chain:", err) + } + f := func(td1, td2 *big.Int) { + if td2.Cmp(td1) <= 0 { + t.Error("expected chainB to have higher difficulty. Got", td2, "expected more than", td1) + } + } + // Sum of numbers must be greater than 10 + // for this to be a longer fork + testFork(t, bman, 0, 11, f) + testFork(t, bman, 0, 15, f) + testFork(t, bman, 1, 10, f) + testFork(t, bman, 1, 12, f) + testFork(t, bman, 5, 6, f) + testFork(t, bman, 5, 8, f) +} + +func TestEqualFork(t *testing.T) { + db, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + bman, err := newCanonical(10, db) + if err != nil { + t.Fatal("Could not make new canonical chain:", err) + } + f := func(td1, td2 *big.Int) { + if td2.Cmp(td1) != 0 { + t.Error("expected chainB to have equal difficulty. Got", td2, "expected ", td1) + } + } + // Sum of numbers must be equal to 10 + // for this to be an equal fork + testFork(t, bman, 1, 9, f) + testFork(t, bman, 2, 8, f) + testFork(t, bman, 5, 5, f) + testFork(t, bman, 6, 4, f) + testFork(t, bman, 9, 1, f) +} + +func TestBrokenChain(t *testing.T) { + db, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + bman, err := newCanonical(10, db) + if err != nil { + t.Fatal("Could not make new canonical chain:", err) + } + db2, err := ethdb.NewMemDatabase() + if err != nil { + t.Fatal("Failed to create db:", err) + } + bman2, err := newCanonical(10, db2) + if err != nil { + t.Fatal("Could not make new canonical chain:", err) + } + bman2.bc.SetProcessor(bman2) + parent := bman2.bc.CurrentBlock() + chainB := makeChain(bman2, parent, 5, db2) + chainB = chainB[1:] + _, err = testChain(chainB, bman) + if err == nil { + t.Error("expected broken chain to return error") + } +} + func TestChainInsertions(t *testing.T) { t.Skip() // travil fails. -- cgit v1.2.3 From 8653db6df0018d08212493e3a3df4677162bdd8f Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Fri, 27 Feb 2015 15:59:33 -0500 Subject: Introducign MixDigest and SeedHash --- core/types/block.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/types/block.go b/core/types/block.go index d57de1311..f637b5c3b 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -41,12 +41,16 @@ type Header struct { Extra string // Block Nonce for verification Nonce ethutil.Bytes + // Mix digest for quick checking to prevent DOS + MixDigest ethutil.Bytes + // SeedHash used for light client verification + SeedHash ethutil.Bytes } func (self *Header) rlpData(withNonce bool) []interface{} { fields := []interface{}{self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra} if withNonce { - fields = append(fields, self.Nonce) + fields = append(fields, self.Nonce, self.MixDigest, self.SeedHash) } return fields @@ -176,6 +180,8 @@ func (self *Block) RlpDataForStorage() interface{} { // Header accessors (add as you need them) func (self *Block) Number() *big.Int { return self.header.Number } func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() } +func (self *Block) MixDigest() []byte { return self.header.MixDigest } +func (self *Block) SeedHash() []byte { return self.header.SeedHash } func (self *Block) Nonce() []byte { return self.header.Nonce } func (self *Block) Bloom() []byte { return self.header.Bloom } func (self *Block) Coinbase() []byte { return self.header.Coinbase } @@ -200,7 +206,6 @@ func (self *Block) GetUncle(i int) *Header { // Implement pow.Block func (self *Block) Difficulty() *big.Int { return self.header.Difficulty } -func (self *Block) N() []byte { return self.header.Nonce } func (self *Block) HashNoNonce() []byte { return self.header.HashNoNonce() } func (self *Block) Hash() []byte { -- cgit v1.2.3 From 0efd6a881afac0b3082f3b1e8780e3438eea5b02 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 27 Feb 2015 16:05:03 -0500 Subject: public functions for making chains on the fly --- core/chain_makers.go | 131 +++++++++++++++++++++++++++++++++++++++++++++ core/chain_manager_test.go | 96 --------------------------------- 2 files changed, 131 insertions(+), 96 deletions(-) create mode 100644 core/chain_makers.go (limited to 'core') diff --git a/core/chain_makers.go b/core/chain_makers.go new file mode 100644 index 000000000..eb43f8aa0 --- /dev/null +++ b/core/chain_makers.go @@ -0,0 +1,131 @@ +package core + +import ( + "fmt" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/pow" + "github.com/ethereum/go-ethereum/state" + "math/big" +) + +// So we can generate blocks easily +type FakePow struct{} + +func (f FakePow) Search(block pow.Block, stop <-chan struct{}) []byte { return nil } +func (f FakePow) Verify(block pow.Block) bool { return true } +func (f FakePow) GetHashrate() int64 { return 0 } +func (f FakePow) Turbo(bool) {} + +func NewBlockFromParent(addr []byte, parent *types.Block) *types.Block { + return newBlockFromParent(addr, parent) +} + +func MakeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database) *types.Block { + return makeBlock(bman, parent, i, db) +} + +func MakeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database) types.Blocks { + return makeChain(bman, parent, max, db) +} + +func NewChainMan(block *types.Block, eventMux *event.TypeMux, db ethutil.Database) *ChainManager { + return newChainManager(block, eventMux, db) +} + +func NewBlockProc(db ethutil.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor { + return newBlockProcessor(db, txpool, cman, eventMux) +} + +func NewCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { + return newCanonical(n, db) +} + +func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { + block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), nil, "") + + block.SetUncles(nil) + block.SetTransactions(nil) + block.SetReceipts(nil) + + header := block.Header() + header.Difficulty = CalcDifficulty(block, parent) + header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1) + header.GasLimit = CalcGasLimit(parent, block) + + block.Td = parent.Td + + return block +} + +// Actually make a block by simulating what miner would do +func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database) *types.Block { + addr := ethutil.LeftPadBytes([]byte{byte(i)}, 20) + block := newBlockFromParent(addr, parent) + state := state.New(block.Root(), db) + cbase := state.GetOrNewStateObject(addr) + cbase.SetGasPool(CalcGasLimit(parent, block)) + cbase.AddBalance(BlockReward) + state.Update(ethutil.Big0) + block.SetRoot(state.Root()) + return block +} + +// Make a chain with real blocks +// Runs ProcessWithParent to get proper state roots +func makeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database) types.Blocks { + bman.bc.currentBlock = parent + blocks := make(types.Blocks, max) + for i := 0; i < max; i++ { + block := makeBlock(bman, parent, i, db) + td, err := bman.processWithParent(block, parent) + if err != nil { + fmt.Println("process with parent failed", err) + panic(err) + } + block.Td = td + blocks[i] = block + parent = block + fmt.Printf("New Block: %x\n", block.Hash()) + } + fmt.Println("Done making chain") + return blocks +} + +// Create a new chain manager starting from given block +// Effectively a fork factory +func newChainManager(block *types.Block, eventMux *event.TypeMux, db ethutil.Database) *ChainManager { + bc := &ChainManager{db: db, genesisBlock: GenesisBlock(db), eventMux: eventMux} + if block == nil { + bc.Reset() + } else { + bc.currentBlock = block + bc.td = block.Td + } + return bc +} + +// block processor with fake pow +func newBlockProcessor(db ethutil.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor { + bman := NewBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) + bman.Pow = FakePow{} + return bman +} + +// Make a new canonical chain by running InsertChain +// on result of makeChain +func newCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { + eventMux := &event.TypeMux{} + txpool := NewTxPool(eventMux) + + bman := newBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) + bman.bc.SetProcessor(bman) + parent := bman.bc.CurrentBlock() + if n == 0 { + return bman, nil + } + lchain := makeChain(bman, parent, n, db) + bman.bc.InsertChain(lchain) + return bman, nil +} diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index a357a8b1a..b76ac187c 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -19,107 +19,11 @@ import ( "github.com/ethereum/go-ethereum/state" ) -// So we can generate blocks easily -type fakePow struct{} - -func (f fakePow) Search(block pow.Block, stop <-chan struct{}) []byte { return nil } -func (f fakePow) Verify(block pow.Block) bool { return true } -func (f fakePow) GetHashrate() int64 { return 0 } -func (f fakePow) Turbo(bool) {} - func init() { runtime.GOMAXPROCS(runtime.NumCPU()) ethutil.ReadConfig("/tmp/ethtest", "/tmp/ethtest", "ETH") } -func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { - block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), nil, "") - - block.SetUncles(nil) - block.SetTransactions(nil) - block.SetReceipts(nil) - - header := block.Header() - header.Difficulty = CalcDifficulty(block, parent) - header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1) - header.GasLimit = CalcGasLimit(parent, block) - - block.Td = parent.Td - - return block -} - -// Actually make a block by simulating what miner would do -func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database) *types.Block { - addr := ethutil.LeftPadBytes([]byte{byte(i)}, 20) - block := newBlockFromParent(addr, parent) - state := state.New(block.Root(), db) - cbase := state.GetOrNewStateObject(addr) - cbase.SetGasPool(CalcGasLimit(parent, block)) - cbase.AddAmount(BlockReward) - state.Update(ethutil.Big0) - block.SetRoot(state.Root()) - return block -} - -// Make a chain with real blocks -// Runs ProcessWithParent to get proper state roots -func makeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database) types.Blocks { - bman.bc.currentBlock = parent - blocks := make(types.Blocks, max) - for i := 0; i < max; i++ { - block := makeBlock(bman, parent, i, db) - td, err := bman.processWithParent(block, parent) - if err != nil { - fmt.Println("process with parent failed", err) - panic(err) - } - block.Td = td - blocks[i] = block - parent = block - fmt.Printf("New Block: %x\n", block.Hash()) - } - fmt.Println("Done making chain") - return blocks -} - -// Create a new chain manager starting from given block -// Effectively a fork factory -func newChainManager(block *types.Block, eventMux *event.TypeMux, db ethutil.Database) *ChainManager { - bc := &ChainManager{db: db, genesisBlock: GenesisBlock(db), eventMux: eventMux} - if block == nil { - bc.Reset() - } else { - bc.currentBlock = block - bc.td = block.Td - } - return bc -} - -// block processor with fake pow -func newBlockProcessor(db ethutil.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor { - bman := NewBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) - bman.Pow = fakePow{} - return bman -} - -// Make a new canonical chain by running InsertChain -// on result of makeChain -func newCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { - eventMux := &event.TypeMux{} - txpool := NewTxPool(eventMux) - - bman := newBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) - bman.bc.SetProcessor(bman) - parent := bman.bc.CurrentBlock() - if n == 0 { - return bman, nil - } - lchain := makeChain(bman, parent, n, db) - bman.bc.InsertChain(lchain) - return bman, nil -} - // Test fork of length N starting from block i func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big.Int)) { fmt.Println("Testing Fork!") -- cgit v1.2.3 From ba1f4bbe9132103af67c0c46f81c913d6a595d77 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Fri, 27 Feb 2015 20:56:24 -0500 Subject: Exposing stuff for ethash --- core/block_processor.go | 3 +++ core/chain_manager_test.go | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index 7eaeb5be0..efad35bf4 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -105,6 +105,9 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated return receipt, txGas, err } +func (self *BlockProcessor) ChainManager() *ChainManager { + return self.bc +} func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, types.Transactions, types.Transactions, types.Transactions, error) { var ( diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index b76ac187c..e6614212f 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -14,9 +14,7 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" - "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/state" ) func init() { -- cgit v1.2.3 From 080823bdeebbab2bcffdaefad703896700ed2c30 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Fri, 27 Feb 2015 10:17:31 -0500 Subject: Only one uncle --- core/block_processor.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index efad35bf4..aef53c940 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -255,6 +255,10 @@ func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error { return fmt.Errorf("GasLimit check failed for block %v, %v", block.Header().GasLimit, expl) } + if len(block.Uncles()) > 1 { + return ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles())) + } + if block.Time() < parent.Time() { return ValidationError("Block timestamp not after prev block (%v - %v)", block.Header().Time, parent.Header().Time) } -- cgit v1.2.3 From de9f79133faa1ff5dcd16fb4fd13d06b7799ded9 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Sat, 28 Feb 2015 14:58:37 -0500 Subject: Introducing ethash --- core/block_processor.go | 5 +++-- core/chain_makers.go | 47 ++++++++++++++++++++++++++++------------------ core/chain_manager_test.go | 46 ++++++++++++++++++++++++++++++--------------- core/transaction_pool.go | 2 +- core/types/block.go | 15 ++++++++++++++- 5 files changed, 78 insertions(+), 37 deletions(-) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index aef53c940..3123511f9 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -7,12 +7,12 @@ import ( "sync" "time" + "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" - "github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/state" "gopkg.in/fatih/set.v0" ) @@ -50,7 +50,7 @@ func NewBlockProcessor(db ethutil.Database, txpool *TxPool, chainManager *ChainM sm := &BlockProcessor{ db: db, mem: make(map[string]*big.Int), - Pow: ezp.New(), + Pow: ethash.New(chainManager), bc: chainManager, eventMux: eventMux, txpool: txpool, @@ -255,6 +255,7 @@ func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error { return fmt.Errorf("GasLimit check failed for block %v, %v", block.Header().GasLimit, expl) } + // There can be at most one uncle if len(block.Uncles()) > 1 { return ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles())) } diff --git a/core/chain_makers.go b/core/chain_makers.go index eb43f8aa0..2c36b892e 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -13,21 +13,31 @@ import ( // So we can generate blocks easily type FakePow struct{} -func (f FakePow) Search(block pow.Block, stop <-chan struct{}) []byte { return nil } -func (f FakePow) Verify(block pow.Block) bool { return true } -func (f FakePow) GetHashrate() int64 { return 0 } -func (f FakePow) Turbo(bool) {} +func (f FakePow) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) { + return nil, nil, nil +} +func (f FakePow) Verify(block pow.Block) bool { return true } +func (f FakePow) GetHashrate() int64 { return 0 } +func (f FakePow) Turbo(bool) {} + +// So we can deterministically seed different blockchains +var ( + CanonicalSeed = 1 + ForkSeed = 2 +) +// Utility functions for making chains on the fly +// Exposed for sake of testing from other packages (eg. go-ethash) func NewBlockFromParent(addr []byte, parent *types.Block) *types.Block { return newBlockFromParent(addr, parent) } -func MakeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database) *types.Block { - return makeBlock(bman, parent, i, db) +func MakeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database, seed int) *types.Block { + return makeBlock(bman, parent, i, db, seed) } -func MakeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database) types.Blocks { - return makeChain(bman, parent, max, db) +func MakeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database, seed int) types.Blocks { + return makeChain(bman, parent, max, db, seed) } func NewChainMan(block *types.Block, eventMux *event.TypeMux, db ethutil.Database) *ChainManager { @@ -42,9 +52,9 @@ func NewCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { return newCanonical(n, db) } +// block time is fixed at 10 seconds func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), nil, "") - block.SetUncles(nil) block.SetTransactions(nil) block.SetReceipts(nil) @@ -52,6 +62,7 @@ func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { header := block.Header() header.Difficulty = CalcDifficulty(block, parent) header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1) + header.Time = parent.Header().Time + 10 header.GasLimit = CalcGasLimit(parent, block) block.Td = parent.Td @@ -60,8 +71,10 @@ func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { } // Actually make a block by simulating what miner would do -func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database) *types.Block { +// we seed chains by the first byte of the coinbase +func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Database, seed int) *types.Block { addr := ethutil.LeftPadBytes([]byte{byte(i)}, 20) + addr[0] = byte(seed) block := newBlockFromParent(addr, parent) state := state.New(block.Root(), db) cbase := state.GetOrNewStateObject(addr) @@ -74,11 +87,11 @@ func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db ethutil.Data // Make a chain with real blocks // Runs ProcessWithParent to get proper state roots -func makeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database) types.Blocks { +func makeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Database, seed int) types.Blocks { bman.bc.currentBlock = parent blocks := make(types.Blocks, max) for i := 0; i < max; i++ { - block := makeBlock(bman, parent, i, db) + block := makeBlock(bman, parent, i, db, seed) td, err := bman.processWithParent(block, parent) if err != nil { fmt.Println("process with parent failed", err) @@ -87,9 +100,7 @@ func makeChain(bman *BlockProcessor, parent *types.Block, max int, db ethutil.Da block.Td = td blocks[i] = block parent = block - fmt.Printf("New Block: %x\n", block.Hash()) } - fmt.Println("Done making chain") return blocks } @@ -113,7 +124,7 @@ func newBlockProcessor(db ethutil.Database, txpool *TxPool, cman *ChainManager, return bman } -// Make a new canonical chain by running InsertChain +// Make a new, deterministic canonical chain by running InsertChain // on result of makeChain func newCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { eventMux := &event.TypeMux{} @@ -125,7 +136,7 @@ func newCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { if n == 0 { return bman, nil } - lchain := makeChain(bman, parent, n, db) - bman.bc.InsertChain(lchain) - return bman, nil + lchain := makeChain(bman, parent, n, db, CanonicalSeed) + err := bman.bc.InsertChain(lchain) + return bman, err } diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index e6614212f..b562b677d 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -24,12 +24,6 @@ func init() { // Test fork of length N starting from block i func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big.Int)) { - fmt.Println("Testing Fork!") - var b *types.Block = nil - if i > 0 { - b = bman.bc.GetBlockByNumber(uint64(i)) - } - _ = b // switch databases to process the new chain db, err := ethdb.NewMemDatabase() if err != nil { @@ -40,13 +34,25 @@ func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big if err != nil { t.Fatal("could not make new canonical in testFork", err) } + // asert the bmans have the same block at i + bi1 := bman.bc.GetBlockByNumber(uint64(i)).Hash() + bi2 := bman2.bc.GetBlockByNumber(uint64(i)).Hash() + if bytes.Compare(bi1, bi2) != 0 { + t.Fatal("chains do not have the same hash at height", i) + } + bman2.bc.SetProcessor(bman2) + // extend the fork parent := bman2.bc.CurrentBlock() - chainB := makeChain(bman2, parent, N, db) - bman2.bc.InsertChain(chainB) + chainB := makeChain(bman2, parent, N, db, ForkSeed) + err = bman2.bc.InsertChain(chainB) + if err != nil { + t.Fatal("Insert chain error for fork:", err) + } tdpre := bman.bc.Td() + // Test the fork's blocks on the original chain td, err := testChain(chainB, bman) if err != nil { t.Fatal("expected chainB not to give errors:", err) @@ -55,6 +61,14 @@ func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big f(tdpre, td) } +func printChain(bc *ChainManager) { + for i := bc.CurrentBlock().Number().Uint64(); i > 0; i-- { + b := bc.GetBlockByNumber(uint64(i)) + fmt.Printf("\t%x\n", b.Hash()) + } +} + +// process blocks against a chain func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) { td := new(big.Int) for _, block := range chainB { @@ -102,12 +116,13 @@ func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t * } func TestExtendCanonical(t *testing.T) { + CanonicalLength := 5 db, err := ethdb.NewMemDatabase() if err != nil { t.Fatal("Failed to create db:", err) } // make first chain starting from genesis - bman, err := newCanonical(5, db) + bman, err := newCanonical(CanonicalLength, db) if err != nil { t.Fatal("Could not make new canonical chain:", err) } @@ -116,11 +131,11 @@ func TestExtendCanonical(t *testing.T) { t.Error("expected chainB to have higher difficulty. Got", td2, "expected more than", td1) } } - // Start fork from current height (5) - testFork(t, bman, 5, 1, f) - testFork(t, bman, 5, 2, f) - testFork(t, bman, 5, 5, f) - testFork(t, bman, 5, 10, f) + // Start fork from current height (CanonicalLength) + testFork(t, bman, CanonicalLength, 1, f) + testFork(t, bman, CanonicalLength, 2, f) + testFork(t, bman, CanonicalLength, 5, f) + testFork(t, bman, CanonicalLength, 10, f) } func TestShorterFork(t *testing.T) { @@ -189,6 +204,7 @@ func TestEqualFork(t *testing.T) { } // Sum of numbers must be equal to 10 // for this to be an equal fork + testFork(t, bman, 0, 10, f) testFork(t, bman, 1, 9, f) testFork(t, bman, 2, 8, f) testFork(t, bman, 5, 5, f) @@ -215,7 +231,7 @@ func TestBrokenChain(t *testing.T) { } bman2.bc.SetProcessor(bman2) parent := bman2.bc.CurrentBlock() - chainB := makeChain(bman2, parent, 5, db2) + chainB := makeChain(bman2, parent, 5, db2, ForkSeed) chainB = chainB[1:] _, err = testChain(chainB, bman) if err == nil { diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 860f57dc3..bd377f679 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -121,7 +121,7 @@ func (self *TxPool) add(tx *types.Transaction) error { if len(tx.From()) > 0 { from = ethutil.Bytes2Hex(tx.From()[:4]) } else { - from = "INVALID" + return errors.New(fmt.Sprintf("FROM ADDRESS MUST BE POSITIVE (was %v)", tx.From())) } txplogger.Debugf("(t) %x => %s (%v) %x\n", from, to, tx.Value, tx.Hash()) diff --git a/core/types/block.go b/core/types/block.go index f637b5c3b..a37038f73 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -48,7 +48,20 @@ type Header struct { } func (self *Header) rlpData(withNonce bool) []interface{} { - fields := []interface{}{self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra} + fields := []interface{}{ + self.ParentHash, + self.UncleHash, + self.Coinbase, + self.Root, + self.TxHash, + self.ReceiptHash, + self.Bloom, + self.Difficulty, + self.Number, + self.GasLimit, + self.GasUsed, + self.Time, + self.Extra} if withNonce { fields = append(fields, self.Nonce, self.MixDigest, self.SeedHash) } -- cgit v1.2.3