Skip to content

Commit

Permalink
Squashed commit:
Browse files Browse the repository at this point in the history
- namespaces update,
- fix for error processing,
- new types,
- removed hardcoded uncle
- added newheads subscription type support,
- fixed multiple bugs and errors,
- added support of sendrawtransaction different scenarios,
- added support of int128,
- corrected gas math (Kaon has 1e-18 denomination),
- corrected rpc client parameters,
- corrected chain prefixes,
- added GetGasPrice method,
- improved ability to extract contract info from UTXO,
- temp: simulation of predefined contract for Kaon token,
- blockheader method improvement,
- getransactionreceipt adjusted,
- helper functions are updated.

Note: Test functions are not updated.
  • Loading branch information
Alex Belets committed Nov 22, 2024
1 parent 9f6a71a commit 0dc59e1
Show file tree
Hide file tree
Showing 111 changed files with 3,728 additions and 2,448 deletions.
42 changes: 27 additions & 15 deletions pkg/blockhash/blockhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"os"
"runtime"
"strings"
"sync"
Expand All @@ -25,7 +26,7 @@ type BlockHash struct {
ctx context.Context
mutex sync.RWMutex

qtumDB *db.QtumDB
kaonDB *db.QtumDB
getLogger func() log.Logger

chainId int
Expand Down Expand Up @@ -61,17 +62,17 @@ func NewBlockHash(ctx context.Context, getLogger func() log.Logger) (*BlockHash,
}, nil
}

