forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withdrawfromsidechain.go
83 lines (68 loc) · 2.31 KB
/
withdrawfromsidechain.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
// 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"
"errors"
"io"
"github.com/elastos/Elastos.ELA/common"
)
const WithdrawFromSideChainVersion byte = 0x00
type WithdrawFromSideChain struct {
BlockHeight uint32
GenesisBlockAddress string
SideChainTransactionHashes []common.Uint256
}
func (t *WithdrawFromSideChain) Data(version byte) []byte {
buf := new(bytes.Buffer)
if err := t.Serialize(buf, version); err != nil {
return []byte{0}
}
return buf.Bytes()
}
func (t *WithdrawFromSideChain) Serialize(w io.Writer, version byte) error {
if err := common.WriteUint32(w, t.BlockHeight); err != nil {
return errors.New("[WithdrawFromSideChain], BlockHeight serialize failed.")
}
if err := common.WriteVarString(w, t.GenesisBlockAddress); err != nil {
return errors.New("[WithdrawFromSideChain], GenesisBlockAddress serialize failed.")
}
if err := common.WriteVarUint(w, uint64(len(t.SideChainTransactionHashes))); err != nil {
return errors.New("[WithdrawFromSideChain], SideChainTransactionHashes count serialize failed")
}
for _, hash := range t.SideChainTransactionHashes {
err := hash.Serialize(w)
if err != nil {
return errors.New("[WithdrawFromSideChain], SideChainTransactionHashes serialize failed")
}
}
return nil
}
func (t *WithdrawFromSideChain) Deserialize(r io.Reader, version byte) error {
height, err := common.ReadUint32(r)
if err != nil {
return errors.New("[WithdrawFromSideChain], BlockHeight deserialize failed.")
}
address, err := common.ReadVarString(r)
if err != nil {
return errors.New("[WithdrawFromSideChain], GenesisBlockAddress deserialize failed.")
}
count, err := common.ReadVarUint(r, 0)
if err != nil {
return errors.New("[WithdrawFromSideChain], SideChainTransactionHashes count deserialize failed")
}
t.SideChainTransactionHashes = make([]common.Uint256, 0)
for i := uint64(0); i < count; i++ {
var hash common.Uint256
err := hash.Deserialize(r)
if err != nil {
return errors.New("[WithdrawFromSideChain], SideChainTransactionHash deserialize failed.")
}
t.SideChainTransactionHashes = append(t.SideChainTransactionHashes, hash)
}
t.BlockHeight = height
t.GenesisBlockAddress = address
return nil
}