Skip to content

Commit

Permalink
rpc:add get finalized block interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bysomeone committed May 9, 2024
1 parent feabc26 commit d66f10d
Show file tree
Hide file tree
Showing 10 changed files with 1,146 additions and 510 deletions.
618 changes: 557 additions & 61 deletions client/mocks/api.go

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions client/queueprotocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,3 +1235,20 @@ func (q *QueueProtocol) GetHighestBlockNum(param *types.ReqNil) (*types.ReplyBlo
}
return nil, types.ErrInvalidParam
}

// GetFinalizedBlock get finalized block choice
func (q *QueueProtocol) GetFinalizedBlock() (*types.SnowChoice, error) {

cli := q.client
msg := cli.NewMessage("blockchain", types.EventSnowmanLastChoice, &types.ReqNil{})
err := cli.Send(msg, true)
if err != nil {
return nil, err
}

reply, err := cli.Wait(msg)
if err != nil {
return nil, err
}
return reply.GetData().(*types.SnowChoice), nil
}
21 changes: 21 additions & 0 deletions client/queueprotocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,24 @@ func testClosePeer(t *testing.T, api client.QueueProtocolAPI) {
assert.Equal(t, "success", string(reply.GetMsg()))

}

func TestQueueProtocol_GetFinalizedBlock(t *testing.T) {

q := queue.New("test")

cli := q.Client()
api, err := client.New(cli, nil)
require.Nil(t, err)
defer cli.Close()
go func() {

cli.Sub("blockchain")
for msg := range cli.Recv() {
msg.Reply(cli.NewMessage("", 0, &types.SnowChoice{Height: 1}))
}
}()

sc, err := api.GetFinalizedBlock()
require.Nil(t, err)
require.Equal(t, 1, int(sc.GetHeight()))
}
2 changes: 2 additions & 0 deletions client/queueprotocolapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,6 @@ type QueueProtocolAPI interface {
DialPeer(in *types.SetPeer) (*types.Reply, error)
//ClosePeer close specified peer
ClosePeer(in *types.SetPeer) (*types.Reply, error)
//GetFinalizedBlock get finalized block choice
GetFinalizedBlock() (*types.SnowChoice, error)
}
11 changes: 11 additions & 0 deletions rpc/grpchandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,17 @@ func (g *Grpc) SignWalletRecoverTx(ctx context.Context, in *pb.ReqSignWalletReco
return g.cli.SignWalletRecoverTx(in)
}

// GetFinalizedBlock get finalized block choice
func (g *Grpc) GetFinalizedBlock(ctx context.Context, in *pb.ReqNil) (*pb.SnowChoice, error) {

choice, err := g.cli.GetFinalizedBlock()
if err != nil {
return nil, err
}

return choice, nil
}

// GetChainConfig 获取chain config 参数
func (g *Grpc) GetChainConfig(ctx context.Context, in *pb.ReqNil) (*pb.ChainConfigInfo, error) {
cfg := g.cli.GetConfig()
Expand Down
11 changes: 11 additions & 0 deletions rpc/grpchandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,17 @@ func TestGrpc_ConvertExectoAddr(t *testing.T) {
assert.Equal(t, "1GaHYpWmqAJsqRwrpoNcB8VvgKtSwjcHqt", replyStr.GetData())
}

func TestGrpc_GetFinalizedBlock(t *testing.T) {
g := Grpc{}
qapi = new(mocks.QueueProtocolAPI)
qapi.On("GetFinalizedBlock", mock.Anything).Return(&types.SnowChoice{Height: 1}, nil)
g.cli.QueueProtocolAPI = qapi
sc, err := g.GetFinalizedBlock(nil, nil)

require.Nil(t, err)
require.Equal(t, 1, int(sc.Height))
}

func TestGrpc_GetCoinSymbol(t *testing.T) {

reply, err := g.GetCoinSymbol(context.Background(), &types.ReqNil{})
Expand Down
12 changes: 12 additions & 0 deletions rpc/jrpchandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1704,3 +1704,15 @@ func (c *Chain33) ClosePeer(in *types.SetPeer, result *interface{}) error {
*result = &resp
return nil
}

// GetFinalizedBlock get finalized block choice
func (c *Chain33) GetFinalizedBlock(in *types.ReqNil, result *interface{}) error {

choice, err := c.cli.GetFinalizedBlock()
if err != nil {
return err
}

*result = choice
return nil
}
15 changes: 15 additions & 0 deletions rpc/jrpchandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1967,3 +1967,18 @@ func TestChain33_ClosePeer(t *testing.T) {
_, ok := testResult.(*rpctypes.Reply)
assert.True(t, ok)
}

func TestChain33_GetFinalizedBlock(t *testing.T) {
cfg := types.NewChain33Config(types.GetDefaultCfgstring())
api := new(mocks.QueueProtocolAPI)
api.On("GetConfig", mock.Anything).Return(cfg)
api.On("GetFinalizedBlock", mock.Anything).Return(&types.SnowChoice{Height: 1}, nil)

chain33 := newTestChain33(api)
var testResult interface{}
err := chain33.GetFinalizedBlock(nil, &testResult)
require.Nil(t, err)
sc, ok := testResult.(*types.SnowChoice)
require.True(t, ok)
require.Equal(t, 1, int(sc.GetHeight()))
}
4 changes: 4 additions & 0 deletions types/proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "p2p.proto";
import "account.proto";
import "executor.proto";
import "push_tx_receipt.proto";
import "snowman.proto";

package types;
option go_package = "github.com/33cn/chain33/types";
Expand Down Expand Up @@ -256,6 +257,9 @@ service chain33 {
// 钱包找回交易签名
rpc SignWalletRecoverTx(ReqSignWalletRecoverTx) returns (ReplySignRawTx) {}

// 获取最终化区块
rpc GetFinalizedBlock(ReqNil) returns(snowChoice) {}

// 获取节点配置信息
rpc GetChainConfig(ReqNil) returns (ChainConfigInfo) {}

Expand Down
945 changes: 496 additions & 449 deletions types/rpc.pb.go

Large diffs are not rendered by default.

0 comments on commit d66f10d

Please sign in to comment.