aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-02-18 20:14:21 +0800
committerobscuren <geffobscura@gmail.com>2015-02-18 20:14:21 +0800
commit655e94259765b02454df93205a30a271103de5a0 (patch)
tree818cd1e9156dda16d78257b2ad656edeb0058759
parentbe90ad89a89502ef3d2c0375d267b667618f5e7c (diff)
downloaddexon-655e94259765b02454df93205a30a271103de5a0.tar
dexon-655e94259765b02454df93205a30a271103de5a0.tar.gz
dexon-655e94259765b02454df93205a30a271103de5a0.tar.bz2
dexon-655e94259765b02454df93205a30a271103de5a0.tar.lz
dexon-655e94259765b02454df93205a30a271103de5a0.tar.xz
dexon-655e94259765b02454df93205a30a271103de5a0.tar.zst
dexon-655e94259765b02454df93205a30a271103de5a0.zip
Added GetBlock GetUncle with OOB guard
-rw-r--r--core/block_processor.go6
-rw-r--r--core/block_processor_test.go34
-rw-r--r--core/chain_manager.go16
-rw-r--r--core/types/block.go12
4 files changed, 59 insertions, 9 deletions
diff --git a/core/block_processor.go b/core/block_processor.go
index 893c586dd..b4449100f 100644
--- a/core/block_processor.go
+++ b/core/block_processor.go
@@ -250,7 +250,11 @@ func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error {
}
if block.Time() > time.Now().Unix() {
- return fmt.Errorf("block time is in the future")
+ return BlockFutureErr
+ }
+
+ if new(big.Int).Sub(block.Number(), parent.Number()).Cmp(big.NewInt(1)) != 0 {
+ return BlockNumberErr
}
// Verify the nonce of the block. Return an error if it's not valid
diff --git a/core/block_processor_test.go b/core/block_processor_test.go
new file mode 100644
index 000000000..35aeaa714
--- /dev/null
+++ b/core/block_processor_test.go
@@ -0,0 +1,34 @@
+package core
+
+import (
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/event"
+)
+
+func proc() (*BlockProcessor, *ChainManager) {
+ db, _ := ethdb.NewMemDatabase()
+ var mux event.TypeMux
+
+ chainMan := NewChainManager(db, &mux)
+ return NewBlockProcessor(db, nil, chainMan, &mux), chainMan
+}
+
+func TestNumber(t *testing.T) {
+ bp, chain := proc()
+ block1 := chain.NewBlock(nil)
+ block1.Header().Number = big.NewInt(3)
+
+ err := bp.ValidateBlock(block1, chain.Genesis())
+ if err != BlockNumberErr {
+ t.Errorf("expected block number error")
+ }
+
+ block1 = chain.NewBlock(nil)
+ err = bp.ValidateBlock(block1, chain.Genesis())
+ if err == BlockNumberErr {
+ t.Errorf("didn't expect block number error")
+ }
+}
diff --git a/core/chain_manager.go b/core/chain_manager.go
index 22d54be03..286282064 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -87,6 +87,14 @@ type ChainManager struct {
transState *state.StateDB
}
+func NewChainManager(db ethutil.Database, mux *event.TypeMux) *ChainManager {
+ bc := &ChainManager{db: db, genesisBlock: GenesisBlock(db), eventMux: mux}
+ bc.setLastBlock()
+ bc.transState = bc.State().Copy()
+
+ return bc
+}
+
func (self *ChainManager) Td() *big.Int {
self.mu.RLock()
defer self.mu.RUnlock()
@@ -108,14 +116,6 @@ func (self *ChainManager) CurrentBlock() *types.Block {
return self.currentBlock
}
-func NewChainManager(db ethutil.Database, mux *event.TypeMux) *ChainManager {
- bc := &ChainManager{db: db, genesisBlock: GenesisBlock(db), eventMux: mux}
- bc.setLastBlock()
- bc.transState = bc.State().Copy()
-
- return bc
-}
-
func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) {
self.mu.RLock()
defer self.mu.RUnlock()
diff --git a/core/types/block.go b/core/types/block.go
index fa28f5cc7..d57de1311 100644
--- a/core/types/block.go
+++ b/core/types/block.go
@@ -185,6 +185,18 @@ func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
func (self *Block) Root() []byte { return self.header.Root }
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) }
+func (self *Block) GetTransaction(i int) *Transaction {
+ if len(self.transactions) > i {
+ return self.transactions[i]
+ }
+ return nil
+}
+func (self *Block) GetUncle(i int) *Header {
+ if len(self.uncles) > i {
+ return self.uncles[i]
+ }
+ return nil
+}
// Implement pow.Block
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }