-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
80 lines (67 loc) · 2.02 KB
/
common.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
package base
import (
"math/big"
"sync"
"github.com/ethereum/go-ethereum/common"
ethermint "github.com/brc20-collab/brczero/app/types"
sdk "github.com/brc20-collab/brczero/libs/cosmos-sdk/types"
tmtypes "github.com/brc20-collab/brczero/libs/tendermint/types"
"github.com/brc20-collab/brczero/x/evm/types"
)
var commitStateDBPool = &sync.Pool{
New: func() interface{} {
return &types.CommitStateDB{GuFactor: types.DefaultGuFactor}
},
}
func getCommitStateDB() *types.CommitStateDB {
return commitStateDBPool.Get().(*types.CommitStateDB)
}
func putCommitStateDB(st *types.CommitStateDB) {
commitStateDBPool.Put(st)
}
func msg2st(ctx *sdk.Context, k *Keeper, msg *types.MsgEthereumTx, st *types.StateTransition) (reuseCsdb bool, err error) {
var chainIDEpoch *big.Int
chainIDEpoch, err = ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return
}
var sender common.Address
// Verify signature and retrieve sender address
sender, err = getSender(ctx, chainIDEpoch, msg)
if err != nil {
return
}
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ethHash := common.BytesToHash(txHash)
st.AccountNonce = msg.Data.AccountNonce
st.Price = msg.Data.Price
st.GasLimit = msg.Data.GasLimit
st.Recipient = msg.Data.Recipient
st.Amount = msg.Data.Amount
st.Payload = msg.Data.Payload
st.ChainID = chainIDEpoch
st.TxHash = ðHash
st.Sender = sender
st.Simulate = ctx.IsCheckTx()
st.TraceTx = ctx.IsTraceTx()
st.TraceTxLog = ctx.IsTraceTxLog()
st.SetCallToCM(k.GetCallToCM())
// TODO disable reuse csdb by scf, need discuss reuse csdb in deliver mode
csdb := getCommitStateDB()
types.ResetCommitStateDB(csdb, k.GenerateCSDBParams(), ctx)
st.Csdb = csdb
reuseCsdb = true
return
}
func getSender(ctx *sdk.Context, chainIDEpoch *big.Int, msg *types.MsgEthereumTx) (sender common.Address, err error) {
if ctx.IsCheckTx() {
if from := ctx.From(); len(from) > 0 {
return common.HexToAddress(from), nil
}
}
err = msg.VerifySig(chainIDEpoch, ctx.BlockHeight())
if err == nil {
sender = msg.EthereumAddress()
}
return
}