@ -7,10 +7,12 @@ import (
"time"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/apitypes"
"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/mitchellh/copystructure"
"github.com/mitchellh/copystructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
)
type testBatch struct {
type testBatch struct {
@ -20,7 +22,7 @@ type testBatch struct {
EthBlockHash ethCommon . Hash ` json:"ethereumBlockHash" `
EthBlockHash ethCommon . Hash ` json:"ethereumBlockHash" `
Timestamp time . Time ` json:"timestamp" `
Timestamp time . Time ` json:"timestamp" `
ForgerAddr ethCommon . Address ` json:"forgerAddr" `
ForgerAddr ethCommon . Address ` json:"forgerAddr" `
CollectedFees map [ common . TokenID ] string ` json:"collectedFees" `
CollectedFees apitypes . CollectedFeesAPI ` json:"collectedFees" `
TotalFeesUSD * float64 ` json:"historicTotalCollectedFeesUSD" `
TotalFeesUSD * float64 ` json:"historicTotalCollectedFeesUSD" `
StateRoot string ` json:"stateRoot" `
StateRoot string ` json:"stateRoot" `
NumAccounts int ` json:"numAccounts" `
NumAccounts int ` json:"numAccounts" `
@ -73,9 +75,9 @@ func genTestBatches(
if ! found {
if ! found {
panic ( "block not found" )
panic ( "block not found" )
}
}
collectedFees := make ( map [ common . TokenID ] string )
collectedFees := apitypes . CollectedFeesAPI ( make ( map [ common . TokenID ] apitypes . BigIntStr ) )
for k , v := range cBatches [ i ] . CollectedFees {
for k , v := range cBatches [ i ] . CollectedFees {
collectedFees [ k ] = v . String ( )
collectedFees [ k ] = * apitypes . NewBigIntStr ( v )
}
}
forgedTxs := 0
forgedTxs := 0
for _ , tx := range txs {
for _ , tx := range txs {
@ -132,7 +134,7 @@ func TestGetBatches(t *testing.T) {
limit := 3
limit := 3
path := fmt . Sprintf ( "%s?limit=%d" , endpoint , limit )
path := fmt . Sprintf ( "%s?limit=%d" , endpoint , limit )
err := doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err := doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
assertBatches ( t , tc . batches , fetchedBatches )
assertBatches ( t , tc . batches , fetchedBatches )
// minBatchNum
// minBatchNum
@ -141,7 +143,7 @@ func TestGetBatches(t *testing.T) {
minBatchNum := tc . batches [ len ( tc . batches ) / 2 ] . BatchNum
minBatchNum := tc . batches [ len ( tc . batches ) / 2 ] . BatchNum
path = fmt . Sprintf ( "%s?minBatchNum=%d&limit=%d" , endpoint , minBatchNum , limit )
path = fmt . Sprintf ( "%s?minBatchNum=%d&limit=%d" , endpoint , minBatchNum , limit )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
minBatchNumBatches := [ ] testBatch { }
minBatchNumBatches := [ ] testBatch { }
for i := 0 ; i < len ( tc . batches ) ; i ++ {
for i := 0 ; i < len ( tc . batches ) ; i ++ {
if tc . batches [ i ] . BatchNum > minBatchNum {
if tc . batches [ i ] . BatchNum > minBatchNum {
@ -156,7 +158,7 @@ func TestGetBatches(t *testing.T) {
maxBatchNum := tc . batches [ len ( tc . batches ) / 2 ] . BatchNum
maxBatchNum := tc . batches [ len ( tc . batches ) / 2 ] . BatchNum
path = fmt . Sprintf ( "%s?maxBatchNum=%d&limit=%d" , endpoint , maxBatchNum , limit )
path = fmt . Sprintf ( "%s?maxBatchNum=%d&limit=%d" , endpoint , maxBatchNum , limit )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
maxBatchNumBatches := [ ] testBatch { }
maxBatchNumBatches := [ ] testBatch { }
for i := 0 ; i < len ( tc . batches ) ; i ++ {
for i := 0 ; i < len ( tc . batches ) ; i ++ {
if tc . batches [ i ] . BatchNum < maxBatchNum {
if tc . batches [ i ] . BatchNum < maxBatchNum {
@ -171,7 +173,7 @@ func TestGetBatches(t *testing.T) {
slotNum := tc . batches [ len ( tc . batches ) / 2 ] . SlotNum
slotNum := tc . batches [ len ( tc . batches ) / 2 ] . SlotNum
path = fmt . Sprintf ( "%s?slotNum=%d&limit=%d" , endpoint , slotNum , limit )
path = fmt . Sprintf ( "%s?slotNum=%d&limit=%d" , endpoint , slotNum , limit )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
slotNumBatches := [ ] testBatch { }
slotNumBatches := [ ] testBatch { }
for i := 0 ; i < len ( tc . batches ) ; i ++ {
for i := 0 ; i < len ( tc . batches ) ; i ++ {
if tc . batches [ i ] . SlotNum == slotNum {
if tc . batches [ i ] . SlotNum == slotNum {
@ -186,7 +188,7 @@ func TestGetBatches(t *testing.T) {
forgerAddr := tc . batches [ len ( tc . batches ) / 2 ] . ForgerAddr
forgerAddr := tc . batches [ len ( tc . batches ) / 2 ] . ForgerAddr
path = fmt . Sprintf ( "%s?forgerAddr=%s&limit=%d" , endpoint , forgerAddr . String ( ) , limit )
path = fmt . Sprintf ( "%s?forgerAddr=%s&limit=%d" , endpoint , forgerAddr . String ( ) , limit )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
forgerAddrBatches := [ ] testBatch { }
forgerAddrBatches := [ ] testBatch { }
for i := 0 ; i < len ( tc . batches ) ; i ++ {
for i := 0 ; i < len ( tc . batches ) ; i ++ {
if tc . batches [ i ] . ForgerAddr == forgerAddr {
if tc . batches [ i ] . ForgerAddr == forgerAddr {
@ -200,7 +202,7 @@ func TestGetBatches(t *testing.T) {
limit = 6
limit = 6
path = fmt . Sprintf ( "%s?limit=%d" , endpoint , limit )
path = fmt . Sprintf ( "%s?limit=%d" , endpoint , limit )
err = doGoodReqPaginated ( path , historydb . OrderDesc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderDesc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
flippedBatches := [ ] testBatch { }
flippedBatches := [ ] testBatch { }
for i := len ( tc . batches ) - 1 ; i >= 0 ; i -- {
for i := len ( tc . batches ) - 1 ; i >= 0 ; i -- {
flippedBatches = append ( flippedBatches , tc . batches [ i ] )
flippedBatches = append ( flippedBatches , tc . batches [ i ] )
@ -214,7 +216,7 @@ func TestGetBatches(t *testing.T) {
minBatchNum = tc . batches [ len ( tc . batches ) / 4 ] . BatchNum
minBatchNum = tc . batches [ len ( tc . batches ) / 4 ] . BatchNum
path = fmt . Sprintf ( "%s?minBatchNum=%d&maxBatchNum=%d&limit=%d" , endpoint , minBatchNum , maxBatchNum , limit )
path = fmt . Sprintf ( "%s?minBatchNum=%d&maxBatchNum=%d&limit=%d" , endpoint , minBatchNum , maxBatchNum , limit )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
minMaxBatchNumBatches := [ ] testBatch { }
minMaxBatchNumBatches := [ ] testBatch { }
for i := 0 ; i < len ( tc . batches ) ; i ++ {
for i := 0 ; i < len ( tc . batches ) ; i ++ {
if tc . batches [ i ] . BatchNum < maxBatchNum && tc . batches [ i ] . BatchNum > minBatchNum {
if tc . batches [ i ] . BatchNum < maxBatchNum && tc . batches [ i ] . BatchNum > minBatchNum {
@ -227,25 +229,25 @@ func TestGetBatches(t *testing.T) {
fetchedBatches = [ ] testBatch { }
fetchedBatches = [ ] testBatch { }
path = fmt . Sprintf ( "%s?slotNum=%d&minBatchNum=%d" , endpoint , 1 , 25 )
path = fmt . Sprintf ( "%s?slotNum=%d&minBatchNum=%d" , endpoint , 1 , 25 )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
err = doGoodReqPaginated ( path , historydb . OrderAsc , & testBatchesResponse { } , appendIter )
assert . NoError ( t , err )
require . NoError ( t , err )
assertBatches ( t , [ ] testBatch { } , fetchedBatches )
assertBatches ( t , [ ] testBatch { } , fetchedBatches )
// 400
// 400
// Invalid minBatchNum
// Invalid minBatchNum
path = fmt . Sprintf ( "%s?minBatchNum=%d" , endpoint , - 2 )
path = fmt . Sprintf ( "%s?minBatchNum=%d" , endpoint , - 2 )
err = doBadReq ( "GET" , path , nil , 400 )
err = doBadReq ( "GET" , path , nil , 400 )
assert . NoError ( t , err )
require . NoError ( t , err )
// Invalid forgerAddr
// Invalid forgerAddr
path = fmt . Sprintf ( "%s?forgerAddr=%s" , endpoint , "0xG0000001" )
path = fmt . Sprintf ( "%s?forgerAddr=%s" , endpoint , "0xG0000001" )
err = doBadReq ( "GET" , path , nil , 400 )
err = doBadReq ( "GET" , path , nil , 400 )
assert . NoError ( t , err )
require . NoError ( t , err )
}
}
func TestGetBatch ( t * testing . T ) {
func TestGetBatch ( t * testing . T ) {
endpoint := apiURL + "batches/"
endpoint := apiURL + "batches/"
for _ , batch := range tc . batches {
for _ , batch := range tc . batches {
fetchedBatch := testBatch { }
fetchedBatch := testBatch { }
assert . NoError (
require . NoError (
t , doGoodReq (
t , doGoodReq (
"GET" ,
"GET" ,
endpoint + strconv . Itoa ( int ( batch . BatchNum ) ) ,
endpoint + strconv . Itoa ( int ( batch . BatchNum ) ) ,
@ -255,16 +257,16 @@ func TestGetBatch(t *testing.T) {
assertBatch ( t , batch , fetchedBatch )
assertBatch ( t , batch , fetchedBatch )
}
}
// 400
// 400
assert . NoError ( t , doBadReq ( "GET" , endpoint + "foo" , nil , 400 ) )
require . NoError ( t , doBadReq ( "GET" , endpoint + "foo" , nil , 400 ) )
// 404
// 404
assert . NoError ( t , doBadReq ( "GET" , endpoint + "99999" , nil , 404 ) )
require . NoError ( t , doBadReq ( "GET" , endpoint + "99999" , nil , 404 ) )
}
}
func TestGetFullBatch ( t * testing . T ) {
func TestGetFullBatch ( t * testing . T ) {
endpoint := apiURL + "full-batches/"
endpoint := apiURL + "full-batches/"
for _ , fullBatch := range tc . fullBatches {
for _ , fullBatch := range tc . fullBatches {
fetchedFullBatch := testFullBatch { }
fetchedFullBatch := testFullBatch { }
assert . NoError (
require . NoError (
t , doGoodReq (
t , doGoodReq (
"GET" ,
"GET" ,
endpoint + strconv . Itoa ( int ( fullBatch . Batch . BatchNum ) ) ,
endpoint + strconv . Itoa ( int ( fullBatch . Batch . BatchNum ) ) ,
@ -275,9 +277,9 @@ func TestGetFullBatch(t *testing.T) {
assertTxs ( t , fullBatch . Txs , fetchedFullBatch . Txs )
assertTxs ( t , fullBatch . Txs , fetchedFullBatch . Txs )
}
}
// 400
// 400
assert . NoError ( t , doBadReq ( "GET" , endpoint + "foo" , nil , 400 ) )
require . NoError ( t , doBadReq ( "GET" , endpoint + "foo" , nil , 400 ) )
// 404
// 404
assert . NoError ( t , doBadReq ( "GET" , endpoint + "99999" , nil , 404 ) )
require . NoError ( t , doBadReq ( "GET" , endpoint + "99999" , nil , 404 ) )
}
}
func assertBatches ( t * testing . T , expected , actual [ ] testBatch ) {
func assertBatches ( t * testing . T , expected , actual [ ] testBatch ) {