miner.Unregister
This commit is contained in:
parent
00fbd1ef67
commit
90dd19fe49
@ -66,6 +66,7 @@ type FullNode interface {
|
|||||||
// miner
|
// miner
|
||||||
|
|
||||||
MinerRegister(context.Context, address.Address) error
|
MinerRegister(context.Context, address.Address) error
|
||||||
|
MinerUnregister(context.Context, address.Address) error
|
||||||
MinerCreateBlock(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error)
|
MinerCreateBlock(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error)
|
||||||
|
|
||||||
// // UX ?
|
// // UX ?
|
||||||
|
@ -55,6 +55,7 @@ type FullNodeStruct struct {
|
|||||||
MpoolPush func(context.Context, *types.SignedMessage) error `perm:"write"`
|
MpoolPush func(context.Context, *types.SignedMessage) error `perm:"write"`
|
||||||
|
|
||||||
MinerRegister func(context.Context, address.Address) error `perm:"admin"`
|
MinerRegister func(context.Context, address.Address) error `perm:"admin"`
|
||||||
|
MinerUnregister func(context.Context, address.Address) error `perm:"admin"`
|
||||||
MinerCreateBlock func(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error) `perm:"write"`
|
MinerCreateBlock func(context.Context, address.Address, *types.TipSet, []*types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error) `perm:"write"`
|
||||||
|
|
||||||
WalletNew func(context.Context, string) (address.Address, error) `perm:"write"`
|
WalletNew func(context.Context, string) (address.Address, error) `perm:"write"`
|
||||||
@ -163,6 +164,10 @@ func (c *FullNodeStruct) MinerRegister(ctx context.Context, addr address.Address
|
|||||||
return c.Internal.MinerRegister(ctx, addr)
|
return c.Internal.MinerRegister(ctx, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *FullNodeStruct) MinerUnregister(ctx context.Context, addr address.Address) error {
|
||||||
|
return c.Internal.MinerUnregister(ctx, addr)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base *types.TipSet, tickets []*types.Ticket, eproof types.ElectionProof, msgs []*types.SignedMessage) (*chain.BlockMsg, error) {
|
func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base *types.TipSet, tickets []*types.Ticket, eproof types.ElectionProof, msgs []*types.SignedMessage) (*chain.BlockMsg, error) {
|
||||||
return c.Internal.MinerCreateBlock(ctx, addr, base, tickets, eproof, msgs)
|
return c.Internal.MinerCreateBlock(ctx, addr, base, tickets, eproof, msgs)
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,8 @@ type Miner struct {
|
|||||||
|
|
||||||
lk sync.Mutex
|
lk sync.Mutex
|
||||||
addresses []address.Address
|
addresses []address.Address
|
||||||
|
stop chan struct{}
|
||||||
|
stopping chan struct{}
|
||||||
|
|
||||||
// time between blocks, network parameter
|
// time between blocks, network parameter
|
||||||
Delay time.Duration
|
Delay time.Duration
|
||||||
@ -64,15 +66,68 @@ func (m *Miner) Register(addr address.Address) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.addresses = append(m.addresses, addr)
|
m.addresses = append(m.addresses, addr)
|
||||||
|
m.stop = make(chan struct{})
|
||||||
|
|
||||||
go m.mine(context.TODO())
|
go m.mine(context.TODO())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Miner) Unregister(ctx context.Context, addr address.Address) error {
|
||||||
|
m.lk.Lock()
|
||||||
|
if len(m.addresses) == 0 {
|
||||||
|
m.lk.Unlock()
|
||||||
|
return xerrors.New("no addresses registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(m.addresses) > 1 {
|
||||||
|
m.lk.Unlock()
|
||||||
|
log.Errorf("UNREGISTER NOT IMPLEMENTED FOR MORE THAN ONE ADDRESS!")
|
||||||
|
return xerrors.New("can't unregister when more than one actor is registered: not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.addresses[0] != addr {
|
||||||
|
m.lk.Unlock()
|
||||||
|
return xerrors.New("unregister: address not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unregistering last address, stop mining first
|
||||||
|
if m.stop != nil {
|
||||||
|
if m.stopping == nil {
|
||||||
|
m.stopping = make(chan struct{})
|
||||||
|
close(m.stop)
|
||||||
|
}
|
||||||
|
stopping := m.stopping
|
||||||
|
m.lk.Unlock()
|
||||||
|
select {
|
||||||
|
case <-stopping:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
m.lk.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
m.addresses = []address.Address{}
|
||||||
|
|
||||||
|
m.lk.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Miner) mine(ctx context.Context) {
|
func (m *Miner) mine(ctx context.Context) {
|
||||||
ctx, span := trace.StartSpan(ctx, "/mine")
|
ctx, span := trace.StartSpan(ctx, "/mine")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-m.stop:
|
||||||
|
m.lk.Lock()
|
||||||
|
close(m.stopping)
|
||||||
|
m.stop = nil
|
||||||
|
m.stopping = nil
|
||||||
|
m.lk.Unlock()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
base, err := m.GetBestMiningCandidate()
|
base, err := m.GetBestMiningCandidate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get best mining candidate: %s", err)
|
log.Errorf("failed to get best mining candidate: %s", err)
|
||||||
|
@ -28,4 +28,8 @@ func (a *FullNodeAPI) MinerRegister(ctx context.Context, addr address.Address) e
|
|||||||
return a.Miner.Register(addr)
|
return a.Miner.Register(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *FullNodeAPI) MinerUnregister(ctx context.Context, addr address.Address) error {
|
||||||
|
return a.Miner.Unregister(ctx, addr)
|
||||||
|
}
|
||||||
|
|
||||||
var _ api.FullNode = &FullNodeAPI{}
|
var _ api.FullNode = &FullNodeAPI{}
|
||||||
|
Loading…
Reference in New Issue
Block a user