FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ev-node/types/utils.go at main · evstack/ev-node · GitHub
evstack
/
ev-node
Public
Notifications
You must be signed in to change notification settings
Fork
281
Star
361
Code
Issues
4
Pull requests
3
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ev-node
/
types
/
utils.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
310 lines (270 loc) · 8.51 KB
Breadcrumbs
ev-node
/
types
/
utils.go
Copy path
File metadata and controls
310 lines (270 loc) · 8.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package
types
import
(
"context"
cryptoRand
"crypto/rand"
"math/rand"
"time"
"github.com/celestiaorg/go-header"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/pkg/signer"
"github.com/evstack/ev-node/pkg/signer/noop"
)
// DefaultSigningKeyType is the key type used by the sequencer signing key
const
DefaultSigningKeyType
=
"ed25519"
// BlockConfig carries all necessary state for block generation
type
BlockConfig
struct
{
Height
uint64
NTxs
int
PrivKey
crypto.
PrivKey
// Input and Output option
ProposerAddr
[]
byte
// Input option
}
// GetRandomBlock creates a block with a given height and number of transactions, intended for testing.
// It's tailored for simplicity, primarily used in test setups where additional outputs are not needed.
func
GetRandomBlock
(
height
uint64
,
nTxs
int
,
chainID
string
) (
*
SignedHeader
,
*
Data
) {
config
:=
BlockConfig
{
Height
:
height
,
NTxs
:
nTxs
,
}
// Assuming GenerateBlock modifies the context directly with the generated block and other needed data.
header
,
data
,
_
:=
GenerateRandomBlockCustom
(
&
config
,
chainID
)
return
header
,
data
}
// GenerateRandomBlockCustomWithAppHash returns a block with random data and the given height, transactions, privateKey, proposer address, and custom appHash.
func
GenerateRandomBlockCustomWithAppHash
(
config
*
BlockConfig
,
chainID
string
,
appHash
[]
byte
) (
*
SignedHeader
,
*
Data
, crypto.
PrivKey
) {
data
:=
getBlockDataWith
(
config
.
NTxs
)
dataHash
:=
data
.
DACommitment
()
if
config
.
PrivKey
==
nil
{
pk
,
_
,
err
:=
crypto
.
GenerateEd25519Key
(
cryptoRand
.
Reader
)
if
err
!=
nil
{
panic
(
err
)
}
config
.
PrivKey
=
pk
}
noopSigner
,
err
:=
noop
.
NewNoopSigner
(
config
.
PrivKey
)
if
err
!=
nil
{
panic
(
err
)
}
headerConfig
:=
HeaderConfig
{
Height
:
config
.
Height
,
DataHash
:
dataHash
,
AppHash
:
appHash
,
Signer
:
noopSigner
,
}
signedHeader
,
err
:=
GetRandomSignedHeaderCustom
(
context
.
Background
(),
&
headerConfig
,
chainID
)
if
err
!=
nil
{
panic
(
err
)
}
if
config
.
ProposerAddr
!=
nil
{
signedHeader
.
ProposerAddress
=
config
.
ProposerAddr
}
data
.
Metadata
=
&
Metadata
{
ChainID
:
chainID
,
Height
:
signedHeader
.
Height
(),
LastDataHash
:
nil
,
Time
:
uint64
(
signedHeader
.
Time
().
UnixNano
()),
}
return
signedHeader
,
data
,
config
.
PrivKey
}
// GenerateRandomBlockCustom returns a block with random data and the given height, transactions, privateKey and proposer address.
func
GenerateRandomBlockCustom
(
config
*
BlockConfig
,
chainID
string
) (
*
SignedHeader
,
*
Data
, crypto.
PrivKey
) {
// Use random bytes for appHash
appHash
:=
GetRandomBytes
(
32
)
return
GenerateRandomBlockCustomWithAppHash
(
config
,
chainID
,
appHash
)
}
// HeaderConfig carries all necessary state for header generation
type
HeaderConfig
struct
{
Height
uint64
DataHash
header.
Hash
AppHash
header.
Hash
Signer
signer.
Signer
}
// GetRandomHeader returns a header with random fields and current time
func
GetRandomHeader
(
chainID
string
,
appHash
[]
byte
)
Header
{
return
Header
{
BaseHeader
:
BaseHeader
{
Height
:
uint64
(
rand
.
Int63
()),
//nolint:gosec
Time
:
uint64
(
time
.
Now
().
UnixNano
()),
ChainID
:
chainID
,
},
Version
:
Version
{
Block
:
InitStateVersion
.
Block
,
App
:
InitStateVersion
.
App
,
},
LastHeaderHash
:
GetRandomBytes
(
32
),
DataHash
:
GetRandomBytes
(
32
),
AppHash
:
appHash
,
ProposerAddress
:
GetRandomBytes
(
32
),
ValidatorHash
:
GetRandomBytes
(
32
),
}
}
// GetRandomNextHeader returns a header with random data and height of +1 from
// the provided Header
func
GetRandomNextHeader
(
header
Header
,
chainID
string
)
Header
{
nextHeader
:=
GetRandomHeader
(
chainID
,
GetRandomBytes
(
32
))
nextHeader
.
BaseHeader
.
Height
=
header
.
Height
()
+
1
nextHeader
.
BaseHeader
.
Time
=
uint64
(
time
.
Now
().
Add
(
1
*
time
.
Second
).
UnixNano
())
nextHeader
.
LastHeaderHash
=
header
.
Hash
()
nextHeader
.
ProposerAddress
=
header
.
ProposerAddress
nextHeader
.
ValidatorHash
=
header
.
ValidatorHash
nextHeader
.
InvalidateHash
()
return
nextHeader
}
// GetRandomSignedHeader generates a signed header with random data and returns it.
func
GetRandomSignedHeader
(
chainID
string
) (
*
SignedHeader
, crypto.
PrivKey
,
error
) {
pk
,
_
,
err
:=
crypto
.
GenerateEd25519Key
(
cryptoRand
.
Reader
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
noopSigner
,
err
:=
noop
.
NewNoopSigner
(
pk
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
config
:=
HeaderConfig
{
Height
:
uint64
(
rand
.
Int63
()),
//nolint:gosec
DataHash
:
GetRandomBytes
(
32
),
AppHash
:
GetRandomBytes
(
32
),
Signer
:
noopSigner
,
}
signedHeader
,
err
:=
GetRandomSignedHeaderCustom
(
context
.
Background
(),
&
config
,
chainID
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
return
signedHeader
,
pk
,
nil
}
// GetRandomSignedHeaderCustom creates a signed header based on the provided HeaderConfig.
func
GetRandomSignedHeaderCustom
(
ctx
context.
Context
,
config
*
HeaderConfig
,
chainID
string
) (
*
SignedHeader
,
error
) {
pk
,
err
:=
config
.
Signer
.
GetPublic
()
if
err
!=
nil
{
return
nil
,
err
}
signer
,
err
:=
NewSigner
(
pk
)
if
err
!=
nil
{
return
nil
,
err
}
signedHeader
:=
&
SignedHeader
{
Header
:
GetRandomHeader
(
chainID
,
config
.
AppHash
),
Signer
:
signer
,
}
signedHeader
.
BaseHeader
.
Height
=
config
.
Height
signedHeader
.
DataHash
=
config
.
DataHash
signedHeader
.
ProposerAddress
=
signer
.
Address
signedHeader
.
ValidatorHash
=
signer
.
Address
signedHeader
.
BaseHeader
.
Time
=
uint64
(
time
.
Now
().
UnixNano
())
+
(
config
.
Height
)
*
10
b
,
err
:=
signedHeader
.
Header
.
MarshalBinary
()
if
err
!=
nil
{
return
nil
,
err
}
signature
,
err
:=
config
.
Signer
.
Sign
(
ctx
,
b
)
if
err
!=
nil
{
return
nil
,
err
}
signedHeader
.
Signature
=
signature
return
signedHeader
,
nil
}
// GetRandomNextSignedHeader returns a signed header with random data and height of +1 from
// the provided signed header
func
GetRandomNextSignedHeader
(
ctx
context.
Context
,
signedHeader
*
SignedHeader
,
signer
signer.
Signer
,
chainID
string
) (
*
SignedHeader
,
error
) {
newSignedHeader
:=
&
SignedHeader
{
Header
:
GetRandomNextHeader
(
signedHeader
.
Header
,
chainID
),
Signer
:
signedHeader
.
Signer
,
}
signature
,
err
:=
GetSignature
(
ctx
,
newSignedHeader
.
Header
,
signer
)
if
err
!=
nil
{
return
nil
,
err
}
newSignedHeader
.
Signature
=
signature
return
newSignedHeader
,
nil
}
// GetFirstSignedHeader creates a 1st signed header for a chain, given a valset and signing key.
func
GetFirstSignedHeader
(
ctx
context.
Context
,
signer
signer.
Signer
,
chainID
string
) (
*
SignedHeader
,
error
) {
pk
,
err
:=
signer
.
GetPublic
()
if
err
!=
nil
{
return
nil
,
err
}
sig
,
err
:=
NewSigner
(
pk
)
if
err
!=
nil
{
return
nil
,
err
}
addr
,
err
:=
signer
.
GetAddress
()
if
err
!=
nil
{
return
nil
,
err
}
header
:=
Header
{
BaseHeader
:
BaseHeader
{
Height
:
1
,
Time
:
uint64
(
time
.
Now
().
UnixNano
()),
ChainID
:
chainID
,
},
Version
:
Version
{
Block
:
InitStateVersion
.
Block
,
App
:
InitStateVersion
.
App
,
},
LastHeaderHash
:
GetRandomBytes
(
32
),
DataHash
:
GetRandomBytes
(
32
),
AppHash
:
make
([]
byte
,
32
),
ProposerAddress
:
addr
,
}
signedHeader
:=
SignedHeader
{
Header
:
header
,
Signer
:
sig
,
}
signature
,
err
:=
GetSignature
(
ctx
,
header
,
signer
)
if
err
!=
nil
{
return
nil
,
err
}
signedHeader
.
Signature
=
signature
return
&
signedHeader
,
nil
}
// GetGenesisWithPrivkey returns a genesis state and a private key
func
GetGenesisWithPrivkey
(
chainID
string
) (genesis.
Genesis
, crypto.
PrivKey
, crypto.
PubKey
) {
privKey
,
pubKey
,
err
:=
crypto
.
GenerateEd25519Key
(
nil
)
if
err
!=
nil
{
panic
(
err
)
}
signer
,
err
:=
NewSigner
(
privKey
.
GetPublic
())
if
err
!=
nil
{
panic
(
err
)
}
return
genesis
.
NewGenesis
(
chainID
,
1
,
time
.
Now
().
UTC
(),
signer
.
Address
,
),
privKey
,
pubKey
}
// GetRandomTx returns a tx with random data
func
GetRandomTx
()
Tx
{
size
:=
rand
.
Int
()
%
100
+
100
//nolint:gosec
return
Tx
(
GetRandomBytes
(
uint
(
size
)))
}
// GetRandomBytes returns a byte slice of random bytes of length n.
// It uses crypto/rand for cryptographically secure random number generation.
func
GetRandomBytes
(
n
uint
) []
byte
{
data
:=
make
([]
byte
,
n
)
if
_
,
err
:=
cryptoRand
.
Read
(
data
);
err
!=
nil
{
panic
(
err
)
}
return
data
}
// GetSignature returns a signature from the given private key over the given header
func
GetSignature
(
ctx
context.
Context
,
header
Header
,
signer
signer.
Signer
) (
Signature
,
error
) {
b
,
err
:=
header
.
MarshalBinary
()
if
err
!=
nil
{
return
nil
,
err
}
return
signer
.
Sign
(
ctx
,
b
)
}
func
getBlockDataWith
(
nTxs
int
)
*
Data
{
data
:=
&
Data
{
Txs
:
make
(
Txs
,
nTxs
),
}
for
i
:=
range
nTxs
{
data
.
Txs
[
i
]
=
GetRandomTx
()
}
// TODO(tzdybal): see https://github.com/evstack/ev-node/issues/143
if
nTxs
==
0
{
data
.
Txs
=
nil
}
return
data
}
Back
|
FazBrowse Home
|
New Git URL