Skip to content

Commit

Permalink
Merge pull request #2 from /issues/1
Browse files Browse the repository at this point in the history
settlement
  • Loading branch information
dabasov authored Sep 4, 2019
2 parents a32c14d + 0dfc14f commit f00b681
Show file tree
Hide file tree
Showing 35 changed files with 1,176 additions and 249 deletions.
39 changes: 31 additions & 8 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ type Blockchain struct {
blockService BlockService
txPool TransactionPool
stateDB state.DB
proposerGetter cmn.ProposerForHeight
blockPersister *BlockPersister
chainPersister *BlockchainPersister
}

func (bc *Blockchain) SetProposerGetter(proposerGetter cmn.ProposerForHeight) {
bc.proposerGetter = proposerGetter
}

func (bc *Blockchain) BlockService(blockService BlockService) {
bc.blockService = blockService
}
Expand All @@ -126,13 +131,14 @@ func CreateBlockchainFromGenesisBlock(cfg *BlockchainConfig) *Blockchain {
blockService: cfg.BlockService,
txPool: cfg.Pool,
stateDB: cfg.Db,
proposerGetter: cfg.ProposerGetter,
blockPersister: cfg.BlockPerister,
chainPersister: cfg.ChainPersister,
}

var s *state.Snapshot
if cfg.Seed != nil {
s = state.NewSnapshotWithAccounts(zero.Header().Hash(), cfg.Seed)
s = state.NewSnapshotWithAccounts(zero.Header().Hash(), common.Address{}, cfg.Seed)
}
if e := cfg.Db.Init(zero.Header().Hash(), s); e != nil {
panic("can't init state DB")
Expand Down Expand Up @@ -419,6 +425,14 @@ func (bc *Blockchain) GetHead() *Block {
return b[0]
}

func (bc *Blockchain) GetHeadSnapshot() *state.Snapshot {
s, f := bc.stateDB.Get(bc.GetHead().Header().Hash())
if !f {
log.Error("Can't find head snapshot")
}
return s
}

func (bc *Blockchain) GetTopHeight() int32 {
return bc.GetHead().Header().Height()
}
Expand All @@ -441,7 +455,7 @@ func (bc *Blockchain) AddBlock(block *Block) error {

//todo remove this hack by handling genesis in special way
if block.Height() != 0 {
if err := bc.applyTransactionsAndValidateProof(block); err != nil {
if err := bc.applyTransactionsAndValidate(block); err != nil {
return err
}
}
Expand Down Expand Up @@ -552,7 +566,8 @@ func (bc Blockchain) IsSibling(sibling *Header, ancestor *Header) bool {
}

func (bc *Blockchain) NewBlock(parent *Block, qc *QuorumCertificate, data []byte) *Block {
s, e := bc.stateDB.Create(parent.Header().Hash())
proposer := bc.proposerGetter.ProposerForHeight(parent.header.height + 1).GetAddress() //this block will be the block of next height
s, e := bc.stateDB.Create(parent.Header().Hash(), proposer)
if e != nil {
log.Error("Can't create new block", e)
return nil
Expand All @@ -579,7 +594,6 @@ func (bc *Blockchain) NewBlock(parent *Block, qc *QuorumCertificate, data []byte
}
txs.InsertOrUpdate([]byte(next.HashKey().Hex()), next.Serialized())
txs_arr = append(txs_arr, next)
spew.Dump(next)
}

header := createHeader(
Expand Down Expand Up @@ -658,21 +672,30 @@ func (bc *Blockchain) UpdateGenesisBlockQC(certificate *QuorumCertificate) {
}
}

func (bc *Blockchain) applyTransactionsAndValidateProof(block *Block) error {
s, e := bc.stateDB.Create(block.Header().Parent())
func (bc *Blockchain) applyTransactionsAndValidate(block *Block) error {
_, f := bc.stateDB.Get(block.Header().hash)
if f {
log.Debug("Found block that was created by us and already processed, skip this step")
return nil
}

s, e := bc.stateDB.Create(block.Header().Parent(), bc.proposerGetter.ProposerForHeight(block.Height()).GetAddress())
if e != nil {
return e
}

iterator := block.Txs()
for next := iterator.Next(); next != nil; next = iterator.Next() {
for iterator.HasNext() {
next := iterator.Next()
if err := s.ApplyTransaction(next); err != nil {
return err
}
}

if !bytes.Equal(s.Proof().Bytes(), block.Header().stateHash.Bytes()) {
log.Debugf("Not equal state hash: expected %v, calculated %v", s.Proof().Hex(), block.Header().stateHash.Hex())

spew.Dump(s.Entries())
log.Debugf("Not equal state hash: expected %v, calculated %v", block.Header().stateHash.Hex(), s.Proof().Hex())
return InvalidStateHashError
}
_, err := bc.stateDB.Commit(block.Header().Parent(), block.Header().Hash())
Expand Down
2 changes: 2 additions & 0 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"os"
)
import "github.com/gagarinchain/network/common/eth/common"
import cmn "github.com/gagarinchain/network/common"

type BlockchainConfig struct {
Seed map[common.Address]*state.Account
BlockPerister *BlockPersister
ProposerGetter cmn.ProposerForHeight
ChainPersister *BlockchainPersister
BlockService BlockService
Pool TransactionPool
Expand Down
24 changes: 18 additions & 6 deletions blockchain/state/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
InsufficientFundsError = errors.New("insufficient funds")
ExpiredTransactionError = errors.New("expired transaction")
FutureTransactionError = errors.New("future transaction")
WrongProofOrigin = errors.New("wrong proof origin")
NotEmptyInitDBError = errors.New("can't initialize not empty DB")
log = logging.MustGetLogger("state")
)
Expand Down Expand Up @@ -94,7 +95,7 @@ func (db *DBImpl) Init(hash common.Hash, seed *Snapshot) error {
}

if seed == nil {
seed = NewSnapshot(hash)
seed = NewSnapshot(hash, common.Address{})
}

db.snapshots[hash] = seed
Expand All @@ -112,7 +113,7 @@ func (db *DBImpl) Get(hash common.Hash) (s *Snapshot, f bool) {
return sn, f
}

func (db *DBImpl) Create(parent common.Hash) (s *Snapshot, e error) {
func (db *DBImpl) Create(parent common.Hash, proposer common.Address) (s *Snapshot, e error) {
db.lock.RLock()
defer db.lock.RUnlock()

Expand All @@ -121,10 +122,7 @@ func (db *DBImpl) Create(parent common.Hash) (s *Snapshot, e error) {
return nil, errors.New("no prent is found")
}

cpy := parentSnapshot.trie.Copy()
snapshot := &Snapshot{trie: cpy}
parentSnapshot.siblings = append(parentSnapshot.siblings, snapshot)
parentSnapshot.SetPending(snapshot)
snapshot := parentSnapshot.NewPendingSnapshot(proposer)

return snapshot, nil
}
Expand Down Expand Up @@ -172,6 +170,20 @@ func (db *DBImpl) release(snapshot *Snapshot) {
type Account struct {
nonce uint64
balance *big.Int
origin common.Address
voters []common.Address
}

func (a *Account) Voters() []common.Address {
return a.voters
}

func (a *Account) Balance() *big.Int {
return a.balance
}

func (a *Account) Nonce() uint64 {
return a.nonce
}

func NewAccount(nonce uint64, balance *big.Int) *Account {
Expand Down
2 changes: 1 addition & 1 deletion blockchain/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
type DB interface {
Init(hash common.Hash, seed *Snapshot) error
Get(hash common.Hash) (s *Snapshot, f bool)
Create(parent common.Hash) (s *Snapshot, e error)
Create(parent common.Hash, proposer common.Address) (s *Snapshot, e error)
Commit(parent, pending common.Hash) (s *Snapshot, e error)
Release(blockHash common.Hash) error
}
Loading

0 comments on commit f00b681

Please sign in to comment.