Skip to content

Commit

Permalink
Move Encode* and Parse* functions from types to common package (
Browse files Browse the repository at this point in the history
#1765)

* Remove types.ParseUint64orHex and use the one from common package instead

* Move encoding.go to helper/common package
  • Loading branch information
Stefan-Ethernal committed Aug 2, 2023
1 parent 75c5324 commit 2b9430c
Show file tree
Hide file tree
Showing 31 changed files with 86 additions and 90 deletions.
32 changes: 16 additions & 16 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ func (g *Genesis) MarshalJSON() ([]byte, error) {
var enc Genesis
enc.Nonce = hex.EncodeToHex(g.Nonce[:])

enc.Timestamp = types.EncodeUint64(g.Timestamp)
enc.ExtraData = types.EncodeBytes(g.ExtraData)
enc.Timestamp = common.EncodeUint64(g.Timestamp)
enc.ExtraData = common.EncodeBytes(g.ExtraData)

enc.GasLimit = types.EncodeUint64(g.GasLimit)
enc.Difficulty = types.EncodeUint64(g.Difficulty)
enc.BaseFee = types.EncodeUint64(g.BaseFee)
enc.BaseFeeEM = types.EncodeUint64(g.BaseFeeEM)
enc.GasLimit = common.EncodeUint64(g.GasLimit)
enc.Difficulty = common.EncodeUint64(g.Difficulty)
enc.BaseFee = common.EncodeUint64(g.BaseFee)
enc.BaseFeeEM = common.EncodeUint64(g.BaseFeeEM)

enc.Mixhash = g.Mixhash
enc.Coinbase = g.Coinbase
Expand All @@ -152,8 +152,8 @@ func (g *Genesis) MarshalJSON() ([]byte, error) {
enc.Alloc = &alloc
}

enc.Number = types.EncodeUint64(g.Number)
enc.GasUsed = types.EncodeUint64(g.GasUsed)
enc.Number = common.EncodeUint64(g.Number)
enc.GasUsed = common.EncodeUint64(g.GasUsed)
enc.ParentHash = g.ParentHash

return json.Marshal(&enc)
Expand Down Expand Up @@ -201,7 +201,7 @@ func (g *Genesis) UnmarshalJSON(data []byte) error {
}

if dec.ExtraData != nil {
g.ExtraData, subErr = types.ParseBytes(dec.ExtraData)
g.ExtraData, subErr = common.ParseBytes(dec.ExtraData)
if subErr != nil {
parseError("extradata", subErr)
}
Expand Down Expand Up @@ -288,23 +288,23 @@ func (g *GenesisAccount) MarshalJSON() ([]byte, error) {
obj := &genesisAccountEncoder{}

if g.Code != nil {
obj.Code = types.EncodeBytes(g.Code)
obj.Code = common.EncodeBytes(g.Code)
}

if len(g.Storage) != 0 {
obj.Storage = g.Storage
}

if g.Balance != nil {
obj.Balance = types.EncodeBigInt(g.Balance)
obj.Balance = common.EncodeBigInt(g.Balance)
}

if g.Nonce != 0 {
obj.Nonce = types.EncodeUint64(g.Nonce)
obj.Nonce = common.EncodeUint64(g.Nonce)
}

if g.PrivateKey != nil {
obj.PrivateKey = types.EncodeBytes(g.PrivateKey)
obj.PrivateKey = common.EncodeBytes(g.PrivateKey)
}

return json.Marshal(obj)
Expand Down Expand Up @@ -335,7 +335,7 @@ func (g *GenesisAccount) UnmarshalJSON(data []byte) error {
}

if dec.Code != nil {
g.Code, subErr = types.ParseBytes(dec.Code)
g.Code, subErr = common.ParseBytes(dec.Code)
if subErr != nil {
parseError("code", subErr)
}
Expand All @@ -345,7 +345,7 @@ func (g *GenesisAccount) UnmarshalJSON(data []byte) error {
g.Storage = dec.Storage
}

g.Balance, subErr = types.ParseUint256orHex(dec.Balance)
g.Balance, subErr = common.ParseUint256orHex(dec.Balance)
if subErr != nil {
parseError("balance", subErr)
}
Expand All @@ -357,7 +357,7 @@ func (g *GenesisAccount) UnmarshalJSON(data []byte) error {
}

if dec.PrivateKey != nil {
g.PrivateKey, subErr = types.ParseBytes(dec.PrivateKey)
g.PrivateKey, subErr = common.ParseBytes(dec.PrivateKey)
if subErr != nil {
parseError("privatekey", subErr)
}
Expand Down
6 changes: 3 additions & 3 deletions command/backup/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/0xPolygon/polygon-edge/archive"
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/hashicorp/go-hclog"
)

Expand Down Expand Up @@ -41,14 +41,14 @@ type backupParams struct {
func (p *backupParams) validateFlags() error {
var parseErr error

if p.from, parseErr = types.ParseUint64orHex(&p.fromRaw); parseErr != nil {
if p.from, parseErr = common.ParseUint64orHex(&p.fromRaw); parseErr != nil {
return errDecodeRange
}

if p.toRaw != "" {
var parsedTo uint64

if parsedTo, parseErr = types.ParseUint64orHex(&p.toRaw); parseErr != nil {
if parsedTo, parseErr = common.ParseUint64orHex(&p.toRaw); parseErr != nil {
return errDecodeRange
}

Expand Down
5 changes: 3 additions & 2 deletions command/bridge/deposit/erc1155/deposit_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -110,7 +111,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
amountRaw := dp.Amounts[i]
tokenIDRaw := dp.TokenIDs[i]

amount, err := types.ParseUint256orHex(&amountRaw)
amount, err := helperCommon.ParseUint256orHex(&amountRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided amount %s: %w", amountRaw, err))

Expand All @@ -119,7 +120,7 @@ func runCommand(cmd *cobra.Command, _ []string) {

amounts[i] = amount

tokenID, err := types.ParseUint256orHex(&tokenIDRaw)
tokenID, err := helperCommon.ParseUint256orHex(&tokenIDRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided token id %s: %w", tokenIDRaw, err))

Expand Down
3 changes: 2 additions & 1 deletion command/bridge/deposit/erc20/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -101,7 +102,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
for i, amountRaw := range dp.Amounts {
amountRaw := amountRaw

amount, err := types.ParseUint256orHex(&amountRaw)
amount, err := helperCommon.ParseUint256orHex(&amountRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided amount %s: %w", amountRaw, err))

Expand Down
3 changes: 2 additions & 1 deletion command/bridge/deposit/erc721/deposit_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -98,7 +99,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
for i, tokenIDRaw := range dp.TokenIDs {
tokenIDRaw := tokenIDRaw

tokenID, err := types.ParseUint256orHex(&tokenIDRaw)
tokenID, err := helperCommon.ParseUint256orHex(&tokenIDRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided token id %s: %w", tokenIDRaw, err))

Expand Down
5 changes: 3 additions & 2 deletions command/bridge/withdraw/erc1155/withdraw_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/contracts"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -106,14 +107,14 @@ func runCommand(cmd *cobra.Command, _ []string) {
amountRaw := wp.Amounts[i]
tokenIDRaw := wp.TokenIDs[i]

amount, err := types.ParseUint256orHex(&amountRaw)
amount, err := helperCommon.ParseUint256orHex(&amountRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided amount %s: %w", amountRaw, err))

return
}

tokenID, err := types.ParseUint256orHex(&tokenIDRaw)
tokenID, err := helperCommon.ParseUint256orHex(&tokenIDRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided token id %s: %w", amountRaw, err))

Expand Down
3 changes: 2 additions & 1 deletion command/bridge/withdraw/erc20/withdraw_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/contracts"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -95,7 +96,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
receiver := wp.Receivers[i]
amount := wp.Amounts[i]

amountBig, err := types.ParseUint256orHex(&amount)
amountBig, err := helperCommon.ParseUint256orHex(&amount)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided amount %s: %w", amount, err))

Expand Down
3 changes: 2 additions & 1 deletion command/bridge/withdraw/erc721/withdraw_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/contracts"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -94,7 +95,7 @@ func run(cmd *cobra.Command, _ []string) {
for i, tokenIDRaw := range wp.TokenIDs {
tokenIDRaw := tokenIDRaw

tokenID, err := types.ParseUint256orHex(&tokenIDRaw)
tokenID, err := helperCommon.ParseUint256orHex(&tokenIDRaw)
if err != nil {
outputter.SetError(fmt.Errorf("failed to decode provided token id %s: %w", tokenIDRaw, err))

Expand Down
5 changes: 3 additions & 2 deletions command/genesis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/secrets/helper"
"github.com/0xPolygon/polygon-edge/secrets/local"
Expand Down Expand Up @@ -81,7 +82,7 @@ func parsePremineInfo(premineInfoRaw string) (*premineInfo, error) {
// <addr>:<balance>
valueRaw := premineInfoRaw[delimiterIdx+1:]

amount, err = types.ParseUint256orHex(&valueRaw)
amount, err = common.ParseUint256orHex(&valueRaw)
if err != nil {
return nil, fmt.Errorf("failed to parse amount %s: %w", valueRaw, err)
}
Expand Down Expand Up @@ -132,7 +133,7 @@ func parseBurnContractInfo(burnContractInfoRaw string) (*polybft.BurnContractInf

blockRaw := burnContractParts[0]

blockNum, err := types.ParseUint64orHex(&blockRaw)
blockNum, err := common.ParseUint64orHex(&blockRaw)
if err != nil {
return nil, fmt.Errorf("failed to parse block number %s: %w", blockRaw, err)
}
Expand Down
9 changes: 4 additions & 5 deletions command/ibft/switch/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/ibft/fork"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/validators"
)

Expand Down Expand Up @@ -136,7 +135,7 @@ func (p *switchParams) initDeployment() error {
)
}

d, err := types.ParseUint64orHex(&p.deploymentRaw)
d, err := common.ParseUint64orHex(&p.deploymentRaw)
if err != nil {
return fmt.Errorf(
"unable to parse deployment value, %w",
Expand Down Expand Up @@ -224,7 +223,7 @@ func (p *switchParams) initPoSConfig() error {
}

if p.minValidatorCountRaw != "" {
value, err := types.ParseUint64orHex(&p.minValidatorCountRaw)
value, err := common.ParseUint64orHex(&p.minValidatorCountRaw)
if err != nil {
return fmt.Errorf(
"unable to parse min validator count value, %w",
Expand All @@ -236,7 +235,7 @@ func (p *switchParams) initPoSConfig() error {
}

if p.maxValidatorCountRaw != "" {
value, err := types.ParseUint64orHex(&p.maxValidatorCountRaw)
value, err := common.ParseUint64orHex(&p.maxValidatorCountRaw)
if err != nil {
return fmt.Errorf(
"unable to parse max validator count value, %w",
Expand Down Expand Up @@ -273,7 +272,7 @@ func (p *switchParams) validateMinMaxValidatorNumber() error {
}

func (p *switchParams) initFrom() error {
from, err := types.ParseUint64orHex(&p.fromRaw)
from, err := common.ParseUint64orHex(&p.fromRaw)
if err != nil {
return fmt.Errorf("unable to parse from value, %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion command/rootchain/helper/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
polybftWallet "github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -203,7 +204,7 @@ func GetValidatorInfo(validatorAddr ethgo.Address, supernetManagerAddr, stakeMan
return nil, err
}

stake, err := types.ParseUint256orHex(&response)
stake, err := common.ParseUint256orHex(&response)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion command/rootchain/supernet/supernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -252,7 +253,7 @@ func getFinalizedStake(owner, validator, stakeManager types.Address, chainID int
return nil, err
}

return types.ParseUint256orHex(&response)
return common.ParseUint256orHex(&response)
}

// validatorSetToABISlice converts given validators to generic map
Expand Down
4 changes: 2 additions & 2 deletions command/server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

"github.com/0xPolygon/polygon-edge/command/server/config"

helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/network/common"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/network"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/server"
"github.com/0xPolygon/polygon-edge/types"
)

var (
Expand Down Expand Up @@ -78,7 +78,7 @@ func (p *serverParams) initLogFileLocation() {
func (p *serverParams) initBlockGasTarget() error {
var parseErr error

if p.blockGasTarget, parseErr = types.ParseUint64orHex(
if p.blockGasTarget, parseErr = helperCommon.ParseUint64orHex(
&p.rawConfig.BlockGasTarget,
); parseErr != nil {
return parseErr
Expand Down
3 changes: 2 additions & 1 deletion command/sidechain/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/umbracle/ethgo"
Expand Down Expand Up @@ -78,7 +79,7 @@ func GetValidatorInfo(validatorAddr ethgo.Address, supernetManager, stakeManager
return nil, err
}

withdrawableRewards, err := types.ParseUint256orHex(&response)
withdrawableRewards, err := common.ParseUint256orHex(&response)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 2b9430c

Please sign in to comment.