Skip to content

Commit

Permalink
Merge pull request #6387 from asamuj/master
Browse files Browse the repository at this point in the history
fix dsbackend lock
  • Loading branch information
simlecode authored Aug 6, 2024
2 parents a8b3355 + bf4562d commit ca832b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
35 changes: 16 additions & 19 deletions pkg/wallet/dsbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"strings"
"sync"
"sync/atomic"

"github.com/awnumar/memguard"
"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -45,12 +46,12 @@ type DSBackend struct {

cache map[address.Address]struct{}

PassphraseConf config.PassphraseConfig
passphraseConf config.PassphraseConfig

password *memguard.Enclave
unLocked map[address.Address]*key.KeyInfo

state int
state atomic.Int64
}

var _ Backend = (*DSBackend)(nil)
Expand Down Expand Up @@ -81,7 +82,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 +105,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,12 +189,10 @@ 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
}

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

func (backend *DSBackend) LockWallet(ctx context.Context) error {
if backend.state == Lock {
if backend.state.Load() == Lock {
return fmt.Errorf("already locked")
}

Expand All @@ -298,7 +297,7 @@ func (backend *DSBackend) LockWallet(ctx context.Context) error {
backend.lk.Unlock()
}
backend.cleanPassword()
backend.state = Lock
backend.state.Store(Lock)

return nil
}
Expand All @@ -310,7 +309,7 @@ func (backend *DSBackend) UnLockWallet(ctx context.Context, password []byte) err
password[i] = 0
}
}()
if backend.state == Unlock {
if backend.state.Load() == Unlock {
return fmt.Errorf("already unlocked")
}

Expand All @@ -328,7 +327,7 @@ func (backend *DSBackend) UnLockWallet(ctx context.Context, password []byte) err
backend.unLocked[addr] = ki
backend.lk.Unlock()
}
backend.state = Unlock
backend.state.Store(Unlock)

return nil
}
Expand All @@ -348,10 +347,8 @@ func (backend *DSBackend) SetPassword(ctx context.Context, password []byte) erro
backend.unLocked[addr] = ki
backend.lk.Unlock()
}
if backend.state == undetermined {
backend.state = Unlock
}

backend.state.CompareAndSwap(undetermined, Unlock)
backend.setPassword(password)

return nil
Expand All @@ -364,7 +361,7 @@ func (backend *DSBackend) HasPassword() bool {

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

func (backend *DSBackend) setPassword(password []byte) {
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 ca832b8

Please sign in to comment.