github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/state/journal.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package state
    26  
    27  import (
    28  	"math/big"
    29  
    30  	"github.com/ethereum/go-ethereum/common"
    31  )
    32  
    33  //Journalentry是状态更改日志中的一个修改条目,可以
    34  //按需归还。
    35  type journalEntry interface {
    36  //REVERT撤消此日记条目引入的更改。
    37  	revert(*StateDB)
    38  
    39  //dirtied返回此日记条目修改的以太坊地址。
    40  	dirtied() *common.Address
    41  }
    42  
    43  //日志包含自上次状态以来应用的状态修改列表
    44  //承诺。它们被跟踪,以便在执行时能够还原。
    45  //异常或还原请求。
    46  type journal struct {
    47  entries []journalEntry         //日记帐跟踪的当前更改
    48  dirties map[common.Address]int //脏帐户和更改数
    49  }
    50  
    51  //NewJournal创建新的初始化日志。
    52  func newJournal() *journal {
    53  	return &journal{
    54  		dirties: make(map[common.Address]int),
    55  	}
    56  }
    57  
    58  //append在变更日志的末尾插入一个新的修改条目。
    59  func (j *journal) append(entry journalEntry) {
    60  	j.entries = append(j.entries, entry)
    61  	if addr := entry.dirtied(); addr != nil {
    62  		j.dirties[*addr]++
    63  	}
    64  }
    65  
    66  //REVERT撤消一批日志修改以及任何已还原的
    67  //处理也很脏。
    68  func (j *journal) revert(statedb *StateDB, snapshot int) {
    69  	for i := len(j.entries) - 1; i >= snapshot; i-- {
    70  //撤消操作所做的更改
    71  		j.entries[i].revert(statedb)
    72  
    73  //删除由更改引起的任何脏跟踪
    74  		if addr := j.entries[i].dirtied(); addr != nil {
    75  			if j.dirties[*addr]--; j.dirties[*addr] == 0 {
    76  				delete(j.dirties, *addr)
    77  			}
    78  		}
    79  	}
    80  	j.entries = j.entries[:snapshot]
    81  }
    82  
    83  //dirty显式地将地址设置为dirty,即使更改条目
    84  //否则建议清洁。这个方法是处理ripemd的丑陋的黑客
    85  //预编译共识异常。
    86  func (j *journal) dirty(addr common.Address) {
    87  	j.dirties[addr]++
    88  }
    89  
    90  //length返回日记帐中的当前条目数。
    91  func (j *journal) length() int {
    92  	return len(j.entries)
    93  }
    94  
    95  type (
    96  //对帐户trie的更改。
    97  	createObjectChange struct {
    98  		account *common.Address
    99  	}
   100  	resetObjectChange struct {
   101  		prev *stateObject
   102  	}
   103  	suicideChange struct {
   104  		account     *common.Address
   105  prev        bool //账户是否已经自杀
   106  		prevbalance *big.Int
   107  	}
   108  
   109  //个人账户变更。
   110  	balanceChange struct {
   111  		account *common.Address
   112  		prev    *big.Int
   113  	}
   114  	nonceChange struct {
   115  		account *common.Address
   116  		prev    uint64
   117  	}
   118  	storageChange struct {
   119  		account       *common.Address
   120  		key, prevalue common.Hash
   121  	}
   122  	codeChange struct {
   123  		account            *common.Address
   124  		prevcode, prevhash []byte
   125  	}
   126  
   127  //其他状态值的更改。
   128  	refundChange struct {
   129  		prev uint64
   130  	}
   131  	addLogChange struct {
   132  		txhash common.Hash
   133  	}
   134  	addPreimageChange struct {
   135  		hash common.Hash
   136  	}
   137  	touchChange struct {
   138  		account   *common.Address
   139  		prev      bool
   140  		prevDirty bool
   141  	}
   142  )
   143  
   144  func (ch createObjectChange) revert(s *StateDB) {
   145  	delete(s.stateObjects, *ch.account)
   146  	delete(s.stateObjectsDirty, *ch.account)
   147  }
   148  
   149  func (ch createObjectChange) dirtied() *common.Address {
   150  	return ch.account
   151  }
   152  
   153  func (ch resetObjectChange) revert(s *StateDB) {
   154  	s.setStateObject(ch.prev)
   155  }
   156  
   157  func (ch resetObjectChange) dirtied() *common.Address {
   158  	return nil
   159  }
   160  
   161  func (ch suicideChange) revert(s *StateDB) {
   162  	obj := s.getStateObject(*ch.account)
   163  	if obj != nil {
   164  		obj.suicided = ch.prev
   165  		obj.setBalance(ch.prevbalance)
   166  	}
   167  }
   168  
   169  func (ch suicideChange) dirtied() *common.Address {
   170  	return ch.account
   171  }
   172  
   173  var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
   174  
   175  func (ch touchChange) revert(s *StateDB) {
   176  }
   177  
   178  func (ch touchChange) dirtied() *common.Address {
   179  	return ch.account
   180  }
   181  
   182  func (ch balanceChange) revert(s *StateDB) {
   183  	s.getStateObject(*ch.account).setBalance(ch.prev)
   184  }
   185  
   186  func (ch balanceChange) dirtied() *common.Address {
   187  	return ch.account
   188  }
   189  
   190  func (ch nonceChange) revert(s *StateDB) {
   191  	s.getStateObject(*ch.account).setNonce(ch.prev)
   192  }
   193  
   194  func (ch nonceChange) dirtied() *common.Address {
   195  	return ch.account
   196  }
   197  
   198  func (ch codeChange) revert(s *StateDB) {
   199  	s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
   200  }
   201  
   202  func (ch codeChange) dirtied() *common.Address {
   203  	return ch.account
   204  }
   205  
   206  func (ch storageChange) revert(s *StateDB) {
   207  	s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
   208  }
   209  
   210  func (ch storageChange) dirtied() *common.Address {
   211  	return ch.account
   212  }
   213  
   214  func (ch refundChange) revert(s *StateDB) {
   215  	s.refund = ch.prev
   216  }
   217  
   218  func (ch refundChange) dirtied() *common.Address {
   219  	return nil
   220  }
   221  
   222  func (ch addLogChange) revert(s *StateDB) {
   223  	logs := s.logs[ch.txhash]
   224  	if len(logs) == 1 {
   225  		delete(s.logs, ch.txhash)
   226  	} else {
   227  		s.logs[ch.txhash] = logs[:len(logs)-1]
   228  	}
   229  	s.logSize--
   230  }
   231  
   232  func (ch addLogChange) dirtied() *common.Address {
   233  	return nil
   234  }
   235  
   236  func (ch addPreimageChange) revert(s *StateDB) {
   237  	delete(s.preimages, ch.hash)
   238  }
   239  
   240  func (ch addPreimageChange) dirtied() *common.Address {
   241  	return nil
   242  }