forked from Fantom-foundation/go-opera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_genesis.go
233 lines (206 loc) · 6.85 KB
/
apply_genesis.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
package gossip
import (
"errors"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/inter/pos"
"github.com/Fantom-foundation/lachesis-base/lachesis"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/frenchie-foundation/go-opera/evmcore"
"github.com/frenchie-foundation/go-opera/gossip/blockproc"
"github.com/frenchie-foundation/go-opera/gossip/evmstore"
"github.com/frenchie-foundation/go-opera/gossip/sfcapi"
"github.com/frenchie-foundation/go-opera/inter"
"github.com/frenchie-foundation/go-opera/inter/drivertype"
"github.com/frenchie-foundation/go-opera/opera"
"github.com/frenchie-foundation/go-opera/opera/genesis"
)
// ApplyGenesis writes initial state.
func (s *Store) ApplyGenesis(blockProc BlockProc, g opera.Genesis) (genesisHash hash.Hash, err error) {
// if we'here, then it's first time genesis is applied
err = s.applyEpoch1Genesis(blockProc, g)
if err != nil {
return genesisHash, err
}
genesisHash = g.Hash()
s.SetGenesisHash(genesisHash)
return genesisHash, err
}
func (s *Store) applyEpoch0Genesis(g opera.Genesis) (evmBlock *evmcore.EvmBlock, err error) {
// write genesis blocks
var highestBlock blockproc.BlockCtx
var startingRoot hash.Hash
g.Blocks.ForEach(func(blockIdx idx.Block, block genesis.Block) {
txHashes := make([]common.Hash, len(block.Txs))
internalTxHashes := make([]common.Hash, len(block.InternalTxs))
for i, tx := range block.Txs {
txHashes[i] = tx.Hash()
}
for i, tx := range block.InternalTxs {
internalTxHashes[i] = tx.Hash()
}
for i, tx := range append(block.InternalTxs, block.Txs...) {
s.evm.SetTxPosition(tx.Hash(), evmstore.TxPosition{
Block: blockIdx,
BlockOffset: uint32(i),
})
s.evm.SetTx(tx.Hash(), tx)
}
gasUsed := uint64(0)
if len(block.Receipts) != 0 {
gasUsed = block.Receipts[len(block.Receipts)-1].CumulativeGasUsed
s.evm.SetRawReceipts(blockIdx, block.Receipts)
allTxs := block.Txs
if block.InternalTxs.Len() > 0 {
allTxs = append(block.InternalTxs, block.Txs...)
}
logIdx := uint(0)
for i, r := range block.Receipts {
for _, l := range r.Logs {
l.BlockNumber = uint64(blockIdx)
l.TxHash = allTxs[i].Hash()
l.Index = logIdx
l.TxIndex = uint(i)
l.BlockHash = common.Hash(block.Atropos)
logIdx++
}
s.evm.IndexLogs(r.Logs...)
}
}
s.SetBlock(blockIdx, &inter.Block{
Time: block.Time,
Atropos: block.Atropos,
Events: hash.Events{},
Txs: txHashes,
InternalTxs: internalTxHashes,
SkippedTxs: []uint32{},
GasUsed: gasUsed,
Root: block.Root,
})
s.SetBlockIndex(block.Atropos, blockIdx)
highestBlock.Idx = blockIdx
highestBlock.Atropos = block.Atropos
highestBlock.Time = block.Time
startingRoot = block.Root
})
// apply EVM genesis
evmBlock, err = s.evm.ApplyGenesis(g, startingRoot)
if err != nil {
return evmBlock, err
}
s.SetBlockEpochState(blockproc.BlockState{
LastBlock: highestBlock,
FinalizedStateRoot: hash.Hash(evmBlock.Root),
EpochGas: 0,
EpochCheaters: lachesis.Cheaters{},
ValidatorStates: make([]blockproc.ValidatorBlockState, 0),
NextValidatorProfiles: make(map[idx.ValidatorID]drivertype.Validator),
DirtyRules: g.Rules,
}, blockproc.EpochState{
Epoch: g.FirstEpoch - 1,
EpochStart: g.PrevEpochTime,
PrevEpochStart: g.PrevEpochTime - 1,
EpochStateRoot: hash.Zero,
Validators: pos.NewBuilder().Build(),
ValidatorStates: make([]blockproc.ValidatorEpochState, 0),
ValidatorProfiles: make(map[idx.ValidatorID]drivertype.Validator),
Rules: g.Rules,
})
return evmBlock, nil
}
func (s *Store) applyEpoch1Genesis(blockProc BlockProc, g opera.Genesis) (err error) {
evmBlock0, err := s.applyEpoch0Genesis(g)
if err != nil {
return err
}
evmStateReader := &EvmStateReader{store: s}
statedb, err := s.evm.StateDB(hash.Hash(evmBlock0.Root))
if err != nil {
return err
}
bs, es := s.GetBlockState(), s.GetEpochState()
blockCtx := blockproc.BlockCtx{
Idx: bs.LastBlock.Idx + 1,
Time: g.Time,
Atropos: hash.Event{},
}
sealer := blockProc.SealerModule.Start(blockCtx, bs, es)
sealing := true
txListener := blockProc.TxListenerModule.Start(blockCtx, bs, es, statedb)
evmProcessor := blockProc.EVMModule.Start(blockCtx, statedb, evmStateReader, func(l *types.Log) {
txListener.OnNewLog(l)
sfcapi.OnNewLog(s.sfcapi, l)
}, es.Rules)
// Execute genesis-internal transactions
genesisInternalTxs := blockProc.GenesisTxTransactor.PopInternalTxs(blockCtx, bs, es, sealing, statedb)
evmProcessor.Execute(genesisInternalTxs, true)
bs = txListener.Finalize()
// Execute pre-internal transactions
preInternalTxs := blockProc.PreTxTransactor.PopInternalTxs(blockCtx, bs, es, sealing, statedb)
evmProcessor.Execute(preInternalTxs, true)
bs = txListener.Finalize()
// Seal epoch if requested
if sealing {
sealer.Update(bs, es)
bs, es = sealer.SealEpoch()
txListener.Update(bs, es)
}
// Execute post-internal transactions
internalTxs := blockProc.PostTxTransactor.PopInternalTxs(blockCtx, bs, es, sealing, statedb)
evmProcessor.Execute(internalTxs, true)
evmBlock, skippedTxs, receipts := evmProcessor.Finalize()
for _, r := range receipts {
if r.Status == 0 {
return errors.New("genesis transaction reverted")
}
}
if len(skippedTxs) != 0 {
return errors.New("genesis transaction is skipped")
}
bs = txListener.Finalize()
bs.FinalizedStateRoot = hash.Hash(evmBlock.Root)
bs.LastBlock = blockCtx
s.SetBlockEpochState(bs, es)
prettyHash := func(root common.Hash, g opera.Genesis) hash.Event {
e := inter.MutableEventPayload{}
// for nice-looking ID
e.SetEpoch(g.FirstEpoch - 1)
e.SetLamport(1)
// actual data hashed
e.SetExtra(append(root[:], g.ExtraData...))
e.SetCreationTime(g.Time)
return e.Build().ID()
}
genesisAtropos := prettyHash(evmBlock.Root, g)
block := &inter.Block{
Time: blockCtx.Time,
Atropos: genesisAtropos,
Events: hash.Events{},
SkippedTxs: skippedTxs,
GasUsed: evmBlock.GasUsed,
Root: hash.Hash(evmBlock.Root),
}
// store txs index
for i, tx := range append(genesisInternalTxs, append(preInternalTxs, internalTxs...)...) {
block.InternalTxs = append(block.InternalTxs, tx.Hash())
s.evm.SetTx(tx.Hash(), tx)
s.evm.SetTxPosition(tx.Hash(), evmstore.TxPosition{
Block: blockCtx.Idx,
BlockOffset: uint32(i),
})
}
if receipts.Len() != 0 {
s.evm.SetReceipts(blockCtx.Idx, receipts)
for _, r := range receipts {
s.evm.IndexLogs(r.Logs...)
}
}
s.commitEVM()
s.SetBlock(blockCtx.Idx, block)
s.SetBlockIndex(genesisAtropos, blockCtx.Idx)
s.SetGenesisBlockIndex(blockCtx.Idx)
// index data for legacy SFC API
sfcapi.ApplyGenesis(s.sfcapi, s.evm.EvmLogs())
return nil
}