Merge pull request #473 from hermeznetwork/feature/buckets-update Update buckets withdrawals on API
a_bennassar arnaubennassar5@gmail.com
Mon, 11 Jan 2021 15:43:00 +0100
3 files changed,
71 insertions(+),
1 deletions(-)
M
api/state.go
→
api/state.go
@@ -109,6 +109,24 @@ a.status.Network.LastEthBlock = lastEthBlock.Num
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 }
M
api/state_test.go
→
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 @@ assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
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 @@
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)
M
db/historydb/historydb.go
→
db/historydb/historydb.go
@@ -1362,6 +1362,12 @@ bucketUpdates[:],
)) } +// 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@@ -1369,6 +1375,19 @@ err := meddler.QueryAll(
hdb.db, &bucketUpdates, `SELECT eth_block_num, num_bucket, block_stamp, withdrawals FROM bucket_update ORDER BY item_id;`, + ) + 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) }