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
/
utils.go
160 lines (134 loc) · 2.81 KB
/
utils.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
// private constants strings
var (
null = []byte(`null`)
)
const CONTRACT = "contract"
// BlockID is an interface to abstract different kinds of block addressing modes
type BlockID interface {
fmt.Stringer
Int64() int64
}
// BlockLevel is a block addressing mode that uses the blocks sequence number a.k.a level
type BlockLevel int64
func (b BlockLevel) String() string {
return strconv.FormatInt(int64(b), 10)
}
func (b BlockLevel) Int64() int64 {
return int64(b)
}
// BlockAlias is a block addressing mode that uses a constant string
type BlockAlias string
const (
Genesis BlockAlias = "genesis"
Head BlockAlias = "head"
)
func (b BlockAlias) String() string {
return string(b)
}
func (b BlockAlias) Int64() int64 {
if b == Genesis {
return 0
}
return -1
}
// BlockOffset is a block addressing mode that uses relative addressing from a given
// base block.
type BlockOffset struct {
Base BlockID
Offset int64
}
func NewBlockOffset(id BlockID, n int64) BlockOffset {
return BlockOffset{
Base: id,
Offset: n,
}
}
func (o BlockOffset) String() string {
ref := o.Base.String()
if o.Offset > 0 {
ref += "+" + strconv.FormatInt(o.Offset, 10)
} else if o.Offset < 0 {
ref += "~" + strconv.FormatInt(-o.Offset, 10)
}
return ref
}
func (b BlockOffset) Int64() int64 {
base := b.Base.Int64()
if base >= 0 {
return base + b.Offset
}
return -1
}
func unmarshalMultiTypeJSONArray(data []byte, vals ...interface{}) (err error) {
dec := json.NewDecoder(bytes.NewBuffer(data))
// read open bracket
_, err = dec.Token()
if err != nil {
return
}
// while the array contains values
var i int
for dec.More() {
if i >= len(vals) {
return fmt.Errorf("short JSON data")
}
err = dec.Decode(vals[i])
if err != nil {
return
}
i++
}
// read closing bracket
_, err = dec.Token()
return
}
func marshalMultiTypeJSONArray(vals ...interface{}) (data []byte, err error) {
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
buf.WriteByte('[')
for i, v := range vals {
if i > 0 {
buf.WriteByte(',')
}
err = enc.Encode(v)
if err != nil {
return
}
}
buf.WriteByte(']')
data = buf.Bytes()
return
}
type Int64orString int64
func (i *Int64orString) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
if data[0] == '"' {
data = data[1 : len(data)-1]
}
num, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return err
}
*i = Int64orString(num)
return nil
}
func (i Int64orString) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(i.String())), nil
}
func (i Int64orString) Int64() int64 {
return int64(i)
}
func (i Int64orString) String() string {
return strconv.FormatInt(int64(i), 10)
}