cmd/evm/internal/t8ntool: minor refactorings

This commit is contained in:
Martin Holst Swende 2023-08-21 18:59:42 +02:00
parent 2b67330782
commit 9e578a5149
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -273,89 +273,69 @@ func (t *txWithKey) UnmarshalJSON(input []byte) error {
// and secondly to read them with the standard tx json format // and secondly to read them with the standard tx json format
func signUnsignedTransactions(txs []*txWithKey, signer types.Signer) (types.Transactions, error) { func signUnsignedTransactions(txs []*txWithKey, signer types.Signer) (types.Transactions, error) {
var signedTxs []*types.Transaction var signedTxs []*types.Transaction
for i, txWithKey := range txs { for i, tx := range txs {
tx := txWithKey.tx var (
key := txWithKey.key v, r, s = tx.tx.RawSignatureValues()
v, r, s := tx.RawSignatureValues() signed *types.Transaction
if key != nil && v.BitLen()+r.BitLen()+s.BitLen() == 0 { err error
// This transaction needs to be signed )
var ( if tx.key == nil || v.BitLen()+r.BitLen()+s.BitLen() != 0 {
signed *types.Transaction
err error
)
if txWithKey.protected {
signed, err = types.SignTx(tx, signer, key)
} else {
signed, err = types.SignTx(tx, types.FrontierSigner{}, key)
}
if err != nil {
return nil, NewError(ErrorJson, fmt.Errorf("tx %d: failed to sign tx: %v", i, err))
}
signedTxs = append(signedTxs, signed)
} else {
// Already signed // Already signed
signedTxs = append(signedTxs, tx) signedTxs = append(signedTxs, tx.tx)
continue
} }
// This transaction needs to be signed
if tx.protected {
signed, err = types.SignTx(tx.tx, signer, tx.key)
} else {
signed, err = types.SignTx(tx.tx, types.FrontierSigner{}, tx.key)
}
if err != nil {
return nil, NewError(ErrorJson, fmt.Errorf("tx %d: failed to sign tx: %v", i, err))
}
signedTxs = append(signedTxs, signed)
} }
return signedTxs, nil return signedTxs, nil
} }
func loadTransactions(txStr string, inputData *input, env stEnv, chainConfig *params.ChainConfig) (types.Transactions, error) { func loadTransactions(txStr string, inputData *input, env stEnv, chainConfig *params.ChainConfig) (types.Transactions, error) {
var txsWithKeys []*txWithKey var txsWithKeys []*txWithKey
var signed types.Transactions
if txStr != stdinSelector { if txStr != stdinSelector {
inFile, err := os.Open(txStr) data, err := os.ReadFile(txStr)
if err != nil { if err != nil {
return nil, NewError(ErrorIO, fmt.Errorf("failed reading txs file: %v", err)) return nil, NewError(ErrorIO, fmt.Errorf("failed reading txs file: %v", err))
} }
defer inFile.Close() if strings.HasSuffix(txStr, ".rlp") { // A file containing an rlp list
decoder := json.NewDecoder(inFile)
if strings.HasSuffix(txStr, ".rlp") {
var body hexutil.Bytes var body hexutil.Bytes
if err := decoder.Decode(&body); err != nil { if err := json.Unmarshal(data, &body); err != nil {
return nil, err return nil, err
} }
var txs types.Transactions // Already signed transactions
if err := rlp.DecodeBytes(body, &txs); err != nil { if err := rlp.DecodeBytes(body, &signed); err != nil {
return nil, err return nil, err
} }
for _, tx := range txs { return signed, nil
txsWithKeys = append(txsWithKeys, &txWithKey{ }
key: nil, if err := json.Unmarshal(data, &txsWithKeys); err != nil {
tx: tx, return nil, NewError(ErrorJson, fmt.Errorf("failed unmarshaling txs-file: %v", err))
})
}
} else {
if err := decoder.Decode(&txsWithKeys); err != nil {
return nil, NewError(ErrorJson, fmt.Errorf("failed unmarshaling txs-file: %v", err))
}
} }
} else { } else {
if len(inputData.TxRlp) > 0 { if len(inputData.TxRlp) > 0 {
// Decode the body of already signed transactions // Decode the body of already signed transactions
body := common.FromHex(inputData.TxRlp) body := common.FromHex(inputData.TxRlp)
var txs types.Transactions // Already signed transactions
if err := rlp.DecodeBytes(body, &txs); err != nil { if err := rlp.DecodeBytes(body, &signed); err != nil {
return nil, err return nil, err
} }
for _, tx := range txs { return signed, nil
txsWithKeys = append(txsWithKeys, &txWithKey{
key: nil,
tx: tx,
})
}
} else {
// JSON encoded transactions
txsWithKeys = inputData.Txs
} }
// JSON encoded transactions
txsWithKeys = inputData.Txs
} }
// We may have to sign the transactions. // We may have to sign the transactions.
signer := types.MakeSigner(chainConfig, big.NewInt(int64(env.Number)), env.Timestamp) signer := types.MakeSigner(chainConfig, big.NewInt(int64(env.Number)), env.Timestamp)
return signUnsignedTransactions(txsWithKeys, signer)
if txs, err := signUnsignedTransactions(txsWithKeys, signer); err != nil {
return nil, NewError(ErrorJson, fmt.Errorf("failed signing transactions: %v", err))
} else {
return txs, err
}
} }
func applyLondonChecks(env *stEnv, chainConfig *params.ChainConfig) error { func applyLondonChecks(env *stEnv, chainConfig *params.ChainConfig) error {