Merge pull request #5 from bas-vk/state

core/quorum, core: add private transaction state processing
This commit is contained in:
Patrick Mylund Nielsen 2016-11-16 10:10:11 -05:00 committed by GitHub
commit f4b5a8c83e
3 changed files with 25 additions and 18 deletions

View File

@ -21,7 +21,7 @@ type BlockMaker interface {
} }
type pendingState struct { type pendingState struct {
state *state.StateDB // apply state changes here publicState, privateState *state.StateDB
tcount int // tx count in cycle tcount int // tx count in cycle
gp *core.GasPool gp *core.GasPool
ownedAccounts *set.Set ownedAccounts *set.Set
@ -38,7 +38,7 @@ type pendingState struct {
} }
func (ps *pendingState) applyTransaction(tx *types.Transaction, bc *core.BlockChain, cc *core.ChainConfig) (error, vm.Logs) { func (ps *pendingState) applyTransaction(tx *types.Transaction, bc *core.BlockChain, cc *core.ChainConfig) (error, vm.Logs) {
snap := ps.state.Snapshot() publicSnaphot, privateSnapshot := ps.publicState.Snapshot(), ps.privateState.Snapshot()
// this is a bit of a hack to force jit for the miners // this is a bit of a hack to force jit for the miners
config := cc.VmConfig config := cc.VmConfig
@ -47,9 +47,11 @@ func (ps *pendingState) applyTransaction(tx *types.Transaction, bc *core.BlockCh
} }
config.ForceJit = false // disable forcing jit config.ForceJit = false // disable forcing jit
receipt, logs, _, err := core.ApplyTransaction(cc, bc, ps.gp, ps.state, ps.state, ps.header, tx, ps.header.GasUsed, config) receipt, logs, _, err := core.ApplyTransaction(cc, bc, ps.gp, ps.publicState, ps.privateState, ps.header, tx, ps.header.GasUsed, config)
if err != nil { if err != nil {
ps.state.RevertToSnapshot(snap) ps.publicState.RevertToSnapshot(publicSnaphot)
ps.privateState.RevertToSnapshot(privateSnapshot)
return err, nil return err, nil
} }
ps.txs = append(ps.txs, tx) ps.txs = append(ps.txs, tx)
@ -76,7 +78,7 @@ func (ps *pendingState) applyTransactions(txs *types.TransactionsByPriorityAndNo
from, _ := tx.From() from, _ := tx.From()
// Start executing the transaction // Start executing the transaction
ps.state.StartRecord(tx.Hash(), common.Hash{}, 0) ps.publicState.StartRecord(tx.Hash(), common.Hash{}, 0)
err, logs := ps.applyTransaction(tx, bc, cc) err, logs := ps.applyTransaction(tx, bc, cc)
switch { switch {

View File

@ -98,14 +98,15 @@ func NewBlockVoting(bc *core.BlockChain, chainConfig *core.ChainConfig, txpool *
} }
func (bv *BlockVoting) resetPendingState(parent *types.Block) { func (bv *BlockVoting) resetPendingState(parent *types.Block) {
statedb, _, err := bv.bc.StateAt(parent.Root()) publicState, privateState, err := bv.bc.State()
if err != nil { if err != nil {
panic(fmt.Sprintf("State corrupt: %v", err)) panic(fmt.Sprintf("State error: %v", err))
} }
ps := &pendingState{ ps := &pendingState{
parent: parent, parent: parent,
state: statedb, publicState: publicState,
privateState: privateState,
header: bv.makeHeader(parent), header: bv.makeHeader(parent),
gp: new(core.GasPool), gp: new(core.GasPool),
ownedAccounts: accountAddressesSet(bv.am.Accounts()), ownedAccounts: accountAddressesSet(bv.am.Accounts()),
@ -295,7 +296,7 @@ func (bv *BlockVoting) applyTransaction(tx *types.Transaction) {
func (bv *BlockVoting) Pending() (*types.Block, *state.StateDB) { func (bv *BlockVoting) Pending() (*types.Block, *state.StateDB) {
bv.pStateMu.Lock() bv.pStateMu.Lock()
defer bv.pStateMu.Unlock() defer bv.pStateMu.Unlock()
return types.NewBlock(bv.pState.header, bv.pState.txs, nil, bv.pState.receipts), bv.pState.state.Copy() return types.NewBlock(bv.pState.header, bv.pState.txs, nil, bv.pState.receipts), bv.pState.publicState.Copy()
} }
func (bv *BlockVoting) createBlock() (*types.Block, error) { func (bv *BlockVoting) createBlock() (*types.Block, error) {
@ -314,13 +315,13 @@ func (bv *BlockVoting) createBlock() (*types.Block, error) {
bv.pStateMu.Lock() bv.pStateMu.Lock()
defer bv.pStateMu.Unlock() defer bv.pStateMu.Unlock()
state := bv.pState.state // shortcut state := bv.pState.publicState // shortcut
header := bv.pState.header header := bv.pState.header
receipts := bv.pState.receipts receipts := bv.pState.receipts
core.AccumulateRewards(state, header, nil) core.AccumulateRewards(state, header, nil)
header.Root = bv.pState.state.IntermediateRoot() header.Root = state.IntermediateRoot()
// Quorum blocks contain a signature of the header in the Extra field. // Quorum blocks contain a signature of the header in the Extra field.
// This signature is verified during block import and ensures that the // This signature is verified during block import and ensures that the

View File

@ -136,6 +136,10 @@ func (self *VMEnv) SnapshotDatabase() int {
return self.currentState().Snapshot() return self.currentState().Snapshot()
} }
// We only need to revert the current state because when we call from private
// public state it's read only, there wouldn't be anything to reset.
// (A)->(B)->C->(B): A failure in (B) wouldn't need to reset C, as C was flagged
// read only.
func (self *VMEnv) RevertToSnapshot(snapshot int) { func (self *VMEnv) RevertToSnapshot(snapshot int) {
self.currentState().RevertToSnapshot(snapshot) self.currentState().RevertToSnapshot(snapshot)
} }