mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Update buckets withdrawals on API
This commit is contained in:
18
api/state.go
18
api/state.go
@@ -109,6 +109,24 @@ func (a *API) UpdateNetworkInfo(
|
|||||||
a.status.Network.LastBatch = lastBatch
|
a.status.Network.LastBatch = lastBatch
|
||||||
a.status.Network.CurrentSlot = currentSlot
|
a.status.Network.CurrentSlot = currentSlot
|
||||||
a.status.Network.NextForgers = nextForgers
|
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()
|
a.status.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hermeznetwork/hermez-node/common"
|
"github.com/hermeznetwork/hermez-node/common"
|
||||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testStatus struct {
|
type testStatus struct {
|
||||||
@@ -55,12 +57,39 @@ func TestUpdateNetworkInfo(t *testing.T) {
|
|||||||
lastBlock := tc.blocks[3]
|
lastBlock := tc.blocks[3]
|
||||||
lastBatchNum := common.BatchNum(3)
|
lastBatchNum := common.BatchNum(3)
|
||||||
currentSlotNum := int64(1)
|
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.NoError(t, err)
|
||||||
assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)
|
assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)
|
||||||
assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
|
assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
|
||||||
assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
|
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, 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) {
|
func TestUpdateMetrics(t *testing.T) {
|
||||||
@@ -111,6 +140,10 @@ func TestGetState(t *testing.T) {
|
|||||||
assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
|
assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
|
||||||
|
|
||||||
// SC vars
|
// 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.rollupVars, status.Rollup)
|
||||||
assert.Equal(t, tc.auctionVars, status.Auction)
|
assert.Equal(t, tc.auctionVars, status.Auction)
|
||||||
assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
|
assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
|
||||||
|
|||||||
@@ -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
|
// GetAllBucketUpdates retrieves all the bucket updates
|
||||||
func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) {
|
func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) {
|
||||||
var bucketUpdates []*common.BucketUpdate
|
var bucketUpdates []*common.BucketUpdate
|
||||||
@@ -1355,6 +1361,19 @@ func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) {
|
|||||||
return db.SlicePtrsToSlice(bucketUpdates).([]common.BucketUpdate), tracerr.Wrap(err)
|
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 {
|
func (hdb *HistoryDB) addTokenExchanges(d meddler.DB, tokenExchanges []common.TokenExchange) error {
|
||||||
if len(tokenExchanges) == 0 {
|
if len(tokenExchanges) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user