-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
243 lines (220 loc) · 6.43 KB
/
types.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package testutil
import (
"crypto/rand"
"math/big"
"time"
"github.com/libp2p/go-libp2p/core/crypto"
abciconv "github.com/dymensionxyz/dymint/conv/abci"
"github.com/dymensionxyz/dymint/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
version "github.com/tendermint/tendermint/proto/tendermint/version"
tmtypes "github.com/tendermint/tendermint/types"
)
const (
// DefaultBatchSize is the default batch size for testing
DefaultBatchSize = 5
// BlockVersion is the default block version for testing
BlockVersion = 1
// AppVersion is the default app version for testing
AppVersion = 2
)
func createRandomHashes() [][32]byte {
h := [][32]byte{}
for i := 0; i < 8; i++ {
var h1 [32]byte
_, err := rand.Read(h1[:])
if err != nil {
panic(err)
}
h = append(h, h1)
}
return h
}
func GetRandomTx() types.Tx {
n, _ := rand.Int(rand.Reader, big.NewInt(100))
size := int(n.Int64()) + 100
return types.Tx(GetRandomBytes(size))
}
func GetRandomBytes(n int) []byte {
data := make([]byte, n)
_, _ = rand.Read(data)
return data
}
// GenerateBlocks generates random blocks.
func generateBlock(height uint64) *types.Block {
h := createRandomHashes()
block := &types.Block{
Header: types.Header{
Version: types.Version{
Block: BlockVersion,
App: AppVersion,
},
NamespaceID: [8]byte{0, 1, 2, 3, 4, 5, 6, 7},
Height: height,
Time: 4567,
LastHeaderHash: h[0],
LastCommitHash: h[1],
DataHash: h[2],
ConsensusHash: h[3],
// AppHash: h[4],
AppHash: [32]byte{},
LastResultsHash: getEmptyLastResultsHash(),
ProposerAddress: []byte{4, 3, 2, 1},
AggregatorsHash: h[6],
},
Data: types.Data{
Txs: nil,
IntermediateStateRoots: types.IntermediateStateRoots{RawRootsList: [][]byte{{0x1}}},
Evidence: types.EvidenceData{Evidence: nil},
},
LastCommit: types.Commit{
Height: 8,
HeaderHash: h[7],
Signatures: []types.Signature{},
},
}
return block
}
func GenerateBlocksWithTxs(startHeight uint64, num uint64, proposerKey crypto.PrivKey, nTxs int) ([]*types.Block, error) {
blocks := make([]*types.Block, num)
for i := uint64(0); i < num; i++ {
block := generateBlock(i + startHeight)
block.Data = types.Data{
Txs: make(types.Txs, nTxs),
IntermediateStateRoots: types.IntermediateStateRoots{
RawRootsList: make([][]byte, nTxs),
},
}
for i := 0; i < nTxs; i++ {
block.Data.Txs[i] = GetRandomTx()
block.Data.IntermediateStateRoots.RawRootsList[i] = GetRandomBytes(32)
}
signature, err := generateSignature(proposerKey, &block.Header)
if err != nil {
return nil, err
}
block.LastCommit.Signatures = []types.Signature{signature}
blocks[i] = block
}
return blocks, nil
}
// GenerateBlocks generates random blocks.
func GenerateBlocks(startHeight uint64, num uint64, proposerKey crypto.PrivKey) ([]*types.Block, error) {
blocks := make([]*types.Block, num)
for i := uint64(0); i < num; i++ {
block := generateBlock(i + startHeight)
signature, err := generateSignature(proposerKey, &block.Header)
if err != nil {
return nil, err
}
block.LastCommit.Signatures = []types.Signature{signature}
blocks[i] = block
}
return blocks, nil
}
// GenerateCommits generates commits based on passed blocks.
func GenerateCommits(blocks []*types.Block, proposerKey crypto.PrivKey) ([]*types.Commit, error) {
commits := make([]*types.Commit, len(blocks))
for i, block := range blocks {
signature, err := generateSignature(proposerKey, &block.Header)
if err != nil {
return nil, err
}
commits[i] = &types.Commit{
Height: block.Header.Height,
HeaderHash: block.Header.Hash(),
Signatures: []types.Signature{signature},
}
}
return commits, nil
}
func generateSignature(proposerKey crypto.PrivKey, header *types.Header) ([]byte, error) {
abciHeaderPb := abciconv.ToABCIHeaderPB(header)
abciHeaderBytes, err := abciHeaderPb.Marshal()
if err != nil {
return nil, err
}
sign, err := proposerKey.Sign(abciHeaderBytes)
if err != nil {
return nil, err
}
return sign, nil
}
// GenerateBatch generates a batch out of random blocks
func GenerateBatch(startHeight uint64, endHeight uint64, proposerKey crypto.PrivKey) (*types.Batch, error) {
blocks, err := GenerateBlocks(startHeight, endHeight-startHeight+1, proposerKey)
if err != nil {
return nil, err
}
commits, err := GenerateCommits(blocks, proposerKey)
if err != nil {
return nil, err
}
batch := &types.Batch{
StartHeight: startHeight,
EndHeight: endHeight,
Blocks: blocks,
Commits: commits,
}
return batch, nil
}
// GenerateRandomValidatorSet generates random validator sets
func GenerateRandomValidatorSet() *tmtypes.ValidatorSet {
pubKey := ed25519.GenPrivKey().PubKey()
return &tmtypes.ValidatorSet{
Proposer: &tmtypes.Validator{PubKey: pubKey, Address: pubKey.Address()},
Validators: []*tmtypes.Validator{
{PubKey: pubKey, Address: pubKey.Address()},
},
}
}
// GenerateState generates an initial state for testing.
func GenerateState(initialHeight int64, lastBlockHeight int64) types.State {
return types.State{
ChainID: "test-chain",
InitialHeight: initialHeight,
AppHash: [32]byte{},
LastResultsHash: getEmptyLastResultsHash(),
Version: tmstate.Version{
Consensus: version.Consensus{
Block: BlockVersion,
App: AppVersion,
},
},
LastBlockHeight: lastBlockHeight,
LastValidators: GenerateRandomValidatorSet(),
Validators: GenerateRandomValidatorSet(),
NextValidators: GenerateRandomValidatorSet(),
}
}
// GenerateGenesis generates a genesis for testing.
func GenerateGenesis(initialHeight int64) *tmtypes.GenesisDoc {
return &tmtypes.GenesisDoc{
ChainID: "test-chain",
InitialHeight: initialHeight,
ConsensusParams: &tmproto.ConsensusParams{
Block: tmproto.BlockParams{
MaxBytes: 1024,
MaxGas: -1,
TimeIotaMs: 1000,
},
Evidence: tmproto.EvidenceParams{
MaxAgeNumBlocks: 100,
MaxAgeDuration: time.Second,
},
Validator: tmproto.ValidatorParams{
PubKeyTypes: []string{"ed25519"},
},
Version: tmproto.VersionParams{
AppVersion: AppVersion,
},
},
}
}
func getEmptyLastResultsHash() [32]byte {
lastResults := []*abci.ResponseDeliverTx{}
return *(*[32]byte)(tmtypes.NewResults(lastResults).Hash())
}