OSDN Git Service

return asset definition when get balances
[bytom/bytom.git] / test / util.go
1 package test
2
3 import (
4         "context"
5         "time"
6
7         dbm "github.com/tendermint/tmlibs/db"
8
9         "github.com/bytom/account"
10         "github.com/bytom/blockchain/pseudohsm"
11         "github.com/bytom/blockchain/txbuilder"
12         "github.com/bytom/consensus"
13         "github.com/bytom/crypto/ed25519/chainkd"
14         "github.com/bytom/database/leveldb"
15         "github.com/bytom/protocol"
16         "github.com/bytom/protocol/bc"
17         "github.com/bytom/protocol/bc/types"
18         "github.com/bytom/protocol/vm"
19 )
20
21 const (
22         vmVersion    = 1
23         assetVersion = 1
24 )
25
26 // MockTxPool mock transaction pool
27 func MockTxPool() *protocol.TxPool {
28         return protocol.NewTxPool()
29 }
30
31 // MockChain mock chain with genesis block
32 func MockChain(testDB dbm.DB) (*protocol.Chain, *leveldb.Store, *protocol.TxPool, error) {
33         store := leveldb.NewStore(testDB)
34         txPool := MockTxPool()
35         chain, err := protocol.NewChain(store, txPool)
36         return chain, store, txPool, err
37 }
38
39 // MockUTXO mock a utxo
40 func MockUTXO(controlProg *account.CtrlProgram) *account.UTXO {
41         utxo := &account.UTXO{}
42         utxo.OutputID = bc.Hash{V0: 1}
43         utxo.SourceID = bc.Hash{V0: 2}
44         utxo.AssetID = *consensus.BTMAssetID
45         utxo.Amount = 1000000000
46         utxo.SourcePos = 0
47         utxo.ControlProgram = controlProg.ControlProgram
48         utxo.AccountID = controlProg.AccountID
49         utxo.Address = controlProg.Address
50         utxo.ControlProgramIndex = controlProg.KeyIndex
51         return utxo
52 }
53
54 // MockTx mock a tx
55 func MockTx(utxo *account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
56         txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
57         if err != nil {
58                 return nil, nil, err
59         }
60
61         b := txbuilder.NewBuilder(time.Now())
62         b.AddInput(txInput, sigInst)
63         out := types.NewTxOutput(*consensus.BTMAssetID, 100, []byte{byte(vm.OP_FAIL)})
64         b.AddOutput(out)
65         return b.Build()
66 }
67
68 // MockSign sign a tx
69 func MockSign(tpl *txbuilder.Template, hsm *pseudohsm.HSM, password string) (bool, error) {
70         err := txbuilder.Sign(nil, tpl, password, func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
71                 return hsm.XSign(xpub, path, data[:], password)
72         })
73         if err != nil {
74                 return false, err
75         }
76         return txbuilder.SignProgress(tpl), nil
77 }
78
79 // MockBlock mock a block
80 func MockBlock() *bc.Block {
81         return &bc.Block{
82                 BlockHeader: &bc.BlockHeader{Height: 1},
83         }
84 }