Browse Source

GetBalance

master
arnaucube 5 years ago
parent
commit
85d0bcc5a3
2 changed files with 34 additions and 1 deletions
  1. +19
    -1
      core/blockchain.go
  2. +15
    -0
      node/node_test.go

+ 19
- 1
core/blockchain.go

@ -3,10 +3,13 @@ package core
import ( import (
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"strconv"
"github.com/arnaucube/slowlorisdb/db" "github.com/arnaucube/slowlorisdb/db"
lvldberrors "github.com/syndtr/goleveldb/leveldb/errors"
) )
type PoA struct { type PoA struct {
@ -99,18 +102,23 @@ func (bc *Blockchain) UpdateWalletsWithNewTx(tx *Tx) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println("sent-->: balance of " + hex.EncodeToString(PackPubK(tx.From)[:10]) + ": " + strconv.Itoa(int(balance)))
} }
for _, out := range tx.Outputs { for _, out := range tx.Outputs {
balanceBytes, err := bc.addressdb.Get(PackPubK(tx.To)) balanceBytes, err := bc.addressdb.Get(PackPubK(tx.To))
if err != nil {
if err != nil && err != lvldberrors.ErrNotFound {
return err return err
} }
if err == lvldberrors.ErrNotFound {
balanceBytes = Uint64ToBytes(uint64(0))
}
balance := Uint64FromBytes(balanceBytes) balance := Uint64FromBytes(balanceBytes)
balance = balance + out.Value balance = balance + out.Value
err = bc.addressdb.Put(PackPubK(tx.To), Uint64ToBytes(balance)) err = bc.addressdb.Put(PackPubK(tx.To), Uint64ToBytes(balance))
if err != nil { if err != nil {
return err return err
} }
fmt.Println("--> received: balance of " + hex.EncodeToString(PackPubK(tx.To)[:10]) + ": " + strconv.Itoa(int(balance)))
} }
return nil return nil
@ -128,6 +136,16 @@ func (bc *Blockchain) GetBlock(hash Hash) (*Block, error) {
return block, nil return block, nil
} }
func (bc *Blockchain) GetBalance(pubK *ecdsa.PublicKey) (uint64, error) {
balanceBytes, err := bc.addressdb.Get(PackPubK(pubK))
if err != nil {
return uint64(0), err
}
balance := Uint64FromBytes(balanceBytes)
return balance, nil
}
func (bc *Blockchain) GetPrevBlock(hash Hash) (*Block, error) { func (bc *Blockchain) GetPrevBlock(hash Hash) (*Block, error) {
currentBlock, err := bc.GetBlock(hash) currentBlock, err := bc.GetBlock(hash)
if err != nil { if err != nil {

+ 15
- 0
node/node_test.go

@ -2,6 +2,8 @@ package node
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex"
"fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -122,6 +124,7 @@ func TestFromGenesisToTenBlocks(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
// create the genesis block // create the genesis block
// genesisBlock sends 100 to pubK
genesisBlock, err := node.CreateGenesis(&privK.PublicKey, uint64(100)) genesisBlock, err := node.CreateGenesis(&privK.PublicKey, uint64(100))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotEqual(t, genesisBlock.Signature, core.Signature{}) assert.NotEqual(t, genesisBlock.Signature, core.Signature{})
@ -169,6 +172,12 @@ func TestFromGenesisToTenBlocks(t *testing.T) {
err = node.Bc.AddBlock(block) err = node.Bc.AddBlock(block)
assert.Nil(t, err) assert.Nil(t, err)
balance, err := node.Bc.GetBalance(&pubK0)
assert.Nil(t, err)
fmt.Println(hex.EncodeToString(core.PackPubK(&pubK0)[:10]))
fmt.Println("balance in pubK0", balance)
assert.Equal(t, balance, uint64(100))
// add another tx sending coins to the pubK1 // add another tx sending coins to the pubK1
privK1, err := core.NewKey() privK1, err := core.NewKey()
assert.Nil(t, err) assert.Nil(t, err)
@ -199,4 +208,10 @@ func TestFromGenesisToTenBlocks(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
err = node.Bc.AddBlock(block) err = node.Bc.AddBlock(block)
assert.Nil(t, err) assert.Nil(t, err)
balance, err = node.Bc.GetBalance(&pubK0)
assert.Nil(t, err)
fmt.Println(hex.EncodeToString(core.PackPubK(&pubK0)[:10]))
fmt.Println("balance in pubK0", balance)
assert.Equal(t, balance, uint64(90))
} }

Loading…
Cancel
Save