forked from jba123/evm-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txpool.go
94 lines (75 loc) · 2.14 KB
/
txpool.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
package state
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethState "github.com/ethereum/go-ethereum/core/state"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
"github.com/sirupsen/logrus"
)
type TxPool struct {
ethState *ethState.StateDB
signer ethTypes.Signer
chainConfig params.ChainConfig // vm.env is still tightly coupled with chainConfig
vmConfig vm.Config
gasLimit uint64
totalUsedGas uint64
gp *core.GasPool
logger *logrus.Logger
}
func NewTxPool(ethState *ethState.StateDB,
signer ethTypes.Signer,
chainConfig params.ChainConfig,
vmConfig vm.Config,
gasLimit uint64,
logger *logrus.Logger) *TxPool {
return &TxPool{
ethState: ethState,
signer: signer,
chainConfig: chainConfig,
vmConfig: vmConfig,
gasLimit: gasLimit,
logger: logger,
}
}
func (p *TxPool) Reset(root common.Hash) error {
err := p.ethState.Reset(root)
if err != nil {
return err
}
p.totalUsedGas = 0
p.gp = new(core.GasPool).AddGas(p.gasLimit)
return nil
}
func (p *TxPool) CheckTx(tx *ethTypes.Transaction) error {
msg, err := tx.AsMessage(p.signer)
if err != nil {
p.logger.WithError(err).Error("Converting Transaction to Message")
return err
}
context := vm.Context{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
GetHash: func(uint64) common.Hash { return common.Hash{} },
// Message information
Origin: msg.From(),
GasLimit: msg.Gas(),
GasPrice: msg.GasPrice(),
BlockNumber: big.NewInt(0), // The vm has a dependency on this.
}
// The EVM should never be reused and is not thread safe.
vmenv := vm.NewEVM(context, p.ethState, &p.chainConfig, p.vmConfig)
// Apply the transaction to the current state (included in the env)
_, gas, _, err := core.ApplyMessage(vmenv, msg, p.gp)
if err != nil {
p.logger.WithError(err).Error("Applying transaction to TxPool")
return err
}
p.totalUsedGas += gas
return nil
}
func (p *TxPool) GetNonce(addr common.Address) uint64 {
return p.ethState.GetNonce(addr)
}