Skip to content

Commit

Permalink
Merge branch 'develop' into Implement-Analyze-Methods-sendTransaction…
Browse files Browse the repository at this point in the history
…_accounts_signTransaction_sendPrivateTransaction
  • Loading branch information
novosandara committed Mar 27, 2024
2 parents 75ececc + 307c7bb commit 9d88475
Show file tree
Hide file tree
Showing 44 changed files with 881 additions and 1,289 deletions.
38 changes: 11 additions & 27 deletions blockchain/storagev2/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ type levelDB struct {
}

var tableMapper = map[uint8][]byte{
// Main DB
storagev2.BODY: []byte("b"), // DB key = block number + block hash + mapper, value = block body
storagev2.DIFFICULTY: []byte("d"), // DB key = block number + block hash + mapper, value = block total diffculty
storagev2.HEADER: []byte("h"), // DB key = block number + block hash + mapper, value = block header
storagev2.RECEIPTS: []byte("r"), // DB key = block number + block hash + mapper, value = block receipts
storagev2.CANONICAL: {}, // DB key = block number + mapper, value = block hash

// Lookup DB
storagev2.FORK: {}, // DB key = FORK_KEY + mapper, value = fork hashes
storagev2.HEAD_HASH: {}, // DB key = HEAD_HASH_KEY + mapper, value = head hash
storagev2.HEAD_NUMBER: {}, // DB key = HEAD_NUMBER_KEY + mapper, value = head number
storagev2.BLOCK_LOOKUP: {}, // DB key = block hash + mapper, value = block number
storagev2.TX_LOOKUP: {}, // DB key = tx hash + mapper, value = block number
storagev2.BODY: []byte("b"), // DB key = block number + block hash + mapper, value = block body
storagev2.DIFFICULTY: []byte("d"), // DB key = block number + block hash + mapper, value = block total diffculty
storagev2.HEADER: []byte("h"), // DB key = block number + block hash + mapper, value = block header
storagev2.RECEIPTS: []byte("r"), // DB key = block number + block hash + mapper, value = block receipts
storagev2.CANONICAL: {}, // DB key = block number + mapper, value = block hash
storagev2.FORK: {}, // DB key = FORK_KEY + mapper, value = fork hashes
storagev2.HEAD_HASH: {}, // DB key = HEAD_HASH_KEY + mapper, value = head hash
storagev2.HEAD_NUMBER: {}, // DB key = HEAD_NUMBER_KEY + mapper, value = head number
storagev2.BLOCK_LOOKUP: {}, // DB key = block hash + mapper, value = block number
storagev2.TX_LOOKUP: {}, // DB key = tx hash + mapper, value = block number
}

// NewLevelDBStorage creates the new storage reference with leveldb default options
Expand All @@ -44,21 +41,8 @@ func NewLevelDBStorage(path string, logger hclog.Logger) (*storagev2.Storage, er
return nil, err
}

// Open Lookup
// Set default options
options = &opt.Options{
BlockCacheCapacity: 64 * opt.MiB,
WriteBuffer: opt.DefaultWriteBuffer,
}
path += "/lookup"

lookup, err := openLevelDBStorage(path, options)
if err != nil {
return nil, err
}

ldbs[0] = &levelDB{maindb}
ldbs[1] = &levelDB{lookup}
ldbs[1] = nil

return storagev2.Open(logger.Named("leveldb"), ldbs)
}
Expand Down
7 changes: 6 additions & 1 deletion blockchain/storagev2/storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:stylecheck
package storagev2

