-
Notifications
You must be signed in to change notification settings - Fork 2
/
bodyv2.go
137 lines (119 loc) · 3.9 KB
/
bodyv2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package types
import (
"io"
"github.com/ethereum/go-ethereum/rlp"
"github.com/PositionExchange/posichain/block"
staking "github.com/PositionExchange/posichain/staking/types"
)
// BodyV2 is the V2 block body
type BodyV2 struct {
f bodyFieldsV2
}
type bodyFieldsV2 struct {
Transactions []*Transaction
StakingTransactions []*staking.StakingTransaction
Uncles []*block.Header
IncomingReceipts CXReceiptsProofs
}
// Transactions returns the list of transactions.
//
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV2) Transactions() (txs []*Transaction) {
for _, tx := range b.f.Transactions {
txs = append(txs, tx.Copy())
}
return txs
}
// StakingTransactions returns the list of staking transactions.
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV2) StakingTransactions() (txs []*staking.StakingTransaction) {
for _, tx := range b.f.StakingTransactions {
txs = append(txs, tx.Copy())
}
return txs
}
// TransactionAt returns the transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV2) TransactionAt(index int) *Transaction {
if index < 0 || index >= len(b.f.Transactions) {
return nil
}
return b.f.Transactions[index].Copy()
}
// StakingTransactionAt returns the staking transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV2) StakingTransactionAt(index int) *staking.StakingTransaction {
if index < 0 || index >= len(b.f.StakingTransactions) {
return nil
}
return b.f.StakingTransactions[index].Copy()
}
// CXReceiptAt returns the CXReceipt at given index in this block
// It returns nil if index is out of bounds
func (b *BodyV2) CXReceiptAt(index int) *CXReceipt {
if index < 0 {
return nil
}
for _, cxp := range b.f.IncomingReceipts {
cxs := cxp.Receipts
if index < len(cxs) {
return cxs[index].Copy()
}
index -= len(cxs)
}
return nil
}
// SetTransactions sets the list of transactions with a deep copy of the given
// list.
func (b *BodyV2) SetTransactions(newTransactions []*Transaction) {
var txs []*Transaction
for _, tx := range newTransactions {
txs = append(txs, tx.Copy())
}
b.f.Transactions = txs
}
// SetStakingTransactions sets the list of staking transactions with a deep copy of the given
// list.
func (b *BodyV2) SetStakingTransactions(newStakingTransactions []*staking.StakingTransaction) {
var txs []*staking.StakingTransaction
for _, tx := range newStakingTransactions {
txs = append(txs, tx.Copy())
}
b.f.StakingTransactions = txs
}
// Uncles returns a deep copy of the list of uncle headers of this block.
func (b *BodyV2) Uncles() (uncles []*block.Header) {
for _, uncle := range b.f.Uncles {
uncles = append(uncles, CopyHeader(uncle))
}
return uncles
}
// SetUncles sets the list of uncle headers with a deep copy of the given list.
func (b *BodyV2) SetUncles(newUncle []*block.Header) {
var uncles []*block.Header
for _, uncle := range newUncle {
uncles = append(uncles, CopyHeader(uncle))
}
b.f.Uncles = uncles
}
// IncomingReceipts returns a deep copy of the list of incoming cross-shard
// transaction receipts of this block.
func (b *BodyV2) IncomingReceipts() (incomingReceipts CXReceiptsProofs) {
return b.f.IncomingReceipts.Copy()
}
// SetIncomingReceipts sets the list of incoming cross-shard transaction
// receipts of this block with a dep copy of the given list.
func (b *BodyV2) SetIncomingReceipts(newIncomingReceipts CXReceiptsProofs) {
b.f.IncomingReceipts = newIncomingReceipts.Copy()
}
// EncodeRLP RLP-encodes the block body into the given writer.
func (b *BodyV2) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &b.f)
}
// DecodeRLP RLP-decodes a block body from the given RLP stream into the
// receiver.
func (b *BodyV2) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&b.f)
}