diff --git a/api/README.md b/api/README.md index a5db57ae..99707d86 100644 --- a/api/README.md +++ b/api/README.md @@ -300,7 +300,8 @@ Request the creation of a new census with the strategy provided for the `blockNu ```json { "strategyId": 1, - "blockNumber": 123456 + "blockNumber": 123456, + "anonymous": false } ``` @@ -333,7 +334,8 @@ Returns the information of the snapshots related to the provided ID. "uri": "ipfs://Qma....", "size": 1000, "weight": "200000000000000000000", - "chainId": 1 + "chainId": 1, + "anonymous": true } ``` diff --git a/api/censuses.go b/api/censuses.go index 8e1e3d5b..c7c49ec3 100644 --- a/api/censuses.go +++ b/api/censuses.go @@ -64,6 +64,7 @@ func (capi *census3API) getCensus(msg *api.APIdata, ctx *httprouter.HTTPContext) URI: "ipfs://" + currentCensus.Uri.String, Size: int32(currentCensus.Size), Weight: new(big.Int).SetBytes(currentCensus.Weight).String(), + Anonymous: currentCensus.CensusType == int64(census.AnonymousCensusType), }) if err != nil { return ErrEncodeCensus @@ -94,7 +95,7 @@ func (capi *census3API) createAndPublishCensus(msg *api.APIdata, ctx *httprouter return ErrCantCreateCensus } defer func() { - if err := tx.Rollback(); err != nil { + if err := tx.Rollback(); err != nil && !errors.Is(sql.ErrTxDone, err) { log.Errorw(err, "holders transaction rollback failed") } }() @@ -141,7 +142,7 @@ func (capi *census3API) createAndPublishCensus(msg *api.APIdata, ctx *httprouter return ErrCantCreateCensus } // create a census tree and publish on IPFS - def := census.DefaultCensusDefinition(int(lastCensusID+1), int(req.StrategyID), strategyHolders) + def := census.NewCensusDefinition(int(lastCensusID+1), int(req.StrategyID), strategyHolders, req.Anonymous) newCensus, err := capi.censusDB.CreateAndPublish(def) if err != nil { log.Errorw(err, "error creating or publishing the census") @@ -178,6 +179,7 @@ func (capi *census3API) createAndPublishCensus(msg *api.APIdata, ctx *httprouter Uri: *sqlURI, Size: int64(len(strategyHolders)), Weight: censusWeight.Bytes(), + CensusType: int64(def.Type), }) if err != nil { log.Errorw(err, "error saving the census on the database") diff --git a/api/types.go b/api/types.go index d296d398..89a27b1f 100644 --- a/api/types.go +++ b/api/types.go @@ -53,6 +53,7 @@ type TokenHoldersResponse struct { type CreateCensusResquest struct { StrategyID uint64 `json:"strategyId"` BlockNumber uint64 `json:"blockNumber"` + Anonymous bool `json:"anonymous"` } type CreateCensusResponse struct { @@ -66,7 +67,7 @@ type GetCensusResponse struct { URI string `json:"uri"` Size int32 `json:"size"` Weight string `json:"weight"` - // ChainID uint64 `json:"chainId"` + Anonymous bool `json:"anonymous"` } type GetCensusesResponse struct { diff --git a/census/census.go b/census/census.go index 104bff0f..71116049 100644 --- a/census/census.go +++ b/census/census.go @@ -24,9 +24,13 @@ import ( ) const ( - censusDBprefix = "cs_" - defaultMaxLevels = censustree.DefaultMaxLevels - defaultCensusType = models.Census_ARBO_BLAKE2B + censusDBprefix = "cs_" + defaultMaxLevels = censustree.DefaultMaxLevels +) + +const ( + DefaultCensusType = models.Census_ARBO_BLAKE2B + AnonymousCensusType = models.Census_ARBO_POSEIDON ) var ( @@ -52,18 +56,22 @@ type CensusDefinition struct { tree *censustree.Tree } -// DefaultCensusDefinition function returns a populated census definition with +// NewCensusDefinition function returns a populated census definition with // the default values for some parameters and the supplied values for the rest. -func DefaultCensusDefinition(id, strategyID int, holders map[common.Address]*big.Int) *CensusDefinition { - return &CensusDefinition{ +func NewCensusDefinition(id, strategyID int, holders map[common.Address]*big.Int, anonymous bool) *CensusDefinition { + def := &CensusDefinition{ ID: id, StrategyID: strategyID, - Type: defaultCensusType, + Type: DefaultCensusType, URI: "", AuthToken: nil, MaxLevels: defaultMaxLevels, Holders: holders, } + if anonymous { + def.Type = AnonymousCensusType + } + return def } type PublishedCensus struct { diff --git a/census/census_test.go b/census/census_test.go index 4f6bdac5..db1e02df 100644 --- a/census/census_test.go +++ b/census/census_test.go @@ -54,11 +54,11 @@ func TestCreateAndPublish(t *testing.T) { c.Assert(cdb.storage.Stop(), qt.IsNil) }() - censusDefinition := DefaultCensusDefinition(1, 1, MonkeysAddresses) + censusDefinition := NewCensusDefinition(1, 1, MonkeysAddresses, false) publishedCensus, err := cdb.CreateAndPublish(censusDefinition) c.Assert(err, qt.IsNil) - importedCensusDefinition := DefaultCensusDefinition(1, 1, nil) + importedCensusDefinition := NewCensusDefinition(1, 1, nil, false) importedCensusDefinition, err = cdb.newTree(importedCensusDefinition) c.Assert(err, qt.IsNil) @@ -94,7 +94,7 @@ func Test_newTree(t *testing.T) { }) c.Assert(err, qt.IsNotNil) - _, err = cdb.newTree(DefaultCensusDefinition(0, 0, map[common.Address]*big.Int{})) + _, err = cdb.newTree(NewCensusDefinition(0, 0, map[common.Address]*big.Int{}, false)) c.Assert(err, qt.IsNil) } @@ -106,7 +106,7 @@ func Test_save(t *testing.T) { c.Assert(cdb.storage.Stop(), qt.IsNil) }() - def := DefaultCensusDefinition(0, 0, map[common.Address]*big.Int{}) + def := NewCensusDefinition(0, 0, map[common.Address]*big.Int{}, false) rtx := cdb.treeDB.ReadTx() _, err = rtx.Get([]byte(censusDBKey(def.ID))) c.Assert(err, qt.IsNotNil) @@ -129,7 +129,7 @@ func Test_publish(t *testing.T) { c.Assert(cdb.storage.Stop(), qt.IsNil) }() - def, err := cdb.newTree(DefaultCensusDefinition(0, 0, MonkeysAddresses)) + def, err := cdb.newTree(NewCensusDefinition(0, 0, MonkeysAddresses, false)) c.Assert(err, qt.IsNil) keys, values := [][]byte{}, [][]byte{} @@ -162,7 +162,7 @@ func Test_delete(t *testing.T) { c.Assert(cdb.storage.Stop(), qt.IsNil) }() - def := DefaultCensusDefinition(0, 0, map[common.Address]*big.Int{}) + def := NewCensusDefinition(0, 0, map[common.Address]*big.Int{}, false) c.Assert(cdb.save(def), qt.IsNil) rtx := cdb.treeDB.ReadTx() diff --git a/db/migrations/0001_census3.sql b/db/migrations/0001_census3.sql index 47d7f91a..525f6501 100644 --- a/db/migrations/0001_census3.sql +++ b/db/migrations/0001_census3.sql @@ -39,6 +39,7 @@ CREATE TABLE censuses ( uri TEXT UNIQUE, size INTEGER NOT NULL, weight BLOB NOT NULL, + census_type INTEGER NOT NULL, FOREIGN KEY (strategy_id) REFERENCES strategies(id) ON DELETE CASCADE ); CREATE INDEX idx_censuses_strategy_id ON censuses(strategy_id); diff --git a/db/queries/censuses.sql b/db/queries/censuses.sql index 85650883..d838b148 100644 --- a/db/queries/censuses.sql +++ b/db/queries/censuses.sql @@ -53,10 +53,11 @@ INSERT INTO censuses ( merkle_root, uri, size, - weight + weight, + census_type ) VALUES ( - ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ? ); -- name: DeleteCensus :execresult diff --git a/db/sqlc/censuses.sql.go b/db/sqlc/censuses.sql.go index f10874ae..a31d174f 100644 --- a/db/sqlc/censuses.sql.go +++ b/db/sqlc/censuses.sql.go @@ -13,7 +13,7 @@ import ( ) const censusByID = `-- name: CensusByID :one -SELECT id, strategy_id, merkle_root, uri, size, weight FROM censuses +SELECT id, strategy_id, merkle_root, uri, size, weight, census_type FROM censuses WHERE id = ? LIMIT 1 ` @@ -28,12 +28,13 @@ func (q *Queries) CensusByID(ctx context.Context, id int64) (Censuse, error) { &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ) return i, err } const censusByMerkleRoot = `-- name: CensusByMerkleRoot :one -SELECT id, strategy_id, merkle_root, uri, size, weight FROM censuses +SELECT id, strategy_id, merkle_root, uri, size, weight, census_type FROM censuses WHERE merkle_root = ? LIMIT 1 ` @@ -48,12 +49,13 @@ func (q *Queries) CensusByMerkleRoot(ctx context.Context, merkleRoot annotations &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ) return i, err } const censusByStrategyID = `-- name: CensusByStrategyID :many -SELECT id, strategy_id, merkle_root, uri, size, weight FROM censuses +SELECT id, strategy_id, merkle_root, uri, size, weight, census_type FROM censuses WHERE strategy_id = ? ` @@ -73,6 +75,7 @@ func (q *Queries) CensusByStrategyID(ctx context.Context, strategyID int64) ([]C &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ); err != nil { return nil, err } @@ -88,7 +91,7 @@ func (q *Queries) CensusByStrategyID(ctx context.Context, strategyID int64) ([]C } const censusByURI = `-- name: CensusByURI :one -SELECT id, strategy_id, merkle_root, uri, size, weight FROM censuses +SELECT id, strategy_id, merkle_root, uri, size, weight, census_type FROM censuses WHERE uri = ? LIMIT 1 ` @@ -103,12 +106,13 @@ func (q *Queries) CensusByURI(ctx context.Context, uri sql.NullString) (Censuse, &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ) return i, err } const censusesByStrategyIDAndBlockID = `-- name: CensusesByStrategyIDAndBlockID :many -SELECT c.id, c.strategy_id, c.merkle_root, c.uri, c.size, c.weight FROM censuses c +SELECT c.id, c.strategy_id, c.merkle_root, c.uri, c.size, c.weight, c.census_type FROM censuses c JOIN census_blocks cb ON c.id = cb.census_id WHERE c.strategy_id = ? AND cb.block_id = ? LIMIT ? OFFSET ? @@ -142,6 +146,7 @@ func (q *Queries) CensusesByStrategyIDAndBlockID(ctx context.Context, arg Census &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ); err != nil { return nil, err } @@ -157,7 +162,7 @@ func (q *Queries) CensusesByStrategyIDAndBlockID(ctx context.Context, arg Census } const censusesByTokenID = `-- name: CensusesByTokenID :many -SELECT c.id, c.strategy_id, c.merkle_root, c.uri, c.size, c.weight FROM censuses AS c +SELECT c.id, c.strategy_id, c.merkle_root, c.uri, c.size, c.weight, c.census_type FROM censuses AS c JOIN strategy_tokens AS st ON c.strategy_id = st.strategy_id WHERE st.token_id = ? LIMIT ? OFFSET ? @@ -185,6 +190,7 @@ func (q *Queries) CensusesByTokenID(ctx context.Context, arg CensusesByTokenIDPa &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ); err != nil { return nil, err } @@ -200,7 +206,7 @@ func (q *Queries) CensusesByTokenID(ctx context.Context, arg CensusesByTokenIDPa } const censusesByTokenType = `-- name: CensusesByTokenType :many -SELECT c.id, c.strategy_id, c.merkle_root, c.uri, c.size, c.weight FROM censuses AS c +SELECT c.id, c.strategy_id, c.merkle_root, c.uri, c.size, c.weight, c.census_type FROM censuses AS c JOIN strategy_tokens AS st ON c.strategy_id = st.strategy_id JOIN tokens AS t ON st.token_id = t.id JOIN token_types AS tt ON t.type_id = tt.id @@ -223,6 +229,7 @@ func (q *Queries) CensusesByTokenType(ctx context.Context, typeName string) ([]C &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ); err != nil { return nil, err } @@ -244,10 +251,11 @@ INSERT INTO censuses ( merkle_root, uri, size, - weight + weight, + census_type ) VALUES ( - ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ? ) ` @@ -258,6 +266,7 @@ type CreateCensusParams struct { Uri sql.NullString Size int64 Weight []byte + CensusType int64 } func (q *Queries) CreateCensus(ctx context.Context, arg CreateCensusParams) (sql.Result, error) { @@ -268,6 +277,7 @@ func (q *Queries) CreateCensus(ctx context.Context, arg CreateCensusParams) (sql arg.Uri, arg.Size, arg.Weight, + arg.CensusType, ) } @@ -328,7 +338,7 @@ func (q *Queries) LastCensusID(ctx context.Context) (int64, error) { } const listCensuses = `-- name: ListCensuses :many -SELECT id, strategy_id, merkle_root, uri, size, weight FROM censuses +SELECT id, strategy_id, merkle_root, uri, size, weight, census_type FROM censuses ORDER BY id ` @@ -348,6 +358,7 @@ func (q *Queries) ListCensuses(ctx context.Context) ([]Censuse, error) { &i.Uri, &i.Size, &i.Weight, + &i.CensusType, ); err != nil { return nil, err } diff --git a/db/sqlc/models.go b/db/sqlc/models.go index 77bda349..f478fd32 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -28,6 +28,7 @@ type Censuse struct { Uri sql.NullString Size int64 Weight []byte + CensusType int64 } type Holder struct {