func (bh *BlockHash) GetQtumBlockHash(ethereumBlockHash string) (*string, error) {
return bh.GetQtumBlockHashContext(nil, ethereumBlockHash)
func (bh *BlockHash) GetKaonBlockHash(ethereumBlockHash string) (*string, error) {
return bh.GetKaonBlockHashContext(nil, ethereumBlockHash)
}

func (bh *BlockHash) GetQtumBlockHashContext(ctx context.Context, ethereumBlockHash string) (*string, error) {
var qtumBlockHash string
func (bh *BlockHash) GetKaonBlockHashContext(ctx context.Context, ethereumBlockHash string) (*string, error) {
var kaonBlockHash string
bh.mutex.RLock()
qtumDB := bh.qtumDB
kaonDB := bh.kaonDB
bh.mutex.RUnlock()
if qtumDB == nil {
return &qtumBlockHash, ErrDatabaseNotConfigured
if kaonDB == nil {
return &kaonBlockHash, ErrDatabaseNotConfigured
}

bh.chainIdMutex.RLock()
Expand All @@ -87,9 +88,9 @@ func (bh *BlockHash) GetQtumBlockHashContext(ctx context.Context, ethereumBlockH
}

if ctx == nil {
return qtumDB.GetQtumHash(chainId, ethereumBlockHash)
return kaonDB.GetQtumHash(chainId, ethereumBlockHash)
} else {
return qtumDB.GetQtumHashContext(ctx, chainId, ethereumBlockHash)
return kaonDB.GetQtumHashContext(ctx, chainId, ethereumBlockHash)
}
}

Expand All @@ -116,7 +117,7 @@ func (bh *BlockHash) Start(databaseConfig *DatabaseConfig, chainIdChan <-chan in
}

bh.mutex.Lock()
bh.qtumDB = qdb
bh.kaonDB = qdb
bh.mutex.Unlock()

go func() {
Expand Down Expand Up @@ -149,9 +150,20 @@ func (bh *BlockHash) Start(databaseConfig *DatabaseConfig, chainIdChan <-chan in
// dispatch blocks to block channel
// ctx, cancelFunc := context.WithCancel(context.Background())

// janus, err := url.Parse("https://janus.qiswap.com")
janus, _ := url.Parse("http://localhost:23889")
providers := []*url.URL{janus}
// Get PUBLIC_URL from environment variables
publicURL := os.Getenv("PUBLIC_URL")
if publicURL == "" {
bh.getLogger().Log("err", "PUBLIC_URL environment variable is not set.")
}

// Parse the URL from the environment variable
ethrpcgate, err := url.Parse(publicURL)
if err != nil {
bh.getLogger().Log("err", "Error parsing PUBLIC_URL", "err", err)
}

// Use the parsed URL
providers := []*url.URL{ethrpcgate}

dispatchLogger, _ := blockHashLog.GetLogger()

Expand Down Expand Up @@ -194,7 +206,7 @@ func (bh *BlockHash) Start(databaseConfig *DatabaseConfig, chainIdChan <-chan in
case <-done:
qdb.Shutdown()
bh.mutex.Lock()
bh.qtumDB = nil
bh.kaonDB = nil
bh.mutex.Unlock()
status = 0
// case <-sigs:
Expand Down
24 changes: 12 additions & 12 deletions pkg/conversion/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"
"strings"

"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
"github.com/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/qtumproject/janus/pkg/utils"
"github.com/kaonone/eth-rpc-gate/pkg/utils"
)

func ExtractETHLogsFromTransactionReceipt(blockData qtum.LogBlockData, logs []qtum.Log) []eth.Log {
func ExtractETHLogsFromTransactionReceipt(blockData kaon.LogBlockData, logs []kaon.Log) []eth.Log {
result := make([]eth.Log, 0, len(logs))
for _, log := range logs {
for i, log := range logs {
topics := make([]string, 0, len(log.GetTopics()))
for _, topic := range log.GetTopics() {
topics = append(topics, utils.AddHexPrefix(topic))
Expand All @@ -26,7 +26,7 @@ func ExtractETHLogsFromTransactionReceipt(blockData qtum.LogBlockData, logs []qt
Data: utils.AddHexPrefix(log.GetData()),
Address: utils.AddHexPrefix(log.GetAddress()),
Topics: topics,
LogIndex: hexutil.EncodeUint64(uint64(log.Index)),
LogIndex: hexutil.EncodeUint64(uint64(i)),
})
}
return result
Expand All @@ -44,7 +44,7 @@ func ConvertLogTopicsToStringArray(topics []interface{}) []string {
return requestedTopics
}

func SearchLogsAndFilterExtraTopics(ctx context.Context, q *qtum.Qtum, req *qtum.SearchLogsRequest) (qtum.SearchLogsResponse, eth.JSONRPCError) {
func SearchLogsAndFilterExtraTopics(ctx context.Context, q *kaon.Kaon, req *kaon.SearchLogsRequest) (kaon.SearchLogsResponse, *eth.JSONRPCError) {
receipts, err := q.SearchLogs(ctx, req)
if err != nil {
return nil, eth.NewCallbackError(err.Error())
Expand All @@ -64,10 +64,10 @@ func SearchLogsAndFilterExtraTopics(ctx context.Context, q *qtum.Qtum, req *qtum

requestedAddressesMap := populateLoopUpMapWithToLower(req.Addresses)

var filteredReceipts qtum.SearchLogsResponse
var filteredReceipts kaon.SearchLogsResponse

for _, receipt := range receipts {
var logs []qtum.Log
var logs []kaon.Log
for index, log := range receipt.Log {
log.Index = index
if hasAddresses && !requestedAddressesMap[strings.ToLower(log.Address)] {
Expand All @@ -87,7 +87,7 @@ func SearchLogsAndFilterExtraTopics(ctx context.Context, q *qtum.Qtum, req *qtum
return filteredReceipts, nil
}

func FilterQtumLogs(addresses []string, filters []qtum.SearchLogsTopic, logs []qtum.Log) []qtum.Log {
func FilterKaonLogs(addresses []string, filters []kaon.SearchLogsTopic, logs []kaon.Log) []kaon.Log {
hasTopics := len(filters) != 0
hasAddresses := len(addresses) != 0

Expand All @@ -102,7 +102,7 @@ func FilterQtumLogs(addresses []string, filters []qtum.SearchLogsTopic, logs []q

requestedAddressesMap := populateLoopUpMapWithToLower(addresses)

filteredLogs := []qtum.Log{}
filteredLogs := []kaon.Log{}

for _, log := range logs {
if hasAddresses && !requestedAddressesMap[strings.ToLower(strings.TrimPrefix(log.Address, "0x"))] {
Expand All @@ -118,7 +118,7 @@ func FilterQtumLogs(addresses []string, filters []qtum.SearchLogsTopic, logs []q
return filteredLogs
}

func DoFiltersMatch(filters []qtum.SearchLogsTopic, topics []string) bool {
func DoFiltersMatch(filters []kaon.SearchLogsTopic, topics []string) bool {
filterCount := len(filters)
for i, topic := range topics {
if i >= filterCount {
Expand Down
73 changes: 49 additions & 24 deletions pkg/eth/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,94 @@ var CallbackErrorCode = -32000
var ShutdownErrorCode = -32000
var ShutdownError = NewJSONRPCError(ShutdownErrorCode, "server is shutting down", nil)

func NewMethodNotFoundError(method string) JSONRPCError {
func NewMethodNotFoundError(method string) *JSONRPCError {
return NewJSONRPCError(
MethodNotFoundErrorCode,
fmt.Sprintf("The method %s does not exist/is not available", method),
nil,
)
}

func NewInvalidRequestError(message string) JSONRPCError {
func NewInvalidRequestError(message string) *JSONRPCError {
return NewJSONRPCError(InvalidRequestErrorCode, message, nil)
}

func NewInvalidMessageError(message string) JSONRPCError {
func NewInvalidMessageError(message string) *JSONRPCError {
return NewJSONRPCError(InvalidMessageErrorCode, message, nil)
}

func NewInvalidParamsError(message string) JSONRPCError {
func NewInvalidParamsError(message string) *JSONRPCError {
return NewJSONRPCError(InvalidParamsErrorCode, message, nil)
}

func NewCallbackError(message string) JSONRPCError {
func NewCallbackError(message string) *JSONRPCError {
return NewJSONRPCError(CallbackErrorCode, message, nil)
}

type JSONRPCError interface {
Code() int
Message() string
Error() error
type JSONRPCError struct {
code int `json:"code"`
message string `json:"message,omitempty"`
err error `json:"details,omitempty"`
}

func NewJSONRPCError(code int, message string, err error) JSONRPCError {
return &GenericJSONRPCError{
func NewJSONRPCError(code int, message string, err error) *JSONRPCError {
return &JSONRPCError{
code: code,
message: message,
err: err,
}
}

// JSONRPCError contains the message and code for an ETH RPC error
type GenericJSONRPCError struct {
code int
message string
err error
}

func (err *GenericJSONRPCError) Code() int {
func (err *JSONRPCError) Code() int {
return err.code
}

func (err *GenericJSONRPCError) Message() string {
func (err *JSONRPCError) Message() string {
return err.message
}

func (err *GenericJSONRPCError) Error() error {
func (err *JSONRPCError) Error() error {
return err.err
}

func (err *GenericJSONRPCError) MarshalJSON() ([]byte, error) {
// MarshalJSON implements the json.Marshaler interface.
func (d *JSONRPCError) MarshalJSON() ([]byte, error) {
if d == nil {
return []byte("null"), nil
}
if d.message == "" {
return []byte("null"), nil
}
return json.Marshal(struct {
Code int `json:"code"`
Message string `json:"message"`
}{
Code: err.code,
Message: err.message,
Code: d.code,
Message: d.message,
})
}

func (r *JSONRPCError) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
if string(data) == "{}" {
return nil
}
type ErrorData struct {
Code int `json:"code"`
Message string `json:"message",omitempty`
}
var resp ErrorData
if err := json.Unmarshal(data, &resp); err != nil {
return err
}

*r = *NewJSONRPCError(
resp.Code,
resp.Message,
nil,
)

return nil
}
4 changes: 2 additions & 2 deletions pkg/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

var EmptyLogsBloom = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
var DefaultSha3Uncles = "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
var DefaultSha3Uncles = "0x0000000000000000000000000000000000000000000000000000000000000000" // We don't need uncles in our system since we are POS

const (
RPCVersion = "2.0"
Expand All @@ -21,7 +21,7 @@ type JSONRPCRequest struct {
type JSONRPCResult struct {
JSONRPC string `json:"jsonrpc"`
RawResult json.RawMessage `json:"result,omitempty"`
Error JSONRPCError `json:"error,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
ID json.RawMessage `json:"id,omitempty"`
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/eth/eth_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package eth

import (
"encoding/json"
"github.com/pkg/errors"
"math/big"

"github.com/pkg/errors"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/qtumproject/janus/pkg/utils"
"github.com/kaonone/eth-rpc-gate/pkg/utils"
)

type ETHInt struct {
Expand Down
Loading

0 comments on commit 0dc59e1

Please sign in to comment.