Skip to content

Commit

Permalink
Fix lint errors, remove unused code and fix typos #2
Browse files Browse the repository at this point in the history
  • Loading branch information
iKapitonau committed Aug 8, 2024
1 parent 71a80eb commit 6bb327a
Show file tree
Hide file tree
Showing 39 changed files with 221 additions and 2,224 deletions.
12 changes: 6 additions & 6 deletions cmd/secretd/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ the node to the chain. This process, if successful, will output a certificate wh
blockchain. Writes the certificate in DER format to ~/attestation_cert
`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
sgxSecretsDir := os.Getenv("SCRT_SGX_STORAGE")
if sgxSecretsDir == "" {
sgxSecretsDir = os.ExpandEnv("/opt/secret/.sgx_secrets")
Expand Down Expand Up @@ -261,7 +261,7 @@ func DumpBin() *cobra.Command {
Long: "Helper to display the contents of a binary file, and extract the public key of the secret node, which is used to" +
"register the node, during node initialization",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
data, err := os.ReadFile(args[0])
if err != nil {
return err
Expand All @@ -281,7 +281,7 @@ func MigrateSealings() *cobra.Command {
Short: "Migrate sealed files to the current format",
Long: "Re-create SGX-sealed files according to the current format",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
_, err := api.MigrateSealing()
if err != nil {
return fmt.Errorf("failed to start enclave. Enclave returned: %s", err)
Expand Down Expand Up @@ -356,7 +356,7 @@ func HealthCheck() *cobra.Command {
Short: "Test enclave status",
Long: "Help diagnose issues by performing a basic sanity test that SGX is working properly",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
res, err := api.HealthCheck()
if err != nil {
return fmt.Errorf("failed to start enclave. Enclave returned: %s", err)
Expand All @@ -377,7 +377,7 @@ func ResetEnclave() *cobra.Command {
Long: "This will delete all registration and enclave parameters. Use when something goes wrong and you want to start fresh." +
"You will have to go through registration again to be able to start the node",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
homeDir, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
Expand Down Expand Up @@ -445,7 +445,7 @@ func AutoRegisterNode() *cobra.Command {
Please report any issues with this command
`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
sgxSecretsFolder := os.Getenv("SCRT_SGX_STORAGE")
if sgxSecretsFolder == "" {
sgxSecretsFolder = os.ExpandEnv("/opt/secret/.sgx_secrets")
Expand Down
2 changes: 1 addition & 1 deletion cmd/secretd/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
cmtcfg "github.com/cometbft/cometbft/config"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/scrtlabs/SecretNetwork/x/compute"
)

Expand Down
223 changes: 4 additions & 219 deletions cmd/secretd/genaccounts.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
package main

// import (
// "bufio"
// "encoding/json"
// "errors"
// "fmt"
// "os"
// "runtime/debug"

// "cosmossdk.io/core/address"
// "github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/crypto/keyring"
// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
// banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
// genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
// "github.com/gogo/protobuf/codec"

// "github.com/spf13/cobra"

// "github.com/cosmos/cosmos-sdk/client/flags"
// "github.com/cosmos/cosmos-sdk/server"
// sdk "github.com/cosmos/cosmos-sdk/types"
// "github.com/cosmos/cosmos-sdk/x/genutil"
// )

/*
const (
flagClientHome = "home-client"
flagVestingStart = "vesting-start-time"
flagVestingEnd = "vesting-end-time"
flagVestingAmt = "vesting-amount"
)
*/

