Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support venus gateway & auth #43

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions chain/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func VerifyVRF(ctx context.Context, worker address.Address, vrfBase, vrfproof []
return nil
}

func ComputeVRF(ctx context.Context, sign SignFunc, worker address.Address, sigInput []byte) ([]byte, error) {
func ComputeVRF(ctx context.Context, sign SignFunc, account string, worker address.Address, sigInput []byte) ([]byte, error) {
// log.Infof("sigInput: %s", hex.EncodeToString(sigInput))
sig, err := sign(ctx, "", worker, sigInput, core.MsgMeta{Type: core.MTDrawRandomParam})
sig, err := sign(ctx, account, worker, sigInput, core.MsgMeta{Type: core.MTDrawRandomParam})
if err != nil {
return nil, err
}
Expand All @@ -85,7 +85,7 @@ func ComputeVRF(ctx context.Context, sign SignFunc, worker address.Address, sigI
return sig.Data, nil
}

func IsRoundWinner(ctx context.Context, ts *types.TipSet, round abi.ChainEpoch,
func IsRoundWinner(ctx context.Context, round abi.ChainEpoch, account string,
miner address.Address, brand types.BeaconEntry, mbi *api.MiningBaseInfo, sign SignFunc) (*types.ElectionProof, error) {

buf := new(bytes.Buffer)
Expand All @@ -109,7 +109,7 @@ func IsRoundWinner(ctx context.Context, ts *types.TipSet, round abi.ChainEpoch,
// return nil, xerrors.Errorf("failed to draw randomness: %w", err)
//}

vrfout, err := ComputeVRF(ctx, sign, mbi.WorkerKey, electionRand.Bytes())
vrfout, err := ComputeVRF(ctx, sign, account, mbi.WorkerKey, electionRand.Bytes())
if err != nil {
return nil, xerrors.Errorf("failed to compute VRF: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var initCmd = &cli.Command{
&cli.StringFlag{
Name: "auth-token",
Usage: "auth node token",
Required: true,
Value: "",
},
&cli.StringFlag{
Name: "gateway-api",
Expand Down
37 changes: 22 additions & 15 deletions miner/multiminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type syncStatus struct {
}

type minerWPP struct {
account string
epp chain.WinningPoStProver
isMining bool
err []string
Expand Down Expand Up @@ -157,7 +158,7 @@ func (m *Miner) Start(ctx context.Context) error {
if err != nil {
return err
}
m.minerWPPMap[minerInfo.Addr] = &minerWPP{epp: epp, isMining: true}
m.minerWPPMap[minerInfo.Addr] = &minerWPP{epp: epp, account: minerInfo.Name, isMining: true}
}

m.stop = make(chan struct{})
Expand Down Expand Up @@ -317,7 +318,7 @@ minerLoop:
tCtx, tCtxCancel := context.WithTimeout(ctx, m.mineTimeout)
defer tCtxCancel()

resChan, err := m.mineOne(tCtx, base, tAddr, tMining.epp)
resChan, err := m.mineOne(tCtx, base, tMining.account, tAddr, tMining.epp)
if err != nil { // ToDo retry or continue minerLoop ? currently err is always nil
log.Errorf("mining block failed for %s: %+v", tAddr.String(), err)
return
Expand Down Expand Up @@ -553,7 +554,7 @@ type winPoStRes struct {
// This method does the following:
//
// 1.
func (m *Miner) mineOne(ctx context.Context, base *MiningBase, addr address.Address, epp chain.WinningPoStProver) (<-chan *winPoStRes, error) {
func (m *Miner) mineOne(ctx context.Context, base *MiningBase, account string, addr address.Address, epp chain.WinningPoStProver) (<-chan *winPoStRes, error) {
log.Infow("attempting to mine a block", "tipset", types.LogCids(base.TipSet.Cids()), "miner", addr)

out := make(chan *winPoStRes)
Expand Down Expand Up @@ -619,7 +620,7 @@ func (m *Miner) mineOne(ctx context.Context, base *MiningBase, addr address.Addr
out <- &winPoStRes{addr: addr, err: xerrors.New("miner not exist")}
return
}
winner, err := chain.IsRoundWinner(ctx, base.TipSet, round, addr, rbase, mbi, sign)
winner, err := chain.IsRoundWinner(ctx, round, account, addr, rbase, mbi, sign)
if err != nil {
log.Errorf("failed to check for %s if we win next round: %w", addr, err)
out <- &winPoStRes{addr: addr, err: err}
Expand Down Expand Up @@ -700,7 +701,9 @@ func (m *Miner) computeTicket(ctx context.Context, brand *types.BeaconEntry, bas
//}

var sign chain.SignFunc
if _, ok := m.minerWPPMap[addr]; ok {
accout := ""
if val, ok := m.minerWPPMap[addr]; ok {
accout = val.account
walletAPI, closer, err := client.NewGatewayRPC(m.gatewayNode)
if err != nil {
log.Errorf("create wallet RPC failed: %w", err)
Expand All @@ -713,7 +716,7 @@ func (m *Miner) computeTicket(ctx context.Context, brand *types.BeaconEntry, bas
return nil, xerrors.New("miner not exist")
}

vrfOut, err := chain.ComputeVRF(ctx, sign, mbi.WorkerKey, input.Bytes())
vrfOut, err := chain.ComputeVRF(ctx, sign, accout, mbi.WorkerKey, input.Bytes())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -749,7 +752,9 @@ func (m *Miner) createBlock(ctx context.Context, base *MiningBase, addr, waddr a
// ToDo check if BlockHeader is signed
if blockMsg.Header.BlockSig == nil {
var sign chain.SignFunc
if _, ok := m.minerWPPMap[addr]; ok {
account := ""
if val, ok := m.minerWPPMap[addr]; ok {
account = val.account
walletAPI, closer, err := client.NewGatewayRPC(m.gatewayNode)
if err != nil {
log.Errorf("create wallet RPC failed: %w", err)
Expand All @@ -767,7 +772,7 @@ func (m *Miner) createBlock(ctx context.Context, base *MiningBase, addr, waddr a
return nil, xerrors.Errorf("failed to get SigningBytes: %v", err)
}

sig, err := sign(ctx, "", waddr, nosigbytes, core.MsgMeta{
sig, err := sign(ctx, account, waddr, nosigbytes, core.MsgMeta{
Type: core.MTBlock,
})
if err != nil {
Expand Down Expand Up @@ -822,7 +827,7 @@ func (m *Miner) UpdateAddress(ctx context.Context, skip, limit int64) ([]dtypes.
continue
}

m.minerWPPMap[minerInfo.Addr] = &minerWPP{epp: epp, isMining: true}
m.minerWPPMap[minerInfo.Addr] = &minerWPP{epp: epp, account: minerInfo.Name, isMining: true}
}
}

Expand Down Expand Up @@ -858,7 +863,7 @@ func (m *Miner) StatesForMining(ctx context.Context, addrs []address.Address) ([
return res, nil
}

func (m *Miner) winCountInRound(ctx context.Context, mAddr address.Address, api chain.SignFunc, epoch abi.ChainEpoch) (*types.ElectionProof, error) {
func (m *Miner) winCountInRound(ctx context.Context, account string, mAddr address.Address, api chain.SignFunc, epoch abi.ChainEpoch) (*types.ElectionProof, error) {
ts, err := m.api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(epoch), types.EmptyTSK)
if err != nil {
log.Error("chain get tipset by height error", err)
Expand All @@ -880,7 +885,7 @@ func (m *Miner) winCountInRound(ctx context.Context, mAddr address.Address, api
rbase = mbi.BeaconEntries[len(mbi.BeaconEntries)-1]
}

return chain.IsRoundWinner(ctx, ts, ts.Height()+1, mAddr, rbase, mbi, api)
return chain.IsRoundWinner(ctx, ts.Height()+1, account, mAddr, rbase, mbi, api)
}

func (m *Miner) CountWinners(ctx context.Context, addrs []address.Address, start abi.ChainEpoch, end abi.ChainEpoch) ([]dtypes.CountWinners, error) {
Expand Down Expand Up @@ -915,16 +920,18 @@ func (m *Miner) CountWinners(ctx context.Context, addrs []address.Address, start
winInfo := make([]dtypes.SimpleWinInfo, 0)
totalWinCount := int64(0)

var miningCheckAPI chain.SignFunc = nil
if _, ok := m.minerWPPMap[tAddr]; ok {
var sign chain.SignFunc = nil
account := ""
if val, ok := m.minerWPPMap[tAddr]; ok {
account = val.account
walletAPI, closer, err := client.NewGatewayRPC(m.gatewayNode)
if err != nil {
log.Errorf("[%v] create wallet RPC failed: %w", tAddr, err)
res = append(res, dtypes.CountWinners{Msg: err.Error(), Miner: tAddr})
return
}
defer closer()
miningCheckAPI = walletAPI.WalletSign
sign = walletAPI.WalletSign
} else {
res = append(res, dtypes.CountWinners{Msg: "miner not exist", Miner: tAddr})
return
Expand All @@ -937,7 +944,7 @@ func (m *Miner) CountWinners(ctx context.Context, addrs []address.Address, start
go func(epoch abi.ChainEpoch) {
defer wgWin.Done()

winner, err := m.winCountInRound(ctx, tAddr, miningCheckAPI, epoch)
winner, err := m.winCountInRound(ctx, account, tAddr, sign, epoch)
if err != nil {
log.Errorf("generate winner met error %w", err)
return
Expand Down
15 changes: 8 additions & 7 deletions node/modules/dtypes/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type MinerState struct {
}

type User struct {
ID string
Name string
Miner string
Comment string
State int //0 for init, 1 for active
CreateTime uint64
UpdateTime uint64
ID string `json:"id"`
Name string `json:"name"`
Miner string `json:"miner"` // miner address f01234
SourceType int `json:"sourceType"`
Comment string `json:"comment"`
State int `json:"state"`
CreateTime int64 `json:"createTime"`
UpdateTime int64 `json:"updateTime"`
}

type SimpleWinInfo struct {
Expand Down