| 122 | } |
| 123 | |
| 124 | func getShardList(ctx context.Context, client *ethclient.Client, contract common.Address, shardLen int) ([]uint64, error) { |
| 125 | var shardId uint64 = 0 |
| 126 | var diffs []*big.Int |
| 127 | for { |
| 128 | diff, err := getDifficulty(ctx, client, contract, shardId) |
| 129 | if err != nil { |
| 130 | lg.Error("Query difficulty by shard", "error", err) |
| 131 | break |
| 132 | } |
| 133 | if diff != nil && diff.Cmp(big.NewInt(0)) == 0 { |
| 134 | // shardId not exist yet |
| 135 | break |
| 136 | } |
| 137 | lg.Info("Query difficulty by shard", "shard", shardId, "difficulty", diff) |
| 138 | diffs = append(diffs, diff) |
| 139 | shardId++ |
| 140 | } |
| 141 | // get the shards with lowest difficulty |
| 142 | sortedShardIds := sortBigIntSlice(diffs) |
| 143 | var result []uint64 |
| 144 | if len(sortedShardIds) == 0 { |
| 145 | // Will create at least one data file |
| 146 | result = []uint64{0} |
| 147 | } else { |
| 148 | if len(sortedShardIds) < shardLen { |
| 149 | shardLen = len(sortedShardIds) |
| 150 | } |
| 151 | for i := 0; i < shardLen; i++ { |
| 152 | result = append(result, uint64(sortedShardIds[i])) |
| 153 | } |
| 154 | } |
| 155 | lg.Info("Get shard list", "shards", result) |
| 156 | return result, nil |
| 157 | } |
| 158 | |
| 159 | func getDifficulty(ctx context.Context, client *ethclient.Client, contract common.Address, shardIdx uint64) (*big.Int, error) { |
| 160 | res, err := getMiningInfo(ctx, client, contract, shardIdx) |