Created
May 17, 2023 06:59
-
-
Save riemannulus/404ab312e1e2717a08d757ff3de9b102 to your computer and use it in GitHub Desktop.
Psuedo IAccount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Pure] | |
internal class AccountStateDeltaImpl : IValidatorSupportStateDelta, IAccountStateDelta | |
{ | |
[Pure] | |
IAccount? IAccountStateView.GetAccount(Address address) | |
{ | |
return UpdatedAccount.TryGetValue(address, out IAccount? account) | |
? account | |
: AccountGetter(address); | |
} | |
[Pure] | |
IAccountStateDelta IAccountStateDelta.SetAccount(IAccount account) => | |
UpdateAccount(UpdatedAccount.SetItem(account.address, account)); | |
} | |
public class Account : IAccount | |
{ | |
public long Nonce => _blockChainStates.GetNextTxNonce(Address); | |
public Address Address { get; } | |
public HashDigest<SHA256> AccountStateRootHash => _accountStorage.Hash; | |
private readonly ITrie _accountStorage; | |
private readonly IBlockChainStates _blockChainStates; | |
public Account( | |
Address address, | |
IBlockChainStates blockChainStates, | |
Trie accountStorage) | |
{ | |
Address = address; | |
_accountStorage = accountStorage; | |
_blockChainStates = blockChainStates; | |
} | |
public IValue? GetState(Address address) | |
{ | |
return _accountStorage.Get(address); | |
} | |
public IAccount SetState(Address address, IValue value) | |
{ | |
return new Account( | |
Address, | |
_blockChainStates, | |
_accountStorage.Set(address, value)); | |
} | |
} | |
public static void ValidateWorldId(this IAccountStateView states, Address avatarAddress, int worldId) | |
{ | |
if (worldId > 1) | |
{ | |
if (worldId == GameConfig.MimisbrunnrWorldId) | |
{ | |
throw new InvalidWorldException(); | |
} | |
// Make world first. | |
IAccount? worldAccount = states.GetAccount(unlockedWorldIdsAddress); | |
if(worldAccount is null) | |
{ | |
throw new InvalidWorldException(); | |
} | |
// Unlock first. | |
if(!(worldIdsAccount.GetState(avatarAddress) { } IValue rawIds)) | |
{ | |
throw new InvalidWorldException(); | |
} | |
List<int> unlockedWorldIds = rawIds.ToList(StateExtensions.ToInteger); | |
if (!unlockedWorldIds.Contains(worldId)) | |
{ | |
throw new InvalidWorldException(); | |
} | |
} | |
} | |
public static IAccountStateDelta SetWorldId(this IAccountStateDelta states, Address avatarAddress, int unlockedWorldId) | |
{ | |
IAccount? workdIdsAccount = state.GetState(unlockedWorldIdsAddress); | |
if(worldAccount is null) | |
{ | |
throw new InvalidWorldException(); | |
} | |
worldAccount.SetState(avatarAddress, unlockedWorldId); | |
return states.SetAccount(worldAccount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment