From c99e78d35b83e57081c0d15fb511118a7d7c9203 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Fri, 6 Sep 2024 17:53:07 -0500 Subject: [PATCH] `GetCommit` with just Time & AppHash returns --- cclient/consensus.go | 7 +++++- cclient/consensus_cmbft.go | 7 ++++-- cclient/consensus_gordian.go | 44 +++++++++++++++++++++++++++++++++--- cclient/gordian_test.go | 6 ++++- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/cclient/consensus.go b/cclient/consensus.go index 66ca3f63c..b8b0f082d 100644 --- a/cclient/consensus.go +++ b/cclient/consensus.go @@ -40,7 +40,7 @@ type ConsensusClient interface { page, perPage *int, orderBy string, ) (*coretypes.ResultBlockSearch, error) - GetCommit(ctx context.Context, height uint64) (*coretypes.ResultCommit, error) + GetCommit(ctx context.Context, height uint64) (*ResultCommit, error) GetABCIQueryWithOptions( ctx context.Context, path string, @@ -51,6 +51,11 @@ type ConsensusClient interface { SimulateTransaction(ctx context.Context, tx []byte, cfg *SimTxConfig) (types.GasInfo, error) } +type ResultCommit struct { + Time time.Time `json:"time"` + AppHash []byte `json:"app_hash"` +} + type SimTxConfig struct { // CometBFT only function (QueryABCI). QueryABCIFunc func(ctx context.Context, req abci.RequestQuery) (abci.ResponseQuery, error) diff --git a/cclient/consensus_cmbft.go b/cclient/consensus_cmbft.go index 2537c0425..e353defaa 100644 --- a/cclient/consensus_cmbft.go +++ b/cclient/consensus_cmbft.go @@ -92,13 +92,16 @@ func (r CometRPCClient) GetBlockSearch(ctx context.Context, query string, page * } // GetCommit implements ConsensusClient. -func (r CometRPCClient) GetCommit(ctx context.Context, height uint64) (*coretypes.ResultCommit, error) { +func (r CometRPCClient) GetCommit(ctx context.Context, height uint64) (*ResultCommit, error) { h := int64(height) c, err := r.Commit(ctx, &h) if err != nil { return nil, fmt.Errorf("failed to get commit: %w", err) } - return c, nil + return &ResultCommit{ + AppHash: c.AppHash, + Time: c.Time, + }, nil } // GetValidators implements ConsensusClient. diff --git a/cclient/consensus_gordian.go b/cclient/consensus_gordian.go index 416a08bd3..c8874db76 100644 --- a/cclient/consensus_gordian.go +++ b/cclient/consensus_gordian.go @@ -144,8 +144,45 @@ func (g *GordianConsensus) GetBlockTime(ctx context.Context, height uint64) (tim } // GetCommit implements ConsensusClient. -func (g *GordianConsensus) GetCommit(ctx context.Context, height uint64) (*coretypes.ResultCommit, error) { - panic("unimplemented") +func (g *GordianConsensus) GetCommit(ctx context.Context, height uint64) (*ResultCommit, error) { + // looks like we just need the apphash. returning just this for gordian to see how it goes. + // get latest header from the network + + res, err := http.Get(fmt.Sprintf("%s/commit", g.addr)) + if err != nil { + fmt.Printf("error making http request: %s\n", err) + os.Exit(1) + } + + // tmconsensus.CommittedBlock + type GetCommitResponse struct { + BlockHash []byte `protobuf:"bytes,1,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"` + BlockHashPrevious []byte `protobuf:"bytes,2,opt,name=block_hash_previous,json=blockHashPrevious,proto3" json:"block_hash_previous,omitempty"` + Height uint64 `protobuf:"varint,3,opt,name=height,proto3" json:"height,omitempty"` + // PreviousCommitProof *CommitProof `protobuf:"bytes,4,opt,name=previous_commit_proof,json=previousCommitProof,proto3" json:"previous_commit_proof,omitempty"` + // ValidatorSet *ValidatorSet `protobuf:"bytes,5,opt,name=validator_set,json=validatorSet,proto3" json:"validator_set,omitempty"` + // ValidatorSetNext *ValidatorSet `protobuf:"bytes,6,opt,name=validator_set_next,json=validatorSetNext,proto3" json:"validator_set_next,omitempty"` + DataId []byte `protobuf:"bytes,7,opt,name=data_id,json=dataId,proto3" json:"data_id,omitempty"` + AppStatePrevHash []byte `protobuf:"bytes,8,opt,name=app_state_prev_hash,json=appStatePrevHash,proto3" json:"app_state_prev_hash,omitempty"` // annotations + } + + var resp GetCommitResponse + if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { + fmt.Printf("error decoding response: %s\n", err) + os.Exit(1) + } + + // Get this from the header annotation directly? + bt, err := g.GetBlockTime(ctx, resp.Height) + if err != nil { + return nil, fmt.Errorf("failed to get block time: %w", err) + } + + // TODO: do we need the full coretypes.NewResultCommit ? Does not seem like it + return &ResultCommit{ + AppHash: resp.AppStatePrevHash, + Time: bt, + }, nil } // GetStatus implements ConsensusClient. @@ -206,7 +243,8 @@ func (g *GordianConsensus) GetTx(ctx context.Context, hash []byte, prove bool) ( // GetTxSearch implements ConsensusClient. func (g *GordianConsensus) GetTxSearch(ctx context.Context, query string, prove bool, page *int, perPage *int, orderBy string) (*ResultTxSearch, error) { - panic("unimplemented") + // TODO: + return nil, nil } // TODO: GetValidators needs pubkey -> address conversions diff --git a/cclient/gordian_test.go b/cclient/gordian_test.go index fa77ec9ac..5f4db0dbb 100644 --- a/cclient/gordian_test.go +++ b/cclient/gordian_test.go @@ -14,7 +14,7 @@ import ( // cat example-tx-signed.json const tx = `{"body":{"messages":[{"@type":"/cosmos.bank.v1beta1.MsgSend","from_address":"cosmos1r5v5srda7xfth3hn2s26txvrcrntldjumt8mhl","to_address":"cosmos10r39fueph9fq7a6lgswu4zdsg8t3gxlqvvvyvn","amount":[{"denom":"stake","amount":"1"}]}],"memo":"","timeout_height":"0","unordered":false,"timeout_timestamp":"0001-01-01T00:00:00Z","extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"ArpmqEz3g5rxcqE+f8n15wCMuLyhWF+PO6+zA57aPB/d"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":"1"}],"fee":{"amount":[],"gas_limit":"200000","payer":"cosmos1r5v5srda7xfth3hn2s26txvrcrntldjumt8mhl","granter":""},"tip":null},"signatures":["CeyHZH8itZikoY8mWtfCzM46qZfOLkncHRe8CxludOUpgvxklTcy4+EetVN++OzBgxxXUMG/B5DIuJAFQ4G6cg=="]}` -// go test -timeout 3000s -run ^TestGordian$ github.com/cosmos/relayer/v2/cclient -v +// go test -timeout 3000s -run ^TestGordian$ github.com/cosmos/relayer/v2/cclient -v -count 1 func TestGordian(t *testing.T) { // TODO: this test is only local for now. Will add CI in the future if os.Getenv("IS_LOCAL_TESTING_GORDIAN") == "" { @@ -53,4 +53,8 @@ func TestGordian(t *testing.T) { require.NoError(t, err) t.Log("vals", vals) + c, err := gc.GetCommit(ctx, uint64(s.LatestBlockHeight)) + require.NoError(t, err) + t.Logf("commit: %+v", c) + }