forked from cerc-io/plugeth
Added GetBlock GetUncle with OOB guard
This commit is contained in:
parent
be90ad89a8
commit
655e942597
@ -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
|
||||
|
34
core/block_processor_test.go
Normal file
34
core/block_processor_test.go
Normal file
@ -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")
|
||||
}
|
||||
}
|
@ -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()
|
||||
|
@ -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 }
|
||||
|
Loading…
Reference in New Issue
Block a user