import (
"bufio"
"encoding/json"
Expand All @@ -41,7 +8,6 @@ import (
"os"
"runtime/debug"

//"cosmossdk.io/core/address"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -70,169 +36,6 @@ const (
flagModuleName = "module-name"
)

// AddGenesisAccountCmd returns add-genesis-account cobra Command.
func _AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]",
Short: "Add a genesis account to genesis.json",
Long: `Add a genesis account to genesis.json. The provided account must specify
the account address or key name and a list of initial coins. If a key name is given,
the address will be looked up in the local Keybase. The list of initial tokens must
contain valid denominations. Accounts may optionally be supplied with vesting parameters.
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
cdc := clientCtx.Codec

serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

config.SetRoot(clientCtx.HomeDir)

addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
inBuf := bufio.NewReader(cmd.InOrStdin())
keyringBackend, err := cmd.Flags().GetString(flags.FlagKeyringBackend)
if err != nil {
return err
}

// attempt to lookup address from Keybase if no address was provided
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf, clientCtx.Codec)
if err != nil {
return err
}

info, err := kb.Key(args[0])
if err != nil {
return fmt.Errorf("failed to get address from Keybase: %w", err)
}

addr, _ = info.GetAddress()
}

// create concrete account type based on input parameters
var genAccount authtypes.GenesisAccount

coins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return fmt.Errorf("failed to parse coins: %w", err)
}

balances := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}

/*
vestingStart, _ := cmd.Flags().GetInt64(flagVestingStart)
vestingEnd, _ := cmd.Flags().GetInt64(flagVestingEnd)
vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt)
vestingAmt, err := sdk.ParseCoins(vestingAmtStr)
if err != nil {
return fmt.Errorf("failed to parse vesting amount: %w", err)
}
baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)
if !vestingAmt.IsZero() {
baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)
if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) ||
baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) {
return errors.New("vesting amount cannot be greater than total amount")
}
switch {
case vestingStart != 0 && vestingEnd != 0:
genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart)
case vestingEnd != 0:
genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount)
default:
return errors.New("invalid vesting parameters; must supply start and end time or end time")
}
} else {
genAccount = baseAccount
}
*/

// TODO: If vesting is enabled, should remove following lines:
baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)
genAccount = baseAccount

if err := genAccount.Validate(); err != nil {
return fmt.Errorf("failed to validate new genesis account: %w", err)
}

genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

authGenState := authtypes.GetGenesisStateFromAppState(cdc, appState)

accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
if err != nil {
return fmt.Errorf("failed to get accounts from any: %w", err)
}

if accs.Contains(addr) {
return fmt.Errorf("cannot add account at existing address %s", addr)
}

// Add the new account to the set of genesis accounts and sanitize the
// accounts afterwards.
accs = append(accs, genAccount)
accs = authtypes.SanitizeGenesisAccounts(accs)

genAccs, err := authtypes.PackAccounts(accs)
if err != nil {
return fmt.Errorf("failed to convert accounts into any's: %w", err)
}
authGenState.Accounts = genAccs

authGenStateBz, err := cdc.MarshalJSON(&authGenState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}

appState[authtypes.ModuleName] = authGenStateBz

bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState)
bankGenState.Balances = append(bankGenState.Balances, balances)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)

bankGenStateBz, err := cdc.MarshalJSON(bankGenState)
if err != nil {
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
}

appState[banktypes.ModuleName] = bankGenStateBz

appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}

genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
// cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
// cmd.Flags().String(flagClientHome, defaultClientHome, "client's home directory")
// cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
// cmd.Flags().Uint64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
// cmd.Flags().Uint64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func AddGenesisAccountCmd(defaultNodeHome string, eCfg app.EncodingConfig) *cobra.Command {
addressCodec := eCfg.TxConfig.SigningContext().AddressCodec()
cmd := &cobra.Command{
Expand All @@ -258,7 +61,6 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

var kr keyring.Keyring
addr, err := addressCodec.StringToBytes(args[0])
// fmt.Fprintf(os.Stderr, "address: [%s]\n", string(addr))
if err != nil {
inBuf := bufio.NewReader(cmd.InOrStdin())
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
Expand All @@ -267,7 +69,6 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
var err error
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf, clientCtx.Codec)
if err != nil {
// panic(err)
return err
}
} else {
Expand All @@ -276,13 +77,11 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
if kr != nil {
k, err := kr.Key(args[0])
if err != nil {
// panic(err)
return fmt.Errorf("failed to get address from Keyring: %w", err)
}

addr, err = k.GetAddress()
if err != nil {
// panic(err)
return err
}
}
Expand Down Expand Up @@ -320,21 +119,6 @@ func AddGenesisAccount(
vestingStart, vestingEnd int64,
moduleName string,
) error {
// Added for debug purposes
// fmt.Fprintln(os.Stderr, "------------- AddGenesisAccount --------------")

// if tCodec, ok := cdc.(*codec.ProtoCodec); ok {
// ir := tCodec.InterfaceRegistry()
// ia := ir.ListAllInterfaces()
// fmt.Fprintf(os.Stderr, "* AddGenesisAccount Codec inspection: %s\n", reflect.ValueOf(cdc).Type())
// fmt.Fprintf(os.Stderr, "* AddGenesisAccount Interface Regist: %s\n", reflect.ValueOf(ir).Type())
// for _, i := range ia {
// ii := ir.ListImplementations(i)
// fmt.Fprintf(os.Stderr, "\t%s -> %s\n", i, ii)
// }
// fmt.Fprintln(os.Stderr, "----------------------------------------------")
// }

coins, err := sdk.ParseCoinsNormalized(amountStr)
if err != nil {
return fmt.Errorf("failed to parse coins: %w", err)
Expand All @@ -351,7 +135,8 @@ func AddGenesisAccount(
balances := banktypes.Balance{Address: accAddr.String(), Coins: coins.Sort()}
baseAccount := authtypes.NewBaseAccount(accAddr, nil, 0, 0)

if !vestingAmt.IsZero() {
switch {
case !vestingAmt.IsZero():
baseVestingAccount, err := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)
if err != nil {
return fmt.Errorf("failed to create base vesting account: %w", err)
Expand All @@ -372,9 +157,9 @@ func AddGenesisAccount(
default:
return errors.New("invalid vesting parameters; must supply start and end time or end time")
}
} else if moduleName != "" {
case moduleName != "":
genAccount = authtypes.NewEmptyModuleAccount(moduleName, authtypes.Burner, authtypes.Minter)
} else {
default:
genAccount = baseAccount
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/secretd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"os"

svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/scrtlabs/SecretNetwork/app"
"os"
)

func main() {
Expand Down
Loading

0 comments on commit 6bb327a

Please sign in to comment.