-
Notifications
You must be signed in to change notification settings - Fork 9
/
types.go
103 lines (87 loc) · 2.61 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
package blockchain
import (
"errors"
"fmt"
"math/big"
"github.com/filefilego/filefilego/common/hexutil"
"google.golang.org/protobuf/proto"
)
// AddressState represents an address's state.
type AddressState struct {
Balance []byte
Nounce []byte
}
// FileMetadata represents channel file metadata.
type FileMetadata struct {
Name string
Hash string
Size uint64
Path string
}
// GetBalance returns the balance as big int.
func (a *AddressState) GetBalance() (*big.Int, error) {
if len(a.Balance) == 0 {
return nil, errors.New("balance is empty")
}
return big.NewInt(0).SetBytes(a.Balance), nil
}
// SetBalance sets the balance to byte array.
func (a *AddressState) SetBalance(amount *big.Int) {
zeroBig := big.NewInt(0)
if zeroBig.Cmp(amount) == 0 {
a.Balance = []byte{0}
} else {
a.Balance = amount.Bytes()
}
}
// GetNounce returns the nounce as uint64.
func (a *AddressState) GetNounce() (uint64, error) {
if len(a.Nounce) == 0 {
return 0, errors.New("nounce is empty")
}
return hexutil.DecodeBigFromBytesToUint64(a.Nounce), nil
}
// SetNounce sets the balance to byte array.
func (a *AddressState) SetNounce(number uint64) {
if number == 0 {
a.Nounce = []byte{0}
} else {
a.Nounce = big.NewInt(0).SetUint64(number).Bytes()
}
}
// ToAddressStateProto returns the proto representation of a state.
func ToAddressStateProto(state AddressState) *AddressStateProto {
addrStateProto := &AddressStateProto{
Balance: make([]byte, len(state.Balance)),
Nounce: make([]byte, len(state.Nounce)),
}
copy(addrStateProto.Balance, state.Balance)
copy(addrStateProto.Nounce, state.Nounce)
return addrStateProto
}
// AddressStateProtoToAddressState returns the AddressState from a AddressStateProto.
func AddressStateProtoToAddressState(state *AddressStateProto) AddressState {
addrState := AddressState{
Balance: make([]byte, len(state.Balance)),
Nounce: make([]byte, len(state.Nounce)),
}
copy(addrState.Balance, state.Balance)
copy(addrState.Nounce, state.Nounce)
return addrState
}
// MarshalAddressStateProto serializes an address state to a protobuf message.
func MarshalAddressStateProto(b *AddressStateProto) ([]byte, error) {
addrData, err := proto.Marshal(b)
if err != nil {
return nil, fmt.Errorf("failed to marshal address state: %w", err)
}
return addrData, nil
}
// UnmarshalProtoBlock unserializes a byte array to a protobuf address state.
func UnmarshalAddressStateProto(data []byte) (*AddressStateProto, error) {
addr := AddressStateProto{}
if err := proto.Unmarshal(data, &addr); err != nil {
return nil, fmt.Errorf("failed to unmarshal an address state: %w", err)
}
return &addr, nil
}