-
Notifications
You must be signed in to change notification settings - Fork 659
/
builtin.go
61 lines (51 loc) · 1.52 KB
/
builtin.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"errors"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
var (
errNotIDType = errors.New("expected ids.ID but got unexpected type")
errNotStatusType = errors.New("expected choices.Status but got unexpected type")
errNotTimeType = errors.New("expected time.Time but got unexpected type")
)
func marshalID(idIntf interface{}) ([]byte, error) {
if id, ok := idIntf.(ids.ID); ok {
return id[:], nil
}
return nil, errNotIDType
}
func unmarshalID(bytes []byte) (interface{}, error) {
return ids.ToID(bytes)
}
func marshalStatus(statusIntf interface{}) ([]byte, error) {
if status, ok := statusIntf.(choices.Status); ok {
return status.Bytes(), nil
}
return nil, errNotStatusType
}
func unmarshalStatus(bytes []byte) (interface{}, error) {
p := wrappers.Packer{Bytes: bytes}
status := choices.Status(p.UnpackInt())
if err := status.Valid(); err != nil {
return nil, err
}
return status, p.Err
}
func marshalTime(timeIntf interface{}) ([]byte, error) {
if t, ok := timeIntf.(time.Time); ok {
p := wrappers.Packer{MaxSize: wrappers.LongLen}
p.PackLong(uint64(t.Unix()))
return p.Bytes, p.Err
}
return nil, errNotTimeType
}
func unmarshalTime(bytes []byte) (interface{}, error) {
p := wrappers.Packer{Bytes: bytes}
unixTime := p.UnpackLong()
return time.Unix(int64(unixTime), 0), nil
}