Add idx for txs that creat account, step1

This commit is contained in:
Arnau B
2021-01-13 13:18:32 +01:00
committed by Eduard S
parent 6651df1e52
commit 50fc607306
6 changed files with 76 additions and 69 deletions

View File

@@ -304,6 +304,7 @@ func TestMain(m *testing.M) {
// Insert block into HistoryDB // Insert block into HistoryDB
// nolint reason: block is used as read only in the function // nolint reason: block is used as read only in the function
if err := api.h.AddBlockSCData(&block); err != nil { //nolint:gosec if err := api.h.AddBlockSCData(&block); err != nil { //nolint:gosec
log.Error(err)
panic(err) panic(err)
} }
// Extract data // Extract data

View File

@@ -143,9 +143,14 @@ func genTestTxs(
bn := common.BatchNum(*tx.L1Info.ToForgeL1TxsNum + 2) bn := common.BatchNum(*tx.L1Info.ToForgeL1TxsNum + 2)
tx.BatchNum = &bn tx.BatchNum = &bn
} }
// If FromIdx is not nil // TODO: User L1 txs that create txs will have fromAccountIndex equal to the idx of the
idxStr := idxToHez(l1.FromIdx, token.Symbol) // created account. Once this is done this test will be broken and will need to be updated here.
tx.FromIdx = &idxStr // At the moment they are null
if l1.Type != common.TxTypeCreateAccountDeposit &&
l1.Type != common.TxTypeCreateAccountDepositTransfer {
idxStr := idxToHez(l1.FromIdx, token.Symbol)
tx.FromIdx = &idxStr
}
// If tx has a normal ToIdx (>255), set FromEthAddr and FromBJJ // If tx has a normal ToIdx (>255), set FromEthAddr and FromBJJ
if l1.ToIdx >= common.UserThreshold { if l1.ToIdx >= common.UserThreshold {
// find account // find account

View File

@@ -851,19 +851,25 @@ 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].Type != common.TxTypeCreateAccountDeposit &&
l1txs[i].Type != common.TxTypeCreateAccountDepositTransfer {
effectiveFromIdx = &l1txs[i].FromIdx
}
txs = append(txs, txWrite{ txs = append(txs, txWrite{
// Generic // Generic
IsL1: true, IsL1: true,
TxID: l1txs[i].TxID, TxID: l1txs[i].TxID,
Type: l1txs[i].Type, Type: l1txs[i].Type,
Position: l1txs[i].Position, Position: l1txs[i].Position,
FromIdx: &l1txs[i].FromIdx, FromIdx: &l1txs[i].FromIdx,
ToIdx: l1txs[i].ToIdx, EffectiveFromIdx: effectiveFromIdx,
Amount: l1txs[i].Amount, ToIdx: l1txs[i].ToIdx,
AmountFloat: amountFloat, Amount: l1txs[i].Amount,
TokenID: l1txs[i].TokenID, AmountFloat: amountFloat,
BatchNum: l1txs[i].BatchNum, TokenID: l1txs[i].TokenID,
EthBlockNum: l1txs[i].EthBlockNum, 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 +895,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, IsL1: false,
TxID: l2txs[i].TxID, TxID: l2txs[i].TxID,
Type: l2txs[i].Type, Type: l2txs[i].Type,
Position: l2txs[i].Position, Position: l2txs[i].Position,
FromIdx: &l2txs[i].FromIdx, FromIdx: &l2txs[i].FromIdx,
ToIdx: l2txs[i].ToIdx, EffectiveFromIdx: &l2txs[i].FromIdx,
Amount: l2txs[i].Amount, ToIdx: l2txs[i].ToIdx,
AmountFloat: amountFloat, Amount: l2txs[i].Amount,
BatchNum: &l2txs[i].BatchNum, AmountFloat: amountFloat,
EthBlockNum: l2txs[i].EthBlockNum, 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 +926,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 +946,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 +954,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 +985,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 +1025,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
} }
@@ -1480,9 +1477,10 @@ func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
return tracerr.Wrap(txn.Commit()) return tracerr.Wrap(txn.Commit())
} }
// setL1UserTxEffectiveAmounts sets the EffectiveAmount and EffectiveDepositAmount // setExtraInfoForgedL1UserTxs sets the EffectiveAmount and EffectiveDepositAmount
// of the given l1UserTxs (with an UPDATE) // of the given l1UserTxs (with an UPDATE)
func (hdb *HistoryDB) setL1UserTxEffectiveAmounts(d sqlx.Ext, txs []common.L1Tx) error { // TODO: Set effective_from_idx for txs that create accounts
func (hdb *HistoryDB) setExtraInfoForgedL1UserTxs(d sqlx.Ext, txs []common.L1Tx) error {
if len(txs) == 0 { if len(txs) == 0 {
return nil return nil
} }
@@ -1599,14 +1597,15 @@ 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 // Add accounts
// L1UserTxs that have been forged in this batch if err := hdb.addAccounts(txn, batch.CreatedAccounts); err != nil {
if err = hdb.setL1UserTxEffectiveAmounts(txn, batch.L1UserTxs); err != nil {
return tracerr.Wrap(err) return tracerr.Wrap(err)
} }
// Add accounts // Set the EffectiveAmount and EffectiveDepositAmount of all the
if err := hdb.addAccounts(txn, batch.CreatedAccounts); err != nil { // L1UserTxs that have been forged in this batch
// TODO: Set also effective_from_idx for txs that create accounts
if err = hdb.setExtraInfoForgedL1UserTxs(txn, batch.L1UserTxs); err != nil {
return tracerr.Wrap(err) return tracerr.Wrap(err)
} }

View File

@@ -752,7 +752,7 @@ func TestSetInitialSCVars(t *testing.T) {
require.Equal(t, wDelayer, dbWDelayer) require.Equal(t, wDelayer, dbWDelayer)
} }
func TestSetL1UserTxEffectiveAmounts(t *testing.T) { func TestSetExtraInfoForgedL1UserTxs(t *testing.T) {
test.WipeDB(historyDB.DB()) test.WipeDB(historyDB.DB())
set := ` set := `
@@ -789,7 +789,7 @@ func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
// Add second batch to trigger the update of the batch_num, // Add second batch to trigger the update of the batch_num,
// while avoiding the implicit call of setL1UserTxEffectiveAmounts // while avoiding the implicit call of setExtraInfoForgedL1UserTxs
err = historyDB.addBlock(historyDB.db, &blocks[1].Block) err = historyDB.addBlock(historyDB.db, &blocks[1].Block)
assert.NoError(t, err) assert.NoError(t, err)
err = historyDB.addBatch(historyDB.db, &blocks[1].Rollup.Batches[0].Batch) err = historyDB.addBatch(historyDB.db, &blocks[1].Rollup.Batches[0].Batch)
@@ -803,7 +803,7 @@ func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
l1Txs[1].EffectiveAmount = big.NewInt(0) l1Txs[1].EffectiveAmount = big.NewInt(0)
l1Txs[2].EffectiveDepositAmount = big.NewInt(0) l1Txs[2].EffectiveDepositAmount = big.NewInt(0)
l1Txs[2].EffectiveAmount = big.NewInt(0) l1Txs[2].EffectiveAmount = big.NewInt(0)
err = historyDB.setL1UserTxEffectiveAmounts(historyDB.db, l1Txs) err = historyDB.setExtraInfoForgedL1UserTxs(historyDB.db, l1Txs)
require.NoError(t, err) require.NoError(t, err)
dbL1Txs, err := historyDB.GetAllL1UserTxs() dbL1Txs, err := historyDB.GetAllL1UserTxs()

View File

@@ -123,17 +123,18 @@ func (tx TxAPI) MarshalJSON() ([]byte, error) {
// EffectiveAmount and EffectiveDepositAmount are not set since they have default values in the DB // EffectiveAmount and EffectiveDepositAmount are not set since they have default values in the DB
type txWrite struct { type txWrite struct {
// Generic // Generic
IsL1 bool `meddler:"is_l1"` IsL1 bool `meddler:"is_l1"`
TxID common.TxID `meddler:"id"` TxID common.TxID `meddler:"id"`
Type common.TxType `meddler:"type"` Type common.TxType `meddler:"type"`
Position int `meddler:"position"` Position int `meddler:"position"`
FromIdx *common.Idx `meddler:"from_idx"` FromIdx *common.Idx `meddler:"from_idx"`
ToIdx common.Idx `meddler:"to_idx"` EffectiveFromIdx *common.Idx `meddler:"effective_from_idx"`
Amount *big.Int `meddler:"amount,bigint"` ToIdx common.Idx `meddler:"to_idx"`
AmountFloat float64 `meddler:"amount_f"` Amount *big.Int `meddler:"amount,bigint"`
TokenID common.TokenID `meddler:"token_id"` AmountFloat float64 `meddler:"amount_f"`
BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0 TokenID common.TokenID `meddler:"token_id"`
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue 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 // L1
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged 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 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
@@ -375,24 +376,24 @@ type RollupVariablesAPI struct {
// AuctionVariablesAPI are the variables of the Auction Smart Contract // AuctionVariablesAPI are the variables of the Auction Smart Contract
type AuctionVariablesAPI struct { type AuctionVariablesAPI struct {
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
// Donation Address // DonationAddress Address where the donations will be sent
DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"` DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
// Boot Coordinator Address // BootCoordinator Address of the boot coordinator
BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"` BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
// Boot Coordinator URL // BootCoordinatorURL URL of the boot coordinator
BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"` BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"`
// The minimum bid value in a series of 6 slots // DefaultSlotSetBid The minimum bid value in a series of 6 slots
DefaultSlotSetBid [6]*apitypes.BigIntStr `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"` DefaultSlotSetBid [6]*apitypes.BigIntStr `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
// SlotNum at which the new default_slot_set_bid applies // DefaultSlotSetBidSlotNum SlotNum at which the new default_slot_set_bid applies
DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"` DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"`
// Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min ) // ClosedAuctionSlots Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"` ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"`
// Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots ) // OpenAuctionSlots Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"` OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"`
// How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%) // AllocationRatio How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"` AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"`
// Minimum outbid (percentage) over the previous one to consider it valid // Outbidding Minimum outbid (percentage) over the previous one to consider it valid
Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"` Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"`
// Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before // SlotDeadline Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"` SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"`
} }

View File

@@ -149,6 +149,7 @@ CREATE TABLE tx (
type VARCHAR(40) NOT NULL, type VARCHAR(40) NOT NULL,
position INT NOT NULL, position INT NOT NULL,
from_idx BIGINT, from_idx BIGINT,
effective_from_idx BIGINT REFERENCES account (idx) ON DELETE SET NULL,
from_eth_addr BYTEA, from_eth_addr BYTEA,
from_bjj BYTEA, from_bjj BYTEA,
to_idx BIGINT NOT NULL, to_idx BIGINT NOT NULL,