github.com/Finschia/ostracon@v1.1.5/types/protobuf_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/golang/protobuf/proto" // nolint: staticcheck // still used by gogoproto
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/tendermint/go-amino"
    10  
    11  	abci "github.com/tendermint/tendermint/abci/types"
    12  	"github.com/tendermint/tendermint/proto/tendermint/version"
    13  
    14  	"github.com/Finschia/ostracon/crypto"
    15  	"github.com/Finschia/ostracon/crypto/ed25519"
    16  	cryptoenc "github.com/Finschia/ostracon/crypto/encoding"
    17  	"github.com/Finschia/ostracon/crypto/secp256k1"
    18  	"github.com/Finschia/ostracon/types/time"
    19  )
    20  
    21  func TestABCIPubKey(t *testing.T) {
    22  	pkEd := ed25519.GenPrivKey().PubKey()
    23  	pkSecp := secp256k1.GenPrivKey().PubKey()
    24  	err := testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519)
    25  	assert.NoError(t, err)
    26  	err = testABCIPubKey(t, pkSecp, ABCIPubKeyTypeSecp256k1)
    27  	assert.NoError(t, err)
    28  }
    29  
    30  func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) error {
    31  	abciPubKey, err := cryptoenc.PubKeyToProto(pk)
    32  	require.NoError(t, err)
    33  	pk2, err := cryptoenc.PubKeyFromProto(&abciPubKey)
    34  	require.NoError(t, err)
    35  	require.Equal(t, pk, pk2)
    36  	return nil
    37  }
    38  
    39  func TestABCIValidators(t *testing.T) {
    40  	pkEd := ed25519.GenPrivKey().PubKey()
    41  
    42  	// correct validator
    43  	tmValExpected := NewValidator(pkEd, 10)
    44  
    45  	tmVal := NewValidator(pkEd, 10)
    46  
    47  	abciVal := OC2PB.ValidatorUpdate(tmVal)
    48  	tmVals, err := PB2OC.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
    49  	assert.Nil(t, err)
    50  	assert.Equal(t, tmValExpected, tmVals[0])
    51  
    52  	abciVals := OC2PB.ValidatorUpdates(NewValidatorSet(tmVals))
    53  	assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
    54  
    55  	// val with address
    56  	tmVal.Address = pkEd.Address()
    57  
    58  	abciVal = OC2PB.ValidatorUpdate(tmVal)
    59  	tmVals, err = PB2OC.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
    60  	assert.Nil(t, err)
    61  	assert.Equal(t, tmValExpected, tmVals[0])
    62  }
    63  
    64  func TestABCIConsensusParams(t *testing.T) {
    65  	cp := DefaultConsensusParams()
    66  	abciCP := OC2PB.ConsensusParams(cp)
    67  	cp2 := UpdateConsensusParams(*cp, abciCP)
    68  
    69  	assert.Equal(t, *cp, cp2)
    70  }
    71  
    72  func newHeader(
    73  	height int64, commitHash, dataHash, evidenceHash []byte,
    74  ) *Header {
    75  	return &Header{
    76  		Height:         height,
    77  		LastCommitHash: commitHash,
    78  		DataHash:       dataHash,
    79  		EvidenceHash:   evidenceHash,
    80  	}
    81  }
    82  
    83  func TestABCIHeader(t *testing.T) {
    84  	// build a full header
    85  	var height int64 = 5
    86  	header := newHeader(height, []byte("lastCommitHash"), []byte("dataHash"), []byte("evidenceHash"))
    87  	protocolVersion := version.Consensus{Block: 7, App: 8}
    88  	timestamp := time.Now()
    89  	lastBlockID := BlockID{
    90  		Hash: []byte("hash"),
    91  		PartSetHeader: PartSetHeader{
    92  			Total: 10,
    93  			Hash:  []byte("hash"),
    94  		},
    95  	}
    96  	header.Populate(
    97  		protocolVersion, "chainID", timestamp, lastBlockID,
    98  		[]byte("valHash"), []byte("nextValHash"),
    99  		[]byte("consHash"), []byte("appHash"), []byte("lastResultsHash"),
   100  		[]byte("proposerAddress"),
   101  	)
   102  
   103  	cdc := amino.NewCodec()
   104  	headerBz := cdc.MustMarshalBinaryBare(header)
   105  
   106  	pbHeader := OC2PB.Header(header)
   107  	pbHeaderBz, err := proto.Marshal(&pbHeader)
   108  	assert.NoError(t, err)
   109  
   110  	// assert some fields match
   111  	assert.EqualValues(t, protocolVersion.Block, pbHeader.Version.Block)
   112  	assert.EqualValues(t, protocolVersion.App, pbHeader.Version.App)
   113  	assert.EqualValues(t, "chainID", pbHeader.ChainID)
   114  	assert.EqualValues(t, height, pbHeader.Height)
   115  	assert.EqualValues(t, timestamp, pbHeader.Time)
   116  	assert.EqualValues(t, lastBlockID.Hash, pbHeader.LastBlockId.Hash)
   117  	assert.EqualValues(t, []byte("lastCommitHash"), pbHeader.LastCommitHash)
   118  	assert.Equal(t, []byte("proposerAddress"), pbHeader.ProposerAddress)
   119  
   120  	// assert the encodings match
   121  	// NOTE: they don't yet because Amino encodes
   122  	// int64 as zig-zag and we're using non-zigzag in the protobuf.
   123  	// See https://github.com/tendermint/tendermint/issues/2682
   124  	_, _ = headerBz, pbHeaderBz
   125  	// assert.EqualValues(t, headerBz, pbHeaderBz)
   126  
   127  }
   128  
   129  func TestABCIEvidence(t *testing.T) {
   130  	val := NewMockPV()
   131  	blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
   132  	blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
   133  	const chainID = "mychain"
   134  	now := time.Now()
   135  	ev := &DuplicateVoteEvidence{
   136  		VoteA:            makeVote(t, val, chainID, 0, 10, 2, 1, blockID, now),
   137  		VoteB:            makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, now),
   138  		TotalVotingPower: int64(100),
   139  		ValidatorPower:   int64(10),
   140  		Timestamp:        now,
   141  	}
   142  	for _, abciEv := range ev.ABCI() {
   143  		assert.Equal(t, "DUPLICATE_VOTE", abciEv.Type.String())
   144  	}
   145  }
   146  
   147  type pubKeyEddie struct{}
   148  
   149  func (pubKeyEddie) Address() Address                                          { return []byte{} }
   150  func (pubKeyEddie) Bytes() []byte                                             { return []byte{} }
   151  func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool               { return false }
   152  func (pubKeyEddie) VRFVerify(proof []byte, msg []byte) (crypto.Output, error) { return nil, nil }
   153  func (pubKeyEddie) Equals(crypto.PubKey) bool                                 { return false }
   154  func (pubKeyEddie) String() string                                            { return "" }
   155  func (pubKeyEddie) Type() string                                              { return "pubKeyEddie" }
   156  
   157  func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
   158  	pubkey := ed25519.GenPrivKey().PubKey()
   159  
   160  	abciVal := OC2PB.NewValidatorUpdate(pubkey, 10)
   161  	assert.Equal(t, int64(10), abciVal.Power)
   162  
   163  	assert.Panics(t, func() { OC2PB.NewValidatorUpdate(nil, 10) })
   164  	assert.Panics(t, func() { OC2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
   165  }
   166  
   167  func TestABCIValidatorWithoutPubKey(t *testing.T) {
   168  	pkEd := ed25519.GenPrivKey().PubKey()
   169  
   170  	abciVal := OC2PB.Validator(NewValidator(pkEd, 10))
   171  
   172  	// pubkey must be nil
   173  	tmValExpected := abci.Validator{
   174  		Address: pkEd.Address(),
   175  		Power:   10,
   176  	}
   177  
   178  	assert.Equal(t, tmValExpected, abciVal)
   179  }