diff options
-rw-r--r-- | .travis.yml | 9 | ||||
-rw-r--r-- | cmd/ethtest/main.go | 4 | ||||
-rw-r--r-- | core/canary.go | 21 | ||||
-rw-r--r-- | miner/agent.go | 24 | ||||
-rw-r--r-- | miner/remote_agent.go | 96 | ||||
-rw-r--r-- | miner/worker.go | 141 | ||||
-rw-r--r-- | rlp/decode.go | 20 | ||||
-rw-r--r-- | rlp/decode_test.go | 19 | ||||
-rw-r--r-- | tests/init.go | 1 | ||||
-rw-r--r-- | tests/rlp_test.go | 20 | ||||
-rw-r--r-- | tests/rlp_test_util.go | 172 | ||||
-rw-r--r-- | xeth/xeth.go | 12 |
12 files changed, 406 insertions, 133 deletions
diff --git a/.travis.yml b/.travis.yml index ff3ff71f7..2b3ff92f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ language: go go: - 1.4.2 -before_install: - - sudo apt-get update -qq - - sudo apt-get install -yqq libgmp3-dev install: # - go get code.google.com/p/go.tools/cmd/goimports # - go get github.com/golang/lint/golint @@ -22,7 +19,11 @@ after_success: env: global: - secure: "U2U1AmkU4NJBgKR/uUAebQY87cNL0+1JHjnLOmmXwxYYyj5ralWb1aSuSH3qSXiT93qLBmtaUkuv9fberHVqrbAeVlztVdUsKAq7JMQH+M99iFkC9UiRMqHmtjWJ0ok4COD1sRYixxi21wb/JrMe3M1iL4QJVS61iltjHhVdM64=" - +sudo: false +addons: + apt: + packages: + - libgmp3-dev notifications: webhooks: urls: diff --git a/cmd/ethtest/main.go b/cmd/ethtest/main.go index 61276b177..0d6286407 100644 --- a/cmd/ethtest/main.go +++ b/cmd/ethtest/main.go @@ -35,7 +35,7 @@ var ( testExtension = ".json" defaultTest = "all" defaultDir = "." - allTests = []string{"BlockTests", "StateTests", "TransactionTests", "VMTests"} + allTests = []string{"BlockTests", "StateTests", "TransactionTests", "VMTests", "RLPTests"} skipTests = []string{} TestFlag = cli.StringFlag{ @@ -75,6 +75,8 @@ func runTestWithReader(test string, r io.Reader) error { err = tests.RunTransactionTestsWithReader(r, skipTests) case "vm", "vmtest", "vmtests": err = tests.RunVmTestWithReader(r, skipTests) + case "rlp", "rlptest", "rlptests": + err = tests.RunRLPTestWithReader(r, skipTests) default: err = fmt.Errorf("Invalid test type specified: %v", test) } diff --git a/core/canary.go b/core/canary.go index 710e31530..b69621a6a 100644 --- a/core/canary.go +++ b/core/canary.go @@ -34,11 +34,18 @@ var ( // If two or more are set to anything other than a 0 the canary // dies a horrible death. func Canary(statedb *state.StateDB) bool { - r := new(big.Int) - r.Add(r, statedb.GetState(jeff, common.Hash{}).Big()) - r.Add(r, statedb.GetState(vitalik, common.Hash{}).Big()) - r.Add(r, statedb.GetState(christoph, common.Hash{}).Big()) - r.Add(r, statedb.GetState(gav, common.Hash{}).Big()) - - return r.Cmp(big.NewInt(1)) > 0 + var r int + if (statedb.GetState(jeff, common.Hash{}).Big().Cmp(big.NewInt(0)) > 0) { + r++ + } + if (statedb.GetState(gav, common.Hash{}).Big().Cmp(big.NewInt(0)) > 0) { + r++ + } + if (statedb.GetState(christoph, common.Hash{}).Big().Cmp(big.NewInt(0)) > 0) { + r++ + } + if (statedb.GetState(vitalik, common.Hash{}).Big().Cmp(big.NewInt(0)) > 0) { + r++ + } + return r > 1 } diff --git a/miner/agent.go b/miner/agent.go index 8455ed36e..370e611f8 100644 --- a/miner/agent.go +++ b/miner/agent.go @@ -20,7 +20,6 @@ import ( "sync" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/pow" @@ -29,10 +28,10 @@ import ( type CpuAgent struct { mu sync.Mutex - workCh chan *types.Block + workCh chan *Work quit chan struct{} quitCurrentOp chan struct{} - returnCh chan<- *types.Block + returnCh chan<- *Result index int pow pow.PoW @@ -47,9 +46,9 @@ func NewCpuAgent(index int, pow pow.PoW) *CpuAgent { return miner } -func (self *CpuAgent) Work() chan<- *types.Block { return self.workCh } -func (self *CpuAgent) Pow() pow.PoW { return self.pow } -func (self *CpuAgent) SetReturnCh(ch chan<- *types.Block) { self.returnCh = ch } +func (self *CpuAgent) Work() chan<- *Work { return self.workCh } +func (self *CpuAgent) Pow() pow.PoW { return self.pow } +func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch } func (self *CpuAgent) Stop() { self.mu.Lock() @@ -65,7 +64,7 @@ func (self *CpuAgent) Start() { self.quit = make(chan struct{}) // creating current op ch makes sure we're not closing a nil ch // later on - self.workCh = make(chan *types.Block, 1) + self.workCh = make(chan *Work, 1) go self.update() } @@ -74,13 +73,13 @@ func (self *CpuAgent) update() { out: for { select { - case block := <-self.workCh: + case work := <-self.workCh: self.mu.Lock() if self.quitCurrentOp != nil { close(self.quitCurrentOp) } self.quitCurrentOp = make(chan struct{}) - go self.mine(block, self.quitCurrentOp) + go self.mine(work, self.quitCurrentOp) self.mu.Unlock() case <-self.quit: self.mu.Lock() @@ -106,13 +105,14 @@ done: } } -func (self *CpuAgent) mine(block *types.Block, stop <-chan struct{}) { +func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) { glog.V(logger.Debug).Infof("(re)started agent[%d]. mining...\n", self.index) // Mine - nonce, mixDigest := self.pow.Search(block, stop) + nonce, mixDigest := self.pow.Search(work.Block, stop) if nonce != 0 { - self.returnCh <- block.WithMiningResult(nonce, common.BytesToHash(mixDigest)) + block := work.Block.WithMiningResult(nonce, common.BytesToHash(mixDigest)) + self.returnCh <- &Result{work, block} } else { self.returnCh <- nil } diff --git a/miner/remote_agent.go b/miner/remote_agent.go index b05d9c7e0..fdd9a6aef 100644 --- a/miner/remote_agent.go +++ b/miner/remote_agent.go @@ -18,39 +18,44 @@ package miner import ( "math/big" + "sync" + "time" "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" ) type RemoteAgent struct { - work *types.Block - currentWork *types.Block + mu sync.Mutex quit chan struct{} - workCh chan *types.Block - returnCh chan<- *types.Block + workCh chan *Work + returnCh chan<- *Result + + currentWork *Work + work map[common.Hash]*Work } func NewRemoteAgent() *RemoteAgent { - agent := &RemoteAgent{} + agent := &RemoteAgent{work: make(map[common.Hash]*Work)} return agent } -func (a *RemoteAgent) Work() chan<- *types.Block { +func (a *RemoteAgent) Work() chan<- *Work { return a.workCh } -func (a *RemoteAgent) SetReturnCh(returnCh chan<- *types.Block) { +func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result) { a.returnCh = returnCh } func (a *RemoteAgent) Start() { a.quit = make(chan struct{}) - a.workCh = make(chan *types.Block, 1) - go a.run() + a.workCh = make(chan *Work, 1) + go a.maintainLoop() } func (a *RemoteAgent) Stop() { @@ -60,47 +65,72 @@ func (a *RemoteAgent) Stop() { func (a *RemoteAgent) GetHashRate() int64 { return 0 } -func (a *RemoteAgent) run() { -out: - for { - select { - case <-a.quit: - break out - case work := <-a.workCh: - a.work = work - } - } -} - func (a *RemoteAgent) GetWork() [3]string { + a.mu.Lock() + defer a.mu.Unlock() + var res [3]string - if a.work != nil { - a.currentWork = a.work + if a.currentWork != nil { + block := a.currentWork.Block - res[0] = a.work.HashNoNonce().Hex() - seedHash, _ := ethash.GetSeedHash(a.currentWork.NumberU64()) + res[0] = block.HashNoNonce().Hex() + seedHash, _ := ethash.GetSeedHash(block.NumberU64()) res[1] = common.BytesToHash(seedHash).Hex() // Calculate the "target" to be returned to the external miner n := big.NewInt(1) n.Lsh(n, 255) - n.Div(n, a.work.Difficulty()) + n.Div(n, block.Difficulty()) n.Lsh(n, 1) res[2] = common.BytesToHash(n.Bytes()).Hex() + + a.work[block.HashNoNonce()] = a.currentWork } return res } -func (a *RemoteAgent) SubmitWork(nonce uint64, mixDigest, seedHash common.Hash) bool { - // Return true or false, but does not indicate if the PoW was correct +// Returns true or false, but does not indicate if the PoW was correct +func (a *RemoteAgent) SubmitWork(nonce uint64, mixDigest, hash common.Hash) bool { + a.mu.Lock() + defer a.mu.Unlock() + + // Make sure the work submitted is present + if a.work[hash] != nil { + block := a.work[hash].Block.WithMiningResult(nonce, mixDigest) + a.returnCh <- &Result{a.work[hash], block} + + delete(a.work, hash) - // Make sure the external miner was working on the right hash - if a.currentWork != nil && a.work != nil { - a.returnCh <- a.currentWork.WithMiningResult(nonce, mixDigest) - //a.returnCh <- Work{a.currentWork.Number().Uint64(), nonce, mixDigest.Bytes(), seedHash.Bytes()} return true + } else { + glog.V(logger.Info).Infof("Work was submitted for %x but no pending work found\n", hash) } return false } + +func (a *RemoteAgent) maintainLoop() { + ticker := time.Tick(5 * time.Second) + +out: + for { + select { + case <-a.quit: + break out + case work := <-a.workCh: + a.mu.Lock() + a.currentWork = work + a.mu.Unlock() + case <-ticker: + // cleanup + a.mu.Lock() + for hash, work := range a.work { + if time.Since(work.createdAt) > 7*(12*time.Second) { + delete(a.work, hash) + } + } + a.mu.Unlock() + } + } +} diff --git a/miner/worker.go b/miner/worker.go index 9f804bf30..a4ebe6fe7 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -38,25 +38,20 @@ import ( var jsonlogger = logger.NewJsonLogger() -// Work holds the current work -type Work struct { - Number uint64 - Nonce uint64 - MixDigest []byte - SeedHash []byte -} +const ( + resultQueueSize = 10 + miningLogAtDepth = 5 +) // Agent can register themself with the worker type Agent interface { - Work() chan<- *types.Block - SetReturnCh(chan<- *types.Block) + Work() chan<- *Work + SetReturnCh(chan<- *Result) Stop() Start() GetHashRate() int64 } -const miningLogAtDepth = 5 - type uint64RingBuffer struct { ints []uint64 //array of all integers in buffer next int //where is the next insertion? assert 0 <= next < len(ints) @@ -64,7 +59,7 @@ type uint64RingBuffer struct { // environment is the workers current environment and holds // all of the current state information -type environment struct { +type Work struct { state *state.StateDB // apply state changes here coinbase *state.StateObject // the miner's account ancestors *set.Set // ancestor set (used for checking uncle parent validity) @@ -78,11 +73,18 @@ type environment struct { lowGasTxs types.Transactions localMinedBlocks *uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion) - block *types.Block // the new block + Block *types.Block // the new block header *types.Header txs []*types.Transaction receipts []*types.Receipt + + createdAt time.Time +} + +type Result struct { + Work *Work + Block *types.Block } // worker is the main object which takes care of applying messages to the new state @@ -90,7 +92,7 @@ type worker struct { mu sync.Mutex agents []Agent - recv chan *types.Block + recv chan *Result mux *event.TypeMux quit chan struct{} pow pow.PoW @@ -105,7 +107,7 @@ type worker struct { extra []byte currentMu sync.Mutex - current *environment + current *Work uncleMu sync.Mutex possibleUncles map[common.Hash]*types.Block @@ -116,6 +118,8 @@ type worker struct { // atomic status counters mining int32 atWork int32 + + fullValidation bool } func newWorker(coinbase common.Address, eth core.Backend) *worker { @@ -123,7 +127,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { eth: eth, mux: eth.EventMux(), extraDb: eth.ExtraDb(), - recv: make(chan *types.Block), + recv: make(chan *Result, resultQueueSize), gasPrice: new(big.Int), chain: eth.ChainManager(), proc: eth.BlockProcessor(), @@ -131,6 +135,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { coinbase: coinbase, txQueue: make(map[common.Hash]*types.Transaction), quit: make(chan struct{}), + fullValidation: false, } go worker.update() go worker.wait() @@ -155,6 +160,7 @@ func (self *worker) pendingState() *state.StateDB { func (self *worker) pendingBlock() *types.Block { self.currentMu.Lock() defer self.currentMu.Unlock() + if atomic.LoadInt32(&self.mining) == 0 { return types.NewBlock( self.current.header, @@ -163,7 +169,7 @@ func (self *worker) pendingBlock() *types.Block { self.current.receipts, ) } - return self.current.block + return self.current.Block } func (self *worker) start() { @@ -223,9 +229,9 @@ out: case core.TxPreEvent: // Apply transaction to the pending state if we're not mining if atomic.LoadInt32(&self.mining) == 0 { - self.mu.Lock() + self.currentMu.Lock() self.current.commitTransactions(types.Transactions{ev.Tx}, self.gasPrice, self.proc) - self.mu.Unlock() + self.currentMu.Unlock() } } case <-self.quit: @@ -250,34 +256,54 @@ func newLocalMinedBlock(blockNumber uint64, prevMinedBlocks *uint64RingBuffer) ( func (self *worker) wait() { for { - for block := range self.recv { + for result := range self.recv { atomic.AddInt32(&self.atWork, -1) - if block == nil { + if result == nil { continue } + block := result.Block - parent := self.chain.GetBlock(block.ParentHash()) - if parent == nil { - glog.V(logger.Error).Infoln("Invalid block found during mining") - continue - } - if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true); err != nil && err != core.BlockFutureErr { - glog.V(logger.Error).Infoln("Invalid header on mined block:", err) - continue - } + self.current.state.Sync() + if self.fullValidation { + if _, err := self.chain.InsertChain(types.Blocks{block}); err != nil { + glog.V(logger.Error).Infoln("mining err", err) + continue + } + go self.mux.Post(core.NewMinedBlockEvent{block}) + } else { + parent := self.chain.GetBlock(block.ParentHash()) + if parent == nil { + glog.V(logger.Error).Infoln("Invalid block found during mining") + continue + } + if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true); err != nil && err != core.BlockFutureErr { + glog.V(logger.Error).Infoln("Invalid header on mined block:", err) + continue + } - stat, err := self.chain.WriteBlock(block, false) - if err != nil { - glog.V(logger.Error).Infoln("error writing block to chain", err) - continue - } - // check if canon block and write transactions - if stat == core.CanonStatTy { - // This puts transactions in a extra db for rpc - core.PutTransactions(self.extraDb, block, block.Transactions()) - // store the receipts - core.PutReceipts(self.extraDb, self.current.receipts) + stat, err := self.chain.WriteBlock(block, false) + if err != nil { + glog.V(logger.Error).Infoln("error writing block to chain", err) + continue + } + // check if canon block and write transactions + if stat == core.CanonStatTy { + // This puts transactions in a extra db for rpc + core.PutTransactions(self.extraDb, block, block.Transactions()) + // store the receipts + core.PutReceipts(self.extraDb, self.current.receipts) + } + + // broadcast before waiting for validation + go func(block *types.Block, logs state.Logs) { + self.mux.Post(core.NewMinedBlockEvent{block}) + self.mux.Post(core.ChainEvent{block, block.Hash(), logs}) + if stat == core.CanonStatTy { + self.mux.Post(core.ChainHeadEvent{block}) + self.mux.Post(logs) + } + }(block, self.current.state.Logs()) } // check staleness and display confirmation @@ -289,19 +315,8 @@ func (self *worker) wait() { confirm = "Wait 5 blocks for confirmation" self.current.localMinedBlocks = newLocalMinedBlock(block.Number().Uint64(), self.current.localMinedBlocks) } - glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm) - // broadcast before waiting for validation - go func(block *types.Block, logs state.Logs) { - self.mux.Post(core.NewMinedBlockEvent{block}) - self.mux.Post(core.ChainEvent{block, block.Hash(), logs}) - if stat == core.CanonStatTy { - self.mux.Post(core.ChainHeadEvent{block}) - self.mux.Post(logs) - } - }(block, self.current.state.Logs()) - self.commitNewWork() } } @@ -320,7 +335,7 @@ func (self *worker) push() { atomic.AddInt32(&self.atWork, 1) if agent.Work() != nil { - agent.Work() <- self.current.block + agent.Work() <- self.current } } } @@ -329,13 +344,14 @@ func (self *worker) push() { // makeCurrent creates a new environment for the current cycle. func (self *worker) makeCurrent(parent *types.Block, header *types.Header) { state := state.New(parent.Root(), self.eth.StateDb()) - current := &environment{ + current := &Work{ state: state, ancestors: set.New(), family: set.New(), uncles: set.New(), header: header, coinbase: state.GetOrNewStateObject(self.coinbase), + createdAt: time.Now(), } // when 08 is processed ancestors contain 07 (quick block) @@ -391,10 +407,10 @@ func (self *worker) isBlockLocallyMined(deepBlockNum uint64) bool { return block != nil && block.Coinbase() == self.coinbase } -func (self *worker) logLocalMinedBlocks(previous *environment) { +func (self *worker) logLocalMinedBlocks(previous *Work) { if previous != nil && self.current.localMinedBlocks != nil { - nextBlockNum := self.current.block.NumberU64() - for checkBlockNum := previous.block.NumberU64(); checkBlockNum < nextBlockNum; checkBlockNum++ { + nextBlockNum := self.current.Block.NumberU64() + for checkBlockNum := previous.Block.NumberU64(); checkBlockNum < nextBlockNum; checkBlockNum++ { inspectBlockNum := checkBlockNum - miningLogAtDepth if self.isBlockLocallyMined(inspectBlockNum) { glog.V(logger.Info).Infof("🔨 🔗 Mined %d blocks back: block #%v", miningLogAtDepth, inspectBlockNum) @@ -475,17 +491,16 @@ func (self *worker) commitNewWork() { // commit state root after all state transitions. core.AccumulateRewards(self.current.state, header, uncles) current.state.SyncObjects() - self.current.state.Sync() header.Root = current.state.Root() } // create the new block whose nonce will be mined. - current.block = types.NewBlock(header, current.txs, uncles, current.receipts) - self.current.block.Td = new(big.Int).Set(core.CalcTD(self.current.block, self.chain.GetBlock(self.current.block.ParentHash()))) + current.Block = types.NewBlock(header, current.txs, uncles, current.receipts) + self.current.Block.Td = new(big.Int).Set(core.CalcTD(self.current.Block, self.chain.GetBlock(self.current.Block.ParentHash()))) // We only care about logging if we're actually mining. if atomic.LoadInt32(&self.mining) == 1 { - glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", current.block.Number(), current.tcount, len(uncles), time.Since(tstart)) + glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", current.Block.Number(), current.tcount, len(uncles), time.Since(tstart)) self.logLocalMinedBlocks(previous) } @@ -507,7 +522,7 @@ func (self *worker) commitUncle(uncle *types.Header) error { return nil } -func (env *environment) commitTransactions(transactions types.Transactions, gasPrice *big.Int, proc *core.BlockProcessor) { +func (env *Work) commitTransactions(transactions types.Transactions, gasPrice *big.Int, proc *core.BlockProcessor) { for _, tx := range transactions { // We can skip err. It has already been validated in the tx pool from, _ := tx.From() @@ -565,7 +580,7 @@ func (env *environment) commitTransactions(transactions types.Transactions, gasP } } -func (env *environment) commitTransaction(tx *types.Transaction, proc *core.BlockProcessor) error { +func (env *Work) commitTransaction(tx *types.Transaction, proc *core.BlockProcessor) error { snap := env.state.Copy() receipt, _, err := proc.ApplyTransaction(env.coinbase, env.state, env.header, tx, env.header.GasUsed, true) if err != nil { diff --git a/rlp/decode.go b/rlp/decode.go index 4462d4be4..cc402fc94 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -110,9 +110,17 @@ func Decode(r io.Reader, val interface{}) error { // DecodeBytes parses RLP data from b into val. // Please see the documentation of Decode for the decoding rules. +// The input must contain exactly one value and no trailing data. func DecodeBytes(b []byte, val interface{}) error { // TODO: this could use a Stream from a pool. - return NewStream(bytes.NewReader(b), uint64(len(b))).Decode(val) + r := bytes.NewReader(b) + if err := NewStream(r, uint64(len(b))).Decode(val); err != nil { + return err + } + if r.Len() > 0 { + return ErrMoreThanOneValue + } + return nil } type decodeError struct { @@ -353,7 +361,7 @@ func decodeByteArray(s *Stream, val reflect.Value) error { return err } // Reject cases where single byte encoding should have been used. - if size == 1 && slice[0] < 56 { + if size == 1 && slice[0] < 128 { return wrapStreamError(ErrCanonSize, val.Type()) } case List: @@ -517,6 +525,10 @@ var ( ErrElemTooLarge = errors.New("rlp: element is larger than containing list") ErrValueTooLarge = errors.New("rlp: value size exceeds available input length") + // This error is reported by DecodeBytes if the slice contains + // additional data after the first RLP value. + ErrMoreThanOneValue = errors.New("rlp: input contains more than one value") + // internal errors errNotInList = errors.New("rlp: call of ListEnd outside of any list") errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL") @@ -611,7 +623,7 @@ func (s *Stream) Bytes() ([]byte, error) { if err = s.readFull(b); err != nil { return nil, err } - if size == 1 && b[0] < 56 { + if size == 1 && b[0] < 128 { return nil, ErrCanonSize } return b, nil @@ -675,7 +687,7 @@ func (s *Stream) uint(maxbits int) (uint64, error) { return 0, ErrCanonInt case err != nil: return 0, err - case size > 0 && v < 56: + case size > 0 && v < 128: return 0, ErrCanonSize default: return v, nil diff --git a/rlp/decode_test.go b/rlp/decode_test.go index 71dacaba4..6f90d6e1d 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -113,12 +113,16 @@ func TestStreamErrors(t *testing.T) { {"00", calls{"Uint"}, nil, ErrCanonInt}, {"820002", calls{"Uint"}, nil, ErrCanonInt}, {"8133", calls{"Uint"}, nil, ErrCanonSize}, - {"8156", calls{"Uint"}, nil, nil}, + {"817F", calls{"Uint"}, nil, ErrCanonSize}, + {"8180", calls{"Uint"}, nil, nil}, // Size tags must use the smallest possible encoding. // Leading zero bytes in the size tag are also rejected. {"8100", calls{"Uint"}, nil, ErrCanonSize}, {"8100", calls{"Bytes"}, nil, ErrCanonSize}, + {"8101", calls{"Bytes"}, nil, ErrCanonSize}, + {"817F", calls{"Bytes"}, nil, ErrCanonSize}, + {"8180", calls{"Bytes"}, nil, nil}, {"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, {"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, {"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, @@ -132,11 +136,11 @@ func TestStreamErrors(t *testing.T) { {"", calls{"Kind"}, nil, io.EOF}, {"", calls{"Uint"}, nil, io.EOF}, {"", calls{"List"}, nil, io.EOF}, - {"8158", calls{"Uint", "Uint"}, nil, io.EOF}, + {"8180", calls{"Uint", "Uint"}, nil, io.EOF}, {"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF}, {"", calls{"List"}, withoutInputLimit, io.EOF}, - {"8158", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF}, + {"8180", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF}, {"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF}, // Input limit errors. @@ -153,11 +157,11 @@ func TestStreamErrors(t *testing.T) { // down toward zero in Stream.remaining, reading too far can overflow // remaining to a large value, effectively disabling the limit. {"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF}, - {"C4010203048158", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge}, + {"C4010203048180", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge}, // Check that the same calls are fine without a limit. {"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil}, - {"C4010203048158", calls{"Raw", "Uint"}, withoutInputLimit, nil}, + {"C4010203048180", calls{"Raw", "Uint"}, withoutInputLimit, nil}, // Unexpected EOF. This only happens when there is // no input limit, so the reader needs to be 'dumbed down'. @@ -349,6 +353,7 @@ var decodeTests = []decodeTest{ // byte arrays {input: "02", ptr: new([1]byte), value: [1]byte{2}}, + {input: "8180", ptr: new([1]byte), value: [1]byte{128}}, {input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}}, // byte array errors @@ -359,6 +364,7 @@ var decodeTests = []decodeTest{ {input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"}, {input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"}, {input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"}, + {input: "817F", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"}, // zero sized byte arrays {input: "80", ptr: new([0]byte), value: [0]byte{}}, @@ -427,7 +433,8 @@ var decodeTests = []decodeTest{ {input: "80", ptr: new(*uint), value: uintp(0)}, {input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"}, {input: "07", ptr: new(*uint), value: uintp(7)}, - {input: "8158", ptr: new(*uint), value: uintp(0x58)}, + {input: "817F", ptr: new(*uint), error: "rlp: non-canonical size information for uint"}, + {input: "8180", ptr: new(*uint), value: uintp(0x80)}, {input: "C109", ptr: new(*[]uint), value: &[]uint{9}}, {input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}}, diff --git a/tests/init.go b/tests/init.go index f149c11f1..30cff6f21 100644 --- a/tests/init.go +++ b/tests/init.go @@ -35,6 +35,7 @@ var ( stateTestDir = filepath.Join(baseDir, "StateTests") transactionTestDir = filepath.Join(baseDir, "TransactionTests") vmTestDir = filepath.Join(baseDir, "VMTests") + rlpTestDir = filepath.Join(baseDir, "RLPTests") BlockSkipTests = []string{ // These tests are not valid, as they are out of scope for RLP and diff --git a/tests/rlp_test.go b/tests/rlp_test.go new file mode 100644 index 000000000..70bd19627 --- /dev/null +++ b/tests/rlp_test.go @@ -0,0 +1,20 @@ +package tests + +import ( + "path/filepath" + "testing" +) + +func TestRLP(t *testing.T) { + err := RunRLPTest(filepath.Join(rlpTestDir, "rlptest.json"), nil) + if err != nil { + t.Fatal(err) + } +} + +func TestRLP_invalid(t *testing.T) { + err := RunRLPTest(filepath.Join(rlpTestDir, "invalidRLPTest.json"), nil) + if err != nil { + t.Fatal(err) + } +} diff --git a/tests/rlp_test_util.go b/tests/rlp_test_util.go new file mode 100644 index 000000000..c322b78c6 --- /dev/null +++ b/tests/rlp_test_util.go @@ -0,0 +1,172 @@ +package tests + +import ( + "bytes" + "encoding/hex" + "errors" + "fmt" + "io" + "math/big" + "os" + "strings" + + "github.com/ethereum/go-ethereum/rlp" +) + +// RLPTest is the JSON structure of a single RLP test. +type RLPTest struct { + // If the value of In is "INVALID" or "VALID", the test + // checks whether Out can be decoded into a value of + // type interface{}. + // + // For other JSON values, In is treated as a driver for + // calls to rlp.Stream. The test also verifies that encoding + // In produces the bytes in Out. + In interface{} + + // Out is a hex-encoded RLP value. + Out string +} + +// RunRLPTest runs the tests in the given file, skipping tests by name. +func RunRLPTest(file string, skip []string) error { + f, err := os.Open(file) + if err != nil { + return err + } + defer f.Close() + return RunRLPTestWithReader(f, skip) +} + +// RunRLPTest runs the tests encoded in r, skipping tests by name. +func RunRLPTestWithReader(r io.Reader, skip []string) error { + var tests map[string]*RLPTest + if err := readJson(r, &tests); err != nil { + return err + } + for _, s := range skip { + delete(tests, s) + } + for name, test := range tests { + if err := test.Run(); err != nil { + return fmt.Errorf("test %q failed: %v", name, err) + } + } + return nil +} + +// Run executes the test. +func (t *RLPTest) Run() error { + outb, err := hex.DecodeString(t.Out) + if err != nil { + return fmt.Errorf("invalid hex in Out") + } + + // Handle simple decoding tests with no actual In value. + if t.In == "VALID" || t.In == "INVALID" { + return checkDecodeInterface(outb, t.In == "VALID") + } + + // Check whether encoding the value produces the same bytes. + in := translateJSON(t.In) + b, err := rlp.EncodeToBytes(in) + if err != nil { + return fmt.Errorf("encode failed: %v", err) + } + if !bytes.Equal(b, outb) { + return fmt.Errorf("encode produced %x, want %x", b, outb) + } + // Test stream decoding. + s := rlp.NewStream(bytes.NewReader(outb), 0) + return checkDecodeFromJSON(s, in) +} + +func checkDecodeInterface(b []byte, isValid bool) error { + err := rlp.DecodeBytes(b, new(interface{})) + switch { + case isValid && err != nil: + return fmt.Errorf("decoding failed: %v", err) + case !isValid && err == nil: + return fmt.Errorf("decoding of invalid value succeeded") + } + return nil +} + +// translateJSON makes test json values encodable with RLP. +func translateJSON(v interface{}) interface{} { + switch v := v.(type) { + case float64: + return uint64(v) + case string: + if len(v) > 0 && v[0] == '#' { // # starts a faux big int. + big, ok := new(big.Int).SetString(v[1:], 10) + if !ok { + panic(fmt.Errorf("bad test: bad big int: %q", v)) + } + return big + } + return []byte(v) + case []interface{}: + new := make([]interface{}, len(v)) + for i := range v { + new[i] = translateJSON(v[i]) + } + return new + default: + panic(fmt.Errorf("can't handle %T", v)) + } +} + +// checkDecodeFromJSON decodes from s guided by exp. exp drives the +// Stream by invoking decoding operations (Uint, Big, List, ...) based +// on the type of each value. The value decoded from the RLP stream +// must match the JSON value. +func checkDecodeFromJSON(s *rlp.Stream, exp interface{}) error { + switch exp := exp.(type) { + case uint64: + i, err := s.Uint() + if err != nil { + return addStack("Uint", exp, err) + } + if i != exp { + return addStack("Uint", exp, fmt.Errorf("result mismatch: got %d", i)) + } + case *big.Int: + big := new(big.Int) + if err := s.Decode(&big); err != nil { + return addStack("Big", exp, err) + } + if big.Cmp(exp) != 0 { + return addStack("Big", exp, fmt.Errorf("result mismatch: got %d", big)) + } + case []byte: + b, err := s.Bytes() + if err != nil { + return addStack("Bytes", exp, err) + } + if !bytes.Equal(b, exp) { + return addStack("Bytes", exp, fmt.Errorf("result mismatch: got %x", b)) + } + case []interface{}: + if _, err := s.List(); err != nil { + return addStack("List", exp, err) + } + for i, v := range exp { + if err := checkDecodeFromJSON(s, v); err != nil { + return addStack(fmt.Sprintf("[%d]", i), exp, err) + } + } + if err := s.ListEnd(); err != nil { + return addStack("ListEnd", exp, err) + } + default: + panic(fmt.Errorf("unhandled type: %T", exp)) + } + return nil +} + +func addStack(op string, val interface{}, err error) error { + lines := strings.Split(err.Error(), "\n") + lines = append(lines, fmt.Sprintf("\t%s: %v", op, val)) + return errors.New(strings.Join(lines, "\n")) +} diff --git a/xeth/xeth.go b/xeth/xeth.go index 2d69dce6d..34409a148 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -518,6 +518,9 @@ func (self *XEth) UninstallFilter(id int) bool { } func (self *XEth) NewLogFilter(earliest, latest int64, skip, max int, address []string, topics [][]string) int { + self.logMu.Lock() + defer self.logMu.Unlock() + var id int filter := core.NewFilter(self.backend) filter.SetEarliestBlock(earliest) @@ -539,6 +542,9 @@ func (self *XEth) NewLogFilter(earliest, latest int64, skip, max int, address [] } func (self *XEth) NewTransactionFilter() int { + self.transactionMu.Lock() + defer self.transactionMu.Unlock() + var id int filter := core.NewFilter(self.backend) filter.TransactionCallback = func(tx *types.Transaction) { @@ -553,6 +559,9 @@ func (self *XEth) NewTransactionFilter() int { } func (self *XEth) NewBlockFilter() int { + self.blockMu.Lock() + defer self.blockMu.Unlock() + var id int filter := core.NewFilter(self.backend) filter.BlockCallback = func(block *types.Block, logs state.Logs) { @@ -609,9 +618,6 @@ func (self *XEth) TransactionFilterChanged(id int) []common.Hash { } func (self *XEth) Logs(id int) state.Logs { - self.logMu.Lock() - defer self.logMu.Unlock() - filter := self.filterManager.GetFilter(id) if filter != nil { return filter.Find() |