diff --git a/api/state.go b/api/state.go index f17812b..aa5133b 100644 --- a/api/state.go +++ b/api/state.go @@ -109,6 +109,24 @@ func (a *API) UpdateNetworkInfo( a.status.Network.LastBatch = lastBatch a.status.Network.CurrentSlot = currentSlot a.status.Network.NextForgers = nextForgers + + // Update buckets withdrawals + bucketsUpdate, err := a.h.GetBucketUpdates() + if tracerr.Unwrap(err) == sql.ErrNoRows { + bucketsUpdate = nil + } else if err != nil { + return tracerr.Wrap(err) + } + + for i, bucketParams := range a.status.Rollup.Buckets { + for _, bucketUpdate := range bucketsUpdate { + if bucketUpdate.NumBucket == i { + bucketParams.Withdrawals = bucketUpdate.Withdrawals + a.status.Rollup.Buckets[i] = bucketParams + break + } + } + } a.status.Unlock() return nil } diff --git a/api/state_test.go b/api/state_test.go index e629118..c63a637 100644 --- a/api/state_test.go +++ b/api/state_test.go @@ -1,11 +1,13 @@ package api import ( + "math/big" "testing" "github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/db/historydb" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type testStatus struct { @@ -55,12 +57,39 @@ func TestUpdateNetworkInfo(t *testing.T) { lastBlock := tc.blocks[3] lastBatchNum := common.BatchNum(3) currentSlotNum := int64(1) - err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) + + // Generate some bucket_update data + bucketUpdates := []common.BucketUpdate{ + { + EthBlockNum: 4, + NumBucket: 0, + BlockStamp: 4, + Withdrawals: big.NewInt(123), + }, + { + EthBlockNum: 5, + NumBucket: 2, + BlockStamp: 5, + Withdrawals: big.NewInt(42), + }, + { + EthBlockNum: 5, + NumBucket: 2, // Repeated bucket + BlockStamp: 5, + Withdrawals: big.NewInt(43), + }, + } + err := api.h.AddBucketUpdatesTest(api.h.DB(), bucketUpdates) + require.NoError(t, err) + + err = api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) assert.NoError(t, err) assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock) assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum) assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot) assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers)) + assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, big.NewInt(123)) + assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, big.NewInt(43)) } func TestUpdateMetrics(t *testing.T) { @@ -111,6 +140,10 @@ func TestGetState(t *testing.T) { assert.NoError(t, doGoodReq("GET", endpoint, nil, &status)) // SC vars + // UpdateNetworkInfo will overwrite buckets withdrawal values + // So we restore them before comparing, they are checked at + // TestUpdateNetworkInfo + status.Rollup.Buckets = tc.rollupVars.Buckets assert.Equal(t, tc.rollupVars, status.Rollup) assert.Equal(t, tc.auctionVars, status.Auction) assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer) diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index 84ed11b..e0a5995 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -1344,6 +1344,12 @@ func (hdb *HistoryDB) addBucketUpdates(d meddler.DB, bucketUpdates []common.Buck )) } +// AddBucketUpdatesTest allows call to unexported method +// only for internal testing purposes +func (hdb *HistoryDB) AddBucketUpdatesTest(d meddler.DB, bucketUpdates []common.BucketUpdate) error { + return hdb.addBucketUpdates(d, bucketUpdates) +} + // GetAllBucketUpdates retrieves all the bucket updates func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) { var bucketUpdates []*common.BucketUpdate @@ -1355,6 +1361,19 @@ func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) { return db.SlicePtrsToSlice(bucketUpdates).([]common.BucketUpdate), tracerr.Wrap(err) } +// GetBucketUpdates retrieves latest values for each bucket +func (hdb *HistoryDB) GetBucketUpdates() ([]common.BucketUpdate, error) { + var bucketUpdates []*common.BucketUpdate + err := meddler.QueryAll( + hdb.db, &bucketUpdates, + `SELECT num_bucket, withdrawals FROM bucket_update + WHERE item_id in(SELECT max(item_id) FROM bucket_update + group by num_bucket) + ORDER BY num_bucket ASC;`, + ) + return db.SlicePtrsToSlice(bucketUpdates).([]common.BucketUpdate), tracerr.Wrap(err) +} + func (hdb *HistoryDB) addTokenExchanges(d meddler.DB, tokenExchanges []common.TokenExchange) error { if len(tokenExchanges) == 0 { return nil