Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from binance-chain/release/v0.30.1-binance.6
Browse files Browse the repository at this point in the history
Release/v0.30.1 binance.6
  • Loading branch information
rickyyangz authored Apr 15, 2019
2 parents f9a2c2b + f9cfdd1 commit 332351c
Show file tree
Hide file tree
Showing 19 changed files with 523 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Special thanks to external contributors on this release:
* P2P Protocol

### FEATURES:
- [node] Support block index by hash, query block by hash through rpc.

### IMPROVEMENTS:

Expand Down
25 changes: 25 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Config struct {
Mempool *MempoolConfig `mapstructure:"mempool"`
Consensus *ConsensusConfig `mapstructure:"consensus"`
TxIndex *TxIndexConfig `mapstructure:"tx_index"`
BlockIndex *BlockIndexConfig `mapstructure:"block_index"`
Instrumentation *InstrumentationConfig `mapstructure:"instrumentation"`
}

Expand All @@ -82,6 +83,7 @@ func DefaultConfig() *Config {
Mempool: DefaultMempoolConfig(),
Consensus: DefaultConsensusConfig(),
TxIndex: DefaultTxIndexConfig(),
BlockIndex: DefaultBlockIndexConfig(),
Instrumentation: DefaultInstrumentationConfig(),
}
}
Expand All @@ -96,6 +98,7 @@ func TestConfig() *Config {
Mempool: TestMempoolConfig(),
Consensus: TestConsensusConfig(),
TxIndex: TestTxIndexConfig(),
BlockIndex: TestBlockIndexConfig(),
Instrumentation: TestInstrumentationConfig(),
}
}
Expand Down Expand Up @@ -871,6 +874,18 @@ type TxIndexConfig struct {
IndexAllTags bool `mapstructure:"index_all_tags"`
}

//-----------------------------------------------------------------------------
// BlockIndexConfig
// BlockIndexConfig defines the configuration for the block indexer
type BlockIndexConfig struct {
// What indexer to use for block
//
// Options:
// 1) "null"
// 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
Indexer string `mapstructure:"indexer"`
}

