forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
87 lines (79 loc) · 2.04 KB
/
result.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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
)
type Result struct {
Success bool
Units uint64
Output []byte
WarpMessage *warp.UnsignedMessage
}
func (r *Result) Size() int {
size := consts.BoolLen + consts.Uint64Len + codec.BytesLen(r.Output)
if r.WarpMessage != nil {
size += codec.BytesLen(r.WarpMessage.Bytes())
} else {
size += codec.BytesLen(nil)
}
return size
}
func (r *Result) Marshal(p *codec.Packer) {
p.PackBool(r.Success)
p.PackUint64(r.Units)
p.PackBytes(r.Output)
var warpBytes []byte
if r.WarpMessage != nil {
warpBytes = r.WarpMessage.Bytes()
}
p.PackBytes(warpBytes)
}
func MarshalResults(src []*Result) ([]byte, error) {
size := consts.IntLen + codec.CummSize(src)
p := codec.NewWriter(size, consts.MaxInt) // could be much larger than [NetworkSizeLimit]
p.PackInt(len(src))
for _, result := range src {
result.Marshal(p)
}
return p.Bytes(), p.Err()
}
func UnmarshalResult(p *codec.Packer) (*Result, error) {
result := &Result{
Success: p.UnpackBool(),
Units: p.UnpackUint64(false),
}
p.UnpackBytes(consts.MaxInt, false, &result.Output)
if len(result.Output) == 0 {
// Enforce object standardization
result.Output = nil
}
var warpMessage []byte
p.UnpackBytes(MaxWarpMessageSize, false, &warpMessage)
if len(warpMessage) > 0 {
msg, err := warp.ParseUnsignedMessage(warpMessage)
if err != nil {
return nil, err
}
result.WarpMessage = msg
}
return result, p.Err()
}
func UnmarshalResults(src []byte) ([]*Result, error) {
p := codec.NewReader(src, consts.MaxInt) // could be much larger than [NetworkSizeLimit]
items := p.UnpackInt(false)
results := make([]*Result, items)
for i := 0; i < items; i++ {
result, err := UnmarshalResult(p)
if err != nil {
return nil, err
}
results[i] = result
}
if !p.Empty() {
return nil, ErrInvalidObject
}
return results, nil
}