From f9ddd8729ddfa218e5e7e56e5807f8d52e52f515 Mon Sep 17 00:00:00 2001 From: Lucas Menendez Date: Thu, 19 Oct 2023 10:31:03 +0200 Subject: [PATCH] fix to allow to recreate census of strategies --- api/censuses.go | 27 +++++++++++++++++++++++++-- api/types.go | 5 ++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/api/censuses.go b/api/censuses.go index dbfa45fd..504b17a0 100644 --- a/api/censuses.go +++ b/api/censuses.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/vocdoni/census3/census" queries "github.com/vocdoni/census3/db/sqlc" + "github.com/vocdoni/census3/state" "go.vocdoni.io/dvote/httprouter" api "go.vocdoni.io/dvote/httprouter/apirest" "go.vocdoni.io/dvote/log" @@ -134,9 +135,31 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str if len(strategyTokens) == 0 { return 0, ErrNoStrategyTokens.WithErr(err) } - + // any census strategy is identified by id created from the concatenation of + // the block number, the strategy id and the anonymous flag. The creation of + // censuses on specific block is not supported yet, so we need to get the + // last block of every token chain id to sum them and get the total block + // number, used to create the census id. + totalTokensBlockNumber := uint64(0) + w3 := state.Web3{} + defer cancel() + // get correct web3 uri provider + for _, token := range strategyTokens { + w3uri, exists := capi.w3p[token.ChainID] + if !exists { + return 0, ErrChainIDNotSupported.With("chain ID not supported") + } + if err := w3.Init(internalCtx, w3uri, common.BytesToAddress(token.ID), state.TokenType(token.TypeID)); err != nil { + return 0, ErrInitializingWeb3.WithErr(err) + } + currentBlockNumber, err := w3.LatestBlockNumber(internalCtx) + if err != nil { + return 0, ErrCantGetLastBlockNumber.WithErr(err) + } + totalTokensBlockNumber += currentBlockNumber + } // compute the new censusId and censusType - newCensusID := census.InnerCensusID(req.BlockNumber, req.StrategyID, req.Anonymous) + newCensusID := census.InnerCensusID(totalTokensBlockNumber, req.StrategyID, req.Anonymous) // check if the census already exists _, err = qtx.CensusByID(internalCtx, newCensusID) if err != nil { diff --git a/api/types.go b/api/types.go index 9b625a99..218f6c92 100644 --- a/api/types.go +++ b/api/types.go @@ -51,9 +51,8 @@ type TokenHoldersResponse struct { } type CreateCensusRequest struct { - StrategyID uint64 `json:"strategyID"` - BlockNumber uint64 `json:"blockNumber"` - Anonymous bool `json:"anonymous"` + StrategyID uint64 `json:"strategyID"` + Anonymous bool `json:"anonymous"` } type CreateCensusResponse struct {