-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathprocessor.go
221 lines (196 loc) · 7.28 KB
/
processor.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
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package processor
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/fractalplatform/fractal/accountmanager"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/consensus"
"github.com/fractalplatform/fractal/params"
"github.com/fractalplatform/fractal/processor/vm"
"github.com/fractalplatform/fractal/state"
"github.com/fractalplatform/fractal/types"
)
// StateProcessor is a basic Processor, which takes care of transitioning
// state from one point to another.
//
// StateProcessor implements Processor.
type StateProcessor struct {
bc ChainContext // Canonical block chain
engine consensus.IEngine // Consensus engine used for block rewards
}
// NewStateProcessor initialises a new StateProcessor.
func NewStateProcessor(bc ChainContext, engine consensus.IEngine) *StateProcessor {
return &StateProcessor{
bc: bc,
engine: engine,
}
}
// Process processes the state changes according to the rules by running
// the transaction messages using the statedb and applying any rewards to both
// the processor (coinbase) and any included uncles.
//
// Process returns the receipts and logs accumulated during the process and
// returns the amount of gas that was used in the process. If any of the
// transactions failed to execute due to insufficient gas it will return an error.
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) ([]*types.Receipt, []*types.Log, uint64, error) {
var (
receipts []*types.Receipt
usedGas = new(uint64)
header = block.Header()
allLogs []*types.Log
gp = new(common.GasPool).AddGas(block.GasLimit())
)
// Prepare the block, applying any consensus engine specific extras (e.g. update last)
p.engine.Prepare(p.bc, header, block.Transactions(), receipts, statedb)
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
statedb.Prepare(tx.Hash(), block.Hash(), i)
receipt, _, err := p.ApplyTransaction(nil, gp, statedb, header, tx, usedGas, cfg)
if err != nil {
return nil, nil, 0, err
}
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
}
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
p.engine.Finalize(p.bc, header, block.Transactions(), receipts, statedb)
return receipts, allLogs, *usedGas, nil
}
// ApplyTransaction attempts to apply a transaction to the given state database
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func (p *StateProcessor) ApplyTransaction(author *common.Name, gp *common.GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error) {
bc := p.bc
config := bc.Config()
accountDB, err := accountmanager.NewAccountManager(statedb)
if err != nil {
return nil, 0, err
}
// todo for the moment,only system asset
// assetID := tx.GasAssetID()
assetID := p.bc.Config().SysTokenID
if assetID != tx.GasAssetID() {
return nil, 0, fmt.Errorf("only support system asset %d as tx fee", p.bc.Config().SysTokenID)
}
//timer for vm exec overtime
var t *time.Timer
//
var totalGas uint64
var ios []*types.ActionResult
detailTx := &types.DetailTx{}
var detailActions []*types.DetailAction
for i, action := range tx.GetActions() {
if needCheckSign(accountDB, action) {
if err := accountDB.RecoverTx(types.NewSigner(config.ChainID), tx); err != nil {
return nil, 0, err
}
}
nonce, err := accountDB.GetNonce(action.Sender())
if err != nil {
return nil, 0, err
}
if nonce < action.Nonce() {
return nil, 0, ErrNonceTooHigh
} else if nonce > action.Nonce() {
return nil, 0, ErrNonceTooLow
}
var gasPayer = action.Sender()
var gasPrice = tx.GasPrice()
if tx.PayerExist() {
if header.CurForkID() >= params.ForkID4 {
gasPayer = action.Payer()
gasPrice = action.PayerGasPrice()
} else {
return nil, 0, errPayerNotSupport
}
} else {
if action.PayerIsExist() {
return nil, 0, errPayerNotSupport
}
}
evmcontext := &EvmContext{
ChainContext: p.bc,
EngineContext: p.engine,
}
context := NewEVMContext(action.Sender(), action.Recipient(), assetID, gasPrice, header, evmcontext, author)
vmenv := vm.NewEVM(context, accountDB, statedb, config, cfg)
//will abort the vm if overtime
if false == cfg.EndTime.IsZero() {
t = time.AfterFunc(cfg.EndTime.Sub(time.Now()), func() {
vmenv.OverTimeAbort()
})
}
_, gas, failed, err, vmerr := ApplyMessage(accountDB, vmenv, action, gp, gasPrice, gasPayer, assetID, config, p.engine)
if false == cfg.EndTime.IsZero() {
//close timer
t.Stop()
}
if err != nil {
return nil, 0, err
}
*usedGas += gas
totalGas += gas
var status uint64
if failed {
status = types.ReceiptStatusFailed
} else {
status = types.ReceiptStatusSuccessful
}
vmerrstr := ""
if vmerr != nil {
vmerrstr = vmerr.Error()
log.Debug("processer apply transaction ", "hash", tx.Hash(), "err", vmerrstr)
}
var gasAllot []*types.GasDistribution
for key, gas := range vmenv.FounderGasMap {
gasAllot = append(gasAllot, &types.GasDistribution{Account: key.ObjectName.String(), Gas: uint64(gas.Value), TypeID: gas.TypeID})
}
ios = append(ios, &types.ActionResult{Status: status, Index: uint64(i), GasUsed: gas, GasAllot: gasAllot, Error: vmerrstr})
internalTxLog := make([]*types.InternalAction, 0, len(vmenv.InternalTxs))
for _, internalAction := range vmenv.InternalTxs {
internalAction.Action.SetHash(action.Hash())
internalTxLog = append(internalTxLog, internalAction)
}
detailActions = append(detailActions, &types.DetailAction{InternalActions: internalTxLog})
}
root := statedb.ReceiptRoot()
receipt := types.NewReceipt(root[:], *usedGas, totalGas)
receipt.TxHash = tx.Hash()
receipt.ActionResults = ios
// Set the receipt logs and create a bloom for filtering
receipt.Logs = statedb.GetLogs(tx.Hash())
receipt.Bloom = types.CreateBloom([]*types.Receipt{receipt})
detailTx.TxHash = receipt.TxHash
detailTx.Actions = detailActions
receipt.SetInternalTxsLog(detailTx)
return receipt, totalGas, nil
}
func needCheckSign(accountDB *accountmanager.AccountManager, action *types.Action) bool {
authorVersion := types.GetAuthorCache(action)
if len(authorVersion) == 0 {
return true
}
for name, version := range authorVersion {
if tmpVersion, err := accountDB.GetAuthorVersion(name); err != nil || version != tmpVersion {
return true
}
}
return false
}