github.com/aergoio/aergo@v1.3.1/types/transaction_test.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/libp2p/go-libp2p-core/crypto"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  var (
    13  	TestNormal          = 0
    14  	TestDuplicatePeerID = 1
    15  	TestInvalidPeerID   = 2
    16  	TestInvalidString   = 3
    17  )
    18  
    19  func TestGovernanceTypeTransaction(t *testing.T) {
    20  	InitGovernance("dpos", false)
    21  
    22  	const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"
    23  	const testReceiver = "AmNhXiU3s2BN26v5B5hT2bbEjvSjqyrBY7DGnD9UqVcwkTrDYyJN"
    24  	account, err := DecodeAddress(testSender)
    25  	assert.NoError(t, err, "should success to decode test address")
    26  	recipient, err := DecodeAddress(testReceiver)
    27  	assert.NoError(t, err, "should success to decode test address")
    28  	tx := &Tx{
    29  		Body: &TxBody{
    30  			Account: account,
    31  			Payload: []byte(`{"Name":"v1unstake"}`),
    32  			Type:    TxType_GOVERNANCE,
    33  		},
    34  	}
    35  
    36  	chainid := []byte("chainid")
    37  	fakechainid := []byte("fake")
    38  	transaction := NewTransaction(tx)
    39  
    40  	transaction.GetBody().ChainIdHash = fakechainid
    41  	err = transaction.Validate(chainid, false)
    42  	assert.EqualError(t, ErrTxInvalidChainIdHash, err.Error(), "invalid chainid")
    43  
    44  	transaction.GetBody().ChainIdHash = chainid
    45  	err = transaction.Validate(chainid, false)
    46  	assert.EqualError(t, ErrTxHasInvalidHash, err.Error(), "empty hash")
    47  
    48  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    49  	err = transaction.Validate(chainid, false)
    50  	assert.EqualError(t, ErrTxInvalidRecipient, err.Error(), "recipient null")
    51  
    52  	transaction.GetTx().GetBody().Recipient = tx.Body.Payload
    53  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    54  	err = transaction.Validate(chainid, false)
    55  	assert.EqualError(t, ErrTxInvalidRecipient, err.Error(), "wrong recipient case")
    56  
    57  	transaction.GetTx().GetBody().Recipient = recipient
    58  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    59  	err = transaction.Validate(chainid, false)
    60  	assert.EqualError(t, ErrTxInvalidRecipient, err.Error(), "recipient should be aergo.*")
    61  
    62  	transaction.GetTx().GetBody().Recipient = []byte(AergoSystem)
    63  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    64  	err = transaction.Validate(chainid, false)
    65  	assert.NoError(t, err, "should success")
    66  
    67  	transaction.GetTx().GetBody().Amount = StakingMinimum.Bytes()
    68  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    69  	err = transaction.Validate(chainid, false)
    70  	assert.NoError(t, err, "should success")
    71  
    72  	transaction.GetTx().GetBody().Payload = buildVoteBPPayloadEx(2, TestInvalidString)
    73  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    74  	err = transaction.Validate(chainid, false)
    75  	assert.EqualError(t, err, ErrTxInvalidPayload.Error(), "invalid string")
    76  
    77  	transaction.GetTx().GetBody().Payload = buildVoteBPPayloadEx(2, TestInvalidPeerID)
    78  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    79  	err = transaction.Validate(chainid, false)
    80  	assert.EqualError(t, err, ErrTxInvalidPayload.Error(), "invalid peer id")
    81  
    82  	transaction.GetTx().GetBody().Payload = buildVoteBPPayloadEx(2, TestDuplicatePeerID)
    83  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    84  	err = transaction.Validate(chainid, false)
    85  	assert.EqualError(t, err, ErrTxInvalidPayload.Error(), "dup peer id")
    86  
    87  	transaction.GetTx().GetBody().Payload = buildVoteBPPayloadEx(2, TestNormal)
    88  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    89  	err = transaction.Validate(chainid, false)
    90  	t.Log(string(transaction.GetTx().GetBody().Payload))
    91  	assert.NoError(t, err, "should success")
    92  
    93  	transaction.GetTx().GetBody().Payload = buildVoteNumBPPayloadEx(1, TestInvalidString)
    94  	transaction.GetTx().Hash = transaction.CalculateTxHash()
    95  	err = transaction.Validate(chainid, false)
    96  	assert.EqualError(t, err, ErrTxInvalidPayload.Error(), "invalid string")
    97  
    98  	transaction.GetTx().GetBody().Payload = buildVoteNumBPPayloadEx(2, TestInvalidString)
    99  	transaction.GetTx().Hash = transaction.CalculateTxHash()
   100  	err = transaction.Validate(chainid, false)
   101  	assert.EqualError(t, err, ErrTxInvalidPayload.Error(), "only one candidate allowed")
   102  
   103  	transaction.GetTx().GetBody().Recipient = []byte(`aergo.name`)
   104  	transaction.GetTx().GetBody().Payload = []byte(`{"Name":"v1createName", "Args":["1"]}`)
   105  	transaction.GetTx().Hash = transaction.CalculateTxHash()
   106  	err = transaction.Validate(chainid, false)
   107  	assert.Error(t, err, "invalid name length in create")
   108  
   109  	transaction.GetTx().GetBody().Payload = []byte(`{"Name":"v1updateName", "Args":["1234567890","AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"]}`)
   110  	transaction.GetTx().Hash = transaction.CalculateTxHash()
   111  	err = transaction.Validate(chainid, false)
   112  	assert.Error(t, err, "invalid name length in update")
   113  }
   114  
   115  func buildVoteBPPayloadEx(count int, err int) []byte {
   116  	var ci CallInfo
   117  	ci.Name = VoteBP
   118  	_, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
   119  	peerid, _ := IDFromPublicKey(pub)
   120  	for i := 0; i < count; i++ {
   121  		if err == TestDuplicatePeerID {
   122  			ci.Args = append(ci.Args, IDB58Encode(peerid))
   123  		} else if err == TestInvalidString {
   124  			ci.Args = append(ci.Args, (i + 1))
   125  		} else if err == TestInvalidPeerID {
   126  			ci.Args = append(ci.Args, string(i+1))
   127  		} else {
   128  			_, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
   129  			peerid, _ := IDFromPublicKey(pub)
   130  			ci.Args = append(ci.Args, IDB58Encode(peerid))
   131  		}
   132  	}
   133  	payload, _ := json.Marshal(ci)
   134  	return payload
   135  }
   136  
   137  func buildVoteNumBPPayloadEx(count int, err int) []byte {
   138  	var ci CallInfo
   139  	ci.Name = VoteNumBP
   140  	candidate := 1
   141  	for i := 0; i < count; i++ {
   142  		if err == TestDuplicatePeerID {
   143  			ci.Args = append(ci.Args, candidate)
   144  		} else if err == TestInvalidString {
   145  			ci.Args = append(ci.Args, (i + 1))
   146  		} else {
   147  			ci.Args = append(ci.Args, strconv.Itoa(i+1))
   148  		}
   149  	}
   150  	payload, _ := json.Marshal(ci)
   151  	return payload
   152  }