-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
81 lines (68 loc) · 2.03 KB
/
context.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
package proxy
import (
"sync"
"time"
clientcontext "github.com/FiboChain/fbc/libs/cosmos-sdk/client/context"
"github.com/FiboChain/fbc/libs/cosmos-sdk/store"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/params"
abci "github.com/FiboChain/fbc/libs/tendermint/abci/types"
tmlog "github.com/FiboChain/fbc/libs/tendermint/libs/log"
dbm "github.com/FiboChain/fbc/libs/tm-db"
evmwatcher "github.com/FiboChain/fbc/x/evm/watcher"
"github.com/ethereum/go-ethereum/common"
)
const (
simulationGasLimit = 3000000
)
var clientCtx clientcontext.CLIContext
func SetCliContext(ctx clientcontext.CLIContext) {
clientCtx = ctx
}
func MakeContext(storeKey sdk.StoreKey) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
authKey := sdk.NewKVStoreKey(auth.StoreKey)
paramsKey := sdk.NewKVStoreKey(params.StoreKey)
paramsTKey := sdk.NewTransientStoreKey(params.TStoreKey)
cms.MountStoreWithDB(authKey, sdk.StoreTypeIAVL, db)
cms.MountStoreWithDB(paramsKey, sdk.StoreTypeIAVL, db)
cms.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
cms.MountStoreWithDB(paramsTKey, sdk.StoreTypeTransient, db)
err := cms.LoadLatestVersion()
if err != nil {
panic(err)
}
header := getHeader()
ctx := sdk.NewContext(cms, header, true, tmlog.NewNopLogger())
ctx.SetGasMeter(sdk.NewGasMeter(simulationGasLimit))
return ctx
}
var (
qOnce sync.Once
evmQuerier *evmwatcher.Querier
)
func getHeader() abci.Header {
qOnce.Do(func() {
evmQuerier = evmwatcher.NewQuerier()
})
timestamp := time.Now()
latest, _ := evmQuerier.GetLatestBlockNumber()
hash, e := evmQuerier.GetBlockHashByNumber(latest)
if e != nil {
hash = common.HexToHash("0x000000000000000000000000000000")
}
block, e := evmQuerier.GetBlockByHash(hash, false)
if e == nil {
timestamp = time.Unix(int64(block.Timestamp), 0)
}
header := abci.Header{
LastBlockId: abci.BlockID{
Hash: hash.Bytes(),
},
Height: int64(latest),
Time: timestamp,
}
return header
}