From 5739e0a8bc336b544f09054b1132420c67b0091d Mon Sep 17 00:00:00 2001 From: abhilash singh Date: Tue, 28 Aug 2018 16:16:36 +0530 Subject: [PATCH 1/4] DPOS poc initial commit --- jsonstore/jsonstore.go | 53 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/jsonstore/jsonstore.go b/jsonstore/jsonstore.go index 3ce7baa..6a841c1 100644 --- a/jsonstore/jsonstore.go +++ b/jsonstore/jsonstore.go @@ -7,14 +7,13 @@ import ( "encoding/json" "fmt" "math" + "mint/code" "net/url" "regexp" "strconv" "strings" "time" - "mint/code" - "github.com/tendermint/abci/types" "golang.org/x/crypto/ed25519" mgo "gopkg.in/mgo.v2" @@ -79,6 +78,13 @@ type JSONStoreApplication struct { types.BaseApplication } +// Validator ... +type Validator struct { + ID bson.ObjectId `bson:"_id" json:"_id"` + Power int64 `bson:"power" json:"power"` + PubKey []byte `bson:"pubKey" json:"pubKey"` +} + func byteToHex(input []byte) string { var hexValue string for _, v := range input { @@ -122,6 +128,18 @@ func (app *JSONStoreApplication) Info(req types.RequestInfo) (resInfo types.Resp return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", 0)} } +// InitChain ... Update list of validators in db on genesis +func (app *JSONStoreApplication) InitChain(params types.RequestInitChain) types.ResponseInitChain { + // TODO: Batch this in one go + for _, v := range params.Validators { + db.C("validators").Upsert( + bson.M{"pubKey": v.PubKey}, + bson.M{"$set": bson.M{"power": v.Power, "pubKey": v.PubKey}}, + ) + } + return types.ResponseInitChain{} +} + // DeliverTx ... Update the global state func (app *JSONStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx { @@ -391,6 +409,11 @@ func (app *JSONStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx { db.C("comments").Update(bson.M{"_id": commentID}, bson.M{"$set": bson.M{"score": score}}) + break + case "upvoteValidator": + entity := body["entity"].(map[string]interface{}) + validatorID := bson.ObjectIdHex(entity["validatorID"].(string)) + db.C("validators").Update(bson.M{"_id": validatorID}, bson.M{"$inc": bson.M{"power": 1}}) break } @@ -502,10 +525,16 @@ func (app *JSONStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx { codeType = code.CodeTypeBadData break } + case "upvoteValidator": + entity := body["entity"].(map[string]interface{}) + if (entity["validatorID"] == nil) || (bson.IsObjectIdHex(entity["validatorID"].(string)) != true) { + codeType = code.CodeTypeBadData + break + } + break } // ===== Data Validation ======= - return types.ResponseCheckTx{Code: codeType} } @@ -524,3 +553,21 @@ func (app *JSONStoreApplication) Commit() types.ResponseCommit { func (app *JSONStoreApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) { return } + +// EndBlock ... Update list of validators here +func (app *JSONStoreApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock { + // TODO: Do the following only when last transaction was "upvoteValidator" + var results []Validator + db.C("validators").Find(nil).Sort("-power").All(&results) + var validatorUpdates []types.Validator + for i, k := range results { + if i < 21 { + validatorUpdates = append(validatorUpdates, types.Validator{Power: k.Power, PubKey: k.PubKey}) + } else { + // All validators beyond first 21 should be removed i.e. power = 0 + validatorUpdates = append(validatorUpdates, types.Validator{Power: 0, PubKey: k.PubKey}) + } + } + + return types.ResponseEndBlock{ValidatorUpdates: validatorUpdates} +} From d9364390cba96537bd15a9e6bae82415358773f9 Mon Sep 17 00:00:00 2001 From: abhilash singh Date: Thu, 30 Aug 2018 11:13:06 +0530 Subject: [PATCH 2/4] Log --- jsonstore/jsonstore.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jsonstore/jsonstore.go b/jsonstore/jsonstore.go index 6a841c1..96cc9bf 100644 --- a/jsonstore/jsonstore.go +++ b/jsonstore/jsonstore.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "log" "math" "mint/code" "net/url" @@ -416,7 +417,7 @@ func (app *JSONStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx { db.C("validators").Update(bson.M{"_id": validatorID}, bson.M{"$inc": bson.M{"power": 1}}) break } - + log.Println(body["type"]) return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: nil} } @@ -535,6 +536,7 @@ func (app *JSONStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx { } // ===== Data Validation ======= + log.Println(codeType, body["type"]) return types.ResponseCheckTx{Code: codeType} } From c256be7acd1455b75b2f87da838829c49b2ca57f Mon Sep 17 00:00:00 2001 From: abhilash singh Date: Fri, 31 Aug 2018 12:44:37 +0530 Subject: [PATCH 3/4] Minor change --- jsonstore/jsonstore.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jsonstore/jsonstore.go b/jsonstore/jsonstore.go index 96cc9bf..fa9ed72 100644 --- a/jsonstore/jsonstore.go +++ b/jsonstore/jsonstore.go @@ -532,7 +532,6 @@ func (app *JSONStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx { codeType = code.CodeTypeBadData break } - break } // ===== Data Validation ======= @@ -563,7 +562,7 @@ func (app *JSONStoreApplication) EndBlock(req types.RequestEndBlock) types.Respo db.C("validators").Find(nil).Sort("-power").All(&results) var validatorUpdates []types.Validator for i, k := range results { - if i < 21 { + if i < 3 { validatorUpdates = append(validatorUpdates, types.Validator{Power: k.Power, PubKey: k.PubKey}) } else { // All validators beyond first 21 should be removed i.e. power = 0 From 0e0cb287cabf88760eeb29b504e89a23f448be04 Mon Sep 17 00:00:00 2001 From: abhilash singh Date: Fri, 31 Aug 2018 14:59:40 +0530 Subject: [PATCH 4/4] New field vid --- jsonstore/jsonstore.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/jsonstore/jsonstore.go b/jsonstore/jsonstore.go index fa9ed72..fd680ef 100644 --- a/jsonstore/jsonstore.go +++ b/jsonstore/jsonstore.go @@ -135,7 +135,7 @@ func (app *JSONStoreApplication) InitChain(params types.RequestInitChain) types. for _, v := range params.Validators { db.C("validators").Upsert( bson.M{"pubKey": v.PubKey}, - bson.M{"$set": bson.M{"power": v.Power, "pubKey": v.PubKey}}, + bson.M{"$set": bson.M{"power": v.Power, "pubKey": v.PubKey, "vid": hex.EncodeToString(v.GetPubKey())}}, ) } return types.ResponseInitChain{} @@ -413,8 +413,7 @@ func (app *JSONStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx { break case "upvoteValidator": entity := body["entity"].(map[string]interface{}) - validatorID := bson.ObjectIdHex(entity["validatorID"].(string)) - db.C("validators").Update(bson.M{"_id": validatorID}, bson.M{"$inc": bson.M{"power": 1}}) + db.C("validators").Update(bson.M{"vid": entity["vid"]}, bson.M{"$inc": bson.M{"power": 1}}) break } log.Println(body["type"]) @@ -528,7 +527,7 @@ func (app *JSONStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx { } case "upvoteValidator": entity := body["entity"].(map[string]interface{}) - if (entity["validatorID"] == nil) || (bson.IsObjectIdHex(entity["validatorID"].(string)) != true) { + if entity["vid"] == nil { codeType = code.CodeTypeBadData break }