From 4908bf64874aca2890a65a2330e57a54dcfa8c4e Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 16 Dec 2015 04:26:23 +0100 Subject: [PATCH 1/2] core: create a header chain structure shared by core.BlockChain and light.LightChain --- core/blockchain.go | 430 +++++++++++----------------------------- core/blockchain_test.go | 9 +- core/headerchain.go | 425 +++++++++++++++++++++++++++++++++++++++ core/types.go | 12 +- 4 files changed, 557 insertions(+), 319 deletions(-) create mode 100644 core/headerchain.go diff --git a/core/blockchain.go b/core/blockchain.go index 2c6ff24f97..2a9749f5fb 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -18,11 +18,9 @@ package core import ( - crand "crypto/rand" "errors" "fmt" "io" - "math" "math/big" mrand "math/rand" "runtime" @@ -82,24 +80,22 @@ const ( // included in the canonical one where as GetBlockByNumber always represents the // canonical chain. type BlockChain struct { + hc *HeaderChain chainDb ethdb.Database eventMux *event.TypeMux genesisBlock *types.Block - // Last known total difficulty + mu sync.RWMutex chainmu sync.RWMutex tsmu sync.RWMutex procmu sync.RWMutex - checkpoint int // checkpoint counts towards the new checkpoint - currentHeader *types.Header // Current head of the header chain (may be above the block chain!) - currentBlock *types.Block // Current head of the block chain - currentFastBlock *types.Block // Current head of the fast-sync chain (may be above the block chain!) + checkpoint int // checkpoint counts towards the new checkpoint + currentBlock *types.Block // Current head of the block chain + currentFastBlock *types.Block // Current head of the fast-sync chain (may be above the block chain!) - headerCache *lru.Cache // Cache for the most recent block headers bodyCache *lru.Cache // Cache for the most recent block bodies bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format - tdCache *lru.Cache // Cache for the most recent block total difficulties blockCache *lru.Cache // Cache for the most recent entire blocks futureBlocks *lru.Cache // future blocks are blocks added for later processing @@ -110,7 +106,6 @@ type BlockChain struct { wg sync.WaitGroup pow pow.PoW - rand *mrand.Rand processor Processor validator Validator } @@ -119,10 +114,8 @@ type BlockChain struct { // available in the database. It initialiser the default Ethereum Validator and // Processor. func NewBlockChain(chainDb ethdb.Database, pow pow.PoW, mux *event.TypeMux) (*BlockChain, error) { - headerCache, _ := lru.New(headerCacheLimit) bodyCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit) - tdCache, _ := lru.New(tdCacheLimit) blockCache, _ := lru.New(blockCacheLimit) futureBlocks, _ := lru.New(maxFutureBlocks) @@ -130,22 +123,21 @@ func NewBlockChain(chainDb ethdb.Database, pow pow.PoW, mux *event.TypeMux) (*Bl chainDb: chainDb, eventMux: mux, quit: make(chan struct{}), - headerCache: headerCache, bodyCache: bodyCache, bodyRLPCache: bodyRLPCache, - tdCache: tdCache, blockCache: blockCache, futureBlocks: futureBlocks, pow: pow, } - // Seed a fast but crypto originating random generator - seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) + bc.SetValidator(NewBlockValidator(bc, pow)) + bc.SetProcessor(NewStateProcessor(bc)) + + gv := func() HeaderValidator { return bc.Validator() } + var err error + bc.hc, err = NewHeaderChain(chainDb, gv, bc.getProcInterrupt) if err != nil { return nil, err } - bc.rand = mrand.New(mrand.NewSource(seed.Int64())) - bc.SetValidator(NewBlockValidator(bc, pow)) - bc.SetProcessor(NewStateProcessor(bc)) bc.genesisBlock = bc.GetBlockByNumber(0) if bc.genesisBlock == nil { @@ -171,6 +163,10 @@ func NewBlockChain(chainDb ethdb.Database, pow pow.PoW, mux *event.TypeMux) (*Bl return bc, nil } +func (self *BlockChain) getProcInterrupt() bool { + return atomic.LoadInt32(&self.procInterrupt) == 1 +} + // loadLastState loads the last known chain state from the database. This method // assumes that the chain manager mutex is held. func (self *BlockChain) loadLastState() error { @@ -189,12 +185,13 @@ func (self *BlockChain) loadLastState() error { } } // Restore the last known head header - self.currentHeader = self.currentBlock.Header() + currentHeader := self.currentBlock.Header() if head := GetHeadHeaderHash(self.chainDb); head != (common.Hash{}) { if header := self.GetHeader(head); header != nil { - self.currentHeader = header + currentHeader = header } } + self.hc.SetCurrentHeader(currentHeader) // Restore the last known head fast block self.currentFastBlock = self.currentBlock if head := GetHeadFastBlockHash(self.chainDb); head != (common.Hash{}) { @@ -203,11 +200,11 @@ func (self *BlockChain) loadLastState() error { } } // Issue a status log and return - headerTd := self.GetTd(self.currentHeader.Hash()) + headerTd := self.GetTd(self.hc.CurrentHeader().Hash()) blockTd := self.GetTd(self.currentBlock.Hash()) fastTd := self.GetTd(self.currentFastBlock.Hash()) - glog.V(logger.Info).Infof("Last header: #%d [%x…] TD=%v", self.currentHeader.Number, self.currentHeader.Hash().Bytes()[:4], headerTd) + glog.V(logger.Info).Infof("Last header: #%d [%x…] TD=%v", self.hc.CurrentHeader().Number, self.hc.CurrentHeader().Hash().Bytes()[:4], headerTd) glog.V(logger.Info).Infof("Last block: #%d [%x…] TD=%v", self.currentBlock.Number(), self.currentBlock.Hash().Bytes()[:4], blockTd) glog.V(logger.Info).Infof("Fast block: #%d [%x…] TD=%v", self.currentFastBlock.Number(), self.currentFastBlock.Hash().Bytes()[:4], fastTd) @@ -222,13 +219,10 @@ func (bc *BlockChain) SetHead(head uint64) { bc.mu.Lock() defer bc.mu.Unlock() + bc.hc.SetHead(head) + // Figure out the highest known canonical headers and/or blocks height := uint64(0) - if bc.currentHeader != nil { - if hh := bc.currentHeader.Number.Uint64(); hh > height { - height = hh - } - } if bc.currentBlock != nil { if bh := bc.currentBlock.NumberU64(); bh > height { height = bh @@ -242,10 +236,6 @@ func (bc *BlockChain) SetHead(head uint64) { // Gather all the hashes that need deletion drop := make(map[common.Hash]struct{}) - for bc.currentHeader != nil && bc.currentHeader.Number.Uint64() > head { - drop[bc.currentHeader.Hash()] = struct{}{} - bc.currentHeader = bc.GetHeader(bc.currentHeader.ParentHash) - } for bc.currentBlock != nil && bc.currentBlock.NumberU64() > head { drop[bc.currentBlock.Hash()] = struct{}{} bc.currentBlock = bc.GetBlock(bc.currentBlock.ParentHash()) @@ -254,18 +244,12 @@ func (bc *BlockChain) SetHead(head uint64) { drop[bc.currentFastBlock.Hash()] = struct{}{} bc.currentFastBlock = bc.GetBlock(bc.currentFastBlock.ParentHash()) } - // Roll back the canonical chain numbering - for i := height; i > head; i-- { - DeleteCanonicalHash(bc.chainDb, i) - } // Delete everything found by the above rewind for hash, _ := range drop { - DeleteHeader(bc.chainDb, hash) DeleteBody(bc.chainDb, hash) DeleteTd(bc.chainDb, hash) } // Clear out any stale content from the caches - bc.headerCache.Purge() bc.bodyCache.Purge() bc.bodyRLPCache.Purge() bc.blockCache.Purge() @@ -275,18 +259,12 @@ func (bc *BlockChain) SetHead(head uint64) { if bc.currentBlock == nil { bc.currentBlock = bc.genesisBlock } - if bc.currentHeader == nil { - bc.currentHeader = bc.genesisBlock.Header() - } if bc.currentFastBlock == nil { bc.currentFastBlock = bc.genesisBlock } if err := WriteHeadBlockHash(bc.chainDb, bc.currentBlock.Hash()); err != nil { glog.Fatalf("failed to reset head block hash: %v", err) } - if err := WriteHeadHeaderHash(bc.chainDb, bc.currentHeader.Hash()); err != nil { - glog.Fatalf("failed to reset head header hash: %v", err) - } if err := WriteHeadFastBlockHash(bc.chainDb, bc.currentFastBlock.Hash()); err != nil { glog.Fatalf("failed to reset head fast block hash: %v", err) } @@ -329,15 +307,6 @@ func (self *BlockChain) LastBlockHash() common.Hash { return self.currentBlock.Hash() } -// CurrentHeader retrieves the current head header of the canonical chain. The -// header is retrieved from the blockchain's internal cache. -func (self *BlockChain) CurrentHeader() *types.Header { - self.mu.RLock() - defer self.mu.RUnlock() - - return self.currentHeader -} - // CurrentBlock retrieves the current head block of the canonical chain. The // block is retrieved from the blockchain's internal cache. func (self *BlockChain) CurrentBlock() *types.Block { @@ -425,7 +394,8 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) { bc.genesisBlock = genesis bc.insert(bc.genesisBlock) bc.currentBlock = bc.genesisBlock - bc.currentHeader = bc.genesisBlock.Header() + bc.hc.SetGenesis(bc.genesisBlock.Header()) + bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) bc.currentFastBlock = bc.genesisBlock } @@ -483,10 +453,7 @@ func (bc *BlockChain) insert(block *types.Block) { // If the block is better than out head or is on a different chain, force update heads if updateHeads { - if err := WriteHeadHeaderHash(bc.chainDb, block.Hash()); err != nil { - glog.Fatalf("failed to insert head header hash: %v", err) - } - bc.currentHeader = block.Header() + bc.hc.SetCurrentHeader(block.Header()) if err := WriteHeadFastBlockHash(bc.chainDb, block.Hash()); err != nil { glog.Fatalf("failed to insert head fast block hash: %v", err) @@ -500,38 +467,6 @@ func (bc *BlockChain) Genesis() *types.Block { return bc.genesisBlock } -// HasHeader checks if a block header is present in the database or not, caching -// it if present. -func (bc *BlockChain) HasHeader(hash common.Hash) bool { - return bc.GetHeader(hash) != nil -} - -// GetHeader retrieves a block header from the database by hash, caching it if -// found. -func (self *BlockChain) GetHeader(hash common.Hash) *types.Header { - // Short circuit if the header's already in the cache, retrieve otherwise - if header, ok := self.headerCache.Get(hash); ok { - return header.(*types.Header) - } - header := GetHeader(self.chainDb, hash) - if header == nil { - return nil - } - // Cache the found header for next time and return - self.headerCache.Add(header.Hash(), header) - return header -} - -// GetHeaderByNumber retrieves a block header from the database by number, -// caching it (associated with its hash) if found. -func (self *BlockChain) GetHeaderByNumber(number uint64) *types.Header { - hash := GetCanonicalHash(self.chainDb, number) - if hash == (common.Hash{}) { - return nil - } - return self.GetHeader(hash) -} - // GetBody retrieves a block body (transactions and uncles) from the database by // hash, caching it if found. func (self *BlockChain) GetBody(hash common.Hash) *types.Body { @@ -565,22 +500,6 @@ func (self *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue { return body } -// GetTd retrieves a block's total difficulty in the canonical chain from the -// database by hash, caching it if found. -func (self *BlockChain) GetTd(hash common.Hash) *big.Int { - // Short circuit if the td's already in the cache, retrieve otherwise - if cached, ok := self.tdCache.Get(hash); ok { - return cached.(*big.Int) - } - td := GetTd(self.chainDb, hash) - if td == nil { - return nil - } - // Cache the found body for next time and return - self.tdCache.Add(hash, td) - return td -} - // HasBlock checks if a block is fully present in the database or not, caching // it if present. func (bc *BlockChain) HasBlock(hash common.Hash) bool { @@ -625,28 +544,6 @@ func (self *BlockChain) GetBlockByNumber(number uint64) *types.Block { return self.GetBlock(hash) } -// GetBlockHashesFromHash retrieves a number of block hashes starting at a given -// hash, fetching towards the genesis block. -func (self *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { - // Get the origin header from which to fetch - header := self.GetHeader(hash) - if header == nil { - return nil - } - // Iterate the headers until enough is collected or the genesis reached - chain := make([]common.Hash, 0, max) - for i := uint64(0); i < max; i++ { - if header = self.GetHeader(header.ParentHash); header == nil { - break - } - chain = append(chain, header.Hash()) - if header.Number.Cmp(common.Big0) == 0 { - break - } - } - return chain -} - // [deprecated by eth/62] // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors. func (self *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) { @@ -698,195 +595,15 @@ func (self *BlockChain) procFutureBlocks() { } } -type writeStatus byte +type WriteStatus byte const ( - NonStatTy writeStatus = iota + NonStatTy WriteStatus = iota CanonStatTy SplitStatTy SideStatTy ) -// writeHeader writes a header into the local chain, given that its parent is -// already known. If the total difficulty of the newly inserted header becomes -// greater than the current known TD, the canonical chain is re-routed. -// -// Note: This method is not concurrent-safe with inserting blocks simultaneously -// into the chain, as side effects caused by reorganisations cannot be emulated -// without the real blocks. Hence, writing headers directly should only be done -// in two scenarios: pure-header mode of operation (light clients), or properly -// separated header/block phases (non-archive clients). -func (self *BlockChain) writeHeader(header *types.Header) error { - self.wg.Add(1) - defer self.wg.Done() - - // Calculate the total difficulty of the header - ptd := self.GetTd(header.ParentHash) - if ptd == nil { - return ParentError(header.ParentHash) - } - - localTd := self.GetTd(self.currentHeader.Hash()) - externTd := new(big.Int).Add(header.Difficulty, ptd) - - // Make sure no inconsistent state is leaked during insertion - self.mu.Lock() - defer self.mu.Unlock() - - // If the total difficulty is higher than our known, add it to the canonical chain - // Second clause in the if statement reduces the vulnerability to selfish mining. - // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf - if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { - // Delete any canonical number assignments above the new head - for i := header.Number.Uint64() + 1; GetCanonicalHash(self.chainDb, i) != (common.Hash{}); i++ { - DeleteCanonicalHash(self.chainDb, i) - } - // Overwrite any stale canonical number assignments - head := self.GetHeader(header.ParentHash) - for GetCanonicalHash(self.chainDb, head.Number.Uint64()) != head.Hash() { - WriteCanonicalHash(self.chainDb, head.Hash(), head.Number.Uint64()) - head = self.GetHeader(head.ParentHash) - } - // Extend the canonical chain with the new header - if err := WriteCanonicalHash(self.chainDb, header.Hash(), header.Number.Uint64()); err != nil { - glog.Fatalf("failed to insert header number: %v", err) - } - if err := WriteHeadHeaderHash(self.chainDb, header.Hash()); err != nil { - glog.Fatalf("failed to insert head header hash: %v", err) - } - self.currentHeader = types.CopyHeader(header) - } - // Irrelevant of the canonical status, write the header itself to the database - if err := WriteTd(self.chainDb, header.Hash(), externTd); err != nil { - glog.Fatalf("failed to write header total difficulty: %v", err) - } - if err := WriteHeader(self.chainDb, header); err != nil { - glog.Fatalf("filed to write header contents: %v", err) - } - return nil -} - -// InsertHeaderChain attempts to insert the given header chain in to the local -// chain, possibly creating a reorg. If an error is returned, it will return the -// index number of the failing header as well an error describing what went wrong. -// -// The verify parameter can be used to fine tune whether nonce verification -// should be done or not. The reason behind the optional check is because some -// of the header retrieval mechanisms already need to verfy nonces, as well as -// because nonces can be verified sparsely, not needing to check each. -func (self *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { - self.wg.Add(1) - defer self.wg.Done() - - // Make sure only one thread manipulates the chain at once - self.chainmu.Lock() - defer self.chainmu.Unlock() - - // Collect some import statistics to report on - stats := struct{ processed, ignored int }{} - start := time.Now() - - // Generate the list of headers that should be POW verified - verify := make([]bool, len(chain)) - for i := 0; i < len(verify)/checkFreq; i++ { - index := i*checkFreq + self.rand.Intn(checkFreq) - if index >= len(verify) { - index = len(verify) - 1 - } - verify[index] = true - } - verify[len(verify)-1] = true // Last should always be verified to avoid junk - - // Create the header verification task queue and worker functions - tasks := make(chan int, len(chain)) - for i := 0; i < len(chain); i++ { - tasks <- i - } - close(tasks) - - errs, failed := make([]error, len(tasks)), int32(0) - process := func(worker int) { - for index := range tasks { - header, hash := chain[index], chain[index].Hash() - - // Short circuit insertion if shutting down or processing failed - if atomic.LoadInt32(&self.procInterrupt) == 1 { - return - } - if atomic.LoadInt32(&failed) > 0 { - return - } - // Short circuit if the header is bad or already known - if BadHashes[hash] { - errs[index] = BadHashError(hash) - atomic.AddInt32(&failed, 1) - return - } - if self.HasHeader(hash) { - continue - } - // Verify that the header honors the chain parameters - checkPow := verify[index] - - var err error - if index == 0 { - err = self.Validator().ValidateHeader(header, self.GetHeader(header.ParentHash), checkPow) - } else { - err = self.Validator().ValidateHeader(header, chain[index-1], checkPow) - } - if err != nil { - errs[index] = err - atomic.AddInt32(&failed, 1) - return - } - } - } - // Start as many worker threads as goroutines allowed - pending := new(sync.WaitGroup) - for i := 0; i < runtime.GOMAXPROCS(0); i++ { - pending.Add(1) - go func(id int) { - defer pending.Done() - process(id) - }(i) - } - pending.Wait() - - // If anything failed, report - if failed > 0 { - for i, err := range errs { - if err != nil { - return i, err - } - } - } - // All headers passed verification, import them into the database - for i, header := range chain { - // Short circuit insertion if shutting down - if atomic.LoadInt32(&self.procInterrupt) == 1 { - glog.V(logger.Debug).Infoln("premature abort during header chain processing") - break - } - hash := header.Hash() - - // If the header's already known, skip it, otherwise store - if self.HasHeader(hash) { - stats.ignored++ - continue - } - if err := self.writeHeader(header); err != nil { - return i, err - } - stats.processed++ - } - // Report some public statistics so the user has a clue what's going on - first, last := chain[0], chain[len(chain)-1] - glog.V(logger.Info).Infof("imported %d header(s) (%d ignored) in %v. #%v [%x… / %x…]", stats.processed, stats.ignored, - time.Since(start), last.Number, first.Hash().Bytes()[:4], last.Hash().Bytes()[:4]) - - return 0, nil -} - // Rollback is designed to remove a chain of links from the database that aren't // certain enough to be valid. func (self *BlockChain) Rollback(chain []common.Hash) { @@ -896,9 +613,8 @@ func (self *BlockChain) Rollback(chain []common.Hash) { for i := len(chain) - 1; i >= 0; i-- { hash := chain[i] - if self.currentHeader.Hash() == hash { - self.currentHeader = self.GetHeader(self.currentHeader.ParentHash) - WriteHeadHeaderHash(self.chainDb, self.currentHeader.Hash()) + if self.hc.CurrentHeader().Hash() == hash { + self.hc.SetCurrentHeader(self.GetHeader(self.hc.CurrentHeader().ParentHash)) } if self.currentFastBlock.Hash() == hash { self.currentFastBlock = self.GetBlock(self.currentFastBlock.ParentHash()) @@ -1055,7 +771,7 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain } // WriteBlock writes the block to the chain. -func (self *BlockChain) WriteBlock(block *types.Block) (status writeStatus, err error) { +func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err error) { self.wg.Add(1) defer self.wg.Done() @@ -1412,3 +1128,89 @@ func reportBlock(block *types.Block, err error) { } go ReportBlock(block, err) } + +// InsertHeaderChain attempts to insert the given header chain in to the local +// chain, possibly creating a reorg. If an error is returned, it will return the +// index number of the failing header as well an error describing what went wrong. +// +// The verify parameter can be used to fine tune whether nonce verification +// should be done or not. The reason behind the optional check is because some +// of the header retrieval mechanisms already need to verfy nonces, as well as +// because nonces can be verified sparsely, not needing to check each. +func (self *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { + // Make sure only one thread manipulates the chain at once + self.chainmu.Lock() + defer self.chainmu.Unlock() + + self.wg.Add(1) + defer self.wg.Done() + + whFunc := func(header *types.Header) error { + self.mu.Lock() + defer self.mu.Unlock() + + _, err := self.hc.WriteHeader(header) + return err + } + + return self.hc.InsertHeaderChain(chain, checkFreq, whFunc) +} + +// writeHeader writes a header into the local chain, given that its parent is +// already known. If the total difficulty of the newly inserted header becomes +// greater than the current known TD, the canonical chain is re-routed. +// +// Note: This method is not concurrent-safe with inserting blocks simultaneously +// into the chain, as side effects caused by reorganisations cannot be emulated +// without the real blocks. Hence, writing headers directly should only be done +// in two scenarios: pure-header mode of operation (light clients), or properly +// separated header/block phases (non-archive clients). +func (self *BlockChain) writeHeader(header *types.Header) error { + self.wg.Add(1) + defer self.wg.Done() + + self.mu.Lock() + defer self.mu.Unlock() + + _, err := self.hc.WriteHeader(header) + return err +} + +// CurrentHeader retrieves the current head header of the canonical chain. The +// header is retrieved from the HeaderChain's internal cache. +func (self *BlockChain) CurrentHeader() *types.Header { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader() +} + +// GetTd retrieves a block's total difficulty in the canonical chain from the +// database by hash, caching it if found. +func (self *BlockChain) GetTd(hash common.Hash) *big.Int { + return self.hc.GetTd(hash) +} + +// GetHeader retrieves a block header from the database by hash, caching it if +// found. +func (self *BlockChain) GetHeader(hash common.Hash) *types.Header { + return self.hc.GetHeader(hash) +} + +// HasHeader checks if a block header is present in the database or not, caching +// it if present. +func (bc *BlockChain) HasHeader(hash common.Hash) bool { + return bc.hc.HasHeader(hash) +} + +// GetBlockHashesFromHash retrieves a number of block hashes starting at a given +// hash, fetching towards the genesis block. +func (self *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { + return self.hc.GetBlockHashesFromHash(hash, max) +} + +// GetHeaderByNumber retrieves a block header from the database by number, +// caching it (associated with its hash) if found. +func (self *BlockChain) GetHeaderByNumber(number uint64) *types.Header { + return self.hc.GetHeaderByNumber(number) +} diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 1bb5f646db..fb8f465609 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -471,11 +471,14 @@ func makeBlockChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.B func chm(genesis *types.Block, db ethdb.Database) *BlockChain { var eventMux event.TypeMux - bc := &BlockChain{chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}, rand: rand.New(rand.NewSource(0))} - bc.headerCache, _ = lru.New(100) + bc := &BlockChain{chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}} + bc.hc = &HeaderChain{chainDb: db, genesisHeader: genesis.Header(), rand: rand.New(rand.NewSource(0))} + bc.hc.getValidator = func() HeaderValidator { return bc.Validator() } + bc.hc.procInterrupt = bc.getProcInterrupt + bc.hc.headerCache, _ = lru.New(100) bc.bodyCache, _ = lru.New(100) bc.bodyRLPCache, _ = lru.New(100) - bc.tdCache, _ = lru.New(100) + bc.hc.tdCache, _ = lru.New(100) bc.blockCache, _ = lru.New(100) bc.futureBlocks, _ = lru.New(100) bc.SetValidator(bproc{}) diff --git a/core/headerchain.go b/core/headerchain.go new file mode 100644 index 0000000000..27153e9a0a --- /dev/null +++ b/core/headerchain.go @@ -0,0 +1,425 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . +package core + +import ( + crand "crypto/rand" + "math" + "math/big" + mrand "math/rand" + "runtime" + "sync" + "sync/atomic" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/pow" + "github.com/hashicorp/golang-lru" +) + +// HeaderChain implements the basic block header chain logic that is shared by +// core.BlockChain and light.LightChain. It is not usable in itself, only as +// a part of either structure. +// It is not thread safe either, the encapsulating chain structures should do +// the necessary mutex locking/unlocking. +type HeaderChain struct { + chainDb ethdb.Database + genesisHeader *types.Header + + currentHeader *types.Header // Current head of the header chain (may be above the block chain!) + headerCache *lru.Cache // Cache for the most recent block headers + tdCache *lru.Cache // Cache for the most recent block total difficulties + + procInterrupt func() bool + + rand *mrand.Rand + getValidator getHeaderValidatorFn +} + +// getHeaderValidatorFn returns a HeaderValidator interface +type getHeaderValidatorFn func() HeaderValidator + +// NewHeaderChain creates a new HeaderChain structure. +// getValidator should return the parent's validator +// procInterrupt points to the parent's interrupt semaphore +// wg points to the parent's shutdown wait group +func NewHeaderChain(chainDb ethdb.Database, getValidator getHeaderValidatorFn, procInterrupt func() bool) (*HeaderChain, error) { + headerCache, _ := lru.New(headerCacheLimit) + tdCache, _ := lru.New(tdCacheLimit) + + // Seed a fast but crypto originating random generator + seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) + if err != nil { + return nil, err + } + + hc := &HeaderChain{ + chainDb: chainDb, + headerCache: headerCache, + tdCache: tdCache, + procInterrupt: procInterrupt, + rand: mrand.New(mrand.NewSource(seed.Int64())), + getValidator: getValidator, + } + + hc.genesisHeader = hc.GetHeaderByNumber(0) + if hc.genesisHeader == nil { + genesisBlock, err := WriteDefaultGenesisBlock(chainDb) + if err != nil { + return nil, err + } + glog.V(logger.Info).Infoln("WARNING: Wrote default ethereum genesis block") + hc.genesisHeader = genesisBlock.Header() + } + + hc.currentHeader = hc.genesisHeader + if head := GetHeadBlockHash(chainDb); head != (common.Hash{}) { + if chead := hc.GetHeader(head); chead != nil { + hc.currentHeader = chead + } + } + + return hc, nil +} + +// WriteHeader writes a header into the local chain, given that its parent is +// already known. If the total difficulty of the newly inserted header becomes +// greater than the current known TD, the canonical chain is re-routed. +// +// Note: This method is not concurrent-safe with inserting blocks simultaneously +// into the chain, as side effects caused by reorganisations cannot be emulated +// without the real blocks. Hence, writing headers directly should only be done +// in two scenarios: pure-header mode of operation (light clients), or properly +// separated header/block phases (non-archive clients). +func (self *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, err error) { + // Calculate the total difficulty of the header + ptd := self.GetTd(header.ParentHash) + if ptd == nil { + return NonStatTy, ParentError(header.ParentHash) + } + localTd := self.GetTd(self.currentHeader.Hash()) + externTd := new(big.Int).Add(header.Difficulty, ptd) + + // If the total difficulty is higher than our known, add it to the canonical chain + // Second clause in the if statement reduces the vulnerability to selfish mining. + // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf + if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { + // Delete any canonical number assignments above the new head + for i := header.Number.Uint64() + 1; GetCanonicalHash(self.chainDb, i) != (common.Hash{}); i++ { + DeleteCanonicalHash(self.chainDb, i) + } + // Overwrite any stale canonical number assignments + head := self.GetHeader(header.ParentHash) + for GetCanonicalHash(self.chainDb, head.Number.Uint64()) != head.Hash() { + WriteCanonicalHash(self.chainDb, head.Hash(), head.Number.Uint64()) + head = self.GetHeader(head.ParentHash) + } + // Extend the canonical chain with the new header + if err := WriteCanonicalHash(self.chainDb, header.Hash(), header.Number.Uint64()); err != nil { + glog.Fatalf("failed to insert header number: %v", err) + } + if err := WriteHeadHeaderHash(self.chainDb, header.Hash()); err != nil { + glog.Fatalf("failed to insert head header hash: %v", err) + } + self.currentHeader = types.CopyHeader(header) + status = CanonStatTy + } else { + status = SideStatTy + } + // Irrelevant of the canonical status, write the header itself to the database + if err := WriteTd(self.chainDb, header.Hash(), externTd); err != nil { + glog.Fatalf("failed to write header total difficulty: %v", err) + } + if err := WriteHeader(self.chainDb, header); err != nil { + glog.Fatalf("failed to write header contents: %v", err) + } + return +} + +// WhCallBack is a callback function for inserting individual headers. +// A callback is used for two reasons: first, in a LightChain, status should be +// processed and light chain events sent, while in a BlockChain this is not +// necessary since chain events are sent after inserting blocks. Second, the +// header writes should be protected by the parent chain mutex individually. +type WhCallBack func(*types.Header) error + +// InsertHeaderChain attempts to insert the given header chain in to the local +// chain, possibly creating a reorg. If an error is returned, it will return the +// index number of the failing header as well an error describing what went wrong. +// +// The verify parameter can be used to fine tune whether nonce verification +// should be done or not. The reason behind the optional check is because some +// of the header retrieval mechanisms already need to verfy nonces, as well as +// because nonces can be verified sparsely, not needing to check each. +func (self *HeaderChain) InsertHeaderChain(chain []*types.Header, checkFreq int, writeHeader WhCallBack) (int, error) { + // Collect some import statistics to report on + stats := struct{ processed, ignored int }{} + start := time.Now() + + // Generate the list of headers that should be POW verified + verify := make([]bool, len(chain)) + for i := 0; i < len(verify)/checkFreq; i++ { + index := i*checkFreq + self.rand.Intn(checkFreq) + if index >= len(verify) { + index = len(verify) - 1 + } + verify[index] = true + } + verify[len(verify)-1] = true // Last should always be verified to avoid junk + + // Create the header verification task queue and worker functions + tasks := make(chan int, len(chain)) + for i := 0; i < len(chain); i++ { + tasks <- i + } + close(tasks) + + errs, failed := make([]error, len(tasks)), int32(0) + process := func(worker int) { + for index := range tasks { + header, hash := chain[index], chain[index].Hash() + + // Short circuit insertion if shutting down or processing failed + if self.procInterrupt() { + return + } + if atomic.LoadInt32(&failed) > 0 { + return + } + // Short circuit if the header is bad or already known + if BadHashes[hash] { + errs[index] = BadHashError(hash) + atomic.AddInt32(&failed, 1) + return + } + if self.HasHeader(hash) { + continue + } + // Verify that the header honors the chain parameters + checkPow := verify[index] + + var err error + if index == 0 { + err = self.getValidator().ValidateHeader(header, self.GetHeader(header.ParentHash), checkPow) + } else { + err = self.getValidator().ValidateHeader(header, chain[index-1], checkPow) + } + if err != nil { + errs[index] = err + atomic.AddInt32(&failed, 1) + return + } + } + } + // Start as many worker threads as goroutines allowed + pending := new(sync.WaitGroup) + for i := 0; i < runtime.GOMAXPROCS(0); i++ { + pending.Add(1) + go func(id int) { + defer pending.Done() + process(id) + }(i) + } + pending.Wait() + + // If anything failed, report + if failed > 0 { + for i, err := range errs { + if err != nil { + return i, err + } + } + } + // All headers passed verification, import them into the database + for i, header := range chain { + // Short circuit insertion if shutting down + if self.procInterrupt() { + glog.V(logger.Debug).Infoln("premature abort during header chain processing") + break + } + hash := header.Hash() + + // If the header's already known, skip it, otherwise store + if self.HasHeader(hash) { + stats.ignored++ + continue + } + if err := writeHeader(header); err != nil { + return i, err + } + stats.processed++ + } + // Report some public statistics so the user has a clue what's going on + first, last := chain[0], chain[len(chain)-1] + glog.V(logger.Info).Infof("imported %d header(s) (%d ignored) in %v. #%v [%x… / %x…]", stats.processed, stats.ignored, + time.Since(start), last.Number, first.Hash().Bytes()[:4], last.Hash().Bytes()[:4]) + + return 0, nil +} + +// GetBlockHashesFromHash retrieves a number of block hashes starting at a given +// hash, fetching towards the genesis block. +func (self *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { + // Get the origin header from which to fetch + header := self.GetHeader(hash) + if header == nil { + return nil + } + // Iterate the headers until enough is collected or the genesis reached + chain := make([]common.Hash, 0, max) + for i := uint64(0); i < max; i++ { + if header = self.GetHeader(header.ParentHash); header == nil { + break + } + chain = append(chain, header.Hash()) + if header.Number.Cmp(common.Big0) == 0 { + break + } + } + return chain +} + +// GetTd retrieves a block's total difficulty in the canonical chain from the +// database by hash, caching it if found. +func (self *HeaderChain) GetTd(hash common.Hash) *big.Int { + // Short circuit if the td's already in the cache, retrieve otherwise + if cached, ok := self.tdCache.Get(hash); ok { + return cached.(*big.Int) + } + td := GetTd(self.chainDb, hash) + if td == nil { + return nil + } + // Cache the found body for next time and return + self.tdCache.Add(hash, td) + return td +} + +// GetHeader retrieves a block header from the database by hash, caching it if +// found. +func (self *HeaderChain) GetHeader(hash common.Hash) *types.Header { + // Short circuit if the header's already in the cache, retrieve otherwise + if header, ok := self.headerCache.Get(hash); ok { + return header.(*types.Header) + } + header := GetHeader(self.chainDb, hash) + if header == nil { + return nil + } + // Cache the found header for next time and return + self.headerCache.Add(header.Hash(), header) + return header +} + +// HasHeader checks if a block header is present in the database or not, caching +// it if present. +func (bc *HeaderChain) HasHeader(hash common.Hash) bool { + return bc.GetHeader(hash) != nil +} + +// GetHeaderByNumber retrieves a block header from the database by number, +// caching it (associated with its hash) if found. +func (self *HeaderChain) GetHeaderByNumber(number uint64) *types.Header { + hash := GetCanonicalHash(self.chainDb, number) + if hash == (common.Hash{}) { + return nil + } + return self.GetHeader(hash) +} + +// CurrentHeader retrieves the current head header of the canonical chain. The +// header is retrieved from the HeaderChain's internal cache. +func (self *HeaderChain) CurrentHeader() *types.Header { + return self.currentHeader +} + +// SetCurrentHeader sets the current head header of the canonical chain. +func (self *HeaderChain) SetCurrentHeader(head *types.Header) { + if err := WriteHeadHeaderHash(self.chainDb, head.Hash()); err != nil { + glog.Fatalf("failed to insert head header hash: %v", err) + } + self.currentHeader = head +} + +// SetHead rewinds the local chain to a new head. Everything above the new head +// will be deleted and the new one set. +func (bc *HeaderChain) SetHead(head uint64) { + height := uint64(0) + if bc.currentHeader != nil { + height = bc.currentHeader.Number.Uint64() + } + + for bc.currentHeader != nil && bc.currentHeader.Number.Uint64() > head { + hash := bc.currentHeader.Hash() + DeleteHeader(bc.chainDb, hash) + DeleteTd(bc.chainDb, hash) + bc.currentHeader = bc.GetHeader(bc.currentHeader.ParentHash) + } + // Roll back the canonical chain numbering + for i := height; i > head; i-- { + DeleteCanonicalHash(bc.chainDb, i) + } + // Clear out any stale content from the caches + bc.headerCache.Purge() + bc.tdCache.Purge() + + if bc.currentHeader == nil { + bc.currentHeader = bc.genesisHeader + } + if err := WriteHeadHeaderHash(bc.chainDb, bc.currentHeader.Hash()); err != nil { + glog.Fatalf("failed to reset head header hash: %v", err) + } +} + +// SetGenesis sets a new genesis block header for the chain +func (self *HeaderChain) SetGenesis(head *types.Header) { + self.genesisHeader = head +} + +// headerValidator is responsible for validating block headers +// +// headerValidator implements HeaderValidator. +type headerValidator struct { + bc *HeaderChain // Canonical header chain + Pow pow.PoW // Proof of work used for validating +} + +// NewBlockValidator returns a new block validator which is safe for re-use +func NewHeaderValidator(chain *HeaderChain, pow pow.PoW) HeaderValidator { + return &headerValidator{ + Pow: pow, + bc: chain, + } +} + +// ValidateHeader validates the given header and, depending on the pow arg, +// checks the proof of work of the given header. Returns an error if the +// validation failed. +func (v *headerValidator) ValidateHeader(header, parent *types.Header, checkPow bool) error { + // Short circuit if the parent is missing. + if parent == nil { + return ParentError(header.ParentHash) + } + // Short circuit if the header's already known or its parent missing + if v.bc.HasHeader(header.Hash()) { + return nil + } + return ValidateHeader(v.Pow, header, parent, checkPow, false) +} diff --git a/core/types.go b/core/types.go index 027f628b15..60eb15662f 100644 --- a/core/types.go +++ b/core/types.go @@ -38,14 +38,22 @@ import ( // ValidateHeader validates the given header and parent and returns an error // if it failed to do so. // -// ValidateStack validates the given statedb and optionally the receipts and +// ValidateState validates the given statedb and optionally the receipts and // gas used. The implementor should decide what to do with the given input. type Validator interface { + HeaderValidator ValidateBlock(block *types.Block) error - ValidateHeader(header, parent *types.Header, checkPow bool) error ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas *big.Int) error } +// HeaderValidator is an interface for validating headers only +// +// ValidateHeader validates the given header and parent and returns an error +// if it failed to do so. +type HeaderValidator interface { + ValidateHeader(header, parent *types.Header, checkPow bool) error +} + // Processor is an interface for processing blocks using a given initial state. // // Process takes the block to be processed and the statedb upon which the From 2aac139b72a75205f36eea63f70ffc38cd76ded9 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 13 Jan 2016 19:35:48 +0100 Subject: [PATCH 2/2] light: implemented light chain, VM env and tx pool --- core/database_util.go | 7 +- core/types/block.go | 9 + light/events.go | 33 +++ light/lightchain.go | 430 +++++++++++++++++++++++++++++++++ light/lightchain_test.go | 399 ++++++++++++++++++++++++++++++ light/odr.go | 100 +++++--- light/odr_test.go | 323 +++++++++++++++++++++++++ light/odr_util.go | 112 +++++++++ light/state.go | 33 ++- light/state_object.go | 29 ++- light/state_test.go | 36 +-- light/trie.go | 25 +- light/txpool.go | 506 +++++++++++++++++++++++++++++++++++++++ light/txpool_test.go | 140 +++++++++++ light/vm_env.go | 241 +++++++++++++++++++ 15 files changed, 2340 insertions(+), 83 deletions(-) create mode 100644 light/events.go create mode 100644 light/lightchain.go create mode 100644 light/lightchain_test.go create mode 100644 light/odr_test.go create mode 100644 light/odr_util.go create mode 100644 light/txpool.go create mode 100644 light/txpool_test.go create mode 100644 light/vm_env.go diff --git a/core/database_util.go b/core/database_util.go index fd2b4c3124..e2e190fdb4 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -288,8 +288,13 @@ func WriteBody(db ethdb.Database, hash common.Hash, body *types.Body) error { if err != nil { return err } + return WriteBodyRLP(db, hash, data) +} + +// WriteBodyRLP writes a serialized body of a block into the database. +func WriteBodyRLP(db ethdb.Database, hash common.Hash, rlp rlp.RawValue) error { key := append(append(blockPrefix, hash.Bytes()...), bodySuffix...) - if err := db.Put(key, data); err != nil { + if err := db.Put(key, rlp); err != nil { glog.Fatalf("failed to store block body into database: %v", err) return err } diff --git a/core/types/block.go b/core/types/block.go index 5536e0ea86..166c07012d 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -70,6 +70,15 @@ type Header struct { Nonce BlockNonce } +func (h *Header) GetNumber() *big.Int { return new(big.Int).Set(h.Number) } +func (h *Header) GetGasLimit() *big.Int { return new(big.Int).Set(h.GasLimit) } +func (h *Header) GetGasUsed() *big.Int { return new(big.Int).Set(h.GasUsed) } +func (h *Header) GetDifficulty() *big.Int { return new(big.Int).Set(h.Difficulty) } +func (h *Header) GetTime() *big.Int { return new(big.Int).Set(h.Time) } +func (h *Header) GetNumberU64() uint64 { return h.Number.Uint64() } +func (h *Header) GetNonce() uint64 { return binary.BigEndian.Uint64(h.Nonce[:]) } +func (h *Header) GetExtra() []byte { return common.CopyBytes(h.Extra) } + func (h *Header) Hash() common.Hash { return rlpHash(h) } diff --git a/light/events.go b/light/events.go new file mode 100644 index 0000000000..28d91e31d4 --- /dev/null +++ b/light/events.go @@ -0,0 +1,33 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package light + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type LightChainSplitEvent struct{ Header *types.Header } + +type LightChainEvent struct { + Header *types.Header + Hash common.Hash +} + +type LightChainSideEvent struct{ Header *types.Header } + +type LightChainHeadEvent struct{ Header *types.Header } diff --git a/light/lightchain.go b/light/lightchain.go new file mode 100644 index 0000000000..8f79597590 --- /dev/null +++ b/light/lightchain.go @@ -0,0 +1,430 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . +package light + +import ( + "math/big" + "sync" + "sync/atomic" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/pow" + "github.com/ethereum/go-ethereum/rlp" + "github.com/hashicorp/golang-lru" + "golang.org/x/net/context" +) + +var ( + bodyCacheLimit = 256 + blockCacheLimit = 256 +) + +// LightChain represents a canonical chain that by default only handles block +// headers, downloading block bodies and receipts on demand through an ODR +// interface. It only does header validation during chain insertion. +type LightChain struct { + hc *core.HeaderChain + chainDb ethdb.Database + odr OdrBackend + eventMux *event.TypeMux + genesisBlock *types.Block + + mu sync.RWMutex + chainmu sync.RWMutex + procmu sync.RWMutex + + bodyCache *lru.Cache // Cache for the most recent block bodies + bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format + blockCache *lru.Cache // Cache for the most recent entire blocks + + quit chan struct{} + running int32 // running must be called automically + // procInterrupt must be atomically called + procInterrupt int32 // interrupt signaler for block processing + wg sync.WaitGroup + + pow pow.PoW + validator core.HeaderValidator +} + +// NewLightChain returns a fully initialised light chain using information +// available in the database. It initialises the default Ethereum header +// validator. +func NewLightChain(odr OdrBackend, pow pow.PoW, mux *event.TypeMux) (*LightChain, error) { + bodyCache, _ := lru.New(bodyCacheLimit) + bodyRLPCache, _ := lru.New(bodyCacheLimit) + blockCache, _ := lru.New(blockCacheLimit) + + bc := &LightChain{ + chainDb: odr.Database(), + odr: odr, + eventMux: mux, + quit: make(chan struct{}), + bodyCache: bodyCache, + bodyRLPCache: bodyRLPCache, + blockCache: blockCache, + pow: pow, + } + + var err error + bc.hc, err = core.NewHeaderChain(odr.Database(), bc.Validator, bc.getProcInterrupt) + bc.SetValidator(core.NewHeaderValidator(bc.hc, pow)) + if err != nil { + return nil, err + } + + bc.genesisBlock, _ = bc.GetBlockByNumber(NoOdr, 0) + if bc.genesisBlock == nil { + bc.genesisBlock, err = core.WriteDefaultGenesisBlock(odr.Database()) + if err != nil { + return nil, err + } + glog.V(logger.Info).Infoln("WARNING: Wrote default ethereum genesis block") + } + if err := bc.loadLastState(); err != nil { + return nil, err + } + // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain + for hash, _ := range core.BadHashes { + if header := bc.GetHeader(hash); header != nil { + glog.V(logger.Error).Infof("Found bad hash, rewinding chain to block #%d [%x…]", header.Number, header.ParentHash[:4]) + bc.SetHead(header.Number.Uint64() - 1) + glog.V(logger.Error).Infoln("Chain rewind was successful, resuming normal operation") + } + } + return bc, nil +} + +func (self *LightChain) getProcInterrupt() bool { + return atomic.LoadInt32(&self.procInterrupt) == 1 +} + +// Odr returns the ODR backend of the chain +func (self *LightChain) Odr() OdrBackend { + return self.odr +} + +// loadLastState loads the last known chain state from the database. This method +// assumes that the chain manager mutex is held. +func (self *LightChain) loadLastState() error { + if head := core.GetHeadHeaderHash(self.chainDb); head == (common.Hash{}) { + // Corrupt or empty database, init from scratch + self.Reset() + } else { + if header := self.GetHeader(head); header != nil { + self.hc.SetCurrentHeader(header) + } + } + + // Issue a status log and return + headerTd := self.GetTd(self.hc.CurrentHeader().Hash()) + glog.V(logger.Info).Infof("Last header: #%d [%x…] TD=%v", self.hc.CurrentHeader().Number, self.hc.CurrentHeader().Hash().Bytes()[:4], headerTd) + + return nil +} + +// SetHead rewinds the local chain to a new head. Everything above the new +// head will be deleted and the new one set. +func (bc *LightChain) SetHead(head uint64) { + bc.mu.Lock() + defer bc.mu.Unlock() + + bc.hc.SetHead(head) + bc.loadLastState() +} + +// GasLimit returns the gas limit of the current HEAD block. +func (self *LightChain) GasLimit() *big.Int { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader().GasLimit +} + +// LastBlockHash return the hash of the HEAD block. +func (self *LightChain) LastBlockHash() common.Hash { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader().Hash() +} + +// Status returns status information about the current chain such as the HEAD Td, +// the HEAD hash and the hash of the genesis block. +func (self *LightChain) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) { + self.mu.RLock() + defer self.mu.RUnlock() + + hash := self.hc.CurrentHeader().Hash() + return self.GetTd(hash), hash, self.genesisBlock.Hash() +} + +// SetValidator sets the validator which is used to validate incoming headers. +func (self *LightChain) SetValidator(validator core.HeaderValidator) { + self.procmu.Lock() + defer self.procmu.Unlock() + self.validator = validator +} + +// Validator returns the current header validator. +func (self *LightChain) Validator() core.HeaderValidator { + self.procmu.RLock() + defer self.procmu.RUnlock() + return self.validator +} + +// State returns a new mutable state based on the current HEAD block. +func (self *LightChain) State() *LightState { + return NewLightState(StateTrieID(self.hc.CurrentHeader()), self.odr) +} + +// Reset purges the entire blockchain, restoring it to its genesis state. +func (bc *LightChain) Reset() { + bc.ResetWithGenesisBlock(bc.genesisBlock) +} + +// ResetWithGenesisBlock purges the entire blockchain, restoring it to the +// specified genesis state. +func (bc *LightChain) ResetWithGenesisBlock(genesis *types.Block) { + // Dump the entire block chain and purge the caches + bc.SetHead(0) + + bc.mu.Lock() + defer bc.mu.Unlock() + + // Prepare the genesis block and reinitialise the chain + if err := core.WriteTd(bc.chainDb, genesis.Hash(), genesis.Difficulty()); err != nil { + glog.Fatalf("failed to write genesis block TD: %v", err) + } + if err := core.WriteBlock(bc.chainDb, genesis); err != nil { + glog.Fatalf("failed to write genesis block: %v", err) + } + bc.genesisBlock = genesis + bc.hc.SetGenesis(bc.genesisBlock.Header()) + bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) +} + +// Accessors + +// Genesis returns the genesis block +func (bc *LightChain) Genesis() *types.Block { + return bc.genesisBlock +} + +// GetBody retrieves a block body (transactions and uncles) from the database +// or ODR service by hash, caching it if found. +func (self *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Body, error) { + // Short circuit if the body's already in the cache, retrieve otherwise + if cached, ok := self.bodyCache.Get(hash); ok { + body := cached.(*types.Body) + return body, nil + } + body, err := GetBody(ctx, self.odr, hash) + if err != nil { + return nil, err + } + // Cache the found body for next time and return + self.bodyCache.Add(hash, body) + return body, nil +} + +// GetBodyRLP retrieves a block body in RLP encoding from the database or +// ODR service by hash, caching it if found. +func (self *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.RawValue, error) { + // Short circuit if the body's already in the cache, retrieve otherwise + if cached, ok := self.bodyRLPCache.Get(hash); ok { + return cached.(rlp.RawValue), nil + } + body, err := GetBodyRLP(ctx, self.odr, hash) + if err != nil { + return nil, err + } + // Cache the found body for next time and return + self.bodyRLPCache.Add(hash, body) + return body, nil +} + +// HasBlock checks if a block is fully present in the database or not, caching +// it if present. +func (bc *LightChain) HasBlock(hash common.Hash) bool { + blk, _ := bc.GetBlock(NoOdr, hash) + return blk != nil +} + +// GetBlock retrieves a block from the database or ODR service by hash, +// caching it if found. +func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { + // Short circuit if the block's already in the cache, retrieve otherwise + if block, ok := self.blockCache.Get(hash); ok { + return block.(*types.Block), nil + } + block, err := GetBlock(ctx, self.odr, hash) + if err != nil { + return nil, err + } + // Cache the found block for next time and return + self.blockCache.Add(block.Hash(), block) + return block, nil +} + +// GetBlockByNumber retrieves a block from the database or ODR service by +// number, caching it (associated with its hash) if found. +func (self *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) { + hash := core.GetCanonicalHash(self.chainDb, number) + if hash == (common.Hash{}) { + return nil, nil + } + return self.GetBlock(ctx, hash) +} + +// Stop stops the blockchain service. If any imports are currently in progress +// it will abort them using the procInterrupt. +func (bc *LightChain) Stop() { + if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) { + return + } + close(bc.quit) + atomic.StoreInt32(&bc.procInterrupt, 1) + + bc.wg.Wait() + + glog.V(logger.Info).Infoln("Chain manager stopped") +} + +// Rollback is designed to remove a chain of links from the database that aren't +// certain enough to be valid. +func (self *LightChain) Rollback(chain []common.Hash) { + self.mu.Lock() + defer self.mu.Unlock() + + for i := len(chain) - 1; i >= 0; i-- { + hash := chain[i] + + if self.hc.CurrentHeader().Hash() == hash { + self.hc.SetCurrentHeader(self.GetHeader(self.hc.CurrentHeader().ParentHash)) + } + } +} + +// postChainEvents iterates over the events generated by a chain insertion and +// posts them into the event mux. +func (self *LightChain) postChainEvents(events []interface{}) { + for _, event := range events { + if event, ok := event.(LightChainEvent); ok { + if self.LastBlockHash() == event.Hash { + self.eventMux.Post(LightChainHeadEvent{event.Header}) + } + } + // Fire the insertion events individually too + self.eventMux.Post(event) + } +} + +// InsertHeaderChain attempts to insert the given header chain in to the local +// chain, possibly creating a reorg. If an error is returned, it will return the +// index number of the failing header as well an error describing what went wrong. +// +// The verify parameter can be used to fine tune whether nonce verification +// should be done or not. The reason behind the optional check is because some +// of the header retrieval mechanisms already need to verfy nonces, as well as +// because nonces can be verified sparsely, not needing to check each. +// +// In the case of a light chain, InsertHeaderChain also creates and posts light +// chain events when necessary. +func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { + // Make sure only one thread manipulates the chain at once + self.chainmu.Lock() + defer self.chainmu.Unlock() + + self.wg.Add(1) + defer self.wg.Done() + + var events []interface{} + whFunc := func(header *types.Header) error { + self.mu.Lock() + defer self.mu.Unlock() + + status, err := self.hc.WriteHeader(header) + + switch status { + case core.CanonStatTy: + if glog.V(logger.Debug) { + glog.Infof("[%v] inserted header #%d (%x...).\n", time.Now().UnixNano(), header.Number, header.Hash().Bytes()[0:4]) + } + events = append(events, LightChainEvent{header, header.Hash()}) + + case core.SideStatTy: + if glog.V(logger.Detail) { + glog.Infof("inserted forked header #%d (TD=%v) (%x...).\n", header.Number, header.Difficulty, header.Hash().Bytes()[0:4]) + } + events = append(events, LightChainSideEvent{header}) + + case core.SplitStatTy: + events = append(events, LightChainSplitEvent{header}) + } + + return err + } + i, err := self.hc.InsertHeaderChain(chain, checkFreq, whFunc) + go self.postChainEvents(events) + return i, err +} + +// CurrentHeader retrieves the current head header of the canonical chain. The +// header is retrieved from the HeaderChain's internal cache. +func (self *LightChain) CurrentHeader() *types.Header { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader() +} + +// GetTd retrieves a block's total difficulty in the canonical chain from the +// database by hash, caching it if found. +func (self *LightChain) GetTd(hash common.Hash) *big.Int { + return self.hc.GetTd(hash) +} + +// GetHeader retrieves a block header from the database by hash, caching it if +// found. +func (self *LightChain) GetHeader(hash common.Hash) *types.Header { + return self.hc.GetHeader(hash) +} + +// HasHeader checks if a block header is present in the database or not, caching +// it if present. +func (bc *LightChain) HasHeader(hash common.Hash) bool { + return bc.hc.HasHeader(hash) +} + +// GetBlockHashesFromHash retrieves a number of block hashes starting at a given +// hash, fetching towards the genesis block. +func (self *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { + return self.hc.GetBlockHashesFromHash(hash, max) +} + +// GetHeaderByNumber retrieves a block header from the database by number, +// caching it (associated with its hash) if found. +func (self *LightChain) GetHeaderByNumber(number uint64) *types.Header { + return self.hc.GetHeaderByNumber(number) +} diff --git a/light/lightchain_test.go b/light/lightchain_test.go new file mode 100644 index 0000000000..5b9b7cdfc6 --- /dev/null +++ b/light/lightchain_test.go @@ -0,0 +1,399 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package light + +import ( + "fmt" + "math/big" + "runtime" + "testing" + + "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/pow" + "github.com/hashicorp/golang-lru" + "golang.org/x/net/context" +) + +// So we can deterministically seed different blockchains +var ( + canonicalSeed = 1 + forkSeed = 2 +) + +// makeHeaderChain creates a deterministic chain of headers rooted at parent. +func makeHeaderChain(parent *types.Header, n int, db ethdb.Database, seed int) []*types.Header { + blocks, _ := core.GenerateChain(types.NewBlockWithHeader(parent), db, n, func(i int, b *core.BlockGen) { + b.SetCoinbase(common.Address{0: byte(seed), 19: byte(i)}) + }) + headers := make([]*types.Header, len(blocks)) + for i, block := range blocks { + headers[i] = block.Header() + } + return headers +} + +// newCanonical creates a chain database, and injects a deterministic canonical +// chain. Depending on the full flag, if creates either a full block chain or a +// header only chain. +func newCanonical(n int) (ethdb.Database, *LightChain, error) { + // Create te new chain database + db, _ := ethdb.NewMemDatabase() + evmux := &event.TypeMux{} + + // Initialize a fresh chain with only a genesis block + genesis, _ := core.WriteTestNetGenesisBlock(db) + + blockchain, _ := NewLightChain(&dummyOdr{db: db}, core.FakePow{}, evmux) + // Create and inject the requested chain + if n == 0 { + return db, blockchain, nil + } + // Header-only chain requested + headers := makeHeaderChain(genesis.Header(), n, db, canonicalSeed) + _, err := blockchain.InsertHeaderChain(headers, 1) + return db, blockchain, err +} + +func init() { + runtime.GOMAXPROCS(runtime.NumCPU()) +} + +func thePow() pow.PoW { + pow, _ := ethash.NewForTesting() + return pow +} + +func theLightChain(db ethdb.Database, t *testing.T) *LightChain { + var eventMux event.TypeMux + core.WriteTestNetGenesisBlock(db) + LightChain, err := NewLightChain(&dummyOdr{db: db}, thePow(), &eventMux) + if err != nil { + t.Error("failed creating LightChain:", err) + t.FailNow() + return nil + } + + return LightChain +} + +// Test fork of length N starting from block i +func testFork(t *testing.T, LightChain *LightChain, i, n int, comparator func(td1, td2 *big.Int)) { + // Copy old chain up to #i into a new db + db, LightChain2, err := newCanonical(i) + if err != nil { + t.Fatal("could not make new canonical in testFork", err) + } + // Assert the chains have the same header/block at #i + var hash1, hash2 common.Hash + hash1 = LightChain.GetHeaderByNumber(uint64(i)).Hash() + hash2 = LightChain2.GetHeaderByNumber(uint64(i)).Hash() + if hash1 != hash2 { + t.Errorf("chain content mismatch at %d: have hash %v, want hash %v", i, hash2, hash1) + } + // Extend the newly created chain + var ( + headerChainB []*types.Header + ) + headerChainB = makeHeaderChain(LightChain2.CurrentHeader(), n, db, forkSeed) + if _, err := LightChain2.InsertHeaderChain(headerChainB, 1); err != nil { + t.Fatalf("failed to insert forking chain: %v", err) + } + // Sanity check that the forked chain can be imported into the original + var tdPre, tdPost *big.Int + + tdPre = LightChain.GetTd(LightChain.CurrentHeader().Hash()) + if err := testHeaderChainImport(headerChainB, LightChain); err != nil { + t.Fatalf("failed to import forked header chain: %v", err) + } + tdPost = LightChain.GetTd(headerChainB[len(headerChainB)-1].Hash()) + // Compare the total difficulties of the chains + comparator(tdPre, tdPost) +} + +func printChain(bc *LightChain) { + for i := bc.CurrentHeader().GetNumberU64(); i > 0; i-- { + b := bc.GetHeaderByNumber(uint64(i)) + fmt.Printf("\t%x %v\n", b.Hash(), b.Difficulty) + } +} + +// testHeaderChainImport tries to process a chain of header, writing them into +// the database if successful. +func testHeaderChainImport(chain []*types.Header, LightChain *LightChain) error { + for _, header := range chain { + // Try and validate the header + if err := LightChain.Validator().ValidateHeader(header, LightChain.GetHeader(header.ParentHash), false); err != nil { + return err + } + // Manually insert the header into the database, but don't reorganize (allows subsequent testing) + LightChain.mu.Lock() + core.WriteTd(LightChain.chainDb, header.Hash(), new(big.Int).Add(header.Difficulty, LightChain.GetTd(header.ParentHash))) + core.WriteHeader(LightChain.chainDb, header) + LightChain.mu.Unlock() + } + return nil +} + +// Tests that given a starting canonical chain of a given size, it can be extended +// with various length chains. +func TestExtendCanonicalHeaders(t *testing.T) { + length := 5 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + better := func(td1, td2 *big.Int) { + if td2.Cmp(td1) <= 0 { + t.Errorf("total difficulty mismatch: have %v, expected more than %v", td2, td1) + } + } + // Start fork from current height + testFork(t, processor, length, 1, better) + testFork(t, processor, length, 2, better) + testFork(t, processor, length, 5, better) + testFork(t, processor, length, 10, better) +} + +// Tests that given a starting canonical chain of a given size, creating shorter +// forks do not take canonical ownership. +func TestShorterForkHeaders(t *testing.T) { + length := 10 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + worse := func(td1, td2 *big.Int) { + if td2.Cmp(td1) >= 0 { + t.Errorf("total difficulty mismatch: have %v, expected less than %v", td2, td1) + } + } + // Sum of numbers must be less than `length` for this to be a shorter fork + testFork(t, processor, 0, 3, worse) + testFork(t, processor, 0, 7, worse) + testFork(t, processor, 1, 1, worse) + testFork(t, processor, 1, 7, worse) + testFork(t, processor, 5, 3, worse) + testFork(t, processor, 5, 4, worse) +} + +// Tests that given a starting canonical chain of a given size, creating longer +// forks do take canonical ownership. +func TestLongerForkHeaders(t *testing.T) { + length := 10 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + better := func(td1, td2 *big.Int) { + if td2.Cmp(td1) <= 0 { + t.Errorf("total difficulty mismatch: have %v, expected more than %v", td2, td1) + } + } + // Sum of numbers must be greater than `length` for this to be a longer fork + testFork(t, processor, 0, 11, better) + testFork(t, processor, 0, 15, better) + testFork(t, processor, 1, 10, better) + testFork(t, processor, 1, 12, better) + testFork(t, processor, 5, 6, better) + testFork(t, processor, 5, 8, better) +} + +// Tests that given a starting canonical chain of a given size, creating equal +// forks do take canonical ownership. +func TestEqualForkHeaders(t *testing.T) { + length := 10 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + equal := func(td1, td2 *big.Int) { + if td2.Cmp(td1) != 0 { + t.Errorf("total difficulty mismatch: have %v, want %v", td2, td1) + } + } + // Sum of numbers must be equal to `length` for this to be an equal fork + testFork(t, processor, 0, 10, equal) + testFork(t, processor, 1, 9, equal) + testFork(t, processor, 2, 8, equal) + testFork(t, processor, 5, 5, equal) + testFork(t, processor, 6, 4, equal) + testFork(t, processor, 9, 1, equal) +} + +// Tests that chains missing links do not get accepted by the processor. +func TestBrokenHeaderChain(t *testing.T) { + // Make chain starting from genesis + db, LightChain, err := newCanonical(10) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Create a forked chain, and try to insert with a missing link + chain := makeHeaderChain(LightChain.CurrentHeader(), 5, db, forkSeed)[1:] + if err := testHeaderChainImport(chain, LightChain); err == nil { + t.Errorf("broken header chain not reported") + } +} + +type bproc struct{} + +func (bproc) ValidateHeader(*types.Header, *types.Header, bool) error { return nil } + +func makeHeaderChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Header { + var chain []*types.Header + for i, difficulty := range d { + header := &types.Header{ + Coinbase: common.Address{seed}, + Number: big.NewInt(int64(i + 1)), + Difficulty: big.NewInt(int64(difficulty)), + UncleHash: types.EmptyUncleHash, + TxHash: types.EmptyRootHash, + ReceiptHash: types.EmptyRootHash, + } + if i == 0 { + header.ParentHash = genesis.Hash() + } else { + header.ParentHash = chain[i-1].Hash() + } + chain = append(chain, types.CopyHeader(header)) + } + return chain +} + +type dummyOdr struct { + OdrBackend + db ethdb.Database +} + +func (odr *dummyOdr) Database() ethdb.Database { + return odr.db +} + +func (odr *dummyOdr) Retrieve(ctx context.Context, req OdrRequest) error { + return nil +} + +func chm(genesis *types.Block, db ethdb.Database) *LightChain { + odr := &dummyOdr{db: db} + var eventMux event.TypeMux + bc := &LightChain{odr: odr, chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: core.FakePow{}} + bc.hc, _ = core.NewHeaderChain(db, bc.Validator, bc.getProcInterrupt) + bc.bodyCache, _ = lru.New(100) + bc.bodyRLPCache, _ = lru.New(100) + bc.blockCache, _ = lru.New(100) + bc.SetValidator(bproc{}) + bc.ResetWithGenesisBlock(genesis) + + return bc +} + +// Tests that reorganizing a long difficult chain after a short easy one +// overwrites the canonical numbers and links in the database. +func TestReorgLongHeaders(t *testing.T) { + testReorg(t, []int{1, 2, 4}, []int{1, 2, 3, 4}, 10) +} + +// Tests that reorganizing a short difficult chain after a long easy one +// overwrites the canonical numbers and links in the database. +func TestReorgShortHeaders(t *testing.T) { + testReorg(t, []int{1, 2, 3, 4}, []int{1, 10}, 11) +} + +func testReorg(t *testing.T, first, second []int, td int64) { + // Create a pristine block chain + db, _ := ethdb.NewMemDatabase() + genesis, _ := core.WriteTestNetGenesisBlock(db) + bc := chm(genesis, db) + + // Insert an easy and a difficult chain afterwards + bc.InsertHeaderChain(makeHeaderChainWithDiff(genesis, first, 11), 1) + bc.InsertHeaderChain(makeHeaderChainWithDiff(genesis, second, 22), 1) + // Check that the chain is valid number and link wise + prev := bc.CurrentHeader() + for header := bc.GetHeaderByNumber(bc.CurrentHeader().Number.Uint64() - 1); header.Number.Uint64() != 0; prev, header = header, bc.GetHeaderByNumber(header.Number.Uint64()-1) { + if prev.ParentHash != header.Hash() { + t.Errorf("parent header hash mismatch: have %x, want %x", prev.ParentHash, header.Hash()) + } + } + // Make sure the chain total difficulty is the correct one + want := new(big.Int).Add(genesis.Difficulty(), big.NewInt(td)) + if have := bc.GetTd(bc.CurrentHeader().Hash()); have.Cmp(want) != 0 { + t.Errorf("total difficulty mismatch: have %v, want %v", have, want) + } +} + +// Tests that the insertion functions detect banned hashes. +func TestBadHeaderHashes(t *testing.T) { + // Create a pristine block chain + db, _ := ethdb.NewMemDatabase() + genesis, _ := core.WriteTestNetGenesisBlock(db) + bc := chm(genesis, db) + + // Create a chain, ban a hash and try to import + var err error + headers := makeHeaderChainWithDiff(genesis, []int{1, 2, 4}, 10) + core.BadHashes[headers[2].Hash()] = true + _, err = bc.InsertHeaderChain(headers, 1) + if !core.IsBadHashError(err) { + t.Errorf("error mismatch: want: BadHashError, have: %v", err) + } +} + +// Tests that bad hashes are detected on boot, and the chan rolled back to a +// good state prior to the bad hash. +func TestReorgBadHeaderHashes(t *testing.T) { + // Create a pristine block chain + db, _ := ethdb.NewMemDatabase() + genesis, _ := core.WriteTestNetGenesisBlock(db) + bc := chm(genesis, db) + + // Create a chain, import and ban aferwards + headers := makeHeaderChainWithDiff(genesis, []int{1, 2, 3, 4}, 10) + + if _, err := bc.InsertHeaderChain(headers, 1); err != nil { + t.Fatalf("failed to import headers: %v", err) + } + if bc.CurrentHeader().Hash() != headers[3].Hash() { + t.Errorf("last header hash mismatch: have: %x, want %x", bc.CurrentHeader().Hash(), headers[3].Hash()) + } + core.BadHashes[headers[3].Hash()] = true + defer func() { delete(core.BadHashes, headers[3].Hash()) }() + // Create a new chain manager and check it rolled back the state + ncm, err := NewLightChain(&dummyOdr{db: db}, core.FakePow{}, new(event.TypeMux)) + if err != nil { + t.Fatalf("failed to create new chain manager: %v", err) + } + if ncm.CurrentHeader().Hash() != headers[2].Hash() { + t.Errorf("last header hash mismatch: have: %x, want %x", ncm.CurrentHeader().Hash(), headers[2].Hash()) + } +} diff --git a/light/odr.go b/light/odr.go index 4c69040ef8..a61d2049f1 100644 --- a/light/odr.go +++ b/light/odr.go @@ -20,13 +20,19 @@ package light import ( "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/net/context" ) -// OdrBackend is an interface to a backend service that handles odr retrievals +// NoOdr is the default context passed to an ODR capable function when the ODR +// service is not required. +var NoOdr = context.Background() + +// OdrBackend is an interface to a backend service that handles ODR retrievals type OdrBackend interface { Database() ethdb.Database Retrieve(ctx context.Context, req OdrRequest) error @@ -37,17 +43,44 @@ type OdrRequest interface { StoreResult(db ethdb.Database) } +// TrieID identifies a state or account storage trie +type TrieID struct { + blockHash, root common.Hash + accKey []byte +} + +// StateTrieID returns a TrieID for a state trie belonging to a certain block +// header. +func StateTrieID(header *types.Header) *TrieID { + return &TrieID{ + blockHash: header.Hash(), + accKey: nil, + root: header.Root, + } +} + +// StorageTrieID returns a TrieID for a contract storage trie at a given account +// of a given state trie. It also requires the root hash of the trie for +// checking Merkle proofs. +func StorageTrieID(state *TrieID, addr common.Address, root common.Hash) *TrieID { + return &TrieID{ + blockHash: state.blockHash, + accKey: crypto.Keccak256(addr[:]), + root: root, + } +} + // TrieRequest is the ODR request type for state/storage trie entries type TrieRequest struct { OdrRequest - root common.Hash - key []byte - proof []rlp.RawValue + Id *TrieID + Key []byte + Proof []rlp.RawValue } // StoreResult stores the retrieved data in local database func (req *TrieRequest) StoreResult(db ethdb.Database) { - storeProof(db, req.proof) + storeProof(db, req.Proof) } // storeProof stores the new trie nodes obtained from a merkle proof in the database @@ -61,38 +94,39 @@ func storeProof(db ethdb.Database, proof []rlp.RawValue) { } } -// NodeDataRequest is the ODR request type for node data (used for retrieving contract code) -type NodeDataRequest struct { +// CodeRequest is the ODR request type for retrieving contract code +type CodeRequest struct { OdrRequest - hash common.Hash - data []byte -} - -// GetData returns the retrieved node data after a successful request -func (req *NodeDataRequest) GetData() []byte { - return req.data + Id *TrieID + Hash common.Hash + Data []byte } // StoreResult stores the retrieved data in local database -func (req *NodeDataRequest) StoreResult(db ethdb.Database) { - db.Put(req.hash[:], req.GetData()) +func (req *CodeRequest) StoreResult(db ethdb.Database) { + db.Put(req.Hash[:], req.Data) } -var sha3_nil = crypto.Keccak256Hash(nil) - -// retrieveNodeData tries to retrieve node data with the given hash from the network -func retrieveNodeData(ctx context.Context, odr OdrBackend, hash common.Hash) ([]byte, error) { - if hash == sha3_nil { - return nil, nil - } - res, _ := odr.Database().Get(hash[:]) - if res != nil { - return res, nil - } - r := &NodeDataRequest{hash: hash} - if err := odr.Retrieve(ctx, r); err != nil { - return nil, err - } else { - return r.GetData(), nil - } +// BlockRequest is the ODR request type for retrieving block bodies +type BlockRequest struct { + OdrRequest + Hash common.Hash + Rlp []byte +} + +// StoreResult stores the retrieved data in local database +func (req *BlockRequest) StoreResult(db ethdb.Database) { + core.WriteBodyRLP(db, req.Hash, req.Rlp) +} + +// ReceiptsRequest is the ODR request type for retrieving block bodies +type ReceiptsRequest struct { + OdrRequest + Hash common.Hash + Receipts types.Receipts +} + +// StoreResult stores the retrieved data in local database +func (req *ReceiptsRequest) StoreResult(db ethdb.Database) { + core.WriteBlockReceipts(db, req.Hash, req.Receipts) } diff --git a/light/odr_test.go b/light/odr_test.go new file mode 100644 index 0000000000..d868801b5e --- /dev/null +++ b/light/odr_test.go @@ -0,0 +1,323 @@ +package light + +import ( + "bytes" + "errors" + "math/big" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" + "golang.org/x/net/context" +) + +var ( + testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) + testBankFunds = big.NewInt(100000000) + + acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") + acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") + acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey) + acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey) + + testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056") + testContractAddr common.Address +) + +type testOdr struct { + OdrBackend + sdb, ldb ethdb.Database + disable bool +} + +func (odr *testOdr) Database() ethdb.Database { + return odr.ldb +} + +var ErrOdrDisabled = errors.New("ODR disabled") + +func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { + if odr.disable { + return ErrOdrDisabled + } + switch req := req.(type) { + case *BlockRequest: + req.Rlp = core.GetBodyRLP(odr.sdb, req.Hash) + case *ReceiptsRequest: + req.Receipts = core.GetBlockReceipts(odr.sdb, req.Hash) + case *TrieRequest: + t, _ := trie.New(req.Id.root, odr.sdb) + req.Proof = t.Prove(req.Key) + trie.ClearGlobalCache() + case *CodeRequest: + req.Data, _ = odr.sdb.Get(req.Hash[:]) + } + req.StoreResult(odr.ldb) + return nil +} + +type odrTestFn func(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte + +func TestOdrGetBlockLes1(t *testing.T) { testChainOdr(t, 1, 1, odrGetBlock) } + +func odrGetBlock(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + var block *types.Block + if bc != nil { + block = bc.GetBlock(bhash) + } else { + block, _ = lc.GetBlock(ctx, bhash) + } + if block == nil { + return nil + } + rlp, _ := rlp.EncodeToBytes(block) + return rlp +} + +func TestOdrGetReceiptsLes1(t *testing.T) { testChainOdr(t, 1, 1, odrGetReceipts) } + +func odrGetReceipts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + var receipts types.Receipts + if bc != nil { + receipts = core.GetBlockReceipts(db, bhash) + } else { + receipts, _ = GetBlockReceipts(ctx, lc.Odr(), bhash) + } + if receipts == nil { + return nil + } + rlp, _ := rlp.EncodeToBytes(receipts) + return rlp +} + +func TestOdrAccountsLes1(t *testing.T) { testChainOdr(t, 1, 1, odrAccounts) } + +func odrAccounts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678") + acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr} + + trie.ClearGlobalCache() + + var res []byte + for _, addr := range acc { + if bc != nil { + header := bc.GetHeader(bhash) + st, err := state.New(header.Root, db) + if err == nil { + bal := st.GetBalance(addr) + rlp, _ := rlp.EncodeToBytes(bal) + res = append(res, rlp...) + } + } else { + header := lc.GetHeader(bhash) + st := NewLightState(StateTrieID(header), lc.Odr()) + bal, err := st.GetBalance(ctx, addr) + if err == nil { + rlp, _ := rlp.EncodeToBytes(bal) + res = append(res, rlp...) + } + } + } + + return res +} + +func TestOdrContractCallLes1(t *testing.T) { testChainOdr(t, 1, 2, odrContractCall) } + +// fullcallmsg is the message type used for call transations. +type fullcallmsg struct { + from *state.StateObject + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m fullcallmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m fullcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } +func (m fullcallmsg) Nonce() uint64 { return m.from.Nonce() } +func (m fullcallmsg) To() *common.Address { return m.to } +func (m fullcallmsg) GasPrice() *big.Int { return m.gasPrice } +func (m fullcallmsg) Gas() *big.Int { return m.gas } +func (m fullcallmsg) Value() *big.Int { return m.value } +func (m fullcallmsg) Data() []byte { return m.data } + +// callmsg is the message type used for call transations. +type lightcallmsg struct { + from *StateObject + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m lightcallmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m lightcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } +func (m lightcallmsg) Nonce() uint64 { return m.from.Nonce() } +func (m lightcallmsg) To() *common.Address { return m.to } +func (m lightcallmsg) GasPrice() *big.Int { return m.gasPrice } +func (m lightcallmsg) Gas() *big.Int { return m.gas } +func (m lightcallmsg) Value() *big.Int { return m.value } +func (m lightcallmsg) Data() []byte { return m.data } + +func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000") + + var res []byte + for i := 0; i < 3; i++ { + data[35] = byte(i) + if bc != nil { + header := bc.GetHeader(bhash) + statedb, err := state.New(header.Root, db) + if err == nil { + from := statedb.GetOrNewStateObject(testBankAddress) + from.SetBalance(common.MaxBig) + + msg := fullcallmsg{ + from: from, + gas: big.NewInt(100000), + gasPrice: big.NewInt(0), + value: big.NewInt(0), + data: data, + to: &testContractAddr, + } + + vmenv := core.NewEnv(statedb, bc, msg, header) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + res = append(res, ret...) + } + } else { + header := lc.GetHeader(bhash) + state := NewLightState(StateTrieID(header), lc.Odr()) + from, err := state.GetOrNewStateObject(ctx, testBankAddress) + if err == nil { + from.SetBalance(common.MaxBig) + + msg := lightcallmsg{ + from: from, + gas: big.NewInt(100000), + gasPrice: big.NewInt(0), + value: big.NewInt(0), + data: data, + to: &testContractAddr, + } + + vmenv := NewEnv(ctx, state, lc, msg, header) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + if vmenv.Error() == nil { + res = append(res, ret...) + } + } + } + } + return res +} + +func testChainGen(i int, block *core.BlockGen) { + switch i { + case 0: + // In block 1, the test bank sends account #1 some ether. + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) + block.AddTx(tx) + case 1: + // In block 2, the test bank sends some more ether to account #1. + // acc1Addr passes it on to account #2. + // acc1Addr creates a test contract. + tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey) + nonce := block.TxNonce(acc1Addr) + tx2, _ := types.NewTransaction(nonce, acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) + nonce++ + tx3, _ := types.NewContractCreation(nonce, big.NewInt(0), big.NewInt(100000), big.NewInt(0), testContractCode).SignECDSA(acc1Key) + testContractAddr = crypto.CreateAddress(acc1Addr, nonce) + block.AddTx(tx1) + block.AddTx(tx2) + block.AddTx(tx3) + case 2: + // Block 3 is empty but was mined by account #2. + block.SetCoinbase(acc2Addr) + block.SetExtra([]byte("yeehaw")) + data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001") + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data).SignECDSA(testBankKey) + block.AddTx(tx) + case 3: + // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data). + b2 := block.PrevBlock(1).Header() + b2.Extra = []byte("foo") + block.AddUncle(b2) + b3 := block.PrevBlock(2).Header() + b3.Extra = []byte("foo") + block.AddUncle(b3) + data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002") + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data).SignECDSA(testBankKey) + block.AddTx(tx) + } +} + +func testChainOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { + var ( + evmux = new(event.TypeMux) + pow = new(core.FakePow) + sdb, _ = ethdb.NewMemDatabase() + ldb, _ = ethdb.NewMemDatabase() + genesis = core.WriteGenesisBlockForTesting(sdb, core.GenesisAccount{testBankAddress, testBankFunds}) + ) + core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{testBankAddress, testBankFunds}) + // Assemble the test environment + blockchain, _ := core.NewBlockChain(sdb, pow, evmux) + gchain, _ := core.GenerateChain(genesis, sdb, 4, testChainGen) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + + odr := &testOdr{sdb: sdb, ldb: ldb} + lightchain, _ := NewLightChain(odr, pow, evmux) + lightchain.SetValidator(bproc{}) + headers := make([]*types.Header, len(gchain)) + for i, block := range gchain { + headers[i] = block.Header() + } + if _, err := lightchain.InsertHeaderChain(headers, 1); err != nil { + panic(err) + } + + test := func(expFail uint64) { + for i := uint64(0); i <= blockchain.CurrentHeader().GetNumberU64(); i++ { + bhash := core.GetCanonicalHash(sdb, i) + b1 := fn(NoOdr, sdb, blockchain, nil, bhash) + ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) + b2 := fn(ctx, ldb, nil, lightchain, bhash) + eq := bytes.Equal(b1, b2) + exp := i < expFail + if exp && !eq { + t.Errorf("odr mismatch") + } + if !exp && eq { + t.Errorf("unexpected odr match") + } + } + } + + odr.disable = true + // expect retrievals to fail (except genesis block) without a les peer + test(expFail) + odr.disable = false + // expect all retrievals to pass + test(5) + odr.disable = true + // still expect all retrievals to pass, now data should be cached locally + test(5) +} diff --git a/light/odr_util.go b/light/odr_util.go new file mode 100644 index 0000000000..1e1b7f2546 --- /dev/null +++ b/light/odr_util.go @@ -0,0 +1,112 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . +package light + +import ( + "bytes" + "errors" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rlp" + "golang.org/x/net/context" +) + +var sha3_nil = crypto.Keccak256Hash(nil) + +var ErrNoHeader = errors.New("Block header not found") + +// retrieveContractCode tries to retrieve the contract code of the given account +// with the given hash from the network (id points to the storage trie belonging +// to the same account) +func retrieveContractCode(ctx context.Context, odr OdrBackend, id *TrieID, hash common.Hash) ([]byte, error) { + if hash == sha3_nil { + return nil, nil + } + res, _ := odr.Database().Get(hash[:]) + if res != nil { + return res, nil + } + r := &CodeRequest{Id: id, Hash: hash} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Data, nil + } +} + +// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. +func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash) (rlp.RawValue, error) { + if data := core.GetBodyRLP(odr.Database(), hash); data != nil { + return data, nil + } + r := &BlockRequest{Hash: hash} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Rlp, nil + } +} + +// GetBody retrieves the block body (transactons, uncles) corresponding to the +// hash. +func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Body, error) { + data, err := GetBodyRLP(ctx, odr, hash) + if err != nil { + return nil, err + } + body := new(types.Body) + if err := rlp.Decode(bytes.NewReader(data), body); err != nil { + glog.V(logger.Error).Infof("invalid block body RLP for hash %x: %v", hash, err) + return nil, err + } + return body, nil +} + +// GetBlock retrieves an entire block corresponding to the hash, assembling it +// back from the stored header and body. +func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Block, error) { + // Retrieve the block header and body contents + header := core.GetHeader(odr.Database(), hash) + if header == nil { + return nil, ErrNoHeader + } + body, err := GetBody(ctx, odr, hash) + if err != nil { + return nil, err + } + // Reassemble the block and return + return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil +} + +// GetBlockReceipts retrieves the receipts generated by the transactions included +// in a block given by its hash. +func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash) (types.Receipts, error) { + receipts := core.GetBlockReceipts(odr.Database(), hash) + if receipts != nil { + return receipts, nil + } + r := &ReceiptsRequest{Hash: hash} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Receipts, nil + } +} diff --git a/light/state.go b/light/state.go index e18f9cdc5d..08bb8e5bac 100644 --- a/light/state.go +++ b/light/state.go @@ -33,10 +33,11 @@ var StartingNonce uint64 // state, retrieving unknown parts on-demand from the ODR backend. Changes are // never stored in the local database, only in the memory objects. type LightState struct { - odr OdrBackend - trie *LightTrie - + odr OdrBackend + trie *LightTrie + id *TrieID stateObjects map[string]*StateObject + refund *big.Int } // NewLightState creates a new LightState with the specified root. @@ -44,15 +45,25 @@ type LightState struct { // root is non-existent. In that case, ODR retrieval will always be unsuccessful // and every operation will return with an error or wait for the context to be // cancelled. -func NewLightState(root common.Hash, odr OdrBackend) *LightState { - tr := NewLightTrie(root, odr, true) +func NewLightState(id *TrieID, odr OdrBackend) *LightState { + var tr *LightTrie + if id != nil { + tr = NewLightTrie(id, odr, true) + } return &LightState{ odr: odr, trie: tr, + id: id, stateObjects: make(map[string]*StateObject), + refund: new(big.Int), } } +// AddRefund adds an amount to the refund value collected during a vm execution +func (self *LightState) AddRefund(gas *big.Int) { + self.refund.Add(self.refund, gas) +} + // HasAccount returns true if an account exists at the given address func (self *LightState) HasAccount(ctx context.Context, addr common.Address) (bool, error) { so, err := self.GetStateObject(ctx, addr) @@ -194,7 +205,7 @@ func (self *LightState) GetStateObject(ctx context.Context, addr common.Address) return nil, nil } - stateObject, err = DecodeObject(ctx, addr, self.odr, []byte(data)) + stateObject, err = DecodeObject(ctx, self.id, addr, self.odr, []byte(data)) if err != nil { return nil, err } @@ -258,12 +269,14 @@ func (self *LightState) CreateStateObject(ctx context.Context, addr common.Addre // Copy creates a copy of the state func (self *LightState) Copy() *LightState { // ignore error - we assume state-to-be-copied always exists - state := NewLightState(common.Hash{}, self.odr) + state := NewLightState(nil, self.odr) state.trie = self.trie + state.id = self.id for k, stateObject := range self.stateObjects { state.stateObjects[k] = stateObject.Copy() } + state.refund.Set(self.refund) return state } @@ -272,4 +285,10 @@ func (self *LightState) Copy() *LightState { func (self *LightState) Set(state *LightState) { self.trie = state.trie self.stateObjects = state.stateObjects + self.refund = state.refund +} + +// GetRefund returns the refund value collected during a vm execution +func (self *LightState) GetRefund() *big.Int { + return self.refund } diff --git a/light/state_object.go b/light/state_object.go index fbb37400cb..9495d344f3 100644 --- a/light/state_object.go +++ b/light/state_object.go @@ -102,7 +102,7 @@ func NewStateObject(address common.Address, odr OdrBackend) *StateObject { codeHash: emptyCodeHash, storage: make(Storage), } - object.trie = NewLightTrie(common.Hash{}, odr, true) + object.trie = NewLightTrie(nil, odr, true) return object } @@ -181,6 +181,9 @@ func (c *StateObject) SetBalance(amount *big.Int) { c.dirty = true } +// ReturnGas returns the gas back to the origin. Used by the Virtual machine or Closures +func (c *StateObject) ReturnGas(gas, price *big.Int) {} + // Copy creates a copy of the state object func (self *StateObject) Copy() *StateObject { stateObject := NewStateObject(self.Address(), self.odr) @@ -235,6 +238,23 @@ func (self *StateObject) Nonce() uint64 { return self.nonce } +// EachStorage calls a callback function for every key/value pair found +// in the local storage cache. Note that unlike core/state.StateObject, +// light.StateObject only returns cached values and doesn't download the +// entire storage tree. +func (self *StateObject) EachStorage(cb func(key, value []byte)) { + for h, v := range self.storage { + cb([]byte(h), v.Bytes()) + } +} + +// Never called, but must be present to allow StateObject to be used +// as a vm.Account interface that also satisfies the vm.ContractRef +// interface. Interfaces are awesome. +func (self *StateObject) Value() *big.Int { + panic("Value on StateObject should never be called") +} + // Encoding type extStateObject struct { @@ -245,7 +265,7 @@ type extStateObject struct { } // DecodeObject decodes an RLP-encoded state object. -func DecodeObject(ctx context.Context, address common.Address, odr OdrBackend, data []byte) (*StateObject, error) { +func DecodeObject(ctx context.Context, stateID *TrieID, address common.Address, odr OdrBackend, data []byte) (*StateObject, error) { var ( obj = &StateObject{address: address, odr: odr, storage: make(Storage)} ext extStateObject @@ -254,9 +274,10 @@ func DecodeObject(ctx context.Context, address common.Address, odr OdrBackend, d if err = rlp.DecodeBytes(data, &ext); err != nil { return nil, err } - obj.trie = NewLightTrie(ext.Root, odr, true) + trieID := StorageTrieID(stateID, address, ext.Root) + obj.trie = NewLightTrie(trieID, odr, true) if !bytes.Equal(ext.CodeHash, emptyCodeHash) { - if obj.code, err = retrieveNodeData(ctx, obj.odr, common.BytesToHash(ext.CodeHash)); err != nil { + if obj.code, err = retrieveContractCode(ctx, obj.odr, trieID, common.BytesToHash(ext.CodeHash)); err != nil { return nil, fmt.Errorf("can't find code for hash %x: %v", ext.CodeHash, err) } } diff --git a/light/state_test.go b/light/state_test.go index 2c2e6daea1..88bf7456b3 100644 --- a/light/state_test.go +++ b/light/state_test.go @@ -22,34 +22,14 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" "golang.org/x/net/context" ) -type testOdr struct { - OdrBackend - sdb, ldb ethdb.Database -} - -func (odr *testOdr) Database() ethdb.Database { - return odr.ldb -} - -func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { - switch req := req.(type) { - case *TrieRequest: - t, _ := trie.New(req.root, odr.sdb) - req.proof = t.Prove(req.key) - trie.ClearGlobalCache() - case *NodeDataRequest: - req.data, _ = odr.sdb.Get(req.hash[:]) - } - req.StoreResult(odr.ldb) - return nil -} - func makeTestState() (common.Hash, ethdb.Database) { sdb, _ := ethdb.NewMemDatabase() st, _ := state.New(common.Hash{}, sdb) @@ -71,9 +51,11 @@ func makeTestState() (common.Hash, ethdb.Database) { func TestLightStateOdr(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root} + core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} - ls := NewLightState(root, odr) + ls := NewLightState(StateTrieID(header), odr) ctx := context.Background() trie.ClearGlobalCache() @@ -156,9 +138,11 @@ func TestLightStateOdr(t *testing.T) { func TestLightStateSetCopy(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root} + core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} - ls := NewLightState(root, odr) + ls := NewLightState(StateTrieID(header), odr) ctx := context.Background() trie.ClearGlobalCache() @@ -233,9 +217,11 @@ func TestLightStateSetCopy(t *testing.T) { func TestLightStateDelete(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root} + core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} - ls := NewLightState(root, odr) + ls := NewLightState(StateTrieID(header), odr) ctx := context.Background() trie.ClearGlobalCache() diff --git a/light/trie.go b/light/trie.go index e9c96ea48e..fa9dcbc791 100644 --- a/light/trie.go +++ b/light/trie.go @@ -17,7 +17,6 @@ package light import ( - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" "golang.org/x/net/context" @@ -25,28 +24,28 @@ import ( // LightTrie is an ODR-capable wrapper around trie.SecureTrie type LightTrie struct { - trie *trie.SecureTrie - originalRoot common.Hash - odr OdrBackend - db ethdb.Database + trie *trie.SecureTrie + id *TrieID + odr OdrBackend + db ethdb.Database } // NewLightTrie creates a new LightTrie instance. It doesn't instantly try to // access the db or network and retrieve the root node, it only initializes its // encapsulated SecureTrie at the first actual operation. -func NewLightTrie(root common.Hash, odr OdrBackend, useFakeMap bool) *LightTrie { +func NewLightTrie(id *TrieID, odr OdrBackend, useFakeMap bool) *LightTrie { return &LightTrie{ // SecureTrie is initialized before first request - originalRoot: root, - odr: odr, - db: odr.Database(), + id: id, + odr: odr, + db: odr.Database(), } } // retrieveKey retrieves a single key, returns true and stores nodes in local // database if successful func (t *LightTrie) retrieveKey(ctx context.Context, key []byte) bool { - r := &TrieRequest{root: t.originalRoot, key: key} + r := &TrieRequest{Id: t.id, Key: key} return t.odr.Retrieve(ctx, r) == nil } @@ -79,7 +78,7 @@ func (t *LightTrie) do(ctx context.Context, fallbackKey []byte, fn func() error) func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.originalRoot, t.db) + t.trie, err = trie.NewSecure(t.id.root, t.db) } if err == nil { res, err = t.trie.TryGet(key) @@ -98,7 +97,7 @@ func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.originalRoot, t.db) + t.trie, err = trie.NewSecure(t.id.root, t.db) } if err == nil { err = t.trie.TryUpdate(key, value) @@ -112,7 +111,7 @@ func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.originalRoot, t.db) + t.trie, err = trie.NewSecure(t.id.root, t.db) } if err == nil { err = t.trie.TryDelete(key) diff --git a/light/txpool.go b/light/txpool.go new file mode 100644 index 0000000000..6e908f5d61 --- /dev/null +++ b/light/txpool.go @@ -0,0 +1,506 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package light + +import ( + "fmt" + "sync" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "golang.org/x/net/context" +) + +// txPermanent is the number of mined blocks after a mined transaction is +// considered permanent and no rollback is expected +var txPermanent = uint64(500) + +// TxPool implements the transaction pool for light clients, which keeps track +// of the status of locally created transactions, detecting if they are included +// in a block (mined) or rolled back. There are no queued transactions since we +// always receive all locally signed transactions in the same order as they are +// created. +type TxPool struct { + quit chan bool + eventMux *event.TypeMux + events event.Subscription + mu sync.RWMutex + chain *LightChain + odr OdrBackend + chainDb ethdb.Database + relay TxRelayBackend + head common.Hash + nonce map[common.Address]uint64 // "pending" nonce + pending map[common.Hash]*types.Transaction // pending transactions by tx hash + mined map[common.Hash][]*types.Transaction // mined transactions by block hash + clearIdx uint64 // earliest block nr that can contain mined tx info + + homestead bool +} + +// TxRelayBackend provides an interface to the mechanism that forwards transacions +// to the ETH network. The implementations of the functions should be non-blocking. +// +// Send instructs backend to forward new transactions +// NewHead notifies backend about a new head after processed by the tx pool, +// including mined and rolled back transactions since the last event +// Discard notifies backend about transactions that should be discarded either +// because they have been replaced by a re-send or because they have been mined +// long ago and no rollback is expected +type TxRelayBackend interface { + Send(txs types.Transactions) + NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) + Discard(hashes []common.Hash) +} + +// NewTxPool creates a new light transaction pool +func NewTxPool(eventMux *event.TypeMux, chain *LightChain, relay TxRelayBackend) *TxPool { + pool := &TxPool{ + nonce: make(map[common.Address]uint64), + pending: make(map[common.Hash]*types.Transaction), + mined: make(map[common.Hash][]*types.Transaction), + quit: make(chan bool), + eventMux: eventMux, + events: eventMux.Subscribe(LightChainHeadEvent{}), + chain: chain, + relay: relay, + odr: chain.Odr(), + chainDb: chain.Odr().Database(), + head: chain.CurrentHeader().Hash(), + clearIdx: chain.CurrentHeader().GetNumberU64(), + } + go pool.eventLoop() + + return pool +} + +// currentState returns the light state of the current head header +func (pool *TxPool) currentState() *LightState { + return NewLightState(StateTrieID(pool.chain.CurrentHeader()), pool.odr) +} + +// GetNonce returns the "pending" nonce of a given address. It always queries +// the nonce belonging to the latest header too in order to detect if another +// client using the same key sent a transaction. +func (pool *TxPool) GetNonce(ctx context.Context, addr common.Address) (uint64, error) { + nonce, err := pool.currentState().GetNonce(ctx, addr) + if err != nil { + return 0, err + } + sn, ok := pool.nonce[addr] + if ok && sn > nonce { + nonce = sn + } + if !ok || sn < nonce { + pool.nonce[addr] = nonce + } + return nonce, nil +} + +type txBlockData struct { + BlockHash common.Hash + BlockIndex uint64 + Index uint64 +} + +// storeTxBlockData stores the block position of a mined tx in the local db +func (pool *TxPool) storeTxBlockData(txh common.Hash, tbd txBlockData) { + data, _ := rlp.EncodeToBytes(tbd) + pool.chainDb.Put(append(txh[:], byte(1)), data) +} + +// removeTxBlockData removes the stored block position of a rolled back tx +func (pool *TxPool) removeTxBlockData(txh common.Hash) { + pool.chainDb.Delete(append(txh[:], byte(1))) +} + +// txStateChanges stores the recent changes between pending/mined states of +// transactions. True means mined, false means rolled back, no entry means no change +type txStateChanges map[common.Hash]bool + +// setState sets the status of a tx to either recently mined or recently rolled back +func (txc txStateChanges) setState(txHash common.Hash, mined bool) { + val, ent := txc[txHash] + if ent && (val != mined) { + delete(txc, txHash) + } else { + txc[txHash] = mined + } +} + +// getLists creates lists of mined and rolled back tx hashes +func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Hash) { + for hash, val := range txc { + if val { + mined = append(mined, hash) + } else { + rollback = append(rollback, hash) + } + } + return +} + +// checkMinedTxs checks newly added blocks for the currently pending transactions +// and marks them as mined if necessary. It also stores block position in the db +// and adds them to the received txStateChanges map. +func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, idx uint64, txc txStateChanges) error { + if len(pool.pending) == 0 { + return nil + } + + receipts, err := GetBlockReceipts(ctx, pool.odr, hash) + if err != nil { + return err + } + list := pool.mined[hash] + for i, receipt := range receipts { + txHash := receipt.TxHash + if tx, ok := pool.pending[txHash]; ok { + pool.storeTxBlockData(txHash, txBlockData{hash, idx, uint64(i)}) + delete(pool.pending, txHash) + list = append(list, tx) + txc.setState(txHash, true) + } + } + if list != nil { + pool.mined[hash] = list + } + return nil +} + +// rollbackTxs marks the transactions contained in recently rolled back blocks +// as rolled back. It also removes block position info from the db and adds them +// to the received txStateChanges map. +func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { + if list, ok := pool.mined[hash]; ok { + for _, tx := range list { + txHash := tx.Hash() + pool.removeTxBlockData(txHash) + pool.pending[txHash] = tx + txc.setState(txHash, false) + } + delete(pool.mined, hash) + } +} + +// setNewHead sets a new head header, processing (and rolling back if necessary) +// the blocks since the last known head and returns a txStateChanges map containing +// the recently mined and rolled back transaction hashes. If an error (context +// timeout) occurs during checking new blocks, it leaves the locally known head +// at the latest checked block and still returns a valid txStateChanges, making it +// possible to continue checking the missing blocks at the next chain head event +func (pool *TxPool) setNewHead(ctx context.Context, newHeader *types.Header) (txStateChanges, error) { + txc := make(txStateChanges) + oldh := pool.chain.GetHeader(pool.head) + newh := newHeader + // find common ancestor, create list of rolled back and new block hashes + var oldHashes, newHashes []common.Hash + for oldh.Hash() != newh.Hash() { + if oldh.GetNumberU64() >= newh.GetNumberU64() { + oldHashes = append(oldHashes, oldh.Hash()) + oldh = pool.chain.GetHeader(oldh.ParentHash) + } + if oldh.GetNumberU64() < newh.GetNumberU64() { + newHashes = append(newHashes, newh.Hash()) + newh = pool.chain.GetHeader(newh.ParentHash) + } + } + if oldh.GetNumberU64() < pool.clearIdx { + pool.clearIdx = oldh.GetNumberU64() + } + // roll back old blocks + for _, hash := range oldHashes { + pool.rollbackTxs(hash, txc) + } + pool.head = oldh.Hash() + // check mined txs of new blocks (array is in reversed order) + for i := len(newHashes) - 1; i >= 0; i-- { + hash := newHashes[i] + if err := pool.checkMinedTxs(ctx, hash, newHeader.GetNumberU64()-uint64(i), txc); err != nil { + return txc, err + } + pool.head = hash + } + + // clear old mined tx entries of old blocks + if idx := newHeader.GetNumberU64(); idx > pool.clearIdx+txPermanent { + idx2 := idx - txPermanent + for i := pool.clearIdx; i < idx2; i++ { + hash := core.GetCanonicalHash(pool.chainDb, i) + if list, ok := pool.mined[hash]; ok { + hashes := make([]common.Hash, len(list)) + for i, tx := range list { + hashes[i] = tx.Hash() + } + pool.relay.Discard(hashes) + delete(pool.mined, hash) + } + } + pool.clearIdx = idx2 + } + + return txc, nil +} + +// blockCheckTimeout is the time limit for checking new blocks for mined +// transactions. Checking resumes at the next chain head event if timed out. +const blockCheckTimeout = time.Second * 3 + +// eventLoop processes chain head events and also notifies the tx relay backend +// about the new head hash and tx state changes +func (pool *TxPool) eventLoop() { + for ev := range pool.events.Chan() { + switch ev.Data.(type) { + case LightChainHeadEvent: + pool.mu.Lock() + ctx, _ := context.WithTimeout(context.Background(), blockCheckTimeout) + head := pool.chain.CurrentHeader() + txc, _ := pool.setNewHead(ctx, head) + m, r := txc.getLists() + pool.relay.NewHead(pool.head, m, r) + pool.homestead = params.IsHomestead(head.Number) + pool.mu.Unlock() + } + } +} + +// Stop stops the light transaction pool +func (pool *TxPool) Stop() { + close(pool.quit) + pool.events.Unsubscribe() + glog.V(logger.Info).Infoln("Transaction pool stopped") +} + +// Stats returns the number of currently pending (locally created) transactions +func (pool *TxPool) Stats() (pending int) { + pool.mu.RLock() + defer pool.mu.RUnlock() + + pending = len(pool.pending) + return +} + +// validateTx checks whether a transaction is valid according to the consensus rules. +func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error { + // Validate sender + var ( + from common.Address + err error + ) + + // Validate the transaction sender and it's sig. Throw + // if the from fields is invalid. + if from, err = tx.From(); err != nil { + return core.ErrInvalidSender + } + + // Make sure the account exist. Non existent accounts + // haven't got funds and well therefor never pass. + currentState := pool.currentState() + if h, err := currentState.HasAccount(ctx, from); err == nil { + if !h { + return core.ErrNonExistentAccount + } + } else { + return err + } + + // Last but not least check for nonce errors + if n, err := currentState.GetNonce(ctx, from); err == nil { + if n > tx.Nonce() { + return core.ErrNonce + } + } else { + return err + } + + // Check the transaction doesn't exceed the current + // block limit gas. + header := pool.chain.GetHeader(pool.head) + if header.GasLimit.Cmp(tx.Gas()) < 0 { + return core.ErrGasLimit + } + + // Transactions can't be negative. This may never happen + // using RLP decoded transactions but may occur if you create + // a transaction using the RPC for example. + if tx.Value().Cmp(common.Big0) < 0 { + return core.ErrNegativeValue + } + + // Transactor should have enough funds to cover the costs + // cost == V + GP * GL + if b, err := currentState.GetBalance(ctx, from); err == nil { + if b.Cmp(tx.Cost()) < 0 { + return core.ErrInsufficientFunds + } + } else { + return err + } + + // Should supply enough intrinsic gas + if tx.Gas().Cmp(core.IntrinsicGas(tx.Data(), core.MessageCreatesContract(tx), pool.homestead)) < 0 { + return core.ErrIntrinsicGas + } + + return nil +} + +// add validates a new transaction and sets its state pending if processable. +// It also updates the locally stored nonce if necessary. +func (self *TxPool) add(ctx context.Context, tx *types.Transaction) error { + hash := tx.Hash() + + if self.pending[hash] != nil { + return fmt.Errorf("Known transaction (%x)", hash[:4]) + } + err := self.validateTx(ctx, tx) + if err != nil { + return err + } + + if _, ok := self.pending[hash]; !ok { + self.pending[hash] = tx + + nonce := tx.Nonce() + 1 + addr, _ := tx.From() + if nonce > self.nonce[addr] { + self.nonce[addr] = nonce + } + + // Notify the subscribers. This event is posted in a goroutine + // because it's possible that somewhere during the post "Remove transaction" + // gets called which will then wait for the global tx pool lock and deadlock. + go self.eventMux.Post(core.TxPreEvent{tx}) + } + + if glog.V(logger.Debug) { + var toname string + if to := tx.To(); to != nil { + toname = common.Bytes2Hex(to[:4]) + } else { + toname = "[NEW_CONTRACT]" + } + // we can ignore the error here because From is + // verified in ValidateTransaction. + f, _ := tx.From() + from := common.Bytes2Hex(f[:4]) + glog.Infof("(t) %x => %s (%v) %x\n", from, toname, tx.Value, hash) + } + + return nil +} + +// Add adds a transaction to the pool if valid and passes it to the tx relay +// backend +func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error { + self.mu.Lock() + defer self.mu.Unlock() + + data, err := rlp.EncodeToBytes(tx) + if err != nil { + return err + } + + if err := self.add(ctx, tx); err != nil { + return err + } + self.relay.Send(types.Transactions{tx}) + + self.chainDb.Put(tx.Hash().Bytes(), data) + return nil +} + +// AddTransactions adds all valid transactions to the pool and passes them to +// the tx relay backend +func (self *TxPool) AddTransactions(ctx context.Context, txs []*types.Transaction) { + self.mu.Lock() + defer self.mu.Unlock() + var sendTx types.Transactions + + for _, tx := range txs { + if err := self.add(ctx, tx); err != nil { + glog.V(logger.Debug).Infoln("tx error:", err) + } else { + sendTx = append(sendTx, tx) + h := tx.Hash() + glog.V(logger.Debug).Infof("tx %x\n", h[:4]) + } + } + + if len(sendTx) > 0 { + self.relay.Send(sendTx) + } +} + +// GetTransaction returns a transaction if it is contained in the pool +// and nil otherwise. +func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction { + // check the txs first + if tx, ok := tp.pending[hash]; ok { + return tx + } + return nil +} + +// GetTransactions returns all currently processable transactions. +// The returned slice may be modified by the caller. +func (self *TxPool) GetTransactions() (txs types.Transactions) { + self.mu.Lock() + defer self.mu.Unlock() + + txs = make(types.Transactions, len(self.pending)) + i := 0 + for _, tx := range self.pending { + txs[i] = tx + i++ + } + return txs +} + +// RemoveTransactions removes all given transactions from the pool. +func (self *TxPool) RemoveTransactions(txs types.Transactions) { + self.mu.Lock() + defer self.mu.Unlock() + var hashes []common.Hash + for _, tx := range txs { + //self.RemoveTx(tx.Hash()) + hash := tx.Hash() + delete(self.pending, hash) + self.chainDb.Delete(hash[:]) + hashes = append(hashes, hash) + } + self.relay.Discard(hashes) +} + +// RemoveTx removes the transaction with the given hash from the pool. +func (pool *TxPool) RemoveTx(hash common.Hash) { + pool.mu.Lock() + defer pool.mu.Unlock() + // delete from pending pool + delete(pool.pending, hash) + pool.chainDb.Delete(hash[:]) + pool.relay.Discard([]common.Hash{hash}) +} diff --git a/light/txpool_test.go b/light/txpool_test.go new file mode 100644 index 0000000000..03402f4c65 --- /dev/null +++ b/light/txpool_test.go @@ -0,0 +1,140 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package light + +import ( + "math" + "math/big" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/params" + "golang.org/x/net/context" +) + +type testTxRelay struct { + send, nhMined, nhRollback, discard int +} + +func (self *testTxRelay) Send(txs types.Transactions) { + self.send = len(txs) +} + +func (self *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) { + self.nhMined = len(mined) + self.nhRollback = len(rollback) +} + +func (self *testTxRelay) Discard(hashes []common.Hash) { + self.discard = len(hashes) +} + +const poolTestTxs = 1000 +const poolTestBlocks = 100 + +// test tx 0..n-1 +var testTx [poolTestTxs]*types.Transaction + +// txs sent before block i +func sentTx(i int) int { + return int(math.Pow(float64(i)/float64(poolTestBlocks), 0.9) * poolTestTxs) +} + +// txs included in block i or before that (minedTx(i) <= sentTx(i)) +func minedTx(i int) int { + return int(math.Pow(float64(i)/float64(poolTestBlocks), 1.1) * poolTestTxs) +} + +func txPoolTestChainGen(i int, block *core.BlockGen) { + s := minedTx(i) + e := minedTx(i + 1) + for i := s; i < e; i++ { + block.AddTx(testTx[i]) + } +} + +func TestTxPool(t *testing.T) { + for i, _ := range testTx { + testTx[i], _ = types.NewTransaction(uint64(i), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) + } + + var ( + evmux = new(event.TypeMux) + pow = new(core.FakePow) + sdb, _ = ethdb.NewMemDatabase() + ldb, _ = ethdb.NewMemDatabase() + genesis = core.WriteGenesisBlockForTesting(sdb, core.GenesisAccount{testBankAddress, testBankFunds}) + ) + core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{testBankAddress, testBankFunds}) + // Assemble the test environment + blockchain, _ := core.NewBlockChain(sdb, pow, evmux) + gchain, _ := core.GenerateChain(genesis, sdb, poolTestBlocks, txPoolTestChainGen) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + + odr := &testOdr{sdb: sdb, ldb: ldb} + relay := &testTxRelay{} + lightchain, _ := NewLightChain(odr, pow, evmux) + lightchain.SetValidator(bproc{}) + txPermanent = 50 + pool := NewTxPool(evmux, lightchain, relay) + + for ii, block := range gchain { + i := ii + 1 + ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) + s := sentTx(i - 1) + e := sentTx(i) + for i := s; i < e; i++ { + relay.send = 0 + pool.Add(ctx, testTx[i]) + got := relay.send + exp := 1 + if got != exp { + t.Errorf("relay.Send expected len = %d, got %d", exp, got) + } + } + + relay.nhMined = 0 + relay.nhRollback = 0 + relay.discard = 0 + if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil { + panic(err) + } + time.Sleep(time.Millisecond * 10) + + got := relay.nhMined + exp := minedTx(i) - minedTx(i-1) + if got != exp { + t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got) + } + + got = relay.discard + exp = 0 + if i > int(txPermanent)+1 { + exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2) + } + if got != exp { + t.Errorf("relay.Discard expected len = %d, got %d", exp, got) + } + } +} diff --git a/light/vm_env.go b/light/vm_env.go new file mode 100644 index 0000000000..493190d74d --- /dev/null +++ b/light/vm_env.go @@ -0,0 +1,241 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package light + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "golang.org/x/net/context" +) + +// VMEnv is the light client version of the vm execution environment. +// Unlike other structures, VMEnv holds a context that is applied by state +// retrieval requests through the entire execution. If any state operation +// returns an error, the execution fails. +type VMEnv struct { + vm.Environment + ctx context.Context + state *VMState + header *types.Header + msg core.Message + depth int + chain *LightChain + typ vm.Type + // structured logging + logs []vm.StructLog + err error +} + +// NewEnv creates a new execution environment based on an ODR capable light state +func NewEnv(ctx context.Context, state *LightState, chain *LightChain, msg core.Message, header *types.Header) *VMEnv { + env := &VMEnv{ + chain: chain, + header: header, + msg: msg, + typ: vm.StdVmTy, + } + env.state = &VMState{ctx: ctx, state: state, env: env} + return env +} + +func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f } +func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number } +func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase } +func (self *VMEnv) Time() *big.Int { return self.header.Time } +func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty } +func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit } +func (self *VMEnv) Value() *big.Int { return self.msg.Value() } +func (self *VMEnv) Db() vm.Database { return self.state } +func (self *VMEnv) Depth() int { return self.depth } +func (self *VMEnv) SetDepth(i int) { self.depth = i } +func (self *VMEnv) VmType() vm.Type { return self.typ } +func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t } +func (self *VMEnv) GetHash(n uint64) common.Hash { + for header := self.chain.GetHeader(self.header.ParentHash); header != nil; header = self.chain.GetHeader(header.ParentHash) { + if header.GetNumberU64() == n { + return header.Hash() + } + } + + return common.Hash{} +} + +func (self *VMEnv) AddLog(log *vm.Log) { + //self.state.AddLog(log) +} +func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool { + return self.state.GetBalance(from).Cmp(balance) >= 0 +} + +func (self *VMEnv) MakeSnapshot() vm.Database { + return &VMState{ctx: self.ctx, state: self.state.state.Copy(), env: self} +} + +func (self *VMEnv) SetSnapshot(copy vm.Database) { + self.state.state.Set(copy.(*VMState).state) +} + +func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) { + core.Transfer(from, to, amount) +} + +func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + return core.Call(self, me, addr, data, gas, price, value) +} +func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + return core.CallCode(self, me, addr, data, gas, price, value) +} + +func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) { + return core.Create(self, me, data, gas, price, value) +} + +func (self *VMEnv) StructLogs() []vm.StructLog { + return self.logs +} + +func (self *VMEnv) AddStructLog(log vm.StructLog) { + self.logs = append(self.logs, log) +} + +// Error returns the error (if any) that happened during execution. +func (self *VMEnv) Error() error { + return self.err +} + +// VMState is a wrapper for the light state that holds the actual context and +// passes it to any state operation that requires it. +type VMState struct { + vm.Database + ctx context.Context + state *LightState + env *VMEnv +} + +// errHandler handles and stores any state error that happens during execution. +func (s *VMState) errHandler(err error) { + if err != nil && s.env.err == nil { + s.env.err = err + } +} + +// GetAccount returns the account object of the given account or nil if the +// account does not exist +func (s *VMState) GetAccount(addr common.Address) vm.Account { + so, err := s.state.GetStateObject(s.ctx, addr) + s.errHandler(err) + return so +} + +// CreateAccount creates creates a new account object and takes ownership. +func (s *VMState) CreateAccount(addr common.Address) vm.Account { + so, err := s.state.CreateStateObject(s.ctx, addr) + s.errHandler(err) + return so +} + +// AddBalance adds the given amount to the balance of the specified account +func (s *VMState) AddBalance(addr common.Address, amount *big.Int) { + err := s.state.AddBalance(s.ctx, addr, amount) + s.errHandler(err) +} + +// GetBalance retrieves the balance from the given address or 0 if the account does +// not exist +func (s *VMState) GetBalance(addr common.Address) *big.Int { + res, err := s.state.GetBalance(s.ctx, addr) + s.errHandler(err) + return res +} + +// GetNonce returns the nonce at the given address or 0 if the account does +// not exist +func (s *VMState) GetNonce(addr common.Address) uint64 { + res, err := s.state.GetNonce(s.ctx, addr) + s.errHandler(err) + return res +} + +// SetNonce sets the nonce of the specified account +func (s *VMState) SetNonce(addr common.Address, nonce uint64) { + err := s.state.SetNonce(s.ctx, addr, nonce) + s.errHandler(err) +} + +// GetCode returns the contract code at the given address or nil if the account +// does not exist +func (s *VMState) GetCode(addr common.Address) []byte { + res, err := s.state.GetCode(s.ctx, addr) + s.errHandler(err) + return res +} + +// SetCode sets the contract code at the specified account +func (s *VMState) SetCode(addr common.Address, code []byte) { + err := s.state.SetCode(s.ctx, addr, code) + s.errHandler(err) +} + +// AddRefund adds an amount to the refund value collected during a vm execution +func (s *VMState) AddRefund(gas *big.Int) { + s.state.AddRefund(gas) +} + +// GetRefund returns the refund value collected during a vm execution +func (s *VMState) GetRefund() *big.Int { + return s.state.GetRefund() +} + +// GetState returns the contract storage value at storage address b from the +// contract address a or common.Hash{} if the account does not exist +func (s *VMState) GetState(a common.Address, b common.Hash) common.Hash { + res, err := s.state.GetState(s.ctx, a, b) + s.errHandler(err) + return res +} + +// SetState sets the storage value at storage address key of the account addr +func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Hash) { + err := s.state.SetState(s.ctx, addr, key, value) + s.errHandler(err) +} + +// Delete marks an account to be removed and clears its balance +func (s *VMState) Delete(addr common.Address) bool { + res, err := s.state.Delete(s.ctx, addr) + s.errHandler(err) + return res +} + +// Exist returns true if an account exists at the given address +func (s *VMState) Exist(addr common.Address) bool { + res, err := s.state.HasAccount(s.ctx, addr) + s.errHandler(err) + return res +} + +// IsDeleted returns true if the given account has been marked for deletion +// or false if the account does not exist +func (s *VMState) IsDeleted(addr common.Address) bool { + res, err := s.state.IsDeleted(s.ctx, addr) + s.errHandler(err) + return res +}