package blockchain import ( "encoding/json" "fmt" "strings" "github.com/dgraph-io/badger" abci "github.com/tendermint/tendermint/abci/types" ) type PromiseApp struct { db *badger.DB currentBatch *badger.Txn } func NewPromiseApp(db *badger.DB) *PromiseApp { return &PromiseApp{db: db} } func (app *PromiseApp) BeginBlock(_ abci.RequestBeginBlock) abci.ResponseBeginBlock { if app.currentBatch != nil { app.currentBatch.Discard() } app.currentBatch = app.db.NewTransaction(true) return abci.ResponseBeginBlock{} } func (app *PromiseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx { var tx map[string]interface{} if err := json.Unmarshal(req.Tx, &tx); err != nil { return abci.ResponseDeliverTx{Code: 1, Log: "invalid JSON"} } id, ok := tx["id"].(string) if !ok || id == "" { return abci.ResponseDeliverTx{Code: 2, Log: "missing id"} } data, _ := json.Marshal(tx) if app.currentBatch == nil { app.currentBatch = app.db.NewTransaction(true) } err := app.currentBatch.Set([]byte(id), data) if err != nil { return abci.ResponseDeliverTx{Code: 1, Log: fmt.Sprintf("failed to set: %v", err)} } return abci.ResponseDeliverTx{Code: 0} } func (app *PromiseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { var tx map[string]interface{} if err := json.Unmarshal(req.Tx, &tx); err != nil { return abci.ResponseCheckTx{Code: 1, Log: "invalid JSON"} } id, ok := tx["id"].(string) if !ok || id == "" { return abci.ResponseCheckTx{Code: 2, Log: "missing id"} } // проверка уникальности id err := app.db.View(func(txn *badger.Txn) error { _, err := txn.Get([]byte(id)) if err == badger.ErrKeyNotFound { return nil } return fmt.Errorf("id exists") }) if err != nil { return abci.ResponseCheckTx{Code: 3, Log: "duplicate id"} } return abci.ResponseCheckTx{Code: 0} } func (app *PromiseApp) Commit() abci.ResponseCommit { if app.currentBatch != nil { err := app.currentBatch.Commit() if err != nil { fmt.Printf("Commit error: %v\n", err) } app.currentBatch = nil } return abci.ResponseCommit{Data: []byte{}} } // SELECT * FROM