Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Aug 13, 2024
1 parent a4c824a commit 2f3c476
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions clientcontroller/cosmwasm/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,33 +298,22 @@ func (wc *CosmwasmConsumerController) QueryLastPublicRandCommit(fpPk *btcec.Publ
return nil, fmt.Errorf("failed to query smart contract state: %w", err)
}

// Define a response struct
var commits []PubRandCommitResponse
err = json.Unmarshal(dataFromContract.Data, &commits)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

if len(commits) == 0 {
if len(dataFromContract.Data) == 0 {
// expected when there is no PR commit at all
// `get_pub_rand_commit`'s return type is Vec<PubRandCommit> and it can be
// empty vector if no results found
return nil, nil
}

if len(commits) > 1 {
return nil, fmt.Errorf("expected length to be 1, but got :%d", len(commits))
// Define a response struct
var commitResp PubRandCommitResponse
err = json.Unmarshal(dataFromContract.Data, &commitResp)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

// Convert the response to the expected map format
var commit *fptypes.PubRandCommit = nil
for _, commitRes := range commits {
commitCopy := commitRes // create a copy to avoid referencing the loop variable
commit = &fptypes.PubRandCommit{
StartHeight: commitCopy.StartHeight,
NumPubRand: commitCopy.NumPubRand,
Commitment: commitCopy.Commitment,
}
commit := &fptypes.PubRandCommit{
StartHeight: commitResp.StartHeight,
NumPubRand: commitResp.NumPubRand,
Commitment: commitResp.Commitment,
}

if err := commit.Validate(); err != nil {
Expand Down

0 comments on commit 2f3c476

Please sign in to comment.