github.com/phillinzzz/newBsc@v1.1.6/core/vm/lightclient/utils.go (about)

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