-
Notifications
You must be signed in to change notification settings - Fork 203
/
types.go
135 lines (115 loc) · 3.19 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
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
package nr
import (
"encoding/json"
"errors"
"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/neblet/pb"
)
const (
CacheSize = 128
)
// Error types
var (
ErrInvalidStartHeight = errors.New("invalid nr start height")
ErrInvalidEndHeight = errors.New("invalid nr end height")
ErrInvalidHeightInterval = errors.New("invalid nr height interval")
ErrNRNotFound = errors.New("nr not found")
ErrNRSummaryNotFound = errors.New("nr summary not found")
)
type Neblet interface {
Config() *nebletpb.Config
BlockChain() *core.BlockChain
}
type NRResults struct {
NrResults []string `json:"nr_results"`
}
// NRItem nr item
type NRItem struct {
Address string `json:"address"`
Median string `json:"median"`
Weight string `json:"weight"`
Score string `json:"score"`
}
// ToBytes serialize data
func (n *NRItem) ToBytes() ([]byte, error) {
return json.Marshal(n)
}
// FromBytes
func (n *NRItem) FromBytes(data []byte) error {
if err := json.Unmarshal(data, n); err != nil {
return err
}
return nil
}
// NRData nr data
type NRData struct {
StartHeight uint64 `json:"start_height,string"`
EndHeight uint64 `json:"end_height,string"`
Version uint64 `json:"version,string"`
Nrs []*NRItem `json:"nrs"`
Err string `json:"err"`
}
// ToBytes serialize data
func (n *NRData) ToBytes() ([]byte, error) {
return json.Marshal(n)
}
// FromBytes
func (n *NRData) FromBytes(data []byte) error {
if err := json.Unmarshal(data, n); err != nil {
return err
}
return nil
}
type NRSums struct {
NrSums []string `json:"nr_sums"`
}
type NRSummary struct {
StartHeight uint64 `json:"start_height,string"`
EndHeight uint64 `json:"end_height,string"`
Version uint64 `json:"version,string"`
Sum *NRSummaryData `json:"sum"`
Err string `json:"err"`
}
// ToBytes serialize data
func (n *NRSummary) ToBytes() ([]byte, error) {
return json.Marshal(n)
}
// FromBytes
func (n *NRSummary) FromBytes(data []byte) error {
if err := json.Unmarshal(data, n); err != nil {
return err
}
return nil
}
type NRSummaryData struct {
InOuts string `json:"in_outs"`
Score string `json:"score"`
}
// ToBytes serialize data
func (n *NRSummaryData) ToBytes() ([]byte, error) {
return json.Marshal(n)
}
// FromBytes
func (n *NRSummaryData) FromBytes(data []byte) error {
if err := json.Unmarshal(data, n); err != nil {
return err
}
return nil
}