Skip to content

Commit

Permalink
Merge PR: fix cli query tx because of local database mismatch (#2464)
Browse files Browse the repository at this point in the history
* fix cli change chain name & add more case of query validatorSet

* rm sortvalidators

* rename

Co-authored-by: KamiD <[email protected]>
  • Loading branch information
ItsFunny and KamiD authored Aug 25, 2022
1 parent aeba520 commit 94087fc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
4 changes: 2 additions & 2 deletions libs/cosmos-sdk/client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func NewCLIContextWithInputAndFrom(input io.Reader, from string) CLIContext {
fmt.Printf("failted to call status: %v\n", err)
} else {
if st.NodeInfo.Network == tmtypes.MainNet {
tmtypes.SetupMainNetEnvironment()
tmtypes.SetupMainNetEnvironment(st.SyncInfo.EarliestBlockHeight)
} else if st.NodeInfo.Network == tmtypes.TestNet {
tmtypes.SetupTestNetEnvironment()
tmtypes.SetupTestNetEnvironment(st.SyncInfo.EarliestBlockHeight)
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions libs/tendermint/lite/client/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ func (p *provider) StatusClient() rpcclient.StatusClient {

// LatestFullCommit implements Provider.
func (p *provider) LatestFullCommit(chainID string, minHeight, maxHeight int64) (fc lite.FullCommit, err error) {
if chainID != p.chainID {
err = fmt.Errorf("expected chainID %s, got %s", p.chainID, chainID)
return
}
if maxHeight != 0 && maxHeight < minHeight {
err = fmt.Errorf("need maxHeight == 0 or minHeight <= maxHeight, got min %v and max %v",
minHeight, maxHeight)
Expand All @@ -74,6 +70,10 @@ func (p *provider) LatestFullCommit(chainID string, minHeight, maxHeight int64)
if err != nil {
return
}
if !verifyEqualChainId(commit.ChainID, p.chainID, commit.Height) {
err = fmt.Errorf("expected chainID %s, got %s", p.chainID, commit.ChainID)
return
}
fc, err = p.fillFullCommit(commit.SignedHeader)
return
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func (p *provider) ValidatorSet(chainID string, height int64) (valset *types.Val
}

func (p *provider) getValidatorSet(chainID string, height int64) (valset *types.ValidatorSet, err error) {
if chainID != p.chainID {
if !verifyEqualChainId(chainID, p.chainID, height) {
err = fmt.Errorf("expected chainID %s, got %s", p.chainID, chainID)
return
}
Expand All @@ -122,7 +122,6 @@ func (p *provider) getValidatorSet(chainID string, height int64) (valset *types.

// This does no validation.
func (p *provider) fillFullCommit(signedHeader types.SignedHeader) (fc lite.FullCommit, err error) {

// Get the validators.
valset, err := p.getValidatorSet(signedHeader.ChainID, signedHeader.Height)
if err != nil {
Expand All @@ -137,3 +136,12 @@ func (p *provider) fillFullCommit(signedHeader types.SignedHeader) (fc lite.Full

return lite.NewFullCommit(signedHeader, valset, nextValset), nil
}

func verifyEqualChainId(chain1 string, flagChainId string, h int64) bool {
if flagChainId == types.TestNet {
if h < types.TestNetChangeChainId && chain1 == types.TestNetChainName1 {
return true
}
}
return chain1 == flagChainId
}
20 changes: 18 additions & 2 deletions libs/tendermint/lite/dynamic_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,21 @@ func (dv *DynamicVerifier) Verify(shdr types.SignedHeader) error {
// The full commit at h-1 contains the valset to sign for h.
prevHeight := shdr.Height - 1
trustedFC, err := dv.trusted.LatestFullCommit(dv.chainID, 1, prevHeight)
commit := func() error {
return nil
}
if err != nil {
return err
// if the commit is not found in the local database , fetch the acommit from the source node
if !lerr.IsErrCommitNotFound(err) {
return err
}
if trustedFC, err = dv.source.LatestFullCommit(dv.chainID, 1, prevHeight); err != nil {
return err
} else {
commit = func() error {
return dv.trusted.SaveFullCommit(trustedFC)
}
}
}

// sync up to the prevHeight and assert our latest NextValidatorSet
Expand Down Expand Up @@ -180,7 +193,10 @@ func (dv *DynamicVerifier) Verify(shdr types.SignedHeader) error {
return err
}
// Trust it.
return dv.trusted.SaveFullCommit(nfc)
if err = dv.trusted.SaveFullCommit(nfc); err != nil {
return err
}
return commit()
}

// verifyAndSave will verify if this is a valid source full commit given the
Expand Down
5 changes: 3 additions & 2 deletions libs/tendermint/lite/proxy/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ func NewVerifier(
// TODO: Make this more secure, e.g. make it interactive in the console?
_, err = trust.LatestFullCommit(chainID, types.GetStartBlockHeight()+1, 1<<63-1)
if err != nil {
logger.Info("lite/proxy/NewVerifier found no trusted full commit, initializing from source from height 1...")
fc, err := source.LatestFullCommit(chainID, types.GetStartBlockHeight()+1, 1<<63-1)
logger.Info("lite/proxy/NewVerifier found no trusted full commit, initializing from source from height prune height")
// note: use the prune height to catchup the validators,see:dynamic_verifier.goL149:Verify#updateToHeight
fc, err := source.LatestFullCommit(chainID, types.GetNodePruneHeight()+1, types.GetNodePruneHeight()+1)
if err != nil {
return nil, errors.Wrap(err, "fetching source full commit @ height 1")
}
Expand Down
16 changes: 14 additions & 2 deletions libs/tendermint/types/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (
MILESTONE_VENUS2_HEIGHT string
milestoneVenus2Height int64

// note: it stores the earlies height of the node,and it is used by cli
nodePruneHeight int64

once sync.Once
)

Expand All @@ -50,6 +53,9 @@ const (

MainNetGenesisHeight = 2322600
TestNetGenesisHeight = 1121818

TestNetChangeChainId = 2270901
TestNetChainName1 = "okexchain-65"
)

func init() {
Expand All @@ -74,17 +80,19 @@ func string2number(input string) int64 {
return res
}

func SetupMainNetEnvironment() {
func SetupMainNetEnvironment(pruneH int64) {
milestoneVenusHeight = MainNetVeneusHeight
milestoneMercuryHeight = MainNetMercuyHeight
genesisHeight = MainNetGenesisHeight
nodePruneHeight = pruneH
milestoneVenus1Height = MainNetVeneus1Height
}

func SetupTestNetEnvironment() {
func SetupTestNetEnvironment(pruneH int64) {
milestoneVenusHeight = TestNetVeneusHeight
milestoneMercuryHeight = TestNetMercuryHeight
genesisHeight = TestNetGenesisHeight
nodePruneHeight = pruneH
milestoneVenus1Height = TestNetVeneus1Height
}

Expand Down Expand Up @@ -131,6 +139,10 @@ func GetStartBlockHeight() int64 {
return genesisHeight
}

func GetNodePruneHeight() int64 {
return nodePruneHeight
}

func GetVenusHeight() int64 {
return milestoneVenusHeight
}
Expand Down
3 changes: 2 additions & 1 deletion libs/tendermint/types/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package types
import (
"bytes"
"fmt"
"github.com/okex/exchain/libs/tendermint/global"
"math"
"math/big"
"sort"
"strings"

"github.com/okex/exchain/libs/tendermint/global"

"github.com/pkg/errors"

"github.com/okex/exchain/libs/tendermint/crypto/merkle"
Expand Down

0 comments on commit 94087fc

Please sign in to comment.