Skip to content

Commit

Permalink
Merge branch 'develop' into improve/sequencer-adding-logs-to-worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Psykepro committed Feb 27, 2023
2 parents c16b85a + 7ea3e76 commit 9a9e06b
Show file tree
Hide file tree
Showing 27 changed files with 525 additions and 134 deletions.
15 changes: 6 additions & 9 deletions cmd/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"strings"

"github.com/0xPolygonHermez/zkevm-node/config"
"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/urfave/cli/v2"
)

func approveTokens(ctx *cli.Context) error {
amountArg := ctx.String(config.FlagAmount)
amount, _ := new(big.Float).SetString(amountArg)
amount, _ := new(big.Int).SetString(amountArg, encoding.Base10)
if amount == nil {
fmt.Println("Please, introduce a valid amount. Use '.' instead of ',' if it is a decimal number")
fmt.Println("Please, introduce a valid amount in wei")
return nil
}

Expand All @@ -27,8 +28,8 @@ func approveTokens(ctx *cli.Context) error {
}

if !ctx.Bool(config.FlagYes) {
fmt.Print("*WARNING* Are you sure you want to approve ", amount,
" tokens to be spent by the smc <Name: PoE. Address: "+c.Etherman.PoEAddr.String()+">? [y/N]: ")
fmt.Print("*WARNING* Are you sure you want to approve ", amount.String(),
" tokens (in wei) for the smc <Name: PoE. Address: "+c.Etherman.PoEAddr.String()+">? [y/N]: ")
var input string
if _, err := fmt.Scanln(&input); err != nil {
return err
Expand All @@ -55,11 +56,7 @@ func approveTokens(ctx *cli.Context) error {
return err
}

const decimals = 1000000000000000000
amountInWei := new(big.Float).Mul(amount, big.NewFloat(decimals))
amountB := new(big.Int)
amountInWei.Int(amountB)
tx, err := etherman.ApproveMatic(ctx.Context, auth.From, amountB, c.Etherman.PoEAddr)
tx, err := etherman.ApproveMatic(ctx.Context, auth.From, amount, c.Etherman.PoEAddr)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ func Test_Defaults(t *testing.T) {
},
{
path: "Etherman.PoEAddr",
expectedValue: common.HexToAddress("0x8A791620dd6260079BF849Dc5567aDC3F2FdC318"),
expectedValue: common.HexToAddress("0x610178dA211FEF7D417bC0e6FeD39F05609AD788"),
},
{
path: "Etherman.MaticAddr",
expectedValue: common.HexToAddress("0x5FbDB2315678afecb367f032d93F642f64180aa3"),
},
{
path: "Etherman.GlobalExitRootManagerAddr",
expectedValue: common.HexToAddress("0xa513E6E4b8f2a923D98304ec87F64353C4D5C853"),
expectedValue: common.HexToAddress("0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"),
},
{
path: "Etherman.MultiGasProvider",
Expand Down
4 changes: 2 additions & 2 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ FreeClaimGasLimit = 150000
[Etherman]
URL = "http://localhost:8545"
L1ChainID = 1337
PoEAddr = "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318"
PoEAddr = "0x610178dA211FEF7D417bC0e6FeD39F05609AD788"
MaticAddr = "0x5FbDB2315678afecb367f032d93F642f64180aa3"
GlobalExitRootManagerAddr = "0xa513E6E4b8f2a923D98304ec87F64353C4D5C853"
GlobalExitRootManagerAddr = "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"
MultiGasProvider = true
[Etherman.Etherscan]
ApiKey = ""
Expand Down
8 changes: 7 additions & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/etherman/etherscan"
"github.com/0xPolygonHermez/zkevm-node/etherman/ethgasstation"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/matic"
Expand Down Expand Up @@ -463,7 +464,12 @@ func (etherMan *Client) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVe
var newStateRoot [32]byte
copy(newStateRoot[:], inputs.NewStateRoot)

proof := common.Hex2Bytes(inputs.FinalProof.Proof)
log.Info("Proof before trim: ", inputs.FinalProof.Proof)
proof, err := encoding.DecodeBytes(&inputs.FinalProof.Proof)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode proof, err: %w", err)
}
log.Info("Proof after trim: %v", common.Bytes2Hex(proof))

const pendStateNum = 0 // TODO hardcoded for now until we implement the pending state feature

Expand Down
6 changes: 4 additions & 2 deletions ethtxmanager/ethtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ func (c *Client) Add(ctx context.Context, owner, id string, from common.Address,
// get gas
gas, err := c.etherman.EstimateGas(ctx, from, to, value, data)
if err != nil {
err := fmt.Errorf("failed to estimate gas: %w", err)
cad := common.Bytes2Hex(data)
err := fmt.Errorf("failed to estimate gas: %w, data: %v", err, cad)
log.Errorf(err.Error())
return err
gas = 1000000
// return err
}
// get gas price
gasPrice, err := c.etherman.SuggestedGasPrice(ctx)
Expand Down
7 changes: 7 additions & 0 deletions pool/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ var (
// ErrInsufficientFunds is returned if the total cost of executing a transaction
// is higher than the balance of the user's account.
ErrInsufficientFunds = errors.New("insufficient funds for gas * price + value")

// ErrIntrinsicGas is returned if the transaction is specified to use less gas
// than required to start the invocation.
ErrIntrinsicGas = errors.New("intrinsic gas too low")

// ErrGasUintOverflow is returned when calculating gas usage.
ErrGasUintOverflow = errors.New("gas uint64 overflow")
)
51 changes: 51 additions & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error {
return ErrInsufficientFunds
}

// Ensure the transaction has more gas than the basic tx fee.
intrGas, err := IntrinsicGas(tx)
if err != nil {
return err
}
if tx.Gas() < intrGas {
return ErrIntrinsicGas
}

// try to get a transaction from the pool with the same nonce to check
// if the new one has a price bump
oldTxs, err := p.storage.GetTxsByFromAndNonce(ctx, from, tx.Nonce())
Expand Down Expand Up @@ -286,3 +295,45 @@ func (p *Pool) DeleteReorgedTransactions(ctx context.Context, transactions []*ty

return p.storage.DeleteTransactionsByHashes(ctx, hashes)
}

const (
txDataNonZeroGas uint64 = 16
txGasContractCreation uint64 = 53000
txGas uint64 = 21000
txDataZeroGas uint64 = 4
)

// IntrinsicGas computes the 'intrinsic gas' for a given transaction.
func IntrinsicGas(tx types.Transaction) (uint64, error) {
// Set the starting gas for the raw transaction
var gas uint64
if tx.To() == nil {
gas = txGasContractCreation
} else {
gas = txGas
}
dataLen := uint64(len(tx.Data()))
// Bump the required gas by the amount of transactional data
if dataLen > 0 {
// Zero and non-zero bytes are priced differently
var nz uint64
for _, byt := range tx.Data() {
if byt != 0 {
nz++
}
}
// Make sure we don't exceed uint64 for all data combinations
nonZeroGas := txDataNonZeroGas
if (math.MaxUint64-gas)/nonZeroGas < nz {
return 0, ErrGasUintOverflow
}
gas += nz * nonZeroGas

z := dataLen - nz
if (math.MaxUint64-gas)/txDataZeroGas < z {
return 0, ErrGasUintOverflow
}
gas += z * txDataZeroGas
}
return gas, nil
}
Loading

0 comments on commit 9a9e06b

Please sign in to comment.