|
| 1 | +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package runtime |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto/sha256" |
| 9 | + "errors" |
| 10 | + |
| 11 | + "github.com/ava-labs/avalanchego/database" |
| 12 | + "github.com/ava-labs/avalanchego/ids" |
| 13 | + |
| 14 | + "github.com/ava-labs/hypersdk/codec" |
| 15 | + "github.com/ava-labs/hypersdk/state" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + _ ContractManager = &ContractStateManager{} |
| 20 | + ErrUnknownAccount = errors.New("unknown account") |
| 21 | + contractKeyBytes = []byte("contract") |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + // Global directory of contractIDs to contractBytes |
| 26 | + contractPrefix = 0x0 |
| 27 | + |
| 28 | + // Prefix for all contract state spaces |
| 29 | + accountPrefix = 0x1 |
| 30 | + // Associated data for an account, such as the contractID |
| 31 | + accountDataPrefix = 0x0 |
| 32 | + // State space associated with an account |
| 33 | + accountStatePrefix = 0x1 |
| 34 | +) |
| 35 | + |
| 36 | +// ContractStateManager is an out of the box implementation of the ContractManager interface. |
| 37 | +// The contract state manager is responsible for managing all state keys associated with contracts. |
| 38 | +type ContractStateManager struct { |
| 39 | + db state.Mutable |
| 40 | +} |
| 41 | + |
| 42 | +// NewContractStateManager returns a new ContractStateManager instance. |
| 43 | +// [prefix] must be unique to ensures the contract's state space |
| 44 | +// remains isolated from other state spaces in [db]. |
| 45 | +func NewContractStateManager( |
| 46 | + db state.Mutable, |
| 47 | + prefix []byte, |
| 48 | +) *ContractStateManager { |
| 49 | + prefixedState := newPrefixStateMutable(prefix, db) |
| 50 | + |
| 51 | + return &ContractStateManager{ |
| 52 | + db: prefixedState, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// GetContractState returns a mutable state instance associated with [account]. |
| 57 | +func (p *ContractStateManager) GetContractState(account codec.Address) state.Mutable { |
| 58 | + return newAccountPrefixedMutable(account, p.db) |
| 59 | +} |
| 60 | + |
| 61 | +// GetAccountContract grabs the associated id with [account]. The ID is the key mapping to the contractbytes |
| 62 | +// Errors if there is no found account or an error fetching |
| 63 | +func (p *ContractStateManager) GetAccountContract(ctx context.Context, account codec.Address) (ContractID, error) { |
| 64 | + contractID, exists, err := p.getAccountContract(ctx, account) |
| 65 | + if err != nil { |
| 66 | + return ids.Empty[:], err |
| 67 | + } |
| 68 | + if !exists { |
| 69 | + return ids.Empty[:], ErrUnknownAccount |
| 70 | + } |
| 71 | + return contractID[:], nil |
| 72 | +} |
| 73 | + |
| 74 | +// [contractID] -> [contractBytes] |
| 75 | +func (p *ContractStateManager) GetContractBytes(ctx context.Context, contractID ContractID) ([]byte, error) { |
| 76 | + // TODO: take fee out of balance? |
| 77 | + contractBytes, err := p.db.GetValue(ctx, contractKey(contractID)) |
| 78 | + if err != nil { |
| 79 | + return []byte{}, ErrUnknownAccount |
| 80 | + } |
| 81 | + |
| 82 | + return contractBytes, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (p *ContractStateManager) NewAccountWithContract(ctx context.Context, contractID ContractID, accountCreationData []byte) (codec.Address, error) { |
| 86 | + newID := sha256.Sum256(append(contractID, accountCreationData...)) |
| 87 | + newAccount := codec.CreateAddress(0, newID) |
| 88 | + return newAccount, p.SetAccountContract(ctx, newAccount, contractID) |
| 89 | +} |
| 90 | + |
| 91 | +func (p *ContractStateManager) SetAccountContract(ctx context.Context, account codec.Address, contractID ContractID) error { |
| 92 | + return p.db.Insert(ctx, accountDataKey(account[:], contractKeyBytes), contractID) |
| 93 | +} |
| 94 | + |
| 95 | +// setContract stores [contract] at [contractID] |
| 96 | +func (p *ContractStateManager) SetContractBytes( |
| 97 | + ctx context.Context, |
| 98 | + contractID ContractID, |
| 99 | + contract []byte, |
| 100 | +) error { |
| 101 | + return p.db.Insert(ctx, contractKey(contractID[:]), contract) |
| 102 | +} |
| 103 | + |
| 104 | +func contractKey(key []byte) (k []byte) { |
| 105 | + k = make([]byte, 0, 1+len(key)) |
| 106 | + k = append(k, contractPrefix) |
| 107 | + k = append(k, key...) |
| 108 | + return |
| 109 | +} |
| 110 | + |
| 111 | +// Creates a key an account balance key |
| 112 | +func accountDataKey(account []byte, key []byte) (k []byte) { |
| 113 | + // accountPrefix + account + accountDataPrefix + key |
| 114 | + k = make([]byte, 0, 2+len(account)+len(key)) |
| 115 | + k = append(k, accountPrefix) |
| 116 | + k = append(k, account...) |
| 117 | + k = append(k, accountDataPrefix) |
| 118 | + k = append(k, key...) |
| 119 | + return |
| 120 | +} |
| 121 | + |
| 122 | +func accountContractKey(account []byte) []byte { |
| 123 | + return accountDataKey(account, contractKeyBytes) |
| 124 | +} |
| 125 | + |
| 126 | +func (p *ContractStateManager) getAccountContract(ctx context.Context, account codec.Address) (ids.ID, bool, error) { |
| 127 | + v, err := p.db.GetValue(ctx, accountContractKey(account[:])) |
| 128 | + if errors.Is(err, database.ErrNotFound) { |
| 129 | + return ids.Empty, false, nil |
| 130 | + } |
| 131 | + if err != nil { |
| 132 | + return ids.Empty, false, err |
| 133 | + } |
| 134 | + return ids.ID(v[:ids.IDLen]), true, nil |
| 135 | +} |
| 136 | + |
| 137 | +// prefixed state |
| 138 | +type prefixedStateMutable struct { |
| 139 | + inner state.Mutable |
| 140 | + prefix []byte |
| 141 | +} |
| 142 | + |
| 143 | +func newPrefixStateMutable(prefix []byte, inner state.Mutable) *prefixedStateMutable { |
| 144 | + return &prefixedStateMutable{inner: inner, prefix: prefix} |
| 145 | +} |
| 146 | + |
| 147 | +func (s *prefixedStateMutable) prefixKey(key []byte) (k []byte) { |
| 148 | + k = make([]byte, len(s.prefix)+len(key)) |
| 149 | + copy(k, s.prefix) |
| 150 | + copy(k[len(s.prefix):], key) |
| 151 | + return |
| 152 | +} |
| 153 | + |
| 154 | +func (s *prefixedStateMutable) GetValue(ctx context.Context, key []byte) (value []byte, err error) { |
| 155 | + return s.inner.GetValue(ctx, s.prefixKey(key)) |
| 156 | +} |
| 157 | + |
| 158 | +func (s *prefixedStateMutable) Insert(ctx context.Context, key []byte, value []byte) error { |
| 159 | + return s.inner.Insert(ctx, s.prefixKey(key), value) |
| 160 | +} |
| 161 | + |
| 162 | +func (s *prefixedStateMutable) Remove(ctx context.Context, key []byte) error { |
| 163 | + return s.inner.Remove(ctx, s.prefixKey(key)) |
| 164 | +} |
| 165 | + |
| 166 | +func newAccountPrefixedMutable(account codec.Address, mutable state.Mutable) state.Mutable { |
| 167 | + return &prefixedStateMutable{inner: mutable, prefix: accountStateKey(account[:])} |
| 168 | +} |
| 169 | + |
| 170 | +// [accountPrefix] + [account] + [accountStatePrefix] = state space associated with a contract |
| 171 | +func accountStateKey(key []byte) (k []byte) { |
| 172 | + k = make([]byte, 2+len(key)) |
| 173 | + k[0] = accountPrefix |
| 174 | + copy(k[1:], key) |
| 175 | + k[len(k)-1] = accountStatePrefix |
| 176 | + return |
| 177 | +} |
0 commit comments