Skip to content

Commit

Permalink
Merge branch 'main' into refactor/remove-generic-error
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Sep 14, 2024
2 parents f358ef6 + 9eeb498 commit 75db860
Show file tree
Hide file tree
Showing 134 changed files with 7,986 additions and 1,516 deletions.
7 changes: 6 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Please follow these guidelines when contributing to the project:
You can use these commands in the Makefile:

- `make build` compiles the code into executable binaries.
- `make build_gui` compiles the gtk GUI code into executable binary.
- `make devtools` installs required development tools.
- `make fmt` formats the code according to the Go standards.
- `make check` runs checks on the code, including formatting and linting.
Expand All @@ -27,6 +28,10 @@ You can use these commands in the Makefile:
- `make proto` generates [protobuf](https://protobuf.dev/) files.
Run this target if you have made any changes to the proto buffer files.

### GUI Development

Development of the Pactus Core GUI have some requirements on your machine which you can find a [quick guide about it here](./docs/gtk-gui-development.md).

### Error and Log Messages

Error and log messages should not start with a capital letter (unless it's a proper noun or acronym) and
Expand Down Expand Up @@ -115,4 +120,4 @@ Please read it before contributing to the project.

---

Thank you for your contributions to Pactus blockchain!
Thank you for your contributions to the Pactus blockchain!
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ devtools:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/NathanBaulch/[email protected]
go install github.com/pactus-project/protoc-gen-doc/cmd/[email protected]20240619124021-76fc28241eb6
go install github.com/pactus-project/protoc-gen-doc/cmd/[email protected]20240815105130-84e89d0170e4
go install github.com/bufbuild/buf/cmd/[email protected]
go install mvdan.cc/gofumpt@latest
go install github.com/rakyll/[email protected]
go install github.com/pacviewer/jrpc-gateway/[email protected]
go install github.com/pacviewer/jrpc-gateway/protoc-gen-jrpc-doc/cmd/[email protected]

########################################
### Building
Expand Down
3 changes: 1 addition & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/node"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/types/param"
"github.com/pactus-project/pactus/types/validator"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/wallet"
Expand Down Expand Up @@ -440,7 +439,7 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
}

// create genesis
params := param.DefaultParams()
params := genesis.DefaultGenesisParams()
params.BlockVersion = 0
gen := genesis.MakeGenesis(util.RoundNow(60), accs, vals, params)

Expand Down
5 changes: 5 additions & 0 deletions cmd/daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func buildPruneCmd(parentCmd *cobra.Command) {
cmd.PrintLine()
cmd.PrintInfoMsgf("❌ The operation canceled.")
cmd.PrintLine()
} else if prunedCount == 0 {
cmd.PrintLine()
cmd.PrintInfoMsgf("⚠️ Your node is not passed the retention_days set in config or it's already a pruned node.")
cmd.PrintLine()
cmd.PrintInfoMsgf("Make sure you try to prune a node after retention_days specified in config.toml")
} else {
cmd.PrintLine()
cmd.PrintInfoMsgf("✅ Your node successfully pruned and changed to prune mode.")
Expand Down
35 changes: 31 additions & 4 deletions cmd/gtk/assets/ui/widget_node.ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkBox" id="id_box_node">
Expand Down Expand Up @@ -48,7 +48,7 @@
<property name="label-xalign">0.019999999552965164</property>
<property name="shadow-type">in</property>
<child>
<!-- n-columns=2 n-rows=10 -->
<!-- n-columns=2 n-rows=12 -->
<object class="GtkGrid">
<property name="width-request">400</property>
<property name="visible">True</property>
Expand Down Expand Up @@ -191,7 +191,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">10</property>
<property name="top-attach">11</property>
</packing>
</child>
<child>
Expand All @@ -204,7 +204,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">10</property>
<property name="top-attach">11</property>
</packing>
</child>
<child>
Expand Down Expand Up @@ -386,6 +386,33 @@
<property name="top-attach">6</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">✂️ Is Prune:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">10</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="id_label_is_prune">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">10</property>
</packing>
</child>
</object>
</child>
<child type="label">
Expand Down
18 changes: 12 additions & 6 deletions cmd/gtk/dialog_wallet_create_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package main

import (
_ "embed"
"fmt"

"github.com/gotk3/gotk3/gtk"
"github.com/pactus-project/pactus/wallet"
Expand All @@ -21,7 +20,8 @@ func createAddress(ww *widgetWallet) {
addressLabel := getEntryObj(builder, "id_entry_account_label")

addressTypeCombo := getComboBoxTextObj(builder, "id_combo_address_type")
addressTypeCombo.Append(wallet.AddressTypeBLSAccount, "Account")
addressTypeCombo.Append(wallet.AddressTypeEd25519Account, "ED25519 Account")
addressTypeCombo.Append(wallet.AddressTypeBLSAccount, "BLS Account")
addressTypeCombo.Append(wallet.AddressTypeValidator, "Validator")

addressTypeCombo.SetActive(0)
Expand All @@ -36,12 +36,18 @@ func createAddress(ww *widgetWallet) {
walletAddressType := addressTypeCombo.GetActiveID()
fatalErrorCheck(err)

if walletAddressType == wallet.AddressTypeBLSAccount {
switch walletAddressType {
case wallet.AddressTypeEd25519Account:
password, ok := getWalletPassword(ww.model.wallet)
if !ok {
return
}

_, err = ww.model.wallet.NewEd25519AccountAddress(walletAddressLabel, password)
case wallet.AddressTypeBLSAccount:
_, err = ww.model.wallet.NewBLSAccountAddress(walletAddressLabel)
} else if walletAddressType == wallet.AddressTypeValidator {
case wallet.AddressTypeValidator:
_, err = ww.model.wallet.NewValidatorAddress(walletAddressLabel)
} else {
err = fmt.Errorf("invalid address type '%s'", walletAddressType)
}

errorCheck(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gtk/startup_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ func startupAssistant(workingDir string, chainType genesis.ChainType) bool {
fatalErrorCheck(err)
setMargin(listBox, 5, 5, 1, 1)
listBox.SetHAlign(gtk.ALIGN_CENTER)
listBox.SetSizeRequest(600, -1)
listBox.SetSizeRequest(700, -1)

ssDLBtn, err := gtk.ButtonNewWithLabel("⏬ Download")
fatalErrorCheck(err)
setMargin(ssDLBtn, 10, 5, 1, 1)
ssDLBtn.SetHAlign(gtk.ALIGN_CENTER)
ssDLBtn.SetSizeRequest(600, -1)
ssDLBtn.SetSizeRequest(700, -1)

ssPBLabel, err := gtk.LabelNew("")
fatalErrorCheck(err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/gtk/widget_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func buildWidgetNode(model *nodeModel) (*widgetNode, error) {
labelNetwork := getLabelObj(builder, "id_label_network")
labelNetworkID := getLabelObj(builder, "id_label_network_id")
labelMoniker := getLabelObj(builder, "id_label_moniker")
labelIsPrune := getLabelObj(builder, "id_label_is_prune")

cwd, err := os.Getwd()
if err != nil {
Expand All @@ -57,6 +58,7 @@ func buildWidgetNode(model *nodeModel) (*widgetNode, error) {
labelNetwork.SetText(model.node.State().Genesis().ChainType().String())
labelNetworkID.SetText(model.node.Network().SelfID().String())
labelMoniker.SetText(model.node.Sync().Moniker())
labelIsPrune.SetText(strconv.FormatBool(model.node.State().IsPruned()))

w := &widgetNode{
Box: box,
Expand Down
91 changes: 74 additions & 17 deletions cmd/shell/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (

"github.com/NathanBaulch/protoc-gen-cobra/client"
"github.com/NathanBaulch/protoc-gen-cobra/naming"
"github.com/c-bata/go-prompt"
"github.com/inancgumus/screen"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/util/shell"
pb "github.com/pactus-project/pactus/www/grpc/gen/go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -18,23 +21,58 @@ const (
defaultResponseFormat = "prettyjson"
)

var _prefix string

func main() {
var (
username string
password string
serverAddr string
username string
password string
)

rootCmd := &cobra.Command{
Use: "shell",
Short: "Pactus Shell",
Long: `pactus-shell is a command line tool for interacting with the Pactus blockchain using gRPC`,
Use: "shell",
Short: "Pactus Shell",
SilenceUsage: true,
Long: `pactus-shell is a command line tool for interacting with the Pactus blockchain using gRPC`,
}

sh := shell.New(rootCmd, nil,
prompt.OptionSuggestionBGColor(prompt.Black),
prompt.OptionSuggestionTextColor(prompt.Green),
prompt.OptionDescriptionBGColor(prompt.Black),
prompt.OptionDescriptionTextColor(prompt.White),
prompt.OptionLivePrefix(livePrefix),
)

client.RegisterFlagBinder(func(fs *pflag.FlagSet, namer naming.Namer) {
fs.StringVar(&username, namer("auth-username"), "", "username for gRPC basic authentication")
fs.StringVar(&password, namer("auth-password"), "", "password for gRPC basic authentication")
})

sh.Flags().StringVar(&serverAddr, "server-addr", defaultServerAddr, "gRPC server address")
sh.Flags().StringVar(&username, "auth-username", "",
"username for gRPC basic authentication")

sh.Flags().StringVar(&password, "auth-password", "",
"username for gRPC basic authentication")

sh.PreRun = func(_ *cobra.Command, _ []string) {
cls()
cmd.PrintInfoMsgf("Welcome to PactusBlockchain shell\n\n- Home: https//pactus.org\n- " +
"Docs: https://docs.pactus.org")
cmd.PrintLine()
_prefix = fmt.Sprintf("pactus@%s > ", serverAddr)
}

sh.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
setAuthContext(cmd, username, password)
}

rootCmd.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
setAuthContext(cmd, username, password)
}

changeDefaultParameters := func(c *cobra.Command) *cobra.Command {
_ = c.PersistentFlags().Lookup("server-addr").Value.Set(defaultServerAddr)
c.PersistentFlags().Lookup("server-addr").DefValue = defaultServerAddr
Expand All @@ -45,24 +83,43 @@ func main() {
return c
}

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
if username != "" && password != "" {
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
md := metadata.Pairs("authorization", "Basic "+auth)
ctx := metadata.NewOutgoingContext(cmd.Context(), md)
cmd.SetContext(ctx)
}

return nil
}

rootCmd.AddCommand(changeDefaultParameters(pb.BlockchainClientCommand()))
rootCmd.AddCommand(changeDefaultParameters(pb.NetworkClientCommand()))
rootCmd.AddCommand(changeDefaultParameters(pb.TransactionClientCommand()))
rootCmd.AddCommand(changeDefaultParameters(pb.WalletClientCommand()))
rootCmd.AddCommand(clearScreen())
rootCmd.AddCommand(sh)

err := rootCmd.Execute()
if err != nil {
cmd.PrintErrorMsgf("%s", err)
cmd.PrintErrorMsgf(err.Error())
}
}

func livePrefix() (string, bool) {
return _prefix, true
}

func clearScreen() *cobra.Command {
return &cobra.Command{
Use: "clear",
Short: "clear screen",
Run: func(_ *cobra.Command, _ []string) {
cls()
},
}
}

func cls() {
screen.MoveTopLeft()
screen.Clear()
}

func setAuthContext(c *cobra.Command, username, password string) {
if username != "" && password != "" {
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
md := metadata.Pairs("authorization", "Basic "+auth)
ctx := metadata.NewOutgoingContext(c.Context(), md)
c.SetContext(ctx)
}
}
8 changes: 7 additions & 1 deletion cmd/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func buildNewAddressCmd(parentCmd *cobra.Command) {

if *addressType == wallet.AddressTypeBLSAccount {
addressInfo, err = wlt.NewBLSAccountAddress(label)
} else if *addressType == wallet.AddressTypeEd25519Account {
password := ""
if wlt.IsEncrypted() {
password = cmd.PromptInput("Password")
}
addressInfo, err = wlt.NewEd25519AccountAddress(label, password)
} else if *addressType == wallet.AddressTypeValidator {
addressInfo, err = wlt.NewValidatorAddress(label)
} else {
Expand Down Expand Up @@ -201,7 +207,7 @@ func buildImportPrivateKeyCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

password := getPassword(wlt, *passOpt)
err = wlt.ImportPrivateKey(password, prv)
err = wlt.ImportBLSPrivateKey(password, prv)
cmd.FatalErrorCheck(err)

err = wlt.Save()
Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func buildShowHistoryCmd(parentCmd *cobra.Command) {
wlt, err := openWallet()
cmd.FatalErrorCheck(err)

history := wlt.GetHistory(addr)
history := wlt.History(addr)
for i, h := range history {
if h.Time != nil {
cmd.PrintInfoMsgf("%d %v %v %v %s\t%v",
Expand Down
28 changes: 28 additions & 0 deletions cmd/wallet/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"time"

"github.com/pactus-project/pactus/cmd"
"github.com/spf13/cobra"
)

// buildInfoCmd builds all sub-commands related to the wallet information.
func buildInfoCmd(parentCmd *cobra.Command) {
infoCmd := &cobra.Command{
Use: "info",
Short: "retrieving the wallet information.",
}

parentCmd.AddCommand(infoCmd)

infoCmd.Run = func(_ *cobra.Command, _ []string) {
wlt, err := openWallet()
cmd.FatalErrorCheck(err)

cmd.PrintInfoMsgf("version: %d", wlt.Version())
cmd.PrintInfoMsgf("created at: %s", wlt.CreationTime().Format(time.RFC3339))
cmd.PrintInfoMsgf("is encrtypted: %t", wlt.IsEncrypted())
cmd.PrintInfoMsgf("network: %s", wlt.Network().String())
}
}
Loading

0 comments on commit 75db860

Please sign in to comment.