Eth unlockAccount

1. 确定解锁时间

go-ethereum/internal/ethapi/api.go

默认值为300s

func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *uint64) (bool, error) {
  const max = uint64(time.Duration(math.MaxInt64) / time.Second)
    var d time.Duration //确定解锁时间
    if duration == nil {
        d = 300 * time.Second
    } else if *duration > max {
        return false, errors.New("unlock duration too large")
    } else {
        d = time.Duration(*duration) * time.Second
    }
    err := fetchKeystore(s.am).TimedUnlock(accounts.Account{Address: addr}, password, d)
    return err == nil, err
}

2. 开启判断是否超时

go-ethereum/accounts/keystore/keystore.go
func (ks *KeyStore) TimedUnlock(a accounts.Account, passphrase string, timeout time.Duration) error {
  if timeout > 0 {
        u = &unlocked{Key: key, abort: make(chan struct{})}
        go ks.expire(a.Address, u, timeout) //倒计时
    } else {
        u = &unlocked{Key: key}
    }
    ks.unlocked[a.Address] = u
}

3. 超时处理

go-ethereum/accounts/keystore/keystore.go
func (ks *KeyStore) expire(addr common.Address, u *unlocked, timeout time.Duration) {
    t := time.NewTimer(timeout)
    defer t.Stop()
    select {
    case <-u.abort:
        // just quit
    case <-t.C:
        ks.mu.Lock()
        // only drop if it's still the same key instance that dropLater
        // was launched with. we can check that using pointer equality
        // because the map stores a new pointer every time the key is
        // unlocked.
        if ks.unlocked[addr] == u {
            zeroKey(u.PrivateKey)
            delete(ks.unlocked, addr)
        }
        ks.mu.Unlock()
    }
}

你可能感兴趣的:(Eth unlockAccount)