github.com/okex/exchain@v1.8.0/libs/tendermint/privval/file_ibc_adapter.go (about)

     1  package privval
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/gogo/protobuf/proto"
     7  	"github.com/okex/exchain/libs/tendermint/libs/protoio"
     8  	tmproto "github.com/okex/exchain/libs/tendermint/proto/types"
     9  	"github.com/okex/exchain/libs/tendermint/types"
    10  	tmtime "github.com/okex/exchain/libs/tendermint/types/time"
    11  	"time"
    12  )
    13  
    14  func ibcCheckVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool) {
    15  	var lastVote, newVote tmproto.CanonicalVote
    16  	if err := protoio.UnmarshalDelimited(lastSignBytes, &lastVote); err != nil {
    17  		panic(fmt.Sprintf("LastSignBytes cannot be unmarshalled into vote: %v", err))
    18  	}
    19  	if err := protoio.UnmarshalDelimited(newSignBytes, &newVote); err != nil {
    20  		panic(fmt.Sprintf("signBytes cannot be unmarshalled into vote: %v", err))
    21  	}
    22  
    23  	lastTime := lastVote.Timestamp
    24  	// set the times to the same value and check equality
    25  	now := tmtime.Now()
    26  	lastVote.Timestamp = now
    27  	newVote.Timestamp = now
    28  
    29  	return lastTime, proto.Equal(&newVote, &lastVote)
    30  }
    31  
    32  func originCheckVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool) {
    33  	var lastVote, newVote types.CanonicalVote
    34  	if err := cdc.UnmarshalBinaryLengthPrefixed(lastSignBytes, &lastVote); err != nil {
    35  		panic(fmt.Sprintf("LastSignBytes cannot be unmarshalled into vote: %v", err))
    36  	}
    37  	if err := cdc.UnmarshalBinaryLengthPrefixed(newSignBytes, &newVote); err != nil {
    38  		panic(fmt.Sprintf("signBytes cannot be unmarshalled into vote: %v", err))
    39  	}
    40  
    41  	lastTime := lastVote.Timestamp
    42  
    43  	// set the times to the same value and check equality
    44  	now := tmtime.Now()
    45  	lastVote.Timestamp = now
    46  	newVote.Timestamp = now
    47  	lastVoteBytes, _ := cdc.MarshalJSON(lastVote)
    48  	newVoteBytes, _ := cdc.MarshalJSON(newVote)
    49  
    50  	return lastTime, bytes.Equal(newVoteBytes, lastVoteBytes)
    51  }