mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Add Last db view in kvdb and statedb
Last db view is an opened pebble db which always contains a checkpoint from the last batch. Methods to access this last batch are thread safe so that views of the last checkpoint can be made anywhere and with a consistent view of the state.
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hermeznetwork/hermez-node/apitypes"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/hermeznetwork/hermez-node/db/statedb"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
)
|
||||
|
||||
func (a *API) getAccount(c *gin.Context) {
|
||||
@@ -22,7 +24,7 @@ func (a *API) getAccount(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Get balance from stateDB
|
||||
account, err := a.s.GetAccount(*idx)
|
||||
account, err := a.s.LastGetAccount(*idx)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
@@ -56,19 +58,23 @@ func (a *API) getAccounts(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Get balances from stateDB
|
||||
for x, apiAccount := range apiAccounts {
|
||||
idx, err := stringToIdx(string(apiAccount.Idx), "Account Idx")
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
if err := a.s.LastRead(func(sdb *statedb.Last) error {
|
||||
for x, apiAccount := range apiAccounts {
|
||||
idx, err := stringToIdx(string(apiAccount.Idx), "Account Idx")
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
account, err := sdb.GetAccount(*idx)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
apiAccounts[x].Balance = apitypes.NewBigIntStr(account.Balance)
|
||||
apiAccounts[x].Nonce = account.Nonce
|
||||
}
|
||||
account, err := a.s.GetAccount(*idx)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
}
|
||||
apiAccounts[x].Balance = apitypes.NewBigIntStr(account.Balance)
|
||||
apiAccounts[x].Nonce = account.Nonce
|
||||
return nil
|
||||
}); err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
}
|
||||
|
||||
// Build succesfull response
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/mitchellh/copystructure"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type testAccount struct {
|
||||
@@ -76,40 +77,40 @@ func TestGetAccounts(t *testing.T) {
|
||||
// Filter by BJJ
|
||||
path := fmt.Sprintf("%s?BJJ=%s&limit=%d", endpoint, tc.accounts[0].PublicKey, limit)
|
||||
err := doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, len(fetchedAccounts), 0)
|
||||
assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
|
||||
fetchedAccounts = []testAccount{}
|
||||
// Filter by ethAddr
|
||||
path = fmt.Sprintf("%s?hezEthereumAddress=%s&limit=%d", endpoint, tc.accounts[3].EthAddr, limit)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, len(fetchedAccounts), 0)
|
||||
assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
|
||||
fetchedAccounts = []testAccount{}
|
||||
// both filters (incompatible)
|
||||
path = fmt.Sprintf("%s?hezEthereumAddress=%s&BJJ=%s&limit=%d", endpoint, tc.accounts[0].EthAddr, tc.accounts[0].PublicKey, limit)
|
||||
err = doBadReq("GET", path, nil, 400)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
fetchedAccounts = []testAccount{}
|
||||
// Filter by token IDs
|
||||
path = fmt.Sprintf("%s?tokenIds=%s&limit=%d", endpoint, stringIds, limit)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, len(fetchedAccounts), 0)
|
||||
assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
|
||||
fetchedAccounts = []testAccount{}
|
||||
// Token Ids + bjj
|
||||
path = fmt.Sprintf("%s?tokenIds=%s&BJJ=%s&limit=%d", endpoint, stringIds, tc.accounts[10].PublicKey, limit)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, len(fetchedAccounts), 0)
|
||||
assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
|
||||
fetchedAccounts = []testAccount{}
|
||||
// No filters (checks response content)
|
||||
path = fmt.Sprintf("%s?limit=%d", endpoint, limit)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(tc.accounts), len(fetchedAccounts))
|
||||
for i := 0; i < len(fetchedAccounts); i++ {
|
||||
fetchedAccounts[i].Token.ItemID = 0
|
||||
@@ -132,7 +133,7 @@ func TestGetAccounts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
err = doGoodReqPaginated(path, historydb.OrderDesc, &testAccountsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(reversedAccounts), len(fetchedAccounts))
|
||||
for i := 0; i < len(fetchedAccounts); i++ {
|
||||
reversedAccounts[i].Token.ItemID = 0
|
||||
@@ -147,21 +148,21 @@ func TestGetAccounts(t *testing.T) {
|
||||
// 400
|
||||
path = fmt.Sprintf("%s?hezEthereumAddress=hez:0x123456", endpoint)
|
||||
err = doBadReq("GET", path, nil, 400)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Test GetAccount
|
||||
path = fmt.Sprintf("%s/%s", endpoint, fetchedAccounts[2].Idx)
|
||||
account := testAccount{}
|
||||
assert.NoError(t, doGoodReq("GET", path, nil, &account))
|
||||
require.NoError(t, doGoodReq("GET", path, nil, &account))
|
||||
account.Token.ItemID = 0
|
||||
assert.Equal(t, fetchedAccounts[2], account)
|
||||
|
||||
// 400
|
||||
path = fmt.Sprintf("%s/hez:12345", endpoint)
|
||||
err = doBadReq("GET", path, nil, 400)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
// 404
|
||||
path = fmt.Sprintf("%s/hez:10:12345", endpoint)
|
||||
err = doBadReq("GET", path, nil, 404)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -355,6 +355,10 @@ func TestMain(m *testing.M) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
// Make a checkpoint to make the accounts available in Last
|
||||
if err := api.s.MakeCheckpoint(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Generate Coordinators and add them to HistoryDB
|
||||
const nCoords = 10
|
||||
|
||||
@@ -170,7 +170,7 @@ func (a *API) verifyPoolL2TxWrite(txw *l2db.PoolL2TxWrite) error {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// Get public key
|
||||
account, err := a.s.GetAccount(poolTx.FromIdx)
|
||||
account, err := a.s.LastGetAccount(poolTx.FromIdx)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user