-
Notifications
You must be signed in to change notification settings - Fork 5
/
templatevalidator.go
181 lines (163 loc) · 6.22 KB
/
templatevalidator.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
package blockmgr
import (
"fmt"
"github.com/drep-project/DREP-Chain/chain/block"
"github.com/drep-project/DREP-Chain/chain/utils"
"github.com/drep-project/DREP-Chain/chain/transactions"
"github.com/drep-project/DREP-Chain/chain"
"github.com/drep-project/DREP-Chain/crypto"
"github.com/drep-project/DREP-Chain/types"
//"github.com/ethereum/go-ethereum/common"
"math/big"
"time"
)
type TemplateBlockValidator struct {
chain chain.ChainServiceInterface
}
func NewTemplateBlockValidator(chain chain.ChainServiceInterface) *TemplateBlockValidator {
return &TemplateBlockValidator{chain}
}
func (chainBlockValidator *TemplateBlockValidator) VerifyHeader(header, parent *types.BlockHeader) error {
return nil
}
func (chainBlockValidator *TemplateBlockValidator) VerifyBody(block *types.Block) error {
return nil
}
func (chainBlockValidator *TemplateBlockValidator) ExecuteBlock(context *block.BlockExecuteContext, blockInterval int) error {
context.Receipts = make([]*types.Receipt, context.Block.Data.TxCount)
context.Logs = make([]*types.Log, 0)
if len(context.Block.Data.TxList) < 0 {
return nil
}
finalTxs := make([]*types.Transaction, 0, len(context.Block.Data.TxList))
finalReceipts := make([]*types.Receipt, 0, len(context.Block.Data.TxList))
//time control
stopchanel := make(chan struct{}, 1)
//80%time ,use for get tx
timeout := 1000 * blockInterval * 8 / 10
fmt.Println("executeblock", blockInterval, timeout, time.Millisecond*time.Duration(timeout), time.Now())
tm := time.AfterFunc(time.Millisecond*time.Duration(timeout), func() {
fmt.Println("exec block timeout ", time.Now())
stopchanel <- struct{}{}
})
defer func() {
context.Block.Data.TxList = finalTxs
context.Block.Data.TxCount = uint64(len(finalTxs))
context.Receipts = finalReceipts
context.Block.Header.GasUsed = *context.GasUsed
context.Block.Header.TxRoot = chainBlockValidator.chain.DeriveMerkleRoot(finalTxs)
context.Block.Header.ReceiptRoot = chainBlockValidator.chain.DeriveReceiptRoot(finalReceipts)
context.Block.Header.Bloom = types.CreateBloom(finalReceipts)
}()
SELECT_TX:
for _, t := range context.Block.Data.TxList {
snap := context.TrieStore.CopyState()
backGp := *context.Gp
select {
case <-stopchanel:
break SELECT_TX
default:
receipt, gasUsed, err := chainBlockValidator.RouteTransaction(context, context.Gp, t)
if err == nil {
finalTxs = append(finalTxs, t)
finalReceipts = append(finalReceipts, receipt)
gasUsedBig := new(big.Int).SetUint64(gasUsed)
context.AddGasUsed(gasUsedBig)
gasFee := new(big.Int).Mul(gasUsedBig, t.GasPrice())
context.AddGasFee(gasFee)
} else if err == chain.ErrOutOfGas {
// return while out of gas
context.TrieStore.RevertState(snap)
context.Gp = &backGp
return nil
} else {
from, _ := t.From()
log.WithField("err", err).WithField("from", from.String()).WithField("tx nonce", t.Nonce()).Info("route tx")
//skip wrong tx
context.TrieStore.RevertState(snap)
context.Gp = &backGp
continue
}
}
}
tm.Stop()
return nil
}
func (chainBlockValidator *TemplateBlockValidator) RouteTransaction(context *block.BlockExecuteContext, gasPool *utils.GasPool, tx *types.Transaction) (*types.Receipt, uint64, error) {
//init transaction tx
from, err := tx.From()
if err != nil {
return nil, 0, err
}
txContext := transactions.NewExecuteTransactionContext(context, context.TrieStore, gasPool, from, tx)
if err := txContext.PreCheck(); err != nil {
return nil, 0, err
}
// Pay intrinsic gastx
gas, err := tx.IntrinsicGas()
if err != nil {
return nil, 0, err
}
if err = txContext.UseGas(gas); err != nil {
return nil, 0, err
}
process := &transactions.Processor{}
ret := process.ExecuteTransaction(txContext)
if ret.Txerror != nil {
return nil, 0, ret.Txerror
}
err = txContext.RefundCoin()
if err != nil {
return nil, 0, err
}
// Create a new receipt for the transaction, storing the intermediate root and gasRemained used by the tx
// based on the eip phase, we're passing whether the root touch-delete accounts.
receipt := types.NewReceipt(crypto.ZeroHash[:], ret.ContractTxExecuteFail, txContext.GasUsed())
receipt.TxHash = *tx.TxHash()
receipt.GasUsed = txContext.GasUsed()
// if the transaction created a contract, store the creation address in the receipt.
if (tx.To() == nil || tx.To().IsEmpty()) && tx.Type() == types.CreateContractType {
receipt.ContractAddress = crypto.CreateAddress(*from, tx.Nonce())
log.WithField("contractAddr:", receipt.ContractAddress).Info("execute contract")
}
// Set the receipt logs and create a bloom for filtering
receipt.Logs = ret.ContractTxLog
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
//receipt.BlockHash = *header.Hash()
receipt.BlockNumber = context.Block.Header.Height
return receipt, txContext.GasUsed(), nil
//exit := false
//for selector, txValidator := range chainBlockValidator.chain.TransactionValidators() {
// if selector.Select(tx) {
// exit = true
// ret := txValidator.ExecuteTransaction(txContext)
// if ret.Txerror != nil {
// return nil, 0, ret.Txerror
// }
// err = txContext.RefundCoin()
// if err != nil {
// return nil, 0, err
// }
// // Create a new receipt for the transaction, storing the intermediate root and gasRemained used by the tx
// // based on the eip phase, we're passing whether the root touch-delete accounts.
// receipt := types.NewReceipt(crypto.ZeroHash[:], ret.ContractTxExecuteFail, txContext.GasUsed())
// receipt.TxHash = *tx.TxHash()
// receipt.GasUsed = txContext.GasUsed()
// // if the transaction created a contract, store the creation address in the receipt.
// if (tx.To() == nil || tx.To().IsEmpty()) && tx.Type() == types.CreateContractType {
// receipt.ContractAddress = crypto.CreateAddress(*from, tx.Nonce())
// log.WithField("contractAddr:", receipt.ContractAddress).Info("execute contract")
// }
// // Set the receipt logs and create a bloom for filtering
// receipt.Logs = ret.ContractTxLog
// receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
// //receipt.BlockHash = *header.Hash()
// receipt.BlockNumber = context.Block.Header.Height
// return receipt, txContext.GasUsed(), nil
// }
//}
//if !exit {
// return nil, 0, chain.ErrUnsupportTxType
//}
panic("never come here")
}