forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
307 lines (271 loc) · 11 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/haowang0402/coreth/core/types"
"github.com/haowang0402/coreth/params"
"github.com/haowang0402/avalanchego/chains/atomic"
"github.com/haowang0402/avalanchego/ids"
"github.com/haowang0402/avalanchego/snow/choices"
)
var (
bonusBlocks = ids.Set{}
bonusBlockMainnetHeights = make(map[uint64]ids.ID)
// first height that processed a TX included on a
// bonus block is the canonical height for that TX.
canonicalBonusBlocks = []uint64{
102928, 103035, 103038, 103114, 103193,
103234, 103338, 103444, 103480, 103491,
103513, 103533, 103535, 103538, 103541,
103546, 103571, 103572, 103619,
103287, 103624, 103591,
}
)
func init() {
mainnetBonusBlocks := map[uint64]string{
102972: "Njm9TcLUXRojZk8YhEM6ksvfiPdC1TME4zJvGaDXgzMCyB6oB",
103105: "BYqLB6xpqy7HsAgP2XNfGE8Ubg1uEzse5mBPTSJH9z5s8pvMa",
103143: "AfWvJH3rB2fdHuPWQp6qYNCFVT29MooQPRigD88rKKwUDEDhq",
103183: "2KPW9G5tiNF14tZNfG4SqHuQrtUYVZyxuof37aZ7AnTKrQdsHn",
103197: "pE93VXY3N5QKfwsEFcM9i59UpPFgeZ8nxpJNaGaDQyDgsscNf",
103203: "2czmtnBS44VCWNRFUM89h4Fe9m3ZeZVYyh7Pe3FhNqjRNgPXhZ",
103208: "esx5J962LtYm2aSrskpLai5e4CMMsaS1dsu9iuLGJ3KWgSu2M",
103209: "DK9NqAJGry1wAo767uuYc1dYXAjUhzwka6vi8d9tNheqzGUTd",
103259: "i1HoerJ1axognkUKKL58FvF9aLrbZKtv7TdKLkT5kgzoeU1vB",
103261: "2DpCuBaH94zKKFNY2XTs4GeJcwsEv6qT2DHc59S8tdg97GZpcJ",
103266: "2ez4CA7w4HHr8SSobHQUAwFgj2giRNjNFUZK9JvrZFa1AuRj6X",
103287: "2QBNMMFJmhVHaGF45GAPszKyj1gK6ToBERRxYvXtM7yfrdUGPK",
103339: "2pSjfo7rkFCfZ2CqAxqfw8vqM2CU2nVLHrFZe3rwxz43gkVuGo",
103346: "2SiSziHHqPjb1qkw7CdGYupokiYpd2b7mMqRiyszurctcA5AKr",
103350: "2F5tSQbdTfhZxvkxZqdFp7KR3FrJPKEsDLQK7KtPhNXj1EZAh4",
103358: "2tCe88ur6MLQcVgwE5XxoaHiTGtSrthwKN3SdbHE4kWiQ7MSTV",
103437: "21o2fVTnzzmtgXqkV1yuQeze7YEQhR5JB31jVVD9oVUnaaV8qm",
103472: "2nG4exd9eUoAGzELfksmBR8XDCKhohY1uDKRFzEXJG4M8p3qA7",
103478: "63YLdYXfXc5tY3mwWLaDsbXzQHYmwWVxMP7HKbRh4Du3C2iM1",
103493: "soPweZ8DGaoUMjrnzjH3V2bypa7ZvvfqBan4UCsMUxMP759gw",
103514: "2dNkpQF4mooveyUDfBYQTBfsGDV4wkncQPpEw4kHKfSTSTo5x",
103536: "PJTkRrHvKZ1m4AQdPND1MBpUXpCrGN4DDmXmJQAiUrsxPoLQX",
103545: "22ck2Z7cC38hmBfX2v3jMWxun8eD8psNaicfYeokS67DxwmPTx",
103547: "pTf7gfk1ksj7bqMrLyMCij8FBKth1uRqQrtfykMFeXhx5xnrL",
103554: "9oZh4qyBCcVwSGyDoUzRAuausvPJN3xH6nopKS6bwYzMfLoQ2",
103555: "MjExz2z1qhwugc1tAyiGxRsCq4GvJwKfyyS29nr4tRVB8ooic",
103559: "cwJusfmn98TW3DjAbfLRN9utYR24KAQ82qpAXmVSvjHyJZuM2",
103561: "2YgxGHns7Z2hMMHJsPCgVXuJaL7x1b3gnHbmSCfCdyAcYGr6mx",
103563: "2AXxT3PSEnaYHNtBTnYrVTf24TtKDWjky9sqoFEhydrGXE9iKH",
103564: "Ry2sfjFfGEnJxRkUGFSyZNn7GR3m4aKAf1scDW2uXSNQB568Y",
103569: "21Jys8UNURmtckKSV89S2hntEWymJszrLQbdLaNcbXcxDAsQSa",
103570: "sg6wAwFBsPQiS5Yfyh41cVkCRQbrrXsxXmeNyQ1xkunf2sdyv",
103575: "z3BgePPpCXq1mRBRvUi28rYYxnEtJizkUEHnDBrcZeVA7MFVk",
103577: "uK5Ff9iBfDtREpVv9NgCQ1STD1nzLJG3yrfibHG4mGvmybw6f",
103578: "Qv5v5Ru8ArfnWKB1w6s4G5EYPh7TybHJtF6UsVwAkfvZFoqmj",
103582: "7KCZKBpxovtX9opb7rMRie9WmW5YbZ8A4HwBBokJ9eSHpZPqx",
103587: "2AfTQ2FXNj9bkSUQnud9pFXULx6EbF7cbbw6i3ayvc2QNhgxfF",
103590: "2gTygYckZgFZfN5QQWPaPBD3nabqjidV55mwy1x1Nd4JmJAwaM",
103591: "2cUPPHy1hspr2nAKpQrrAEisLKkaWSS9iF2wjNFyFRs8vnSkKK",
103594: "5MptSdP6dBMPSwk9GJjeVe39deZJTRh9i82cgNibjeDffrrTf",
103597: "2J8z7HNv4nwh82wqRGyEHqQeuw4wJ6mCDCSvUgusBu35asnshK",
103598: "2i2FP6nJyvhX9FR15qN2D9AVoK5XKgBD2i2AQ7FoSpfowxvQDX",
103603: "2v3smb35s4GLACsK4Zkd2RcLBLdWA4huqrvq8Y3VP4CVe8kfTM",
103604: "b7XfDDLgwB12DfL7UTWZoxwBpkLPL5mdHtXngD94Y2RoeWXSh",
103607: "PgaRk1UAoUvRybhnXsrLq5t6imWhEa6ksNjbN6hWgs4qPrSzm",
103612: "2oueNTj4dUE2FFtGyPpawnmCCsy6EUQeVHVLZy8NHeQmkAciP4",
103614: "2YHZ1KymFjiBhpXzgt6HXJhLSt5SV9UQ4tJuUNjfN1nQQdm5zz",
103617: "amgH2C1s9H3Av7vSW4y7n7TXb9tKyKHENvrDXutgNN6nsejgc",
103618: "fV8k1U8oQDmfVwK66kAwN73aSsWiWhm8quNpVnKmSznBycV2W",
103621: "Nzs93kFTvcXanFUp9Y8VQkKYnzmH8xykxVNFJTkdyAEeuxWbP",
103623: "2rAsBj3emqQa13CV8r5fTtHogs4sXnjvbbXVzcKPi3WmzhpK9D",
103624: "2JbuExUGKW5mYz5KfXATwq1ibRDimgks9wEdYGNSC6Ttey1R4U",
103627: "tLLijh7oKfvWT1yk9zRv4FQvuQ5DAiuvb5kHCNN9zh4mqkFMG",
103628: "dWBsRYRwFrcyi3DPdLoHsL67QkZ5h86hwtVfP94ZBaY18EkmF",
103629: "XMoEsew2DhSgQaydcJFJUQAQYP8BTNTYbEJZvtbrV2QsX7iE3",
103630: "2db2wMbVAoCc5EUJrsBYWvNZDekqyY8uNpaaVapdBAQZ5oRaou",
103633: "2QiHZwLhQ3xLuyyfcdo5yCUfoSqWDvRZox5ECU19HiswfroCGp",
}
for height, blkIDStr := range mainnetBonusBlocks {
blkID, err := ids.FromString(blkIDStr)
if err != nil {
panic(err)
}
bonusBlocks.Add(blkID)
bonusBlockMainnetHeights[height] = blkID
}
}
// Block implements the snowman.Block interface
type Block struct {
id ids.ID
ethBlock *types.Block
vm *VM
status choices.Status
atomicTxs []*Tx
}
// ID implements the snowman.Block interface
func (b *Block) ID() ids.ID { return b.id }
// Accept implements the snowman.Block interface
func (b *Block) Accept() error {
vm := b.vm
// Although returning an error from Accept is considered fatal, it is good
// practice to cleanup the batch we were modifying in the case of an error.
defer vm.db.Abort()
b.status = choices.Accepted
log.Debug(fmt.Sprintf("Accepting block %s (%s) at height %d", b.ID().Hex(), b.ID(), b.Height()))
if err := vm.chain.Accept(b.ethBlock); err != nil {
return fmt.Errorf("chain could not accept %s: %w", b.ID(), err)
}
if err := vm.acceptedBlockDB.Put(lastAcceptedKey, b.id[:]); err != nil {
return fmt.Errorf("failed to put %s as the last accepted block: %w", b.ID(), err)
}
if len(b.atomicTxs) == 0 {
if err := b.vm.atomicTrie.Index(b.Height(), nil); err != nil {
return err
}
return vm.db.Commit()
}
batchChainsAndInputs, err := mergeAtomicOps(b.atomicTxs)
if err != nil {
return err
}
for _, tx := range b.atomicTxs {
// Remove the accepted transaction from the mempool
vm.mempool.RemoveTx(tx)
}
isBonus := bonusBlocks.Contains(b.id)
if err := b.indexAtomics(vm, b.Height(), b.atomicTxs, batchChainsAndInputs, isBonus); err != nil {
return err
}
// If [b] is a bonus block, then we commit the database without applying the requests from
// the atmoic transactions to shared memory.
if isBonus {
log.Info("skipping atomic tx acceptance on bonus block", "block", b.id)
return vm.db.Commit()
}
batch, err := vm.db.CommitBatch()
if err != nil {
return fmt.Errorf("failed to create commit batch due to: %w", err)
}
return vm.ctx.SharedMemory.Apply(batchChainsAndInputs, batch)
}
// indexAtomics writes given list of atomic transactions and atomic operations to atomic repository
// and atomic trie respectively
func (b *Block) indexAtomics(vm *VM, height uint64, atomicTxs []*Tx, batchChainsAndInputs map[ids.ID]*atomic.Requests, isBonus bool) error {
if isBonus {
// avoid indexing atomic operations of txs on bonus blocks in the trie
// so we do not re-execute them the second time that they appear
return vm.atomicTxRepository.WriteBonus(height, atomicTxs)
}
if err := vm.atomicTxRepository.Write(height, atomicTxs); err != nil {
return err
}
return b.vm.atomicTrie.Index(height, batchChainsAndInputs)
}
// Reject implements the snowman.Block interface
// If [b] contains an atomic transaction, attempt to re-issue it
func (b *Block) Reject() error {
b.status = choices.Rejected
log.Debug(fmt.Sprintf("Rejecting block %s (%s) at height %d", b.ID().Hex(), b.ID(), b.Height()))
for _, tx := range b.atomicTxs {
b.vm.mempool.RemoveTx(tx)
if err := b.vm.issueTx(tx, false /* set local to false when re-issuing */); err != nil {
log.Debug("Failed to re-issue transaction in rejected block", "txID", tx.ID(), "err", err)
}
}
return b.vm.chain.Reject(b.ethBlock)
}
// SetStatus implements the InternalBlock interface allowing ChainState
// to set the status on an existing block
func (b *Block) SetStatus(status choices.Status) { b.status = status }
// Status implements the snowman.Block interface
func (b *Block) Status() choices.Status {
return b.status
}
// Parent implements the snowman.Block interface
func (b *Block) Parent() ids.ID {
return ids.ID(b.ethBlock.ParentHash())
}
// Height implements the snowman.Block interface
func (b *Block) Height() uint64 {
return b.ethBlock.NumberU64()
}
// Timestamp implements the snowman.Block interface
func (b *Block) Timestamp() time.Time {
return time.Unix(int64(b.ethBlock.Time()), 0)
}
// syntacticVerify verifies that a *Block is well-formed.
func (b *Block) syntacticVerify() (params.Rules, error) {
if b == nil || b.ethBlock == nil {
return params.Rules{}, errInvalidBlock
}
header := b.ethBlock.Header()
rules := b.vm.chainConfig.AvalancheRules(header.Number, new(big.Int).SetUint64(header.Time))
return rules, b.vm.getBlockValidator(rules).SyntacticVerify(b)
}
// Verify implements the snowman.Block interface
func (b *Block) Verify() error {
return b.verify(true)
}
func (b *Block) verify(writes bool) error {
rules, err := b.syntacticVerify()
if err != nil {
return fmt.Errorf("syntactic block verification failed: %w", err)
}
if err := b.verifyAtomicTxs(rules); err != nil {
return err
}
return b.vm.chain.BlockChain().InsertBlockManual(b.ethBlock, writes)
}
func (b *Block) verifyAtomicTxs(rules params.Rules) error {
// Ensure that the parent was verified and inserted correctly.
ancestorID := b.Parent()
ancestorHash := common.Hash(ancestorID)
if !b.vm.chain.BlockChain().HasBlock(ancestorHash, b.Height()-1) {
return errRejectedParent
}
// If the tx is an atomic tx, ensure that it doesn't conflict with any of
// its processing ancestry.
inputs := &ids.Set{}
for _, atomicTx := range b.atomicTxs {
// If the ancestor is unknown, then the parent failed verification when
// it was called.
// If the ancestor is rejected, then this block shouldn't be inserted
// into the canonical chain because the parent will be missing.
ancestorInf, err := b.vm.GetBlockInternal(ancestorID)
if err != nil {
return errRejectedParent
}
if blkStatus := ancestorInf.Status(); blkStatus == choices.Unknown || blkStatus == choices.Rejected {
return errRejectedParent
}
ancestor, ok := ancestorInf.(*Block)
if !ok {
return fmt.Errorf("expected %s, parent of %s, to be *Block but is %T", ancestor.ID(), b.ID(), ancestorInf)
}
if bonusBlocks.Contains(b.id) {
log.Info("skipping atomic tx verification on bonus block", "block", b.id)
} else {
utx := atomicTx.UnsignedAtomicTx
if err := utx.SemanticVerify(b.vm, atomicTx, ancestor, b.ethBlock.BaseFee(), rules); err != nil {
return fmt.Errorf("invalid block due to failed semanatic verify: %w at height %d", err, b.Height())
}
txInputs := utx.InputUTXOs()
if inputs.Overlaps(txInputs) {
return errConflictingAtomicInputs
}
inputs.Union(txInputs)
}
}
return nil
}
// Bytes implements the snowman.Block interface
func (b *Block) Bytes() []byte {
res, err := rlp.EncodeToBytes(b.ethBlock)
if err != nil {
panic(err)
}
return res
}
func (b *Block) String() string { return fmt.Sprintf("EVM block, ID = %s", b.ID()) }