Skip to content

Commit aa1d98d

Browse files
authored
Merge branch 'main' into sanaz/blocksync-commit-check
2 parents 85a5a6d + 2ca5a77 commit aa1d98d

15 files changed

Lines changed: 3139 additions & 18 deletions

File tree

‎cmd/seid/cmd/root.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"time"
1212

13+
evmonlycli "github.com/sei-protocol/sei-chain/giga/evmonly/client/cli"
14+
1315
"github.com/sei-protocol/sei-chain/admin"
1416
"github.com/sei-protocol/sei-chain/app"
1517
"github.com/sei-protocol/sei-chain/app/params"
@@ -160,6 +162,7 @@ func initRootCmd(
160162
ReplayCmd(app.DefaultNodeHome),
161163
BlocktestCmd(app.DefaultNodeHome),
162164
)
165+
evmonlycli.Route(rootCmd)
163166
}
164167

165168
// queryCommand returns the sub-command to send queries to the app

‎giga/evmonly/client/cli/convert.go‎

Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"math/big"
7+
"strings"
8+
"time"
9+
10+
"github.com/gogo/protobuf/proto"
11+
12+
"github.com/ethereum/go-ethereum/accounts/abi"
13+
"github.com/ethereum/go-ethereum/common"
14+
15+
codectypes "github.com/sei-protocol/sei-chain/sei-cosmos/codec/types"
16+
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
17+
govtypes "github.com/sei-protocol/sei-chain/sei-cosmos/x/gov/types"
18+
paramproposal "github.com/sei-protocol/sei-chain/sei-cosmos/x/params/types/proposal"
19+
upgradetypes "github.com/sei-protocol/sei-chain/sei-cosmos/x/upgrade/types"
20+
)
21+
22+
// baseDenom is the only denom an EVM-only chain's deposits are in.
23+
const baseDenom = "usei"
24+
25+
// decPrecision is the decimal places of a 1e18-scaled ratio or weight.
26+
const decPrecision = 18
27+
28+
var weiPerUsei = big.NewInt(1_000_000_000_000)
29+
30+
// Content types of the gov precompile's ProposalData.contentType.
31+
const (
32+
contentText uint8 = iota
33+
contentSoftwareUpgrade
34+
contentCancelSoftwareUpgrade
35+
contentParamChange
36+
)
37+
38+
// Go shapes of the gov and upgrade precompiles' ABI structs.
39+
type (
40+
proposalData struct {
41+
Id uint64
42+
Status int32
43+
FinalTallyResult tallyData
44+
SubmitTime uint64
45+
DepositEndTime uint64
46+
TotalDeposit *big.Int
47+
VotingStartTime uint64
48+
VotingEndTime uint64
49+
IsExpedited bool
50+
ContentType uint8
51+
Content []byte
52+
}
53+
tallyData struct {
54+
Yes *big.Int
55+
Abstain *big.Int
56+
No *big.Int
57+
NoWithVeto *big.Int
58+
}
59+
weightedOption struct {
60+
Option int32
61+
Weight *big.Int
62+
}
63+
voteData struct {
64+
ProposalId uint64
65+
Voter common.Address
66+
Options []weightedOption
67+
}
68+
depositData struct {
69+
ProposalId uint64
70+
Depositor common.Address
71+
Amount *big.Int
72+
}
73+
govParamsData struct {
74+
VotingPeriod uint64
75+
ExpeditedVotingPeriod uint64
76+
MinDeposit *big.Int
77+
MaxDepositPeriod uint64
78+
MinExpeditedDeposit *big.Int
79+
Quorum *big.Int
80+
Threshold *big.Int
81+
VetoThreshold *big.Int
82+
ExpeditedQuorum *big.Int
83+
ExpeditedThreshold *big.Int
84+
}
85+
upgradePlan struct {
86+
Name string
87+
Height uint64
88+
Info string
89+
}
90+
paramChange struct {
91+
Subspace string
92+
Key string
93+
Value []byte
94+
}
95+
textContent struct {
96+
Title string
97+
Description string
98+
IsExpedited bool
99+
}
100+
softwareUpgradeContent struct {
101+
Title string
102+
Description string
103+
Plan upgradePlan
104+
IsExpedited bool
105+
}
106+
cancelSoftwareUpgradeContent struct {
107+
Title string
108+
Description string
109+
Name string
110+
IsExpedited bool
111+
}
112+
paramChangeContent struct {
113+
Title string
114+
Description string
115+
Changes []paramChange
116+
IsExpedited bool
117+
}
118+
)
119+
120+
var (
121+
stringArg = abi.ArgumentMarshaling{Type: "string"}
122+
planArg = abi.ArgumentMarshaling{Name: "plan", Type: "tuple", Components: []abi.ArgumentMarshaling{named("name", stringArg), {Name: "height", Type: "uint64"}, named("info", stringArg)}}
123+
changesArg = abi.ArgumentMarshaling{Name: "changes", Type: "tuple[]", Components: []abi.ArgumentMarshaling{named("subspace", stringArg), named("key", stringArg), {Name: "value", Type: "bytes"}}}
124+
expArg = abi.ArgumentMarshaling{Name: "isExpedited", Type: "bool"}
125+
126+
textArgs = contentArgs(named("title", stringArg), named("description", stringArg), expArg)
127+
softwareUpgArgs = contentArgs(named("title", stringArg), named("description", stringArg), planArg, expArg)
128+
cancelUpgradeArgs = contentArgs(named("title", stringArg), named("description", stringArg), named("name", stringArg), expArg)
129+
paramChangeArgs = contentArgs(named("title", stringArg), named("description", stringArg), changesArg, expArg)
130+
)
131+
132+
func named(name string, arg abi.ArgumentMarshaling) abi.ArgumentMarshaling {
133+
arg.Name = name
134+
return arg
135+
}
136+
137+
// contentArgs are the arguments of the abi.encode of a content struct.
138+
func contentArgs(fields ...abi.ArgumentMarshaling) abi.Arguments {
139+
t, err := abi.NewType("tuple", "", fields)
140+
if err != nil {
141+
panic(err)
142+
}
143+
return abi.Arguments{{Type: t}}
144+
}
145+
146+
// decodeContentAs decodes bz, the abi.encode of a content struct, into T.
147+
func decodeContentAs[T any](args abi.Arguments, bz []byte) (T, error) {
148+
var out T
149+
vals, err := args.Unpack(bz)
150+
if err != nil {
151+
return out, err
152+
}
153+
return *abi.ConvertType(vals[0], new(T)).(*T), nil
154+
}
155+
156+
// v2Content returns a proposal's content as V2's proposal content type.
157+
func v2Content(contentType uint8, bz []byte) (govtypes.Content, error) {
158+
switch contentType {
159+
case contentText:
160+
c, err := decodeContentAs[textContent](textArgs, bz)
161+
if err != nil {
162+
return nil, err
163+
}
164+
return &govtypes.TextProposal{Title: c.Title, Description: c.Description, IsExpedited: c.IsExpedited}, nil
165+
case contentSoftwareUpgrade:
166+
c, err := decodeContentAs[softwareUpgradeContent](softwareUpgArgs, bz)
167+
if err != nil {
168+
return nil, err
169+
}
170+
height, err := toInt64(c.Plan.Height)
171+
if err != nil {
172+
return nil, err
173+
}
174+
return &upgradetypes.SoftwareUpgradeProposal{
175+
Title: c.Title,
176+
Description: c.Description,
177+
Plan: upgradetypes.Plan{Name: c.Plan.Name, Height: height, Info: c.Plan.Info},
178+
}, nil
179+
case contentCancelSoftwareUpgrade:
180+
c, err := decodeContentAs[cancelSoftwareUpgradeContent](cancelUpgradeArgs, bz)
181+
if err != nil {
182+
return nil, err
183+
}
184+
return &upgradetypes.CancelSoftwareUpgradeProposal{Title: c.Title, Description: c.Description}, nil
185+
case contentParamChange:
186+
c, err := decodeContentAs[paramChangeContent](paramChangeArgs, bz)
187+
if err != nil {
188+
return nil, err
189+
}
190+
changes, err := v2ParamChanges(c.Changes)
191+
if err != nil {
192+
return nil, err
193+
}
194+
return &paramproposal.ParameterChangeProposal{
195+
Title: c.Title,
196+
Description: c.Description,
197+
Changes: changes,
198+
IsExpedited: c.IsExpedited,
199+
}, nil
200+
default:
201+
return nil, fmt.Errorf("unknown proposal content type %d", contentType)
202+
}
203+
}
204+
205+
// v2Proposal returns p as V2's Proposal.
206+
// cancelPlanName returns the plan a cancel-software-upgrade proposal cancels,
207+
// and false for any other proposal.
208+
func cancelPlanName(p proposalData) (string, bool, error) {
209+
if p.ContentType != contentCancelSoftwareUpgrade {
210+
return "", false, nil
211+
}
212+
c, err := decodeContentAs[cancelSoftwareUpgradeContent](cancelUpgradeArgs, p.Content)
213+
if err != nil {
214+
return "", false, fmt.Errorf("proposal %d: %w", p.Id, err)
215+
}
216+
return c.Name, true, nil
217+
}
218+
219+
func v2Proposal(p proposalData) (govtypes.Proposal, error) {
220+
content, err := v2Content(p.ContentType, p.Content)
221+
if err != nil {
222+
return govtypes.Proposal{}, fmt.Errorf("proposal %d: %w", p.Id, err)
223+
}
224+
msg, ok := content.(proto.Message)
225+
if !ok {
226+
return govtypes.Proposal{}, fmt.Errorf("proposal %d: content %T is not a proto message", p.Id, content)
227+
}
228+
contentAny, err := codectypes.NewAnyWithValue(msg)
229+
if err != nil {
230+
return govtypes.Proposal{}, err
231+
}
232+
return govtypes.Proposal{
233+
ProposalId: p.Id,
234+
Content: contentAny,
235+
Status: govtypes.ProposalStatus(p.Status),
236+
FinalTallyResult: v2Tally(p.FinalTallyResult),
237+
SubmitTime: unixNano(p.SubmitTime),
238+
DepositEndTime: unixNano(p.DepositEndTime),
239+
TotalDeposit: weiToCoins(p.TotalDeposit),
240+
VotingStartTime: unixNano(p.VotingStartTime),
241+
VotingEndTime: unixNano(p.VotingEndTime),
242+
IsExpedited: p.IsExpedited,
243+
}, nil
244+
}
245+
246+
func v2Tally(t tallyData) govtypes.TallyResult {
247+
return govtypes.TallyResult{
248+
Yes: sdk.NewIntFromBigInt(orZero(t.Yes)),
249+
Abstain: sdk.NewIntFromBigInt(orZero(t.Abstain)),
250+
No: sdk.NewIntFromBigInt(orZero(t.No)),
251+
NoWithVeto: sdk.NewIntFromBigInt(orZero(t.NoWithVeto)),
252+
}
253+
}
254+
255+
// v2Vote returns v as V2's Vote, with the voter as a 0x address.
256+
func v2Vote(v voteData) govtypes.Vote {
257+
options := make([]govtypes.WeightedVoteOption, len(v.Options))
258+
for i, o := range v.Options {
259+
options[i] = govtypes.WeightedVoteOption{Option: govtypes.VoteOption(o.Option), Weight: decFromScaled(o.Weight)}
260+
}
261+
out := govtypes.Vote{ProposalId: v.ProposalId, Voter: v.Voter.Hex(), Options: options}
262+
if len(options) == 1 {
263+
out.Option = options[0].Option //nolint:staticcheck // V2 still fills the deprecated single option
264+
}
265+
return out
266+
}
267+
268+
// v2Deposit returns d as V2's Deposit, with the depositor as a 0x address.
269+
func v2Deposit(d depositData) govtypes.Deposit {
270+
return govtypes.Deposit{ProposalId: d.ProposalId, Depositor: d.Depositor.Hex(), Amount: weiToCoins(d.Amount)}
271+
}
272+
273+
// v2GovParams returns p as V2's gov Params.
274+
func v2GovParams(p govParamsData) (govtypes.Params, error) {
275+
voting, err := toDuration(p.VotingPeriod)
276+
if err != nil {
277+
return govtypes.Params{}, err
278+
}
279+
expeditedVoting, err := toDuration(p.ExpeditedVotingPeriod)
280+
if err != nil {
281+
return govtypes.Params{}, err
282+
}
283+
maxDeposit, err := toDuration(p.MaxDepositPeriod)
284+
if err != nil {
285+
return govtypes.Params{}, err
286+
}
287+
return govtypes.Params{
288+
VotingParams: govtypes.VotingParams{VotingPeriod: voting, ExpeditedVotingPeriod: expeditedVoting},
289+
TallyParams: govtypes.TallyParams{
290+
Quorum: decFromScaled(p.Quorum),
291+
Threshold: decFromScaled(p.Threshold),
292+
VetoThreshold: decFromScaled(p.VetoThreshold),
293+
ExpeditedQuorum: decFromScaled(p.ExpeditedQuorum),
294+
ExpeditedThreshold: decFromScaled(p.ExpeditedThreshold),
295+
},
296+
DepositParams: govtypes.DepositParams{
297+
MinDeposit: weiToCoins(p.MinDeposit),
298+
MaxDepositPeriod: maxDeposit,
299+
MinExpeditedDeposit: weiToCoins(p.MinExpeditedDeposit),
300+
},
301+
}, nil
302+
}
303+
304+
// depositWei returns coins, a V2 coin string of usei, in wei. An empty string
305+
// is zero.
306+
func depositWei(coins string) (*big.Int, error) {
307+
if strings.TrimSpace(coins) == "" {
308+
return new(big.Int), nil
309+
}
310+
parsed, err := sdk.ParseCoinsNormalized(coins)
311+
if err != nil {
312+
return nil, err
313+
}
314+
for _, c := range parsed {
315+
if c.Denom != baseDenom {
316+
return nil, fmt.Errorf("deposit %q: only %s is accepted", coins, baseDenom)
317+
}
318+
}
319+
usei := parsed.AmountOf(baseDenom).BigInt()
320+
return usei.Mul(usei, weiPerUsei), nil
321+
}
322+
323+
// weiToCoins returns wei as usei coins, dropping any fraction of a usei.
324+
func weiToCoins(wei *big.Int) sdk.Coins {
325+
usei := new(big.Int).Quo(orZero(wei), weiPerUsei)
326+
if usei.Sign() == 0 {
327+
return sdk.Coins{}
328+
}
329+
return sdk.NewCoins(sdk.NewCoin(baseDenom, sdk.NewIntFromBigInt(usei)))
330+
}
331+
332+
// decFromScaled returns a 1e18-scaled integer as a Dec.
333+
func decFromScaled(v *big.Int) sdk.Dec {
334+
return sdk.NewDecFromBigIntWithPrec(orZero(v), decPrecision)
335+
}
336+
337+
// scaledFromDec returns d as a 1e18-scaled integer.
338+
func scaledFromDec(d sdk.Dec) *big.Int {
339+
return d.BigInt()
340+
}
341+
342+
// unixNano returns a Unix nanosecond time, with 0 as the zero time.
343+
func unixNano(ns uint64) time.Time {
344+
if ns == 0 || ns > math.MaxInt64 {
345+
return time.Time{}
346+
}
347+
return time.Unix(0, int64(ns)).UTC()
348+
}
349+
350+
func toDuration(ns uint64) (time.Duration, error) {
351+
if ns > math.MaxInt64 {
352+
return 0, fmt.Errorf("duration %dns overflows", ns)
353+
}
354+
return time.Duration(ns), nil
355+
}
356+
357+
func toInt64(v uint64) (int64, error) {
358+
if v > math.MaxInt64 {
359+
return 0, fmt.Errorf("%d overflows int64", v)
360+
}
361+
return int64(v), nil
362+
}
363+
364+
func orZero(v *big.Int) *big.Int {
365+
if v == nil {
366+
return new(big.Int)
367+
}
368+
return v
369+
}

0 commit comments

Comments
 (0)