forked from alexbakker/gonano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pending.go
56 lines (43 loc) · 1.15 KB
/
pending.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
package store
import (
"bytes"
"encoding/binary"
"github.com/BananoCoin/gobanano/nano"
"github.com/BananoCoin/gobanano/nano/block"
"github.com/BananoCoin/gobanano/nano/internal/util"
)
const (
PendingKeySize = nano.AddressSize + block.HashSize
)
type PendingKey [PendingKeySize]byte
type Pending struct {
Address nano.Address
Amount nano.Balance
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (p *Pending) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
var err error
if _, err = buf.Write(p.Address[:]); err != nil {
return nil, err
}
if _, err = buf.Write(p.Amount.Bytes(binary.BigEndian)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (p *Pending) UnmarshalBinary(data []byte) error {
reader := bytes.NewReader(data)
if _, err := reader.Read(p.Address[:]); err != nil {
return err
}
balance := make([]byte, nano.BalanceSize)
if _, err := reader.Read(balance); err != nil {
return err
}
if err := p.Amount.UnmarshalBinary(balance); err != nil {
return err
}
return util.AssertReaderEOF(reader)
}