/
body.go
72 lines (59 loc) · 2 KB
/
body.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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package block
import (
"math/big"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/action"
)
// Body defines the struct of body
type Body struct {
Actions []action.SealedEnvelope
}
// Proto converts Body to Protobuf
func (b *Body) Proto() *iotextypes.BlockBody {
actions := []*iotextypes.Action{}
for _, act := range b.Actions {
actions = append(actions, act.Proto())
}
return &iotextypes.BlockBody{
Actions: actions,
}
}
// Serialize returns the serialized byte stream of the block
func (b *Body) Serialize() ([]byte, error) {
return proto.Marshal(b.Proto())
}
// LoadProto loads body from proto
func (b *Body) LoadProto(pbBlock *iotextypes.BlockBody) error {
b.Actions = []action.SealedEnvelope{}
for _, actPb := range pbBlock.Actions {
act := action.SealedEnvelope{}
if err := act.LoadProto(actPb); err != nil {
return err
}
b.Actions = append(b.Actions, act)
}
return nil
}
// Deserialize parses the byte stream into a Block
func (b *Body) Deserialize(buf []byte) error {
pb := iotextypes.BlockBody{}
if err := proto.Unmarshal(buf, &pb); err != nil {
return err
}
return b.LoadProto(&pb)
}
// CalculateTxRoot returns the Merkle root of all txs and actions in this block.
func (b *Body) CalculateTxRoot() hash.Hash256 {
return calculateTxRoot(b.Actions)
}
// CalculateTransferAmount returns the calculated transfer amount in this block.
func (b *Body) CalculateTransferAmount() *big.Int {
return calculateTransferAmount(b.Actions)
}