/
block.go
270 lines (219 loc) · 6.05 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2022 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package block
import (
"fmt"
"github.com/amazechain/amc/api/protocol/types_pb"
"github.com/amazechain/amc/common/hash"
"github.com/amazechain/amc/common/transaction"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/utils"
"github.com/holiman/uint256"
"google.golang.org/protobuf/proto"
"strings"
"sync/atomic"
"time"
)
//type writeCounter types.StorageSize
//
//func (c *writeCounter) Write(b []byte) (int, error) {
// *c += writeCounter(len(b))
// return len(b), nil
//}
type Block struct {
header *Header
body *Body
hash atomic.Value
size atomic.Value
ReceiveAt time.Time
ReceivedFrom interface{}
}
type Verify struct {
Address types.Address
PublicKey types.PublicKey
}
func (v *Verify) ToProtoMessage() proto.Message {
var pbVerifier types_pb.Verifier
pbVerifier.Address = utils.ConvertAddressToH160(v.Address)
pbVerifier.PublicKey = utils.ConvertPublicKeyToH384(v.PublicKey)
return &pbVerifier
}
func (v *Verify) FromProtoMessage(pbVerifier *types_pb.Verifier) *Verify {
v.Address = utils.ConvertH160toAddress(pbVerifier.Address)
v.PublicKey = utils.ConvertH384ToPublicKey(pbVerifier.PublicKey)
return v
}
type Reward struct {
Address types.Address
Amount *uint256.Int
}
type Rewards []*Reward
func (r Rewards) Len() int {
return len(r)
}
func (r Rewards) Less(i, j int) bool {
return strings.Compare(r[i].Address.String(), r[j].Address.String()) > 0
}
func (r Rewards) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r *Reward) ToProtoMessage() proto.Message {
var pbReward types_pb.Reward
pbReward.Address = utils.ConvertAddressToH160(r.Address)
pbReward.Amount = utils.ConvertUint256IntToH256(r.Amount)
return &pbReward
}
func (r *Reward) FromProtoMessage(pbReward *types_pb.Reward) *Reward {
r.Address = utils.ConvertH160toAddress(pbReward.Address)
r.Amount = utils.ConvertH256ToUint256Int(pbReward.Amount)
return r
}
func (b *Block) Transactions() []*transaction.Transaction {
if b.body != nil {
return b.body.Transactions()
}
return nil
}
func (b *Block) StateRoot() types.Hash {
return b.header.Root
}
func (b *Block) Hash() types.Hash {
return b.Header().Hash()
}
func (b *Block) Marshal() ([]byte, error) {
bpBlock := b.ToProtoMessage()
return proto.Marshal(bpBlock)
}
func (b *Block) Unmarshal(data []byte) error {
var pBlock types_pb.Block
if err := proto.Unmarshal(data, &pBlock); err != nil {
return err
}
if err := b.FromProtoMessage(&pBlock); err != nil {
return err
}
return nil
}
// NewBlock creates a new block. The input data is copied,
// changes to header and to the field values will not affect the
// block.
//
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
// are ignored and set to values derived from the given txs, uncles
// and receipts.
func NewBlock(h IHeader, txs []*transaction.Transaction) IBlock {
block := &Block{
header: CopyHeader(h.(*Header)),
body: &Body{Txs: txs},
ReceiveAt: time.Now(),
ReceivedFrom: nil,
}
return block
}
func NewBlockFromReceipt(h IHeader, txs []*transaction.Transaction, uncles []IHeader, receipts []*Receipt, reward []*Reward) IBlock {
block := &Block{
header: CopyHeader(h.(*Header)),
body: &Body{Txs: txs, Rewards: CopyReward(reward)},
ReceiveAt: time.Now(),
ReceivedFrom: nil,
}
//if len(receipts) > 0 {
// print(receipts)
//}
block.header.Bloom = CreateBloom(receipts)
block.header.TxHash = hash.DeriveSha(transaction.Transactions(txs))
block.header.ReceiptHash = hash.DeriveSha(Receipts(receipts))
return block
}
func (b *Block) Header() IHeader {
return CopyHeader(b.header)
}
func (b *Block) Body() IBody {
return b.body
}
func (b *Block) Number64() *uint256.Int {
return b.header.Number
}
func (b *Block) BaseFee64() *uint256.Int {
return b.header.BaseFee
}
func (b *Block) Difficulty() *uint256.Int {
return b.header.Difficulty
}
func (b *Block) Time() uint64 {
return b.header.Time
}
func (b *Block) GasLimit() uint64 {
return b.header.GasLimit
}
func (b *Block) GasUsed() uint64 {
return b.header.GasUsed
}
func (b *Block) Nonce() uint64 {
return b.header.Nonce.Uint64()
}
func (b *Block) Coinbase() types.Address {
return b.header.Coinbase
}
func (b *Block) ParentHash() types.Hash {
return b.header.ParentHash
}
func (b *Block) TxHash() types.Hash {
return b.header.TxHash
}
func (b *Block) WithSeal(header IHeader) *Block {
b.header = CopyHeader(header.(*Header))
return b
}
func (b *Block) Transaction(hash types.Hash) *transaction.Transaction {
return nil
}
func (b *Block) ToProtoMessage() proto.Message {
pbHeader := b.header.ToProtoMessage()
pbBody := b.body.ToProtoMessage()
pBlock := types_pb.Block{
Header: pbHeader.(*types_pb.Header),
Body: pbBody.(*types_pb.Body),
}
return &pBlock
}
func (b *Block) FromProtoMessage(message proto.Message) error {
var (
pBlock *types_pb.Block
header Header
body Body
ok bool
)
if pBlock, ok = message.(*types_pb.Block); !ok {
return fmt.Errorf("type conversion failure")
}
if err := header.FromProtoMessage(pBlock.Header); err != nil {
return err
}
if err := body.FromProtoMessage(pBlock.Body); err != nil {
return err
}
b.header = &header
b.body = &body
b.ReceiveAt = time.Now()
return nil
}
func (b *Block) SendersToTxs(senders []types.Address) {
//todo
}
func (b *Block) Uncles() []*Header {
return nil
}