refactor: refactored the chainDB and added write capability.

This commit is contained in:
James Wells 2023-03-22 17:37:11 -07:00
parent 1bcc682b7c
commit d5fcd11592
Signed by: jwells
GPG key ID: 73196D10B8E65666
2 changed files with 62 additions and 24 deletions

View file

@ -1,68 +1,101 @@
package chaindb package chaindb
import ( import (
sha "crypto/sha256" sha256 "crypto/sha256"
fmt "fmt" fmt "fmt"
ioutil "io/ioutil"
log "log" log "log"
time "time" time "time"
gstructs "github.com/dragonheim/gagent/internal/gstructs" gstructs "github.com/dragonheim/gagent/internal/gstructs"
cty "github.com/zclconf/go-cty/cty"
hclsimple "github.com/hashicorp/hcl/v2/hclsimple" hclsimple "github.com/hashicorp/hcl/v2/hclsimple"
/* hclwrite "github.com/hashicorp/hcl/v2/hclwrite"
* hclwrite "github.com/hashicorp/hcl/v2/hclwrite" )
*/)
type GagentDb struct { type GagentDb struct {
chainRow []*gagentDbRow `hcl:"timestamp,block"` ChainRow []*GagentDbRow `hcl:"timestamp,block"`
} }
type gagentDbRow struct { type GagentDbRow struct {
timestamp time.Time `hcl:"timestamp"` Timestamp time.Time `hcl:"timestamp"`
DBName string `hcl:"chainid,optional"` DBName string `hcl:"chainid,optional"`
Agent gstructs.AgentDetails `hcl:"agent,block"` Agent gstructs.AgentDetails `hcl:"agent,block"`
dbCurrHash [32]byte `hcl:"currhash"` DbCurrHash [32]byte `hcl:"currhash"`
dbPrevHash [32]byte `hcl:"prevhash"` DbPrevHash [32]byte `hcl:"prevhash"`
} }
/* /*
* Initialize the database * Initialize the database
*/ */
func (db *GagentDb) Init() { func NewGagentDb() *GagentDb {
db.chainRow = make([]*gagentDbRow, 0) return &GagentDb{
ChainRow: make([]*GagentDbRow, 0),
}
} }
/* /*
* Load the database from disk * Load the database from disk
*/ */
func (db *GagentDb) Load() error { func (db *GagentDb) LoadHCL() error {
err := hclsimple.DecodeFile("chaindb.hcl", nil, &db) err := hclsimple.DecodeFile("chaindb.hcl", nil, db)
if err != nil {
return err
}
log.Printf("[DEBUG] DB values: %v\n", db) log.Printf("[DEBUG] DB values: %v\n", db)
return err return nil
}
/*
* Write the database to an HCL file
*/
func (db *GagentDb) WriteHCL() error {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
for _, row := range db.ChainRow {
rowBlock := rootBody.AppendNewBlock("row", []string{})
rowBody := rowBlock.Body()
rowBody.SetAttributeValue("timestamp", cty.StringVal(row.Timestamp.Format(time.RFC3339)))
rowBody.SetAttributeValue("chainid", cty.StringVal(row.DBName))
rowBody.SetAttributeValue("currhash", cty.StringVal(fmt.Sprintf("%x", row.DbCurrHash)))
rowBody.SetAttributeValue("prevhash", cty.StringVal(fmt.Sprintf("%x", row.DbPrevHash)))
agentBlock := rowBody.AppendNewBlock("agent", []string{})
agentBody := agentBlock.Body()
agentBody.SetAttributeValue("name", cty.StringVal(row.Agent.Client))
agentBody.SetAttributeValue("version", cty.StringVal(row.Agent.Shasum))
}
return ioutil.WriteFile("chaindb_out.hcl", f.Bytes(), 0644)
} }
/* /*
* Add a new row to the chaindb * Add a new row to the chaindb
*/ */
func (db *GagentDb) AddRow(row *gagentDbRow) error { func (db *GagentDb) AddRow(row *GagentDbRow) {
row.timestamp = time.Now() row.Timestamp = time.Now()
db.chainRow = append(db.chainRow, row) db.ChainRow = append(db.ChainRow, row)
db.SetCurrHash()
return nil db.SetPrevHash()
} }
/* /*
* Set current hash of the database * Set current hash of the database
*/ */
func (db *GagentDb) SetCurrHash() { func (db *GagentDb) SetCurrHash() {
db.chainRow[len(db.chainRow)-1].dbCurrHash = [32]byte{} row := db.ChainRow[len(db.ChainRow)-1]
foo := sha.Sum256([]byte(fmt.Sprintf("%v", db))) row.DbCurrHash = sha256.Sum256([]byte(fmt.Sprintf("%v", db)))
db.chainRow[len(db.chainRow)-1].dbCurrHash = foo
} }
/* /*
* Set previous hash of the database * Set previous hash of the database
*/ */
func (db *GagentDb) SetPrevHash() { func (db *GagentDb) SetPrevHash() {
db.chainRow[len(db.chainRow)-1].dbPrevHash = db.chainRow[len(db.chainRow)-1].dbCurrHash row := db.ChainRow[len(db.ChainRow)-1]
if len(db.ChainRow) > 1 {
row.DbPrevHash = db.ChainRow[len(db.ChainRow)-2].DbCurrHash
}
} }

View file

@ -43,7 +43,12 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
workerSock, _ := zmq.NewSocket(zmq.DEALER) workerSock, _ := zmq.NewSocket(zmq.DEALER)
defer workerSock.Close() defer workerSock.Close()
db.Init() chain := gcdb.NewGagentDb()
log.Println("[DEBUG] Loading chaindb ")
err := chain.LoadHCL()
if err != nil {
log.Printf("[ERROR] Error loading chaindb: %s", err)
}
workerListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.WorkerPort) workerListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.WorkerPort)
_ = workerSock.Bind(workerListener) _ = workerSock.Bind(workerListener)