Conflux-Chain/go-conflux-sdk

Add epoch pivot hash support for cfx_getEpochReceipts

Closed this issue · 1 comments

As per to fullnode pull request (Conflux-Chain/conflux-rust#2193), there is new support for cfx_getEpochReceipts to use epoch pivot hash as request parameter. Golang sdk should add this support accordingly.

Also since we need to parse the response of "cfx_getEpochReceipts" rpc call to detect pivot switch, please provide a way to check logic error or return rpc call response to client side.

        // Batch get epoch receipts
	pivotHash := blockHashes[len(blockHashes)-1]

	// TODO use golang sdk cfx.GetEpochReceiptsByPivotBlockHash once it's ready
	/*
		epochReceipts, err := cfx.GetEpochReceiptsByPivotBlockHash(pivotHash)
		if err != nil {
			return emptyEpochData, false, errors.WithMessagef(err, "Failed to get epoch receipts for epoch %v", epoch)
		}
	*/

	var jsonStr = []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"cfx_getEpochReceipts","params":["hash:%v"],"id":1}`, pivotHash))
	resp, err := http.Post(viper.GetString("cfx.http"), "application/json", bytes.NewBuffer(jsonStr))

	if err == nil && !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
		err = errors.New(resp.Status)
	}

	if err != nil {
		return emptyEpochData, errors.WithMessagef(err, "failed to request batch epoch receipts from endpoint %v", viper.GetString("cfx.http"))
	}

	defer resp.Body.Close()

	var respObj struct {
		Result [][]types.TransactionReceipt `json:"result"`
		Error  struct {
			Code    int    `json:"code"`
			Message string `json:"message"`
			Data    string `json:"data"`
		} `json:"error"`
	}

	if err = json.NewDecoder(resp.Body).Decode(&respObj); err != nil {
		return emptyEpochData, errors.WithMessagef(err, "failed to json decode cfx_getEpochReceipts response")
	}

	if respObj.Error.Code == -32602 { // pivot hash assumption failed, must be pivot switched
		return emptyEpochData, errors.WithMessagef(ErrEpochPivotSwitched, "invalid pivot hash (msg: %v; data: %v)", respObj.Error.Message, respObj.Error.Data)
	}

	epochReceipts := respObj.Result