/
valset_update.go
191 lines (167 loc) · 5.58 KB
/
valset_update.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package peggy
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
log "github.com/xlab/suplog"
"github.com/InjectiveLabs/sdk-go/chain/peggy/types"
)
func (s *peggyContract) SendEthValsetUpdate(
ctx context.Context,
oldValset *types.Valset,
newValset *types.Valset,
confirms []*types.MsgValsetConfirm,
) (*common.Hash, error) {
if newValset.Nonce <= oldValset.Nonce {
err := errors.New("new valset nonce should be greater than old valset nonce")
return nil, err
}
log.WithFields(log.Fields{
"old_nonce": oldValset.Nonce,
"new_nonce": newValset.Nonce,
}).Infoln("Checking signatures and submitting validator set update to Ethereum")
newValidators, newPowers := validatorsAndPowers(newValset)
newValsetNonce := new(big.Int).SetUint64(newValset.Nonce)
// we need to use the old valset here because our signatures need to match the current
// members of the validator set in the contract.
currentValidators, currentPowers, sigV, sigR, sigS, err := checkValsetSigsAndRepack(oldValset, confirms)
if err != nil {
err = errors.Wrap(err, "confirmations check failed")
return nil, err
}
currentValsetNonce := new(big.Int).SetUint64(oldValset.Nonce)
// Solidity function signature
// function updateValset(
// // The new version of the validator set
// address[] memory _newValidators,
// uint256[] memory _newPowers,
// uint256 _newValsetNonce,
//
// // The current validators that approve the change
// address[] memory _currentValidators,
// uint256[] memory _currentPowers,
// uint256 _currentValsetNonce,
//
// // These are arrays of the parts of the current validator's signatures
// uint8[] memory _v,
// bytes32[] memory _r,
// bytes32[] memory _s
// )
log.Debugln("Sending updateValset Ethereum tx", "currentValidators", currentValidators, "currentPowers", currentPowers, "currentValsetNonce", currentValsetNonce)
txData, err := peggyABI.Pack("updateValset",
newValidators,
newPowers,
newValsetNonce,
currentValidators,
currentPowers,
currentValsetNonce,
sigV,
sigR,
sigS,
)
if err != nil {
log.WithError(err).Errorln("ABI Pack Peggy [updateValset] method")
return nil, err
}
txHash, err := s.EVMCommitter.SendTx(s.peggyAddress, txData)
if err != nil {
log.WithError(err).WithField("tx_hash", txHash.Hex()).Errorln("Failed to sign and submit (Peggy updateValset) to EVM")
return nil, err
}
log.Infoln("Sent Tx (Peggy updateValset):", txHash.Hex())
// let before_nonce = get_valset_nonce(peggy_contract_address, eth_address, web3).await?;
// if before_nonce != old_nonce {
// info!(
// "Someone else updated the valset to {}, exiting early",
// before_nonce
// );
// return Ok(());
// }
// let tx = web3
// .send_transaction(
// peggy_contract_address,
// payload,
// 0u32.into(),
// eth_address,
// our_eth_key,
// vec![SendTxOption::GasLimit(1_000_000u32.into())],
// )
// .await?;
// info!("Sent valset update with txid {:#066x}", tx);
// // TODO this segment of code works around the race condition for submitting valsets mostly
// // by not caring if our own submission reverts and only checking if the valset has been updated
// // period not if our update succeeded in particular. This will require some further consideration
// // in the future as many independent relayers racing to update the same thing will hopefully
// // be the common case.
// web3.wait_for_transaction(tx, timeout, None).await?;
// let last_nonce = get_valset_nonce(peggy_contract_address, eth_address, web3).await?;
// if last_nonce != new_nonce {
// error!(
// "Current nonce is {} expected to update to nonce {}",
// last_nonce, new_nonce
// );
// } else {
// info!(
// "Successfully updated Valset with new Nonce {:?}",
// last_nonce
// );
// }
// Ok(())
return &txHash, nil
}
func validatorsAndPowers(valset *types.Valset) (
validators []common.Address,
powers []*big.Int,
) {
for _, m := range valset.Members {
mPower := big.NewInt(0).SetUint64(m.Power)
validators = append(validators, common.HexToAddress(m.EthereumAddress))
powers = append(powers, mPower)
}
return
}
func checkValsetSigsAndRepack(
valset *types.Valset,
confirms []*types.MsgValsetConfirm,
) (
validators []common.Address,
powers []*big.Int,
v []uint8,
r []common.Hash,
s []common.Hash,
err error,
) {
if len(confirms) == 0 {
err = errors.New("no signatures in valset confirmation")
return
}
signerToSig := make(map[string]*types.MsgValsetConfirm, len(confirms))
for _, sig := range confirms {
signerToSig[sig.EthAddress] = sig
}
powerOfGoodSigs := new(big.Int)
for _, m := range valset.Members {
mPower := big.NewInt(0).SetUint64(m.Power)
if sig, ok := signerToSig[m.EthereumAddress]; ok && sig.EthAddress == m.EthereumAddress {
powerOfGoodSigs.Add(powerOfGoodSigs, mPower)
validators = append(validators, common.HexToAddress(m.EthereumAddress))
powers = append(powers, mPower)
sigV, sigR, sigS := sigToVRS(sig.Signature)
v = append(v, sigV)
r = append(r, sigR)
s = append(s, sigS)
} else {
validators = append(validators, common.HexToAddress(m.EthereumAddress))
powers = append(powers, mPower)
v = append(v, 0)
r = append(r, [32]byte{})
s = append(s, [32]byte{})
}
}
if peggyPowerToPercent(powerOfGoodSigs) < 66 {
err = ErrInsufficientVotingPowerToPass
return
}
return
}