Skip to content

Commit

Permalink
reduce the go type of number columns to just two: 'int' for increment…
Browse files Browse the repository at this point in the history
…al IDs, sizes, number of decimals or progresses, and 'int64' for block numbers, balances, chainIDs
  • Loading branch information
lucasmenendez committed Sep 12, 2023
1 parent 4de267a commit b139b49
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 192 deletions.
58 changes: 25 additions & 33 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,25 @@ func (capi *census3API) getCensus(msg *api.APIdata, ctx *httprouter.HTTPContext)
}
internalCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
currentCensus, err := capi.db.QueriesRO.CensusByID(internalCtx, int64(censusID))
currentCensus, err := capi.db.QueriesRO.CensusByID(internalCtx, censusID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundCensus.WithErr(err)
}
return ErrCantGetCensus.WithErr(err)
}
censusSize := int32(0)
if currentCensus.Size.Valid {
censusSize = currentCensus.Size.Int32
}
censusWeight := []byte{}
if currentCensus.Weight.Valid {
censusWeight = []byte(currentCensus.Weight.String)
}
res, err := json.Marshal(GetCensusResponse{
CensusID: uint64(censusID),
StrategyID: uint64(currentCensus.StrategyID),
CensusID: censusID,
StrategyID: currentCensus.StrategyID,
MerkleRoot: common.Bytes2Hex(currentCensus.MerkleRoot),
URI: "ipfs://" + currentCensus.Uri.String,
Size: censusSize,
Size: currentCensus.Size,
Weight: new(big.Int).SetBytes(censusWeight).String(),
Anonymous: currentCensus.CensusType == int64(census.AnonymousCensusType),
Anonymous: currentCensus.CensusType == int(census.AnonymousCensusType),
})
if err != nil {
return ErrEncodeCensus.WithErr(err)
Expand Down Expand Up @@ -128,7 +124,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
}()
qtx := capi.db.QueriesRW.WithTx(tx)

