-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgenerate.go
117 lines (97 loc) · 3.28 KB
/
generate.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
package main
import (
_ "embed"
"os"
"reflect"
"runtime"
"sort"
"strings"
"text/template"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
assetfttypes "github.com/CoreumFoundation/coreum/x/asset/ft/types"
"github.com/CoreumFoundation/coreum/x/deterministicgas"
)
//go:generate go run . ./README.md
var (
//go:embed README.tmpl.md
readmeTmpl string
)
func main() {
type determMsg struct {
Type deterministicgas.MsgURL
Gas uint64
}
var (
determMsgs []determMsg
nonDetermMsgURLs []deterministicgas.MsgURL
determSpeicialCaseMsgURLs []deterministicgas.MsgURL
)
cfg := deterministicgas.DefaultConfig()
for msgURL, gasFunc := range cfg.GasByMessageMap() {
fnFullName := runtime.FuncForPC(reflect.ValueOf(gasFunc).Pointer()).Name()
fnParts := strings.Split(fnFullName, "/")
fnShortName := fnParts[len(fnParts)-1]
if fnShortName == "deterministicgas.nondeterministicGasFunc" {
nonDetermMsgURLs = append(nonDetermMsgURLs, msgURL)
continue
}
gas, ok := gasFunc(nil)
// gasFunc returns ok equal to true only for deterministic messages which are not special cases.
// For special cases it returns false because type-casting to a specific message type inside function fails.
if ok {
determMsgs = append(determMsgs, determMsg{msgURL, gas})
} else {
determSpeicialCaseMsgURLs = append(determSpeicialCaseMsgURLs, msgURL)
}
}
sort.Slice(determMsgs, func(i, j int) bool {
return determMsgs[i].Type < determMsgs[j].Type
})
sort.Slice(determSpeicialCaseMsgURLs, func(i, j int) bool {
return determSpeicialCaseMsgURLs[i] < determSpeicialCaseMsgURLs[j]
})
sort.Slice(nonDetermMsgURLs, func(i, j int) bool {
return nonDetermMsgURLs[i] < nonDetermMsgURLs[j]
})
msgIssueGasPrice, _ := cfg.GasRequiredByMessage(&assetfttypes.MsgIssue{})
generatorComment := `[//]: # (GENERATED DOC.)
[//]: # (DO NOT EDIT MANUALLY!!!)`
file, err := os.OpenFile(os.Args[1], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
err = template.Must(template.New("README.md").Parse(readmeTmpl)).Execute(file, struct {
GeneratorComment string
SigVerifyCost uint64
TxSizeCostPerByte uint64
FixedGas uint64
FreeBytes uint64
FreeSignatures uint64
MsgIssueGasPrice uint64
BankSendPerCoinGas uint64
BankMultiSendPerOperationsGas uint64
AuthzExecOverhead uint64
DetermMsgsSpecialCases []deterministicgas.MsgURL
DetermMsgs []determMsg
NonDetermMsgs []deterministicgas.MsgURL
DeterministicMessagesTable string
NonDeterministicMessagesTable string
}{
GeneratorComment: generatorComment,
FixedGas: cfg.FixedGas,
SigVerifyCost: auth.DefaultSigVerifyCostSecp256k1,
TxSizeCostPerByte: auth.DefaultTxSizeCostPerByte,
FreeBytes: cfg.FreeBytes,
FreeSignatures: cfg.FreeSignatures,
MsgIssueGasPrice: msgIssueGasPrice,
BankSendPerCoinGas: deterministicgas.BankSendPerCoinGas,
BankMultiSendPerOperationsGas: deterministicgas.BankMultiSendPerOperationsGas,
AuthzExecOverhead: deterministicgas.AuthzExecOverhead,
DetermMsgsSpecialCases: determSpeicialCaseMsgURLs,
DetermMsgs: determMsgs,
NonDetermMsgs: nonDetermMsgURLs,
})
if err != nil {
panic(err)
}
}