Skip to content

Commit

Permalink
add submitProof interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Jan 23, 2024
1 parent 8945c21 commit 894294d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 12 deletions.
57 changes: 54 additions & 3 deletions pkg/b2node/b2node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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...)
Expand Down Expand Up @@ -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
}
60 changes: 51 additions & 9 deletions pkg/b2node/b2node_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}

0 comments on commit 894294d

Please sign in to comment.