Skip to content

Commit

Permalink
fix dsbackend lock
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuj committed Aug 6, 2024
1 parent a8b3355 commit b3cad7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
36 changes: 20 additions & 16 deletions pkg/wallet/dsbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type DSBackend struct {

cache map[address.Address]struct{}

PassphraseConf config.PassphraseConfig
passphraseConf config.PassphraseConfig

password *memguard.Enclave
unLocked map[address.Address]*key.KeyInfo
Expand Down Expand Up @@ -81,7 +81,7 @@ func NewDSBackend(ctx context.Context, ds repo.Datastore, passphraseCfg config.P
backend := &DSBackend{
ds: ds,
cache: addrCache,
PassphraseConf: passphraseCfg,
passphraseConf: passphraseCfg,
unLocked: make(map[address.Address]*key.KeyInfo, len(addrCache)),
}

Expand All @@ -104,11 +104,11 @@ func (backend *DSBackend) Addresses(ctx context.Context) []address.Address {
backend.lk.RLock()
defer backend.lk.RUnlock()

var cpy []address.Address
var addrs []address.Address
for addr := range backend.cache {
cpy = append(cpy, addr)
addrs = append(addrs, addr)
}
return cpy
return addrs
}

// HasAddress checks if the passed in address is stored in this backend.
Expand Down Expand Up @@ -188,20 +188,21 @@ func (backend *DSBackend) putKeyInfo(ctx context.Context, ki *key.KeyInfo) error
}

var keyJSON []byte
err = backend.UsePassword(func(password []byte) error {
var err error
keyJSON, err = encryptKey(key, password, backend.PassphraseConf.ScryptN, backend.PassphraseConf.ScryptP)
if err := backend.UsePassword(func(password []byte) error {
keyJSON, err = encryptKey(key, password, backend.passphraseConf.ScryptN, backend.passphraseConf.ScryptP)
return err
})
if err != nil {
}); err != nil {
return err
}

if err := backend.ds.Put(ctx, ds.NewKey(key.Address.String()), keyJSON); err != nil {
return errors.Wrapf(err, "failed to store new address: %s", key.Address.String())
}

backend.lk.Lock()
backend.cache[addr] = struct{}{}
backend.unLocked[addr] = ki
backend.lk.Unlock()
return nil
}

Expand Down Expand Up @@ -284,6 +285,8 @@ func (backend *DSBackend) getKey(ctx context.Context, addr address.Address, pass
}

func (backend *DSBackend) LockWallet(ctx context.Context) error {
backend.lk.Lock()
defer backend.lk.Unlock()
if backend.state == Lock {
return fmt.Errorf("already locked")
}
Expand All @@ -293,9 +296,7 @@ func (backend *DSBackend) LockWallet(ctx context.Context) error {
}

for _, addr := range backend.Addresses(ctx) {
backend.lk.Lock()
delete(backend.unLocked, addr)
backend.lk.Unlock()
}
backend.cleanPassword()
backend.state = Lock
Expand All @@ -305,7 +306,9 @@ func (backend *DSBackend) LockWallet(ctx context.Context) error {

// UnLockWallet unlock wallet with password, decrypt local key in db and save to protected memory
func (backend *DSBackend) UnLockWallet(ctx context.Context, password []byte) error {
backend.lk.Lock()
defer func() {
backend.lk.Unlock()
for i := range password {
password[i] = 0
}
Expand All @@ -324,9 +327,7 @@ func (backend *DSBackend) UnLockWallet(ctx context.Context, password []byte) err
return err
}

backend.lk.Lock()
backend.unLocked[addr] = ki
backend.lk.Unlock()
}
backend.state = Unlock

Expand All @@ -335,6 +336,8 @@ func (backend *DSBackend) UnLockWallet(ctx context.Context, password []byte) err

// SetPassword set password for wallet , and wallet used this password to encrypt private key
func (backend *DSBackend) SetPassword(ctx context.Context, password []byte) error {
backend.lk.Lock()
defer backend.lk.Unlock()
if backend.password != nil {
return ErrRepeatPassword
}
Expand All @@ -344,9 +347,8 @@ func (backend *DSBackend) SetPassword(ctx context.Context, password []byte) erro
if err != nil {
return err
}
backend.lk.Lock()

backend.unLocked[addr] = ki
backend.lk.Unlock()
}
if backend.state == undetermined {
backend.state = Unlock
Expand All @@ -364,6 +366,8 @@ func (backend *DSBackend) HasPassword() bool {

// WalletState return wallet state(lock/unlock)
func (backend *DSBackend) WalletState(ctx context.Context) int {
backend.lk.Lock()
defer backend.lk.Unlock()
return backend.state
}

Expand Down
6 changes: 3 additions & 3 deletions tools/conformance/chaos/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (

//go:generate go run ./gen

// Actor is a chaos actor. It implements a variety of illegal behaviours that
// trigger violations of VM invariants. These behaviours are not found in
// Actor is a chaos actor. It implements a variety of illegal behaviors that
// trigger violations of VM invariants. These behaviors are not found in
// production code, but are important to test that the VM constraints are
// properly enforced.
//
// The chaos actor is being incubated and its behaviour and ABI be standardised
// The chaos actor is being incubated and its behavior and ABI be standardized
// shortly. Its CID is ChaosActorCodeCID, and its singleton address is 98 (Address).
// It cannot be instantiated via the init actor, and its constructor panics.
//
Expand Down

0 comments on commit b3cad7b

Please sign in to comment.