You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.7 KiB

  1. package state
  2. import (
  3. ac "github.com/tendermint/tendermint/account"
  4. . "github.com/tendermint/tendermint/common"
  5. "github.com/tendermint/tendermint/vm"
  6. "github.com/tendermint/tendermint/vm/sha3"
  7. )
  8. type TxCache struct {
  9. backend *BlockCache
  10. accounts map[Word256]vmAccountInfo
  11. storages map[Tuple256]Word256
  12. logs []*vm.Log
  13. }
  14. func NewTxCache(backend *BlockCache) *TxCache {
  15. return &TxCache{
  16. backend: backend,
  17. accounts: make(map[Word256]vmAccountInfo),
  18. storages: make(map[Tuple256]Word256),
  19. logs: make([]*vm.Log, 0),
  20. }
  21. }
  22. //-------------------------------------
  23. // TxCache.account
  24. func (cache *TxCache) GetAccount(addr Word256) *vm.Account {
  25. acc, removed := vmUnpack(cache.accounts[addr])
  26. if removed {
  27. return nil
  28. } else if acc == nil {
  29. acc2 := cache.backend.GetAccount(addr.Postfix(20))
  30. if acc2 != nil {
  31. return toVMAccount(acc2)
  32. }
  33. }
  34. return acc
  35. }
  36. func (cache *TxCache) UpdateAccount(acc *vm.Account) {
  37. addr := acc.Address
  38. // SANITY CHECK
  39. _, removed := vmUnpack(cache.accounts[addr])
  40. if removed {
  41. panic("UpdateAccount on a removed account")
  42. }
  43. // SANITY CHECK END
  44. cache.accounts[addr] = vmAccountInfo{acc, false}
  45. }
  46. func (cache *TxCache) RemoveAccount(acc *vm.Account) {
  47. addr := acc.Address
  48. // SANITY CHECK
  49. _, removed := vmUnpack(cache.accounts[addr])
  50. if removed {
  51. panic("RemoveAccount on a removed account")
  52. }
  53. // SANITY CHECK END
  54. cache.accounts[addr] = vmAccountInfo{acc, true}
  55. }
  56. // Creates a 20 byte address and bumps the creator's nonce.
  57. func (cache *TxCache) CreateAccount(creator *vm.Account) *vm.Account {
  58. // Generate an address
  59. nonce := creator.Nonce
  60. creator.Nonce += 1
  61. addr := LeftPadWord256(NewContractAddress(creator.Address.Postfix(20), int(nonce)))
  62. // Create account from address.
  63. account, removed := vmUnpack(cache.accounts[addr])
  64. if removed || account == nil {
  65. account = &vm.Account{
  66. Address: addr,
  67. Balance: 0,
  68. Code: nil,
  69. Nonce: 0,
  70. StorageRoot: Zero256,
  71. }
  72. cache.accounts[addr] = vmAccountInfo{account, false}
  73. return account
  74. } else {
  75. panic(Fmt("Could not create account, address already exists: %X", addr))
  76. }
  77. }
  78. // TxCache.account
  79. //-------------------------------------
  80. // TxCache.storage
  81. func (cache *TxCache) GetStorage(addr Word256, key Word256) Word256 {
  82. // Check cache
  83. value, ok := cache.storages[Tuple256{addr, key}]
  84. if ok {
  85. return value
  86. }
  87. // Load from backend
  88. return cache.backend.GetStorage(addr, key)
  89. }
  90. // NOTE: Set value to zero to removed from the trie.
  91. func (cache *TxCache) SetStorage(addr Word256, key Word256, value Word256) {
  92. _, removed := vmUnpack(cache.accounts[addr])
  93. if removed {
  94. panic("SetStorage() on a removed account")
  95. }
  96. cache.storages[Tuple256{addr, key}] = value
  97. }
  98. // TxCache.storage
  99. //-------------------------------------
  100. // These updates do not have to be in deterministic order,
  101. // the backend is responsible for ordering updates.
  102. func (cache *TxCache) Sync() {
  103. // Remove or update storage
  104. for addrKey, value := range cache.storages {
  105. addr, key := Tuple256Split(addrKey)
  106. cache.backend.SetStorage(addr, key, value)
  107. }
  108. // Remove or update accounts
  109. for addr, accInfo := range cache.accounts {
  110. acc, removed := vmUnpack(accInfo)
  111. if removed {
  112. cache.backend.RemoveAccount(addr.Postfix(20))
  113. } else {
  114. cache.backend.UpdateAccount(toStateAccount(acc))
  115. }
  116. }
  117. // TODO support logs, add them to the cache somehow.
  118. }
  119. func (cache *TxCache) AddLog(log *vm.Log) {
  120. cache.logs = append(cache.logs, log)
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Convenience function to return address of new contract
  124. func NewContractAddress(caller []byte, nonce int) []byte {
  125. temp := make([]byte, 32+8)
  126. copy(temp, caller)
  127. PutInt64BE(temp[32:], int64(nonce))
  128. return sha3.Sha3(temp)[:20]
  129. }
  130. // Converts backend.Account to vm.Account struct.
  131. func toVMAccount(acc *ac.Account) *vm.Account {
  132. return &vm.Account{
  133. Address: LeftPadWord256(acc.Address),
  134. Balance: acc.Balance,
  135. Code: acc.Code, // This is crazy.
  136. Nonce: int64(acc.Sequence),
  137. StorageRoot: LeftPadWord256(acc.StorageRoot),
  138. Other: acc.PubKey,
  139. }
  140. }
  141. // Converts vm.Account to backend.Account struct.
  142. func toStateAccount(acc *vm.Account) *ac.Account {
  143. pubKey, ok := acc.Other.(ac.PubKey)
  144. if !ok {
  145. pubKey = nil
  146. }
  147. var storageRoot []byte
  148. if acc.StorageRoot.IsZero() {
  149. storageRoot = nil
  150. } else {
  151. storageRoot = acc.StorageRoot.Bytes()
  152. }
  153. return &ac.Account{
  154. Address: acc.Address.Postfix(20),
  155. PubKey: pubKey,
  156. Balance: acc.Balance,
  157. Code: acc.Code,
  158. Sequence: int(acc.Nonce),
  159. StorageRoot: storageRoot,
  160. }
  161. }
  162. type vmAccountInfo struct {
  163. account *vm.Account
  164. removed bool
  165. }
  166. func vmUnpack(accInfo vmAccountInfo) (*vm.Account, bool) {
  167. return accInfo.account, accInfo.removed
  168. }