@ -851,19 +851,29 @@ func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
amountFloat , _ := af . Float64 ( )
amountFloat , _ := af . Float64 ( )
laf := new ( big . Float ) . SetInt ( l1txs [ i ] . DepositAmount )
laf := new ( big . Float ) . SetInt ( l1txs [ i ] . DepositAmount )
depositAmountFloat , _ := laf . Float64 ( )
depositAmountFloat , _ := laf . Float64 ( )
var effectiveFromIdx * common . Idx
if l1txs [ i ] . UserOrigin {
if l1txs [ i ] . Type != common . TxTypeCreateAccountDeposit &&
l1txs [ i ] . Type != common . TxTypeCreateAccountDepositTransfer {
effectiveFromIdx = & l1txs [ i ] . FromIdx
}
} else {
effectiveFromIdx = & l1txs [ i ] . EffectiveFromIdx
}
txs = append ( txs , txWrite {
txs = append ( txs , txWrite {
// Generic
// Generic
IsL1 : true ,
TxID : l1txs [ i ] . TxID ,
Type : l1txs [ i ] . Type ,
Position : l1txs [ i ] . Position ,
FromIdx : & l1txs [ i ] . FromIdx ,
ToIdx : l1txs [ i ] . ToIdx ,
Amount : l1txs [ i ] . Amount ,
AmountFloat : amountFloat ,
TokenID : l1txs [ i ] . TokenID ,
BatchNum : l1txs [ i ] . BatchNum ,
EthBlockNum : l1txs [ i ] . EthBlockNum ,
IsL1 : true ,
TxID : l1txs [ i ] . TxID ,
Type : l1txs [ i ] . Type ,
Position : l1txs [ i ] . Position ,
FromIdx : & l1txs [ i ] . FromIdx ,
EffectiveFromIdx : effectiveFromIdx ,
ToIdx : l1txs [ i ] . ToIdx ,
Amount : l1txs [ i ] . Amount ,
AmountFloat : amountFloat ,
TokenID : l1txs [ i ] . TokenID ,
BatchNum : l1txs [ i ] . BatchNum ,
EthBlockNum : l1txs [ i ] . EthBlockNum ,
// L1
// L1
ToForgeL1TxsNum : l1txs [ i ] . ToForgeL1TxsNum ,
ToForgeL1TxsNum : l1txs [ i ] . ToForgeL1TxsNum ,
UserOrigin : & l1txs [ i ] . UserOrigin ,
UserOrigin : & l1txs [ i ] . UserOrigin ,
@ -889,16 +899,17 @@ func (hdb *HistoryDB) addL2Txs(d meddler.DB, l2txs []common.L2Tx) error {
amountFloat , _ := f . Float64 ( )
amountFloat , _ := f . Float64 ( )
txs = append ( txs , txWrite {
txs = append ( txs , txWrite {
// Generic
// Generic
IsL1 : false ,
TxID : l2txs [ i ] . TxID ,
Type : l2txs [ i ] . Type ,
Position : l2txs [ i ] . Position ,
FromIdx : & l2txs [ i ] . FromIdx ,
ToIdx : l2txs [ i ] . ToIdx ,
Amount : l2txs [ i ] . Amount ,
AmountFloat : amountFloat ,
BatchNum : & l2txs [ i ] . BatchNum ,
EthBlockNum : l2txs [ i ] . EthBlockNum ,
IsL1 : false ,
TxID : l2txs [ i ] . TxID ,
Type : l2txs [ i ] . Type ,
Position : l2txs [ i ] . Position ,
FromIdx : & l2txs [ i ] . FromIdx ,
EffectiveFromIdx : & l2txs [ i ] . FromIdx ,
ToIdx : l2txs [ i ] . ToIdx ,
Amount : l2txs [ i ] . Amount ,
AmountFloat : amountFloat ,
BatchNum : & l2txs [ i ] . BatchNum ,
EthBlockNum : l2txs [ i ] . EthBlockNum ,
// L2
// L2
Fee : & l2txs [ i ] . Fee ,
Fee : & l2txs [ i ] . Fee ,
Nonce : & l2txs [ i ] . Nonce ,
Nonce : & l2txs [ i ] . Nonce ,
@ -919,6 +930,7 @@ func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
type ,
type ,
position ,
position ,
from_idx ,
from_idx ,
effective_from_idx ,
to_idx ,
to_idx ,
amount ,
amount ,
amount_f ,
amount_f ,
@ -938,17 +950,6 @@ func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
) )
) )
}
}
// // GetTxs returns a list of txs from the DB
// func (hdb *HistoryDB) GetTxs() ([]common.Tx, error) {
// var txs []*common.Tx
// err := meddler.QueryAll(
// hdb.db, &txs,
// `SELECT * FROM tx
// ORDER BY (batch_num, position) ASC`,
// )
// return db.SlicePtrsToSlice(txs).([]common.Tx), err
// }
// GetHistoryTx returns a tx from the DB given a TxID
// GetHistoryTx returns a tx from the DB given a TxID
func ( hdb * HistoryDB ) GetHistoryTx ( txID common . TxID ) ( * TxAPI , error ) {
func ( hdb * HistoryDB ) GetHistoryTx ( txID common . TxID ) ( * TxAPI , error ) {
// Warning: amount_success and deposit_amount_success have true as default for
// Warning: amount_success and deposit_amount_success have true as default for
@ -957,7 +958,7 @@ func (hdb *HistoryDB) GetHistoryTx(txID common.TxID) (*TxAPI, error) {
tx := & TxAPI { }
tx := & TxAPI { }
err := meddler . QueryRow (
err := meddler . QueryRow (
hdb . db , tx , ` SELECT tx . item_id , tx . is_l1 , tx . id , tx . type , tx . position ,
hdb . db , tx , ` SELECT tx . item_id , tx . is_l1 , tx . id , tx . type , tx . position ,
hez_idx ( tx . from_idx , token . symbol ) AS from_idx , tx . from_eth_addr , tx . from_bjj ,
hez_idx ( tx . effective_ from_idx, token . symbol ) AS from_idx , tx . from_eth_addr , tx . from_bjj ,
hez_idx ( tx . to_idx , token . symbol ) AS to_idx , tx . to_eth_addr , tx . to_bjj ,
hez_idx ( tx . to_idx , token . symbol ) AS to_idx , tx . to_eth_addr , tx . to_bjj ,
tx . amount , tx . amount_success , tx . token_id , tx . amount_usd ,
tx . amount , tx . amount_success , tx . token_id , tx . amount_usd ,
tx . batch_num , tx . eth_block_num , tx . to_forge_l1_txs_num , tx . user_origin ,
tx . batch_num , tx . eth_block_num , tx . to_forge_l1_txs_num , tx . user_origin ,
@ -988,7 +989,7 @@ func (hdb *HistoryDB) GetHistoryTxs(
var query string
var query string
var args [ ] interface { }
var args [ ] interface { }
queryStr := ` SELECT tx . item_id , tx . is_l1 , tx . id , tx . type , tx . position ,
queryStr := ` SELECT tx . item_id , tx . is_l1 , tx . id , tx . type , tx . position ,
hez_idx ( tx . from_idx , token . symbol ) AS from_idx , tx . from_eth_addr , tx . from_bjj ,
hez_idx ( tx . effective_ from_idx, token . symbol ) AS from_idx , tx . from_eth_addr , tx . from_bjj ,
hez_idx ( tx . to_idx , token . symbol ) AS to_idx , tx . to_eth_addr , tx . to_bjj ,
hez_idx ( tx . to_idx , token . symbol ) AS to_idx , tx . to_eth_addr , tx . to_bjj ,
tx . amount , tx . amount_success , tx . token_id , tx . amount_usd ,
tx . amount , tx . amount_success , tx . token_id , tx . amount_usd ,
tx . batch_num , tx . eth_block_num , tx . to_forge_l1_txs_num , tx . user_origin ,
tx . batch_num , tx . eth_block_num , tx . to_forge_l1_txs_num , tx . user_origin ,
@ -1028,7 +1029,7 @@ func (hdb *HistoryDB) GetHistoryTxs(
} else {
} else {
queryStr += "WHERE "
queryStr += "WHERE "
}
}
queryStr += "(tx.from_idx = ? OR tx.to_idx = ?) "
queryStr += "(tx.effective_ from_idx = ? OR tx.to_idx = ?) "
args = append ( args , idx , idx )
args = append ( args , idx , idx )
nextIsAnd = true
nextIsAnd = true
}
}
@ -1245,7 +1246,7 @@ func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
err := meddler . QueryAll (
err := meddler . QueryAll (
hdb . db , & txs , // Note that '\x' gets parsed as a big.Int with value = 0
hdb . db , & txs , // Note that '\x' gets parsed as a big.Int with value = 0
` SELECT tx . id , tx . to_forge_l1_txs_num , tx . position , tx . user_origin ,
` 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 . from_idx , tx . effective_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 . amount , ( CASE WHEN tx . batch_num IS NULL THEN NULL WHEN tx . amount_success THEN tx . amount ELSE ' \ x ' END ) AS effective_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 . 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
tx . eth_block_num , tx . type , tx . batch_num
@ -1262,7 +1263,7 @@ func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
err := meddler . QueryAll (
err := meddler . QueryAll (
hdb . db , & txs ,
hdb . db , & txs ,
` SELECT tx . id , tx . to_forge_l1_txs_num , tx . position , tx . user_origin ,
` 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 . from_idx , tx . effective_from_idx , tx . from_eth_addr , tx . from_bjj , tx . to_idx , tx . token_id ,
tx . amount , tx . amount AS effective_amount ,
tx . amount , tx . amount AS effective_amount ,
tx . deposit_amount , tx . deposit_amount AS effective_deposit_amount ,
tx . deposit_amount , tx . deposit_amount AS effective_deposit_amount ,
tx . eth_block_num , tx . type , tx . batch_num
tx . eth_block_num , tx . type , tx . batch_num
@ -1480,19 +1481,21 @@ func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
return tracerr . Wrap ( txn . Commit ( ) )
return tracerr . Wrap ( txn . Commit ( ) )
}
}
// setL1UserTxEffectiveAmounts sets the EffectiveAmount and EffectiveDepositAmount
// of the given l1UserTxs (with an UPDATE)
func ( hdb * HistoryDB ) setL1UserTxEffectiveAmount s ( d sqlx . Ext , txs [ ] common . L1Tx ) error {
// setExtraInfoForgedL1UserTxs sets the EffectiveAmount, EffectiveDepositAmount
// and EffectiveFromIdx of the given l1UserTxs (with an UPDATE)
func ( hdb * HistoryDB ) setExtraInfoForged L1UserTxs ( d sqlx . Ext , txs [ ] common . L1Tx ) error {
if len ( txs ) == 0 {
if len ( txs ) == 0 {
return nil
return nil
}
}
// Effective amounts are stored as success flags in the DB, with true value by default
// 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
// to reduce the amount of updates. Therefore, only amounts that became uneffective should be
// updated to become false
// updated to become false. At the same time, all the txs that contain
// accounts (FromIdx == 0) are updated to set the EffectiveFromIdx.
type txUpdate struct {
type txUpdate struct {
ID common . TxID ` db:"id" `
ID common . TxID ` db:"id" `
AmountSuccess bool ` db:"amount_success" `
AmountSuccess bool ` db:"amount_success" `
DepositAmountSuccess bool ` db:"deposit_amount_success" `
DepositAmountSuccess bool ` db:"deposit_amount_success" `
EffectiveFromIdx common . Idx ` db:"effective_from_idx" `
}
}
txUpdates := [ ] txUpdate { }
txUpdates := [ ] txUpdate { }
equal := func ( a * big . Int , b * big . Int ) bool {
equal := func ( a * big . Int , b * big . Int ) bool {
@ -1501,22 +1504,24 @@ func (hdb *HistoryDB) setL1UserTxEffectiveAmounts(d sqlx.Ext, txs []common.L1Tx)
for i := range txs {
for i := range txs {
amountSuccess := equal ( txs [ i ] . Amount , txs [ i ] . EffectiveAmount )
amountSuccess := equal ( txs [ i ] . Amount , txs [ i ] . EffectiveAmount )
depositAmountSuccess := equal ( txs [ i ] . DepositAmount , txs [ i ] . EffectiveDepositAmount )
depositAmountSuccess := equal ( txs [ i ] . DepositAmount , txs [ i ] . EffectiveDepositAmount )
if ! amountSuccess || ! depositAmountSuccess {
if ! amountSuccess || ! depositAmountSuccess || txs [ i ] . FromIdx == 0 {
txUpdates = append ( txUpdates , txUpdate {
txUpdates = append ( txUpdates , txUpdate {
ID : txs [ i ] . TxID ,
ID : txs [ i ] . TxID ,
AmountSuccess : amountSuccess ,
AmountSuccess : amountSuccess ,
DepositAmountSuccess : depositAmountSuccess ,
DepositAmountSuccess : depositAmountSuccess ,
EffectiveFromIdx : txs [ i ] . EffectiveFromIdx ,
} )
} )
}
}
}
}
const query string = `
const query string = `
UPDATE tx SET
UPDATE tx SET
amount_success = tx_update . amount_success ,
amount_success = tx_update . amount_success ,
deposit_amount_success = tx_update . deposit_amount_success
deposit_amount_success = tx_update . deposit_amount_success ,
effective_from_idx = tx_update . effective_from_idx
FROM ( VALUES
FROM ( VALUES
( NULL : : : : BYTEA , NULL : : : : BOOL , NULL : : : : BOOL ) ,
( : id , : amount_success , : deposit_amount_success )
) as tx_update ( id , amount_success , deposit_amount_success )
( NULL : : : : BYTEA , NULL : : : : BOOL , NULL : : : : BOOL , NULL : : : : BIGINT ) ,
( : id , : amount_success , : deposit_amount_success , : effective_from_idx )
) as tx_update ( id , amount_success , deposit_amount_success , effective_from_idx )
WHERE tx . id = tx_update . id ;
WHERE tx . id = tx_update . id ;
`
`
if len ( txUpdates ) > 0 {
if len ( txUpdates ) > 0 {
@ -1599,14 +1604,14 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
return tracerr . Wrap ( err )
return tracerr . Wrap ( err )
}
}
// Set the EffectiveAmount and EffectiveDepositAmount of all the
// L1UserTxs that have been forged in this batch
if err = hdb . setL1UserTxEffectiveAmounts ( txn , batch . L1UserTxs ) ; err != nil {
// Add accounts
if err := hdb . addAccounts ( txn , batch . CreatedAccounts ) ; err != nil {
return tracerr . Wrap ( err )
return tracerr . Wrap ( err )
}
}
// Add accounts
if err := hdb . addAccounts ( txn , batch . CreatedAccounts ) ; err != nil {
// Set the EffectiveAmount and EffectiveDepositAmount of all the
// L1UserTxs that have been forged in this batch
if err = hdb . setExtraInfoForgedL1UserTxs ( txn , batch . L1UserTxs ) ; err != nil {
return tracerr . Wrap ( err )
return tracerr . Wrap ( err )
}
}