Skip to content

Commit

Permalink
Merge branch 'main' into feature/multiple_web3_providers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Sep 6, 2023
2 parents 1f7dd2b + ccbb280 commit a8cd816
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 30 deletions.
6 changes: 4 additions & 2 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down Expand Up @@ -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
}
```

Expand Down
6 changes: 4 additions & 2 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
22 changes: 15 additions & 7 deletions census/census.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions census/census_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand All @@ -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{}
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions db/migrations/0001_census3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions db/queries/censuses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ INSERT INTO censuses (
merkle_root,
uri,
size,
weight
weight,
census_type
)
VALUES (
?, ?, ?, ?, ?, ?
?, ?, ?, ?, ?, ?, ?
);

-- name: DeleteCensus :execresult
Expand Down
31 changes: 21 additions & 10 deletions db/sqlc/censuses.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8cd816

Please sign in to comment.