-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
109 lines (92 loc) · 2.4 KB
/
block.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
// Copyright (c) 2018 The VeChainThor developers
// Copyright (c) 2019 The PlayMaker developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package block
import (
"fmt"
"io"
"sync/atomic"
"github.com/ethereum/go-ethereum/rlp"
"github.com/playmakerchain/powerplay/metric"
"github.com/playmakerchain/powerplay/tx"
)
// Block is an immutable block type.
type Block struct {
header *Header
txs tx.Transactions
cache struct {
size atomic.Value
}
}
// Body defines body of a block.
type Body struct {
Txs tx.Transactions
}
// Compose a block with all needed components
// Note: This method is usually to recover a block by its portions, and the TxsRoot is not verified.
// To build up a block, use a Builder.
func Compose(header *Header, txs tx.Transactions) *Block {
return &Block{
header: header,
txs: append(tx.Transactions(nil), txs...),
}
}
// WithSignature create a new block object with signature set.
func (b *Block) WithSignature(sig []byte) *Block {
return &Block{
header: b.header.withSignature(sig),
txs: b.txs,
}
}
// Header returns the block header.
func (b *Block) Header() *Header {
return b.header
}
// Transactions returns a copy of transactions.
func (b *Block) Transactions() tx.Transactions {
return append(tx.Transactions(nil), b.txs...)
}
// Body returns body of a block.
func (b *Block) Body() *Body {
return &Body{append(tx.Transactions(nil), b.txs...)}
}
// EncodeRLP implements rlp.Encoder.
func (b *Block) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{
b.header,
b.txs,
})
}
// DecodeRLP implements rlp.Decoder.
func (b *Block) DecodeRLP(s *rlp.Stream) error {
_, size, _ := s.Kind()
payload := struct {
Header Header
Txs tx.Transactions
}{}
if err := s.Decode(&payload); err != nil {
return err
}
*b = Block{
header: &payload.Header,
txs: payload.Txs,
}
b.cache.size.Store(metric.StorageSize(rlp.ListSize(size)))
return nil
}
// Size returns block size in bytes.
func (b *Block) Size() metric.StorageSize {
if cached := b.cache.size.Load(); cached != nil {
return cached.(metric.StorageSize)
}
var size metric.StorageSize
rlp.Encode(&size, b)
b.cache.size.Store(size)
return size
}
func (b *Block) String() string {
return fmt.Sprintf(`Block(%v)
%v
Transactions: %v`, b.Size(), b.header, b.txs)
}