mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Rename load amount to deposit amount
This commit is contained in:
@@ -767,23 +767,23 @@ func (hdb *HistoryDB) GetAllAccounts() ([]common.Account, error) {
|
||||
return db.SlicePtrsToSlice(accs).([]common.Account), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// AddL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
|
||||
// AddL1Txs inserts L1 txs to the DB. USD and DepositAmountUSD will be set automatically before storing the tx.
|
||||
// If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
|
||||
// BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
|
||||
// EffectiveAmount and EffectiveLoadAmount are seted with default values by the DB.
|
||||
// EffectiveAmount and EffectiveDepositAmount are seted with default values by the DB.
|
||||
func (hdb *HistoryDB) AddL1Txs(l1txs []common.L1Tx) error { return hdb.addL1Txs(hdb.db, l1txs) }
|
||||
|
||||
// addL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
|
||||
// addL1Txs inserts L1 txs to the DB. USD and DepositAmountUSD will be set automatically before storing the tx.
|
||||
// If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
|
||||
// BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
|
||||
// EffectiveAmount and EffectiveLoadAmount are seted with default values by the DB.
|
||||
// EffectiveAmount and EffectiveDepositAmount are seted with default values by the DB.
|
||||
func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
|
||||
txs := []txWrite{}
|
||||
for i := 0; i < len(l1txs); i++ {
|
||||
af := new(big.Float).SetInt(l1txs[i].Amount)
|
||||
amountFloat, _ := af.Float64()
|
||||
laf := new(big.Float).SetInt(l1txs[i].LoadAmount)
|
||||
loadAmountFloat, _ := laf.Float64()
|
||||
laf := new(big.Float).SetInt(l1txs[i].DepositAmount)
|
||||
depositAmountFloat, _ := laf.Float64()
|
||||
txs = append(txs, txWrite{
|
||||
// Generic
|
||||
IsL1: true,
|
||||
@@ -798,12 +798,12 @@ func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
|
||||
BatchNum: l1txs[i].BatchNum,
|
||||
EthBlockNum: l1txs[i].EthBlockNum,
|
||||
// L1
|
||||
ToForgeL1TxsNum: l1txs[i].ToForgeL1TxsNum,
|
||||
UserOrigin: &l1txs[i].UserOrigin,
|
||||
FromEthAddr: &l1txs[i].FromEthAddr,
|
||||
FromBJJ: l1txs[i].FromBJJ,
|
||||
LoadAmount: l1txs[i].LoadAmount,
|
||||
LoadAmountFloat: &loadAmountFloat,
|
||||
ToForgeL1TxsNum: l1txs[i].ToForgeL1TxsNum,
|
||||
UserOrigin: &l1txs[i].UserOrigin,
|
||||
FromEthAddr: &l1txs[i].FromEthAddr,
|
||||
FromBJJ: l1txs[i].FromBJJ,
|
||||
DepositAmount: l1txs[i].DepositAmount,
|
||||
DepositAmountFloat: &depositAmountFloat,
|
||||
})
|
||||
}
|
||||
return hdb.addTxs(d, txs)
|
||||
@@ -857,8 +857,8 @@ func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
|
||||
user_origin,
|
||||
from_eth_addr,
|
||||
from_bjj,
|
||||
load_amount,
|
||||
load_amount_f,
|
||||
deposit_amount,
|
||||
deposit_amount_f,
|
||||
fee,
|
||||
nonce
|
||||
) VALUES %s;`,
|
||||
@@ -887,7 +887,7 @@ func (hdb *HistoryDB) GetHistoryTx(txID common.TxID) (*TxAPI, error) {
|
||||
hez_idx(tx.to_idx, token.symbol) AS to_idx, tx.to_eth_addr, tx.to_bjj,
|
||||
tx.amount, tx.token_id, tx.amount_usd,
|
||||
tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin,
|
||||
tx.load_amount, tx.load_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
|
||||
tx.deposit_amount, tx.deposit_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
|
||||
token.token_id, token.item_id AS token_item_id, token.eth_block_num AS token_block,
|
||||
token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
|
||||
token.usd_update, block.timestamp
|
||||
@@ -916,7 +916,7 @@ func (hdb *HistoryDB) GetHistoryTxs(
|
||||
hez_idx(tx.to_idx, token.symbol) AS to_idx, tx.to_eth_addr, tx.to_bjj,
|
||||
tx.amount, tx.token_id, tx.amount_usd,
|
||||
tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin,
|
||||
tx.load_amount, tx.load_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
|
||||
tx.deposit_amount, tx.deposit_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
|
||||
token.token_id, token.item_id AS token_item_id, token.eth_block_num AS token_block,
|
||||
token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
|
||||
token.usd_update, block.timestamp, count(*) OVER() AS total_items
|
||||
@@ -1169,7 +1169,7 @@ func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
|
||||
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
|
||||
tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id,
|
||||
tx.amount, (CASE WHEN tx.batch_num IS NULL THEN NULL WHEN tx.amount_success THEN tx.amount ELSE '\x' END) AS effective_amount,
|
||||
tx.load_amount, (CASE WHEN tx.batch_num IS NULL THEN NULL WHEN tx.load_amount_success THEN tx.load_amount ELSE '\x' END) AS effective_load_amount,
|
||||
tx.deposit_amount, (CASE WHEN tx.batch_num IS NULL THEN NULL WHEN tx.deposit_amount_success THEN tx.deposit_amount ELSE '\x' END) AS effective_deposit_amount,
|
||||
tx.eth_block_num, tx.type, tx.batch_num
|
||||
FROM tx WHERE is_l1 = TRUE AND user_origin = TRUE;`,
|
||||
)
|
||||
@@ -1186,7 +1186,7 @@ func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
|
||||
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
|
||||
tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id,
|
||||
tx.amount, tx.amount AS effective_amount,
|
||||
tx.load_amount, tx.load_amount AS effective_load_amount,
|
||||
tx.deposit_amount, tx.deposit_amount AS effective_deposit_amount,
|
||||
tx.eth_block_num, tx.type, tx.batch_num
|
||||
FROM tx WHERE is_l1 = TRUE AND user_origin = FALSE;`,
|
||||
)
|
||||
@@ -1214,7 +1214,7 @@ func (hdb *HistoryDB) GetUnforgedL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx
|
||||
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
|
||||
tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id,
|
||||
tx.amount, NULL AS effective_amount,
|
||||
tx.load_amount, NULL AS effective_load_amount,
|
||||
tx.deposit_amount, NULL AS effective_deposit_amount,
|
||||
tx.eth_block_num, tx.type, tx.batch_num
|
||||
FROM tx WHERE batch_num IS NULL AND to_forge_l1_txs_num = $1;`,
|
||||
toForgeL1TxsNum,
|
||||
@@ -1298,16 +1298,16 @@ func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
|
||||
return tracerr.Wrap(txn.Commit())
|
||||
}
|
||||
|
||||
// setL1UserTxEffectiveAmounts sets the EffectiveAmount and EffectiveLoadAmount
|
||||
// setL1UserTxEffectiveAmounts sets the EffectiveAmount and EffectiveDepositAmount
|
||||
// of the given l1UserTxs (with an UPDATE)
|
||||
func (hdb *HistoryDB) setL1UserTxEffectiveAmounts(d sqlx.Ext, txs []common.L1Tx) error {
|
||||
// Effective amounts are stored as success flags in the DB, with true value by default
|
||||
// to reduce the amount of updates. Therefore, only amounts that became uneffective should be
|
||||
// updated to become false
|
||||
type txUpdate struct {
|
||||
ID common.TxID `db:"id"`
|
||||
AmountSuccess bool `db:"amount_success"`
|
||||
LoadAmountSuccess bool `db:"load_amount_success"`
|
||||
ID common.TxID `db:"id"`
|
||||
AmountSuccess bool `db:"amount_success"`
|
||||
DepositAmountSuccess bool `db:"deposit_amount_success"`
|
||||
}
|
||||
txUpdates := []txUpdate{}
|
||||
equal := func(a *big.Int, b *big.Int) bool {
|
||||
@@ -1315,23 +1315,23 @@ func (hdb *HistoryDB) setL1UserTxEffectiveAmounts(d sqlx.Ext, txs []common.L1Tx)
|
||||
}
|
||||
for i := range txs {
|
||||
amountSuccess := equal(txs[i].Amount, txs[i].EffectiveAmount)
|
||||
loadAmountSuccess := equal(txs[i].LoadAmount, txs[i].EffectiveLoadAmount)
|
||||
if !amountSuccess || !loadAmountSuccess {
|
||||
depositAmountSuccess := equal(txs[i].DepositAmount, txs[i].EffectiveDepositAmount)
|
||||
if !amountSuccess || !depositAmountSuccess {
|
||||
txUpdates = append(txUpdates, txUpdate{
|
||||
ID: txs[i].TxID,
|
||||
AmountSuccess: amountSuccess,
|
||||
LoadAmountSuccess: loadAmountSuccess,
|
||||
ID: txs[i].TxID,
|
||||
AmountSuccess: amountSuccess,
|
||||
DepositAmountSuccess: depositAmountSuccess,
|
||||
})
|
||||
}
|
||||
}
|
||||
const query string = `
|
||||
UPDATE tx SET
|
||||
amount_success = tx_update.amount_success,
|
||||
load_amount_success = tx_update.load_amount_success
|
||||
deposit_amount_success = tx_update.deposit_amount_success
|
||||
FROM (VALUES
|
||||
(NULL::::BYTEA, NULL::::BOOL, NULL::::BOOL),
|
||||
(:id, :amount_success, :load_amount_success)
|
||||
) as tx_update (id, amount_success, load_amount_success)
|
||||
(:id, :amount_success, :deposit_amount_success)
|
||||
) as tx_update (id, amount_success, deposit_amount_success)
|
||||
WHERE tx.id = tx_update.id
|
||||
`
|
||||
if len(txUpdates) > 0 {
|
||||
@@ -1414,7 +1414,7 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
for i := range blockData.Rollup.Batches {
|
||||
batch := &blockData.Rollup.Batches[i]
|
||||
|
||||
// Set the EffectiveAmount and EffectiveLoadAmount of all the
|
||||
// Set the EffectiveAmount and EffectiveDepositAmount of all the
|
||||
// L1UserTxs that have been forged in this batch
|
||||
if len(batch.L1UserTxs) > 0 {
|
||||
if err = hdb.setL1UserTxEffectiveAmounts(txn, batch.L1UserTxs); err != nil {
|
||||
|
||||
@@ -517,16 +517,16 @@ func TestTxs(t *testing.T) {
|
||||
assert.Equal(t, true, dbL1Txs[9].UserOrigin)
|
||||
|
||||
// Load Amount
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[0].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[1].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(20), dbL1Txs[2].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[3].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[4].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[5].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), dbL1Txs[6].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), dbL1Txs[7].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[8].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[9].LoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[0].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[1].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(20), dbL1Txs[2].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[3].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[4].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[5].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), dbL1Txs[6].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), dbL1Txs[7].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[8].DepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), dbL1Txs[9].DepositAmount)
|
||||
|
||||
// Check saved txID's batch_num is not nil
|
||||
assert.Equal(t, txID, dbL1Txs[len(dbL1Txs)-2].TxID)
|
||||
@@ -736,12 +736,12 @@ func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Set the Effective{Amount,LoadAmount} of the L1UserTxs that are forged in the second block
|
||||
// Set the Effective{Amount,DepositAmount} of the L1UserTxs that are forged in the second block
|
||||
l1Txs := blocks[1].Rollup.Batches[0].L1UserTxs
|
||||
require.Equal(t, 3, len(l1Txs))
|
||||
// Change some values to test all cases
|
||||
l1Txs[1].EffectiveAmount = big.NewInt(0)
|
||||
l1Txs[2].EffectiveLoadAmount = big.NewInt(0)
|
||||
l1Txs[2].EffectiveDepositAmount = big.NewInt(0)
|
||||
l1Txs[2].EffectiveAmount = big.NewInt(0)
|
||||
err = historyDB.setL1UserTxEffectiveAmounts(historyDB.db, l1Txs)
|
||||
require.NoError(t, err)
|
||||
@@ -749,18 +749,18 @@ func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
|
||||
dbL1Txs, err := historyDB.GetAllL1UserTxs()
|
||||
require.NoError(t, err)
|
||||
for i, tx := range dbL1Txs {
|
||||
log.Infof("%d %v %v", i, tx.EffectiveAmount, tx.EffectiveLoadAmount)
|
||||
log.Infof("%d %v %v", i, tx.EffectiveAmount, tx.EffectiveDepositAmount)
|
||||
assert.NotNil(t, tx.EffectiveAmount)
|
||||
assert.NotNil(t, tx.EffectiveLoadAmount)
|
||||
assert.NotNil(t, tx.EffectiveDepositAmount)
|
||||
switch tx.TxID {
|
||||
case l1Txs[0].TxID:
|
||||
assert.Equal(t, l1Txs[0].LoadAmount, tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, l1Txs[0].DepositAmount, tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, l1Txs[0].Amount, tx.EffectiveAmount)
|
||||
case l1Txs[1].TxID:
|
||||
assert.Equal(t, l1Txs[1].LoadAmount, tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, l1Txs[1].DepositAmount, tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
|
||||
case l1Txs[2].TxID:
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,10 +32,10 @@ type TxAPI struct {
|
||||
BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
|
||||
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
// L1
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
LoadAmount *apitypes.BigIntStr `meddler:"load_amount"`
|
||||
HistoricLoadAmountUSD *float64 `meddler:"load_amount_usd"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
DepositAmount *apitypes.BigIntStr `meddler:"deposit_amount"`
|
||||
HistoricDepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
|
||||
// L2
|
||||
Fee *common.FeeSelector `meddler:"fee"`
|
||||
HistoricFeeUSD *float64 `meddler:"fee_usd"`
|
||||
@@ -93,8 +93,8 @@ func (tx TxAPI) MarshalJSON() ([]byte, error) {
|
||||
jsonTx["L1Info"] = map[string]interface{}{
|
||||
"toForgeL1TransactionsNum": tx.ToForgeL1TxsNum,
|
||||
"userOrigin": tx.UserOrigin,
|
||||
"loadAmount": tx.LoadAmount,
|
||||
"historicLoadAmountUSD": tx.HistoricLoadAmountUSD,
|
||||
"depositAmount": tx.DepositAmount,
|
||||
"historicDepositAmountUSD": tx.HistoricDepositAmountUSD,
|
||||
"ethereumBlockNum": tx.EthBlockNum,
|
||||
}
|
||||
} else {
|
||||
@@ -125,12 +125,12 @@ type txWrite struct {
|
||||
BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
|
||||
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
// L1
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
|
||||
LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
|
||||
LoadAmountFloat *float64 `meddler:"load_amount_f"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
|
||||
DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
|
||||
DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
|
||||
// L2
|
||||
Fee *common.FeeSelector `meddler:"fee"`
|
||||
Nonce *common.Nonce `meddler:"nonce"`
|
||||
|
||||
@@ -135,14 +135,14 @@ FOR EACH ROW EXECUTE PROCEDURE set_token_usd_update();
|
||||
|
||||
CREATE SEQUENCE tx_item_id;
|
||||
|
||||
-- important note about "amount_success" and "load_amount_success" (only relevant to L1 user txs):
|
||||
-- important note about "amount_success" and "deposit_amount_success" (only relevant to L1 user txs):
|
||||
-- The behaviour should be:
|
||||
-- When tx is not forged: amount_success = false, load_amount_success = false
|
||||
-- When tx is not forged: amount_success = false, deposit_amount_success = false
|
||||
-- When tx is forged:
|
||||
-- amount_success = false if the "effective amount" is 0, else true
|
||||
-- load_amount_success = false if the "effective load amount" is 0, else true
|
||||
-- deposit_amount_success = false if the "effective load amount" is 0, else true
|
||||
--
|
||||
-- However, in order to reduce the amount of updates, by default amount_success and load_amount_success will be set to true (when tx is unforged)
|
||||
-- However, in order to reduce the amount of updates, by default amount_success and deposit_amount_success will be set to true (when tx is unforged)
|
||||
-- whne they should be false. This can be worked around at a query level by checking if "batch_num IS NULL" (which indicates that the tx is unforged).
|
||||
CREATE TABLE tx (
|
||||
-- Generic TX
|
||||
@@ -167,10 +167,10 @@ CREATE TABLE tx (
|
||||
-- L1
|
||||
to_forge_l1_txs_num BIGINT,
|
||||
user_origin BOOLEAN,
|
||||
load_amount BYTEA,
|
||||
load_amount_success BOOLEAN NOT NULL DEFAULT true,
|
||||
load_amount_f NUMERIC,
|
||||
load_amount_usd NUMERIC,
|
||||
deposit_amount BYTEA,
|
||||
deposit_amount_success BOOLEAN NOT NULL DEFAULT true,
|
||||
deposit_amount_f NUMERIC,
|
||||
deposit_amount_usd NUMERIC,
|
||||
-- L2
|
||||
fee INT,
|
||||
fee_usd NUMERIC,
|
||||
@@ -463,8 +463,8 @@ BEGIN
|
||||
IF NEW.user_origin IS NULL OR
|
||||
NEW.from_eth_addr IS NULL OR
|
||||
NEW.from_bjj IS NULL OR
|
||||
NEW.load_amount IS NULL OR
|
||||
NEW.load_amount_f IS NULL OR
|
||||
NEW.deposit_amount IS NULL OR
|
||||
NEW.deposit_amount_f IS NULL OR
|
||||
(NOT NEW.user_origin AND NEW.batch_num IS NULL) THEN -- If is Coordinator L1, must include batch_num
|
||||
RAISE EXCEPTION 'Invalid L1 tx: %', NEW;
|
||||
END IF;
|
||||
@@ -493,8 +493,8 @@ BEGIN
|
||||
NEW."fee_usd" = (SELECT NEW."amount_usd" * fee_percentage(NEW.fee::NUMERIC));
|
||||
END IF;
|
||||
END IF;
|
||||
IF NEW."is_l1" AND NEW."load_amount_f" > 0.0 THEN
|
||||
NEW."load_amount_usd" = (SELECT _value * NEW.load_amount_f);
|
||||
IF NEW."is_l1" AND NEW."deposit_amount_f" > 0.0 THEN
|
||||
NEW."deposit_amount_usd" = (SELECT _value * NEW.deposit_amount_f);
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
@@ -455,11 +455,11 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
s.zki.OnChain[s.i] = big.NewInt(1)
|
||||
|
||||
// L1Txs
|
||||
loadAmountF16, err := common.NewFloat16(tx.LoadAmount)
|
||||
depositAmountF16, err := common.NewFloat16(tx.DepositAmount)
|
||||
if err != nil {
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.LoadAmountF[s.i] = big.NewInt(int64(loadAmountF16))
|
||||
s.zki.DepositAmountF[s.i] = big.NewInt(int64(depositAmountF16))
|
||||
s.zki.FromEthAddr[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
|
||||
if tx.FromBJJ != nil {
|
||||
s.zki.FromBJJCompressed[s.i] = BJJCompressedTo256BigInts(tx.FromBJJ.Compress())
|
||||
@@ -660,7 +660,7 @@ func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
|
||||
account := &common.Account{
|
||||
TokenID: tx.TokenID,
|
||||
Nonce: 0,
|
||||
Balance: tx.EffectiveLoadAmount,
|
||||
Balance: tx.EffectiveDepositAmount,
|
||||
PublicKey: tx.FromBJJ,
|
||||
EthAddr: tx.FromEthAddr,
|
||||
}
|
||||
@@ -676,7 +676,7 @@ func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
|
||||
s.zki.Sign1[s.i] = big.NewInt(1)
|
||||
}
|
||||
s.zki.Ay1[s.i] = tx.FromBJJ.Y
|
||||
s.zki.Balance1[s.i] = tx.EffectiveLoadAmount
|
||||
s.zki.Balance1[s.i] = tx.EffectiveDepositAmount
|
||||
s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
|
||||
s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
|
||||
if p.IsOld0 {
|
||||
@@ -704,12 +704,12 @@ func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
|
||||
// andTransfer parameter is set to true, the method will also apply the
|
||||
// Transfer of the L1Tx/DepositTransfer
|
||||
func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// deposit the tx.EffectiveLoadAmount into the sender account
|
||||
// deposit the tx.EffectiveDepositAmount into the sender account
|
||||
accSender, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
accSender.Balance = new(big.Int).Add(accSender.Balance, tx.EffectiveLoadAmount)
|
||||
accSender.Balance = new(big.Int).Add(accSender.Balance, tx.EffectiveDepositAmount)
|
||||
|
||||
// in case that the tx is a L1Tx>DepositTransfer
|
||||
var accReceiver *common.Account
|
||||
@@ -883,7 +883,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
accSender := &common.Account{
|
||||
TokenID: tx.TokenID,
|
||||
Nonce: 0,
|
||||
Balance: tx.EffectiveLoadAmount,
|
||||
Balance: tx.EffectiveDepositAmount,
|
||||
PublicKey: tx.FromBJJ,
|
||||
EthAddr: tx.FromEthAddr,
|
||||
}
|
||||
@@ -908,7 +908,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
s.zki.Sign1[s.i] = big.NewInt(1)
|
||||
}
|
||||
s.zki.Ay1[s.i] = tx.FromBJJ.Y
|
||||
s.zki.Balance1[s.i] = tx.EffectiveLoadAmount
|
||||
s.zki.Balance1[s.i] = tx.EffectiveDepositAmount
|
||||
s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
|
||||
s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
|
||||
if p.IsOld0 {
|
||||
@@ -1039,20 +1039,20 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
|
||||
if !tx.UserOrigin {
|
||||
// case where the L1Tx is generated by the Coordinator
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
tx.EffectiveLoadAmount = big.NewInt(0)
|
||||
tx.EffectiveDepositAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
tx.EffectiveAmount = tx.Amount
|
||||
tx.EffectiveLoadAmount = tx.LoadAmount
|
||||
tx.EffectiveDepositAmount = tx.DepositAmount
|
||||
if tx.Type == common.TxTypeCreateAccountDeposit {
|
||||
return
|
||||
}
|
||||
|
||||
if tx.ToIdx >= common.UserThreshold && tx.FromIdx == common.Idx(0) {
|
||||
// CreateAccountDepositTransfer case
|
||||
cmp := tx.LoadAmount.Cmp(tx.Amount)
|
||||
if cmp == -1 { // LoadAmount<Amount
|
||||
cmp := tx.DepositAmount.Cmp(tx.Amount)
|
||||
if cmp == -1 { // DepositAmount<Amount
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
@@ -1061,24 +1061,24 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
|
||||
|
||||
accSender, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
log.Debugf("EffectiveAmount & EffectiveLoadAmount = 0: can not get account for tx.FromIdx: %d", tx.FromIdx)
|
||||
tx.EffectiveLoadAmount = big.NewInt(0)
|
||||
log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.FromIdx: %d", tx.FromIdx)
|
||||
tx.EffectiveDepositAmount = big.NewInt(0)
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
// check that tx.TokenID corresponds to the Sender account TokenID
|
||||
if tx.TokenID != accSender.TokenID {
|
||||
log.Debugf("EffectiveAmount & EffectiveLoadAmount = 0: tx.TokenID (%d) !=sender account TokenID (%d)", tx.TokenID, accSender.TokenID)
|
||||
tx.EffectiveLoadAmount = big.NewInt(0)
|
||||
log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx.TokenID (%d) !=sender account TokenID (%d)", tx.TokenID, accSender.TokenID)
|
||||
tx.EffectiveDepositAmount = big.NewInt(0)
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
// check that Sender has enough balance
|
||||
bal := accSender.Balance
|
||||
if tx.LoadAmount != nil {
|
||||
bal = new(big.Int).Add(bal, tx.EffectiveLoadAmount)
|
||||
if tx.DepositAmount != nil {
|
||||
bal = new(big.Int).Add(bal, tx.EffectiveDepositAmount)
|
||||
}
|
||||
cmp := bal.Cmp(tx.Amount)
|
||||
if cmp == -1 {
|
||||
@@ -1090,8 +1090,8 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
|
||||
// check that the tx.FromEthAddr is the same than the EthAddress of the
|
||||
// Sender
|
||||
if !bytes.Equal(tx.FromEthAddr.Bytes(), accSender.EthAddr.Bytes()) {
|
||||
log.Debugf("EffectiveAmount & EffectiveLoadAmount = 0: tx.FromEthAddr (%s) must be the same EthAddr of the sender account by the Idx (%s)", tx.FromEthAddr.Hex(), accSender.EthAddr.Hex())
|
||||
tx.EffectiveLoadAmount = big.NewInt(0)
|
||||
log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx.FromEthAddr (%s) must be the same EthAddr of the sender account by the Idx (%s)", tx.FromEthAddr.Hex(), accSender.EthAddr.Hex())
|
||||
tx.EffectiveDepositAmount = big.NewInt(0)
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
@@ -1104,14 +1104,14 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
|
||||
// check that TokenID is the same for Sender & Receiver account
|
||||
accReceiver, err := s.GetAccount(tx.ToIdx)
|
||||
if err != nil {
|
||||
log.Debugf("EffectiveAmount & EffectiveLoadAmount = 0: can not get account for tx.ToIdx: %d", tx.ToIdx)
|
||||
tx.EffectiveLoadAmount = big.NewInt(0)
|
||||
log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.ToIdx: %d", tx.ToIdx)
|
||||
tx.EffectiveDepositAmount = big.NewInt(0)
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
if accSender.TokenID != accReceiver.TokenID {
|
||||
log.Debugf("EffectiveAmount & EffectiveLoadAmount = 0: sender account TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accSender.TokenID)
|
||||
tx.EffectiveLoadAmount = big.NewInt(0)
|
||||
log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: sender account TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accSender.TokenID)
|
||||
tx.EffectiveDepositAmount = big.NewInt(0)
|
||||
tx.EffectiveAmount = big.NewInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -58,80 +58,80 @@ func TestCheckL1TxInvalidData(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
tx := common.L1Tx{
|
||||
FromIdx: 256,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(10),
|
||||
LoadAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["A"].Addr,
|
||||
UserOrigin: true,
|
||||
FromIdx: 256,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(10),
|
||||
DepositAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["A"].Addr,
|
||||
UserOrigin: true,
|
||||
}
|
||||
sdb.computeEffectiveAmounts(&tx)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
|
||||
|
||||
// expect error due not enough funds
|
||||
tx = common.L1Tx{
|
||||
FromIdx: 256,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(11),
|
||||
LoadAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["A"].Addr,
|
||||
UserOrigin: true,
|
||||
FromIdx: 256,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(11),
|
||||
DepositAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["A"].Addr,
|
||||
UserOrigin: true,
|
||||
}
|
||||
sdb.computeEffectiveAmounts(&tx)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
|
||||
|
||||
// expect no-error due not enough funds in a
|
||||
// CreateAccountDepositTransfer transction
|
||||
tx = common.L1Tx{
|
||||
FromIdx: 0,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(10),
|
||||
LoadAmount: big.NewInt(10),
|
||||
UserOrigin: true,
|
||||
FromIdx: 0,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(10),
|
||||
DepositAmount: big.NewInt(10),
|
||||
UserOrigin: true,
|
||||
}
|
||||
sdb.computeEffectiveAmounts(&tx)
|
||||
assert.Equal(t, big.NewInt(10), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
|
||||
|
||||
// expect error due not enough funds in a CreateAccountDepositTransfer
|
||||
// transction
|
||||
tx = common.L1Tx{
|
||||
FromIdx: 0,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(11),
|
||||
LoadAmount: big.NewInt(10),
|
||||
UserOrigin: true,
|
||||
FromIdx: 0,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(11),
|
||||
DepositAmount: big.NewInt(10),
|
||||
UserOrigin: true,
|
||||
}
|
||||
sdb.computeEffectiveAmounts(&tx)
|
||||
assert.Equal(t, big.NewInt(10), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(10), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
|
||||
|
||||
// expect error due not same TokenID
|
||||
tx = common.L1Tx{
|
||||
FromIdx: 256,
|
||||
ToIdx: 258,
|
||||
Amount: big.NewInt(5),
|
||||
LoadAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["A"].Addr,
|
||||
UserOrigin: true,
|
||||
FromIdx: 256,
|
||||
ToIdx: 258,
|
||||
Amount: big.NewInt(5),
|
||||
DepositAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["A"].Addr,
|
||||
UserOrigin: true,
|
||||
}
|
||||
sdb.computeEffectiveAmounts(&tx)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
|
||||
|
||||
// expect error due not same EthAddr
|
||||
tx = common.L1Tx{
|
||||
FromIdx: 256,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(8),
|
||||
LoadAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["B"].Addr,
|
||||
UserOrigin: true,
|
||||
FromIdx: 256,
|
||||
ToIdx: 257,
|
||||
Amount: big.NewInt(8),
|
||||
DepositAmount: big.NewInt(0),
|
||||
FromEthAddr: tc.Users["B"].Addr,
|
||||
UserOrigin: true,
|
||||
}
|
||||
sdb.computeEffectiveAmounts(&tx)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
|
||||
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
|
||||
}
|
||||
|
||||
@@ -555,15 +555,15 @@ func TestProcessTxsRootTestVectors(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
l1Txs := []common.L1Tx{
|
||||
{
|
||||
FromIdx: 0,
|
||||
LoadAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
FromIdx: 0,
|
||||
DepositAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
},
|
||||
}
|
||||
l2Txs := []common.PoolL2Tx{
|
||||
@@ -611,15 +611,15 @@ func TestCircomTest(t *testing.T) {
|
||||
|
||||
l1Txs := []common.L1Tx{
|
||||
{
|
||||
FromIdx: 0,
|
||||
LoadAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
FromIdx: 0,
|
||||
DepositAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
},
|
||||
}
|
||||
l2Txs := []common.PoolL2Tx{
|
||||
@@ -700,15 +700,15 @@ func TestZKInputsHashTestVector0(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
l1Txs := []common.L1Tx{
|
||||
{
|
||||
FromIdx: 0,
|
||||
LoadAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
FromIdx: 0,
|
||||
DepositAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
},
|
||||
}
|
||||
l2Txs := []common.PoolL2Tx{
|
||||
@@ -778,26 +778,26 @@ func TestZKInputsHashTestVector1(t *testing.T) {
|
||||
l1Txs := []common.L1Tx{
|
||||
{
|
||||
FromIdx: 0,
|
||||
// LoadAmount: big.NewInt(10400),
|
||||
LoadAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
// DepositAmount: big.NewInt(10400),
|
||||
DepositAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj0,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
},
|
||||
{
|
||||
FromIdx: 0,
|
||||
LoadAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj1,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x2b5ad5c4795c026514f8317c7a215e218dccd6cf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
FromIdx: 0,
|
||||
DepositAmount: big.NewInt(16000000),
|
||||
Amount: big.NewInt(0),
|
||||
TokenID: 1,
|
||||
FromBJJ: bjj1,
|
||||
FromEthAddr: ethCommon.HexToAddress("0x2b5ad5c4795c026514f8317c7a215e218dccd6cf"),
|
||||
ToIdx: 0,
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
UserOrigin: true,
|
||||
},
|
||||
}
|
||||
l2Txs := []common.PoolL2Tx{
|
||||
|
||||
Reference in New Issue
Block a user