github.com/devwanda/aphelion-staking@v0.33.9/types/protobuf_test.go (about)

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