Skip to content

Commit

Permalink
Merge branch 'main' into wallet-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jul 2, 2024
2 parents 8df4151 + 5727751 commit 2131b47
Show file tree
Hide file tree
Showing 33 changed files with 371 additions and 345 deletions.
42 changes: 33 additions & 9 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/gofrs/flock"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/node"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/version"
"github.com/pactus-project/pactus/wallet"
Expand Down Expand Up @@ -83,6 +86,9 @@ func main() {
log.Println("application startup")
})

n, wlt, err := newNode(workingDir)
fatalErrorCheck(err)

// Connect function to application activate event
app.Connect("activate", func() {
log.Println("application activate")
Expand All @@ -105,9 +111,9 @@ func main() {
gtk.MainIteration()
}

// Running the start-up logic in a separate goroutine
// Running the run-up logic in a separate goroutine
glib.TimeoutAdd(uint(100), func() bool {
start(workingDir, app)
run(n, wlt, app)
splashDlg.Destroy()

// Ensures the function is not called again
Expand All @@ -117,20 +123,32 @@ func main() {

// Connect function to application shutdown event, this is not required.
app.Connect("shutdown", func() {
n.Stop()
_ = fileLock.Unlock()
log.Println("application shutdown")
})

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

go func() {
s := <-interrupt
log.Printf("signal %s received", s.String())
n.Stop()
_ = fileLock.Unlock()
os.Exit(0)
}()

// Launch the application
os.Exit(app.Run(nil))
}

func start(workingDir string, app *gtk.Application) {
func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) {
// change working directory
if err := os.Chdir(workingDir); err != nil {
log.Println("Aborted! Unable to changes working directory. " + err.Error())

return
return nil, nil, err
}

passwordFetcher := func(wlt *wallet.Wallet) (string, bool) {
Expand All @@ -140,16 +158,22 @@ func start(workingDir string, app *gtk.Application) {

return getWalletPassword(wlt)
}
node, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
fatalErrorCheck(err)
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
if err != nil {
return nil, nil, err
}

return n, wlt, nil
}

grpcAddr := node.GRPC().Address()
func run(n *node.Node, wlt *wallet.Wallet, app *gtk.Application) {
grpcAddr := n.GRPC().Address()
cmd.PrintInfoMsgf("connect wallet to grpc server: %s\n", grpcAddr)

wlt.SetServerAddr(grpcAddr)

nodeModel := newNodeModel(node)
walletModel := newWalletModel(wlt, node)
nodeModel := newNodeModel(n)
walletModel := newWalletModel(wlt, n)

// building main window
win := buildMainWindow(nodeModel, walletModel)
Expand Down
34 changes: 21 additions & 13 deletions cmd/shell/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package main

import (
"context"
"encoding/base64"
"fmt"

"github.com/NathanBaulch/protoc-gen-cobra/client"
"github.com/NathanBaulch/protoc-gen-cobra/naming"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/www/grpc/basicauth"
pb "github.com/pactus-project/pactus/www/grpc/gen/go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

const (
Expand All @@ -19,23 +19,20 @@ const (
)

func main() {
var (
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`,
}

auth := &basicauth.BasicAuth{}

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

client.RegisterPreDialer(func(_ context.Context, opts *[]grpc.DialOption) error {
*opts = append(*opts, grpc.WithPerRPCCredentials(auth))

return nil
fs.StringVar(&username, namer("auth-username"), "", "username for gRPC basic authentication")
fs.StringVar(&password, namer("auth-password"), "", "password for gRPC basic authentication")
})

changeDefaultParameters := func(c *cobra.Command) *cobra.Command {
Expand All @@ -48,6 +45,17 @@ 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()))
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (cs *consensus) announceNewBlock(blk *block.Block, cert *certificate.BlockC

func (cs *consensus) makeBlockCertificate(votes map[crypto.Address]*vote.Vote,
) *certificate.BlockCertificate {
cert := certificate.NewBlockCertificate(cs.height, cs.round, false)
cert := certificate.NewBlockCertificate(cs.height, cs.round)
cert.SetSignature(cs.signersInfo(votes))

return cert
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (td *testData) commitBlockForAllStates(t *testing.T) (*block.Block, *certif
var err error
prop := td.makeProposal(t, height+1, 0)

cert := certificate.NewBlockCertificate(height+1, 0, false)
cert := certificate.NewBlockCertificate(height+1, 0)
sb := cert.SignBytes(prop.Block().Hash())
sig1 := td.consX.valKey.Sign(sb)
sig2 := td.consY.valKey.Sign(sb)
Expand Down
7 changes: 4 additions & 3 deletions consensus/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/pactus-project/pactus/types/proposal"
"github.com/pactus-project/pactus/types/vote"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,7 +24,7 @@ func TestSetProposalInvalidProposer(t *testing.T) {
assert.Nil(t, td.consY.Proposal())

addr := td.consB.valKey.Address()
blk, _ := td.GenerateTestBlockWithProposer(1, addr)
blk, _ := td.GenerateTestBlock(1, testsuite.BlockWithProposer(addr))
invalidProp := proposal.NewProposal(1, 0, blk)

td.consY.SetProposal(invalidProp)
Expand All @@ -38,7 +39,7 @@ func TestSetProposalInvalidBlock(t *testing.T) {
td := setup(t)

addr := td.consB.valKey.Address()
blk, _ := td.GenerateTestBlockWithProposer(1, addr)
blk, _ := td.GenerateTestBlock(1, testsuite.BlockWithProposer(addr))
invProp := proposal.NewProposal(1, 2, blk)
td.HelperSignProposal(td.consB.valKey, invProp)

Expand All @@ -54,7 +55,7 @@ func TestSetProposalInvalidHeight(t *testing.T) {
td := setup(t)

addr := td.consB.valKey.Address()
blk, _ := td.GenerateTestBlockWithProposer(2, addr)
blk, _ := td.GenerateTestBlock(2, testsuite.BlockWithProposer(addr))
invProp := proposal.NewProposal(2, 0, blk)
td.HelperSignProposal(td.consB.valKey, invProp)

Expand Down
1 change: 1 addition & 0 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func testConfig() *Config {
ForcePrivateNetwork: true,
NetworkName: "test",
DefaultPort: 12345,
PeerStorePath: util.TempFilePath(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func TestAccountChange(t *testing.T) {
func TestAnyRecentTransaction(t *testing.T) {
td := setup(t)

randTx1, _ := td.GenerateTestTransferTx()
randTx2, _ := td.GenerateTestTransferTx()
randTx1 := td.GenerateTestTransferTx()
randTx2 := td.GenerateTestTransferTx()
td.sandbox.CommitTransaction(randTx1)
td.sandbox.CommitTransaction(randTx2)

Expand Down
8 changes: 4 additions & 4 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func TestProposeBlock(t *testing.T) {
lockTime := td.state.LastBlockHeight()
dupSubsidyTx := tx.NewSubsidyTx(lockTime, proposer.Address(),
td.state.params.BlockReward, "duplicated subsidy transaction")
invTransferTx, _ := td.GenerateTestTransferTx()
invBondTx, _ := td.GenerateTestBondTx()
invSortitionTx, _ := td.GenerateTestSortitionTx()
invTransferTx := td.GenerateTestTransferTx()
invBondTx := td.GenerateTestBondTx()
invSortitionTx := td.GenerateTestSortitionTx()

pub, _ := td.RandBLSKeyPair()
validTrx1 := tx.NewTransferTx(lockTime, td.genAccKey.PublicKeyNative().AccountAddress(),
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestExecuteBlock(t *testing.T) {
rewardAddr := td.RandAccAddress()
invSubsidyTx := td.state.createSubsidyTx(rewardAddr, 1001)
validSubsidyTx := td.state.createSubsidyTx(rewardAddr, 1000)
invTransferTx, _ := td.GenerateTestTransferTx()
invTransferTx := td.GenerateTestTransferTx()

validTx1 := tx.NewTransferTx(1, td.genAccKey.PublicKeyNative().AccountAddress(),
td.RandAccAddress(), 1, 1000, "")
Expand Down
2 changes: 1 addition & 1 deletion state/lastinfo/last_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func setup(t *testing.T) *testData {
prevCert, lastSeed, val2.Address())

sig := valKey.Sign([]byte("fatdog"))
lastCert := certificate.NewBlockCertificate(lastHeight, 0, true)
lastCert := certificate.NewBlockCertificate(lastHeight, 0)
lastCert.SetSignature(committers, []int32{}, sig)
mockStore.SaveBlock(lastBlock, lastCert)
assert.Equal(t, mockStore.LastHeight, lastHeight)
Expand Down
2 changes: 1 addition & 1 deletion state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (m *MockState) CommitBlock(b *block.Block, cert *certificate.BlockCertifica
func (*MockState) Close() {}

func (m *MockState) ProposeBlock(valKey *bls.ValidatorKey, _ crypto.Address) (*block.Block, error) {
blk, _ := m.ts.GenerateTestBlockWithProposer(m.TestStore.LastHeight, valKey.Address())
blk, _ := m.ts.GenerateTestBlock(m.TestStore.LastHeight, testsuite.BlockWithProposer(valKey.Address()))

return blk, nil
}
Expand Down
10 changes: 5 additions & 5 deletions state/score/score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ func TestScoreManager(t *testing.T) {
maxCert := uint32(3)
sm := NewScoreManager(maxCert)

cert1 := certificate.NewBlockCertificate(1, 0, false)
cert1 := certificate.NewBlockCertificate(1, 0)
cert1.SetSignature([]int32{0, 1, 2, 3}, []int32{0}, nil)

cert2 := certificate.NewBlockCertificate(2, 0, false)
cert2 := certificate.NewBlockCertificate(2, 0)
cert2.SetSignature([]int32{0, 1, 2, 3}, []int32{3}, nil)

cert3 := certificate.NewBlockCertificate(3, 0, false)
cert3 := certificate.NewBlockCertificate(3, 0)
cert3.SetSignature([]int32{1, 2, 3, 4}, []int32{2}, nil)

cert4 := certificate.NewBlockCertificate(4, 0, false)
cert4 := certificate.NewBlockCertificate(4, 0)
cert4.SetSignature([]int32{1, 2, 3, 4}, []int32{2}, nil)

cert5 := certificate.NewBlockCertificate(5, 0, false)
cert5 := certificate.NewBlockCertificate(5, 0)
cert5.SetSignature([]int32{1, 2, 3, 4}, []int32{2}, nil)

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (td *testData) makeCertificateAndSign(t *testing.T, blockHash hash.Hash,

sigs := make([]*bls.Signature, 0, len(td.genValKeys))
height := td.state.LastBlockHeight()
cert := certificate.NewBlockCertificate(height+1, round, true)
cert := certificate.NewBlockCertificate(height+1, round)
signBytes := cert.SignBytes(blockHash)
committers := []int32{0, 1, 2, 3}
absentees := []int32{3}
Expand Down
1 change: 0 additions & 1 deletion state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func TestBlockValidation(t *testing.T) {
invPrevCert := certificate.NewBlockCertificate(
blk0.PrevCertificate().Height(),
blk0.PrevCertificate().Round(),
blk0.PrevCertificate().FastPath(),
)
invPrevCert.SetSignature(
blk0.PrevCertificate().Committers(),
Expand Down
2 changes: 1 addition & 1 deletion sync/bundle/message/block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBlockAnnounceMessage(t *testing.T) {

t.Run("Invalid certificate", func(t *testing.T) {
blk, _ := ts.GenerateTestBlock(ts.RandHeight())
cert := certificate.NewBlockCertificate(0, 0, false)
cert := certificate.NewBlockCertificate(0, 0)
m := NewBlockAnnounceMessage(blk, cert)
err := m.BasicCheck()

Expand Down
2 changes: 1 addition & 1 deletion sync/bundle/message/blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBlocksResponseMessage(t *testing.T) {
sid := 123
t.Run("Invalid certificate", func(t *testing.T) {
blk, _ := ts.GenerateTestBlock(ts.RandHeight())
cert := certificate.NewBlockCertificate(0, 0, false)
cert := certificate.NewBlockCertificate(0, 0)
d, _ := blk.Bytes()
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, ResponseCodeMoreBlocks.String(),
sid, ts.RandHeight(), [][]byte{d}, cert)
Expand Down
2 changes: 1 addition & 1 deletion sync/bundle/message/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestTransactionsMessage(t *testing.T) {
})

t.Run("OK", func(t *testing.T) {
trx, _ := ts.GenerateTestTransferTx()
trx := ts.GenerateTestTransferTx()
m := NewTransactionsMessage([]*tx.Tx{trx})

assert.NoError(t, m.BasicCheck())
Expand Down
4 changes: 3 additions & 1 deletion sync/handler_block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
func TestParsingBlockAnnounceMessages(t *testing.T) {
td := setup(t, nil)

td.state.CommitTestBlocks(10)

pid := td.RandPeerID()
lastHeight := td.state.LastBlockHeight()
blk1, cert1 := td.GenerateTestBlock(lastHeight + 1)
Expand Down Expand Up @@ -38,7 +40,7 @@ func TestInvalidBlockAnnounce(t *testing.T) {
pid := td.RandPeerID()
height := td.state.LastBlockHeight() + 1
blk, _ := td.GenerateTestBlock(height)
invCert := certificate.NewBlockCertificate(height, 0, false)
invCert := certificate.NewBlockCertificate(height, 0)
msg := message.NewBlockAnnounceMessage(blk, invCert)

err := td.receivingNewMessage(td.sync, msg, pid)
Expand Down
6 changes: 4 additions & 2 deletions sync/handler_blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func TestInvalidBlockData(t *testing.T) {
func TestOneBlockShorter(t *testing.T) {
td := setup(t, nil)

td.state.CommitTestBlocks(10)

lastHeight := td.state.LastBlockHeight()
blk1, cert1 := td.GenerateTestBlock(lastHeight + 1)
d1, _ := blk1.Bytes()
Expand Down Expand Up @@ -282,7 +284,7 @@ func TestSyncing(t *testing.T) {
blockInterval := td.syncBob.state.Genesis().Params().BlockInterval()
blockTime := td.syncBob.state.Genesis().GenesisTime()
for i := uint32(0); i < 100; i++ {
blk, cert := td.GenerateTestBlockWithTime(i+1, blockTime)
blk, cert := td.GenerateTestBlock(i+1, testsuite.BlockWithTime(blockTime))
assert.NoError(t, td.syncBob.state.CommitBlock(blk, cert))

blockTime = blockTime.Add(blockInterval)
Expand Down Expand Up @@ -343,7 +345,7 @@ func TestSyncingHasBlockInCache(t *testing.T) {
blockInterval := td.syncBob.state.Genesis().Params().BlockInterval()
blockTime := td.syncBob.state.Genesis().GenesisTime()
for i := uint32(0); i < 23; i++ {
blk, cert := td.GenerateTestBlockWithTime(i+1, blockTime)
blk, cert := td.GenerateTestBlock(i+1, testsuite.BlockWithTime(blockTime))
assert.NoError(t, td.syncBob.state.CommitBlock(blk, cert))

blockTime = blockTime.Add(blockInterval)
Expand Down
Loading

0 comments on commit 2131b47

Please sign in to comment.