forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inactivearbitrators.go
114 lines (92 loc) · 2.38 KB
/
inactivearbitrators.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
// Copyright (c) 2017-2019 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//
package payload
import (
"bytes"
"io"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/crypto"
)
const InactiveArbitratorsVersion byte = 0x00
type InactiveArbitrators struct {
Sponsor []byte
Arbitrators [][]byte
BlockHeight uint32
hash *common.Uint256
}
func (i *InactiveArbitrators) Type() IllegalDataType {
return InactiveArbitrator
}
func (i *InactiveArbitrators) GetBlockHeight() uint32 {
return i.BlockHeight
}
func (i *InactiveArbitrators) Data(version byte) []byte {
buf := new(bytes.Buffer)
if err := i.Serialize(buf, version); err != nil {
return []byte{0}
}
return buf.Bytes()
}
func (i *InactiveArbitrators) SerializeUnsigned(w io.Writer, version byte) error {
if err := common.WriteVarBytes(w, i.Sponsor); err != nil {
return err
}
if err := common.WriteUint32(w, i.BlockHeight); err != nil {
return err
}
return nil
}
func (i *InactiveArbitrators) Serialize(w io.Writer, version byte) error {
if err := i.SerializeUnsigned(w, version); err != nil {
return err
}
if err := common.WriteVarUint(w, uint64(len(i.Arbitrators))); err != nil {
return err
}
for _, v := range i.Arbitrators {
if err := common.WriteVarBytes(w, v); err != nil {
return err
}
}
return nil
}
func (i *InactiveArbitrators) DeserializeUnsigned(r io.Reader,
version byte) (err error) {
if i.Sponsor, err = common.ReadVarBytes(r, crypto.NegativeBigLength,
"public key"); err != nil {
return err
}
if i.BlockHeight, err = common.ReadUint32(r); err != nil {
return err
}
return err
}
func (i *InactiveArbitrators) Deserialize(r io.Reader,
version byte) (err error) {
if err = i.DeserializeUnsigned(r, version); err != nil {
return err
}
var count uint64
if count, err = common.ReadVarUint(r, 0); err != nil {
return err
}
i.Arbitrators = make([][]byte, count)
for u := uint64(0); u < count; u++ {
if i.Arbitrators[u], err = common.ReadVarBytes(r,
crypto.NegativeBigLength, "public key"); err != nil {
return err
}
}
return nil
}
func (i *InactiveArbitrators) Hash() common.Uint256 {
if i.hash == nil {
buf := new(bytes.Buffer)
i.SerializeUnsigned(buf, InactiveArbitratorsVersion)
hash := common.Uint256(common.Sha256D(buf.Bytes()))
i.hash = &hash
}
return *i.hash
}