mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Fix repeated items when coordinator is updated
This commit is contained in:
@@ -353,6 +353,14 @@ func TestMain(m *testing.M) {
|
||||
// Generate Coordinators and add them to HistoryDB
|
||||
const nCoords = 10
|
||||
commonCoords := test.GenCoordinators(nCoords, commonBlocks)
|
||||
// Update one coordinator to test behaviour when bidder address is repeated
|
||||
updatedCoordBlock := commonCoords[len(commonCoords)-1].EthBlockNum
|
||||
commonCoords = append(commonCoords, common.Coordinator{
|
||||
Bidder: commonCoords[0].Bidder,
|
||||
Forger: commonCoords[0].Forger,
|
||||
EthBlockNum: updatedCoordBlock,
|
||||
URL: commonCoords[0].URL + ".new",
|
||||
})
|
||||
if err := api.h.AddCoordinators(commonCoords); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -473,7 +481,7 @@ func TestMain(m *testing.M) {
|
||||
nonBootForger := historydb.CoordinatorAPI{
|
||||
Bidder: commonCoords[0].Bidder,
|
||||
Forger: commonCoords[0].Forger,
|
||||
URL: commonCoords[0].URL,
|
||||
URL: commonCoords[0].URL + ".new",
|
||||
}
|
||||
// Slot 4
|
||||
nextForgers[3].Coordinator = nonBootForger
|
||||
@@ -762,10 +770,16 @@ func getBlockByNum(ethBlockNum int64, blocks []common.Block) common.Block {
|
||||
}
|
||||
|
||||
func getCoordinatorByBidder(bidder ethCommon.Address, coordinators []historydb.CoordinatorAPI) historydb.CoordinatorAPI {
|
||||
var coordLastUpdate historydb.CoordinatorAPI
|
||||
found := false
|
||||
for _, c := range coordinators {
|
||||
if c.Bidder == bidder {
|
||||
return c
|
||||
coordLastUpdate = c
|
||||
found = true
|
||||
}
|
||||
}
|
||||
panic("coordinator not found")
|
||||
if !found {
|
||||
panic("coordinator not found")
|
||||
}
|
||||
return coordLastUpdate
|
||||
}
|
||||
|
||||
@@ -31,12 +31,27 @@ func (t testCoordinatorsResponse) New() Pendinger { return &testCoordinatorsResp
|
||||
func genTestCoordinators(coordinators []common.Coordinator) []historydb.CoordinatorAPI {
|
||||
testCoords := []historydb.CoordinatorAPI{}
|
||||
for i := 0; i < len(coordinators); i++ {
|
||||
testCoords = append(testCoords, historydb.CoordinatorAPI{
|
||||
Bidder: coordinators[i].Bidder,
|
||||
Forger: coordinators[i].Forger,
|
||||
EthBlockNum: coordinators[i].EthBlockNum,
|
||||
URL: coordinators[i].URL,
|
||||
})
|
||||
alreadyRegistered := false
|
||||
for j := 0; j < len(testCoords); j++ {
|
||||
// coordinator already registered
|
||||
if coordinators[i].Bidder == testCoords[j].Bidder {
|
||||
alreadyRegistered = true
|
||||
if coordinators[i].EthBlockNum > testCoords[j].EthBlockNum {
|
||||
// This occurrence is more updated, delete current and append new
|
||||
testCoords = append(testCoords[:j], testCoords[j+1:]...)
|
||||
alreadyRegistered = false
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !alreadyRegistered {
|
||||
testCoords = append(testCoords, historydb.CoordinatorAPI{
|
||||
Bidder: coordinators[i].Bidder,
|
||||
Forger: coordinators[i].Forger,
|
||||
EthBlockNum: coordinators[i].EthBlockNum,
|
||||
URL: coordinators[i].URL,
|
||||
})
|
||||
}
|
||||
}
|
||||
return testCoords
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/mitchellh/copystructure"
|
||||
@@ -148,7 +149,12 @@ func TestGetSlots(t *testing.T) {
|
||||
// maxSlotNum & wonByEthereumAddress
|
||||
fetchedSlots = []testSlot{}
|
||||
limit = 1
|
||||
bidderAddr := tc.coordinators[0].Bidder
|
||||
var bidderAddr ethCommon.Address
|
||||
for i := 0; i < len(tc.slots); i++ {
|
||||
if tc.slots[i].WinnerBid != nil {
|
||||
bidderAddr = tc.slots[i].WinnerBid.Bidder
|
||||
}
|
||||
}
|
||||
path = fmt.Sprintf("%s?maxSlotNum=%d&wonByEthereumAddress=%s&limit=%d", endpoint, maxSlotNum, bidderAddr.String(), limit)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user