Subscribe NewBlocks
Last updated
Last updated
用於訂閱高性能網絡中的區塊數據流,方法名NewBlocks
。高性能網絡根據區塊是否經過狀態驗證,提供兩種類型的数据流。
未驗證的區塊數據流:在高性能網絡上广播但狀態尚未經驗證的全部區塊,部分區塊可能無效,对于速度更敏感的用户可选择订阅。
已驗證的區塊數據流:在高性能網絡上广播且狀態經過驗證的區塊,傳播速度相比前者可能慢50-200ms。
Tier 3 | Tier 2 | Tier 1 | |
---|---|---|---|
參數 | 必選 | 格式 | 示例 | 描述 |
---|---|---|---|---|
package main
import (
"context"
"fmt"
// directory of the generated code using the provided relay.proto file
pb "github.com/BlockRazorinc/relay_example/protobuf"
"google.golang.org/grpc"
)
// auth will be used to verify the credential
type Authentication struct {
apiKey string
}
func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{"apiKey": a.apiKey}, nil
}
func (a *Authentication) RequireTransportSecurity() bool {
return false
}
func main() {
// BlockRazor relay endpoint address
blzrelayEndPoint := "ip:port"
// auth will be used to verify the credential
auth := Authentication{
"your auth token",
}
// open gRPC connection to BlockRazor relay
var err error
conn, err := grpc.Dial(blzrelayEndPoint, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth), grpc.WithWriteBufferSize(0), grpc.WithInitialConnWindowSize(128*1024))
if err != nil {
fmt.Println("error: ", err)
return
}
// use the Gateway client connection interface
client := pb.NewGatewayClient(conn)
// create context and defer cancel of context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a subscription using the stream-specific method and request
stream, err := client.NewBlocks(ctx, &pb.BlocksRequest{NodeValidation: true})
if err != nil {
fmt.Println("failed to subscribe new block: ", err)
return
}
for {
reply, err := stream.Recv()
if err != nil {
fmt.Println("stream receive error: ", err)
}
fmt.Println("recieve new block, block hash is ", reply.Hash)
}
}
正常
{
"hash": "0xe4a85aaa8cf4c85c4abf59c06b744ae680941e7ffba351fd4c166f0264e860de",
"header": {
"parent_hash": "0x0105992a6c305b0bbfbf7b4eebbbc92c4dca1bb2a18c1f93c551afc2c73c0668",
"sha3_uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0x9f1b7FAE54BE07F4FEE34Eb1aaCb39A1F7B6FC92",
"state_root": "0xce2a7e35a643e5840e510df9eea8ba9177dd62fe743439ef159d7d9e783afd28",
"transactions_root": "0x63c952adbe28ef60d8e723acec299ca62a915b51d05b060cb6fe97bb28814ab3",
"receipts_root": "0xd3232a5308a69591dda132bd4ce1c1dbd5968ed9feee5f1385a5e7ad1b10527b",
"logs_bloom": "8477149424673580296520836103527692456522284192111573121045681184670018737794069194486118998020237483301054022136573687133364882576385281526094331485603954025070865625599001965016471857986082361337851986790329901990002923474000909032395056767065187980257808021700240863783968353544053840361314138235944346718686049569138609472702141575196319415411746602993576034439427711191239874622732325947862558346553652593648034520192193214092215911630192819139737571343595880627533215841996399050061528845101804940660169259391681335118771766679784629118424769368884717750281985023526825099358014255162966726322082309693440030370",
"difficulty": "2",
"number": "40370748",
"gas_limit": 139997863,
"gas_used": 12180159,
"timestamp": 1720674742,
"extra_data": "2IMBBAqEZ2V0aIhnbzEuMjEuNYVsaW51eAAAAEj6ewX4tYMf//+4YKyfFdynrjSSLQKJj9FdhVSwaDFtnAdMnqNzKeVm2agkWE79uSpjZ2Wbrxt2eCFSvBRuPLIhONzRYpQgx/DeH/Xrhsz/A+EZw4BFaOVV3ch76i1QdJy1CADUqoc/XEaBHvhMhAJoAjqg3GksrNaxsaC5pxx8JS0YFpA6OQh7ofh7pTmra2Ve7lOEAmgCO6ABBZkqbDBbC7+/e07ru8ksTcobsqGMH5PFUa/CxzwGaIBHk8oYx40FGg4hnpMSQsTiOEAObH2DV3ytkUGOdEk4KENXgVf0/xsCA56w2r/VIqP7ux7HCD9vrh7H7fjxdU3xAA==",
"mix_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"noncpe":0,
"base_fee_per_gas":0,
"withdrawals_root":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"blob_gas_used": 131072,
"excess_blob_gas":0,
"parent_beacon_block_root":""
},
"nextValidator": [{
"block_height": "40370749",
"coinbase": "0xbdcc079bbb23c1d9a6f36aa31309676c258abac7"
}, {
"block_height": "40370750",
"coinbase": "0xc2d534f079444e6e7ff9dabb3fd8a26c607932c8"
}],
"txs": [{
"raw_tx": "+K6DzSWShQEqBfIAgwEwY5R2021E3EWV6NLrOtdF8XXtoTQoT4C4RKkFnLsAAAAAAAAAAAAAAAAFWjs3lXv70zRb7Zlo5+jdVtZwZgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGBV8GygaywAAgZOguQQg9l/3+bmldXAcm9lsSsgxwq8m+wqcXh/KwbKJ7E+gAS8g6ZuII8KJWFBFpVzTcyptzqfro00WDKR3oi1ly7g="
}, {
"raw_tx": "+QVPgxUyKIUBKgXyAIMHoSCUE3kk18NoFuDcrwFuthfMLJLAV4KAuQTkyYB1OQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAQEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoAAAAAAAAAAAAAAAGQptag/qdv1AH5JjIvJ4fgACqZ4AQoECA4FDA0HAQsPAgAJBgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAw9aP+EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADD2B6d4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMPYNaEAAAAAAAAAAAAAAAAAA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isq/0Cpz/+2fzV4KknIckM8W1VsH8S34kLRauuV1UKaY7AU0K0BIA1dr8HMkOm/Ci+fDyuSFJY94GToC30fN+eoJ0+scIcdj9XpEmHUxWAT6TtSwk1zVm14HyboHCzcmyYjRIcHxPeMrJiwXSKXkgCTggJw9V0ETOLYuZL"
}, {
"raw_tx": "+K6DzSWThQEqBfIAgwEw4JRV05gyb5kFn/d1SFJGmZAnsxl5VYC4RKkFnLsAAAAAAAAAAAAAAADt2wDQgCLygqAXnLTDVpf1DvOxOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALA2UyUvg7vwAgZOgReRi2miA5qA+ERtjgmiOmqwmtCDhvtj0TFKrtUAuQWegP7xd7Gj89uQM03KMcfceKvAKkxwwajcJB1M8xHQlR4g="
}, {
"raw_tx": "+QFrBYTodUcAgwQ7j5Td7QSbsOJzaTkPQPjEolIpjjUwjoC5AQS39rSuAAAAAAAAAAAAAAAAVdOYMm+ZBZ/3dUhSRpmQJ7MZeVUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHP5UVLi+/8gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcWBJiX8HPgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAisdqUcyVDZgi1ouD/hrZezLNWA0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIGUoDDbxwrT1VjxX+u9C70/BMIgFYEATNA/P4NaAIu8AYupoFgRK+X8L9sYxgVNJobVpne44NbM/wQTYLLDGqanoAeH"
}]
}
异常
rpc error: code = Unknown desc = data streams have exceeded its max limit [5]
並行數據流
-
5
10
NodeValidation
是
boolean
true
默認為false,接收全量區塊數據流,包含未經状态驗證的新區塊,如設置為true,則接收經過状态驗證的區塊數據流。