github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/acm/acmstate/memory_state.go (about)

     1  package acmstate
     2  
     3  import (
     4  	"github.com/hyperledger/burrow/acm"
     5  	"github.com/hyperledger/burrow/binary"
     6  	"github.com/hyperledger/burrow/crypto"
     7  	"github.com/hyperledger/burrow/execution/errors"
     8  	"github.com/hyperledger/burrow/permission"
     9  )
    10  
    11  type MemoryState struct {
    12  	Accounts map[crypto.Address]*acm.Account
    13  	Storage  map[crypto.Address]map[binary.Word256][]byte
    14  	Metadata map[MetadataHash]string
    15  }
    16  
    17  var _ IterableReaderWriter = &MemoryState{}
    18  
    19  // Get an in-memory state IterableReader
    20  func NewMemoryState() *MemoryState {
    21  	return &MemoryState{
    22  		Accounts: map[crypto.Address]*acm.Account{
    23  			acm.GlobalPermissionsAddress: {
    24  				Permissions: permission.DefaultAccountPermissions,
    25  			},
    26  		},
    27  		Storage:  make(map[crypto.Address]map[binary.Word256][]byte),
    28  		Metadata: make(map[MetadataHash]string),
    29  	}
    30  }
    31  
    32  func (ms *MemoryState) GetAccount(address crypto.Address) (*acm.Account, error) {
    33  	return ms.Accounts[address], nil
    34  }
    35  
    36  func (ms *MemoryState) UpdateAccount(updatedAccount *acm.Account) error {
    37  	if updatedAccount == nil {
    38  		return errors.Errorf(errors.Codes.IllegalWrite, "UpdateAccount passed nil account in MemoryState")
    39  	}
    40  	ms.Accounts[updatedAccount.GetAddress()] = updatedAccount
    41  	return nil
    42  }
    43  
    44  func (ms *MemoryState) GetMetadata(metahash MetadataHash) (string, error) {
    45  	return ms.Metadata[metahash], nil
    46  }
    47  
    48  func (ms *MemoryState) SetMetadata(metahash MetadataHash, metadata string) error {
    49  	ms.Metadata[metahash] = metadata
    50  	return nil
    51  }
    52  
    53  func (ms *MemoryState) RemoveAccount(address crypto.Address) error {
    54  	delete(ms.Accounts, address)
    55  	return nil
    56  }
    57  
    58  func (ms *MemoryState) GetStorage(address crypto.Address, key binary.Word256) ([]byte, error) {
    59  	_, ok := ms.Accounts[address]
    60  	if !ok {
    61  		return nil, errors.Errorf(errors.Codes.NonExistentAccount,
    62  			"could not get storage for non-existent account: %v", address)
    63  	}
    64  	storage, ok := ms.Storage[address]
    65  	if !ok {
    66  		return nil, nil
    67  	}
    68  	return storage[key], nil
    69  }
    70  
    71  func (ms *MemoryState) SetStorage(address crypto.Address, key binary.Word256, value []byte) error {
    72  	storage, ok := ms.Storage[address]
    73  	if !ok {
    74  		storage = make(map[binary.Word256][]byte)
    75  		ms.Storage[address] = storage
    76  	}
    77  	storage[key] = value
    78  	return nil
    79  }
    80  
    81  func (ms *MemoryState) IterateAccounts(consumer func(*acm.Account) error) (err error) {
    82  	for _, acc := range ms.Accounts {
    83  		if err := consumer(acc); err != nil {
    84  			return err
    85  		}
    86  	}
    87  	return nil
    88  }
    89  
    90  func (ms *MemoryState) IterateStorage(address crypto.Address, consumer func(key binary.Word256, value []byte) error) (err error) {
    91  	for key, value := range ms.Storage[address] {
    92  		if err := consumer(key, value); err != nil {
    93  			return err
    94  		}
    95  	}
    96  	return nil
    97  }