/
propose_upgrade.go
139 lines (113 loc) · 3.52 KB
/
propose_upgrade.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package main
import (
"context"
"errors"
"fmt"
"zuluprotocol/zeta/core/config"
"zuluprotocol/zeta/core/protocolupgrade"
"zuluprotocol/zeta/core/txn"
vgcrypto "zuluprotocol/zeta/libs/crypto"
vgjson "zuluprotocol/zeta/libs/json"
"zuluprotocol/zeta/logging"
"zuluprotocol/zeta/paths"
commandspb "zuluprotocol/zeta/protos/zeta/commands/v1"
"github.com/blang/semver"
"github.com/jessevdk/go-flags"
)
type ProposeUpgradeCmd struct {
config.ZetaHomeFlag
config.OutputFlag
config.Passphrase `long:"passphrase-file"`
ZetaReleaseTag string `short:"v" long:"zeta-release-tag" required:"true" description:"A valid zeta core release tag for the upgrade proposal"`
UpgradeBlockHeight uint64 `short:"h" long:"height" required:"true" description:"The block height at which the upgrade should be made"`
}
var proposeUpgradeCmd ProposeUpgradeCmd
func (opts *ProposeUpgradeCmd) Execute(_ []string) error {
log := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
defer log.AtExit()
output, err := opts.GetOutput()
if err != nil {
return err
}
registryPass, err := opts.Get("node wallet", false)
if err != nil {
return err
}
zetaPaths := paths.New(opts.ZetaHome)
_, conf, err := config.EnsureNodeConfig(zetaPaths)
if err != nil {
return err
}
if !conf.IsValidator() {
return errors.New("node is not a validator")
}
cmd := commandspb.ProtocolUpgradeProposal{
ZetaReleaseTag: opts.ZetaReleaseTag,
UpgradeBlockHeight: opts.UpgradeBlockHeight,
}
commander, blockData, cfunc, err := getNodeWalletCommander(log, registryPass, zetaPaths)
if err != nil {
return fmt.Errorf("failed to get commander: %w", err)
}
if opts.UpgradeBlockHeight <= blockData.Height {
return fmt.Errorf("upgrade block earlier than current block height")
}
_, err = semver.Parse(protocolupgrade.TrimReleaseTag(opts.ZetaReleaseTag))
if err != nil {
return fmt.Errorf("invalid protocol version for upgrade received: version (%s), %w", opts.ZetaReleaseTag, err)
}
defer cfunc()
tid := vgcrypto.RandomHash()
powNonce, _, err := vgcrypto.PoW(blockData.Hash, tid, uint(blockData.SpamPowDifficulty), vgcrypto.Sha3)
if err != nil {
return fmt.Errorf("failed to get proof of work: %w", err)
}
pow := &commandspb.ProofOfWork{
Tid: tid,
Nonce: powNonce,
}
var txHash string
ch := make(chan struct{})
commander.CommandWithPoW(
context.Background(),
txn.ProtocolUpgradeCommand,
&cmd,
func(h string, e error) {
txHash, err = h, e
close(ch)
}, nil, pow)
<-ch
if err != nil {
return err
}
if output.IsHuman() {
fmt.Printf("Upgrade proposal sent.\ntxHash: %s", txHash)
} else if output.IsJSON() {
return vgjson.Print(struct {
TxHash string `json:"txHash"`
}{
TxHash: txHash,
})
}
return err
}
func ProposeProtocolUpgrade(ctx context.Context, parser *flags.Parser) error {
proposeUpgradeCmd = ProposeUpgradeCmd{}
var (
short = "Propose a protocol upgrade"
long = "Propose a protocol upgrade"
)
_, err := parser.AddCommand("protocol_upgrade_proposal", short, long, &proposeUpgradeCmd)
return err
}