From 894294d8e631c0c5aef26921789892d65e712404 Mon Sep 17 00:00:00 2001 From: zhouop0 <11733741+zhouop0@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:30:11 +0800 Subject: [PATCH] add submitProof interface --- pkg/b2node/b2node.go | 57 +++++++++++++++++++++++++++++++++++-- pkg/b2node/b2node_test.go | 60 +++++++++++++++++++++++++++++++++------ 2 files changed, 105 insertions(+), 12 deletions(-) diff --git a/pkg/b2node/b2node.go b/pkg/b2node/b2node.go index 1728b04..7ac057a 100644 --- a/pkg/b2node/b2node.go +++ b/pkg/b2node/b2node.go @@ -60,8 +60,34 @@ func (n NodeClient) GetAccountInfo(address string) (*eTypes.EthAccount, error) { return ethAccount, nil } -func (n NodeClient) SubmitProof(msgSubmitProof *committerTypes.MsgSubmitProof) (*tx.BroadcastTxResponse, error) { - txBytes, err := n.BuildSimTx(7, msgSubmitProof) +func (n NodeClient) SubmitProof(id uint64, from string, proofHash string, stateRootHash string, + startIndex uint64, endIndex uint64) (uint64, error) { + msg := committerTypes.NewMsgSubmitProof(id, from, proofHash, stateRootHash, startIndex, endIndex) + msgReponse, err := n.broadcastTx(msg) + if err != nil { + return 0, fmt.Errorf("[SubmitProof] err: %s", err) + } + hexData := msgReponse.TxResponse.Data + byteData, err := hex.DecodeString(hexData) + if err != nil { + return 0, fmt.Errorf("[SubmitProof][hex.DecodeString] err: %s", err) + } + var pbMsg = &sdk.TxMsgData{} + err = pbMsg.Unmarshal(byteData) + if err != nil { + return 0, fmt.Errorf("[SubmitProof][pbMsg.Unmarshal] err: %s", err) + } + + resMsgRes := &committerTypes.MsgSubmitProofResponse{} + err = resMsgRes.Unmarshal(pbMsg.MsgResponses[0].GetValue()) + if err != nil { + return 0, fmt.Errorf("[SubmitProof][resMsgRes.Unmarshal] err: %s", err) + } + return resMsgRes.Id, err +} + +func (n NodeClient) broadcastTx(msgs ...sdk.Msg) (*tx.BroadcastTxResponse, error) { + txBytes, err := n.buildSimTx(7, msgs...) if err != nil { return nil, fmt.Errorf("[SubmitProof] err: %s", err) } @@ -76,7 +102,7 @@ func (n NodeClient) SubmitProof(msgSubmitProof *committerTypes.MsgSubmitProof) ( return res, err } -func (n NodeClient) BuildSimTx(gasPrice uint64, msgs ...sdk.Msg) ([]byte, error) { +func (n NodeClient) buildSimTx(gasPrice uint64, msgs ...sdk.Msg) ([]byte, error) { encCfg := simapp.MakeTestEncodingConfig() txBuilder := encCfg.TxConfig.NewTxBuilder() err := txBuilder.SetMsgs(msgs...) @@ -136,3 +162,28 @@ func (n NodeClient) QueryLastProposalID() (uint64, uint64, error) { } return res.LastProposalId, res.EndIndex, nil } + +func (n NodeClient) QueryProposalByID(id uint64) (*committerTypes.Proposal, error) { + queryClient := committerTypes.NewQueryClient(n.GrpcConn) + res, err := queryClient.Proposal(context.Background(), &committerTypes.QueryProposalRequest{ProposalId: id}) + if err != nil { + return nil, fmt.Errorf("[QueryProposalByID] err: %s", err) + } + return res.Proposal, nil +} + +func (n NodeClient) CommitterBitcoinTx(msg *committerTypes.MsgBitcoinTx) (*tx.BroadcastTxResponse, error) { + txBytes, err := n.buildSimTx(7, msg) + if err != nil { + return nil, fmt.Errorf("[SubmitProof] err: %s", err) + } + txClient := tx.NewServiceClient(n.GrpcConn) + res, err := txClient.BroadcastTx(context.Background(), &tx.BroadcastTxRequest{ + Mode: tx.BroadcastMode_BROADCAST_MODE_BLOCK, + TxBytes: txBytes, + }) + if err != nil { + return nil, fmt.Errorf("[SubmitProof][BroadcastTx] err: %s", err) + } + return res, err +} diff --git a/pkg/b2node/b2node_test.go b/pkg/b2node/b2node_test.go index f7a20df..f560e61 100644 --- a/pkg/b2node/b2node_test.go +++ b/pkg/b2node/b2node_test.go @@ -1,7 +1,10 @@ package b2node import ( + "encoding/hex" "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" "testing" "github.com/b2network/b2committer/internal/types" @@ -33,15 +36,19 @@ func TestSubmitProof(t *testing.T) { panic(err) } nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn) - mbpTx := xcommitterTypes.NewMsgSubmitProof(4, "ethm10ky5utnz5ddlmus5t2mm5ftxal3u0u6rsnx5nl", "proof1", "state1", 21, 30) - txRes, _ := nodeClient.SubmitProof(mbpTx) - fmt.Println(txRes.TxResponse) - - //txSvcClient := tx.NewServiceClient(nodeClient.GrpcConn) - //simRes, err := txSvcClient.Simulate(context.Background(), &tx.SimulateRequest{ - // TxBytes: txBytes, - //}) - //fmt.Println(simRes) + proposalID, _ := nodeClient.SubmitProof(101, "ethm10ky5utnz5ddlmus5t2mm5ftxal3u0u6rsnx5nl", "proof7", "stateRoot", 61, 70) + fmt.Println(proposalID) +} + +func TestDecodeTxResponseData(t *testing.T) { + byteData, _ := hex.DecodeString("12370A312F65766D6F732E65746865726D696E742E636F6D6D69747465722E4D73675375626D697450726F6F66526573706F6E736512020808") + var pbMsg = &sdk.TxMsgData{} + //proto.Marshal(&sdk.TxMsgData{MsgResponses: msgResponses}) + pbMsg.Unmarshal(byteData) + fmt.Println(pbMsg.MsgResponses[0].TypeUrl) + resMsgRes := &xcommitterTypes.MsgSubmitProofResponse{} + resMsgRes.Unmarshal(pbMsg.MsgResponses[0].GetValue()) + fmt.Println(resMsgRes.Id) } func TestQueryLastProposalID(t *testing.T) { @@ -60,3 +67,38 @@ func TestQueryLastProposalID(t *testing.T) { fmt.Println("lastID:", lastID) fmt.Println("index:", endIndex) } + +func TestQueryProposalByID(t *testing.T) { + privateKeHex := "ccfda99f572bd491c085e19986b6db234f7e1f8c6db3fc7a2eabb96615340ec5" + chainID := "ethermint_9000-1" + address := "ethm10ky5utnz5ddlmus5t2mm5ftxal3u0u6rsnx5nl" + grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) + if err != nil { + panic(err) + } + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn) + proposal, err := nodeClient.QueryProposalByID(6) + fmt.Println("id:", proposal.Id) + fmt.Println("proposer:", proposal.Proposer) + fmt.Println("status:", proposal.Status) + fmt.Println("stateRootHash:", proposal.StateRootHash) + fmt.Println("winner:", proposal.Winner) + fmt.Println("voteList:", proposal.VotedListPhaseCommit) + fmt.Println("start index:", proposal.StartIndex) + fmt.Println("end index:", proposal.EndIndex) + fmt.Println("bitcoinTx:", proposal.BitcoinTxHash) +} + +func TestCommitterBitcoinTx(t *testing.T) { + privateKeHex := "ccfda99f572bd491c085e19986b6db234f7e1f8c6db3fc7a2eabb96615340ec5" + chainID := "ethermint_9000-1" + address := "ethm10ky5utnz5ddlmus5t2mm5ftxal3u0u6rsnx5nl" + grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) + if err != nil { + panic(err) + } + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn) + res, err := nodeClient.CommitterBitcoinTx(&xcommitterTypes.MsgBitcoinTx{Id: 1, From: "ethm10ky5utnz5ddlmus5t2mm5ftxal3u0u6rsnx5nl", BitcoinTxHash: "1234567890"}) + require.NoError(t, err) + fmt.Println(res) +}