Skip to content

Commit

Permalink
rpc/api: don't crash for unknown blocks
Browse files Browse the repository at this point in the history
Most eth RPC calls that work with blocks crashed when the block was not
found because they called Hash on a nil block. This is a regression
introduced in cdc2662 (ethereum#1779).

While here, remove the insane conversions in get*CountBy*. There is no
need to construct a complete BlockRes and converting
int->int64->*big.Int->[]byte->hexnum->string to format the length of a
slice as hex.
  • Loading branch information
fjl committed Sep 22, 2015
1 parent 70b6174 commit 90cd8ae
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions rpc/api/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,19 @@ func (self *ethApi) GetTransactionCount(req *shared.Request) (interface{}, error
}

count := self.xeth.AtStateNum(args.BlockNumber).TxCountAt(args.Address)
return newHexNum(big.NewInt(int64(count)).Bytes()), nil
return fmt.Sprintf("%#x", count), nil
}

func (self *ethApi) GetBlockTransactionCountByHash(req *shared.Request) (interface{}, error) {
args := new(HashArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, shared.NewDecodeParamError(err.Error())
}

raw := self.xeth.EthBlockByHash(args.Hash)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false)
block := self.xeth.EthBlockByHash(args.Hash)
if block == nil {
return nil, nil
} else {
return newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes()), nil
}
return fmt.Sprintf("%#x", len(block.Transactions())), nil
}

func (self *ethApi) GetBlockTransactionCountByNumber(req *shared.Request) (interface{}, error) {
Expand All @@ -234,13 +231,11 @@ func (self *ethApi) GetBlockTransactionCountByNumber(req *shared.Request) (inter
return nil, shared.NewDecodeParamError(err.Error())
}

raw := self.xeth.EthBlockByNumber(args.BlockNumber)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false)
block := self.xeth.EthBlockByNumber(args.BlockNumber)
if block == nil {
return nil, nil
} else {
return newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes()), nil
}
return fmt.Sprintf("%#x", len(block.Transactions())), nil
}

func (self *ethApi) GetUncleCountByBlockHash(req *shared.Request) (interface{}, error) {
Expand All @@ -249,12 +244,11 @@ func (self *ethApi) GetUncleCountByBlockHash(req *shared.Request) (interface{},
return nil, shared.NewDecodeParamError(err.Error())
}

raw := self.xeth.EthBlockByHash(args.Hash)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false)
block := self.xeth.EthBlockByHash(args.Hash)
if block == nil {
return nil, nil
}
return newHexNum(big.NewInt(int64(len(block.Uncles))).Bytes()), nil
return fmt.Sprintf("%#x", len(block.Uncles())), nil
}

func (self *ethApi) GetUncleCountByBlockNumber(req *shared.Request) (interface{}, error) {
Expand All @@ -263,12 +257,11 @@ func (self *ethApi) GetUncleCountByBlockNumber(req *shared.Request) (interface{}
return nil, shared.NewDecodeParamError(err.Error())
}

raw := self.xeth.EthBlockByNumber(args.BlockNumber)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false)
block := self.xeth.EthBlockByNumber(args.BlockNumber)
if block == nil {
return nil, nil
}
return newHexNum(big.NewInt(int64(len(block.Uncles))).Bytes()), nil
return fmt.Sprintf("%#x", len(block.Uncles())), nil
}

func (self *ethApi) GetData(req *shared.Request) (interface{}, error) {
Expand Down Expand Up @@ -377,8 +370,10 @@ func (self *ethApi) GetBlockByHash(req *shared.Request) (interface{}, error) {
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, shared.NewDecodeParamError(err.Error())
}

block := self.xeth.EthBlockByHash(args.BlockHash)
if block == nil {
return nil, nil
}
return NewBlockRes(block, self.xeth.Td(block.Hash()), args.IncludeTxs), nil
}

Expand All @@ -389,6 +384,9 @@ func (self *ethApi) GetBlockByNumber(req *shared.Request) (interface{}, error) {
}

block := self.xeth.EthBlockByNumber(args.BlockNumber)
if block == nil {
return nil, nil
}
return NewBlockRes(block, self.xeth.Td(block.Hash()), args.IncludeTxs), nil
}

Expand Down Expand Up @@ -419,10 +417,10 @@ func (self *ethApi) GetTransactionByBlockHashAndIndex(req *shared.Request) (inte
}

raw := self.xeth.EthBlockByHash(args.Hash)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true)
if block == nil {
if raw == nil {
return nil, nil
}
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true)
if args.Index >= int64(len(block.Transactions)) || args.Index < 0 {
return nil, nil
} else {
Expand All @@ -437,10 +435,10 @@ func (self *ethApi) GetTransactionByBlockNumberAndIndex(req *shared.Request) (in
}

raw := self.xeth.EthBlockByNumber(args.BlockNumber)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true)
if block == nil {
if raw == nil {
return nil, nil
}
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true)
if args.Index >= int64(len(block.Transactions)) || args.Index < 0 {
// return NewValidationError("Index", "does not exist")
return nil, nil
Expand All @@ -455,10 +453,10 @@ func (self *ethApi) GetUncleByBlockHashAndIndex(req *shared.Request) (interface{
}

raw := self.xeth.EthBlockByHash(args.Hash)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false)
if block == nil {
if raw == nil {
return nil, nil
}
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false)
if args.Index >= int64(len(block.Uncles)) || args.Index < 0 {
// return NewValidationError("Index", "does not exist")
return nil, nil
Expand All @@ -473,10 +471,10 @@ func (self *ethApi) GetUncleByBlockNumberAndIndex(req *shared.Request) (interfac
}

raw := self.xeth.EthBlockByNumber(args.BlockNumber)
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true)
if block == nil {
if raw == nil {
return nil, nil
}
block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true)
if args.Index >= int64(len(block.Uncles)) || args.Index < 0 {
return nil, nil
} else {
Expand Down

0 comments on commit 90cd8ae

Please sign in to comment.