github.com/datachainlab/burrow@v0.25.0/execution/evm/fake_app_state.go (about)

     1  // Copyright 2017 Monax Industries Limited
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package evm
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"bytes"
    21  
    22  	"github.com/hyperledger/burrow/acm"
    23  	"github.com/hyperledger/burrow/acm/acmstate"
    24  	. "github.com/hyperledger/burrow/binary"
    25  	"github.com/hyperledger/burrow/crypto"
    26  )
    27  
    28  type FakeAppState struct {
    29  	accounts map[crypto.Address]*acm.Account
    30  	storage  map[string]Word256
    31  }
    32  
    33  var _ acmstate.ReaderWriter = &FakeAppState{}
    34  
    35  func (fas *FakeAppState) GetAccount(addr crypto.Address) (*acm.Account, error) {
    36  	account := fas.accounts[addr]
    37  	return account, nil
    38  }
    39  
    40  func (fas *FakeAppState) UpdateAccount(account *acm.Account) error {
    41  	fas.accounts[account.GetAddress()] = account
    42  	return nil
    43  }
    44  
    45  func (fas *FakeAppState) RemoveAccount(address crypto.Address) error {
    46  	_, ok := fas.accounts[address]
    47  	if !ok {
    48  		panic(fmt.Sprintf("Invalid account addr: %s", address))
    49  	} else {
    50  		// Remove account
    51  		delete(fas.accounts, address)
    52  	}
    53  	return nil
    54  }
    55  
    56  func (fas *FakeAppState) GetStorage(addr crypto.Address, key Word256) (Word256, error) {
    57  	_, ok := fas.accounts[addr]
    58  	if !ok {
    59  		panic(fmt.Sprintf("Invalid account addr: %s", addr))
    60  	}
    61  
    62  	value, ok := fas.storage[addr.String()+key.String()]
    63  	if ok {
    64  		return value, nil
    65  	} else {
    66  		return Zero256, nil
    67  	}
    68  }
    69  
    70  func (fas *FakeAppState) SetStorage(addr crypto.Address, key Word256, value Word256) error {
    71  	_, ok := fas.accounts[addr]
    72  	if !ok {
    73  
    74  		fmt.Println("\n\n", fas.accountsDump())
    75  		panic(fmt.Sprintf("Invalid account addr: %s", addr))
    76  	}
    77  
    78  	fas.storage[addr.String()+key.String()] = value
    79  	return nil
    80  }
    81  
    82  func (fas *FakeAppState) accountsDump() string {
    83  	buf := new(bytes.Buffer)
    84  	fmt.Fprint(buf, "Dumping accounts...", "\n")
    85  	for _, acc := range fas.accounts {
    86  		fmt.Fprint(buf, acc.GetAddress().String(), "\n")
    87  	}
    88  	return buf.String()
    89  }