Skip to content

Commit

Permalink
Merge pull request #22 from ironbeer/fix/unlock-wallet
Browse files Browse the repository at this point in the history
Bug fix for wallet unlock command
  • Loading branch information
ironbeer committed Feb 27, 2024
2 parents e48ca57 + cce8fcd commit f5624ed
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
10 changes: 7 additions & 3 deletions cmd/ipccmd/status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ipccmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -36,7 +37,7 @@ func (c *status) NewHandler(h host.Host) (handlerID int, handler ipc.Handler) {
if data, err := json.Marshal(st); err != nil {
s.Write(c.handlerID, []byte(fmt.Sprintf("failed to marshal status: %s", err)))
} else {
s.Write(c.handlerID, data)
s.ChunkedWrite(c.handlerID, data)
}
}
}
Expand All @@ -55,14 +56,17 @@ func (c *status) Run(sockname string) {
}

// read message
var chunks [][]byte
for {
data, err := cl.Read()
if errors.Is(err, io.EOF) {
return
break
} else if err != nil {
util.Exit(1, "failed to read ipc message: %s\n", err)
} else {
fmt.Println(string(data))
chunks = append(chunks, data)
}
}

fmt.Println(string(bytes.Join(chunks, nil)))
}
24 changes: 16 additions & 8 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func runStartCmd(cmd *cobra.Command, args []string) {
}()

// unlock walelts(wait forever)
waitForUnlockWallets(ctx, conf, ks)
waitForUnlockWallets(ctx, ipc, conf, ks)

// create hub-layer client
hub, err := ethutil.NewReadOnlyClient(conf.HubLayer.RPC)
Expand Down Expand Up @@ -158,18 +158,13 @@ func runStartCmd(cmd *cobra.Command, args []string) {
sccVerifier := newSccVerifier(ctx, conf, ks, db)

// start p2p
p2p := newP2P(ctx, conf, db, sccVerifier, cache)
p2p := newP2P(ctx, ipc, conf, db, sccVerifier, cache)
wg.Add(1)
go func() {
defer wg.Done()
p2p.Start(ctx)
}()

// set ipc handlers
ipc.SetHandler(ipccmd.WalletUnlockCmd.NewHandler(ks))
ipc.SetHandler(ipccmd.PingCmd.NewHandler(ctx, p2p.Host(), p2p.HolePunchHelper()))
ipc.SetHandler(ipccmd.StatusCmd.NewHandler(p2p.Host()))

// start state verifier
if sccVerifier != nil {
wg.Add(1)
Expand Down Expand Up @@ -259,7 +254,15 @@ func getOrCreateP2PKey(filename string) (crypto.PrivKey, error) {
return priv, nil
}

func waitForUnlockWallets(ctx context.Context, c *config.Config, ks *wallet.KeyStore) {
func waitForUnlockWallets(
ctx context.Context,
ipc *ipc.IPCServer,
c *config.Config,
ks *wallet.KeyStore,
) {
// set ipc handler
ipc.SetHandler(ipccmd.WalletUnlockCmd.NewHandler(ks))

wg := &sync.WaitGroup{}
wg.Add(len(c.Wallets))

Expand Down Expand Up @@ -314,6 +317,7 @@ func newIPC(c *config.IPC, ks *wallet.KeyStore) *ipc.IPCServer {

func newP2P(
ctx context.Context,
ipc *ipc.IPCServer,
c *config.Config,
db *database.Database,
verifier *verselayer.SccVerifier,
Expand Down Expand Up @@ -343,6 +347,10 @@ func newP2P(
log.Crit("Failed to create p2p server", "err", err)
}

// set ipc handlers
ipc.SetHandler(ipccmd.StatusCmd.NewHandler(node.Host()))
ipc.SetHandler(ipccmd.PingCmd.NewHandler(ctx, node.Host(), node.HolePunchHelper()))

return node
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ func runUnlockCmd(cmd *cobra.Command, args []string) {
password = string(input)
}

ipccmd.WalletUnlockCmd.Run(commandName, wallet.Address, password)
ipccmd.WalletUnlockCmd.Run(conf.IPC.Sockname, wallet.Address, password)
}
23 changes: 22 additions & 1 deletion ipc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
EOM = 65536
)

const (
chunkSize = 1024
)

type Handler func(*IPCServer, []byte)

type IPCServer struct {
Expand Down Expand Up @@ -82,7 +86,24 @@ func (s *IPCServer) Write(msgType int, message []byte) error {
return err
}
// If they do not sleep, clients will read messages in the wrong order.
time.Sleep(time.Second / 4)
time.Sleep(time.Second / 10)
return nil
}

func (s *IPCServer) ChunkedWrite(msgType int, message []byte) error {
var chunks [][]byte
for chunkSize < len(message) {
message, chunks = message[chunkSize:],
append(chunks, message[0:chunkSize:chunkSize])
}
chunks = append(chunks, message)

for _, chunk := range chunks {
if err := s.Write(msgType, chunk); err != nil {
return err
}
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "fmt"
const (
Major = 0
Minor = 0
Patch = 9
Patch = 10
Meta = ""
)

Expand Down

0 comments on commit f5624ed

Please sign in to comment.