Skip to content

Commit

Permalink
fix to allow to recreate census of strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Oct 19, 2023
1 parent 57b9a79 commit f9ddd87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
27 changes: 25 additions & 2 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f9ddd87

Please sign in to comment.