Skip to content

Commit

Permalink
Merge pull request #1930 from PlatONnetwork/rpc-compatiblewith-eth
Browse files Browse the repository at this point in the history
Rpc compatiblewith eth
  • Loading branch information
benbaley authored Apr 29, 2022
2 parents 890ac8f + c758c74 commit 82696f4
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/platon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ var (
utils.GraphQLCORSDomainFlag,
utils.GraphQLVirtualHostsFlag,
utils.HTTPApiFlag,
utils.HTTPEnabledEthCompatibleFlag,
utils.LegacyRPCApiFlag,
utils.WSEnabledFlag,
utils.WSListenAddrFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/platon/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.HTTPListenAddrFlag,
utils.HTTPPortFlag,
utils.HTTPApiFlag,
utils.HTTPEnabledEthCompatibleFlag,
utils.HTTPCORSDomainFlag,
utils.HTTPVirtualHostsFlag,
utils.WSEnabledFlag,
Expand Down
13 changes: 11 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package utils
import (
"crypto/ecdsa"
"fmt"
"github.com/PlatONnetwork/PlatON-Go/graphql"
"github.com/PlatONnetwork/PlatON-Go/internal/ethapi"
"io/ioutil"
"math/big"
"os"
Expand All @@ -30,6 +28,9 @@ import (
"strings"
"time"

"github.com/PlatONnetwork/PlatON-Go/graphql"
"github.com/PlatONnetwork/PlatON-Go/internal/ethapi"

"github.com/PlatONnetwork/PlatON-Go/miner"

"github.com/PlatONnetwork/PlatON-Go/core/snapshotdb"
Expand All @@ -43,6 +44,7 @@ import (
"github.com/PlatONnetwork/PlatON-Go/consensus"
"github.com/PlatONnetwork/PlatON-Go/consensus/cbft/types"
"github.com/PlatONnetwork/PlatON-Go/core"
types2 "github.com/PlatONnetwork/PlatON-Go/core/types"
"github.com/PlatONnetwork/PlatON-Go/core/vm"
"github.com/PlatONnetwork/PlatON-Go/crypto"
"github.com/PlatONnetwork/PlatON-Go/crypto/bls"
Expand Down Expand Up @@ -322,6 +324,10 @@ var (
Usage: "API's offered over the HTTP-RPC interface",
Value: "",
}
HTTPEnabledEthCompatibleFlag = cli.BoolFlag{
Name: "http.ethcompatible",
Usage: "Enable eth compatible",
}
GraphQLEnabledFlag = cli.BoolFlag{
Name: "graphql",
Usage: "Enable GraphQL on the HTTP-RPC server. Note that GraphQL can only be started if an HTTP server is started as well.",
Expand Down Expand Up @@ -740,6 +746,9 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
if ctx.GlobalIsSet(HTTPApiFlag.Name) {
cfg.HTTPModules = splitAndTrim(ctx.GlobalString(HTTPApiFlag.Name))
}
if ctx.GlobalBool(HTTPEnabledEthCompatibleFlag.Name) {
types2.HttpEthCompatible = true
}

if ctx.GlobalIsSet(LegacyRPCVirtualHostsFlag.Name) {
cfg.HTTPVirtualHosts = splitAndTrim(ctx.GlobalString(LegacyRPCVirtualHostsFlag.Name))
Expand Down
76 changes: 75 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package types

import (
"crypto/ecdsa"
"encoding/binary"
"fmt"
"io"
"math/big"
Expand All @@ -44,7 +45,8 @@ import (
var (
EmptyRootHash = DeriveSha(Transactions{})
// Extra field in the block header, maximum length
ExtraMaxSize = 97
ExtraMaxSize = 97
HttpEthCompatible = false
)

// BlockNonce is an 81-byte vrf proof containing random numbers
Expand Down Expand Up @@ -72,6 +74,41 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("BlockNonce", input, n[:])
}

func (n *BlockNonce) ETHBlockNonce() ETHBlockNonce {
var a ETHBlockNonce
for i := 0; i < len(a); i++ {
a[i] = n[i]
}
return a
}

// A ETHBlockNonce is a 64-bit hash which proves (combined with the
// mix-hash) that a sufficient amount of computation has been carried
// out on a block.
type ETHBlockNonce [8]byte

// ETHBlockNonce converts the given integer to a block nonce.
func EncodeETHNonce(i uint64) ETHBlockNonce {
var n ETHBlockNonce
binary.BigEndian.PutUint64(n[:], i)
return n
}

// Uint64 returns the integer value of a block nonce.
func (n ETHBlockNonce) Uint64() uint64 {
return binary.BigEndian.Uint64(n[:])
}

// MarshalText encodes n as a hex string with 0x prefix.
func (n ETHBlockNonce) MarshalText() ([]byte, error) {
return hexutil.Bytes(n[:]).MarshalText()
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (n *ETHBlockNonce) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("BlockNonce", input, n[:])
}

//go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go

// Header represents a block header in the Ethereum blockchain.
Expand All @@ -97,6 +134,43 @@ type Header struct {

// MarshalJSON2 marshals as JSON.
func (h Header) MarshalJSON2() ([]byte, error) {
if HttpEthCompatible {
type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom Bloom `json:"logsBloom" gencodec:"required"`
Number *hexutil.Big `json:"number" gencodec:"required"`
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time hexutil.Uint64 `json:"timestamp" gencodec:"required"`
Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
Nonce ETHBlockNonce `json:"nonce" gencodec:"required"`
Hash common.Hash `json:"hash"`

UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
}
var enc Header
enc.ParentHash = h.ParentHash
enc.Coinbase = h.Coinbase
enc.Root = h.Root
enc.TxHash = h.TxHash
enc.ReceiptHash = h.ReceiptHash
enc.Bloom = h.Bloom
enc.Number = (*hexutil.Big)(h.Number)
enc.GasLimit = hexutil.Uint64(h.GasLimit)
enc.GasUsed = hexutil.Uint64(h.GasUsed)
enc.Time = hexutil.Uint64(h.Time / 1000)
enc.Extra = h.Extra
enc.Nonce = h.Nonce.ETHBlockNonce()
enc.Hash = h.Hash()
enc.UncleHash = common.ZeroHash
enc.Difficulty = (*hexutil.Big)(h.Number)
return json2.Marshal(&enc)
}
type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
Expand Down
13 changes: 11 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"context"
"errors"
"fmt"
"github.com/PlatONnetwork/PlatON-Go/x/gov"
"math/big"
"time"

"github.com/PlatONnetwork/PlatON-Go/x/gov"

"github.com/PlatONnetwork/PlatON-Go/core/state"

"github.com/PlatONnetwork/PlatON-Go/accounts/abi"
Expand Down Expand Up @@ -1060,7 +1061,7 @@ func FormatLogs(logs []vm.StructLog) []StructLogRes {

// RPCMarshalHeader converts the given header to the RPC output .
func RPCMarshalHeader(head *types.Header) map[string]interface{} {
return map[string]interface{}{
m := map[string]interface{}{
"number": (*hexutil.Big)(head.Number),
"hash": head.Hash(),
"parentHash": head.ParentHash,
Expand All @@ -1079,6 +1080,14 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} {
"transactionsRoot": head.TxHash,
"receiptsRoot": head.ReceiptHash,
}
if types.HttpEthCompatible {
m["nonce"] = hexutil.Bytes(head.Nonce[0:8])
m["timestamp"] = hexutil.Uint64(head.Time / 1000)
m["sha3Uncles"] = common.ZeroHash
m["difficulty"] = (*hexutil.Big)(head.Number)
}

return m
}

// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
//These versions are meaning the current code version.
VersionMajor = 1 // Major version component of the current release
VersionMinor = 2 // Minor version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionPatch = 2 // Patch version component of the current release
VersionMeta = "unstable" // Version metadata to append to the version string

//CAUTION: DO NOT MODIFY THIS ONCE THE CHAIN HAS BEEN INITIALIZED!!!
Expand Down

0 comments on commit 82696f4

Please sign in to comment.