Skip to content

Commit

Permalink
Updated faucet token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ashi31 committed Aug 18, 2024
1 parent 6dc9c64 commit 12f1fd5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 38 deletions.
3 changes: 2 additions & 1 deletion client/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ func (c *Client) GenerateFaucetTestRBT(numTokens int, didStr string) (*model.Bas
return &rm, nil
}

func (c *Client) FaucetTokenCheck(token string) (*model.BasicResponse, error) {
func (c *Client) FaucetTokenCheck(token string, did string) (*model.BasicResponse, error) {
m := make(map[string]string)
m["token"] = token
m["did"] = did
var rm model.BasicResponse
err := c.sendJSONRequest("GET", setup.APIFaucetTokenCheck, m, nil, &rm)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion command/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (cmd *Command) FaucetTokenCheck() {
return
}

br, err := cmd.c.FaucetTokenCheck(cmd.token)
br, err := cmd.c.FaucetTokenCheck(cmd.token, cmd.did)
if err != nil {
cmd.log.Info("Cannot get token details")
return
Expand Down
85 changes: 63 additions & 22 deletions core/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package core

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -531,30 +533,45 @@ func (c *Core) GetpinnedTokens(did string) ([]wallet.Token, error) {
}

func (c *Core) GenerateFaucetTestTokens(reqID string, tokenCount int, did string) {
tdd, err := c.generateTestTokensFaucet(reqID, tokenCount, did)
tokenDetails, err := c.generateTestTokensFaucet(reqID, tokenCount, did)

br := model.BasicResponse{
Status: true,
Message: "DID registered successfully",
Message: "",
}

//If an error occurs at any given time, and the tokens have been created for that, reduce the latest token number by 1
if err != nil {
br.Status = false
br.Message = err.Error()
tdd.LatestTokenNumber = tdd.LatestTokenNumber - 1
if tdd.LatestTokenNumber == 0 && tdd.TokenLevel != 1 {
tdd.TokenLevel = tdd.TokenLevel - 1
tokenDetails.CurrentTokenNumber = tokenDetails.CurrentTokenNumber - 1
if tokenDetails.CurrentTokenNumber == 0 && tokenDetails.TokenLevel != 1 {
tokenDetails.TokenLevel = tokenDetails.TokenLevel - 1
}
}
//Updating the Faucet token table with latest token number and level once the tokens are generated
err = c.s.Update(wallet.FaucetTokenDetail, &tdd, "faucet_id=?", token.FaucetName)
// Send a POST request to update the token details to the faucet server
jsonData, err := json.Marshal(tokenDetails)
if err != nil {
c.log.Error("Error marshaling JSON:", "err", err)
br.Status = false
br.Message = br.Message + ", " + err.Error()
return
}
resp, err := http.Post("http://localhost:3001/api/update-token-value", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
c.log.Error("Failed to update latest token number in Faucet", "err", err)
br.Status = false
br.Message = err.Error()
br.Message = br.Message + ", " + err.Error()
return
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
br.Message = br.Message + ", " + "Successfully updated token details."
} else {
br.Status = false
br.Message = br.Message + ", " + "Failed to update token details. Status code:" + string(resp.StatusCode)
}
dc := c.GetWebReq(reqID)
if dc == nil {
c.log.Error("Failed to get did channels")
Expand All @@ -572,33 +589,40 @@ func (c *Core) generateTestTokensFaucet(reqID string, numTokens int, did string)
return nil, fmt.Errorf("DID is not exist")
}

var tokendetail token.FaucetToken
c.s.Read(wallet.FaucetTokenDetail, &tokendetail, "faucet_id=?", token.FaucetName)
//Updating the Faucet token table for the first time when empty
if tokendetail == (token.FaucetToken{}) {
tokendetail.LatestTokenNumber = 0
tokendetail.TokenLevel = 1
tokendetail.FaucetID = token.FaucetName
c.s.Write(wallet.FaucetTokenDetail, &tokendetail)
// Get the current value from Faucet
resp, err := http.Get("http://localhost:3001/api/current-token-value")
if err != nil {
fmt.Println("Error fetching value from React:", err)
return nil, err
}
defer resp.Body.Close()

var tokendetail token.FaucetToken

body, err := io.ReadAll(resp.Body)
//Populating the tokendetail with current token number and current token level received from Faucet.
json.Unmarshal(body, &tokendetail)
if err != nil {
fmt.Println("Error parsing JSON response:", err)
return nil, err
}
//Updating the Faucet token details with each new token
for i := 0; i < numTokens; i++ {
tokendetail.LatestTokenNumber += 1
tokendetail.CurrentTokenNumber += 1

//If the latest token number to be generated is more than the max token value of previous token, increase the token level
maxTokens := token.TokenMap[tokendetail.TokenLevel]
if tokendetail.LatestTokenNumber == maxTokens+1 {
if tokendetail.CurrentTokenNumber == maxTokens+1 {
tokendetail.TokenLevel += 1
tokendetail.LatestTokenNumber = 1
tokendetail.CurrentTokenNumber = 1
}

// Creating tokens at that level
rt := &rac.RacType{
Type: rac.RacTestTokenType,
DID: did,
TotalSupply: 1,
TokenNumber: uint64(tokendetail.LatestTokenNumber),
TokenNumber: uint64(tokendetail.CurrentTokenNumber),
TokenLevel: uint64(tokendetail.TokenLevel),
CreatorID: token.FaucetName,
}
Expand Down Expand Up @@ -685,7 +709,7 @@ func (c *Core) generateTestTokensFaucet(reqID string, numTokens int, did string)
return &tokendetail, nil
}

func (c *Core) FaucetTokenCheck(tokenID string) model.BasicResponse {
func (c *Core) FaucetTokenCheck(tokenID string, did string) model.BasicResponse {
br := model.BasicResponse{
Status: false,
}
Expand Down Expand Up @@ -733,7 +757,24 @@ func (c *Core) FaucetTokenCheck(tokenID string) model.BasicResponse {
//Validating token chain
tokenType := c.TokenType(RBTString)
genBlock := c.w.GetGenesisTokenBlock(tokenID, tokenType)
response, err := c.Validate_Token_Owner(genBlock, "bafybmif2cnmxooupsefy2rdy3vf3yt7xoojess4zedmoqvh3neezhi6uyq") //The did will be hardcoded to match the faucet DID

signers, err := genBlock.GetSigner()
if err != nil {
br.Message = "Couldn't get signer details"
return br
}

if len(signers) != 1 {
br.Message = "Invalid signer details"
return br
}
//The did will be hardcoded to match the faucet DID
if signers[0] != "bafybmif2cnmxooupsefy2rdy3vf3yt7xoojess4zedmoqvh3neezhi6uyq" {
br.Message = "Signer DID doesn't match faucet DID"
return br
}

response, err := c.Validate_Token_Owner(genBlock, did)
if err != nil {
c.log.Error("msg", response.Message, "err", err)
br.Message = "Token Details : " + tokenval + " Couldn't validate token chain"
Expand Down
8 changes: 0 additions & 8 deletions core/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
ipfsnode "github.com/ipfs/go-ipfs-api"
"github.com/rubixchain/rubixgoplatform/core/model"
"github.com/rubixchain/rubixgoplatform/core/storage"
"github.com/rubixchain/rubixgoplatform/token"
"github.com/rubixchain/rubixgoplatform/wrapper/logger"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
Expand All @@ -32,7 +31,6 @@ const (
TokenStateHash string = "TokenStateHashTable"
UnpledgeQueueTable string = "unpledgequeue"
UnpledgeSequence string = "UnpledgeSequence"
FaucetTokenDetail string = "FaucetTokenDetails"
)

type WalletConfig struct {
Expand Down Expand Up @@ -147,12 +145,6 @@ func InitWallet(s storage.Storage, dir string, log logger.Logger) (*Wallet, erro
return nil, err
}

err = w.s.Init(FaucetTokenDetail, &token.FaucetToken{}, true)
if err != nil {
w.log.Error("Failed to initialize Token", "err", err)
return nil, err
}

smartcontracTokenchainstorageDB, err := leveldb.OpenFile(dir+SmartContractTokenChainStorage, op)
if err != nil {
w.log.Error("failed to configure token chain block storage", "err", err)
Expand Down
3 changes: 2 additions & 1 deletion server/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ func (s *Server) APIGenerateFaucetTestToken(req *ensweb.Request) *ensweb.Result

func (s *Server) APIFaucetTokenCheck(req *ensweb.Request) *ensweb.Result {
token := s.GetQuerry(req, "token")
did := s.GetQuerry(req, "did")

br := s.c.FaucetTokenCheck(token)
br := s.c.FaucetTokenCheck(token, did)
return s.RenderJSON(req, br, http.StatusOK)
}
10 changes: 5 additions & 5 deletions token/token_mapping.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package token

const FaucetName = "faucet1"
const FaucetName = "faucettest1"

var TokenMap = map[int]int{
0: 0,
1: 5000000,
2: 2425000,
3: 2303750,
3: 2188563,
4: 2188563,
5: 2079134,
6: 1975178,
Expand Down Expand Up @@ -85,7 +85,7 @@ var TokenMap = map[int]int{
}

type FaucetToken struct {
TokenLevel int `gorm:"column:token_level"`
FaucetID string `gorm:"column:faucet_id"`
LatestTokenNumber int `gorm:"column:latest_token_number;"`
TokenLevel int `json:"token_level"`
FaucetID string `json:"faucet_id"`
CurrentTokenNumber int `json:"current_token_number"`
}

0 comments on commit 12f1fd5

Please sign in to comment.