utils.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package lightclient
  2. import (
  3. "fmt"
  4. rpcclient "github.com/tendermint/tendermint/rpc/client"
  5. tmtypes "github.com/tendermint/tendermint/types"
  6. )
  7. func GetInitConsensusState(node rpcclient.Client, height int64) (*ConsensusState, error) {
  8. status, err := node.Status()
  9. if err != nil {
  10. return nil, err
  11. }
  12. nextValHeight := height + 1
  13. nextValidatorSet, err := node.Validators(&nextValHeight)
  14. if err != nil {
  15. return nil, err
  16. }
  17. header, err := node.Block(&height)
  18. if err != nil {
  19. return nil, err
  20. }
  21. appHash := header.BlockMeta.Header.AppHash
  22. curValidatorSetHash := header.BlockMeta.Header.ValidatorsHash
  23. cs := &ConsensusState{
  24. ChainID: status.NodeInfo.Network,
  25. Height: uint64(height),
  26. AppHash: appHash,
  27. CurValidatorSetHash: curValidatorSetHash,
  28. NextValidatorSet: &tmtypes.ValidatorSet{
  29. Validators: nextValidatorSet.Validators,
  30. },
  31. }
  32. return cs, nil
  33. }
  34. func QueryTendermintHeader(node rpcclient.Client, height int64) (*Header, error) {
  35. nextHeight := height + 1
  36. commit, err := node.Commit(&height)
  37. if err != nil {
  38. return nil, err
  39. }
  40. validators, err := node.Validators(&height)
  41. if err != nil {
  42. return nil, err
  43. }
  44. nextvalidators, err := node.Validators(&nextHeight)
  45. if err != nil {
  46. return nil, err
  47. }
  48. header := &Header{
  49. SignedHeader: commit.SignedHeader,
  50. ValidatorSet: tmtypes.NewValidatorSet(validators.Validators),
  51. NextValidatorSet: tmtypes.NewValidatorSet(nextvalidators.Validators),
  52. }
  53. return header, nil
  54. }
  55. func QueryKeyWithProof(node rpcclient.Client, key []byte, storeName string, height int64) ([]byte, []byte, []byte, error) {
  56. opts := rpcclient.ABCIQueryOptions{
  57. Height: height,
  58. Prove: true,
  59. }
  60. path := fmt.Sprintf("/store/%s/%s", storeName, "key")
  61. result, err := node.ABCIQueryWithOptions(path, key, opts)
  62. if err != nil {
  63. return nil, nil, nil, err
  64. }
  65. proofBytes, err := result.Response.Proof.Marshal()
  66. if err != nil {
  67. return nil, nil, nil, err
  68. }
  69. return key, result.Response.Value, proofBytes, nil
  70. }