From c2af10d256f9fb29f86fc0c33abafeb62ebbe570 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 18:15:48 +0000 Subject: transaction pool changes - use eventer events to broadcast transactions - CurrentTransactions -> GetTransactions - add AddTransactions --- core/transaction_pool.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 7166d35e8..2eb0b55df 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -10,7 +10,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/wire" ) var txplogger = logger.NewLogger("TXP") @@ -18,7 +17,9 @@ var txplogger = logger.NewLogger("TXP") const txPoolQueueSize = 50 type TxPoolHook chan *types.Transaction -type TxMsgTy byte +type TxMsg struct { + Tx *types.Transaction +} const ( minGasPrice = 1000000 @@ -26,11 +27,6 @@ const ( var MinGasPrice = big.NewInt(10000000000000) -type TxMsg struct { - Tx *types.Transaction - Type TxMsgTy -} - func EachTx(pool *list.List, it func(*types.Transaction, *list.Element) bool) { for e := pool.Front(); e != nil; e = e.Next() { if it(e.Value.(*types.Transaction), e) { @@ -94,7 +90,7 @@ func (pool *TxPool) addTransaction(tx *types.Transaction) { pool.pool.PushBack(tx) // Broadcast the transaction to the rest of the peers - pool.Ethereum.Broadcast(wire.MsgTxTy, []interface{}{tx.RlpData()}) + pool.Ethereum.EventMux().Post(TxPreEvent{tx}) } func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { @@ -169,7 +165,17 @@ func (self *TxPool) Size() int { return self.pool.Len() } -func (pool *TxPool) CurrentTransactions() []*types.Transaction { +func (self *TxPool) AddTransactions(txs []*types.Transaction) { + for _, tx := range txs { + if err := self.Add(tx); err != nil { + txplogger.Infoln(err) + } else { + txplogger.Infof("tx %x\n", tx.Hash()[0:4]) + } + } +} + +func (pool *TxPool) GetTransactions() []*types.Transaction { pool.mutex.Lock() defer pool.mutex.Unlock() @@ -216,7 +222,7 @@ func (self *TxPool) RemoveSet(txs types.Transactions) { } func (pool *TxPool) Flush() []*types.Transaction { - txList := pool.CurrentTransactions() + txList := pool.GetTransactions() // Recreate a new list all together // XXX Is this the fastest way? -- cgit v1.2.3 From 5022a31889f87b95dd664cac2b049d5081e4af61 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 18:18:24 +0000 Subject: add NewMinedBlockEvent --- core/events.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'core') diff --git a/core/events.go b/core/events.go index deeba3e98..fe106da49 100644 --- a/core/events.go +++ b/core/events.go @@ -10,3 +10,6 @@ type TxPostEvent struct{ Tx *types.Transaction } // NewBlockEvent is posted when a block has been imported. type NewBlockEvent struct{ Block *types.Block } + +// NewMinedBlockEvent is posted when a block has been imported. +type NewMinedBlockEvent struct{ Block *types.Block } -- cgit v1.2.3 From 39d86a28e7d27bf1f6a2e2e298b52614ee0b9be3 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 18:19:29 +0000 Subject: adapt to new eth pkg and p2p --- core/block_manager.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index f6c73bc2c..cf47218ed 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -2,7 +2,6 @@ package core import ( "bytes" - "container/list" "errors" "fmt" "math/big" @@ -14,10 +13,10 @@ import ( "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/wire" ) var statelogger = logger.NewLogger("BLOCK") @@ -38,13 +37,12 @@ type EthManager interface { BlockManager() *BlockManager ChainManager() *ChainManager TxPool() *TxPool - Broadcast(msgType wire.MsgType, data []interface{}) PeerCount() int IsMining() bool IsListening() bool - Peers() *list.List + Peers() []*p2p.Peer KeyManager() *crypto.KeyManager - ClientIdentity() wire.ClientIdentity + ClientIdentity() p2p.ClientIdentity Db() ethutil.Database EventMux() *event.TypeMux } -- cgit v1.2.3 From d84810d89df128d9120a4562bb9e6334ba6a33d4 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 18:20:37 +0000 Subject: add Status to retrieve TD, currentBlock, genesis for easy interface with eth/protocol --- core/chain_manager.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index edf50e715..f9fb3b3f8 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -72,6 +72,10 @@ func NewChainManager(mux *event.TypeMux) *ChainManager { return bc } +func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) { + return self.TD, self.CurrentBlock.Hash(), self.Genesis().Hash() +} + func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { self.processor = proc } @@ -173,7 +177,7 @@ func (bc *ChainManager) HasBlock(hash []byte) bool { return len(data) != 0 } -func (self *ChainManager) GetChainHashesFromHash(hash []byte, max uint64) (chain [][]byte) { +func (self *ChainManager) GetBlockHashesFromHash(hash []byte, max uint64) (chain [][]byte) { block := self.GetBlock(hash) if block == nil { return -- cgit v1.2.3 From 7b39cc83cc5dcbcab6b2c35b81ea593628bbdb1f Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 19:30:32 +0000 Subject: adapt chain_manager to eth protocol interface - add Status() to return td, currentblock hash, genesis hash - GetChainHashesFromHash -> GetBlockHashesFromHash --- core/chain_manager.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index f9fb3b3f8..9ed2c1c42 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -137,6 +137,10 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { return block } +func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) { + return self.TD, self.CurrentBlock.Hash(), self.Genesis().Hash() +} + func (bc *ChainManager) Reset() { AddTestNetFunds(bc.genesisBlock) -- cgit v1.2.3 From 72290f67fee2be183981c9672d830040466187bd Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 20:57:29 +0000 Subject: resolve merge conflict hell --- core/chain_manager.go | 4 ---- 1 file changed, 4 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 9ed2c1c42..f9fb3b3f8 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -137,10 +137,6 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { return block } -func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) { - return self.TD, self.CurrentBlock.Hash(), self.Genesis().Hash() -} - func (bc *ChainManager) Reset() { AddTestNetFunds(bc.genesisBlock) -- cgit v1.2.3 From 4dbdcaecb117d7e1fcaf0869f5d4602312552991 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 17 Dec 2014 23:58:52 +0100 Subject: Moved pre-compiled, moved depth check * Depth check has been moved to the execution * Pre compiled execution has been moved to the VM * PrecompiledAddress has been renamed to PrecompiledAccount --- core/execution.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/execution.go b/core/execution.go index 58d46c509..827e1ee0e 100644 --- a/core/execution.go +++ b/core/execution.go @@ -4,7 +4,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) @@ -36,6 +35,11 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret env := self.vm.Env() chainlogger.Debugf("pre state %x\n", env.State().Root()) + if self.vm.Env().Depth() == vm.MaxCallDepth { + // Consume all gas (by not returning it) and return a depth error + return nil, vm.DepthError{} + } + from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address) // Skipping transfer is used on testing for the initial call if !self.SkipTransfer { @@ -50,24 +54,14 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret snapshot := env.State().Copy() defer func() { - if vm.IsDepthErr(err) || vm.IsOOGErr(err) { + if /*vm.IsDepthErr(err) ||*/ vm.IsOOGErr(err) { env.State().Set(snapshot) } chainlogger.Debugf("post state %x\n", env.State().Root()) }() self.object = to - // Pre-compiled contracts (address.go) 1, 2 & 3. - naddr := ethutil.BigD(contextAddr).Uint64() - if p := vm.Precompiled[naddr]; p != nil { - if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 { - ret = p.Call(self.input) - self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret) - self.vm.Endl() - } - } else { - ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) - } + ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) return } -- cgit v1.2.3 From 590aace88dce9922d40fca71e87905383a71d12b Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 12:18:19 +0100 Subject: Removed ethereum as dependency --- core/block_manager.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index f6c73bc2c..98c6d006d 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -58,8 +58,8 @@ type BlockManager struct { mem map[string]*big.Int // Proof of work used for validating Pow pow.PoW - // The ethereum manager interface - eth EthManager + + txpool *TxPool // The last attempted block is mainly used for debugging purposes // This does not have to be a valid block and will be set during @@ -71,13 +71,13 @@ type BlockManager struct { eventMux *event.TypeMux } -func NewBlockManager(ethereum EthManager) *BlockManager { +func NewBlockManager(txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockManager { sm := &BlockManager{ mem: make(map[string]*big.Int), Pow: ezp.New(), - eth: ethereum, - bc: ethereum.ChainManager(), - eventMux: ethereum.EventMux(), + bc: chainManager, + eventMux: eventMux, + txpool: txpool, } return sm @@ -240,7 +240,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I chainlogger.Infof("Processed block #%d (%x...)\n", block.Number, block.Hash()[0:4]) - sm.eth.TxPool().RemoveSet(block.Transactions()) + sm.txpool.RemoveSet(block.Transactions()) return td, messages, nil } else { -- cgit v1.2.3 From 49e0267fe76cfd13eaf3e5e26caa637b93dbdd29 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 13:12:54 +0100 Subject: Locks, refactor, tests * Added additional chain tests * Added proper mutex' on chain * Removed ethereum dependencies --- core/block_manager.go | 6 +-- core/chain_manager.go | 113 ++++++++++++++++++++++++++++++--------------- core/chain_manager_test.go | 66 ++++++++++++++++++++++++-- core/filter.go | 4 +- core/filter_test.go | 6 --- core/transaction_pool.go | 26 +++++++---- core/types/block.go | 2 +- core/types/common.go | 5 ++ 8 files changed, 165 insertions(+), 63 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index 98c6d006d..794c87f52 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -256,12 +256,12 @@ func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) { // TD(genesis_block) = 0 and TD(B) = TD(B.parent) + sum(u.difficulty for u in B.uncles) + B.difficulty td := new(big.Int) - td = td.Add(sm.bc.TD, uncleDiff) + td = td.Add(sm.bc.Td(), uncleDiff) td = td.Add(td, block.Difficulty) // The new TD will only be accepted if the new difficulty is // is greater than the previous. - if td.Cmp(sm.bc.TD) > 0 { + if td.Cmp(sm.bc.Td()) > 0 { return td, true } @@ -279,7 +279,7 @@ func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error { diff := block.Time - parent.Time if diff < 0 { - return ValidationError("Block timestamp less then prev block %v (%v - %v)", diff, block.Time, sm.bc.CurrentBlock.Time) + return ValidationError("Block timestamp less then prev block %v (%v - %v)", diff, block.Time, sm.bc.CurrentBlock().Time) } /* XXX diff --git a/core/chain_manager.go b/core/chain_manager.go index 3e48579b9..c81552b5d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -3,6 +3,7 @@ package core import ( "fmt" "math/big" + "sync" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" @@ -50,14 +51,34 @@ type ChainManager struct { eventMux *event.TypeMux genesisBlock *types.Block // Last known total difficulty - TD *big.Int + mu sync.RWMutex + td *big.Int + lastBlockNumber uint64 + currentBlock *types.Block + lastBlockHash []byte - LastBlockNumber uint64 + transState *state.StateDB +} - CurrentBlock *types.Block - LastBlockHash []byte +func (self *ChainManager) Td() *big.Int { + self.mu.RLock() + defer self.mu.RUnlock() - transState *state.StateDB + return self.td +} + +func (self *ChainManager) LastBlockNumber() uint64 { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.lastBlockNumber +} + +func (self *ChainManager) CurrentBlock() *types.Block { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.currentBlock } func NewChainManager(mux *event.TypeMux) *ChainManager { @@ -77,7 +98,7 @@ func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { } func (self *ChainManager) State() *state.StateDB { - return self.CurrentBlock.State() + return self.CurrentBlock().State() } func (self *ChainManager) TransState() *state.StateDB { @@ -91,27 +112,30 @@ func (bc *ChainManager) setLastBlock() { AddTestNetFunds(bc.genesisBlock) block := types.NewBlockFromBytes(data) - bc.CurrentBlock = block - bc.LastBlockHash = block.Hash() - bc.LastBlockNumber = block.Number.Uint64() + bc.currentBlock = block + bc.lastBlockHash = block.Hash() + bc.lastBlockNumber = block.Number.Uint64() // Set the last know difficulty (might be 0x0 as initial value, Genesis) - bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) + bc.td = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) } else { bc.Reset() } - chainlogger.Infof("Last block (#%d) %x\n", bc.LastBlockNumber, bc.CurrentBlock.Hash()) + chainlogger.Infof("Last block (#%d) %x\n", bc.lastBlockNumber, bc.currentBlock.Hash()) } // Block creation & chain handling func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { + bc.mu.RLock() + defer bc.mu.RUnlock() + var root interface{} hash := ZeroHash256 if bc.CurrentBlock != nil { - root = bc.CurrentBlock.Root() - hash = bc.LastBlockHash + root = bc.currentBlock.Root() + hash = bc.lastBlockHash } block := types.CreateBlock( @@ -122,11 +146,11 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { nil, "") - parent := bc.CurrentBlock + parent := bc.currentBlock if parent != nil { block.Difficulty = CalcDifficulty(block, parent) - block.Number = new(big.Int).Add(bc.CurrentBlock.Number, ethutil.Big1) - block.GasLimit = block.CalcGasLimit(bc.CurrentBlock) + block.Number = new(big.Int).Add(bc.currentBlock.Number, ethutil.Big1) + block.GasLimit = block.CalcGasLimit(bc.currentBlock) } @@ -134,35 +158,42 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { } func (bc *ChainManager) Reset() { + bc.mu.Lock() + defer bc.mu.Unlock() + AddTestNetFunds(bc.genesisBlock) bc.genesisBlock.Trie().Sync() // Prepare the genesis block bc.write(bc.genesisBlock) bc.insert(bc.genesisBlock) - bc.CurrentBlock = bc.genesisBlock + bc.currentBlock = bc.genesisBlock - bc.SetTotalDifficulty(ethutil.Big("0")) + bc.setTotalDifficulty(ethutil.Big("0")) // Set the last know difficulty (might be 0x0 as initial value, Genesis) - bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) + bc.td = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) } func (self *ChainManager) Export() []byte { - chainlogger.Infoln("exporting", self.CurrentBlock.Number, "blocks") + self.mu.RLock() + defer self.mu.RUnlock() + + chainlogger.Infof("exporting %v blocks...\n", self.currentBlock.Number) - blocks := make(types.Blocks, int(self.CurrentBlock.Number.Int64())+1) - for block := self.CurrentBlock; block != nil; block = self.GetBlock(block.PrevHash) { + blocks := make([]*types.Block, int(self.currentBlock.Number.Int64())+1) + for block := self.currentBlock; block != nil; block = self.GetBlock(block.PrevHash) { blocks[block.Number.Int64()] = block } + return ethutil.Encode(blocks) } func (bc *ChainManager) insert(block *types.Block) { encodedBlock := block.RlpEncode() ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock) - bc.CurrentBlock = block - bc.LastBlockHash = block.Hash() + bc.currentBlock = block + bc.lastBlockHash = block.Hash() } func (bc *ChainManager) write(block *types.Block) { @@ -213,7 +244,10 @@ func (self *ChainManager) GetBlock(hash []byte) *types.Block { } func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block { - block := self.CurrentBlock + self.mu.RLock() + defer self.mu.RUnlock() + + block := self.currentBlock for ; block != nil; block = self.GetBlock(block.PrevHash) { if block.Number.Uint64() == num { break @@ -227,9 +261,9 @@ func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block { return block } -func (bc *ChainManager) SetTotalDifficulty(td *big.Int) { +func (bc *ChainManager) setTotalDifficulty(td *big.Int) { ethutil.Config.Db.Put([]byte("LTD"), td.Bytes()) - bc.TD = td + bc.td = td } func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { @@ -262,8 +296,8 @@ func (bc *ChainManager) BlockInfo(block *types.Block) types.BlockInfo { // Unexported method for writing extra non-essential block info to the db func (bc *ChainManager) writeBlockInfo(block *types.Block) { - bc.LastBlockNumber++ - bi := types.BlockInfo{Number: bc.LastBlockNumber, Hash: block.Hash(), Parent: block.PrevHash, TD: bc.TD} + bc.lastBlockNumber++ + bi := types.BlockInfo{Number: bc.lastBlockNumber, Hash: block.Hash(), Parent: block.PrevHash, TD: bc.td} // For now we use the block hash with the words "info" appended as key ethutil.Config.Db.Put(append(block.Hash(), []byte("Info")...), bi.RlpEncode()) @@ -289,17 +323,22 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { return err } - self.write(block) - if td.Cmp(self.TD) > 0 { - if block.Number.Cmp(new(big.Int).Add(self.CurrentBlock.Number, ethutil.Big1)) < 0 { - chainlogger.Infof("Split detected. New head #%v (%x), was #%v (%x)\n", block.Number, block.Hash()[:4], self.CurrentBlock.Number, self.CurrentBlock.Hash()[:4]) + self.mu.Lock() + { + + self.write(block) + if td.Cmp(self.td) > 0 { + if block.Number.Cmp(new(big.Int).Add(self.currentBlock.Number, ethutil.Big1)) < 0 { + chainlogger.Infof("Split detected. New head #%v (%x), was #%v (%x)\n", block.Number, block.Hash()[:4], self.currentBlock.Number, self.currentBlock.Hash()[:4]) + } + + self.setTotalDifficulty(td) + self.insert(block) + self.transState = self.currentBlock.State().Copy() } - self.SetTotalDifficulty(td) - self.insert(block) - self.transState = self.State().Copy() - //sm.eth.TxPool().RemoveSet(block.Transactions()) } + self.mu.Unlock() self.eventMux.Post(NewBlockEvent{block}) self.eventMux.Post(messages) diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index a84e3ff3b..52be8b0ea 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -3,17 +3,75 @@ package core import ( "fmt" "path" + "runtime" "testing" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/event" + //logpkg "github.com/ethereum/go-ethereum/logger" ) -func TestChainInsertions(t *testing.T) { - c1, err := ethutil.ReadAllFile(path.Join("..", "_data", "chain1")) +//var Logger logpkg.LogSystem +//var Log = logpkg.NewLogger("TEST") + +func init() { + runtime.GOMAXPROCS(runtime.NumCPU()) + //Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.InfoLevel) + //logpkg.AddLogSystem(Logger) + + ethutil.ReadConfig("/tmp/ethtest", "/tmp/ethtest", "ETH") + + db, err := ethdb.NewMemDatabase() + if err != nil { + panic("Could not create mem-db, failing") + } + ethutil.Config.Db = db +} + +func loadChain(fn string, t *testing.T) types.Blocks { + c1, err := ethutil.ReadAllFile(path.Join("..", "_data", fn)) if err != nil { fmt.Println(err) t.FailNow() } - data1, _ := ethutil.Decode([]byte(c1), 0) - fmt.Println(data1) + value := ethutil.NewValueFromBytes([]byte(c1)) + blocks := make(types.Blocks, value.Len()) + it := value.NewIterator() + for it.Next() { + blocks[it.Idx()] = types.NewBlockFromRlpValue(it.Value()) + } + + return blocks +} + +func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *testing.T) { + err := chainMan.InsertChain(chain) + if err != nil { + fmt.Println(err) + t.FailNow() + } + done <- true +} + +func TestChainInsertions(t *testing.T) { + chain1 := loadChain("chain1", t) + chain2 := loadChain("chain2", t) + var eventMux event.TypeMux + chainMan := NewChainManager(&eventMux) + txPool := NewTxPool(chainMan, nil, &eventMux) + blockMan := NewBlockManager(txPool, chainMan, &eventMux) + chainMan.SetProcessor(blockMan) + + const max = 2 + done := make(chan bool, max) + + go insertChain(done, chainMan, chain1, t) + go insertChain(done, chainMan, chain2, t) + + for i := 0; i < max; i++ { + <-done + } + fmt.Println(chainMan.CurrentBlock()) } diff --git a/core/filter.go b/core/filter.go index fe3665bf3..fb992782d 100644 --- a/core/filter.go +++ b/core/filter.go @@ -78,11 +78,11 @@ func (self *Filter) SetSkip(skip int) { func (self *Filter) Find() []*state.Message { var earliestBlockNo uint64 = uint64(self.earliest) if self.earliest == -1 { - earliestBlockNo = self.eth.ChainManager().CurrentBlock.Number.Uint64() + earliestBlockNo = self.eth.ChainManager().CurrentBlock().Number.Uint64() } var latestBlockNo uint64 = uint64(self.latest) if self.latest == -1 { - latestBlockNo = self.eth.ChainManager().CurrentBlock.Number.Uint64() + latestBlockNo = self.eth.ChainManager().CurrentBlock().Number.Uint64() } var ( diff --git a/core/filter_test.go b/core/filter_test.go index d53b835b7..9a8bc9592 100644 --- a/core/filter_test.go +++ b/core/filter_test.go @@ -1,7 +1 @@ package core - -// import "testing" - -// func TestFilter(t *testing.T) { -// NewFilter(NewTestManager()) -// } diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 7166d35e8..36b0beb28 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -8,6 +8,7 @@ import ( "sync" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/wire" @@ -61,7 +62,6 @@ type TxProcessor interface { // pool is being drained or synced for whatever reason the transactions // will simple queue up and handled when the mutex is freed. type TxPool struct { - Ethereum EthManager // The mutex for accessing the Tx pool. mutex sync.Mutex // Queueing channel for reading and writing incoming @@ -75,14 +75,20 @@ type TxPool struct { SecondaryProcessor TxProcessor subscribers []chan TxMsg + + broadcaster types.Broadcaster + chainManager *ChainManager + eventMux *event.TypeMux } -func NewTxPool(ethereum EthManager) *TxPool { +func NewTxPool(chainManager *ChainManager, broadcaster types.Broadcaster, eventMux *event.TypeMux) *TxPool { return &TxPool{ - pool: list.New(), - queueChan: make(chan *types.Transaction, txPoolQueueSize), - quit: make(chan bool), - Ethereum: ethereum, + pool: list.New(), + queueChan: make(chan *types.Transaction, txPoolQueueSize), + quit: make(chan bool), + chainManager: chainManager, + eventMux: eventMux, + broadcaster: broadcaster, } } @@ -94,13 +100,13 @@ func (pool *TxPool) addTransaction(tx *types.Transaction) { pool.pool.PushBack(tx) // Broadcast the transaction to the rest of the peers - pool.Ethereum.Broadcast(wire.MsgTxTy, []interface{}{tx.RlpData()}) + pool.broadcaster.Broadcast(wire.MsgTxTy, []interface{}{tx.RlpData()}) } func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { // Get the last block so we can retrieve the sender and receiver from // the merkle trie - block := pool.Ethereum.ChainManager().CurrentBlock + block := pool.chainManager.CurrentBlock // Something has gone horribly wrong if this happens if block == nil { return fmt.Errorf("No last block on the block chain") @@ -116,7 +122,7 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { } // Get the sender - sender := pool.Ethereum.ChainManager().State().GetAccount(tx.Sender()) + sender := pool.chainManager.State().GetAccount(tx.Sender()) totAmount := new(big.Int).Set(tx.Value) // Make sure there's enough in the sender's account. Having insufficient @@ -160,7 +166,7 @@ func (self *TxPool) Add(tx *types.Transaction) error { txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.Sender()[:4], tmp, tx.Value, tx.Hash()) // Notify the subscribers - go self.Ethereum.EventMux().Post(TxPreEvent{tx}) + go self.eventMux.Post(TxPreEvent{tx}) return nil } diff --git a/core/types/block.go b/core/types/block.go index 0108bd586..2d889f35f 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -409,7 +409,7 @@ func (self *Block) Size() ethutil.StorageSize { // Implement RlpEncodable func (self *Block) RlpData() interface{} { - return self.Value().Val + return []interface{}{self.header(), self.transactions, self.rlpUncles()} } // Implement pow.Block diff --git a/core/types/common.go b/core/types/common.go index ba88b77e1..89cb5f498 100644 --- a/core/types/common.go +++ b/core/types/common.go @@ -4,8 +4,13 @@ import ( "math/big" "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/wire" ) type BlockProcessor interface { Process(*Block) (*big.Int, state.Messages, error) } + +type Broadcaster interface { + Broadcast(wire.MsgType, []interface{}) +} -- cgit v1.2.3 From 2d09e67713757e2a80eb614562c97f962af36cf7 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 13:17:24 +0100 Subject: Updated to new methods --- core/chain_manager.go | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index c81552b5d..794ae0011 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -74,6 +74,13 @@ func (self *ChainManager) LastBlockNumber() uint64 { return self.lastBlockNumber } +func (self *ChainManager) LastBlockHash() []byte { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.lastBlockHash +} + func (self *ChainManager) CurrentBlock() *types.Block { self.mu.RLock() defer self.mu.RUnlock() -- cgit v1.2.3 From 0dc566124aee24cf3f01b4e586bde7752da89824 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 13:22:59 +0100 Subject: Merge fixes --- core/chain_manager.go | 5 ++++- core/transaction_pool.go | 6 ++---- core/types/common.go | 5 ----- 3 files changed, 6 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 4bbecd109..e6268c01e 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -101,7 +101,10 @@ func NewChainManager(mux *event.TypeMux) *ChainManager { } func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) { - return self.TD, self.CurrentBlock.Hash(), self.Genesis().Hash() + self.mu.RLock() + defer self.mu.RUnlock() + + return self.td, self.currentBlock.Hash(), self.Genesis().Hash() } func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 86286f23d..2bbda7d90 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -72,19 +72,17 @@ type TxPool struct { subscribers []chan TxMsg - broadcaster types.Broadcaster chainManager *ChainManager eventMux *event.TypeMux } -func NewTxPool(chainManager *ChainManager, broadcaster types.Broadcaster, eventMux *event.TypeMux) *TxPool { +func NewTxPool(chainManager *ChainManager, eventMux *event.TypeMux) *TxPool { return &TxPool{ pool: list.New(), queueChan: make(chan *types.Transaction, txPoolQueueSize), quit: make(chan bool), chainManager: chainManager, eventMux: eventMux, - broadcaster: broadcaster, } } @@ -96,7 +94,7 @@ func (pool *TxPool) addTransaction(tx *types.Transaction) { pool.pool.PushBack(tx) // Broadcast the transaction to the rest of the peers - pool.Ethereum.EventMux().Post(TxPreEvent{tx}) + pool.eventMux.Post(TxPreEvent{tx}) } func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { diff --git a/core/types/common.go b/core/types/common.go index 89cb5f498..ba88b77e1 100644 --- a/core/types/common.go +++ b/core/types/common.go @@ -4,13 +4,8 @@ import ( "math/big" "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/wire" ) type BlockProcessor interface { Process(*Block) (*big.Int, state.Messages, error) } - -type Broadcaster interface { - Broadcast(wire.MsgType, []interface{}) -} -- cgit v1.2.3 From db494170dc819b1eb0d267b6e1ab36c6cfb63569 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 15:18:13 +0100 Subject: Created generic message (easy for testing) --- core/block_manager.go | 4 +- core/state_transition.go | 98 +++++++++++++++++++++++++++++++---------------- core/transaction_pool.go | 17 ++++---- core/types/transaction.go | 88 +++++++++++++++++++++++++++--------------- core/vm_env.go | 10 ++--- 5 files changed, 137 insertions(+), 80 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index 794c87f52..aa845a007 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -111,7 +111,7 @@ done: // If we are mining this block and validating we want to set the logs back to 0 state.EmptyLogs() - txGas := new(big.Int).Set(tx.Gas) + txGas := new(big.Int).Set(tx.Gas()) cb := state.GetStateObject(coinbase.Address()) st := NewStateTransition(cb, tx, state, block) @@ -134,7 +134,7 @@ done: } txGas.Sub(txGas, st.gas) - cumulativeSum.Add(cumulativeSum, new(big.Int).Mul(txGas, tx.GasPrice)) + cumulativeSum.Add(cumulativeSum, new(big.Int).Mul(txGas, tx.GasPrice())) // Update the state with pending changes state.Update(txGas) diff --git a/core/state_transition.go b/core/state_transition.go index 820ba66e6..a6b654842 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -5,6 +5,8 @@ import ( "math/big" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) @@ -27,7 +29,7 @@ import ( */ type StateTransition struct { coinbase, receiver []byte - tx *types.Transaction + msg Message gas, gasPrice *big.Int value *big.Int data []byte @@ -35,10 +37,42 @@ type StateTransition struct { block *types.Block cb, rec, sen *state.StateObject + + Env vm.Environment +} + +type Message interface { + Hash() []byte + + CreatesContract() bool + + From() []byte + To() []byte + + GasValue() *big.Int + GasPrice() *big.Int + Gas() *big.Int + Value() *big.Int + + Nonce() uint64 + Data() []byte } -func NewStateTransition(coinbase *state.StateObject, tx *types.Transaction, state *state.StateDB, block *types.Block) *StateTransition { - return &StateTransition{coinbase.Address(), tx.Recipient, tx, new(big.Int), new(big.Int).Set(tx.GasPrice), tx.Value, tx.Data, state, block, coinbase, nil, nil} +func AddressFromMessage(msg Message) []byte { + // Generate a new address + return crypto.Sha3(ethutil.NewValue([]interface{}{msg.From(), msg.Nonce()}).Encode())[12:] +} + +func NewStateTransition(coinbase *state.StateObject, msg Message, state *state.StateDB, block *types.Block) *StateTransition { + return &StateTransition{coinbase.Address(), msg.To(), msg, new(big.Int), new(big.Int).Set(msg.GasPrice()), msg.Value(), msg.Data(), state, block, coinbase, nil, nil, nil} +} + +func (self *StateTransition) VmEnv() vm.Environment { + if self.Env == nil { + self.Env = NewEnv(self.state, self.msg, self.block) + } + + return self.Env } func (self *StateTransition) Coinbase() *state.StateObject { @@ -49,17 +83,17 @@ func (self *StateTransition) Coinbase() *state.StateObject { self.cb = self.state.GetOrNewStateObject(self.coinbase) return self.cb } -func (self *StateTransition) Sender() *state.StateObject { +func (self *StateTransition) From() *state.StateObject { if self.sen != nil { return self.sen } - self.sen = self.state.GetOrNewStateObject(self.tx.Sender()) + self.sen = self.state.GetOrNewStateObject(self.msg.From()) return self.sen } -func (self *StateTransition) Receiver() *state.StateObject { - if self.tx != nil && self.tx.CreatesContract() { +func (self *StateTransition) To() *state.StateObject { + if self.msg != nil && self.msg.CreatesContract() { return nil } @@ -67,7 +101,7 @@ func (self *StateTransition) Receiver() *state.StateObject { return self.rec } - self.rec = self.state.GetOrNewStateObject(self.tx.Recipient) + self.rec = self.state.GetOrNewStateObject(self.msg.To()) return self.rec } @@ -87,41 +121,41 @@ func (self *StateTransition) AddGas(amount *big.Int) { func (self *StateTransition) BuyGas() error { var err error - sender := self.Sender() - if sender.Balance().Cmp(self.tx.GasValue()) < 0 { - return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance()) + sender := self.From() + if sender.Balance().Cmp(self.msg.GasValue()) < 0 { + return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.msg.GasValue(), sender.Balance()) } coinbase := self.Coinbase() - err = coinbase.BuyGas(self.tx.Gas, self.tx.GasPrice) + err = coinbase.BuyGas(self.msg.Gas(), self.msg.GasPrice()) if err != nil { return err } - self.AddGas(self.tx.Gas) - sender.SubAmount(self.tx.GasValue()) + self.AddGas(self.msg.Gas()) + sender.SubAmount(self.msg.GasValue()) return nil } func (self *StateTransition) RefundGas() { - coinbase, sender := self.Coinbase(), self.Sender() - coinbase.RefundGas(self.gas, self.tx.GasPrice) + coinbase, sender := self.Coinbase(), self.From() + coinbase.RefundGas(self.gas, self.msg.GasPrice()) // Return remaining gas - remaining := new(big.Int).Mul(self.gas, self.tx.GasPrice) + remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice()) sender.AddAmount(remaining) } func (self *StateTransition) preCheck() (err error) { var ( - tx = self.tx - sender = self.Sender() + msg = self.msg + sender = self.From() ) // Make sure this transaction's nonce is correct - if sender.Nonce != tx.Nonce { - return NonceError(tx.Nonce, sender.Nonce) + if sender.Nonce != msg.Nonce() { + return NonceError(msg.Nonce(), sender.Nonce) } // Pre-pay gas / Buy gas of the coinbase account @@ -133,7 +167,7 @@ func (self *StateTransition) preCheck() (err error) { } func (self *StateTransition) TransitionState() (err error) { - statelogger.Debugf("(~) %x\n", self.tx.Hash()) + statelogger.Debugf("(~) %x\n", self.msg.Hash()) // XXX Transactions after this point are considered valid. if err = self.preCheck(); err != nil { @@ -141,8 +175,8 @@ func (self *StateTransition) TransitionState() (err error) { } var ( - tx = self.tx - sender = self.Sender() + msg = self.msg + sender = self.From() ) defer self.RefundGas() @@ -169,15 +203,15 @@ func (self *StateTransition) TransitionState() (err error) { } var ret []byte - vmenv := NewEnv(self.state, self.tx, self.block) + vmenv := self.VmEnv() var ref vm.ClosureRef - if tx.CreatesContract() { - self.rec = MakeContract(tx, self.state) + if msg.CreatesContract() { + self.rec = MakeContract(msg, self.state) - ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.tx.Data, self.gas, self.gasPrice, self.value) + ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) ref.SetCode(ret) } else { - ret, err = vmenv.Call(self.Sender(), self.Receiver().Address(), self.tx.Data, self.gas, self.gasPrice, self.value) + ret, err = vmenv.Call(self.From(), self.To().Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) } if err != nil { statelogger.Debugln(err) @@ -187,11 +221,11 @@ func (self *StateTransition) TransitionState() (err error) { } // Converts an transaction in to a state object -func MakeContract(tx *types.Transaction, state *state.StateDB) *state.StateObject { - addr := tx.CreationAddress(state) +func MakeContract(msg Message, state *state.StateDB) *state.StateObject { + addr := AddressFromMessage(msg) contract := state.GetOrNewStateObject(addr) - contract.InitCode = tx.Data + contract.InitCode = msg.Data() return contract } diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 36b0beb28..da91ec568 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -112,8 +112,8 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { return fmt.Errorf("No last block on the block chain") } - if len(tx.Recipient) != 0 && len(tx.Recipient) != 20 { - return fmt.Errorf("Invalid recipient. len = %d", len(tx.Recipient)) + if len(tx.To()) != 0 && len(tx.To()) != 20 { + return fmt.Errorf("Invalid recipient. len = %d", len(tx.To())) } v, _, _ := tx.Curve() @@ -124,15 +124,15 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { // Get the sender sender := pool.chainManager.State().GetAccount(tx.Sender()) - totAmount := new(big.Int).Set(tx.Value) + totAmount := new(big.Int).Set(tx.Value()) // Make sure there's enough in the sender's account. Having insufficient // funds won't invalidate this transaction but simple ignores it. if sender.Balance().Cmp(totAmount) < 0 { - return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.Sender()) + return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From()) } if tx.IsContract() { - if tx.GasPrice.Cmp(big.NewInt(minGasPrice)) < 0 { + if tx.GasPrice().Cmp(big.NewInt(minGasPrice)) < 0 { return fmt.Errorf("Gasprice too low, %s given should be at least %d.", tx.GasPrice, minGasPrice) } } @@ -160,10 +160,7 @@ func (self *TxPool) Add(tx *types.Transaction) error { self.addTransaction(tx) - tmp := make([]byte, 4) - copy(tmp, tx.Recipient) - - txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.Sender()[:4], tmp, tx.Value, tx.Hash()) + txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.From()[:4], tx.To()[:4], tx.Value, tx.Hash()) // Notify the subscribers go self.eventMux.Post(TxPreEvent{tx}) @@ -200,7 +197,7 @@ func (pool *TxPool) RemoveInvalid(state *state.StateDB) { tx := e.Value.(*types.Transaction) sender := state.GetAccount(tx.Sender()) err := pool.ValidateTransaction(tx) - if err != nil || sender.Nonce >= tx.Nonce { + if err != nil || sender.Nonce >= tx.Nonce() { pool.pool.Remove(e) } } diff --git a/core/types/transaction.go b/core/types/transaction.go index 63edef756..4b75d3abd 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -6,7 +6,6 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/state" "github.com/obscuren/secp256k1-go" ) @@ -18,12 +17,12 @@ func IsContractAddr(addr []byte) bool { } type Transaction struct { - Nonce uint64 - Recipient []byte - Value *big.Int - Gas *big.Int - GasPrice *big.Int - Data []byte + nonce uint64 + recipient []byte + value *big.Int + gas *big.Int + gasPrice *big.Int + data []byte v byte r, s []byte @@ -32,11 +31,11 @@ type Transaction struct { } func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction { - return &Transaction{Recipient: nil, Value: value, Gas: gas, GasPrice: gasPrice, Data: script, contractCreation: true} + return &Transaction{recipient: nil, value: value, gas: gas, gasPrice: gasPrice, data: script, contractCreation: true} } func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction { - return &Transaction{Recipient: to, Value: value, GasPrice: gasPrice, Gas: gas, Data: data, contractCreation: IsContractAddr(to)} + return &Transaction{recipient: to, value: value, gasPrice: gasPrice, gas: gas, data: data, contractCreation: IsContractAddr(to)} } func NewTransactionFromBytes(data []byte) *Transaction { @@ -54,20 +53,52 @@ func NewTransactionFromValue(val *ethutil.Value) *Transaction { } func (self *Transaction) GasValue() *big.Int { - return new(big.Int).Mul(self.Gas, self.GasPrice) + return new(big.Int).Mul(self.gas, self.gasPrice) } func (self *Transaction) TotalValue() *big.Int { v := self.GasValue() - return v.Add(v, self.Value) + return v.Add(v, self.value) } func (tx *Transaction) Hash() []byte { - data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data} + data := []interface{}{tx.Nonce, tx.gasPrice, tx.gas, tx.recipient, tx.Value, tx.Data} return crypto.Sha3(ethutil.NewValue(data).Encode()) } +func (self *Transaction) Data() []byte { + return self.data +} + +func (self *Transaction) Gas() *big.Int { + return self.gas +} + +func (self *Transaction) GasPrice() *big.Int { + return self.gasPrice +} + +func (self *Transaction) Value() *big.Int { + return self.value +} + +func (self *Transaction) Nonce() uint64 { + return self.nonce +} + +func (self *Transaction) SetNonce(nonce uint64) { + self.nonce = nonce +} + +func (self *Transaction) From() []byte { + return self.Sender() +} + +func (self *Transaction) To() []byte { + return self.recipient +} + func (tx *Transaction) CreatesContract() bool { return tx.contractCreation } @@ -77,11 +108,6 @@ func (tx *Transaction) IsContract() bool { return tx.CreatesContract() } -func (tx *Transaction) CreationAddress(state *state.StateDB) []byte { - // Generate a new address - return crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:] -} - func (tx *Transaction) Curve() (v byte, r []byte, s []byte) { v = tx.v r = ethutil.LeftPadBytes(tx.r, 32) @@ -136,7 +162,7 @@ func (tx *Transaction) Sign(privk []byte) error { } func (tx *Transaction) RlpData() interface{} { - data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data} + data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.recipient, tx.Value, tx.Data} // TODO Remove prefixing zero's @@ -156,18 +182,18 @@ func (tx *Transaction) RlpDecode(data []byte) { } func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) { - tx.Nonce = decoder.Get(0).Uint() - tx.GasPrice = decoder.Get(1).BigInt() - tx.Gas = decoder.Get(2).BigInt() - tx.Recipient = decoder.Get(3).Bytes() - tx.Value = decoder.Get(4).BigInt() - tx.Data = decoder.Get(5).Bytes() + tx.nonce = decoder.Get(0).Uint() + tx.gasPrice = decoder.Get(1).BigInt() + tx.gas = decoder.Get(2).BigInt() + tx.recipient = decoder.Get(3).Bytes() + tx.value = decoder.Get(4).BigInt() + tx.data = decoder.Get(5).Bytes() tx.v = byte(decoder.Get(6).Uint()) tx.r = decoder.Get(7).Bytes() tx.s = decoder.Get(8).Bytes() - if IsContractAddr(tx.Recipient) { + if IsContractAddr(tx.recipient) { tx.contractCreation = true } } @@ -188,12 +214,12 @@ func (tx *Transaction) String() string { S: 0x%x `, tx.Hash(), - len(tx.Recipient) == 0, + len(tx.recipient) == 0, tx.Sender(), - tx.Recipient, - tx.Nonce, - tx.GasPrice, - tx.Gas, + tx.recipient, + tx.nonce, + tx.gasPrice, + tx.gas, tx.Value, tx.Data, tx.v, @@ -221,5 +247,5 @@ func (s Transactions) GetRlp(i int) []byte { return ethutil.Rlp(s[i]) } type TxByNonce struct{ Transactions } func (s TxByNonce) Less(i, j int) bool { - return s.Transactions[i].Nonce < s.Transactions[j].Nonce + return s.Transactions[i].nonce < s.Transactions[j].nonce } diff --git a/core/vm_env.go b/core/vm_env.go index 9e1815188..0b6744972 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -11,26 +11,26 @@ import ( type VMEnv struct { state *state.StateDB block *types.Block - tx *types.Transaction + msg Message depth int } -func NewEnv(state *state.StateDB, tx *types.Transaction, block *types.Block) *VMEnv { +func NewEnv(state *state.StateDB, msg Message, block *types.Block) *VMEnv { return &VMEnv{ state: state, block: block, - tx: tx, + msg: msg, } } -func (self *VMEnv) Origin() []byte { return self.tx.Sender() } +func (self *VMEnv) Origin() []byte { return self.msg.From() } func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number } func (self *VMEnv) PrevHash() []byte { return self.block.PrevHash } func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase } func (self *VMEnv) Time() int64 { return self.block.Time } func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty } func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } -func (self *VMEnv) Value() *big.Int { return self.tx.Value } +func (self *VMEnv) Value() *big.Int { return self.msg.Value() } func (self *VMEnv) State() *state.StateDB { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } func (self *VMEnv) Depth() int { return self.depth } -- cgit v1.2.3 From 5ad473d7581b92811c3a3e035274a82fc5568f57 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 15:33:22 +0100 Subject: Moved methods to messages --- core/state_transition.go | 10 ++++++---- core/transaction_pool.go | 6 ------ core/types/transaction.go | 23 ++--------------------- 3 files changed, 8 insertions(+), 31 deletions(-) (limited to 'core') diff --git a/core/state_transition.go b/core/state_transition.go index a6b654842..61c9558e3 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -44,8 +44,6 @@ type StateTransition struct { type Message interface { Hash() []byte - CreatesContract() bool - From() []byte To() []byte @@ -63,6 +61,10 @@ func AddressFromMessage(msg Message) []byte { return crypto.Sha3(ethutil.NewValue([]interface{}{msg.From(), msg.Nonce()}).Encode())[12:] } +func MessageCreatesContract(msg Message) bool { + return len(msg.To()) == 0 +} + func NewStateTransition(coinbase *state.StateObject, msg Message, state *state.StateDB, block *types.Block) *StateTransition { return &StateTransition{coinbase.Address(), msg.To(), msg, new(big.Int), new(big.Int).Set(msg.GasPrice()), msg.Value(), msg.Data(), state, block, coinbase, nil, nil, nil} } @@ -93,7 +95,7 @@ func (self *StateTransition) From() *state.StateObject { return self.sen } func (self *StateTransition) To() *state.StateObject { - if self.msg != nil && self.msg.CreatesContract() { + if self.msg != nil && MessageCreatesContract(self.msg) { return nil } @@ -205,7 +207,7 @@ func (self *StateTransition) TransitionState() (err error) { var ret []byte vmenv := self.VmEnv() var ref vm.ClosureRef - if msg.CreatesContract() { + if MessageCreatesContract(msg) { self.rec = MakeContract(msg, self.state) ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) diff --git a/core/transaction_pool.go b/core/transaction_pool.go index da91ec568..58c2255a4 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -131,12 +131,6 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From()) } - if tx.IsContract() { - if tx.GasPrice().Cmp(big.NewInt(minGasPrice)) < 0 { - return fmt.Errorf("Gasprice too low, %s given should be at least %d.", tx.GasPrice, minGasPrice) - } - } - // Increment the nonce making each tx valid only once to prevent replay // attacks diff --git a/core/types/transaction.go b/core/types/transaction.go index 4b75d3abd..21d455f2e 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -9,11 +9,8 @@ import ( "github.com/obscuren/secp256k1-go" ) -var ContractAddr = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - func IsContractAddr(addr []byte) bool { return len(addr) == 0 - //return bytes.Compare(addr, ContractAddr) == 0 } type Transaction struct { @@ -25,17 +22,14 @@ type Transaction struct { data []byte v byte r, s []byte - - // Indicates whether this tx is a contract creation transaction - contractCreation bool } func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction { - return &Transaction{recipient: nil, value: value, gas: gas, gasPrice: gasPrice, data: script, contractCreation: true} + return &Transaction{recipient: nil, value: value, gas: gas, gasPrice: gasPrice, data: script} } func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction { - return &Transaction{recipient: to, value: value, gasPrice: gasPrice, gas: gas, data: data, contractCreation: IsContractAddr(to)} + return &Transaction{recipient: to, value: value, gasPrice: gasPrice, gas: gas, data: data} } func NewTransactionFromBytes(data []byte) *Transaction { @@ -99,15 +93,6 @@ func (self *Transaction) To() []byte { return self.recipient } -func (tx *Transaction) CreatesContract() bool { - return tx.contractCreation -} - -/* Deprecated */ -func (tx *Transaction) IsContract() bool { - return tx.CreatesContract() -} - func (tx *Transaction) Curve() (v byte, r []byte, s []byte) { v = tx.v r = ethutil.LeftPadBytes(tx.r, 32) @@ -192,10 +177,6 @@ func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) { tx.r = decoder.Get(7).Bytes() tx.s = decoder.Get(8).Bytes() - - if IsContractAddr(tx.recipient) { - tx.contractCreation = true - } } func (tx *Transaction) String() string { -- cgit v1.2.3 From 198cc69357a0f25ae486a041786e1239c6f5ab0f Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 21:58:26 +0100 Subject: Gas corrections and vm fixes --- core/block_manager.go | 2 +- core/state_transition.go | 73 ++++++++++++++++++++++++----------------------- core/types/transaction.go | 9 ------ 3 files changed, 38 insertions(+), 46 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index aa845a007..8d319f84e 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -115,7 +115,7 @@ done: cb := state.GetStateObject(coinbase.Address()) st := NewStateTransition(cb, tx, state, block) - err = st.TransitionState() + _, err = st.TransitionState() if err != nil { switch { case IsNonceErr(err): diff --git a/core/state_transition.go b/core/state_transition.go index 61c9558e3..34d8cca74 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -31,6 +31,7 @@ type StateTransition struct { coinbase, receiver []byte msg Message gas, gasPrice *big.Int + initialGas *big.Int value *big.Int data []byte state *state.StateDB @@ -47,7 +48,6 @@ type Message interface { From() []byte To() []byte - GasValue() *big.Int GasPrice() *big.Int Gas() *big.Int Value() *big.Int @@ -65,8 +65,12 @@ func MessageCreatesContract(msg Message) bool { return len(msg.To()) == 0 } +func MessageGasValue(msg Message) *big.Int { + return new(big.Int).Mul(msg.Gas(), msg.GasPrice()) +} + func NewStateTransition(coinbase *state.StateObject, msg Message, state *state.StateDB, block *types.Block) *StateTransition { - return &StateTransition{coinbase.Address(), msg.To(), msg, new(big.Int), new(big.Int).Set(msg.GasPrice()), msg.Value(), msg.Data(), state, block, coinbase, nil, nil, nil} + return &StateTransition{coinbase.Address(), msg.To(), msg, new(big.Int), new(big.Int).Set(msg.GasPrice()), new(big.Int), msg.Value(), msg.Data(), state, block, coinbase, nil, nil, nil} } func (self *StateTransition) VmEnv() vm.Environment { @@ -78,33 +82,16 @@ func (self *StateTransition) VmEnv() vm.Environment { } func (self *StateTransition) Coinbase() *state.StateObject { - if self.cb != nil { - return self.cb - } - - self.cb = self.state.GetOrNewStateObject(self.coinbase) - return self.cb + return self.state.GetOrNewStateObject(self.coinbase) } func (self *StateTransition) From() *state.StateObject { - if self.sen != nil { - return self.sen - } - - self.sen = self.state.GetOrNewStateObject(self.msg.From()) - - return self.sen + return self.state.GetOrNewStateObject(self.msg.From()) } func (self *StateTransition) To() *state.StateObject { if self.msg != nil && MessageCreatesContract(self.msg) { return nil } - - if self.rec != nil { - return self.rec - } - - self.rec = self.state.GetOrNewStateObject(self.msg.To()) - return self.rec + return self.state.GetOrNewStateObject(self.msg.To()) } func (self *StateTransition) UseGas(amount *big.Int) error { @@ -124,8 +111,8 @@ func (self *StateTransition) BuyGas() error { var err error sender := self.From() - if sender.Balance().Cmp(self.msg.GasValue()) < 0 { - return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.msg.GasValue(), sender.Balance()) + if sender.Balance().Cmp(MessageGasValue(self.msg)) < 0 { + return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", MessageGasValue(self.msg), sender.Balance()) } coinbase := self.Coinbase() @@ -135,20 +122,12 @@ func (self *StateTransition) BuyGas() error { } self.AddGas(self.msg.Gas()) - sender.SubAmount(self.msg.GasValue()) + self.initialGas.Set(self.msg.Gas()) + sender.SubAmount(MessageGasValue(self.msg)) return nil } -func (self *StateTransition) RefundGas() { - coinbase, sender := self.Coinbase(), self.From() - coinbase.RefundGas(self.gas, self.msg.GasPrice()) - - // Return remaining gas - remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice()) - sender.AddAmount(remaining) -} - func (self *StateTransition) preCheck() (err error) { var ( msg = self.msg @@ -168,7 +147,7 @@ func (self *StateTransition) preCheck() (err error) { return nil } -func (self *StateTransition) TransitionState() (err error) { +func (self *StateTransition) TransitionState() (ret []byte, err error) { statelogger.Debugf("(~) %x\n", self.msg.Hash()) // XXX Transactions after this point are considered valid. @@ -204,7 +183,6 @@ func (self *StateTransition) TransitionState() (err error) { return } - var ret []byte vmenv := self.VmEnv() var ref vm.ClosureRef if MessageCreatesContract(msg) { @@ -231,3 +209,26 @@ func MakeContract(msg Message, state *state.StateDB) *state.StateObject { return contract } + +func (self *StateTransition) RefundGas() { + coinbaseSub := new(big.Int).Set(self.gas) + uhalf := new(big.Int).Div(self.GasUsed(), ethutil.Big2) + for addr, refs := range self.state.Refunds() { + for _, ref := range refs { + coinbaseSub.Add(self.gas, ref) + refund := ethutil.BigMin(uhalf, ref) + self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice())) + } + } + + coinbase, sender := self.Coinbase(), self.From() + coinbase.RefundGas(coinbaseSub, self.msg.GasPrice()) + + // Return remaining gas + remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice()) + sender.AddAmount(remaining) +} + +func (self *StateTransition) GasUsed() *big.Int { + return new(big.Int).Sub(self.initialGas, self.gas) +} diff --git a/core/types/transaction.go b/core/types/transaction.go index 21d455f2e..c64fb69f0 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -46,15 +46,6 @@ func NewTransactionFromValue(val *ethutil.Value) *Transaction { return tx } -func (self *Transaction) GasValue() *big.Int { - return new(big.Int).Mul(self.gas, self.gasPrice) -} - -func (self *Transaction) TotalValue() *big.Int { - v := self.GasValue() - return v.Add(v, self.value) -} - func (tx *Transaction) Hash() []byte { data := []interface{}{tx.Nonce, tx.gasPrice, tx.gas, tx.recipient, tx.Value, tx.Data} -- cgit v1.2.3 From 332568379454dce6b1fb3c3e023a53d0c52cded0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 22:38:51 +0100 Subject: Fixed refund model --- core/state_transition.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/state_transition.go b/core/state_transition.go index 34d8cca74..9e81ddf28 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -213,12 +213,10 @@ func MakeContract(msg Message, state *state.StateDB) *state.StateObject { func (self *StateTransition) RefundGas() { coinbaseSub := new(big.Int).Set(self.gas) uhalf := new(big.Int).Div(self.GasUsed(), ethutil.Big2) - for addr, refs := range self.state.Refunds() { - for _, ref := range refs { - coinbaseSub.Add(self.gas, ref) - refund := ethutil.BigMin(uhalf, ref) - self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice())) - } + for addr, ref := range self.state.Refunds() { + refund := ethutil.BigMin(uhalf, ref) + coinbaseSub.Add(self.gas, refund) + self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice())) } coinbase, sender := self.Coinbase(), self.From() -- cgit v1.2.3 From 59ef6e36931c980ba15babfb3680514635faebf6 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Dec 2014 00:18:52 +0100 Subject: Cleaned up objects --- core/execution.go | 6 ++++-- core/vm_env.go | 4 +--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/execution.go b/core/execution.go index 827e1ee0e..cd98746c4 100644 --- a/core/execution.go +++ b/core/execution.go @@ -16,8 +16,10 @@ type Execution struct { SkipTransfer bool } -func NewExecution(vm vm.VirtualMachine, address, input []byte, gas, gasPrice, value *big.Int) *Execution { - return &Execution{vm: vm, address: address, input: input, Gas: gas, price: gasPrice, value: value} +func NewExecution(env vm.Environment, address, input []byte, gas, gasPrice, value *big.Int) *Execution { + evm := vm.New(env, vm.DebugVmTy) + + return &Execution{vm: evm, address: address, input: input, Gas: gas, price: gasPrice, value: value} } func (self *Execution) Addr() []byte { diff --git a/core/vm_env.go b/core/vm_env.go index 0b6744972..ad63ecf9c 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -43,9 +43,7 @@ func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { } func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution { - evm := vm.New(self, vm.DebugVmTy) - - return NewExecution(evm, addr, data, gas, price, value) + return NewExecution(self, addr, data, gas, price, value) } func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { -- cgit v1.2.3 From 12671c82eadc367a43502109e5e0237e228da998 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Dec 2014 00:23:00 +0100 Subject: Moved VM to execution --- core/execution.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/execution.go b/core/execution.go index cd98746c4..0b5e0558f 100644 --- a/core/execution.go +++ b/core/execution.go @@ -9,7 +9,7 @@ import ( ) type Execution struct { - vm vm.VirtualMachine + env vm.Environment address, input []byte Gas, price, value *big.Int object *state.StateObject @@ -17,9 +17,7 @@ type Execution struct { } func NewExecution(env vm.Environment, address, input []byte, gas, gasPrice, value *big.Int) *Execution { - evm := vm.New(env, vm.DebugVmTy) - - return &Execution{vm: evm, address: address, input: input, Gas: gas, price: gasPrice, value: value} + return &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value} } func (self *Execution) Addr() []byte { @@ -28,16 +26,18 @@ func (self *Execution) Addr() []byte { func (self *Execution) Call(codeAddr []byte, caller vm.ClosureRef) ([]byte, error) { // Retrieve the executing code - code := self.vm.Env().State().GetCode(codeAddr) + code := self.env.State().GetCode(codeAddr) return self.exec(code, codeAddr, caller) } func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret []byte, err error) { - env := self.vm.Env() + env := self.env + evm := vm.New(env, vm.DebugVmTy) + chainlogger.Debugf("pre state %x\n", env.State().Root()) - if self.vm.Env().Depth() == vm.MaxCallDepth { + if env.Depth() == vm.MaxCallDepth { // Consume all gas (by not returning it) and return a depth error return nil, vm.DepthError{} } @@ -56,21 +56,21 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret snapshot := env.State().Copy() defer func() { - if /*vm.IsDepthErr(err) ||*/ vm.IsOOGErr(err) { + if vm.IsOOGErr(err) { env.State().Set(snapshot) } chainlogger.Debugf("post state %x\n", env.State().Root()) }() self.object = to - ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) + ret, err = evm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) return } func (self *Execution) Create(caller vm.ClosureRef) (ret []byte, err error, account *state.StateObject) { ret, err = self.exec(self.input, nil, caller) - account = self.vm.Env().State().GetStateObject(self.address) + account = self.env.State().GetStateObject(self.address) return } -- cgit v1.2.3 From 0e93b98533968f4a0033ec6214391f5ca647a956 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Dec 2014 13:34:53 +0100 Subject: Transaction was generating incorrect hash because of var changes --- core/state_transition.go | 2 +- core/types/transaction.go | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/state_transition.go b/core/state_transition.go index 9e81ddf28..a54246eba 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -112,7 +112,7 @@ func (self *StateTransition) BuyGas() error { sender := self.From() if sender.Balance().Cmp(MessageGasValue(self.msg)) < 0 { - return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", MessageGasValue(self.msg), sender.Balance()) + return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address()[:4], MessageGasValue(self.msg), sender.Balance()) } coinbase := self.Coinbase() diff --git a/core/types/transaction.go b/core/types/transaction.go index c64fb69f0..f6ad0774b 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -47,7 +47,7 @@ func NewTransactionFromValue(val *ethutil.Value) *Transaction { } func (tx *Transaction) Hash() []byte { - data := []interface{}{tx.Nonce, tx.gasPrice, tx.gas, tx.recipient, tx.Value, tx.Data} + data := []interface{}{tx.nonce, tx.gasPrice, tx.gas, tx.recipient, tx.value, tx.data} return crypto.Sha3(ethutil.NewValue(data).Encode()) } @@ -108,8 +108,8 @@ func (tx *Transaction) PublicKey() []byte { sig := append(r, s...) sig = append(sig, v-27) - pubkey := crypto.Ecrecover(append(hash, sig...)) - //pubkey, _ := secp256k1.RecoverPubkey(hash, sig) + //pubkey := crypto.Ecrecover(append(hash, sig...)) + pubkey, _ := secp256k1.RecoverPubkey(hash, sig) return pubkey } @@ -138,9 +138,7 @@ func (tx *Transaction) Sign(privk []byte) error { } func (tx *Transaction) RlpData() interface{} { - data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.recipient, tx.Value, tx.Data} - - // TODO Remove prefixing zero's + data := []interface{}{tx.nonce, tx.gasPrice, tx.gas, tx.recipient, tx.value, tx.data} return append(data, tx.v, new(big.Int).SetBytes(tx.r).Bytes(), new(big.Int).SetBytes(tx.s).Bytes()) } @@ -184,6 +182,7 @@ func (tx *Transaction) String() string { V: 0x%x R: 0x%x S: 0x%x + Hex: %x `, tx.Hash(), len(tx.recipient) == 0, @@ -192,11 +191,13 @@ func (tx *Transaction) String() string { tx.nonce, tx.gasPrice, tx.gas, - tx.Value, - tx.Data, + tx.value, + tx.data, tx.v, tx.r, - tx.s) + tx.s, + ethutil.Encode(tx), + ) } // Transaction slice type for basic sorting -- cgit v1.2.3 From f5b8f3d41b533d51eb81e895ed9b6aa31d7aaaef Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Dec 2014 13:59:49 +0100 Subject: Removed OOG check. Revert should always happen. --- core/block_manager.go | 2 +- core/execution.go | 4 +--- core/state_transition.go | 11 +++++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index 8d319f84e..20285f8f0 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -129,7 +129,6 @@ done: statelogger.Infoln(err) erroneous = append(erroneous, tx) err = nil - continue } } @@ -215,6 +214,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I receiptSha := types.DeriveSha(receipts) if bytes.Compare(receiptSha, block.ReceiptSha) != 0 { + chainlogger.Debugln(receipts) err = fmt.Errorf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha) return } diff --git a/core/execution.go b/core/execution.go index 0b5e0558f..44dbd3ace 100644 --- a/core/execution.go +++ b/core/execution.go @@ -56,9 +56,7 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret snapshot := env.State().Copy() defer func() { - if vm.IsOOGErr(err) { - env.State().Set(snapshot) - } + env.State().Set(snapshot) chainlogger.Debugf("post state %x\n", env.State().Root()) }() diff --git a/core/state_transition.go b/core/state_transition.go index a54246eba..a60f31e3e 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -189,12 +189,19 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) { self.rec = MakeContract(msg, self.state) ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) - ref.SetCode(ret) + if err == nil { + dataGas := big.NewInt(int64(len(ret))) + dataGas.Mul(dataGas, vm.GasCreateByte) + if err = self.UseGas(dataGas); err == nil { + ref.SetCode(ret) + } + } } else { ret, err = vmenv.Call(self.From(), self.To().Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) } + if err != nil { - statelogger.Debugln(err) + self.UseGas(self.gas) } return -- cgit v1.2.3 From 1508a23a6fe3cc50f718bfd6c62caae056534c09 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 20 Dec 2014 02:21:13 +0100 Subject: Minor updates on gas and removed/refactored old code. --- core/block_manager.go | 4 +++- core/execution.go | 15 ++++++--------- core/state_transition.go | 21 ++++++++++----------- 3 files changed, 19 insertions(+), 21 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index 20285f8f0..2567e39c4 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -142,6 +142,7 @@ done: receipt := types.NewReceipt(state.Root(), cumulative) receipt.SetLogs(state.Logs()) receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) + chainlogger.Debugln(receipt) // Notify all subscribers if !transientProcess { @@ -214,7 +215,8 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I receiptSha := types.DeriveSha(receipts) if bytes.Compare(receiptSha, block.ReceiptSha) != 0 { - chainlogger.Debugln(receipts) + //chainlogger.Debugf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha) + fmt.Printf("%x\n", ethutil.Encode(receipts)) err = fmt.Errorf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha) return } diff --git a/core/execution.go b/core/execution.go index 44dbd3ace..b7eead0dd 100644 --- a/core/execution.go +++ b/core/execution.go @@ -3,6 +3,7 @@ package core import ( "fmt" "math/big" + "time" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" @@ -12,7 +13,6 @@ type Execution struct { env vm.Environment address, input []byte Gas, price, value *big.Int - object *state.StateObject SkipTransfer bool } @@ -35,8 +35,6 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret env := self.env evm := vm.New(env, vm.DebugVmTy) - chainlogger.Debugf("pre state %x\n", env.State().Root()) - if env.Depth() == vm.MaxCallDepth { // Consume all gas (by not returning it) and return a depth error return nil, vm.DepthError{} @@ -55,13 +53,12 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret } snapshot := env.State().Copy() - defer func() { - env.State().Set(snapshot) - chainlogger.Debugf("post state %x\n", env.State().Root()) - }() - - self.object = to + start := time.Now() ret, err = evm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) + if err != nil { + env.State().Set(snapshot) + } + chainlogger.Debugf("vm took %v\n", time.Since(start)) return } diff --git a/core/state_transition.go b/core/state_transition.go index a60f31e3e..7b7026c29 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -186,13 +186,13 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) { vmenv := self.VmEnv() var ref vm.ClosureRef if MessageCreatesContract(msg) { - self.rec = MakeContract(msg, self.state) - - ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) + contract := MakeContract(msg, self.state) + ret, err, ref = vmenv.Create(sender, contract.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) if err == nil { dataGas := big.NewInt(int64(len(ret))) dataGas.Mul(dataGas, vm.GasCreateByte) if err = self.UseGas(dataGas); err == nil { + //self.state.SetCode(ref.Address(), ret) ref.SetCode(ret) } } @@ -218,20 +218,19 @@ func MakeContract(msg Message, state *state.StateDB) *state.StateObject { } func (self *StateTransition) RefundGas() { - coinbaseSub := new(big.Int).Set(self.gas) + coinbase, sender := self.Coinbase(), self.From() + // Return remaining gas + remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice()) + sender.AddAmount(remaining) + uhalf := new(big.Int).Div(self.GasUsed(), ethutil.Big2) for addr, ref := range self.state.Refunds() { refund := ethutil.BigMin(uhalf, ref) - coinbaseSub.Add(self.gas, refund) + self.gas.Add(self.gas, refund) self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice())) } - coinbase, sender := self.Coinbase(), self.From() - coinbase.RefundGas(coinbaseSub, self.msg.GasPrice()) - - // Return remaining gas - remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice()) - sender.AddAmount(remaining) + coinbase.RefundGas(self.gas, self.msg.GasPrice()) } func (self *StateTransition) GasUsed() *big.Int { -- cgit v1.2.3 From 4cd79d8ddd7608d60344b13fe4bda7315429d1d9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Dec 2014 13:48:44 +0100 Subject: Refactored block & Transaction * Includes new rlp decoder --- core/block_manager.go | 86 ++++---- core/chain_manager.go | 134 +++++++------ core/filter.go | 13 +- core/genesis.go | 65 +++--- core/types/block.go | 490 ++++++++++++++++------------------------------ core/types/block_test.go | 23 +++ core/types/transaction.go | 121 ++++++------ core/vm_env.go | 12 +- 8 files changed, 416 insertions(+), 528 deletions(-) create mode 100644 core/types/block_test.go (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index b60cecc29..c61cf6504 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -17,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/state" + "gopkg.in/fatih/set.v0" ) var statelogger = logger.NewLogger("BLOCK") @@ -82,8 +83,8 @@ func NewBlockManager(txpool *TxPool, chainManager *ChainManager, eventMux *event } func (sm *BlockManager) TransitionState(statedb *state.StateDB, parent, block *types.Block) (receipts types.Receipts, err error) { - coinbase := statedb.GetOrNewStateObject(block.Coinbase) - coinbase.SetGasPool(block.CalcGasLimit(parent)) + coinbase := statedb.GetOrNewStateObject(block.Header().Coinbase) + coinbase.SetGasPool(CalcGasLimit(parent, block)) // Process the transactions on to current block receipts, _, _, _, err = sm.ApplyTransactions(coinbase, statedb, block, block.Transactions(), false) @@ -156,7 +157,7 @@ done: } block.Reward = cumulativeSum - block.GasUsed = totalUsedGas + block.Header().GasUsed = totalUsedGas return receipts, handled, unhandled, erroneous, err } @@ -166,14 +167,15 @@ func (sm *BlockManager) Process(block *types.Block) (td *big.Int, msgs state.Mes sm.mutex.Lock() defer sm.mutex.Unlock() - if sm.bc.HasBlock(block.Hash()) { - return nil, nil, &KnownBlockError{block.Number, block.Hash()} + header := block.Header() + if sm.bc.HasBlock(header.Hash()) { + return nil, nil, &KnownBlockError{header.Number, header.Hash()} } - if !sm.bc.HasBlock(block.PrevHash) { - return nil, nil, ParentError(block.PrevHash) + if !sm.bc.HasBlock(header.ParentHash) { + return nil, nil, ParentError(header.ParentHash) } - parent := sm.bc.GetBlock(block.PrevHash) + parent := sm.bc.GetBlock(header.ParentHash) return sm.ProcessWithParent(block, parent) } @@ -181,7 +183,7 @@ func (sm *BlockManager) Process(block *types.Block) (td *big.Int, msgs state.Mes func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.Int, messages state.Messages, err error) { sm.lastAttemptedBlock = block - state := parent.State().Copy() + state := state.New(parent.Trie().Copy()) // Defer the Undo on the Trie. If the block processing happened // we don't want to undo but since undo only happens on dirty @@ -199,23 +201,23 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I return } + header := block.Header() + rbloom := types.CreateBloom(receipts) - if bytes.Compare(rbloom, block.LogsBloom) != 0 { + if bytes.Compare(rbloom, header.Bloom) != 0 { err = fmt.Errorf("unable to replicate block's bloom=%x", rbloom) return } txSha := types.DeriveSha(block.Transactions()) - if bytes.Compare(txSha, block.TxSha) != 0 { - err = fmt.Errorf("validating transaction root. received=%x got=%x", block.TxSha, txSha) + if bytes.Compare(txSha, header.TxHash) != 0 { + err = fmt.Errorf("validating transaction root. received=%x got=%x", header.TxHash, txSha) return } receiptSha := types.DeriveSha(receipts) - if bytes.Compare(receiptSha, block.ReceiptSha) != 0 { - //chainlogger.Debugf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha) - fmt.Printf("%x\n", ethutil.Encode(receipts)) - err = fmt.Errorf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha) + if bytes.Compare(receiptSha, header.ReceiptHash) != 0 { + err = fmt.Errorf("validating receipt root. received=%x got=%x", header.ReceiptHash, receiptSha) return } @@ -225,8 +227,8 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I state.Update(ethutil.Big0) - if !block.State().Cmp(state) { - err = fmt.Errorf("invalid merkle root. received=%x got=%x", block.Root(), state.Root()) + if !bytes.Equal(header.Root, state.Root()) { + err = fmt.Errorf("invalid merkle root. received=%x got=%x", header.Root, state.Root()) return } @@ -238,7 +240,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I messages := state.Manifest().Messages state.Manifest().Reset() - chainlogger.Infof("Processed block #%d (%x...)\n", block.Number, block.Hash()[0:4]) + chainlogger.Infof("Processed block #%d (%x...)\n", header.Number, block.Hash()[0:4]) sm.txpool.RemoveSet(block.Transactions()) @@ -250,14 +252,14 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) { uncleDiff := new(big.Int) - for _, uncle := range block.Uncles { + for _, uncle := range block.Uncles() { uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) } // TD(genesis_block) = 0 and TD(B) = TD(B.parent) + sum(u.difficulty for u in B.uncles) + B.difficulty td := new(big.Int) td = td.Add(sm.bc.Td(), uncleDiff) - td = td.Add(td, block.Difficulty) + td = td.Add(td, block.Header().Difficulty) // The new TD will only be accepted if the new difficulty is // is greater than the previous. @@ -273,13 +275,13 @@ func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) { // Validation validates easy over difficult (dagger takes longer time = difficult) func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error { expd := CalcDifficulty(block, parent) - if expd.Cmp(block.Difficulty) < 0 { - return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd) + if expd.Cmp(block.Header().Difficulty) < 0 { + return fmt.Errorf("Difficulty check failed for block %v, %v", block.Header().Difficulty, expd) } - diff := block.Time - parent.Time + diff := block.Header().Time - parent.Header().Time if diff < 0 { - return ValidationError("Block timestamp less then prev block %v (%v - %v)", diff, block.Time, sm.bc.CurrentBlock().Time) + return ValidationError("Block timestamp less then prev block %v (%v - %v)", diff, block.Header().Time, sm.bc.CurrentBlock().Header().Time) } /* XXX @@ -291,7 +293,7 @@ func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error { // Verify the nonce of the block. Return an error if it's not valid if !sm.Pow.Verify(block /*block.HashNoNonce(), block.Difficulty, block.Nonce*/) { - return ValidationError("Block's nonce is invalid (= %v)", ethutil.Bytes2Hex(block.Nonce)) + return ValidationError("Block's nonce is invalid (= %v)", ethutil.Bytes2Hex(block.Header().Nonce)) } return nil @@ -300,24 +302,28 @@ func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error { func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent *types.Block) error { reward := new(big.Int).Set(BlockReward) - knownUncles := ethutil.Set(parent.Uncles) - nonces := ethutil.NewSet(block.Nonce) - for _, uncle := range block.Uncles { + knownUncles := set.New() + for _, uncle := range parent.Uncles() { + knownUncles.Add(uncle.Hash()) + } + + nonces := ethutil.NewSet(block.Header().Nonce) + for _, uncle := range block.Uncles() { if nonces.Include(uncle.Nonce) { // Error not unique return UncleError("Uncle not unique") } - uncleParent := sm.bc.GetBlock(uncle.PrevHash) + uncleParent := sm.bc.GetBlock(uncle.ParentHash) if uncleParent == nil { - return UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.PrevHash[0:4])) + return UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) } - if uncleParent.Number.Cmp(new(big.Int).Sub(parent.Number, big.NewInt(6))) < 0 { + if uncleParent.Header().Number.Cmp(new(big.Int).Sub(parent.Header().Number, big.NewInt(6))) < 0 { return UncleError("Uncle too old") } - if knownUncles.Include(uncle.Hash()) { + if knownUncles.Has(uncle.Hash()) { return UncleError("Uncle in chain") } @@ -333,15 +339,15 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent } // Get the account associated with the coinbase - account := statedb.GetAccount(block.Coinbase) + account := statedb.GetAccount(block.Header().Coinbase) // Reward amount of ether to the coinbase address account.AddAmount(reward) statedb.Manifest().AddMessage(&state.Message{ - To: block.Coinbase, + To: block.Header().Coinbase, Input: nil, Origin: nil, - Block: block.Hash(), Timestamp: block.Time, Coinbase: block.Coinbase, Number: block.Number, + Block: block.Hash(), Timestamp: int64(block.Header().Time), Coinbase: block.Header().Coinbase, Number: block.Header().Number, Value: new(big.Int).Add(reward, block.Reward), }) @@ -349,15 +355,15 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent } func (sm *BlockManager) GetMessages(block *types.Block) (messages []*state.Message, err error) { - if !sm.bc.HasBlock(block.PrevHash) { - return nil, ParentError(block.PrevHash) + if !sm.bc.HasBlock(block.Header().ParentHash) { + return nil, ParentError(block.Header().ParentHash) } sm.lastAttemptedBlock = block var ( - parent = sm.bc.GetBlock(block.PrevHash) - state = parent.State().Copy() + parent = sm.bc.GetBlock(block.Header().ParentHash) + state = state.New(parent.Trie().Copy()) ) defer state.Reset() diff --git a/core/chain_manager.go b/core/chain_manager.go index e6268c01e..e35c4aa3a 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -1,6 +1,7 @@ package core import ( + "bytes" "fmt" "math/big" "sync" @@ -9,11 +10,13 @@ import ( "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" ) var chainlogger = logger.NewLogger("CHAIN") +/* func AddTestNetFunds(block *types.Block) { for _, addr := range []string{ "51ba59315b3a95761d0863b05ccc7a7f54703d99", @@ -31,20 +34,41 @@ func AddTestNetFunds(block *types.Block) { block.State().UpdateStateObject(account) } } +*/ func CalcDifficulty(block, parent *types.Block) *big.Int { diff := new(big.Int) - adjust := new(big.Int).Rsh(parent.Difficulty, 10) - if block.Time >= parent.Time+5 { - diff.Sub(parent.Difficulty, adjust) + bh, ph := block.Header(), parent.Header() + adjust := new(big.Int).Rsh(ph.Difficulty, 10) + if bh.Time >= ph.Time+5 { + diff.Sub(ph.Difficulty, adjust) } else { - diff.Add(parent.Difficulty, adjust) + diff.Add(ph.Difficulty, adjust) } return diff } +func CalcGasLimit(parent, block *types.Block) *big.Int { + if block.Number().Cmp(big.NewInt(0)) == 0 { + return ethutil.BigPow(10, 6) + } + + // ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024 + + previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit()) + current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed()), big.NewRat(6, 5)) + curInt := new(big.Int).Div(current.Num(), current.Denom()) + + result := new(big.Int).Add(previous, curInt) + result.Div(result, big.NewInt(1024)) + + min := big.NewInt(125000) + + return ethutil.BigMax(min, result) +} + type ChainManager struct { //eth EthManager processor types.BlockProcessor @@ -90,7 +114,7 @@ func (self *ChainManager) CurrentBlock() *types.Block { func NewChainManager(mux *event.TypeMux) *ChainManager { bc := &ChainManager{} - bc.genesisBlock = types.NewBlockFromBytes(ethutil.Encode(Genesis)) + bc.genesisBlock = GenesisBlock() bc.eventMux = mux bc.setLastBlock() @@ -112,7 +136,7 @@ func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { } func (self *ChainManager) State() *state.StateDB { - return self.CurrentBlock().State() + return state.New(self.CurrentBlock().Trie()) } func (self *ChainManager) TransState() *state.StateDB { @@ -122,13 +146,11 @@ func (self *ChainManager) TransState() *state.StateDB { func (bc *ChainManager) setLastBlock() { data, _ := ethutil.Config.Db.Get([]byte("LastBlock")) if len(data) != 0 { - // Prep genesis - AddTestNetFunds(bc.genesisBlock) - - block := types.NewBlockFromBytes(data) - bc.currentBlock = block + var block types.Block + rlp.Decode(bytes.NewReader(data), &block) + bc.currentBlock = &block bc.lastBlockHash = block.Hash() - bc.lastBlockNumber = block.Number.Uint64() + bc.lastBlockNumber = block.Header().Number.Uint64() // Set the last know difficulty (might be 0x0 as initial value, Genesis) bc.td = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) @@ -144,27 +166,28 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { bc.mu.RLock() defer bc.mu.RUnlock() - var root interface{} - hash := ZeroHash256 + var root []byte + parentHash := ZeroHash256 if bc.CurrentBlock != nil { - root = bc.currentBlock.Root() - hash = bc.lastBlockHash + root = bc.currentBlock.Header().Root + parentHash = bc.lastBlockHash } - block := types.CreateBlock( - root, - hash, + block := types.NewBlock( + parentHash, coinbase, + root, ethutil.BigPow(2, 32), nil, "") parent := bc.currentBlock if parent != nil { - block.Difficulty = CalcDifficulty(block, parent) - block.Number = new(big.Int).Add(bc.currentBlock.Number, ethutil.Big1) - block.GasLimit = block.CalcGasLimit(bc.currentBlock) + header := block.Header() + header.Difficulty = CalcDifficulty(block, parent) + header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1) + header.GasLimit = CalcGasLimit(parent, block) } @@ -175,9 +198,6 @@ func (bc *ChainManager) Reset() { bc.mu.Lock() defer bc.mu.Unlock() - AddTestNetFunds(bc.genesisBlock) - - bc.genesisBlock.Trie().Sync() // Prepare the genesis block bc.write(bc.genesisBlock) bc.insert(bc.genesisBlock) @@ -193,18 +213,20 @@ func (self *ChainManager) Export() []byte { self.mu.RLock() defer self.mu.RUnlock() - chainlogger.Infof("exporting %v blocks...\n", self.currentBlock.Number) + chainlogger.Infof("exporting %v blocks...\n", self.currentBlock.Header().Number) - blocks := make([]*types.Block, int(self.currentBlock.Number.Int64())+1) - for block := self.currentBlock; block != nil; block = self.GetBlock(block.PrevHash) { - blocks[block.Number.Int64()] = block + blocks := make([]*types.Block, int(self.currentBlock.NumberU64())+1) + for block := self.currentBlock; block != nil; block = self.GetBlock(block.Header().ParentHash) { + blocks[block.NumberU64()] = block } + //fmt.Println(blocks) + return nil return ethutil.Encode(blocks) } func (bc *ChainManager) insert(block *types.Block) { - encodedBlock := block.RlpEncode() + encodedBlock := ethutil.Encode(block) ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock) bc.currentBlock = block bc.lastBlockHash = block.Hash() @@ -213,7 +235,7 @@ func (bc *ChainManager) insert(block *types.Block) { func (bc *ChainManager) write(block *types.Block) { bc.writeBlockInfo(block) - encodedBlock := block.RlpEncode() + encodedBlock := ethutil.Encode(block) ethutil.Config.Db.Put(block.Hash(), encodedBlock) } @@ -238,11 +260,11 @@ func (self *ChainManager) GetBlockHashesFromHash(hash []byte, max uint64) (chain for i := uint64(0); i < max; i++ { chain = append(chain, block.Hash()) - if block.Number.Cmp(ethutil.Big0) <= 0 { + if block.Header().Number.Cmp(ethutil.Big0) <= 0 { break } - block = self.GetBlock(block.PrevHash) + block = self.GetBlock(block.Header().ParentHash) } return @@ -253,8 +275,13 @@ func (self *ChainManager) GetBlock(hash []byte) *types.Block { if len(data) == 0 { return nil } + var block types.Block + if err := rlp.Decode(bytes.NewReader(data), &block); err != nil { + fmt.Println(err) + return nil + } - return types.NewBlockFromBytes(data) + return &block } func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block { @@ -262,13 +289,13 @@ func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block { defer self.mu.RUnlock() block := self.currentBlock - for ; block != nil; block = self.GetBlock(block.PrevHash) { - if block.Number.Uint64() == num { + for ; block != nil; block = self.GetBlock(block.Header().ParentHash) { + if block.Header().Number.Uint64() == num { break } } - if block != nil && block.Number.Uint64() == 0 && num != 0 { + if block != nil && block.Header().Number.Uint64() == 0 && num != 0 { return nil } @@ -281,40 +308,28 @@ func (bc *ChainManager) setTotalDifficulty(td *big.Int) { } func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { - parent := self.GetBlock(block.PrevHash) + parent := self.GetBlock(block.Header().ParentHash) if parent == nil { - return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.PrevHash) + return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.Header().ParentHash) } - parentTd := parent.BlockInfo().TD + parentTd := parent.Td uncleDiff := new(big.Int) - for _, uncle := range block.Uncles { + for _, uncle := range block.Uncles() { uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) } td := new(big.Int) td = td.Add(parentTd, uncleDiff) - td = td.Add(td, block.Difficulty) + td = td.Add(td, block.Header().Difficulty) return td, nil } -func (bc *ChainManager) BlockInfo(block *types.Block) types.BlockInfo { - bi := types.BlockInfo{} - data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...)) - bi.RlpDecode(data) - - return bi -} - // Unexported method for writing extra non-essential block info to the db func (bc *ChainManager) writeBlockInfo(block *types.Block) { bc.lastBlockNumber++ - bi := types.BlockInfo{Number: bc.lastBlockNumber, Hash: block.Hash(), Parent: block.PrevHash, TD: bc.td} - - // For now we use the block hash with the words "info" appended as key - ethutil.Config.Db.Put(append(block.Hash(), []byte("Info")...), bi.RlpEncode()) } func (bc *ChainManager) Stop() { @@ -331,7 +346,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { continue } - chainlogger.Infof("block #%v process failed (%x)\n", block.Number, block.Hash()[:4]) + h := block.Header() + chainlogger.Infof("block #%v process failed (%x)\n", h.Number, h.Hash()[:4]) chainlogger.Infoln(block) chainlogger.Infoln(err) return err @@ -339,16 +355,16 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { self.mu.Lock() { - self.write(block) + cblock := self.currentBlock if td.Cmp(self.td) > 0 { - if block.Number.Cmp(new(big.Int).Add(self.currentBlock.Number, ethutil.Big1)) < 0 { - chainlogger.Infof("Split detected. New head #%v (%x), was #%v (%x)\n", block.Number, block.Hash()[:4], self.currentBlock.Number, self.currentBlock.Hash()[:4]) + if block.Header().Number.Cmp(new(big.Int).Add(cblock.Header().Number, ethutil.Big1)) < 0 { + chainlogger.Infof("Split detected. New head #%v (%x), was #%v (%x)\n", block.Header().Number, block.Hash()[:4], cblock.Header().Number, cblock.Hash()[:4]) } self.setTotalDifficulty(td) self.insert(block) - self.transState = self.currentBlock.State().Copy() + self.transState = state.New(cblock.Trie().Copy()) } } diff --git a/core/filter.go b/core/filter.go index fb992782d..7c34748df 100644 --- a/core/filter.go +++ b/core/filter.go @@ -76,13 +76,14 @@ func (self *Filter) SetSkip(skip int) { // Run filters messages with the current parameters set func (self *Filter) Find() []*state.Message { + earliestBlock := self.eth.ChainManager().CurrentBlock() var earliestBlockNo uint64 = uint64(self.earliest) if self.earliest == -1 { - earliestBlockNo = self.eth.ChainManager().CurrentBlock().Number.Uint64() + earliestBlockNo = earliestBlock.NumberU64() } var latestBlockNo uint64 = uint64(self.latest) if self.latest == -1 { - latestBlockNo = self.eth.ChainManager().CurrentBlock().Number.Uint64() + latestBlockNo = earliestBlock.NumberU64() } var ( @@ -93,7 +94,7 @@ func (self *Filter) Find() []*state.Message { for i := 0; !quit && block != nil; i++ { // Quit on latest switch { - case block.Number.Uint64() == earliestBlockNo, block.Number.Uint64() == 0: + case block.NumberU64() == earliestBlockNo, block.NumberU64() == 0: quit = true case self.max <= len(messages): break @@ -113,7 +114,7 @@ func (self *Filter) Find() []*state.Message { messages = append(messages, self.FilterMessages(msgs)...) } - block = self.eth.ChainManager().GetBlock(block.PrevHash) + block = self.eth.ChainManager().GetBlock(block.ParentHash()) } skip := int(math.Min(float64(len(messages)), float64(self.skip))) @@ -176,7 +177,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool { var fromIncluded, toIncluded bool if len(self.from) > 0 { for _, from := range self.from { - if types.BloomLookup(block.LogsBloom, from) || bytes.Equal(block.Coinbase, from) { + if types.BloomLookup(block.Bloom(), from) || bytes.Equal(block.Coinbase(), from) { fromIncluded = true break } @@ -187,7 +188,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool { if len(self.to) > 0 { for _, to := range self.to { - if types.BloomLookup(block.LogsBloom, ethutil.U256(new(big.Int).Add(ethutil.Big1, ethutil.BigD(to))).Bytes()) || bytes.Equal(block.Coinbase, to) { + if types.BloomLookup(block.Bloom(), ethutil.U256(new(big.Int).Add(ethutil.Big1, ethutil.BigD(to))).Bytes()) || bytes.Equal(block.Coinbase(), to) { toIncluded = true break } diff --git a/core/genesis.go b/core/genesis.go index 707154759..51afa314e 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -3,8 +3,10 @@ package core import ( "math/big" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" ) /* @@ -17,36 +19,35 @@ var ZeroHash512 = make([]byte, 64) var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{})) var EmptyListRoot = crypto.Sha3(ethutil.Encode("")) -var GenesisHeader = []interface{}{ - // Previous hash (none) - ZeroHash256, - // Empty uncles - EmptyShaList, - // Coinbase - ZeroHash160, - // Root state - EmptyShaList, - // tx root - EmptyListRoot, - // receipt root - EmptyListRoot, - // bloom - ZeroHash512, - // Difficulty - //ethutil.BigPow(2, 22), - big.NewInt(131072), - // Number - ethutil.Big0, - // Block upper gas bound - big.NewInt(1000000), - // Block gas used - ethutil.Big0, - // Time - ethutil.Big0, - // Extra - nil, - // Nonce - crypto.Sha3(big.NewInt(42).Bytes()), -} +func GenesisBlock() *types.Block { + genesis := types.NewBlock(ZeroHash256, ZeroHash160, EmptyListRoot, big.NewInt(131072), crypto.Sha3(big.NewInt(42).Bytes()), "") + genesis.Header().Number = ethutil.Big0 + genesis.Header().GasLimit = big.NewInt(1000000) + genesis.Header().GasUsed = ethutil.Big0 + genesis.Header().Time = 0 + + genesis.SetUncles([]*types.Header{}) + genesis.SetTransactions(types.Transactions{}) + genesis.SetReceipts(types.Receipts{}) -var Genesis = []interface{}{GenesisHeader, []interface{}{}, []interface{}{}} + statedb := state.New(genesis.Trie()) + for _, addr := range []string{ + "51ba59315b3a95761d0863b05ccc7a7f54703d99", + "e4157b34ea9615cfbde6b4fda419828124b70c78", + "b9c015918bdaba24b4ff057a92a3873d6eb201be", + "6c386a4b26f73c802f34673f7248bb118f97424a", + "cd2a3d9f938e13cd947ec05abc7fe734df8dd826", + "2ef47100e0787b915105fd5e3f4ff6752079d5cb", + "e6716f9544a56c530d868e4bfbacb172315bdead", + "1a26338f0d905e295fccb71fa9ea849ffa12aaf4", + } { + codedAddr := ethutil.Hex2Bytes(addr) + account := statedb.GetAccount(codedAddr) + account.SetBalance(ethutil.Big("1606938044258990275541962092341162602522202993782792835301376")) //ethutil.BigPow(2, 200) + statedb.UpdateStateObject(account) + } + statedb.Sync() + genesis.Header().Root = statedb.Root() + + return genesis +} diff --git a/core/types/block.go b/core/types/block.go index 2d889f35f..b0fcbdd9b 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -9,408 +9,252 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/trie" ) -type BlockInfo struct { - Number uint64 - Hash []byte - Parent []byte - TD *big.Int -} - -func (bi *BlockInfo) RlpDecode(data []byte) { - decoder := ethutil.NewValueFromBytes(data) - - bi.Number = decoder.Get(0).Uint() - bi.Hash = decoder.Get(1).Bytes() - bi.Parent = decoder.Get(2).Bytes() - bi.TD = decoder.Get(3).BigInt() -} - -func (bi *BlockInfo) RlpEncode() []byte { - return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent, bi.TD}) -} - -type Blocks []*Block - -func (self Blocks) AsSet() ethutil.UniqueSet { - set := make(ethutil.UniqueSet) - for _, block := range self { - set.Insert(block.Hash()) - } - - return set -} - -type BlockBy func(b1, b2 *Block) bool - -func (self BlockBy) Sort(blocks Blocks) { - bs := blockSorter{ - blocks: blocks, - by: self, - } - sort.Sort(bs) -} - -type blockSorter struct { - blocks Blocks - by func(b1, b2 *Block) bool -} - -func (self blockSorter) Len() int { return len(self.blocks) } -func (self blockSorter) Swap(i, j int) { - self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i] -} -func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) } - -func Number(b1, b2 *Block) bool { return b1.Number.Cmp(b2.Number) < 0 } - -type Block struct { +type Header struct { // Hash to the previous block - PrevHash ethutil.Bytes + ParentHash ethutil.Bytes // Uncles of this block - Uncles Blocks - UncleSha []byte + UncleHash []byte // The coin base address Coinbase []byte // Block Trie state - //state *ethutil.Trie - state *state.StateDB + Root []byte + // Tx sha + TxHash []byte + // Receipt sha + ReceiptHash []byte + // Bloom + Bloom []byte // Difficulty for the current block Difficulty *big.Int - // Creation time - Time int64 // The block number Number *big.Int // Gas limit GasLimit *big.Int // Gas used GasUsed *big.Int + // Creation time + Time uint64 // Extra data Extra string // Block Nonce for verification Nonce ethutil.Bytes - // List of transactions and/or contracts - transactions Transactions - receipts Receipts - TxSha, ReceiptSha []byte - LogsBloom []byte - - Reward *big.Int } -func NewBlockFromBytes(raw []byte) *Block { - block := &Block{} - block.RlpDecode(raw) +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) + } - return block + return fields +} + +func (self *Header) RlpData() interface{} { + return self.rlpData(true) } -// New block takes a raw encoded string -func NewBlockFromRlpValue(rlpValue *ethutil.Value) *Block { - block := &Block{} - block.RlpValueDecode(rlpValue) +func (self *Header) Hash() []byte { + return crypto.Sha3(ethutil.Encode(self.rlpData(true))) +} - return block +func (self *Header) HashNoNonce() []byte { + return crypto.Sha3(ethutil.Encode(self.rlpData(false))) } -func CreateBlock(root interface{}, - prevHash []byte, - base []byte, - Difficulty *big.Int, - Nonce []byte, - extra string) *Block { - - block := &Block{ - PrevHash: prevHash, - Coinbase: base, - Difficulty: Difficulty, - Nonce: Nonce, - Time: time.Now().Unix(), +type Block struct { + header *Header + uncles []*Header + transactions Transactions + Td *big.Int + + receipts Receipts + Reward *big.Int +} + +func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce []byte, extra string) *Block { + header := &Header{ + Root: root, + ParentHash: parentHash, + Coinbase: coinbase, + Difficulty: difficulty, + Nonce: nonce, + Time: uint64(time.Now().Unix()), Extra: extra, - UncleSha: nil, GasUsed: new(big.Int), GasLimit: new(big.Int), } - block.SetUncles([]*Block{}) - block.state = state.New(trie.New(ethutil.Config.Db, root)) + block := &Block{header: header, Reward: new(big.Int)} return block } -// Returns a hash of the block -func (block *Block) Hash() ethutil.Bytes { - return crypto.Sha3(ethutil.NewValue(block.header()).Encode()) - //return crypto.Sha3(block.Value().Encode()) -} - -func (block *Block) HashNoNonce() []byte { - return crypto.Sha3(ethutil.Encode(block.miningHeader())) -} - -func (block *Block) State() *state.StateDB { - return block.state -} - -func (block *Block) Transactions() Transactions { - return block.transactions +func NewBlockWithHeader(header *Header) *Block { + return &Block{header: header} } -func (block *Block) CalcGasLimit(parent *Block) *big.Int { - if block.Number.Cmp(big.NewInt(0)) == 0 { - return ethutil.BigPow(10, 6) +func (self *Block) DecodeRLP(s *rlp.Stream) error { + if _, err := s.List(); err != nil { + return err } - // ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024 - - previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit) - current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed), big.NewRat(6, 5)) - curInt := new(big.Int).Div(current.Num(), current.Denom()) - - result := new(big.Int).Add(previous, curInt) - result.Div(result, big.NewInt(1024)) - - min := big.NewInt(125000) - - return ethutil.BigMax(min, result) -} - -func (block *Block) BlockInfo() BlockInfo { - bi := BlockInfo{} - data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...)) - bi.RlpDecode(data) - - return bi -} - -func (self *Block) GetTransaction(hash []byte) *Transaction { - for _, tx := range self.transactions { - if bytes.Compare(tx.Hash(), hash) == 0 { - return tx - } + var header Header + if err := s.Decode(&header); err != nil { + return err } - return nil -} - -// Sync the block's state and contract respectively -func (block *Block) Sync() { - block.state.Sync() -} - -func (block *Block) Undo() { - // Sync the block state itself - block.state.Reset() -} - -/////// Block Encoding -func (block *Block) rlpReceipts() interface{} { - // Marshal the transactions of this block - encR := make([]interface{}, len(block.receipts)) - for i, r := range block.receipts { - // Cast it to a string (safe) - encR[i] = r.RlpData() + var transactions []*Transaction + if err := s.Decode(&transactions); err != nil { + return err } - return encR -} + var uncleHeaders []*Header + if err := s.Decode(&uncleHeaders); err != nil { + return err + } -func (block *Block) rlpUncles() interface{} { - // Marshal the transactions of this block - uncles := make([]interface{}, len(block.Uncles)) - for i, uncle := range block.Uncles { - // Cast it to a string (safe) - uncles[i] = uncle.header() + var tdBytes []byte + if err := s.Decode(&tdBytes); err != nil { + // If this block comes from the network that's fine. If loaded from disk it should be there + // Blocks don't store their Td when propagated over the network + } else { + self.Td = ethutil.BigD(tdBytes) } - return uncles -} + if err := s.ListEnd(); err != nil { + return err + } -func (block *Block) SetUncles(uncles []*Block) { - block.Uncles = uncles - block.UncleSha = crypto.Sha3(ethutil.Encode(block.rlpUncles())) -} + self.header = &header + self.uncles = uncleHeaders + self.transactions = transactions -func (self *Block) SetReceipts(receipts Receipts) { - self.receipts = receipts - self.ReceiptSha = DeriveSha(receipts) - self.LogsBloom = CreateBloom(receipts) + return nil } -func (self *Block) SetTransactions(txs Transactions) { - self.transactions = txs - self.TxSha = DeriveSha(txs) +func (self *Block) Header() *Header { + return self.header } -func (block *Block) Value() *ethutil.Value { - return ethutil.NewValue([]interface{}{block.header(), block.transactions, block.rlpUncles()}) +func (self *Block) Uncles() []*Header { + return self.uncles } -func (block *Block) RlpEncode() []byte { - // Encode a slice interface which contains the header and the list of - // transactions. - return block.Value().Encode() +func (self *Block) SetUncles(uncleHeaders []*Header) { + self.uncles = uncleHeaders + self.header.UncleHash = crypto.Sha3(ethutil.Encode(uncleHeaders)) } -func (block *Block) RlpDecode(data []byte) { - rlpValue := ethutil.NewValueFromBytes(data) - block.RlpValueDecode(rlpValue) +func (self *Block) Transactions() Transactions { + return self.transactions } -func (block *Block) RlpValueDecode(decoder *ethutil.Value) { - block.setHeader(decoder.Get(0)) - - // Tx list might be empty if this is an uncle. Uncles only have their - // header set. - if decoder.Get(1).IsNil() == false { // Yes explicitness - //receipts := decoder.Get(1) - //block.receipts = make([]*Receipt, receipts.Len()) - txs := decoder.Get(1) - block.transactions = make(Transactions, txs.Len()) - for i := 0; i < txs.Len(); i++ { - block.transactions[i] = NewTransactionFromValue(txs.Get(i)) - //receipt := NewRecieptFromValue(receipts.Get(i)) - //block.transactions[i] = receipt.Tx - //block.receipts[i] = receipt - } - - } - - if decoder.Get(2).IsNil() == false { // Yes explicitness - uncles := decoder.Get(2) - block.Uncles = make([]*Block, uncles.Len()) - for i := 0; i < uncles.Len(); i++ { - block.Uncles[i] = NewUncleBlockFromValue(uncles.Get(i)) +func (self *Block) Transaction(hash []byte) *Transaction { + for _, transaction := range self.transactions { + if bytes.Equal(hash, transaction.Hash()) { + return transaction } } - + return nil } -func (self *Block) setHeader(header *ethutil.Value) { - self.PrevHash = header.Get(0).Bytes() - self.UncleSha = header.Get(1).Bytes() - self.Coinbase = header.Get(2).Bytes() - self.state = state.New(trie.New(ethutil.Config.Db, header.Get(3).Val)) - self.TxSha = header.Get(4).Bytes() - self.ReceiptSha = header.Get(5).Bytes() - self.LogsBloom = header.Get(6).Bytes() - self.Difficulty = header.Get(7).BigInt() - self.Number = header.Get(8).BigInt() - self.GasLimit = header.Get(9).BigInt() - self.GasUsed = header.Get(10).BigInt() - self.Time = int64(header.Get(11).BigInt().Uint64()) - self.Extra = header.Get(12).Str() - self.Nonce = header.Get(13).Bytes() +func (self *Block) SetTransactions(transactions Transactions) { + self.transactions = transactions + self.header.TxHash = DeriveSha(transactions) } -func NewUncleBlockFromValue(header *ethutil.Value) *Block { - block := &Block{} - block.setHeader(header) - - return block +func (self *Block) Receipts() Receipts { + return self.receipts } -func (block *Block) Trie() *trie.Trie { - return block.state.Trie +func (self *Block) SetReceipts(receipts Receipts) { + self.receipts = receipts + self.header.ReceiptHash = DeriveSha(receipts) + self.header.Bloom = CreateBloom(receipts) } -func (block *Block) Root() interface{} { - return block.state.Root() +func (self *Block) RlpData() interface{} { + return []interface{}{self.header, self.transactions, self.uncles} +} + +func (self *Block) RlpDataForStorage() interface{} { + return []interface{}{self.header, self.transactions, self.uncles, self.Td /* TODO receipts */} +} + +// 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) ParentHash() []byte { return self.header.ParentHash } +func (self *Block) Bloom() []byte { return self.header.Bloom } +func (self *Block) Coinbase() []byte { return self.header.Coinbase } +func (self *Block) Time() int64 { return int64(self.header.Time) } +func (self *Block) GasLimit() *big.Int { return self.header.GasLimit } +func (self *Block) GasUsed() *big.Int { return self.header.GasUsed } +func (self *Block) Hash() []byte { return self.header.Hash() } +func (self *Block) Trie() *trie.Trie { return trie.New(ethutil.Config.Db, self.header.Root) } +func (self *Block) State() *state.StateDB { return state.New(self.Trie()) } +func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) } + +// Implement block.Pow +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 crypto.Sha3(ethutil.Encode(self.header.rlpData(false))) +} + +func (self *Block) String() string { + return fmt.Sprintf(`BLOCK(%x): Size: %v { +%v +%v +%v +} +`, self.header.Hash(), self.Size(), self.header, self.uncles, self.transactions) +} + +func (self *Header) String() string { + return fmt.Sprintf(`ParentHash: %x +UncleHash: %x +Coinbase: %x +Root: %x +TxSha %x +ReceiptSha: %x +Bloom: %x +Difficulty: %v +Number: %v +GasLimit: %v +GasUsed: %v +Time: %v +Extra: %v +Nonce: %x +`, 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, self.Nonce) } -func (block *Block) Diff() *big.Int { - return block.Difficulty -} +type Blocks []*Block -func (self *Block) Receipts() []*Receipt { - return self.receipts -} +type BlockBy func(b1, b2 *Block) bool -func (block *Block) miningHeader() []interface{} { - return []interface{}{ - // Sha of the previous block - block.PrevHash, - // Sha of uncles - block.UncleSha, - // Coinbase address - block.Coinbase, - // root state - block.Root(), - // tx root - block.TxSha, - // Sha of tx - block.ReceiptSha, - // Bloom - block.LogsBloom, - // Current block Difficulty - block.Difficulty, - // The block number - block.Number, - // Block upper gas bound - block.GasLimit, - // Block gas used - block.GasUsed, - // Time the block was found? - block.Time, - // Extra data - block.Extra, +func (self BlockBy) Sort(blocks Blocks) { + bs := blockSorter{ + blocks: blocks, + by: self, } + sort.Sort(bs) } -func (block *Block) header() []interface{} { - return append(block.miningHeader(), block.Nonce) -} - -func (block *Block) String() string { - return fmt.Sprintf(` - BLOCK(%x): Size: %v - PrevHash: %x - UncleSha: %x - Coinbase: %x - Root: %x - TxSha %x - ReceiptSha: %x - Bloom: %x - Difficulty: %v - Number: %v - MaxLimit: %v - GasUsed: %v - Time: %v - Extra: %v - Nonce: %x - NumTx: %v -`, - block.Hash(), - block.Size(), - block.PrevHash, - block.UncleSha, - block.Coinbase, - block.Root(), - block.TxSha, - block.ReceiptSha, - block.LogsBloom, - block.Difficulty, - block.Number, - block.GasLimit, - block.GasUsed, - block.Time, - block.Extra, - block.Nonce, - len(block.transactions), - ) -} - -func (self *Block) Size() ethutil.StorageSize { - return ethutil.StorageSize(len(self.RlpEncode())) +type blockSorter struct { + blocks Blocks + by func(b1, b2 *Block) bool } -// Implement RlpEncodable -func (self *Block) RlpData() interface{} { - return []interface{}{self.header(), self.transactions, self.rlpUncles()} +func (self blockSorter) Len() int { return len(self.blocks) } +func (self blockSorter) Swap(i, j int) { + self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i] } +func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) } -// Implement pow.Block -func (self *Block) N() []byte { return self.Nonce } +func Number(b1, b2 *Block) bool { return b1.Header().Number.Cmp(b2.Header().Number) < 0 } diff --git a/core/types/block_test.go b/core/types/block_test.go new file mode 100644 index 000000000..c85708975 --- /dev/null +++ b/core/types/block_test.go @@ -0,0 +1,23 @@ +package types + +import ( + "bytes" + "testing" + + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/rlp" +) + +func init() { + ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "") + ethutil.Config.Db, _ = ethdb.NewMemDatabase() +} + +func TestNewBlock(t *testing.T) { + block := GenesisBlock() + data := ethutil.Encode(block) + + var genesis Block + err := rlp.Decode(bytes.NewReader(data), &genesis) +} diff --git a/core/types/transaction.go b/core/types/transaction.go index 3a87f7844..c2f7df7a7 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -1,11 +1,13 @@ package types import ( + "bytes" "fmt" "math/big" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/rlp" "github.com/obscuren/secp256k1-go" ) @@ -14,22 +16,22 @@ func IsContractAddr(addr []byte) bool { } type Transaction struct { - nonce uint64 - recipient []byte - value *big.Int - gas *big.Int - gasPrice *big.Int - data []byte - v byte - r, s []byte + AccountNonce uint64 + Recipient []byte + Amount *big.Int + GasAmount *big.Int + Price *big.Int + Payload []byte + V uint64 + R, S []byte } -func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction { - return &Transaction{recipient: nil, value: value, gas: gas, gasPrice: gasPrice, data: script} +func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction { + return NewTransactionMessage(nil, Amount, gasAmount, price, data) } -func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction { - return &Transaction{recipient: to, value: value, gasPrice: gasPrice, gas: gas, data: data} +func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction { + return &Transaction{Recipient: to, Amount: Amount, Price: price, GasAmount: gasAmount, Payload: data} } func NewTransactionFromBytes(data []byte) *Transaction { @@ -39,7 +41,7 @@ func NewTransactionFromBytes(data []byte) *Transaction { return tx } -func NewTransactionFromValue(val *ethutil.Value) *Transaction { +func NewTransactionFromAmount(val *ethutil.Value) *Transaction { tx := &Transaction{} tx.RlpValueDecode(val) @@ -47,33 +49,33 @@ func NewTransactionFromValue(val *ethutil.Value) *Transaction { } func (tx *Transaction) Hash() []byte { - data := []interface{}{tx.nonce, tx.gasPrice, tx.gas, tx.recipient, tx.value, tx.data} + data := []interface{}{tx.AccountNonce, tx.Price, tx.GasAmount, tx.Recipient, tx.Amount, tx.Payload} - return crypto.Sha3(ethutil.NewValue(data).Encode()) + return crypto.Sha3(ethutil.Encode(data)) } func (self *Transaction) Data() []byte { - return self.data + return self.Payload } func (self *Transaction) Gas() *big.Int { - return self.gas + return self.GasAmount } func (self *Transaction) GasPrice() *big.Int { - return self.gasPrice + return self.Price } func (self *Transaction) Value() *big.Int { - return self.value + return self.Amount } func (self *Transaction) Nonce() uint64 { - return self.nonce + return self.AccountNonce } -func (self *Transaction) SetNonce(nonce uint64) { - self.nonce = nonce +func (self *Transaction) SetNonce(AccountNonce uint64) { + self.AccountNonce = AccountNonce } func (self *Transaction) From() []byte { @@ -81,13 +83,13 @@ func (self *Transaction) From() []byte { } func (self *Transaction) To() []byte { - return self.recipient + return self.Recipient } func (tx *Transaction) Curve() (v byte, r []byte, s []byte) { - v = tx.v - r = ethutil.LeftPadBytes(tx.r, 32) - s = ethutil.LeftPadBytes(tx.s, 32) + v = byte(tx.V) + r = ethutil.LeftPadBytes(tx.R, 32) + s = ethutil.LeftPadBytes(tx.S, 32) return } @@ -130,42 +132,37 @@ func (tx *Transaction) Sign(privk []byte) error { sig := tx.Signature(privk) - tx.r = sig[:32] - tx.s = sig[32:64] - tx.v = sig[64] + 27 + tx.R = sig[:32] + tx.S = sig[32:64] + tx.V = uint64(sig[64] + 27) return nil } func (tx *Transaction) RlpData() interface{} { - data := []interface{}{tx.nonce, tx.gasPrice, tx.gas, tx.recipient, tx.value, tx.data} + data := []interface{}{tx.AccountNonce, tx.Price, tx.GasAmount, tx.Recipient, tx.Amount, tx.Payload} - return append(data, tx.v, new(big.Int).SetBytes(tx.r).Bytes(), new(big.Int).SetBytes(tx.s).Bytes()) -} - -func (tx *Transaction) RlpValue() *ethutil.Value { - return ethutil.NewValue(tx.RlpData()) + return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes()) } func (tx *Transaction) RlpEncode() []byte { - return tx.RlpValue().Encode() + return ethutil.Encode(tx) } func (tx *Transaction) RlpDecode(data []byte) { - tx.RlpValueDecode(ethutil.NewValueFromBytes(data)) + rlp.Decode(bytes.NewReader(data), tx) } func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) { - tx.nonce = decoder.Get(0).Uint() - tx.gasPrice = decoder.Get(1).BigInt() - tx.gas = decoder.Get(2).BigInt() - tx.recipient = decoder.Get(3).Bytes() - tx.value = decoder.Get(4).BigInt() - tx.data = decoder.Get(5).Bytes() - tx.v = byte(decoder.Get(6).Uint()) - - tx.r = decoder.Get(7).Bytes() - tx.s = decoder.Get(8).Bytes() + tx.AccountNonce = decoder.Get(0).Uint() + tx.Price = decoder.Get(1).BigInt() + tx.GasAmount = decoder.Get(2).BigInt() + tx.Recipient = decoder.Get(3).Bytes() + tx.Amount = decoder.Get(4).BigInt() + tx.Payload = decoder.Get(5).Bytes() + tx.V = decoder.Get(6).Uint() + tx.R = decoder.Get(7).Bytes() + tx.S = decoder.Get(8).Bytes() } func (tx *Transaction) String() string { @@ -174,10 +171,10 @@ func (tx *Transaction) String() string { Contract: %v From: %x To: %x - Nonce: %v - GasPrice: %v - Gas: %v - Value: %v + AccountNonce: %v + GasAmountPrice: %v + GasAmount: %v + Amount: %v Data: 0x%x V: 0x%x R: 0x%x @@ -185,17 +182,17 @@ func (tx *Transaction) String() string { Hex: %x `, tx.Hash(), - len(tx.recipient) == 0, + len(tx.Recipient) == 0, tx.From(), - tx.recipient, - tx.nonce, - tx.gasPrice, - tx.gas, - tx.value, - tx.data, - tx.v, - tx.r, - tx.s, + tx.Recipient, + tx.AccountNonce, + tx.Price, + tx.GasAmount, + tx.Amount, + tx.Payload, + tx.V, + tx.R, + tx.S, ethutil.Encode(tx), ) } @@ -220,5 +217,5 @@ func (s Transactions) GetRlp(i int) []byte { return ethutil.Rlp(s[i]) } type TxByNonce struct{ Transactions } func (s TxByNonce) Less(i, j int) bool { - return s.Transactions[i].nonce < s.Transactions[j].nonce + return s.Transactions[i].AccountNonce < s.Transactions[j].AccountNonce } diff --git a/core/vm_env.go b/core/vm_env.go index ad63ecf9c..209115eab 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -24,15 +24,15 @@ func NewEnv(state *state.StateDB, msg Message, block *types.Block) *VMEnv { } func (self *VMEnv) Origin() []byte { return self.msg.From() } -func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number } -func (self *VMEnv) PrevHash() []byte { return self.block.PrevHash } -func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase } -func (self *VMEnv) Time() int64 { return self.block.Time } -func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty } +func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() } +func (self *VMEnv) PrevHash() []byte { return self.block.ParentHash() } +func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase() } +func (self *VMEnv) Time() int64 { return self.block.Time() } +func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() } func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } +func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() } func (self *VMEnv) Value() *big.Int { return self.msg.Value() } func (self *VMEnv) State() *state.StateDB { return self.state } -func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } func (self *VMEnv) Depth() int { return self.depth } func (self *VMEnv) SetDepth(i int) { self.depth = i } func (self *VMEnv) AddLog(log state.Log) { -- cgit v1.2.3 From 9e5257b83b8572077b9c26e4ae9a9443f765bf6e Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Dec 2014 14:33:15 +0100 Subject: Chain importer --- core/block_manager.go | 4 ++-- core/chain_manager.go | 5 ----- core/types/block.go | 36 +++++++++++++++++++++--------------- core/types/transaction.go | 28 ++++++++++++++-------------- 4 files changed, 37 insertions(+), 36 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index c61cf6504..ef2113bfb 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -304,7 +304,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent knownUncles := set.New() for _, uncle := range parent.Uncles() { - knownUncles.Add(uncle.Hash()) + knownUncles.Add(string(uncle.Hash())) } nonces := ethutil.NewSet(block.Header().Nonce) @@ -323,7 +323,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent return UncleError("Uncle too old") } - if knownUncles.Has(uncle.Hash()) { + if knownUncles.Has(string(uncle.Hash())) { return UncleError("Uncle in chain") } diff --git a/core/chain_manager.go b/core/chain_manager.go index e35c4aa3a..fe687e501 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -204,9 +204,6 @@ func (bc *ChainManager) Reset() { bc.currentBlock = bc.genesisBlock bc.setTotalDifficulty(ethutil.Big("0")) - - // Set the last know difficulty (might be 0x0 as initial value, Genesis) - bc.td = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) } func (self *ChainManager) Export() []byte { @@ -219,9 +216,7 @@ func (self *ChainManager) Export() []byte { for block := self.currentBlock; block != nil; block = self.GetBlock(block.Header().ParentHash) { blocks[block.NumberU64()] = block } - //fmt.Println(blocks) - return nil return ethutil.Encode(blocks) } diff --git a/core/types/block.go b/core/types/block.go index b0fcbdd9b..940f2402e 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -209,28 +209,34 @@ func (self *Block) HashNoNonce() []byte { func (self *Block) String() string { return fmt.Sprintf(`BLOCK(%x): Size: %v { +Header: +[ %v +] +Transactions: %v +Uncles: %v } -`, self.header.Hash(), self.Size(), self.header, self.uncles, self.transactions) +`, self.header.Hash(), self.Size(), self.header, self.transactions, self.uncles) } func (self *Header) String() string { - return fmt.Sprintf(`ParentHash: %x -UncleHash: %x -Coinbase: %x -Root: %x -TxSha %x -ReceiptSha: %x -Bloom: %x -Difficulty: %v -Number: %v -GasLimit: %v -GasUsed: %v -Time: %v -Extra: %v -Nonce: %x + return fmt.Sprintf(` + ParentHash: %x + UncleHash: %x + Coinbase: %x + Root: %x + TxSha %x + ReceiptSha: %x + Bloom: %x + Difficulty: %v + Number: %v + GasLimit: %v + GasUsed: %v + Time: %v + Extra: %v + Nonce: %x `, 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, self.Nonce) } diff --git a/core/types/transaction.go b/core/types/transaction.go index c2f7df7a7..59244adc3 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -17,10 +17,10 @@ func IsContractAddr(addr []byte) bool { type Transaction struct { AccountNonce uint64 + Price *big.Int + GasLimit *big.Int Recipient []byte Amount *big.Int - GasAmount *big.Int - Price *big.Int Payload []byte V uint64 R, S []byte @@ -31,7 +31,7 @@ func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Tran } func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction { - return &Transaction{Recipient: to, Amount: Amount, Price: price, GasAmount: gasAmount, Payload: data} + return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data} } func NewTransactionFromBytes(data []byte) *Transaction { @@ -49,7 +49,7 @@ func NewTransactionFromAmount(val *ethutil.Value) *Transaction { } func (tx *Transaction) Hash() []byte { - data := []interface{}{tx.AccountNonce, tx.Price, tx.GasAmount, tx.Recipient, tx.Amount, tx.Payload} + data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload} return crypto.Sha3(ethutil.Encode(data)) } @@ -59,7 +59,7 @@ func (self *Transaction) Data() []byte { } func (self *Transaction) Gas() *big.Int { - return self.GasAmount + return self.GasLimit } func (self *Transaction) GasPrice() *big.Int { @@ -140,7 +140,7 @@ func (tx *Transaction) Sign(privk []byte) error { } func (tx *Transaction) RlpData() interface{} { - data := []interface{}{tx.AccountNonce, tx.Price, tx.GasAmount, tx.Recipient, tx.Amount, tx.Payload} + data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload} return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes()) } @@ -156,7 +156,7 @@ func (tx *Transaction) RlpDecode(data []byte) { func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) { tx.AccountNonce = decoder.Get(0).Uint() tx.Price = decoder.Get(1).BigInt() - tx.GasAmount = decoder.Get(2).BigInt() + tx.GasLimit = decoder.Get(2).BigInt() tx.Recipient = decoder.Get(3).Bytes() tx.Amount = decoder.Get(4).BigInt() tx.Payload = decoder.Get(5).Bytes() @@ -171,23 +171,23 @@ func (tx *Transaction) String() string { Contract: %v From: %x To: %x - AccountNonce: %v - GasAmountPrice: %v - GasAmount: %v - Amount: %v + Nonce: %v + GasPrice: %v + GasLimit %v + Value: %v Data: 0x%x V: 0x%x R: 0x%x S: 0x%x Hex: %x - `, +`, tx.Hash(), len(tx.Recipient) == 0, tx.From(), - tx.Recipient, + tx.To(), tx.AccountNonce, tx.Price, - tx.GasAmount, + tx.GasLimit, tx.Amount, tx.Payload, tx.V, -- cgit v1.2.3 From 1382e8d84bf039e8f5588f07029255d0f901cf44 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Dec 2014 14:50:04 +0100 Subject: Delete current chain for reset --- core/chain_manager.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index fe687e501..485c195d5 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -198,6 +198,10 @@ func (bc *ChainManager) Reset() { bc.mu.Lock() defer bc.mu.Unlock() + for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.Header().ParentHash) { + ethutil.Config.Db.Delete(block.Hash()) + } + // Prepare the genesis block bc.write(bc.genesisBlock) bc.insert(bc.genesisBlock) -- cgit v1.2.3 From 780abaec988df302e0c98f1a35e9af35b5623746 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Dec 2014 18:35:36 +0100 Subject: Switched to new trie --- core/block_manager.go | 1 + core/genesis.go | 2 +- core/types/block.go | 4 ++-- core/types/derive_sha.go | 8 ++++---- 4 files changed, 8 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index ef2113bfb..1b9da1269 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -217,6 +217,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I receiptSha := types.DeriveSha(receipts) if bytes.Compare(receiptSha, header.ReceiptHash) != 0 { + fmt.Println("receipts", receipts) err = fmt.Errorf("validating receipt root. received=%x got=%x", header.ReceiptHash, receiptSha) return } diff --git a/core/genesis.go b/core/genesis.go index 51afa314e..10b40516f 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -20,7 +20,7 @@ var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{})) var EmptyListRoot = crypto.Sha3(ethutil.Encode("")) func GenesisBlock() *types.Block { - genesis := types.NewBlock(ZeroHash256, ZeroHash160, EmptyListRoot, big.NewInt(131072), crypto.Sha3(big.NewInt(42).Bytes()), "") + genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(131072), crypto.Sha3(big.NewInt(42).Bytes()), "") genesis.Header().Number = ethutil.Big0 genesis.Header().GasLimit = big.NewInt(1000000) genesis.Header().GasUsed = ethutil.Big0 diff --git a/core/types/block.go b/core/types/block.go index 940f2402e..054767d67 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -9,9 +9,9 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/ptrie" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/trie" ) type Header struct { @@ -196,7 +196,7 @@ func (self *Block) Time() int64 { return int64(self.header.Time) } func (self *Block) GasLimit() *big.Int { return self.header.GasLimit } func (self *Block) GasUsed() *big.Int { return self.header.GasUsed } func (self *Block) Hash() []byte { return self.header.Hash() } -func (self *Block) Trie() *trie.Trie { return trie.New(ethutil.Config.Db, self.header.Root) } +func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) } func (self *Block) State() *state.StateDB { return state.New(self.Trie()) } func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) } diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go index 1897ff198..0beb19670 100644 --- a/core/types/derive_sha.go +++ b/core/types/derive_sha.go @@ -2,7 +2,7 @@ package types import ( "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/ptrie" ) type DerivableList interface { @@ -11,10 +11,10 @@ type DerivableList interface { } func DeriveSha(list DerivableList) []byte { - trie := trie.New(ethutil.Config.Db, "") + trie := ptrie.New(nil, ethutil.Config.Db) for i := 0; i < list.Len(); i++ { - trie.Update(string(ethutil.NewValue(i).Encode()), string(list.GetRlp(i))) + trie.Update(ethutil.Encode(i), list.GetRlp(i)) } - return trie.GetRoot() + return trie.Root() } -- cgit v1.2.3 From fb1edd05f40bad04f2514d1463b5593dc51e9f77 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 24 Dec 2014 11:20:43 +0100 Subject: Removed the deferred reset --- core/block_manager.go | 6 ------ 1 file changed, 6 deletions(-) (limited to 'core') diff --git a/core/block_manager.go b/core/block_manager.go index 1b9da1269..8a5455306 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -185,12 +185,6 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I state := state.New(parent.Trie().Copy()) - // Defer the Undo on the Trie. If the block processing happened - // we don't want to undo but since undo only happens on dirty - // nodes this won't happen because Commit would have been called - // before that. - defer state.Reset() - // Block validation if err = sm.ValidateBlock(block, parent); err != nil { return -- cgit v1.2.3 From 2f8a45cd8b2f565359f2c955145047acca2a2433 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 30 Dec 2014 13:32:01 +0100 Subject: Fixed chain test & added new chain --- core/chain_manager_test.go | 47 ++++++++++++++++++++++++++++++++-------------- core/helper_test.go | 6 +++--- core/types/block.go | 1 + 3 files changed, 37 insertions(+), 17 deletions(-) (limited to 'core') diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index 52be8b0ea..6e85bae9a 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -2,7 +2,9 @@ package core import ( "fmt" + "os" "path" + "reflect" "runtime" "testing" @@ -10,6 +12,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/rlp" //logpkg "github.com/ethereum/go-ethereum/logger" ) @@ -30,20 +33,19 @@ func init() { ethutil.Config.Db = db } -func loadChain(fn string, t *testing.T) types.Blocks { - c1, err := ethutil.ReadAllFile(path.Join("..", "_data", fn)) +func loadChain(fn string, t *testing.T) (types.Blocks, error) { + fh, err := os.OpenFile(path.Join("..", "_data", fn), os.O_RDONLY, os.ModePerm) if err != nil { - fmt.Println(err) - t.FailNow() + return nil, err } - value := ethutil.NewValueFromBytes([]byte(c1)) - blocks := make(types.Blocks, value.Len()) - it := value.NewIterator() - for it.Next() { - blocks[it.Idx()] = types.NewBlockFromRlpValue(it.Value()) + defer fh.Close() + + var chain types.Blocks + if err := rlp.Decode(fh, &chain); err != nil { + return nil, err } - return blocks + return chain, nil } func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *testing.T) { @@ -56,11 +58,21 @@ func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t * } func TestChainInsertions(t *testing.T) { - chain1 := loadChain("chain1", t) - chain2 := loadChain("chain2", t) + chain1, err := loadChain("chain1", t) + if err != nil { + fmt.Println(err) + t.FailNow() + } + + chain2, err := loadChain("chain2", t) + if err != nil { + fmt.Println(err) + t.FailNow() + } + var eventMux event.TypeMux chainMan := NewChainManager(&eventMux) - txPool := NewTxPool(chainMan, nil, &eventMux) + txPool := NewTxPool(chainMan, &eventMux) blockMan := NewBlockManager(txPool, chainMan, &eventMux) chainMan.SetProcessor(blockMan) @@ -73,5 +85,12 @@ func TestChainInsertions(t *testing.T) { for i := 0; i < max; i++ { <-done } - fmt.Println(chainMan.CurrentBlock()) + + if reflect.DeepEqual(chain2[len(chain2)-1], chainMan.CurrentBlock()) { + t.Error("chain2 is canonical and shouldn't be") + } + + if !reflect.DeepEqual(chain1[len(chain1)-1], chainMan.CurrentBlock()) { + t.Error("chain1 isn't canonical and should be") + } } diff --git a/core/helper_test.go b/core/helper_test.go index b340144fd..b8bf254d7 100644 --- a/core/helper_test.go +++ b/core/helper_test.go @@ -9,7 +9,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/wire" + "github.com/ethereum/go-ethereum/p2p" ) // Implement our EthTest Manager @@ -54,11 +54,11 @@ func (tm *TestManager) TxPool() *TxPool { func (tm *TestManager) EventMux() *event.TypeMux { return tm.eventMux } -func (tm *TestManager) Broadcast(msgType wire.MsgType, data []interface{}) { +func (tm *TestManager) Broadcast(msgType p2p.Msg, data []interface{}) { fmt.Println("Broadcast not implemented") } -func (tm *TestManager) ClientIdentity() wire.ClientIdentity { +func (tm *TestManager) ClientIdentity() p2p.ClientIdentity { return nil } func (tm *TestManager) KeyManager() *crypto.KeyManager { diff --git a/core/types/block.go b/core/types/block.go index 054767d67..7b4695f73 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -199,6 +199,7 @@ func (self *Block) Hash() []byte { return self.header.Hash() } func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) } func (self *Block) State() *state.StateDB { return state.New(self.Trie()) } func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) } +func (self *Block) SetRoot(root []byte) { self.header.Root = root } // Implement block.Pow func (self *Block) Difficulty() *big.Int { return self.header.Difficulty } -- cgit v1.2.3 From 8df689bd448390c44ee2c344314257a5987a2e97 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 30 Dec 2014 15:42:26 +0100 Subject: Chain tests --- core/chain_manager_test.go | 53 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index 6e85bae9a..108718901 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -6,6 +6,7 @@ import ( "path" "reflect" "runtime" + "strconv" "testing" "github.com/ethereum/go-ethereum/core/types" @@ -13,15 +14,15 @@ import ( "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/rlp" - //logpkg "github.com/ethereum/go-ethereum/logger" ) //var Logger logpkg.LogSystem + //var Log = logpkg.NewLogger("TEST") func init() { runtime.GOMAXPROCS(runtime.NumCPU()) - //Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.InfoLevel) + //Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.DebugLevel) //logpkg.AddLogSystem(Logger) ethutil.ReadConfig("/tmp/ethtest", "/tmp/ethtest", "ETH") @@ -50,21 +51,22 @@ func loadChain(fn string, t *testing.T) (types.Blocks, error) { func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *testing.T) { err := chainMan.InsertChain(chain) + done <- true if err != nil { fmt.Println(err) t.FailNow() } - done <- true } func TestChainInsertions(t *testing.T) { - chain1, err := loadChain("chain1", t) + chain1, err := loadChain("valid1", t) if err != nil { fmt.Println(err) t.FailNow() } + fmt.Println(len(chain1)) - chain2, err := loadChain("chain2", t) + chain2, err := loadChain("valid2", t) if err != nil { fmt.Println(err) t.FailNow() @@ -94,3 +96,44 @@ func TestChainInsertions(t *testing.T) { t.Error("chain1 isn't canonical and should be") } } + +func TestChainMultipleInsertions(t *testing.T) { + const max = 4 + chains := make([]types.Blocks, max) + var longest int + for i := 0; i < max; i++ { + var err error + name := "valid" + strconv.Itoa(i+1) + chains[i], err = loadChain(name, t) + if len(chains[i]) >= len(chains[longest]) { + longest = i + } + fmt.Println("loaded", name, "with a length of", len(chains[i])) + if err != nil { + fmt.Println(err) + t.FailNow() + } + } + + var eventMux event.TypeMux + chainMan := NewChainManager(&eventMux) + txPool := NewTxPool(chainMan, &eventMux) + blockMan := NewBlockManager(txPool, chainMan, &eventMux) + chainMan.SetProcessor(blockMan) + done := make(chan bool, max) + for i, chain := range chains { + var i int = i + go func() { + insertChain(done, chainMan, chain, t) + fmt.Println(i, "done") + }() + } + + for i := 0; i < max; i++ { + <-done + } + + if !reflect.DeepEqual(chains[longest][len(chains[longest])-1], chainMan.CurrentBlock()) { + t.Error("Invalid canonical chain") + } +} -- cgit v1.2.3