2013-12-26 03:45:52 -08:00
|
|
|
|
|
|
|
// Blocks, blocks will have transactions.
|
|
|
|
// Transactions/contracts are updated in goroutines
|
|
|
|
// Each contract should send a message on a channel with usage statistics
|
|
|
|
// The statics can be used for fee calculation within the block update method
|
|
|
|
// Statistics{transaction, /* integers */ normal_ops, store_load, extro_balance, crypto, steps}
|
|
|
|
// The block updater will wait for all goroutines to be finished and update the block accordingly
|
|
|
|
// in one go and should use minimal IO overhead.
|
|
|
|
// The actual block updating will happen within a goroutine as well so normal operation may continue
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
_"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlockManager struct {
|
|
|
|
vm *Vm
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockManager() *BlockManager {
|
|
|
|
bm := &BlockManager{vm: NewVm()}
|
|
|
|
|
|
|
|
return bm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process a block.
|
|
|
|
func (bm *BlockManager) ProcessBlock(block *Block) error {
|
2013-12-28 16:36:59 -08:00
|
|
|
// Get the tx count. Used to create enough channels to 'join' the go routines
|
2013-12-26 03:45:52 -08:00
|
|
|
txCount := len(block.transactions)
|
2013-12-28 16:36:59 -08:00
|
|
|
// Locking channel. When it has been fully buffered this method will return
|
2013-12-26 03:45:52 -08:00
|
|
|
lockChan := make(chan bool, txCount)
|
|
|
|
|
2013-12-28 16:36:59 -08:00
|
|
|
// Process each transaction/contract
|
2013-12-26 03:45:52 -08:00
|
|
|
for _, tx := range block.transactions {
|
2014-01-02 14:02:24 -08:00
|
|
|
go bm.ProcessTransaction(tx, block, lockChan)
|
2013-12-26 03:45:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all Tx to finish processing
|
|
|
|
for i := 0; i < txCount; i++ {
|
|
|
|
<- lockChan
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-02 14:02:24 -08:00
|
|
|
func (bm *BlockManager) ProcessTransaction(tx *Transaction, block *Block, lockChan chan bool) {
|
2014-01-02 15:43:49 -08:00
|
|
|
bm.vm.RunTransaction(tx, block, func(opType OpType) bool {
|
|
|
|
// TODO calculate fees
|
2013-12-26 03:45:52 -08:00
|
|
|
|
2014-01-02 15:43:49 -08:00
|
|
|
return true // Continue
|
|
|
|
})
|
2013-12-26 03:45:52 -08:00
|
|
|
|
|
|
|
// Broadcast we're done
|
|
|
|
lockChan <- true
|
|
|
|
}
|