From 6d75c3b1c07a98705561199d23e3e1ad9868c2be Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Wed, 16 Nov 2016 13:42:57 +0100 Subject: [PATCH] core/quorum, core: add private transaction state processing --- core/quorum/block_maker.go | 26 ++++++++++++++------------ core/quorum/block_voting.go | 13 +++++++------ core/vm_env.go | 4 ++++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/core/quorum/block_maker.go b/core/quorum/block_maker.go index 832d3d1f5..a5e6b0aaf 100755 --- a/core/quorum/block_maker.go +++ b/core/quorum/block_maker.go @@ -21,14 +21,14 @@ type BlockMaker interface { } type pendingState struct { - state *state.StateDB // apply state changes here - tcount int // tx count in cycle - gp *core.GasPool - ownedAccounts *set.Set - txs types.Transactions // set of transactions - lowGasTxs types.Transactions - failedTxs types.Transactions - parent *types.Block + publicState, privateState *state.StateDB + tcount int // tx count in cycle + gp *core.GasPool + ownedAccounts *set.Set + txs types.Transactions // set of transactions + lowGasTxs types.Transactions + failedTxs types.Transactions + parent *types.Block header *types.Header receipts types.Receipts @@ -38,7 +38,7 @@ type pendingState struct { } 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 config := cc.VmConfig @@ -47,9 +47,11 @@ func (ps *pendingState) applyTransaction(tx *types.Transaction, bc *core.BlockCh } 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 { - ps.state.RevertToSnapshot(snap) + ps.publicState.RevertToSnapshot(publicSnaphot) + ps.privateState.RevertToSnapshot(privateSnapshot) + return err, nil } ps.txs = append(ps.txs, tx) @@ -76,7 +78,7 @@ func (ps *pendingState) applyTransactions(txs *types.TransactionsByPriorityAndNo from, _ := tx.From() // 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) switch { diff --git a/core/quorum/block_voting.go b/core/quorum/block_voting.go index f3830c9c8..5503c1bf8 100755 --- a/core/quorum/block_voting.go +++ b/core/quorum/block_voting.go @@ -98,14 +98,15 @@ func NewBlockVoting(bc *core.BlockChain, chainConfig *core.ChainConfig, txpool * } func (bv *BlockVoting) resetPendingState(parent *types.Block) { - statedb, _, err := bv.bc.StateAt(parent.Root()) + publicState, privateState, err := bv.bc.State() if err != nil { - panic(fmt.Sprintf("State corrupt: %v", err)) + panic(fmt.Sprintf("State error: %v", err)) } ps := &pendingState{ parent: parent, - state: statedb, + publicState: publicState, + privateState: privateState, header: bv.makeHeader(parent), gp: new(core.GasPool), ownedAccounts: accountAddressesSet(bv.am.Accounts()), @@ -295,7 +296,7 @@ func (bv *BlockVoting) applyTransaction(tx *types.Transaction) { func (bv *BlockVoting) Pending() (*types.Block, *state.StateDB) { bv.pStateMu.Lock() 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) { @@ -314,13 +315,13 @@ func (bv *BlockVoting) createBlock() (*types.Block, error) { bv.pStateMu.Lock() defer bv.pStateMu.Unlock() - state := bv.pState.state // shortcut + state := bv.pState.publicState // shortcut header := bv.pState.header receipts := bv.pState.receipts 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. // This signature is verified during block import and ensures that the diff --git a/core/vm_env.go b/core/vm_env.go index c28b1ed4e..b6ca2ed80 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -136,6 +136,10 @@ func (self *VMEnv) SnapshotDatabase() int { 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) { self.currentState().RevertToSnapshot(snapshot) }