Browse Source

Remember StorageRoot in Other

pull/123/head
Jae Kwon 9 years ago
parent
commit
bff06ac657
1 changed files with 31 additions and 12 deletions
  1. +31
    -12
      state/tx_cache.go

+ 31
- 12
state/tx_cache.go View File

@ -28,7 +28,7 @@ func NewTxCache(backend *BlockCache) *TxCache {
// TxCache.account // TxCache.account
func (cache *TxCache) GetAccount(addr Word256) *vm.Account { func (cache *TxCache) GetAccount(addr Word256) *vm.Account {
acc, removed := vmUnpack(cache.accounts[addr])
acc, removed := cache.accounts[addr].unpack()
if removed { if removed {
return nil return nil
} else if acc == nil { } else if acc == nil {
@ -42,7 +42,7 @@ func (cache *TxCache) GetAccount(addr Word256) *vm.Account {
func (cache *TxCache) UpdateAccount(acc *vm.Account) { func (cache *TxCache) UpdateAccount(acc *vm.Account) {
addr := acc.Address addr := acc.Address
_, removed := vmUnpack(cache.accounts[addr])
_, removed := cache.accounts[addr].unpack()
if removed { if removed {
PanicSanity("UpdateAccount on a removed account") PanicSanity("UpdateAccount on a removed account")
} }
@ -51,7 +51,7 @@ func (cache *TxCache) UpdateAccount(acc *vm.Account) {
func (cache *TxCache) RemoveAccount(acc *vm.Account) { func (cache *TxCache) RemoveAccount(acc *vm.Account) {
addr := acc.Address addr := acc.Address
_, removed := vmUnpack(cache.accounts[addr])
_, removed := cache.accounts[addr].unpack()
if removed { if removed {
PanicSanity("RemoveAccount on a removed account") PanicSanity("RemoveAccount on a removed account")
} }
@ -68,7 +68,7 @@ func (cache *TxCache) CreateAccount(creator *vm.Account) *vm.Account {
addr := LeftPadWord256(NewContractAddress(creator.Address.Postfix(20), int(nonce))) addr := LeftPadWord256(NewContractAddress(creator.Address.Postfix(20), int(nonce)))
// Create account from address. // Create account from address.
account, removed := vmUnpack(cache.accounts[addr])
account, removed := cache.accounts[addr].unpack()
if removed || account == nil { if removed || account == nil {
account = &vm.Account{ account = &vm.Account{
Address: addr, Address: addr,
@ -76,7 +76,10 @@ func (cache *TxCache) CreateAccount(creator *vm.Account) *vm.Account {
Code: nil, Code: nil,
Nonce: 0, Nonce: 0,
Permissions: cache.GetAccount(ptypes.GlobalPermissionsAddress256).Permissions, Permissions: cache.GetAccount(ptypes.GlobalPermissionsAddress256).Permissions,
Other: nil,
Other: vmAccountOther{
PubKey: nil,
StorageRoot: nil,
},
} }
cache.accounts[addr] = vmAccountInfo{account, false} cache.accounts[addr] = vmAccountInfo{account, false}
return account return account
@ -104,7 +107,7 @@ func (cache *TxCache) GetStorage(addr Word256, key Word256) Word256 {
// NOTE: Set value to zero to removed from the trie. // NOTE: Set value to zero to removed from the trie.
func (cache *TxCache) SetStorage(addr Word256, key Word256, value Word256) { func (cache *TxCache) SetStorage(addr Word256, key Word256, value Word256) {
_, removed := vmUnpack(cache.accounts[addr])
_, removed := cache.accounts[addr].unpack()
if removed { if removed {
PanicSanity("SetStorage() on a removed account") PanicSanity("SetStorage() on a removed account")
} }
@ -126,7 +129,7 @@ func (cache *TxCache) Sync() {
// Remove or update accounts // Remove or update accounts
for addr, accInfo := range cache.accounts { for addr, accInfo := range cache.accounts {
acc, removed := vmUnpack(accInfo)
acc, removed := accInfo.unpack()
if removed { if removed {
cache.backend.RemoveAccount(addr.Postfix(20)) cache.backend.RemoveAccount(addr.Postfix(20))
} else { } else {
@ -157,15 +160,19 @@ func toVMAccount(acc *acm.Account) *vm.Account {
Code: acc.Code, // This is crazy. Code: acc.Code, // This is crazy.
Nonce: int64(acc.Sequence), Nonce: int64(acc.Sequence),
Permissions: acc.Permissions, // Copy Permissions: acc.Permissions, // Copy
Other: acc.PubKey,
Other: vmAccountOther{
PubKey: acc.PubKey,
StorageRoot: acc.StorageRoot,
},
} }
} }
// Converts vm.Account to backend.Account struct. // Converts vm.Account to backend.Account struct.
func toStateAccount(acc *vm.Account) *acm.Account { func toStateAccount(acc *vm.Account) *acm.Account {
pubKey, ok := acc.Other.(acm.PubKey)
if !ok {
pubKey = nil
var pubKey acm.PubKey
var storageRoot []byte
if acc.Other != nil {
pubKey, storageRoot = acc.Other.(vmAccountOther).unpack()
} }
return &acm.Account{ return &acm.Account{
@ -174,15 +181,27 @@ func toStateAccount(acc *vm.Account) *acm.Account {
Balance: acc.Balance, Balance: acc.Balance,
Code: acc.Code, Code: acc.Code,
Sequence: int(acc.Nonce), Sequence: int(acc.Nonce),
StorageRoot: storageRoot,
Permissions: acc.Permissions, // Copy Permissions: acc.Permissions, // Copy
} }
} }
// Everything in acmAccount that doesn't belong in
// exported vmAccount fields.
type vmAccountOther struct {
PubKey acm.PubKey
StorageRoot []byte
}
func (accOther vmAccountOther) unpack() (acm.PubKey, []byte) {
return accOther.PubKey, accOther.StorageRoot
}
type vmAccountInfo struct { type vmAccountInfo struct {
account *vm.Account account *vm.Account
removed bool removed bool
} }
func vmUnpack(accInfo vmAccountInfo) (*vm.Account, bool) {
func (accInfo vmAccountInfo) unpack() (*vm.Account, bool) {
return accInfo.account, accInfo.removed return accInfo.account, accInfo.removed
} }

Loading…
Cancel
Save