OSDN Git Service

Merge pull request #606 from Bytom/dev
[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         blockVersion = 1
24         assetVersion = 1
25         maxNonce     = ^uint64(0)
26 )
27
28 // MockTxPool mock transaction pool
29 func MockTxPool() *protocol.TxPool {
30         return protocol.NewTxPool()
31 }
32
33 // MockChain mock chain with genesis block
34 func MockChain(testDB dbm.DB) (*protocol.Chain, *leveldb.Store, *protocol.TxPool, error) {
35         store := leveldb.NewStore(testDB)
36         txPool := MockTxPool()
37         chain, err := protocol.NewChain(store, txPool)
38         return chain, store, txPool, err
39 }
40
41 // MockUTXO mock a utxo
42 func MockUTXO(controlProg *account.CtrlProgram) *account.UTXO {
43         utxo := &account.UTXO{}
44         utxo.OutputID = bc.Hash{V0: 1}
45         utxo.SourceID = bc.Hash{V0: 2}
46         utxo.AssetID = *consensus.BTMAssetID
47         utxo.Amount = 1000000000
48         utxo.SourcePos = 0
49         utxo.ControlProgram = controlProg.ControlProgram
50         utxo.AccountID = controlProg.AccountID
51         utxo.Address = controlProg.Address
52         utxo.ControlProgramIndex = controlProg.KeyIndex
53         return utxo
54 }
55
56 // MockTx mock a tx
57 func MockTx(utxo *account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
58         txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
59         if err != nil {
60                 return nil, nil, err
61         }
62
63         b := txbuilder.NewBuilder(time.Now())
64         b.AddInput(txInput, sigInst)
65         out := types.NewTxOutput(*consensus.BTMAssetID, 100, []byte{byte(vm.OP_FAIL)})
66         b.AddOutput(out)
67         return b.Build()
68 }
69
70 // MockSign sign a tx
71 func MockSign(tpl *txbuilder.Template, hsm *pseudohsm.HSM, password string) (bool, error) {
72         err := txbuilder.Sign(nil, tpl, nil, password, func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
73                 return hsm.XSign(xpub, path, data[:], password)
74         })
75         if err != nil {
76                 return false, err
77         }
78         return txbuilder.SignProgress(tpl), nil
79 }
80
81 // MockBlock mock a block
82 func MockBlock() *bc.Block {
83         return &bc.Block{
84                 BlockHeader: &bc.BlockHeader{Height: 1},
85         }
86 }