Skip to content

Commit

Permalink
Relax required flags when index-only mode is set
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Dec 19, 2024
1 parent a4ce4cc commit 5954739
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 91 deletions.
46 changes: 24 additions & 22 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
110 changes: 56 additions & 54 deletions cmd/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-key-path / coa-cloud-kms-keys",
)
}

// 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 {
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 18 additions & 15 deletions services/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 5954739

Please sign in to comment.