strategyTokens, err := qtx.TokensByStrategyID(bgCtx, int64(req.StrategyID))
strategyTokens, err := qtx.TokensByStrategyID(bgCtx, req.StrategyID)
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
return -1, ErrNoStrategyTokens.WithErr(err)
Expand All @@ -146,7 +142,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
return -1, ErrCantCreateCensus.WithErr(err)
}
// compute the new censusId and censusType
newCensusID := int(lastCensusID) + 1
newCensusID := lastCensusID + 1
censusType := census.DefaultCensusType
if req.Anonymous {
censusType = census.AnonymousCensusType
Expand Down Expand Up @@ -178,7 +174,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
return -1, ErrNotFoundTokenHolders.With("no holders for strategy")
}
// create a census tree and publish on IPFS
def := census.NewCensusDefinition(newCensusID, int(req.StrategyID), strategyHolders, req.Anonymous)
def := census.NewCensusDefinition(newCensusID, req.StrategyID, strategyHolders, req.Anonymous)
newCensus, err := capi.censusDB.CreateAndPublish(def)
if err != nil {
return -1, ErrCantCreateCensus.WithErr(err)
Expand All @@ -187,32 +183,32 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
// census
currentCensus, err := qtx.CensusByMerkleRoot(bgCtx, newCensus.RootHash)
if err == nil {
return int(currentCensus.ID), ErrCensusAlreadyExists
return currentCensus.ID, ErrCensusAlreadyExists
}
if err != nil && !errors.Is(sql.ErrNoRows, err) {
return -1, ErrCantCreateCensus.WithErr(err)
}
// save the new census in the SQL database
sqlURI := new(sql.NullString)
sqlURI := &sql.NullString{}
if err := sqlURI.Scan(newCensus.URI); err != nil {
return -1, ErrCantCreateCensus.WithErr(err)
}
sqlCensusSize := sql.NullInt32{}
sqlCensusSize := &sql.NullInt64{}
if err := sqlCensusSize.Scan(int64(len(strategyHolders))); err != nil {
return -1, ErrCantCreateCensus.WithErr(err)
}
sqlCensusWeight := sql.NullString{}
sqlCensusWeight := &sql.NullString{}
if err := sqlCensusWeight.Scan(censusWeight.String()); err != nil {
return -1, ErrCantCreateCensus.WithErr(err)
}
_, err = qtx.CreateCensus(bgCtx, queries.CreateCensusParams{
ID: int64(newCensus.ID),
StrategyID: int64(req.StrategyID),
CensusType: int64(censusType),
ID: newCensus.ID,
StrategyID: req.StrategyID,
CensusType: int(censusType),
MerkleRoot: newCensus.RootHash,
Uri: *sqlURI,
Size: sqlCensusSize,
Weight: sqlCensusWeight,
Size: currentCensus.Size,
Weight: *sqlCensusWeight,
QueueID: qID,
})
if err != nil {
Expand Down Expand Up @@ -256,28 +252,24 @@ func (capi *census3API) enqueueCensus(msg *api.APIdata, ctx *httprouter.HTTPCont
}

// get the census from the database by queue_id
currentCensus, err := capi.db.QueriesRO.CensusByID(internalCtx, int64(censusID))
currentCensus, err := capi.db.QueriesRO.CensusByID(internalCtx, censusID)
if err != nil {
return ErrCantGetCensus.WithErr(err)
}
// get values for optional parameters
censusSize := int32(0)
if currentCensus.Size.Valid {
censusSize = currentCensus.Size.Int32
}
censusWeight := []byte{}
if currentCensus.Weight.Valid {
censusWeight = []byte(currentCensus.Weight.String)
}
// encode census
queueCensus.Census = &GetCensusResponse{
CensusID: uint64(currentCensus.ID),
StrategyID: uint64(currentCensus.StrategyID),
CensusID: currentCensus.ID,
StrategyID: currentCensus.StrategyID,
MerkleRoot: common.Bytes2Hex(currentCensus.MerkleRoot),
URI: "ipfs://" + currentCensus.Uri.String,
Size: censusSize,
Size: currentCensus.Size,
Weight: new(big.Int).SetBytes(censusWeight).String(),
Anonymous: currentCensus.CensusType == int64(census.AnonymousCensusType),
Anonymous: currentCensus.CensusType == int(census.AnonymousCensusType),
}
// remove the item from the queue
capi.queue.Dequeue(queueID)
Expand All @@ -301,17 +293,17 @@ func (capi *census3API) getStrategyCensuses(msg *api.APIdata, ctx *httprouter.HT
// get censuses by this strategy ID
internalCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
rows, err := capi.db.QueriesRO.CensusByStrategyID(internalCtx, int64(strategyID))
rows, err := capi.db.QueriesRO.CensusByStrategyID(internalCtx, strategyID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundCensus.WithErr(err)
}
return ErrCantGetCensus.WithErr(err)
}
// parse and encode response
censuses := GetCensusesResponse{Censuses: []uint64{}}
censuses := GetCensusesResponse{Censuses: []int{}}
for _, censusInfo := range rows {
censuses.Censuses = append(censuses.Censuses, uint64(censusInfo.ID))
censuses.Censuses = append(censuses.Censuses, censusInfo.ID)
}
res, err := json.Marshal(censuses)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions api/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (capi *census3API) createDummyStrategy(tokenID []byte) error {
return err
}
_, err = capi.db.QueriesRW.CreateStrategyToken(ctx, queries.CreateStrategyTokenParams{
StrategyID: strategyID,
StrategyID: int(strategyID),
TokenID: tokenID,
MinBalance: big.NewInt(0).Bytes(),
MethodHash: []byte("test"),
Expand All @@ -72,9 +72,9 @@ func (capi *census3API) getStrategies(msg *api.APIdata, ctx *httprouter.HTTPCont
return ErrNoStrategies
}
// parse and encode the strategies
strategies := GetStrategiesResponse{Strategies: []uint64{}}
strategies := GetStrategiesResponse{Strategies: []int{}}
for _, strategy := range rows {
strategies.Strategies = append(strategies.Strategies, uint64(strategy.ID))
strategies.Strategies = append(strategies.Strategies, strategy.ID)
}
res, err := json.Marshal(strategies)
if err != nil {
Expand All @@ -96,7 +96,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
// get strategy from the database
internalCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
strategyData, err := capi.db.QueriesRO.StrategyByID(internalCtx, int64(strategyID))
strategyData, err := capi.db.QueriesRO.StrategyByID(internalCtx, strategyID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundStrategy.WithErr(err)
Expand All @@ -105,7 +105,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
}
// parse strategy information
strategy := GetStrategyResponse{
ID: uint64(strategyData.ID),
ID: strategyData.ID,
Predicate: strategyData.Predicate,
Tokens: []GetStrategyToken{},
}
Expand Down Expand Up @@ -151,9 +151,9 @@ func (capi *census3API) getTokenStrategies(msg *api.APIdata, ctx *httprouter.HTT
return ErrNoStrategies
}
// parse and encode strategies
strategies := GetStrategiesResponse{Strategies: []uint64{}}
strategies := GetStrategiesResponse{Strategies: []int{}}
for _, tokenStrategy := range rows {
strategies.Strategies = append(strategies.Strategies, uint64(tokenStrategy.ID))
strategies.Strategies = append(strategies.Strategies, tokenStrategy.ID)
}
res, err := json.Marshal(strategies)
if err != nil {
Expand Down
28 changes: 12 additions & 16 deletions api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
ID: common.BytesToAddress(tokenData.ID).String(),
Type: state.TokenType(int(tokenData.TypeID)).String(),
Name: tokenData.Name.String,
StartBlock: uint64(tokenData.CreationBlock.Int32),
StartBlock: tokenData.CreationBlock.Int64,
Tag: tokenData.Tag.String,
Symbol: tokenData.Symbol.String,
}
Expand Down Expand Up @@ -106,8 +106,7 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex
var (
name = new(sql.NullString)
symbol = new(sql.NullString)
decimals = new(sql.NullInt64)
creationBlock = new(sql.NullInt32)
creationBlock = new(sql.NullInt64)
totalSupply = new(big.Int)
tag = new(sql.NullString)
)
Expand All @@ -117,9 +116,6 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex
if err := symbol.Scan(info.Symbol); err != nil {
return ErrCantGetToken.WithErr(err)
}
if err := decimals.Scan(info.Decimals); err != nil {
return ErrCantGetToken.WithErr(err)
}
if info.TotalSupply != nil {
totalSupply = info.TotalSupply
}
Expand All @@ -132,10 +128,10 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex
ID: info.Address.Bytes(),
Name: *name,
Symbol: *symbol,
Decimals: *decimals,
Decimals: int(info.Decimals),
TotalSupply: totalSupply.Bytes(),
CreationBlock: *creationBlock,
TypeID: int64(tokenType),
TypeID: int(tokenType),
Synced: false,
Tag: *tag,
ChainID: req.ChainID,
Expand Down Expand Up @@ -173,9 +169,9 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return ErrCantGetToken.WithErr(err)
}
defaultStrategyID := uint64(0)
defaultStrategyID := 0
if len(tokenStrategies) > 0 {
defaultStrategyID = uint64(tokenStrategies[0].ID)
defaultStrategyID = tokenStrategies[0].ID
}
// get last block with token information
atBlock, err := capi.db.QueriesRO.LastBlockByTokenID(internalCtx, address.Bytes())
Expand All @@ -187,7 +183,7 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext)
}
// if the token is not synced, get the last block of the network to
// calculate the current scan progress
tokenProgress := uint64(100)
tokenProgress := 100
if !tokenData.Synced {
// get correct web3 uri provider
w3uri, exists := capi.w3p[tokenData.ChainID]
Expand All @@ -204,7 +200,7 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext)
if err != nil {
return ErrCantGetLastBlockNumber.WithErr(err)
}
tokenProgress = uint64(float64(atBlock) / float64(lastBlockNumber) * 100)
tokenProgress = int(float64(atBlock) / float64(lastBlockNumber) * 100)
}

// get token holders count
Expand All @@ -219,13 +215,13 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext)
tokenResponse := GetTokenResponse{
ID: address.String(),
Type: state.TokenType(int(tokenData.TypeID)).String(),
Decimals: uint64(tokenData.Decimals.Int64),
Size: uint32(holders),
Decimals: tokenData.Decimals,
Size: int(holders),
Name: tokenData.Name.String,
Symbol: tokenData.Symbol.String,
TotalSupply: new(big.Int).SetBytes(tokenData.TotalSupply).String(),
Status: &GetTokenStatusResponse{
AtBlock: uint64(atBlock),
AtBlock: atBlock,
Synced: tokenData.Synced,
Progress: tokenProgress,
},
Expand All @@ -235,7 +231,7 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext)
ChainID: tokenData.ChainID,
}
if tokenData.CreationBlock.Valid {
tokenResponse.StartBlock = uint64(tokenData.CreationBlock.Int32)
tokenResponse.StartBlock = tokenData.CreationBlock.Int64
}
res, err := json.Marshal(tokenResponse)
if err != nil {
Expand Down
34 changes: 17 additions & 17 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ type CreateTokenRequest struct {
}

type GetTokenStatusResponse struct {
AtBlock uint64 `json:"atBlock"`
Synced bool `json:"synced"`
Progress uint64 `json:"progress"`
AtBlock int64 `json:"atBlock"`
Synced bool `json:"synced"`
Progress int `json:"progress"`
}

type GetTokenResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Decimals uint64 `json:"decimals"`
StartBlock uint64 `json:"startBlock"`
Decimals int `json:"decimals"`
StartBlock int64 `json:"startBlock"`
Symbol string `json:"symbol"`
TotalSupply string `json:"totalSupply"`
Name string `json:"name"`
Status *GetTokenStatusResponse `json:"status"`
Size uint32 `json:"size"`
DefaultStrategy uint64 `json:"defaultStrategy,omitempty"`
Size int `json:"size"`
DefaultStrategy int `json:"defaultStrategy,omitempty"`
Tag string `json:"tag,omitempty"`
ChainID int64 `json:"chainID"`
}

