github.com/klaytn/klaytn@v1.12.1/tests/transaction_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from tests/transaction_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package tests 22 23 import ( 24 "crypto/ecdsa" 25 "errors" 26 "math/big" 27 "testing" 28 29 "github.com/klaytn/klaytn/blockchain" 30 "github.com/klaytn/klaytn/blockchain/types" 31 "github.com/klaytn/klaytn/kerrors" 32 "github.com/klaytn/klaytn/log" 33 "github.com/klaytn/klaytn/params" 34 "github.com/stretchr/testify/assert" 35 ) 36 37 func TestTransaction(t *testing.T) { 38 t.Parallel() 39 40 txt := new(testMatcher) 41 txt.config(`^Byzantium/`, params.ChainConfig{}) 42 43 txt.walk(t, transactionTestDir, func(t *testing.T, name string, test *TransactionTest) { 44 cfg := txt.findConfig(name) 45 if err := txt.checkFailure(t, name, test.Run(cfg)); err != nil { 46 t.Error(err) 47 } 48 }) 49 } 50 51 // TestAccountCreationDisable tries to use accountCreation tx types which is disabled now. 52 // The tx should be invalided in txPool and execution process. 53 func TestAccountCreationDisable(t *testing.T) { 54 log.EnableLogForTest(log.LvlCrit, log.LvlTrace) 55 56 // the same with types.errUndefinedTxType 57 errUndefinedTxType := errors.New("undefined tx type") 58 59 // Initialize blockchain 60 bcdata, err := NewBCData(6, 4) 61 if err != nil { 62 t.Fatal(err) 63 } 64 defer bcdata.Shutdown() 65 66 // Initialize address-balance map for verification 67 accountMap := NewAccountMap() 68 if err := accountMap.Initialize(bcdata); err != nil { 69 t.Fatal(err) 70 } 71 72 signer := types.LatestSignerForChainID(bcdata.bc.Config().ChainID) 73 74 // reservoir account 75 reservoir := &TestAccountType{ 76 Addr: *bcdata.addrs[0], 77 Keys: []*ecdsa.PrivateKey{bcdata.privKeys[0]}, 78 Nonce: uint64(0), 79 } 80 81 anon, err := createAnonymousAccount("a5c9a50938a089618167c9d67dbebc0deaffc3c76ddc6b40c2777ae59438e989") 82 assert.Equal(t, nil, err) 83 84 // make TxPool to test validation in 'TxPool add' process 85 txpool := blockchain.NewTxPool(blockchain.DefaultTxPoolConfig, bcdata.bc.Config(), bcdata.bc) 86 87 { 88 // generate an accountCreation tx 89 values := map[types.TxValueKeyType]interface{}{ 90 types.TxValueKeyNonce: reservoir.GetNonce(), 91 types.TxValueKeyFrom: reservoir.GetAddr(), 92 types.TxValueKeyTo: anon.Addr, 93 types.TxValueKeyAmount: big.NewInt(0), 94 types.TxValueKeyGasLimit: gasLimit, 95 types.TxValueKeyGasPrice: big.NewInt(25 * params.Ston), 96 types.TxValueKeyHumanReadable: false, 97 types.TxValueKeyAccountKey: anon.AccKey, 98 } 99 100 tx, err := types.NewAccountCreationTransactionWithMap(values) 101 assert.Equal(t, nil, err) 102 103 err = tx.SignWithKeys(signer, reservoir.Keys) 104 assert.Equal(t, nil, err) 105 106 // fail to add tx in txPool 107 err = txpool.AddRemote(tx) 108 assert.Equal(t, errUndefinedTxType, err) 109 110 // fail to execute tx 111 receipt, err := applyTransaction(t, bcdata, tx) 112 assert.Equal(t, errUndefinedTxType, err) 113 assert.Equal(t, (*types.Receipt)(nil), receipt) 114 } 115 } 116 117 // TestContractDeployWithDisabledAddress tests invalid contract deploy transactions. 118 // 1. If the humanReadable field of an tx is 'true', it should fail. 119 // 2. If the recipient field of an tx is not nil, it should fail. 120 func TestContractDeployWithDisabledAddress(t *testing.T) { 121 log.EnableLogForTest(log.LvlCrit, log.LvlTrace) 122 123 testTxTypes := []types.TxType{ 124 types.TxTypeSmartContractDeploy, 125 types.TxTypeFeeDelegatedSmartContractDeploy, 126 types.TxTypeFeeDelegatedSmartContractDeployWithRatio, 127 } 128 129 // Initialize blockchain 130 bcdata, err := NewBCData(6, 4) 131 if err != nil { 132 t.Fatal(err) 133 } 134 defer bcdata.Shutdown() 135 136 // Initialize address-balance map for verification 137 accountMap := NewAccountMap() 138 if err := accountMap.Initialize(bcdata); err != nil { 139 t.Fatal(err) 140 } 141 142 signer := types.LatestSignerForChainID(bcdata.bc.Config().ChainID) 143 144 // reservoir account 145 reservoir := &TestAccountType{ 146 Addr: *bcdata.addrs[0], 147 Keys: []*ecdsa.PrivateKey{bcdata.privKeys[0]}, 148 Nonce: uint64(0), 149 } 150 151 contract, err := createAnonymousAccount("a5c9a50938a089618167c9d67dbebc0deaffc3c76ddc6b40c2777ae59438e989") 152 assert.Equal(t, nil, err) 153 154 // make TxPool to test validation in 'TxPool add' process 155 txpool := blockchain.NewTxPool(blockchain.DefaultTxPoolConfig, bcdata.bc.Config(), bcdata.bc) 156 157 for _, txType := range testTxTypes { 158 // generate an invalid contract deploy tx with humanReadable flag as true 159 { 160 values, _ := genMapForTxTypes(reservoir, nil, txType) 161 values[types.TxValueKeyHumanReadable] = true 162 163 tx, err := types.NewTransactionWithMap(txType, values) 164 assert.Equal(t, nil, err) 165 166 err = tx.SignWithKeys(signer, reservoir.Keys) 167 assert.Equal(t, nil, err) 168 169 if txType.IsFeeDelegatedTransaction() { 170 err = tx.SignFeePayerWithKeys(signer, reservoir.Keys) 171 assert.Equal(t, nil, err) 172 } 173 // fail to add tx in txPool 174 err = txpool.AddRemote(tx) 175 assert.Equal(t, kerrors.ErrHumanReadableNotSupported, err) 176 177 // fail to execute tx 178 receipt, err := applyTransaction(t, bcdata, tx) 179 assert.Equal(t, kerrors.ErrHumanReadableNotSupported, err) 180 assert.Equal(t, (*types.Receipt)(nil), receipt) 181 } 182 183 // generate an invalid contract deploy tx with an recipient address not nil 184 { 185 values, _ := genMapForTxTypes(reservoir, nil, txType) 186 values[types.TxValueKeyTo] = &contract.Addr 187 188 tx, err := types.NewTransactionWithMap(txType, values) 189 assert.Equal(t, nil, err) 190 191 err = tx.SignWithKeys(signer, reservoir.Keys) 192 assert.Equal(t, nil, err) 193 194 if txType.IsFeeDelegatedTransaction() { 195 err = tx.SignFeePayerWithKeys(signer, reservoir.Keys) 196 assert.Equal(t, nil, err) 197 } 198 // fail to add tx in txPool 199 err = txpool.AddRemote(tx) 200 assert.Equal(t, kerrors.ErrInvalidContractAddress, err) 201 202 // fail to execute tx 203 receipt, err := applyTransaction(t, bcdata, tx) 204 assert.Equal(t, kerrors.ErrInvalidContractAddress, err) 205 assert.Equal(t, (*types.Receipt)(nil), receipt) 206 } 207 } 208 }