Skip to content

Commit

Permalink
fix panic
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Sep 25, 2023
1 parent 253077c commit e4cb311
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
25 changes: 9 additions & 16 deletions go/rpc/encrypted_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ func (c *EncRPCClient) Subscribe(ctx context.Context, result interface{}, namesp
return nil, fmt.Errorf("expected a channel of type `chan types.Log`, got %T", ch)
}
clientChannel := make(chan common.IDAndEncLog)
subscription, err := c.obscuroClient.Subscribe(ctx, nil, namespace, clientChannel, subscriptionType, encryptedParams)
subscriptionToObscuro, err := c.obscuroClient.Subscribe(ctx, nil, namespace, clientChannel, subscriptionType, encryptedParams)
if err != nil {
return nil, err
}

// We need to return the subscription ID, to allow unsubscribing. However, the client API has already converted
// from a subscription ID to a subscription object under the hood, so we can't retrieve the subscription ID.
// To hack around this, we always return the subscription ID as the first message on the newly-created subscription.
err = c.setResultToSubID(clientChannel, result, subscription)
err = c.setResultToSubID(clientChannel, result, subscriptionToObscuro)
if err != nil {
subscription.Unsubscribe()
subscriptionToObscuro.Unsubscribe()
return nil, err
}

go c.forwardLogs(clientChannel, logCh, subscription)
go c.forwardLogs(clientChannel, logCh, subscriptionToObscuro)

return subscription, nil
return subscriptionToObscuro, nil
}

func (c *EncRPCClient) forwardLogs(clientChannel chan common.IDAndEncLog, logCh chan common.IDAndLog, subscription *rpc.ClientSubscription) {
Expand Down Expand Up @@ -165,18 +165,11 @@ func (c *EncRPCClient) forwardLogs(clientChannel chan common.IDAndEncLog, logCh
}

case err := <-subscription.Err():
if c == nil {
panic("c is nill")
}
if c.logger == nil {
panic("c.logger is nill")
}
if subscription == nil {
panic("subscription is nil")
if err != nil {
c.logger.Info("subscription to obscuro node closed with error", log.ErrKey, err)
} else {
c.logger.Info("subscription to obscuro node closed")
}

c.logger.Info("subscription closed", log.ErrKey, err)

return
}
}
Expand Down
10 changes: 8 additions & 2 deletions tools/walletextension/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"

Check failure on line 7 in tools/walletextension/common/common.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
gethlog "github.com/ethereum/go-ethereum/log"

Check failure on line 8 in tools/walletextension/common/common.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"regexp"

Check failure on line 9 in tools/walletextension/common/common.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

gethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -63,7 +64,12 @@ func GetUserIDbyte(userID string) ([]byte, error) {
return hex.DecodeString(userID)
}

func CreateEncClient(hostRPCBindAddr string, addressBytes []byte, privateKeyBytes []byte, signature []byte) (*rpc.EncRPCClient, error) {
func CreateEncClient(
hostRPCBindAddr string,
addressBytes []byte,
privateKeyBytes []byte,
signature []byte,
logger gethlog.Logger) (*rpc.EncRPCClient, error) {

Check failure on line 72 in tools/walletextension/common/common.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
privateKey, err := BytesToPrivateKey(privateKeyBytes)
if err != nil {
return nil, fmt.Errorf("unable to convert bytes to ecies private key: %w", err)
Expand All @@ -77,7 +83,7 @@ func CreateEncClient(hostRPCBindAddr string, addressBytes []byte, privateKeyByte
PublicKey: PrivateKeyToCompressedPubKey(privateKey),
Signature: signature,
}
encClient, err := rpc.NewEncNetworkClient(hostRPCBindAddr, vk, nil)
encClient, err := rpc.NewEncNetworkClient(hostRPCBindAddr, vk, logger)
if err != nil {
return nil, fmt.Errorf("unable to create EncRPCClient: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewWalletExtensionContainerFromConfig(config config.Config, logger gethlog.
logger.Error(fmt.Errorf("error getting accounts for user: %s, %w", hex.EncodeToString(user.UserID), err).Error())
}
for _, account := range accounts {
encClient, err := wecommon.CreateEncClient(hostRPCBindAddr, account.AccountAddress, user.PrivateKey, account.Signature)
encClient, err := wecommon.CreateEncClient(hostRPCBindAddr, account.AccountAddress, user.PrivateKey, account.Signature, logger)
if err != nil {
logger.Error(fmt.Errorf("error creating new client, %w", err).Error())
}
Expand Down
3 changes: 1 addition & 2 deletions tools/walletextension/storage/database/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ func (m *MariaDB) AddAccount(userID []byte, accountAddress []byte, signature []b
}
defer stmt.Close()

res, err := stmt.Exec(userID, accountAddress, signature)
_, err = stmt.Exec(userID, accountAddress, signature)
if err != nil {
return err
}
fmt.Println(res)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion tools/walletextension/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (w *WalletExtension) AddAddressToUser(hexUserID string, message string, sig

accManager := w.userAccountManager.AddAndReturnAccountManager(hexUserID)

encClient, err := common.CreateEncClient(w.hostAddr, addressFromMessage.Bytes(), privateKeyBytes, signature)
encClient, err := common.CreateEncClient(w.hostAddr, addressFromMessage.Bytes(), privateKeyBytes, signature, w.Logger())
if err != nil {
w.Logger().Error(fmt.Errorf("error creating encrypted client for user: (%s), %w", hexUserID, err).Error())
}
Expand Down

0 comments on commit e4cb311

Please sign in to comment.