diff options
author | Zsolt Felfoldi <zsfelfoldi@gmail.com> | 2016-10-14 11:47:09 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-11-09 09:12:53 +0800 |
commit | 760fd65487614b7a61443cd9371015925795f40f (patch) | |
tree | a2b122f03b7782e729be85b136f4c5304a164c9e /light | |
parent | 8b1df1a259fe6dc4c15e391e9c0762c9621d9d72 (diff) | |
download | dexon-760fd65487614b7a61443cd9371015925795f40f.tar.gz dexon-760fd65487614b7a61443cd9371015925795f40f.tar.zst dexon-760fd65487614b7a61443cd9371015925795f40f.zip |
light: light chain, VM env and tx pool
Diffstat (limited to 'light')
-rw-r--r-- | light/lightchain.go | 505 | ||||
-rw-r--r-- | light/lightchain_test.go | 403 | ||||
-rw-r--r-- | light/odr.go | 118 | ||||
-rw-r--r-- | light/odr_test.go | 323 | ||||
-rw-r--r-- | light/odr_util.go | 185 | ||||
-rw-r--r-- | light/state.go | 42 | ||||
-rw-r--r-- | light/state_object.go | 42 | ||||
-rw-r--r-- | light/state_test.go | 50 | ||||
-rw-r--r-- | light/trie.go | 25 | ||||
-rw-r--r-- | light/txpool.go | 551 | ||||
-rw-r--r-- | light/txpool_test.go | 140 | ||||
-rw-r--r-- | light/vm_env.go | 271 |
12 files changed, 2558 insertions, 97 deletions
diff --git a/light/lightchain.go b/light/lightchain.go new file mode 100644 index 000000000..88a25ba08 --- /dev/null +++ b/light/lightchain.go @@ -0,0 +1,505 @@ +// 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 <http://www.gnu.org/licenses/>. +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, config *core.ChainConfig, 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(), config, bc.Validator, bc.getProcInterrupt) + bc.SetValidator(core.NewHeaderValidator(config, 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 bc.genesisBlock.Hash() == (common.Hash{212, 229, 103, 64, 248, 118, 174, 248, 192, 16, 184, 106, 64, 213, 245, 103, 69, 161, 24, 208, 144, 106, 52, 230, 154, 236, 140, 13, 177, 203, 143, 163}) { + // add trusted CHT + if config.DAOForkSupport { + WriteTrustedCht(bc.chainDb, TrustedCht{ + Number: 564, + Root: common.HexToHash("ee31f7fc21f627dc2b8d3ed8fed5b74dbc393d146a67249a656e163148e39016"), + }) + } else { + WriteTrustedCht(bc.chainDb, TrustedCht{ + Number: 523, + Root: common.HexToHash("c035076523faf514038f619715de404a65398c51899b5dccca9c05b00bc79315"), + }) + } + glog.V(logger.Info).Infoln("Added trusted CHT for mainnet") + } else { + if bc.genesisBlock.Hash() == (common.Hash{12, 215, 134, 162, 66, 93, 22, 241, 82, 198, 88, 49, 108, 66, 62, 108, 225, 24, 30, 21, 195, 41, 88, 38, 215, 201, 144, 76, 186, 156, 227, 3}) { + // add trusted CHT for testnet + WriteTrustedCht(bc.chainDb, TrustedCht{ + Number: 319, + Root: common.HexToHash("43b679ff9b4918b0b19e6256f20e35877365ec3e20b38e3b2a02cef5606176dc"), + }) + glog.V(logger.Info).Infoln("Added trusted CHT for testnet") + } else { + DeleteTrustedCht(bc.chainDb) + } + } + + 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.GetHeaderByHash(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.GetHeaderByHash(head); header != nil { + self.hc.SetCurrentHeader(header) + } + } + + // Issue a status log and return + header := self.hc.CurrentHeader() + headerTd := self.GetTd(header.Hash(), header.Number.Uint64()) + 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, nil) + 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() + + header := self.hc.CurrentHeader() + hash := header.Hash() + return self.GetTd(hash, header.Number.Uint64()), 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.NumberU64(), 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, self.hc.GetBlockNumber(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, self.hc.GetBlockNumber(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.GetBlockByHash(NoOdr, hash) + return blk != nil +} + +// GetBlock retrieves a block from the database or ODR service by hash and number, +// caching it if found. +func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash, number uint64) (*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, number) + if err != nil { + return nil, err + } + // Cache the found block for next time and return + self.blockCache.Add(block.Hash(), block) + return block, nil +} + +// GetBlockByHash retrieves a block from the database or ODR service by hash, +// caching it if found. +func (self *LightChain) GetBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { + return self.GetBlock(ctx, hash, self.hc.GetBlockNumber(hash)) +} + +// 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, err := GetCanonicalHash(ctx, self.odr, number) + if hash == (common.Hash{}) || err != nil { + return nil, err + } + return self.GetBlock(ctx, hash, number) +} + +// 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 head := self.hc.CurrentHeader(); head.Hash() == hash { + self.hc.SetCurrentHeader(self.GetHeader(head.ParentHash, head.Number.Uint64()-1)) + } + } +} + +// 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.(core.ChainEvent); ok { + if self.LastBlockHash() == event.Hash { + self.eventMux.Post(core.ChainHeadEvent{Block: event.Block}) + } + } + // 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, core.ChainEvent{Block: types.NewBlockWithHeader(header), Hash: 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, core.ChainSideEvent{Block: types.NewBlockWithHeader(header)}) + + case core.SplitStatTy: + events = append(events, core.ChainSplitEvent{Block: types.NewBlockWithHeader(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 and number, caching it if found. +func (self *LightChain) GetTd(hash common.Hash, number uint64) *big.Int { + return self.hc.GetTd(hash, number) +} + +// GetTdByHash retrieves a block's total difficulty in the canonical chain from the +// database by hash, caching it if found. +func (self *LightChain) GetTdByHash(hash common.Hash) *big.Int { + return self.hc.GetTdByHash(hash) +} + +// GetHeader retrieves a block header from the database by hash and number, +// caching it if found. +func (self *LightChain) GetHeader(hash common.Hash, number uint64) *types.Header { + return self.hc.GetHeader(hash, number) +} + +// GetHeaderByHash retrieves a block header from the database by hash, caching it if +// found. +func (self *LightChain) GetHeaderByHash(hash common.Hash) *types.Header { + return self.hc.GetHeaderByHash(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) +} + +// GetHeaderByNumberOdr retrieves a block header from the database or network +// by number, caching it (associated with its hash) if found. +func (self *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64) (*types.Header, error) { + if header := self.hc.GetHeaderByNumber(number); header != nil { + return header, nil + } + return GetHeaderByNumber(ctx, self.odr, number) +} + +func (self *LightChain) SyncCht(ctx context.Context) bool { + headNum := self.CurrentHeader().Number.Uint64() + cht := GetTrustedCht(self.chainDb) + if headNum+1 < cht.Number*ChtFrequency { + num := cht.Number*ChtFrequency - 1 + header, err := GetHeaderByNumber(ctx, self.odr, num) + if header != nil && err == nil { + self.mu.Lock() + if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() { + self.hc.SetCurrentHeader(header) + } + self.mu.Unlock() + return true + } + } + return false +} diff --git a/light/lightchain_test.go b/light/lightchain_test.go new file mode 100644 index 000000000..b7668a3b5 --- /dev/null +++ b/light/lightchain_test.go @@ -0,0 +1,403 @@ +// 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 <http://www.gnu.org/licenses/>. + +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(nil, 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 +} + +func testChainConfig() *core.ChainConfig { + return &core.ChainConfig{HomesteadBlock: big.NewInt(0)} +} + +// 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}, testChainConfig(), 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}, testChainConfig(), 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.GetTdByHash(LightChain.CurrentHeader().Hash()) + if err := testHeaderChainImport(headerChainB, LightChain); err != nil { + t.Fatalf("failed to import forked header chain: %v", err) + } + tdPost = LightChain.GetTdByHash(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.GetHeaderByHash(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(), header.Number.Uint64(), new(big.Int).Add(header.Difficulty, LightChain.GetTdByHash(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, testChainConfig(), 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.GetTdByHash(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}, testChainConfig(), 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 4c69040ef..679569bf9 100644 --- a/light/odr.go +++ b/light/odr.go @@ -19,14 +19,22 @@ 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/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 +45,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 +96,61 @@ 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 + Id *TrieID + Hash common.Hash + Data []byte } -// GetData returns the retrieved node data after a successful request -func (req *NodeDataRequest) GetData() []byte { - return req.data +// StoreResult stores the retrieved data in local database +func (req *CodeRequest) StoreResult(db ethdb.Database) { + db.Put(req.Hash[:], req.Data) +} + +// BlockRequest is the ODR request type for retrieving block bodies +type BlockRequest struct { + OdrRequest + Hash common.Hash + Number uint64 + Rlp []byte } // StoreResult stores the retrieved data in local database -func (req *NodeDataRequest) StoreResult(db ethdb.Database) { - db.Put(req.hash[:], req.GetData()) +func (req *BlockRequest) StoreResult(db ethdb.Database) { + core.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp) } -var sha3_nil = crypto.Keccak256Hash(nil) +// ReceiptsRequest is the ODR request type for retrieving block bodies +type ReceiptsRequest struct { + OdrRequest + Hash common.Hash + Number uint64 + Receipts types.Receipts +} -// 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 - } +// StoreResult stores the retrieved data in local database +func (req *ReceiptsRequest) StoreResult(db ethdb.Database) { + core.WriteBlockReceipts(db, req.Hash, req.Number, req.Receipts) +} + +// TrieRequest is the ODR request type for state/storage trie entries +type ChtRequest struct { + OdrRequest + ChtNum, BlockNum uint64 + ChtRoot common.Hash + Header *types.Header + Td *big.Int + Proof []rlp.RawValue +} + +// StoreResult stores the retrieved data in local database +func (req *ChtRequest) StoreResult(db ethdb.Database) { + // if there is a canonical hash, there is a header too + core.WriteHeader(db, req.Header) + hash, num := req.Header.Hash(), req.Header.Number.Uint64() + core.WriteTd(db, hash, num, req.Td) + core.WriteCanonicalHash(db, hash, num) + //storeProof(db, req.Proof) } diff --git a/light/odr_test.go b/light/odr_test.go new file mode 100644 index 000000000..1cf9bce3c --- /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/core/vm" + "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, core.GetBlockNumber(odr.sdb, req.Hash)) + case *ReceiptsRequest: + req.Receipts = core.GetBlockReceipts(odr.sdb, req.Hash, core.GetBlockNumber(odr.sdb, req.Hash)) + case *TrieRequest: + t, _ := trie.New(req.Id.Root, odr.sdb) + req.Proof = t.Prove(req.Key) + 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.GetBlockByHash(bhash) + } else { + block, _ = lc.GetBlockByHash(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, core.GetBlockNumber(db, bhash)) + } else { + receipts, _ = GetBlockReceipts(ctx, lc.Odr(), bhash, core.GetBlockNumber(db, 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} + + var res []byte + for _, addr := range acc { + if bc != nil { + header := bc.GetHeaderByHash(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.GetHeaderByHash(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 0 } +func (m fullcallmsg) CheckNonce() bool { return false } +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 0 } +func (m lightcallmsg) CheckNonce() bool { return false } +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.GetHeaderByHash(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, testChainConfig(), bc, msg, header, vm.Config{}) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + res = append(res, ret...) + } + } else { + header := lc.GetHeaderByHash(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, testChainConfig(), lc, msg, header, vm.Config{}) + 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(1000000), 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{Address: testBankAddress, Balance: testBankFunds}) + ) + core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) + // Assemble the test environment + blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux) + gchain, _ := core.GenerateChain(nil, genesis, sdb, 4, testChainGen) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + + odr := &testOdr{sdb: sdb, ldb: ldb} + lightchain, _ := NewLightChain(odr, testChainConfig(), 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 000000000..458f8233b --- /dev/null +++ b/light/odr_util.go @@ -0,0 +1,185 @@ +// 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 <http://www.gnu.org/licenses/>. +package light + +import ( + "bytes" + "errors" + "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/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "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 ( + ErrNoTrustedCht = errors.New("No trusted canonical hash trie") + ErrNoHeader = errors.New("Header not found") + + ChtFrequency = uint64(4096) + trustedChtKey = []byte("TrustedCHT") +) + +type ChtNode struct { + Hash common.Hash + Td *big.Int +} + +type TrustedCht struct { + Number uint64 + Root common.Hash +} + +func GetTrustedCht(db ethdb.Database) TrustedCht { + data, _ := db.Get(trustedChtKey) + var res TrustedCht + if err := rlp.DecodeBytes(data, &res); err != nil { + return TrustedCht{0, common.Hash{}} + } + return res +} + +func WriteTrustedCht(db ethdb.Database, cht TrustedCht) { + data, _ := rlp.EncodeToBytes(cht) + db.Put(trustedChtKey, data) +} + +func DeleteTrustedCht(db ethdb.Database) { + db.Delete(trustedChtKey) +} + +func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) { + db := odr.Database() + hash := core.GetCanonicalHash(db, number) + if (hash != common.Hash{}) { + // if there is a canonical hash, there is a header too + header := core.GetHeader(db, hash, number) + if header == nil { + panic("Canonical hash present but header not found") + } + return header, nil + } + + cht := GetTrustedCht(db) + if number >= cht.Number*ChtFrequency { + return nil, ErrNoTrustedCht + } + + r := &ChtRequest{ChtRoot: cht.Root, ChtNum: cht.Number, BlockNum: number} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Header, nil + } +} + +func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) { + hash := core.GetCanonicalHash(odr.Database(), number) + if (hash != common.Hash{}) { + return hash, nil + } + header, err := GetHeaderByNumber(ctx, odr, number) + if header != nil { + return header.Hash(), nil + } + return common.Hash{}, err +} + +// 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, number uint64) (rlp.RawValue, error) { + if data := core.GetBodyRLP(odr.Database(), hash, number); data != nil { + return data, nil + } + r := &BlockRequest{Hash: hash, Number: number} + 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, number uint64) (*types.Body, error) { + data, err := GetBodyRLP(ctx, odr, hash, number) + 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, number uint64) (*types.Block, error) { + // Retrieve the block header and body contents + header := core.GetHeader(odr.Database(), hash, number) + if header == nil { + return nil, ErrNoHeader + } + body, err := GetBody(ctx, odr, hash, number) + 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, number uint64) (types.Receipts, error) { + receipts := core.GetBlockReceipts(odr.Database(), hash, number) + if receipts != nil { + return receipts, nil + } + r := &ReceiptsRequest{Hash: hash, Number: number} + 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 4f2177238..88f60efbb 100644 --- a/light/state.go +++ b/light/state.go @@ -20,6 +20,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "golang.org/x/net/context" @@ -33,10 +34,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 +46,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) @@ -109,9 +121,9 @@ func (self *LightState) GetState(ctx context.Context, a common.Address, b common return common.Hash{}, err } -// IsDeleted returns true if the given account has been marked for deletion +// HasSuicided returns true if the given account has been marked for deletion // or false if the account does not exist -func (self *LightState) IsDeleted(ctx context.Context, addr common.Address) (bool, error) { +func (self *LightState) HasSuicided(ctx context.Context, addr common.Address) (bool, error) { stateObject, err := self.GetStateObject(ctx, addr) if err == nil && stateObject != nil { return stateObject.remove, nil @@ -145,7 +157,7 @@ func (self *LightState) SetNonce(ctx context.Context, addr common.Address, nonce func (self *LightState) SetCode(ctx context.Context, addr common.Address, code []byte) error { stateObject, err := self.GetOrNewStateObject(ctx, addr) if err == nil && stateObject != nil { - stateObject.SetCode(code) + stateObject.SetCode(crypto.Keccak256Hash(code), code) } return err } @@ -160,7 +172,7 @@ func (self *LightState) SetState(ctx context.Context, addr common.Address, key c } // Delete marks an account to be removed and clears its balance -func (self *LightState) Delete(ctx context.Context, addr common.Address) (bool, error) { +func (self *LightState) Suicide(ctx context.Context, addr common.Address) (bool, error) { stateObject, err := self.GetOrNewStateObject(ctx, addr) if err == nil && stateObject != nil { stateObject.MarkForDeletion() @@ -194,7 +206,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,14 +270,16 @@ 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 { if stateObject.dirty { state.stateObjects[k] = stateObject.Copy() } } + state.refund.Set(self.refund) return state } @@ -274,4 +288,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 1e9c7f4b1..61c3888fe 100644 --- a/light/state_object.go +++ b/light/state_object.go @@ -40,7 +40,7 @@ func (self Code) String() string { } // Storage is a memory map cache of a contract storage -type Storage map[string]common.Hash +type Storage map[common.Hash]common.Hash // String returns a string representation of the storage cache func (self Storage) String() (str string) { @@ -100,7 +100,7 @@ func NewStateObject(address common.Address, odr OdrBackend) *StateObject { codeHash: emptyCodeHash, storage: make(Storage), } - object.trie = NewLightTrie(common.Hash{}, odr, true) + object.trie = NewLightTrie(&TrieID{}, odr, true) return object } @@ -133,8 +133,7 @@ func (self *StateObject) Storage() Storage { // GetState returns the storage value at the given address from either the cache // or the trie func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common.Hash, error) { - strkey := key.Str() - value, exists := self.storage[strkey] + value, exists := self.storage[key] if !exists { var err error value, err = self.getAddr(ctx, key) @@ -142,7 +141,7 @@ func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common. return common.Hash{}, err } if (value != common.Hash{}) { - self.storage[strkey] = value + self.storage[key] = value } } @@ -151,7 +150,7 @@ func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common. // SetState sets the storage value at the given address func (self *StateObject) SetState(k, value common.Hash) { - self.storage[k.Str()] = value + self.storage[k] = value self.dirty = true } @@ -179,6 +178,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) @@ -215,9 +217,9 @@ func (self *StateObject) Code() []byte { } // SetCode sets the contract code -func (self *StateObject) SetCode(code []byte) { +func (self *StateObject) SetCode(hash common.Hash, code []byte) { self.code = code - self.codeHash = crypto.Keccak256(code) + self.codeHash = hash[:] self.dirty = true } @@ -232,6 +234,23 @@ func (self *StateObject) Nonce() uint64 { return self.nonce } +// ForEachStorage 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) ForEachStorage(cb func(key, value common.Hash) bool) { + for h, v := range self.storage { + cb(h, v) + } +} + +// 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 { @@ -242,7 +261,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 @@ -251,9 +270,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 a6b115786..89a64483d 100644 --- a/light/state_test.go +++ b/light/state_test.go @@ -22,33 +22,13 @@ 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) - 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) @@ -67,9 +47,11 @@ func makeTestState() (common.Hash, ethdb.Database) { func TestLightStateOdr(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root, Number: big.NewInt(0)} + 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() for i := byte(0); i < 100; i++ { @@ -151,9 +133,11 @@ func TestLightStateOdr(t *testing.T) { func TestLightStateSetCopy(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root, Number: big.NewInt(0)} + 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() for i := byte(0); i < 100; i++ { @@ -227,9 +211,11 @@ func TestLightStateSetCopy(t *testing.T) { func TestLightStateDelete(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root, Number: big.NewInt(0)} + 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() addr := common.Address{42} @@ -242,21 +228,21 @@ func TestLightStateDelete(t *testing.T) { t.Fatalf("HasAccount returned false, expected true") } - b, err = ls.IsDeleted(ctx, addr) + b, err = ls.HasSuicided(ctx, addr) if err != nil { - t.Fatalf("IsDeleted error: %v", err) + t.Fatalf("HasSuicided error: %v", err) } if b { - t.Fatalf("IsDeleted returned true, expected false") + t.Fatalf("HasSuicided returned true, expected false") } - ls.Delete(ctx, addr) + ls.Suicide(ctx, addr) - b, err = ls.IsDeleted(ctx, addr) + b, err = ls.HasSuicided(ctx, addr) if err != nil { - t.Fatalf("IsDeleted error: %v", err) + t.Fatalf("HasSuicided error: %v", err) } if !b { - t.Fatalf("IsDeleted returned false, expected true") + t.Fatalf("HasSuicided returned false, expected true") } } diff --git a/light/trie.go b/light/trie.go index 42a943d50..c5525358a 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, 0) + t.trie, err = trie.NewSecure(t.id.Root, t.db, 0) } 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, 0) + t.trie, err = trie.NewSecure(t.id.Root, t.db, 0) } 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, 0) + t.trie, err = trie.NewSecure(t.id.Root, t.db, 0) } if err == nil { err = t.trie.TryDelete(key) diff --git a/light/txpool.go b/light/txpool.go new file mode 100644 index 000000000..01fd54597 --- /dev/null +++ b/light/txpool.go @@ -0,0 +1,551 @@ +// 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 <http://www.gnu.org/licenses/>. + +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/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 { + config *core.ChainConfig + 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(config *core.ChainConfig, eventMux *event.TypeMux, chain *LightChain, relay TxRelayBackend) *TxPool { + pool := &TxPool{ + config: config, + 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(core.ChainHeadEvent{}), + 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) { + //fmt.Println("storeTxBlockData", txh, tbd) + 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) { + //fmt.Println("removeTxBlockData", txh) + 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 { + //fmt.Println("checkMinedTxs") + if len(pool.pending) == 0 { + return nil + } + //fmt.Println("len(pool) =", len(pool.pending)) + + block, err := GetBlock(ctx, pool.odr, hash, idx) + var receipts types.Receipts + if err != nil { + //fmt.Println(err) + return err + } + //fmt.Println("len(block.Transactions()) =", len(block.Transactions())) + + list := pool.mined[hash] + for i, tx := range block.Transactions() { + txHash := tx.Hash() + //fmt.Println(" txHash:", txHash) + if tx, ok := pool.pending[txHash]; ok { + //fmt.Println("TX FOUND") + if receipts == nil { + receipts, err = GetBlockReceipts(ctx, pool.odr, hash, idx) + if err != nil { + return err + } + if len(receipts) != len(block.Transactions()) { + panic(nil) // should never happen if hashes did match + } + core.SetReceiptsData(block, receipts) + } + //fmt.Println("WriteReceipt", receipts[i].TxHash) + core.WriteReceipt(pool.chainDb, receipts[i]) + 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.GetHeaderByHash(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, oldh.Number.Uint64()-1) + } + if oldh.GetNumberU64() < newh.GetNumberU64() { + newHashes = append(newHashes, newh.Hash()) + newh = pool.chain.GetHeader(newh.ParentHash, newh.Number.Uint64()-1) + if newh == nil { + // happens when CHT syncing, nothing to do + newh = oldh + } + } + } + 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 core.ChainHeadEvent: + 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 = pool.config.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.GetHeaderByHash(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: 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 + } + //fmt.Println("Send", tx.Hash()) + 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) AddBatch(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.RLock() + defer self.mu.RUnlock() + + txs = make(types.Transactions, len(self.pending)) + i := 0 + for _, tx := range self.pending { + txs[i] = tx + i++ + } + return txs +} + +// Content retrieves the data content of the transaction pool, returning all the +// pending as well as queued transactions, grouped by account and nonce. +func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { + self.mu.RLock() + defer self.mu.RUnlock() + + // Retrieve all the pending transactions and sort by account and by nonce + pending := make(map[common.Address]types.Transactions) + for _, tx := range self.pending { + account, _ := tx.From() + pending[account] = append(pending[account], tx) + } + // There are no queued transactions in a light pool, just return an empty map + queued := make(map[common.Address]types.Transactions) + return pending, queued +} + +// 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 000000000..2aee4bedb --- /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 <http://www.gnu.org/licenses/>. + +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{Address: testBankAddress, Balance: testBankFunds}) + ) + core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{Address: testBankAddress, Balance: testBankFunds}) + // Assemble the test environment + blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux) + gchain, _ := core.GenerateChain(nil, genesis, sdb, poolTestBlocks, txPoolTestChainGen) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + + odr := &testOdr{sdb: sdb, ldb: ldb} + relay := &testTxRelay{} + lightchain, _ := NewLightChain(odr, testChainConfig(), pow, evmux) + lightchain.SetValidator(bproc{}) + txPermanent = 50 + pool := NewTxPool(testChainConfig(), 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 * 30) + + 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 000000000..0f4b90908 --- /dev/null +++ b/light/vm_env.go @@ -0,0 +1,271 @@ +// 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 <http://www.gnu.org/licenses/>. + +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" + "github.com/ethereum/go-ethereum/crypto" + "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 + chainConfig *core.ChainConfig + evm *vm.EVM + state *VMState + header *types.Header + msg core.Message + depth int + chain *LightChain + err error +} + +// NewEnv creates a new execution environment based on an ODR capable light state +func NewEnv(ctx context.Context, state *LightState, chainConfig *core.ChainConfig, chain *LightChain, msg core.Message, header *types.Header, cfg vm.Config) *VMEnv { + env := &VMEnv{ + chainConfig: chainConfig, + chain: chain, + header: header, + msg: msg, + } + env.state = &VMState{ctx: ctx, state: state, env: env} + + env.evm = vm.New(env, cfg) + return env +} + +func (self *VMEnv) RuleSet() vm.RuleSet { return self.chainConfig } +func (self *VMEnv) Vm() vm.Vm { return self.evm } +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) 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) GetHash(n uint64) common.Hash { + for header := self.chain.GetHeader(self.header.ParentHash, self.header.Number.Uint64()-1); header != nil; header = self.chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) { + 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) SnapshotDatabase() int { + return self.state.SnapshotDatabase() +} + +func (self *VMEnv) RevertToSnapshot(idx int) { + self.state.RevertToSnapshot(idx) +} + +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) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) { + return core.DelegateCall(self, me, addr, data, gas, price) +} + +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) +} + +// 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 + snapshots []*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 + } +} + +func (self *VMState) SnapshotDatabase() int { + self.snapshots = append(self.snapshots, self.state.Copy()) + return len(self.snapshots) - 1 +} + +func (self *VMState) RevertToSnapshot(idx int) { + self.state.Set(self.snapshots[idx]) + self.snapshots = self.snapshots[:idx] +} + +// 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) + if err != nil { + // return a dummy state object to avoid panics + so = s.state.newStateObject(addr) + } + 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) + if err != nil { + // return a dummy state object to avoid panics + so = s.state.newStateObject(addr) + } + 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 +} + +// GetCodeHash returns the contract code hash at the given address +func (s *VMState) GetCodeHash(addr common.Address) common.Hash { + res, err := s.state.GetCode(s.ctx, addr) + s.errHandler(err) + return crypto.Keccak256Hash(res) +} + +// GetCodeSize returns the contract code size at the given address +func (s *VMState) GetCodeSize(addr common.Address) int { + res, err := s.state.GetCode(s.ctx, addr) + s.errHandler(err) + return len(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) +} + +// Suicide marks an account to be removed and clears its balance +func (s *VMState) Suicide(addr common.Address) bool { + res, err := s.state.Suicide(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 +} + +// HasSuicided returns true if the given account has been marked for deletion +// or false if the account does not exist +func (s *VMState) HasSuicided(addr common.Address) bool { + res, err := s.state.HasSuicided(s.ctx, addr) + s.errHandler(err) + return res +} |