OSDN Git Service

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