12 log "github.com/sirupsen/logrus"
14 "github.com/bytom/bytom/account"
15 "github.com/bytom/bytom/asset"
16 "github.com/bytom/bytom/blockchain/pseudohsm"
17 "github.com/bytom/bytom/consensus"
18 dbm "github.com/bytom/bytom/database/leveldb"
19 "github.com/bytom/bytom/protocol/bc"
20 "github.com/bytom/bytom/protocol/bc/types"
21 "github.com/bytom/bytom/protocol/validation"
22 "github.com/bytom/bytom/protocol/vm"
26 log.SetLevel(log.ErrorLevel)
29 type TxTestConfig struct {
30 Keys []*keyInfo `json:"keys"`
31 Accounts []*accountInfo `json:"accounts"`
32 Transactions []*ttTransaction `json:"transactions"`
35 func (cfg *TxTestConfig) Run() error {
36 dirPath, err := ioutil.TempDir(".", "pseudo_hsm")
40 defer os.RemoveAll(dirPath)
41 hsm, err := pseudohsm.New(dirPath)
46 chainDB := dbm.NewDB("chain_db", "leveldb", "chain_db")
47 defer os.RemoveAll("chain_db")
48 chain, _, _, _ := MockChain(chainDB)
49 txTestDB := dbm.NewDB("tx_test_db", "leveldb", "tx_test_db")
50 defer os.RemoveAll("tx_test_db")
51 accountManager := account.NewManager(txTestDB, chain)
52 assets := asset.NewRegistry(txTestDB, chain)
54 generator := NewTxGenerator(accountManager, assets, hsm)
55 for _, key := range cfg.Keys {
56 if err := generator.createKey(key.Name, key.Password); err != nil {
61 for _, acc := range cfg.Accounts {
62 if err := generator.createAccount(acc.Name, acc.Keys, acc.Quorum); err != nil {
68 BlockHeader: &bc.BlockHeader{
73 for _, t := range cfg.Transactions {
74 tx, err := t.create(generator)
79 tx.TxData.Version = t.Version
80 tx.Tx = types.MapTx(&tx.TxData)
81 status, err := validation.ValidateTx(tx.Tx, block)
83 if result != t.Valid {
84 return fmt.Errorf("tx %s validate failed, expected: %t, have: %t", t.Describe, t.Valid, result)
90 if result && t.TxFee != status.BTMValue {
91 return fmt.Errorf("gas used dismatch, expected: %d, have: %d", t.TxFee, status.BTMValue)
97 type ttTransaction struct {
99 Describe string `json:"describe"`
100 Version uint64 `json:"version"`
101 Valid bool `json:"valid"`
102 TxFee uint64 `json:"tx_fee"`
105 // UnmarshalJSON unmarshal transaction with default version 1
106 func (t *ttTransaction) UnmarshalJSON(data []byte) error {
107 type typeAlias ttTransaction
112 err := json.Unmarshal(data, tx)
116 *t = ttTransaction(*tx)
120 func (t *ttTransaction) create(g *TxGenerator) (*types.Tx, error) {
122 for _, input := range t.Inputs {
124 case "spend_account":
125 utxo, err := g.mockUtxo(input.AccountAlias, input.AssetAlias, input.Amount)
129 if err := g.AddTxInputFromUtxo(utxo, input.AccountAlias); err != nil {
133 _, err := g.createAsset(input.AccountAlias, input.AssetAlias)
137 if err := g.AddIssuanceInput(input.AssetAlias, input.Amount); err != nil {
143 for _, output := range t.Outputs {
146 if err := g.AddTxOutput(output.AccountAlias, output.AssetAlias, output.Amount); err != nil {
150 if err := g.AddRetirement(output.AssetAlias, output.Amount); err != nil {
155 return g.Sign(t.Passwords)
158 func TestTx(t *testing.T) {
159 walk(t, txTestDir, func(t *testing.T, name string, test *TxTestConfig) {
160 if err := test.Run(); err != nil {
166 func TestCoinbaseMature(t *testing.T) {
167 db := dbm.NewDB("test_coinbase_mature_db", "leveldb", "test_coinbase_mature_db")
168 defer os.RemoveAll("test_coinbase_mature_db")
169 chain, _, _, _ := MockChain(db)
171 defaultCtrlProg := []byte{byte(vm.OP_TRUE)}
172 if err := AppendBlocks(chain, 1); err != nil {
176 height := chain.BestBlockHeight()
177 block, err := chain.GetBlockByHeight(height)
182 tx, err := CreateTxFromTx(block.Transactions[0], 0, 1000000000, defaultCtrlProg)
187 txs := []*types.Tx{tx}
188 matureHeight := chain.BestBlockHeight() + consensus.CoinbasePendingBlockNumber
189 currentHeight := chain.BestBlockHeight()
190 for h := currentHeight + 1; h < matureHeight; h++ {
191 block, err := NewBlock(chain, txs, defaultCtrlProg)
195 if _, err = chain.ProcessBlock(block); err == nil {
196 t.Fatal("spent immature coinbase output success")
198 block, err = NewBlock(chain, nil, defaultCtrlProg)
202 if _, err := chain.ProcessBlock(block); err != nil {
207 block, err = NewBlock(chain, txs, defaultCtrlProg)
211 if _, err = chain.ProcessBlock(block); err != nil {
212 t.Fatalf("spent mature coinbase output failed: %s", err)
216 func TestCoinbaseTx(t *testing.T) {
217 db := dbm.NewDB("test_coinbase_tx_db", "leveldb", "test_coinbase_tx_db")
218 defer os.RemoveAll("test_coinbase_tx_db")
219 chain, _, _, _ := MockChain(db)
221 defaultCtrlProg := []byte{byte(vm.OP_TRUE)}
222 if err := AppendBlocks(chain, 1); err != nil {
226 block, err := chain.GetBlockByHeight(chain.BestBlockHeight())
231 tx, err := CreateTxFromTx(block.Transactions[0], 0, 1000000000, defaultCtrlProg)
236 block, err = NewBlock(chain, []*types.Tx{tx}, defaultCtrlProg)
241 txFees := []uint64{100000, 5000000000000}
242 for _, txFee := range txFees {
243 coinbaseTx, err := CreateCoinbaseTx(defaultCtrlProg, block.Height, txFee)
248 if err := ReplaceCoinbase(block, coinbaseTx); err != nil {
252 if _, err = chain.ProcessBlock(block); err == nil {
253 t.Fatalf("invalid coinbase tx validate success")