mirror of
https://github.com/arnaucube/slowlorisdb.git
synced 2026-02-28 05:46:48 +01:00
store tx in txDb, and store address balance in addressDb for each block added~
This commit is contained in:
@@ -20,6 +20,8 @@ type Blockchain struct {
|
|||||||
LastBlock *Block
|
LastBlock *Block
|
||||||
blockdb *db.Db
|
blockdb *db.Db
|
||||||
txdb *db.Db
|
txdb *db.Db
|
||||||
|
addressdb *db.Db
|
||||||
|
walletsdb *db.Db
|
||||||
PoA PoA
|
PoA PoA
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +38,10 @@ func NewBlockchain(database *db.Db, dif uint64) *Blockchain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewPoABlockchain(database *db.Db, authNodes []*ecdsa.PublicKey) *Blockchain {
|
func NewPoABlockchain(database *db.Db, authNodes []*ecdsa.PublicKey) *Blockchain {
|
||||||
|
blockDb := database.WithPrefix([]byte("blockDb"))
|
||||||
|
txDb := database.WithPrefix([]byte("txDb"))
|
||||||
|
addressDb := database.WithPrefix([]byte("addressDb"))
|
||||||
|
|
||||||
poa := PoA{
|
poa := PoA{
|
||||||
AuthMiners: authNodes,
|
AuthMiners: authNodes,
|
||||||
}
|
}
|
||||||
@@ -44,7 +50,9 @@ func NewPoABlockchain(database *db.Db, authNodes []*ecdsa.PublicKey) *Blockchain
|
|||||||
Difficulty: uint64(0),
|
Difficulty: uint64(0),
|
||||||
Genesis: HashBytes([]byte("genesis")), // tmp
|
Genesis: HashBytes([]byte("genesis")), // tmp
|
||||||
LastBlock: &Block{},
|
LastBlock: &Block{},
|
||||||
blockdb: database,
|
blockdb: blockDb,
|
||||||
|
txdb: txDb,
|
||||||
|
addressdb: addressDb,
|
||||||
PoA: poa,
|
PoA: poa,
|
||||||
}
|
}
|
||||||
return blockchain
|
return blockchain
|
||||||
@@ -64,9 +72,50 @@ func (bc *Blockchain) AddBlock(block *Block) error {
|
|||||||
}
|
}
|
||||||
bc.LastBlock = block.Copy()
|
bc.LastBlock = block.Copy()
|
||||||
err := bc.blockdb.Put(block.Hash[:], block.Bytes())
|
err := bc.blockdb.Put(block.Hash[:], block.Bytes())
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add each tx to txDb & update addressDb balances
|
||||||
|
for _, tx := range block.Txs {
|
||||||
|
err = bc.txdb.Put(tx.TxId[:], tx.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
bc.UpdateWalletsWithNewTx(&tx)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bc *Blockchain) UpdateWalletsWithNewTx(tx *Tx) error {
|
||||||
|
for _, in := range tx.Inputs {
|
||||||
|
balanceBytes, err := bc.addressdb.Get(PackPubK(tx.From))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
balance := Uint64FromBytes(balanceBytes)
|
||||||
|
balance = balance - in.Value
|
||||||
|
err = bc.addressdb.Put(PackPubK(tx.From), Uint64ToBytes(balance))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, out := range tx.Outputs {
|
||||||
|
balanceBytes, err := bc.addressdb.Get(PackPubK(tx.To))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
balance := Uint64FromBytes(balanceBytes)
|
||||||
|
balance = balance + out.Value
|
||||||
|
err = bc.addressdb.Put(PackPubK(tx.To), Uint64ToBytes(balance))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (bc *Blockchain) GetBlock(hash Hash) (*Block, error) {
|
func (bc *Blockchain) GetBlock(hash Hash) (*Block, error) {
|
||||||
v, err := bc.blockdb.Get(hash[:])
|
v, err := bc.blockdb.Get(hash[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package core
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,3 +54,17 @@ func CalculatePoW(data PoWData, difficulty uint64) (uint64, error) {
|
|||||||
}
|
}
|
||||||
return data.GetNonce(), nil
|
return data.GetNonce(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Uint64ToBytes(u uint64) []byte {
|
||||||
|
buff := new(bytes.Buffer)
|
||||||
|
err := binary.Write(buff, binary.LittleEndian, u)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return buff.Bytes()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint64FromBytes(b []byte) uint64 {
|
||||||
|
return binary.LittleEndian.Uint64(b)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user