mirror of
https://github.com/dragonheim/gagent.git
synced 2025-01-18 09:36:28 -08:00
Forgot to add the chaindb directory
This commit is contained in:
parent
458214aaa9
commit
66970dcd25
1 changed files with 67 additions and 0 deletions
67
internal/chaindb/chaindb.go
Normal file
67
internal/chaindb/chaindb.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package chaindb
|
||||
|
||||
import (
|
||||
sha "crypto/sha256"
|
||||
fmt "fmt"
|
||||
log "log"
|
||||
time "time"
|
||||
|
||||
gstructs "git.dragonheim.net/dragonheim/gagent/internal/gstructs"
|
||||
|
||||
hclsimple "github.com/hashicorp/hcl/v2/hclsimple"
|
||||
// hclwrite "github.com/hashicorp/hcl/v2/hclwrite"
|
||||
)
|
||||
|
||||
type GagentDb struct {
|
||||
chainRow []*gagentDbRow `hcl:"timestamp,block"`
|
||||
}
|
||||
|
||||
type gagentDbRow struct {
|
||||
timestamp time.Time `hcl:"timestamp"`
|
||||
DBName string `hcl:"chainid,optional"`
|
||||
Agent gstructs.AgentDetails `hcl:"agent,block"`
|
||||
dbCurrHash [32]byte `hcl:"currhash"`
|
||||
dbPrevHash [32]byte `hcl:"prevhash"`
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the database
|
||||
*/
|
||||
func (db *GagentDb) Init() {
|
||||
db.chainRow = make([]*gagentDbRow, 0)
|
||||
}
|
||||
|
||||
/*
|
||||
* Load the database from disk
|
||||
*/
|
||||
func (db *GagentDb) Load() error {
|
||||
err := hclsimple.DecodeFile("chaindb.hcl", nil, &db)
|
||||
log.Printf("[DEBUG] DB values: %v\n", db)
|
||||
return err
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a new row to the chaindb
|
||||
*/
|
||||
func (db *GagentDb) AddRow(row *gagentDbRow) error {
|
||||
row.timestamp = time.Now()
|
||||
db.chainRow = append(db.chainRow, row)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
* Set current hash of the database
|
||||
*/
|
||||
func (db *GagentDb) SetCurrHash() {
|
||||
db.chainRow[len(db.chainRow)-1].dbCurrHash = [32]byte{}
|
||||
foo := sha.Sum256([]byte(fmt.Sprintf("%v", db)))
|
||||
db.chainRow[len(db.chainRow)-1].dbCurrHash = foo
|
||||
}
|
||||
|
||||
/*
|
||||
* Set previous hash of the database
|
||||
*/
|
||||
func (db *GagentDb) SetPrevHash() {
|
||||
db.chainRow[len(db.chainRow)-1].dbPrevHash = db.chainRow[len(db.chainRow)-1].dbCurrHash
|
||||
}
|
Loading…
Add table
Reference in a new issue