From 26ac20830c1128fbf4c50e513ab6a9d5d8950d16 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 19 Dec 2024 16:38:55 +0200 Subject: [PATCH] Relax required flags when index-only mode is set --- bootstrap/bootstrap.go | 46 ++++++------- cmd/run/cmd.go | 110 ++++++++++++++++---------------- services/requester/requester.go | 33 +++++----- 3 files changed, 98 insertions(+), 91 deletions(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 9190f226f..0499bad8b 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -200,29 +200,31 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error { ) accountKeys := make([]*requester.AccountKey, 0) - account, err := b.client.GetAccount(ctx, b.config.COAAddress) - if err != nil { - return fmt.Errorf( - "failed to get signer info account for address: %s, with: %w", - b.config.COAAddress, - err, - ) - } - signer, err := createSigner(ctx, b.config, b.logger) - if err != nil { - return err - } - for _, key := range account.Keys { - // Skip account keys that do not use the same Publick Key as the - // configured crypto.Signer object. - if !key.PublicKey.Equals(signer.PublicKey()) { - continue + if !b.config.IndexOnly { + account, err := b.client.GetAccount(ctx, b.config.COAAddress) + if err != nil { + return fmt.Errorf( + "failed to get signer info account for address: %s, with: %w", + b.config.COAAddress, + err, + ) + } + signer, err := createSigner(ctx, b.config, b.logger) + if err != nil { + return err + } + for _, key := range account.Keys { + // Skip account keys that do not use the same Publick Key as the + // configured crypto.Signer object. + if !key.PublicKey.Equals(signer.PublicKey()) { + continue + } + accountKeys = append(accountKeys, &requester.AccountKey{ + AccountKey: *key, + Address: b.config.COAAddress, + Signer: signer, + }) } - accountKeys = append(accountKeys, &requester.AccountKey{ - AccountKey: *key, - Address: b.config.COAAddress, - Signer: signer, - }) } b.keystore = requester.NewKeyStore(accountKeys) diff --git a/cmd/run/cmd.go b/cmd/run/cmd.go index 6663a6d5a..b88bfa6cb 100644 --- a/cmd/run/cmd.go +++ b/cmd/run/cmd.go @@ -90,58 +90,71 @@ var Cmd = &cobra.Command{ } func parseConfigFromFlags() error { - if coinbase == "" { - return fmt.Errorf("coinbase EVM address required") - } - cfg.Coinbase = gethCommon.HexToAddress(coinbase) - if cfg.Coinbase == (gethCommon.Address{}) { - return fmt.Errorf("invalid coinbase address: %s", coinbase) - } + if !cfg.IndexOnly { + if coinbase == "" { + return fmt.Errorf("coinbase EVM address required") + } + cfg.Coinbase = gethCommon.HexToAddress(coinbase) + if cfg.Coinbase == (gethCommon.Address{}) { + return fmt.Errorf("invalid coinbase address: %s", coinbase) + } - if g, ok := new(big.Int).SetString(gas, 10); ok { - cfg.GasPrice = g - } else { - return fmt.Errorf("invalid gas price") - } + cfg.COAAddress = flow.HexToAddress(coa) + if cfg.COAAddress == flow.EmptyAddress { + return fmt.Errorf("COA address value is the empty address") + } - cfg.COAAddress = flow.HexToAddress(coa) - if cfg.COAAddress == flow.EmptyAddress { - return fmt.Errorf("COA address value is the empty address") - } + if key != "" { + sigAlgo := crypto.StringToSignatureAlgorithm(keyAlg) + if sigAlgo == crypto.UnknownSignatureAlgorithm { + return fmt.Errorf("invalid signature algorithm: %s", keyAlg) + } + pkey, err := crypto.DecodePrivateKeyHex(sigAlgo, key) + if err != nil { + return fmt.Errorf("invalid COA private key: %w", err) + } + cfg.COAKey = pkey + } else if cloudKMSKey != "" { + if cloudKMSProjectID == "" || cloudKMSLocationID == "" || cloudKMSKeyRingID == "" { + return fmt.Errorf( + "using coa-cloud-kms-key requires also coa-cloud-kms-project-id & coa-cloud-kms-location-id & coa-cloud-kms-key-ring-id", + ) + } - if key != "" { - sigAlgo := crypto.StringToSignatureAlgorithm(keyAlg) - if sigAlgo == crypto.UnknownSignatureAlgorithm { - return fmt.Errorf("invalid signature algorithm: %s", keyAlg) - } - pkey, err := crypto.DecodePrivateKeyHex(sigAlgo, key) - if err != nil { - return fmt.Errorf("invalid COA private key: %w", err) - } - cfg.COAKey = pkey - } else if cloudKMSKey != "" { - if cloudKMSProjectID == "" || cloudKMSLocationID == "" || cloudKMSKeyRingID == "" { + // key has the form "{keyID}@{keyVersion}" + keyParts := strings.Split(cloudKMSKey, "@") + if len(keyParts) != 2 { + return fmt.Errorf("wrong format for Cloud KMS key: %s", key) + } + cfg.COACloudKMSKey = &flowGoKMS.Key{ + ProjectID: cloudKMSProjectID, + LocationID: cloudKMSLocationID, + KeyRingID: cloudKMSKeyRingID, + KeyID: keyParts[0], + KeyVersion: keyParts[1], + } + } else { return fmt.Errorf( - "using coa-cloud-kms-keys requires also coa-cloud-kms-project-id & coa-cloud-kms-location-id & coa-cloud-kms-key-ring-id", + "must either provide coa-key / coa-cloud-kms-key", ) } - // key has the form "{keyID}@{keyVersion}" - keyParts := strings.Split(cloudKMSKey, "@") - if len(keyParts) != 2 { - return fmt.Errorf("wrong format for Cloud KMS key: %s", key) - } - cfg.COACloudKMSKey = &flowGoKMS.Key{ - ProjectID: cloudKMSProjectID, - LocationID: cloudKMSLocationID, - KeyRingID: cloudKMSKeyRingID, - KeyID: keyParts[0], - KeyVersion: keyParts[1], + if walletKey != "" { + k, err := gethCrypto.HexToECDSA(walletKey) + if err != nil { + return fmt.Errorf("invalid private key for wallet API: %w", err) + } + + cfg.WalletKey = k + cfg.WalletEnabled = true + log.Warn().Msg("wallet API is enabled. Ensure this is not used in production environments.") } + } + + if g, ok := new(big.Int).SetString(gas, 10); ok { + cfg.GasPrice = g } else { - return fmt.Errorf( - "must either provide coa-key / coa-key-path / coa-cloud-kms-keys", - ) + return fmt.Errorf("invalid gas price") } switch flowNetwork { @@ -203,17 +216,6 @@ func parseConfigFromFlags() error { cfg.ForceStartCadenceHeight = forceStartHeight } - if walletKey != "" { - k, err := gethCrypto.HexToECDSA(walletKey) - if err != nil { - return fmt.Errorf("invalid private key for wallet API: %w", err) - } - - cfg.WalletKey = k - cfg.WalletEnabled = true - log.Warn().Msg("wallet API is enabled. Ensure this is not used in production environments.") - } - if txStateValidation == config.LocalIndexValidation { cfg.TxStateValidation = config.LocalIndexValidation } else if txStateValidation == config.TxSealValidation { diff --git a/services/requester/requester.go b/services/requester/requester.go index 6521943ab..98a242983 100644 --- a/services/requester/requester.go +++ b/services/requester/requester.go @@ -122,22 +122,25 @@ func NewEVM( keystore *KeyStore, ) (*EVM, error) { logger = logger.With().Str("component", "requester").Logger() - address := config.COAAddress - acc, err := client.GetAccount(context.Background(), address) - if err != nil { - return nil, fmt.Errorf( - "could not fetch the configured COA account: %s make sure it exists: %w", - address.String(), - err, - ) - } - if acc.Balance < minFlowBalance { - return nil, fmt.Errorf( - "COA account must be funded with at least %d Flow, but has balance of: %d", - minFlowBalance, - acc.Balance, - ) + if !config.IndexOnly { + address := config.COAAddress + acc, err := client.GetAccount(context.Background(), address) + if err != nil { + return nil, fmt.Errorf( + "could not fetch the configured COA account: %s make sure it exists: %w", + address.String(), + err, + ) + } + + if acc.Balance < minFlowBalance { + return nil, fmt.Errorf( + "COA account must be funded with at least %d Flow, but has balance of: %d", + minFlowBalance, + acc.Balance, + ) + } } head := &types.Header{