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

fix bug in eth_subscribe with 0x prefix and bug in unit test for agen… #86

Merged
merged 1 commit into from
Oct 23, 2021

Conversation

alejoacosta74
Copy link
Collaborator

PR description

This PR implement 2 fixes related to bugs when subscribing to events via web3 and the corresponding unit test

1. Fix bug in eth_subscribe with "0x" prefix in contract address

Bug description

A web3 script that subscribes to an ethereum event sends contract address with "0x" prefix, but qtum expects contract address without "0x":

level=debug component=qtum.Client method=waitforlogs
=> qtum RPC request
{
"jsonrpc": "1.0",
"method": "waitforlogs",
"id": "1",
"params": [
null,
null,
{
"addresses": [
"0x086edcf3fc8a042c1b174e941187369d2919e06b"
],
"topics": [
"0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9",
null,
null
]
},
0
]
}
<= qtum RPC response
{
"error": {
"code": -32602,
"message": "Invalid hex 160 string"
},
"id": "1",
"result": null
}

Bug fix

In notifier/subscription.go:

	for i, ethAddress := range ethAddresses {
		if strings.HasPrefix(ethAddress.String(), "0x") {
			stringAddresses[i] = strings.TrimPrefix(ethAddress.String(), "0x")
		} else {
			stringAddresses[i] = ethAddress.String()
		}
	}

2. Fix bug in unit tests for websocket subscription

Bug description

After implementing fix described above, unit tests fail for web sockets subscription

Bug fix

The mocked qtum response when testing event subscription should use a contract address without "0x" prefix

internal/tests_common.go

func QtumWaitForLogsEntry(log qtum.Log) qtum.WaitForLogsEntry {
	return qtum.WaitForLogsEntry{
		BlockHash:         GetTransactionByHashBlockHexHash,
		BlockNumber:       GetTransactionByHashBlockNumberInteger,
		TransactionHash:   GetTransactionByHashResponseData.Hash,
		TransactionIndex:  hexutil.MustDecodeUint64(GetTransactionByHashResponseData.TransactionIndex),
		From:              GetTransactionByHashResponseData.From,
		To:                GetTransactionByHashResponseData.To,
		CumulativeGasUsed: hexutil.MustDecodeUint64(GetTransactionByHashResponseData.Gas),
		GasUsed:           hexutil.MustDecodeUint64(GetTransactionByHashResponseData.Gas),
		ContractAddress:   strings.TrimPrefix(GetTransactionByHashResponseData.To, "0x"),
		Topics:            log.Topics,
		Data:              log.Data,
	}
}

@ripply ripply merged commit b2d033b into qtumproject:master Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants