Pre-check if the miner has been whitelisted before actually mining
(ctx context.Context, contract, miner common.Address)
| 46 | |
| 47 | // Pre-check if the miner has been whitelisted before actually mining |
| 48 | func (m *l1MiningAPI) CheckMinerRole(ctx context.Context, contract, miner common.Address) error { |
| 49 | version, err := m.GetContractVersion() |
| 50 | if err != nil { |
| 51 | return fmt.Errorf("failed to get contract version: %w", err) |
| 52 | } |
| 53 | m.lg.Info("Storage Contract version", "version", version) |
| 54 | if semver.Compare(version, versionMineRoleEnabled) == -1 { |
| 55 | return nil |
| 56 | } |
| 57 | enforced, err := m.PollingClient.ReadContractField("enforceMinerRole", nil) |
| 58 | if err != nil { |
| 59 | return fmt.Errorf("failed to query enforceMinerRole(): %w", err) |
| 60 | } |
| 61 | if new(big.Int).SetBytes(enforced).Uint64() == 1 { |
| 62 | m.lg.Info("Miner role enforced") |
| 63 | |
| 64 | addrType, _ := abi.NewType("address", "", nil) |
| 65 | data, _ := abi.Arguments{{Type: addrType}}.Pack(miner) |
| 66 | sig := crypto.Keccak256Hash([]byte(`hasMinerRole(address)`)) |
| 67 | calldata := append(sig[:4], data...) |
| 68 | result, err := m.CallContract(ctx, ethereum.CallMsg{To: &contract, Data: calldata}, nil) |
| 69 | if err != nil { |
| 70 | return fmt.Errorf("failed to query hasMinerRole(): %w", err) |
| 71 | } |
| 72 | hasMinerRole := new(big.Int).SetBytes(result).Uint64() |
| 73 | if hasMinerRole == 1 { |
| 74 | m.lg.Info("Miner role granted", "miner", miner) |
| 75 | return nil |
| 76 | } |
| 77 | return fmt.Errorf("miner role not granted to: %s", miner) |
| 78 | } |
| 79 | m.lg.Info("Miner role not enforced") |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func (m *l1MiningAPI) GetMiningInfo(ctx context.Context, contract common.Address, shardIdx uint64) (*miningInfo, error) { |
| 84 | uint256Type, _ := abi.NewType("uint256", "", nil) |
no test coverage detected