type GetTokensItem struct {
ID string `json:"id"`
Type string `json:"type"`
StartBlock uint64 `json:"startBlock"`
StartBlock int64 `json:"startBlock"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Tag string `json:"tag,omitempty"`
Expand All @@ -51,31 +51,31 @@ type TokenHoldersResponse struct {
}

type CreateCensusResquest struct {
StrategyID uint64 `json:"strategyId"`
BlockNumber uint64 `json:"blockNumber"`
Anonymous bool `json:"anonymous"`
StrategyID int `json:"strategyId"`
BlockNumber int64 `json:"blockNumber"`
Anonymous bool `json:"anonymous"`
}

type CreateCensusResponse struct {
QueueID string `json:"queueId"`
}

type GetCensusResponse struct {
CensusID uint64 `json:"censusId"`
StrategyID uint64 `json:"strategyId"`
CensusID int `json:"censusId"`
StrategyID int `json:"strategyId"`
MerkleRoot string `json:"merkleRoot"`
URI string `json:"uri"`
Size int32 `json:"size"`
Size int `json:"size"`
Weight string `json:"weight"`
Anonymous bool `json:"anonymous"`
}

type GetCensusesResponse struct {
Censuses []uint64 `json:"censuses"`
Censuses []int `json:"censuses"`
}

type GetStrategiesResponse struct {
Strategies []uint64 `json:"strategies"`
Strategies []int `json:"strategies"`
}

type GetStrategyToken struct {
Expand All @@ -86,7 +86,7 @@ type GetStrategyToken struct {
}

type GetStrategyResponse struct {
ID uint64 `json:"id"`
ID int `json:"id"`
Tokens []GetStrategyToken `json:"tokens"`
Predicate string `json:"strategy"`
}
Expand Down
Loading

0 comments on commit b139b49

Please sign in to comment.