-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
151 lines (133 loc) · 3.97 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package tests
import (
"crypto/ecdsa"
"github.com/noah-blockchain/noah-go-node/cmd/utils"
"github.com/noah-blockchain/noah-go-node/config"
"github.com/noah-blockchain/noah-go-node/core/noah"
"github.com/noah-blockchain/noah-go-node/core/transaction"
"github.com/noah-blockchain/noah-go-node/core/types"
"github.com/noah-blockchain/noah-go-node/crypto"
"github.com/noah-blockchain/noah-go-node/rlp"
"github.com/tendermint/go-amino"
tmTypes "github.com/tendermint/tendermint/abci/types"
"os"
"path/filepath"
"time"
)
// CreateApp creates and returns new Blockchain instance
// Recreates $HOME/.noah_test dir
func CreateApp(state types.AppState) *noah.Blockchain {
utils.NoahHome = os.ExpandEnv(filepath.Join("$HOME", ".noah_test"))
_ = os.RemoveAll(utils.NoahHome)
jsonState, err := amino.MarshalJSON(state)
if err != nil {
panic(err)
}
cfg := config.GetConfig()
app := noah.NewNoahBlockchain(cfg)
app.InitChain(tmTypes.RequestInitChain{
Time: time.Now(),
ChainId: "test",
Validators: []tmTypes.ValidatorUpdate{
{
PubKey: tmTypes.PubKey{},
Power: 1,
},
},
AppStateBytes: jsonState,
})
return app
}
// SendCommit sends Commit message to given Blockchain instance
func SendCommit(app *noah.Blockchain) tmTypes.ResponseCommit {
return app.Commit()
}
// SendBeginBlock sends BeginBlock message to given Blockchain instance
func SendBeginBlock(app *noah.Blockchain) tmTypes.ResponseBeginBlock {
return app.BeginBlock(tmTypes.RequestBeginBlock{
Hash: nil,
Header: tmTypes.Header{
Version: tmTypes.Version{},
ChainID: "",
Height: 1,
Time: time.Time{},
LastBlockId: tmTypes.BlockID{},
LastCommitHash: nil,
DataHash: nil,
ValidatorsHash: nil,
NextValidatorsHash: nil,
ConsensusHash: nil,
AppHash: nil,
LastResultsHash: nil,
EvidenceHash: nil,
ProposerAddress: nil,
},
LastCommitInfo: tmTypes.LastCommitInfo{
Round: 0,
Votes: nil,
},
ByzantineValidators: nil,
})
}
// SendEndBlock sends EndBlock message to given Blockchain instance
func SendEndBlock(app *noah.Blockchain) tmTypes.ResponseEndBlock {
return app.EndBlock(tmTypes.RequestEndBlock{
Height: 0,
})
}
// CreateTx composes and returns Tx with given params.
// Nonce, chain id, gas price, gas coin and signature type fields are auto-filled.
func CreateTx(app *noah.Blockchain, address types.Address, txType transaction.TxType, data interface{}) transaction.Transaction {
nonce := app.CurrentState().Accounts().GetNonce(address) + 1
bData, err := rlp.EncodeToBytes(data)
if err != nil {
panic(err)
}
tx := transaction.Transaction{
Nonce: nonce,
ChainID: types.CurrentChainID,
GasPrice: 1,
GasCoin: types.GetBaseCoinID(),
Type: txType,
Data: bData,
SignatureType: transaction.SigTypeSingle,
}
return tx
}
// SendTx sends DeliverTx message to given Blockchain instance
func SendTx(app *noah.Blockchain, bytes []byte) tmTypes.ResponseDeliverTx {
return app.DeliverTx(tmTypes.RequestDeliverTx{
Tx: bytes,
})
}
// SignTx returns bytes of signed with given pk transaction
func SignTx(pk *ecdsa.PrivateKey, tx transaction.Transaction) []byte {
err := tx.Sign(pk)
if err != nil {
panic(err)
}
b, _ := rlp.EncodeToBytes(tx)
return b
}
// CreateAddress returns random address and corresponding private key
func CreateAddress() (types.Address, *ecdsa.PrivateKey) {
pk, _ := crypto.GenerateKey()
return crypto.PubkeyToAddress(pk.PublicKey), pk
}
// DefaultAppState returns new AppState with some predefined values
func DefaultAppState() types.AppState {
return types.AppState{
Note: "",
StartHeight: 1,
Validators: nil,
Candidates: nil,
BlockListCandidates: nil,
Accounts: nil,
Coins: nil,
FrozenFunds: nil,
HaltBlocks: nil,
UsedChecks: nil,
MaxGas: 0,
TotalSlashed: "0",
}
}