import (
Expand Down Expand Up @@ -39,6 +38,8 @@ const (
)

// Lookup tables
//
//nolint:stylecheck // needed because linter considers _ in name as an error
const (
FORK = uint8(0) | LOOKUP_INDEX
HEAD_HASH = uint8(2) | LOOKUP_INDEX
Expand All @@ -47,14 +48,18 @@ const (
TX_LOOKUP = uint8(8) | LOOKUP_INDEX
)

//nolint:stylecheck // needed because linter considers _ in name as an error
const MAX_TABLES = uint8(20)

// Database indexes
//
//nolint:stylecheck // needed because linter considers _ in name as an error
const (
MAINDB_INDEX = uint8(0)
LOOKUP_INDEX = uint8(1)
)

//nolint:stylecheck // needed because linter considers _ in name as an error
var (
FORK_KEY = []byte("0000000f")
HEAD_HASH_KEY = []byte("0000000h")
Expand Down
16 changes: 8 additions & 8 deletions command/bridge/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"math/big"
"sync"

"github.com/0xPolygon/polygon-edge/jsonrpc"
"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"
"golang.org/x/sync/errgroup"

"github.com/0xPolygon/polygon-edge/chain"
Expand Down Expand Up @@ -368,7 +368,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

client, err := jsonrpc.NewClient(params.jsonRPCAddress)
client, err := jsonrpc.NewEthClient(params.jsonRPCAddress)
if err != nil {
outputter.SetError(fmt.Errorf("failed to initialize JSON RPC client for provided IP address: %s: %w",
params.jsonRPCAddress, err))
Expand All @@ -377,7 +377,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

if consensusCfg.Bridge != nil {
code, err := client.Eth().GetCode(ethgo.Address(consensusCfg.Bridge.StateSenderAddr), ethgo.Latest)
code, err := client.GetCode(consensusCfg.Bridge.StateSenderAddr, jsonrpc.LatestBlockNumberOrHash)
if err != nil {
outputter.SetError(fmt.Errorf("failed to check if rootchain contracts are deployed: %w", err))

Expand All @@ -394,7 +394,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
// set event tracker start blocks for rootchain contract(s) of interest
// the block number should be queried before deploying contracts so that no events during deployment
// and initialization are missed
blockNum, err := client.Eth().BlockNumber()
blockNum, err := client.BlockNumber()
if err != nil {
outputter.SetError(fmt.Errorf("failed to query rootchain latest block number: %w", err))

Expand Down Expand Up @@ -434,7 +434,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// deployContracts deploys and initializes rootchain smart contracts
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client, chainID int64,
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.EthClient, chainID int64,
initialValidators []*validator.GenesisValidator, cmdCtx context.Context) (deploymentResultInfo, error) {
txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(client), txrelayer.WithWriter(outputter),
txrelayer.WithReceiptsTimeout(params.txTimeout))
Expand Down Expand Up @@ -474,7 +474,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
if !consensusCfg.NativeTokenConfig.IsMintable {
if params.rootERC20TokenAddr != "" {
// use existing root chain ERC20 token
if err := populateExistingTokenAddr(client.Eth(),
if err := populateExistingTokenAddr(client,
params.rootERC20TokenAddr, rootERC20Name, rootchainConfig); err != nil {
return deploymentResultInfo{RootchainCfg: nil, CommandResults: nil}, err
}
Expand Down Expand Up @@ -697,11 +697,11 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,

// populateExistingTokenAddr checks whether given token is deployed on the provided address.
// If it is, then its address is set to the rootchain config, otherwise an error is returned
func populateExistingTokenAddr(eth *jsonrpc.Eth, tokenAddr, tokenName string,
func populateExistingTokenAddr(eth *jsonrpc.EthClient, tokenAddr, tokenName string,
rootchainCfg *polybft.RootchainConfig) error {
addr := types.StringToAddress(tokenAddr)

code, err := eth.GetCode(ethgo.Address(addr), ethgo.Latest)
code, err := eth.GetCode(addr, jsonrpc.LatestBlockNumberOrHash)
if err != nil {
return fmt.Errorf("failed to check is %s token deployed: %w", tokenName, err)
} else if code == "0x" {
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"
"testing"

"github.com/0xPolygon/polygon-edge/jsonrpc"
"github.com/stretchr/testify/require"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/testutil"

"github.com/0xPolygon/polygon-edge/command"
Expand All @@ -27,7 +27,7 @@ func TestDeployContracts_NoPanics(t *testing.T) {
}
})

client, err := jsonrpc.NewClient(server.HTTPAddr())
client, err := jsonrpc.NewEthClient(server.HTTPAddr())
require.NoError(t, err)

testKey, err := helper.DecodePrivateKey("")
Expand Down
8 changes: 3 additions & 5 deletions command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"time"

"github.com/spf13/cobra"
"github.com/umbracle/ethgo/jsonrpc"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/command/bridge/helper"
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/jsonrpc"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -118,7 +118,7 @@ func run(cmd *cobra.Command, _ []string) {
return
}

childClient, err := jsonrpc.NewClient(ep.childJSONRPCAddr)
childClient, err := jsonrpc.NewEthClient(ep.childJSONRPCAddr)
if err != nil {
outputter.SetError(fmt.Errorf("could not create child chain JSON RPC client: %w", err))

Expand All @@ -127,9 +127,7 @@ func run(cmd *cobra.Command, _ []string) {

// acquire proof for given exit event
var proof types.Proof

err = childClient.Call(generateExitProofFn, &proof, fmt.Sprintf("0x%x", ep.exitID))
if err != nil {
if err = childClient.EndpointCall(generateExitProofFn, &proof, fmt.Sprintf("0x%x", ep.exitID)); err != nil {
outputter.SetError(fmt.Errorf("failed to get exit proof (exit id=%d): %w", ep.exitID, err))

return
Expand Down
14 changes: 6 additions & 8 deletions command/regenesis/get_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package regenesis
import (
"fmt"

"github.com/0xPolygon/polygon-edge/command"
"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/jsonrpc"
)

var (
jsonRPCAddress string
blockNumber int64
)

/*
./polygon-edge regenesis getroot --rpc "http://localhost:10002"
*/
func GetRootCMD() *cobra.Command {
getRootCmd := &cobra.Command{
Use: "getroot",
Expand All @@ -26,21 +24,21 @@ func GetRootCMD() *cobra.Command {
outputter := command.InitializeOutputter(getRootCmd)
defer outputter.WriteOutput()

rpcClient, err := jsonrpc.NewClient(jsonRPCAddress)
rpcClient, err := jsonrpc.NewEthClient(jsonRPCAddress)
if err != nil {
outputter.SetError(fmt.Errorf("connect to client error:%w", err))

return
}

block, err := rpcClient.Eth().GetBlockByNumber(ethgo.BlockNumber(blockNumber), false)
block, err := rpcClient.GetBlockByNumber(jsonrpc.BlockNumber(blockNumber), false)
if err != nil {
outputter.SetError(fmt.Errorf("get block error:%w", err))

return
}

_, err = outputter.Write([]byte(fmt.Sprintf("state root %s for block %d\n", block.StateRoot, block.Number)))
_, err = outputter.Write([]byte(fmt.Sprintf("state root %s for block %d\n", block.Header.StateRoot, block.Number())))
if err != nil {
outputter.SetError(fmt.Errorf("get block error:%w", err))

Expand Down
2 changes: 1 addition & 1 deletion command/validator/registration/register_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return err
}

rootChainID, err := txRelayer.Client().Eth().ChainID()
rootChainID, err := txRelayer.Client().ChainID()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/checkpoint_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"testing"

"github.com/umbracle/ethgo/jsonrpc"
"github.com/0xPolygon/polygon-edge/jsonrpc"

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
Expand Down Expand Up @@ -495,7 +495,7 @@ func (d *dummyTxRelayer) SendTransactionLocal(txn *types.Transaction) (*ethgo.Re
return args.Get(0).(*ethgo.Receipt), args.Error(1)
}

func (d *dummyTxRelayer) Client() *jsonrpc.Client {
func (d *dummyTxRelayer) Client() *jsonrpc.EthClient {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/stake_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/jsonrpc"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/abi"
"github.com/umbracle/ethgo/jsonrpc"
)

func TestStakeManager_PostBlock(t *testing.T) {
Expand Down Expand Up @@ -534,6 +534,6 @@ func (d *dummyStakeTxRelayer) SendTransactionLocal(txn *types.Transaction) (*eth
return args.Get(0).(*ethgo.Receipt), args.Error(1)
}

func (d *dummyStakeTxRelayer) Client() *jsonrpc.Client {
func (d *dummyStakeTxRelayer) Client() *jsonrpc.EthClient {
return nil
}
17 changes: 9 additions & 8 deletions e2e-polybft/e2e/acls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ func TestE2E_AllowList_ContractDeployment(t *testing.T) {
deployTxn := cluster.Deploy(t, target, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Reverted())
require.False(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))
require.False(t, cluster.ExistsCode(t, types.Address(deployTxn.Receipt().ContractAddress)))
}

{
// Step 3. 'adminAddr' can create contracts
deployTxn := cluster.Deploy(t, admin, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Succeed())
require.True(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))

proxyContract = types.Address(deployTxn.Receipt().ContractAddress)

require.True(t, deployTxn.Succeed())
require.True(t, cluster.ExistsCode(t, proxyContract))
}

{
Expand All @@ -104,7 +105,7 @@ func TestE2E_AllowList_ContractDeployment(t *testing.T) {
deployTxn := cluster.Deploy(t, target, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Succeed())
require.True(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))
require.True(t, cluster.ExistsCode(t, types.Address(deployTxn.Receipt().ContractAddress)))
}

{
Expand Down Expand Up @@ -168,15 +169,15 @@ func TestE2E_BlockList_ContractDeployment(t *testing.T) {
deployTxn := cluster.Deploy(t, target, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Succeed())
require.True(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))
require.True(t, cluster.ExistsCode(t, types.Address(deployTxn.Receipt().ContractAddress)))
}

{
// Step 3. 'adminAddr' can create contracts
deployTxn := cluster.Deploy(t, admin, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Succeed())
require.True(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))
require.True(t, cluster.ExistsCode(t, types.Address(deployTxn.Receipt().ContractAddress)))
}

{
Expand All @@ -193,7 +194,7 @@ func TestE2E_BlockList_ContractDeployment(t *testing.T) {
deployTxn := cluster.Deploy(t, target, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Reverted())
require.False(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))
require.False(t, cluster.ExistsCode(t, types.Address(deployTxn.Receipt().ContractAddress)))
}

{
Expand Down Expand Up @@ -256,7 +257,7 @@ func TestE2E_AllowList_Transactions(t *testing.T) {
deployTxn := cluster.Deploy(t, target, bytecode)
require.NoError(t, deployTxn.Wait())
require.True(t, deployTxn.Reverted())
require.False(t, cluster.ExistsCode(t, deployTxn.Receipt().ContractAddress))
require.False(t, cluster.ExistsCode(t, types.Address(deployTxn.Receipt().ContractAddress)))
}

{
Expand Down
Loading

0 comments on commit 9d88475

Please sign in to comment.