forked from NethermindEth/starknet.go
/
types_block.go
103 lines (84 loc) · 2.59 KB
/
types_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
package rpcv01
import (
"errors"
"fmt"
"strconv"
"github.com/6xiaowu9/caigo/types"
)
var ErrInvalidBlockID = errors.New("invalid blockid")
// BlockHashAndNumberOutput is a struct that is returned by BlockHashAndNumber.
type BlockHashAndNumberOutput struct {
BlockNumber uint64 `json:"block_number,omitempty"`
BlockHash string `json:"block_hash,omitempty"`
}
// BlockID is a struct that is used to choose between different
// search types.
type BlockID struct {
Number *uint64 `json:"block_number,omitempty"`
Hash *types.Hash `json:"block_hash,omitempty"`
Tag string `json:"block_tag,omitempty"`
}
func (b BlockID) MarshalJSON() ([]byte, error) {
if b.Tag == "pending" || b.Tag == "latest" {
return []byte(strconv.Quote(b.Tag)), nil
}
if b.Tag != "" {
return nil, ErrInvalidBlockID
}
if b.Number != nil {
return []byte(fmt.Sprintf(`{"block_number":%d}`, *b.Number)), nil
}
if b.Hash != nil {
return []byte(fmt.Sprintf(`{"block_hash":"%s"}`, (*b.Hash).Hex())), nil
}
return nil, ErrInvalidBlockID
}
type BlockStatus string
const (
BlockStatus_Pending BlockStatus = "PENDING"
BlockStatus_AcceptedOnL2 BlockStatus = "ACCEPTED_ON_L2"
BlockStatus_AcceptedOnL1 BlockStatus = "ACCEPTED_ON_L1"
BlockStatus_Rejected BlockStatus = "REJECTED"
)
func (bs *BlockStatus) UnmarshalJSON(data []byte) error {
unquoted, err := strconv.Unquote(string(data))
if err != nil {
return err
}
switch unquoted {
case "PENDING":
*bs = BlockStatus_Pending
case "ACCEPTED_ON_L2":
*bs = BlockStatus_AcceptedOnL2
case "ACCEPTED_ON_L1":
*bs = BlockStatus_AcceptedOnL1
case "REJECTED":
*bs = BlockStatus_Rejected
default:
return fmt.Errorf("unsupported status: %s", data)
}
return nil
}
func (bs BlockStatus) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(string(bs))), nil
}
type Block struct {
BlockHeader
Status BlockStatus `json:"status"`
// Transactions The hashes of the transactions included in this block
Transactions Transactions `json:"transactions"`
}
type BlockHeader struct {
// BlockHash The hash of this block
BlockHash types.Hash `json:"block_hash"`
// ParentHash The hash of this block's parent
ParentHash types.Hash `json:"parent_hash"`
// BlockNumber the block number (its height)
BlockNumber uint64 `json:"block_number"`
// NewRoot The new global state root
NewRoot string `json:"new_root"`
// Timestamp the time in which the block was created, encoded in Unix time
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress string `json:"sequencer_address"`
}