// DefaultTxIndexConfig returns a default configuration for the transaction indexer.
func DefaultTxIndexConfig() *TxIndexConfig {
return &TxIndexConfig{
Expand All @@ -885,6 +900,16 @@ func TestTxIndexConfig() *TxIndexConfig {
return DefaultTxIndexConfig()
}

// DefaultBlockIndexConfig returns a default configuration for the block indexer.
func DefaultBlockIndexConfig() *BlockIndexConfig {
return &BlockIndexConfig{Indexer: "null"}
}

// DefaultBlockIndexConfig returns a default configuration for the block indexer.
func TestBlockIndexConfig() *BlockIndexConfig {
return DefaultBlockIndexConfig()
}

//-----------------------------------------------------------------------------
// InstrumentationConfig

Expand Down
10 changes: 10 additions & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ index_tags = "{{ .TxIndex.IndexTags }}"
# indexed).
index_all_tags = {{ .TxIndex.IndexAllTags }}
##### block indexer configuration options #####
[block_index]
# What indexer to use for blocks
#
# Options:
# 1) "null"
# 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
indexer = "{{ .BlockIndex.Indexer }}"
##### instrumentation configuration options #####
[instrumentation]
Expand Down
37 changes: 33 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import (
grpccore "github.com/tendermint/tendermint/rpc/grpc"
rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/state/blockindex"
bkv "github.com/tendermint/tendermint/state/blockindex/kv"
nullblk "github.com/tendermint/tendermint/state/blockindex/null"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/state/txindex/kv"
"github.com/tendermint/tendermint/state/txindex/null"
Expand Down Expand Up @@ -165,7 +168,9 @@ type Node struct {
proxyApp proxy.AppConns // connection to the application
rpcListeners []net.Listener // rpc servers
txIndexer txindex.TxIndexer
blockIndexer blockindex.BlockIndexer
indexerService *txindex.IndexerService
blockIndexService *blockindex.IndexerService
prometheusSrv *http.Server
}

Expand Down Expand Up @@ -248,10 +253,30 @@ func NewNode(config *cfg.Config,
txIndexer = &null.TxIndex{}
}

indexerService := txindex.NewIndexerService(txIndexer, eventBus)
indexerService.SetLogger(logger.With("module", "txindex"))
txIndexerService := txindex.NewIndexerService(txIndexer, eventBus)
txIndexerService.SetLogger(logger.With("module", "txindex"))

err = indexerService.Start()
err = txIndexerService.Start()
if err != nil {
return nil, err
}

// Block indexing
var blockIndexer blockindex.BlockIndexer
switch config.BlockIndex.Indexer {
case "kv":
store, err := dbProvider(&DBContext{"block_index", config})
if err != nil {
return nil, err
}
blockIndexer = bkv.NewBlockIndex(store)
default:
blockIndexer = &nullblk.BlockIndex{}
}
blockIndexerService := blockindex.NewIndexerService(blockIndexer, eventBus)
blockIndexerService.SetLogger(logger.With("module", "blockindex"))

err = blockIndexerService.Start()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -553,7 +578,9 @@ func NewNode(config *cfg.Config,
evidencePool: evidencePool,
proxyApp: proxyApp,
txIndexer: txIndexer,
indexerService: indexerService,
blockIndexer: blockIndexer,
indexerService: txIndexerService,
blockIndexService: blockIndexerService,
eventBus: eventBus,
}
node.BaseService = *cmn.NewBaseService(logger, "Node", node)
Expand Down Expand Up @@ -624,6 +651,7 @@ func (n *Node) OnStop() {
// first stop the non-reactor services
n.eventBus.Stop()
n.indexerService.Stop()
n.blockIndexService.Stop()

// now stop the reactors
// TODO: gracefully disconnect from peers.
Expand Down Expand Up @@ -676,6 +704,7 @@ func (n *Node) ConfigureRPC() {
rpccore.SetAddrBook(n.addrBook)
rpccore.SetProxyAppQuery(n.proxyApp.Query())
rpccore.SetTxIndexer(n.txIndexer)
rpccore.SetBlockIndexer(n.blockIndexer)
rpccore.SetConsensusReactor(n.consensusReactor)
rpccore.SetEventBus(n.eventBus)
rpccore.SetLogger(n.Logger.With("module", "rpc"))
Expand Down
9 changes: 9 additions & 0 deletions rpc/client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ func (c *HTTP) Block(height *int64) (*ctypes.ResultBlock, error) {
return result, nil
}

func (c *HTTP) BlockByHash(blockHash []byte) (*ctypes.ResultBlock, error) {
result := new(ctypes.ResultBlock)
_, err := c.rpc.Call("block_by_hash", map[string]interface{}{"hash": blockHash}, result)
if err != nil {
return nil, errors.Wrap(err, "Block_by_hash")
}
return result, nil
}

func (c *HTTP) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
result := new(ctypes.ResultBlockResults)
_, err := c.rpc.Call("block_results", map[string]interface{}{"height": height}, result)
Expand Down
1 change: 1 addition & 0 deletions rpc/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ABCIClient interface {
// signatures and prove anything about the chain
type SignClient interface {
Block(height *int64) (*ctypes.ResultBlock, error)
BlockByHash(hash []byte) (*ctypes.ResultBlock, error)
BlockResults(height *int64) (*ctypes.ResultBlockResults, error)
Commit(height *int64) (*ctypes.ResultCommit, error)
Validators(height *int64) (*ctypes.ResultValidators, error)
Expand Down
4 changes: 4 additions & 0 deletions rpc/client/localclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (Local) Block(height *int64) (*ctypes.ResultBlock, error) {
return core.Block(height)
}

func (Local) BlockByHash(blockHash []byte) (*ctypes.ResultBlock, error) {
return core.BlockByHash(blockHash)
}

func (Local) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
return core.BlockResults(height)
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/client/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (c Client) Block(height *int64) (*ctypes.ResultBlock, error) {
return core.Block(height)
}

func (c Client) BlockByHash(blockHash []byte) (*ctypes.ResultBlock, error) {
return core.BlockByHash(blockHash)
}

func (c Client) Commit(height *int64) (*ctypes.ResultCommit, error) {
return core.Commit(height)
}
Expand Down
134 changes: 134 additions & 0 deletions rpc/core/blocks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"crypto/sha256"
"fmt"

cmn "github.com/tendermint/tendermint/libs/common"
Expand Down Expand Up @@ -238,6 +239,122 @@ func Block(heightPtr *int64) (*ctypes.ResultBlock, error) {
return &ctypes.ResultBlock{BlockMeta: blockMeta, Block: block}, nil
}

// Get block at a given block hash.
// If no block hash is provided, it will return with error.
//
// ```shell
// curl 'localhost:27147/block_by_hash?hash=0xF70588DAB36BDA5A953D548A16F7D48C6C2DFD78'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:27147", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// info, err := client.BlockByHash([]byte("F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"))
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "block": {
// "last_commit": {
// "precommits": [
// {
// "signature": {
// "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
// "type": "ed25519"
// },
// "block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "type": "2",
// "round": "0",
// "height": "9",
// "validator_index": "0",
// "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
// }
// ],
// "blockID": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// }
// },
// "data": {
// "txs": []
// },
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// }
// },
// "block_meta": {
// "header": {
// "app_hash": "",
// "chain_id": "test-chain-6UTNIN",
// "height": "10",
// "time": "2017-05-29T15:05:53.877Z",
// "num_txs": "0",
// "last_block_id": {
// "parts": {
// "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
// "total": "1"
// },
// "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
// },
// "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
// "data_hash": "",
// "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
// },
// "block_id": {
// "parts": {
// "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
// "total": "1"
// },
// "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
// }
// }
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
func BlockByHash(blockHash []byte) (*ctypes.ResultBlock, error) {
height, err := getHeightViaHash(blockHash)
if err != nil {
return nil, err
}

blockMeta := blockStore.LoadBlockMeta(height)
block := blockStore.LoadBlock(height)
return &ctypes.ResultBlock{BlockMeta: blockMeta, Block: block}, nil
}

// Get block commit at a given height.
// If no height is provided, it will fetch the commit for the latest block.
//
Expand Down Expand Up @@ -405,3 +522,20 @@ func getHeight(currentHeight int64, heightPtr *int64) (int64, error) {
}
return currentHeight, nil
}

func getHeightViaHash(blockHash []byte) (int64, error) {
if blockHash == nil {
return 0, fmt.Errorf("blockHash is missing")
}
if len(blockHash) != sha256.Size {
return 0, fmt.Errorf("length of blockHash is not 32")
}
blockHeight, err := blockIndexer.Get(blockHash)
if err != nil {
return 0, fmt.Errorf("get block from blockHash indexer failed, err: %v", err)
}
if blockHeight == 0 {
return 0, fmt.Errorf("the specified block blockHash do not exist")
}
return blockHeight, nil
}
6 changes: 6 additions & 0 deletions rpc/core/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/tendermint/tendermint/proxy"
rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/state/blockindex"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -66,6 +67,7 @@ var (
genDoc *types.GenesisDoc // cache the genesis structure
addrBook p2p.AddrBook
txIndexer txindex.TxIndexer
blockIndexer blockindex.BlockIndexer
consensusReactor *consensus.ConsensusReactor
eventBus *types.EventBus // thread safe
mempool *mempl.Mempool
Expand Down Expand Up @@ -121,6 +123,10 @@ func SetTxIndexer(indexer txindex.TxIndexer) {
txIndexer = indexer
}

func SetBlockIndexer(indexer blockindex.BlockIndexer) {
blockIndexer = indexer
}

func SetConsensusReactor(conR *consensus.ConsensusReactor) {
consensusReactor = conR
}
Expand Down
1 change: 1 addition & 0 deletions rpc/core/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Routes = map[string]*rpc.RPCFunc{
"blockchain": rpc.NewRPCFunc(BlockchainInfo, "minHeight,maxHeight"),
"genesis": rpc.NewRPCFunc(Genesis, ""),
"block": rpc.NewRPCFunc(Block, "height"),
"block_by_hash": rpc.NewRPCFunc(BlockByHash, "hash"),
"block_results": rpc.NewRPCFunc(BlockResults, "height"),
"commit": rpc.NewRPCFunc(Commit, "height"),
"tx": rpc.NewRPCFunc(Tx, "hash,prove"),
Expand Down
Loading

0 comments on commit 332351c

Please sign in to comment.