Skip to content

Commit

Permalink
Feature/async census creation (#70)
Browse files Browse the repository at this point in the history
* now when the user tries to create a new census, it is included into the database and generated in background, a new census column has ben created on the database called 'published' that indicates if the current census is built and published on IPFS
* new queue for track ongoing censuses and new endpoint to get the status of a ongoing census. The processes are purged from the queue when they are finished and someone checks them.
* new queue map attribute to store metadata, now when the user request the creation of a census that is already created, it is returned through the queue
* data race test based on go test timeout flag
  • Loading branch information
lucasmenendez authored Sep 8, 2023
1 parent 9a86a59 commit 9073525
Show file tree
Hide file tree
Showing 17 changed files with 625 additions and 170 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
run:
go: '1.20'
issues:
max-same-issues: 0
exclude-use-default: false
Expand All @@ -16,4 +18,4 @@ linters:

linters-settings:
lll:
line-length: 130
line-length: 130
34 changes: 28 additions & 6 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Triggers a new scan for the provided token, starting from the defined block.
| 409 | `token already created` | 4009 |
| 500 | `the token cannot be created` | 5000 |
| 500 | `error getting token information` | 5004 |
| 500 | `error initialising web3 client` | 5018 |
| 500 | `error initialising web3 client` | 5019 |

### GET `/token/{tokenID}`
Returns the information about the token referenced by the provided ID.
Expand Down Expand Up @@ -290,7 +290,7 @@ Returns a list of censusID for the strategy provided.
| 204 | `-` | 4007 |
| 404 | `census not found` | 4006 |
| 500 | `error getting census information` | 5009 |
| 500 | `error encoding cenuses` | 5018 |
| 500 | `error encoding censuses` | 5018 |

## Censuses

Expand All @@ -311,17 +311,15 @@ Request the creation of a new census with the strategy provided for the `blockNu

```json
{
"censusId": 12
"queueId": "0123456789abcdef0123456789abcdef01234567"
}
```

- ⚠️ errors :

| HTTP Status | Message | Internal error |
|:---:|:---|:---:|
| 400 | `malformed strategy ID, it must be a integer` | 4002 |
| 404 | `no strategy found with the ID provided` | 4005 |
| 500 | `error creating the census tree on the census database` | 5001 |
| 400 | `malformed strategy ID, it must be an integer` | 4002 |
| 500 | `error encoding strategy holders` | 5014 |

### GET `/census/{censusId}`
Expand Down Expand Up @@ -350,3 +348,27 @@ Returns the information of the snapshots related to the provided ID.
| 404 | `census not found` | 4006 |
| 500 | `error getting census information` | 5009 |
| 500 | `error encoding census` | 5017 |

### GET `/census/queue/{queueId}`
Returns the information of the census that are in the creation queue.

- 📥 response:
```json
{
"done": true,
"error": {
"code": 0,
"err": "error message or null"
},
"census": { /* <same_get_census_response> */ }
}
```

- ⚠️ errors:

| HTTP Status | Message | Internal error |
|:---:|:---|:---:|
| 400 | `malformed queue ID` | 4010 |
| 404 | `census not found` | 4006 |
| 500 | `error getting census information` | 5009 |
| 500 | `error encoding census queue item` | 5022 |
11 changes: 6 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/vocdoni/census3/census"
"github.com/vocdoni/census3/db"
"github.com/vocdoni/census3/queue"
"go.vocdoni.io/dvote/httprouter"
api "go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/log"
Expand All @@ -23,14 +24,16 @@ type census3API struct {
db *db.DB
endpoint *api.API
censusDB *census.CensusDB
queue *queue.BackgroundQueue
w3p map[int64]string
}

func Init(db *db.DB, conf Census3APIConf) error {
newAPI := &census3API{
conf: conf,
db: db,
w3p: conf.Web3Providers,
conf: conf,
db: db,
w3p: conf.Web3Providers,
queue: queue.NewBackgroundQueue(),
}
// get the current chainID
log.Infow("starting API", "chainID-web3Providers", conf.Web3Providers)
Expand Down Expand Up @@ -79,13 +82,11 @@ func (capi *census3API) getAPIInfo(msg *api.APIdata, ctx *httprouter.HTTPContext
for chainID := range capi.w3p {
chainIDs = append(chainIDs, chainID)
}

info := map[string]any{"chainIDs": chainIDs}
res, err := json.Marshal(info)
if err != nil {
log.Errorw(err, "error encoding api info")
return ErrEncodeAPIInfo
}

return ctx.Send(res, api.HTTPstatusOK)
}
Loading

0 comments on commit 9073525

Please sign in to comment.