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()
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,942评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,268评论 19 139
  • 其实,我始终都不知道,我是不是幸运,只是按步就班的走了我该走的路,或许在别人眼睛里我是幸运的。 年少的时候单...
    伍月端午阅读 222评论 0 0
  • 每次从奶奶家回来都是周一,这周回来的早一天,今天是星期日。因为要给老公沈阳的一个大伯送东西。回来时先到了姥姥家,取...
    小瑨阅读 166评论 0 0
  • 文、庞文钊 庞文钊一边寻思着自己的怪状况,百思不得其解,自己怎不由控制地和那人说了这么多话,一边思考着。太阳西下,...
    庞文钊阅读 370评论 0 0