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.

199 lines
5.0 KiB

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