12 dbm "github.com/tendermint/tmlibs/db"
14 "github.com/bytom/account"
15 "github.com/bytom/asset"
16 "github.com/bytom/blockchain/pseudohsm"
17 "github.com/bytom/consensus"
18 "github.com/bytom/protocol/bc"
19 "github.com/bytom/protocol/bc/types"
20 "github.com/bytom/protocol/validation"
21 "github.com/bytom/protocol/vm"
24 type TxTestConfig struct {
25 Keys []*keyInfo `json:"keys"`
26 Accounts []*accountInfo `json:"accounts"`
27 Transactions []*ttTransaction `json:"transactions"`
30 func (cfg *TxTestConfig) Run() error {
31 dirPath, err := ioutil.TempDir(".", "pseudo_hsm")
35 defer os.RemoveAll(dirPath)
36 hsm, err := pseudohsm.New(dirPath)
41 chainDB := dbm.NewDB("chain_db", "leveldb", "chain_db")
42 defer os.RemoveAll("chain_db")
43 chain, _, _, _ := MockChain(chainDB)
44 txTestDB := dbm.NewDB("tx_test_db", "leveldb", "tx_test_db")
45 defer os.RemoveAll("tx_test_db")
46 accountManager := account.NewManager(txTestDB, chain)
47 assets := asset.NewRegistry(txTestDB, chain)
49 generator := NewTxGenerator(accountManager, assets, hsm)
50 for _, key := range cfg.Keys {
51 if err := generator.createKey(key.Name, key.Password); err != nil {
56 for _, acc := range cfg.Accounts {
57 if err := generator.createAccount(acc.Name, acc.Keys, acc.Quorum); err != nil {
63 BlockHeader: &bc.BlockHeader{
68 for _, t := range cfg.Transactions {
69 tx, err := t.create(generator)
74 tx.TxData.Version = t.Version
75 tx.Tx = types.MapTx(&tx.TxData)
76 status, err := validation.ValidateTx(tx.Tx, block)
78 if result != t.Valid {
79 return fmt.Errorf("tx %s validate failed, expected: %t, have: %t", t.Describe, t.Valid, result)
86 if err != nil && status.GasValid {
89 if gasOnlyTx != t.GasOnly {
90 return fmt.Errorf("gas only tx %s validate failed", t.Describe)
92 if result && t.TxFee != status.BTMValue {
93 return fmt.Errorf("gas used dismatch, expected: %d, have: %d", t.TxFee, status.BTMValue)
99 type ttTransaction struct {
101 Describe string `json:"describe"`
102 Version uint64 `json:"version"`
103 Valid bool `json:"valid"`
104 GasOnly bool `json:"gas_only"`
105 TxFee uint64 `json:"tx_fee"`
108 // UnmarshalJSON unmarshal transaction with default version 1
109 func (t *ttTransaction) UnmarshalJSON(data []byte) error {
110 type typeAlias ttTransaction
115 err := json.Unmarshal(data, tx)
119 *t = ttTransaction(*tx)
123 func (t *ttTransaction) create(g *TxGenerator) (*types.Tx, error) {
125 for _, input := range t.Inputs {
127 case "spend_account":
128 utxo, err := g.mockUtxo(input.AccountAlias, input.AssetAlias, input.Amount)
132 if err := g.AddTxInputFromUtxo(utxo, input.AccountAlias); err != nil {
136 _, err := g.createAsset(input.AccountAlias, input.AssetAlias)
140 if err := g.AddIssuanceInput(input.AssetAlias, input.Amount); err != nil {
146 for _, output := range t.Outputs {
149 if err := g.AddTxOutput(output.AccountAlias, output.AssetAlias, output.Amount); err != nil {
153 if err := g.AddRetirement(output.AssetAlias, output.Amount); err != nil {
158 return g.Sign(t.Passwords)
161 func TestTx(t *testing.T) {
162 walk(t, txTestDir, func(t *testing.T, name string, test *TxTestConfig) {
163 if err := test.Run(); err != nil {
169 func TestCoinbaseMature(t *testing.T) {
170 db := dbm.NewDB("test_coinbase_mature_db", "leveldb", "test_coinbase_mature_db")
171 defer os.RemoveAll("test_coinbase_mature_db")
172 chain, _, _, _ := MockChain(db)
174 defaultCtrlProg := []byte{byte(vm.OP_TRUE)}
175 if err := AppendBlocks(chain, 1); err != nil {
179 height := chain.BestBlockHeight()
180 block, err := chain.GetBlockByHeight(height)
185 tx, err := CreateTxFromTx(block.Transactions[0], 0, 1000000000, defaultCtrlProg)
190 txs := []*types.Tx{tx}
191 matureHeight := chain.BestBlockHeight() + consensus.CoinbasePendingBlockNumber
192 currentHeight := chain.BestBlockHeight()
193 for h := currentHeight + 1; h < matureHeight; h++ {
194 block, err := NewBlock(chain, txs, defaultCtrlProg)
198 if err := SolveAndUpdate(chain, block); err == nil {
199 t.Fatal("spent immature coinbase output success")
201 block, err = NewBlock(chain, nil, defaultCtrlProg)
205 if err := SolveAndUpdate(chain, block); err != nil {
210 block, err = NewBlock(chain, txs, defaultCtrlProg)
214 if err := SolveAndUpdate(chain, block); err != nil {
215 t.Fatalf("spent mature coinbase output failed: %s", err)
219 func TestCoinbaseTx(t *testing.T) {
220 db := dbm.NewDB("test_coinbase_tx_db", "leveldb", "test_coinbase_tx_db")
221 defer os.RemoveAll("test_coinbase_tx_db")
222 chain, _, _, _ := MockChain(db)
224 defaultCtrlProg := []byte{byte(vm.OP_TRUE)}
225 if err := AppendBlocks(chain, 1); err != nil {
229 block, err := chain.GetBlockByHeight(chain.BestBlockHeight())
234 tx, err := CreateTxFromTx(block.Transactions[0], 0, 1000000000, defaultCtrlProg)
239 block, err = NewBlock(chain, []*types.Tx{tx}, defaultCtrlProg)
244 txFees := []uint64{100000, 5000000000000}
245 for _, txFee := range txFees {
246 coinbaseTx, err := CreateCoinbaseTx(defaultCtrlProg, block.Height, txFee)
251 if err := ReplaceCoinbase(block, coinbaseTx); err != nil {
255 if err := SolveAndUpdate(chain, block); err == nil {
256 t.Fatalf("invalid coinbase tx validate success")