Skip to content

Commit

Permalink
GetCommit with just Time & AppHash returns
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Sep 6, 2024
1 parent f2ae8db commit c99e78d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
7 changes: 6 additions & 1 deletion cclient/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions cclient/consensus_cmbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 41 additions & 3 deletions cclient/consensus_gordian.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion cclient/gordian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") == "" {
Expand Down Expand Up @@ -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)

}

0 comments on commit c99e78d

Please sign in to comment.