This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
block.go
393 lines (351 loc) · 14.1 KB
/
block.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"time"
"blockwatch.cc/tzgo/tezos"
)
// Block holds information about a Tezos block
type Block struct {
Protocol tezos.ProtocolHash `json:"protocol"`
ChainId tezos.ChainIdHash `json:"chain_id"`
Hash tezos.BlockHash `json:"hash"`
Header BlockHeader `json:"header"`
Metadata BlockMetadata `json:"metadata"`
Operations [][]*Operation `json:"operations"`
}
func (b Block) GetLevel() int64 {
return b.Header.Level
}
func (b Block) GetTimestamp() time.Time {
return b.Header.Timestamp
}
func (b Block) GetVersion() int {
return b.Header.Proto
}
func (b Block) GetCycle() int64 {
if b.Metadata.LevelInfo != nil {
return b.Metadata.LevelInfo.Cycle
}
if b.Metadata.Level != nil {
return b.Metadata.Level.Cycle
}
return 0
}
func (b Block) GetLevelInfo() LevelInfo {
if b.Metadata.LevelInfo != nil {
return *b.Metadata.LevelInfo
}
if b.Metadata.Level != nil {
return *b.Metadata.Level
}
return LevelInfo{}
}
// only works for mainnet when before Edo or for all nets after Edo
// due to fixed constants used
func (b Block) GetVotingInfo() VotingPeriodInfo {
if b.Metadata.VotingPeriodInfo != nil {
return *b.Metadata.VotingPeriodInfo
}
if b.Metadata.Level != nil {
return VotingPeriodInfo{
Position: b.Metadata.Level.VotingPeriodPosition,
Remaining: 32768 - b.Metadata.Level.VotingPeriodPosition,
VotingPeriod: VotingPeriod{
Index: b.Metadata.Level.VotingPeriod,
Kind: *b.Metadata.VotingPeriodKind,
StartPosition: b.Metadata.Level.VotingPeriod * 32768,
},
}
}
return VotingPeriodInfo{}
}
func (b Block) GetVotingPeriodKind() tezos.VotingPeriodKind {
if b.Metadata.VotingPeriodInfo != nil {
return b.Metadata.VotingPeriodInfo.VotingPeriod.Kind
}
if b.Metadata.VotingPeriodKind != nil {
return *b.Metadata.VotingPeriodKind
}
return tezos.VotingPeriodInvalid
}
func (b Block) GetVotingPeriod() int64 {
if b.Metadata.VotingPeriodInfo != nil {
return b.Metadata.VotingPeriodInfo.VotingPeriod.Index
}
if b.Metadata.Level != nil {
return b.Metadata.Level.VotingPeriod
}
return 0
}
func (b Block) IsProtocolUpgrade() bool {
return !b.Metadata.Protocol.Equal(b.Metadata.NextProtocol)
}
// InvalidBlock represents invalid block hash along with the errors that led to it being declared invalid
type InvalidBlock struct {
Block tezos.BlockHash `json:"block"`
Level int64 `json:"level"`
Error Errors `json:"error"`
}
// BlockHeader is a part of the Tezos block data
type BlockHeader struct {
Level int64 `json:"level"`
Proto int `json:"proto"`
Predecessor tezos.BlockHash `json:"predecessor"`
Timestamp time.Time `json:"timestamp"`
ValidationPass int `json:"validation_pass"`
OperationsHash tezos.OpListListHash `json:"operations_hash"`
Fitness []tezos.HexBytes `json:"fitness"`
Context tezos.ContextHash `json:"context"`
PayloadHash tezos.PayloadHash `json:"payload_hash"`
PayloadRound int `json:"payload_round"`
Priority int `json:"priority"`
ProofOfWorkNonce tezos.HexBytes `json:"proof_of_work_nonce"`
SeedNonceHash *tezos.NonceHash `json:"seed_nonce_hash"`
Signature tezos.Signature `json:"signature"`
Content *BlockContent `json:"content,omitempty"`
LiquidityBakingEscapeVote bool `json:"liquidity_baking_escape_vote"`
LiquidityBakingToggleVote tezos.LbVote `json:"liquidity_baking_toggle_vote"`
// only present when header is fetched explicitly
Hash tezos.BlockHash `json:"hash"`
Protocol tezos.ProtocolHash `json:"protocol"`
ChainId tezos.ChainIdHash `json:"chain_id"`
}
func (h BlockHeader) LbVote() tezos.LbVote {
if h.LiquidityBakingToggleVote.IsValid() {
return h.LiquidityBakingToggleVote
}
if h.LiquidityBakingEscapeVote {
return tezos.LbVoteOn
}
return tezos.LbVoteOff
}
// ProtocolData exports protocol-specific extra header fields as binary encoded data.
// Used to produce compliant block monitor data streams.
//
// tezos-codec describe 012-Psithaca.block_header.protocol_data binary schema
// +---------------------------------------+----------+-------------------------------------+
// | Name | Size | Contents |
// +=======================================+==========+=====================================+
// | payload_hash | 32 bytes | bytes |
// +---------------------------------------+----------+-------------------------------------+
// | payload_round | 4 bytes | signed 32-bit integer |
// +---------------------------------------+----------+-------------------------------------+
// | proof_of_work_nonce | 8 bytes | bytes |
// +---------------------------------------+----------+-------------------------------------+
// | ? presence of field "seed_nonce_hash" | 1 byte | boolean (0 for false, 255 for true) |
// +---------------------------------------+----------+-------------------------------------+
// | seed_nonce_hash | 32 bytes | bytes |
// +---------------------------------------+----------+-------------------------------------+
// | liquidity_baking_escape_vote | 1 byte | boolean (0 for false, 255 for true) |
// +---------------------------------------+----------+-------------------------------------+
// | signature | 64 bytes | bytes |
// +---------------------------------------+----------+-------------------------------------+
// Jakarta
// +---------------------------------------+----------+-------------------------------------+
// | liquidity_baking_toggle_vote | 1 byte | signed 8-bit integer |
// +---------------------------------------+----------+-------------------------------------+
func (h BlockHeader) ProtocolData() []byte {
buf := bytes.NewBuffer(nil)
buf.Write(h.PayloadHash.Bytes())
binary.Write(buf, binary.BigEndian, uint32(h.PayloadRound))
buf.Write(h.ProofOfWorkNonce)
if h.SeedNonceHash != nil {
buf.WriteByte(0xff)
buf.Write(h.SeedNonceHash.Bytes())
} else {
buf.WriteByte(0x0)
}
switch {
case h.LiquidityBakingToggleVote.IsValid():
buf.WriteByte(h.LiquidityBakingToggleVote.Tag())
case h.LiquidityBakingEscapeVote:
buf.WriteByte(0xff)
default:
buf.WriteByte(0x0)
}
if h.Signature.IsValid() {
buf.Write(h.Signature.Data) // raw, no tag!
}
return buf.Bytes()
}
// BlockContent is part of block 1 header that seeds the initial context
type BlockContent struct {
Command string `json:"command"`
Protocol tezos.ProtocolHash `json:"hash"`
Fitness []tezos.HexBytes `json:"fitness"`
Parameters *GenesisData `json:"protocol_parameters"`
}
// OperationListLength is a part of the BlockMetadata
type OperationListLength struct {
MaxSize int `json:"max_size"`
MaxOp int `json:"max_op"`
}
// BlockLevel is a part of BlockMetadata
type LevelInfo struct {
Level int64 `json:"level"`
LevelPosition int64 `json:"level_position"`
Cycle int64 `json:"cycle"`
CyclePosition int64 `json:"cycle_position"`
ExpectedCommitment bool `json:"expected_commitment"`
// <v008
VotingPeriod int64 `json:"voting_period"`
VotingPeriodPosition int64 `json:"voting_period_position"`
}
type VotingPeriod struct {
Index int64 `json:"index"`
Kind tezos.VotingPeriodKind `json:"kind"`
StartPosition int64 `json:"start_position"`
}
type VotingPeriodInfo struct {
Position int64 `json:"position"`
Remaining int64 `json:"remaining"`
VotingPeriod VotingPeriod `json:"voting_period"`
}
// BlockMetadata is a part of the Tezos block data
type BlockMetadata struct {
Protocol tezos.ProtocolHash `json:"protocol"`
NextProtocol tezos.ProtocolHash `json:"next_protocol"`
MaxOperationsTTL int `json:"max_operations_ttl"`
MaxOperationDataLength int `json:"max_operation_data_length"`
MaxBlockHeaderLength int `json:"max_block_header_length"`
MaxOperationListLength []*OperationListLength `json:"max_operation_list_length"`
Baker tezos.Address `json:"baker"`
Proposer tezos.Address `json:"proposer"`
NonceHash tezos.NonceHash `json:"nonce_hash"`
ConsumedGas int64 `json:"consumed_gas,string"`
Deactivated []tezos.Address `json:"deactivated"`
BalanceUpdates BalanceUpdates `json:"balance_updates"`
// <v008
Level *LevelInfo `json:"level"`
VotingPeriodKind *tezos.VotingPeriodKind `json:"voting_period_kind"`
// v008+
LevelInfo *LevelInfo `json:"level_info"`
VotingPeriodInfo *VotingPeriodInfo `json:"voting_period_info"`
// v010+
ImplicitOperationsResults []ImplicitResult `json:"implicit_operations_results"`
LiquidityBakingEscapeEma int64 `json:"liquidity_baking_escape_ema"`
}
func (m *BlockMetadata) GetLevel() int64 {
if m.LevelInfo != nil {
return m.LevelInfo.Level
}
return m.Level.Level
}
// GetBlock returns information about a Tezos block
// https://tezos.gitlab.io/mainnet/api/rpc.html#get-block-id
func (c *Client) GetBlock(ctx context.Context, id BlockID) (*Block, error) {
var block Block
u := fmt.Sprintf("chains/main/blocks/%s?metadata=always", id)
if err := c.Get(ctx, u, &block); err != nil {
return nil, err
}
return &block, nil
}
// GetBlockHeight returns information about a Tezos block
// https://tezos.gitlab.io/mainnet/api/rpc.html#get-block-id
func (c *Client) GetBlockHeight(ctx context.Context, height int64) (*Block, error) {
return c.GetBlock(ctx, BlockLevel(height))
}
// GetTips returns hashes of the current chain tip blocks, first in the array is the
// current main chain.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetTips(ctx context.Context, depth int, head tezos.BlockHash) ([][]tezos.BlockHash, error) {
if depth == 0 {
depth = 1
}
tips := make([][]tezos.BlockHash, 0, 10)
var u string
if head.IsValid() {
u = fmt.Sprintf("chains/main/blocks?length=%d&head=%s", depth, head)
} else {
u = fmt.Sprintf("chains/main/blocks?length=%d", depth)
}
if err := c.Get(ctx, u, &tips); err != nil {
return nil, err
}
return tips, nil
}
// GetHeadBlock returns the chain's head block.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetHeadBlock(ctx context.Context) (*Block, error) {
return c.GetBlock(ctx, Head)
}
// GetGenesisBlock returns main chain genesis block.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetGenesisBlock(ctx context.Context) (*Block, error) {
return c.GetBlock(ctx, Genesis)
}
// GetTipHeader returns the head block's header.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetTipHeader(ctx context.Context) (*BlockHeader, error) {
var head BlockHeader
u := "chains/main/blocks/head/header"
if err := c.Get(ctx, u, &head); err != nil {
return nil, err
}
return &head, nil
}
// GetBlockHeader returns a block header.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetBlockHeader(ctx context.Context, id BlockID) (*BlockHeader, error) {
var head BlockHeader
u := fmt.Sprintf("chains/main/blocks/%s/header", id)
if err := c.Get(ctx, u, &head); err != nil {
return nil, err
}
return &head, nil
}
// GetBlockMetadata returns a block metadata.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetBlockMetadata(ctx context.Context, id BlockID) (*BlockMetadata, error) {
var meta BlockMetadata
u := fmt.Sprintf("chains/main/blocks/%s/metadata?metadata=always", id)
if err := c.Get(ctx, u, &meta); err != nil {
return nil, err
}
return &meta, nil
}
// GetBlockHash returns the main chain's block header.
// https://tezos.gitlab.io/mainnet/api/rpc.html#chains-chain-id-blocks
func (c *Client) GetBlockHash(ctx context.Context, id BlockID) (hash tezos.BlockHash, err error) {
u := fmt.Sprintf("chains/main/blocks/%s/hash", id)
err = c.Get(ctx, u, &hash)
return
}
// GetBlockPredHashes returns count parent blocks before block with given hash.
// https://tezos.gitlab.io/mainnet/api/rpc.html#get-chains-chain-id-blocks
func (c *Client) GetBlockPredHashes(ctx context.Context, hash tezos.BlockHash, count int) ([]tezos.BlockHash, error) {
if count <= 0 {
count = 1
}
blockIds := make([][]tezos.BlockHash, 0, count)
u := fmt.Sprintf("chains/main/blocks?length=%d&head=%s", count, hash)
if err := c.Get(ctx, u, &blockIds); err != nil {
return nil, err
}
return blockIds[0], nil
}
// GetInvalidBlocks lists blocks that have been declared invalid along with the errors that led to them being declared invalid.
// https://tezos.gitlab.io/mainnet/api/rpc.html#get-chains-chain-id-invalid-blocks
func (c *Client) GetInvalidBlocks(ctx context.Context) ([]*InvalidBlock, error) {
var invalidBlocks []*InvalidBlock
if err := c.Get(ctx, "chains/main/invalid_blocks", &invalidBlocks); err != nil {
return nil, err
}
return invalidBlocks, nil
}
// GetInvalidBlock returns a single invalid block with the errors that led to it being declared invalid.
// https://tezos.gitlab.io/mainnet/api/rpc.html#get-chains-chain-id-invalid-blocks-block-hash
func (c *Client) GetInvalidBlock(ctx context.Context, blockID tezos.BlockHash) (*InvalidBlock, error) {
var invalidBlock InvalidBlock
u := fmt.Sprintf("chains/main/invalid_blocks/%s", blockID)
if err := c.Get(ctx, u, &invalidBlock); err != nil {
return nil, err
}
return &invalidBlock, nil
}