forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement most of eth_subscribe in a websocket
- Loading branch information
Showing
77 changed files
with
1,259 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package eth | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
) | ||
|
||
var ErrNoHexPrefix = errors.New("Missing 0x prefix") | ||
var ErrInvalidLength = errors.New("Invalid length") | ||
|
||
type ETHAddress struct { | ||
address string | ||
} | ||
|
||
func (addr *ETHAddress) String() string { | ||
return addr.address | ||
} | ||
|
||
func (addr ETHAddress) MarshalJSON() ([]byte, error) { | ||
if err := validateAddress(addr.address); err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
return json.Marshal(addr.address) | ||
} | ||
|
||
// UnmarshalJSON needs to be able to parse ETHAddress from both hex string or number | ||
func (addr *ETHAddress) UnmarshalJSON(data []byte) (err error) { | ||
asString := string(data) | ||
if strings.HasPrefix(asString, `"`) && strings.HasSuffix(asString, `"`) { | ||
asString = asString[1 : len(asString)-1] | ||
} | ||
if err := validateAddress(asString); err != nil { | ||
return err | ||
} | ||
|
||
addr.address = asString | ||
return nil | ||
} | ||
|
||
func validateAddress(address string) error { | ||
if !strings.HasPrefix(address, "0x") { | ||
return ErrNoHexPrefix | ||
} | ||
|
||
if len(address) != 42 { | ||
return ErrInvalidLength | ||
} | ||
|
||
_, err := hexutil.Decode(address) | ||
if err != nil { | ||
return errors.Wrap(err, "Invalid hexadecimal") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package eth | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestEthLogSubscriptionRequestSerialization(t *testing.T) { | ||
jsonValue := `["logs",{"address":"0x8320fe7702b96808f7bbc0d4a888ed1468216cfd","topics":["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"]}]` | ||
var request EthSubscriptionRequest | ||
err := json.Unmarshal([]byte(jsonValue), &request) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
asJson, err := json.Marshal(request) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(asJson) != jsonValue { | ||
t.Fatalf(`"%s" != "%s"\n`, string(asJson), jsonValue) | ||
} | ||
} | ||
|
||
func TestEthLogSubscriptionRequestWithInvalidAddressSerialization(t *testing.T) { | ||
jsonValue := `["logs",{"address":"0x0","topics":["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"]}]` | ||
var request EthSubscriptionRequest | ||
err := json.Unmarshal([]byte(jsonValue), &request) | ||
if err != ErrInvalidLength { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestEthNewPendingTransactionsRequestSerialization(t *testing.T) { | ||
jsonValue := `["newPendingTransactions"]` | ||
var request EthSubscriptionRequest | ||
err := json.Unmarshal([]byte(jsonValue), &request) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
asJson, err := json.Marshal(request) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(asJson) != jsonValue { | ||
t.Fatalf(`"%s" != "%s"\n`, string(asJson), jsonValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package eth | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/qtumproject/janus/pkg/utils" | ||
) | ||
|
||
// translateTopics takes in an ethReq's topics field and translates it to a it's equivalent QtumReq | ||
// topics (optional) has a max lenght of 4 | ||
func TranslateTopics(ethTopics []interface{}) ([]interface{}, error) { | ||
|
||
var topics []interface{} | ||
|
||
if len(ethTopics) > 4 { | ||
return nil, errors.Errorf("invalid number of topics. Logs have a max of 4 topics.") | ||
} | ||
|
||
for _, topic := range ethTopics { | ||
switch topic.(type) { | ||
case []interface{}: | ||
topic, err := TranslateTopics(topic.([]interface{})) | ||
if err != nil { | ||
return nil, err | ||
} | ||
topics = append(topics, topic) | ||
case string: | ||
topics = append(topics, utils.RemoveHexPrefix(topic.(string))) | ||
case nil: | ||
topics = append(topics, nil) | ||
} | ||
} | ||
|
||
return topics, nil | ||
|
||
} |
Oops, something went wrong.