Skip to content

Commit

Permalink
Merge pull request #2052 from moreal/bugfix/action-eval-common-compon…
Browse files Browse the repository at this point in the history
…ents/link-states
  • Loading branch information
moreal authored Aug 4, 2023
2 parents 62df43a + 9bb9064 commit 8840ec2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class AccountStateDelta : IAccountStateDelta
private ValidatorSet? _validatorSet;
private IAccountDelta _delta;

public IAccountState BaseState { get; set; }

public IImmutableSet<Address> UpdatedAddresses => _delta.UpdatedAddresses;

public IImmutableSet<Address> StateUpdatedAddresses => _delta.StateUpdatedAddresses;
Expand Down Expand Up @@ -121,7 +123,7 @@ public AccountStateDelta(byte[] bytes)
public IValue? GetState(Address address) =>
_states.ContainsKey(address)
? _states[address]
: throw new NotSupportedException();
: BaseState.GetState(address);

public IReadOnlyList<IValue?> GetStates(IReadOnlyList<Address> addresses) =>
addresses.Select(GetState).ToArray();
Expand All @@ -132,7 +134,7 @@ public FungibleAssetValue GetBalance(Address address, Currency currency)
{
if (!_fungibles.TryGetValue((address, currency), out BigInteger rawValue))
{
throw new NotSupportedException();
return BaseState.GetBalance(address, currency);
}

return FungibleAssetValue.FromRawValue(currency, rawValue);
Expand All @@ -155,7 +157,7 @@ public FungibleAssetValue GetTotalSupply(Currency currency)
return FungibleAssetValue.FromRawValue(currency, totalSupplyValue);
}

throw new NotSupportedException();
return BaseState.GetTotalSupply(currency);
}

public IAccountStateDelta MintAsset(
Expand Down Expand Up @@ -192,7 +194,10 @@ public IAccountStateDelta MintAsset(
),
_totalSupplies.SetItem(currency, (currentTotalSupply + value).RawValue),
_validatorSet
);
)
{
BaseState = BaseState,
};
}

return new AccountStateDelta(
Expand All @@ -203,7 +208,10 @@ public IAccountStateDelta MintAsset(
),
_totalSupplies,
_validatorSet
);
)
{
BaseState = BaseState,
};
}

public IAccountStateDelta TransferAsset(
Expand Down Expand Up @@ -234,7 +242,10 @@ public IAccountStateDelta TransferAsset(
var balances = _fungibles
.SetItem((sender, currency), senderRemains.RawValue)
.SetItem((recipient, currency), recipientRemains.RawValue);
return new AccountStateDelta(_states, balances, _totalSupplies, _validatorSet);
return new AccountStateDelta(_states, balances, _totalSupplies, _validatorSet)
{
BaseState = BaseState,
};
}

public IAccountStateDelta BurnAsset(
Expand Down Expand Up @@ -273,12 +284,15 @@ public IAccountStateDelta BurnAsset(
(GetTotalSupply(currency) - value).RawValue)
: _totalSupplies,
_validatorSet
);
)
{
BaseState = BaseState,
};
}

public ValidatorSet GetValidatorSet()
{
return _validatorSet ?? throw new NotSupportedException();
return _validatorSet ?? BaseState.GetValidatorSet();
}

public IAccountStateDelta SetValidator(Validator validator)
Expand All @@ -288,6 +302,9 @@ public IAccountStateDelta SetValidator(Validator validator)
_fungibles,
_totalSupplies,
GetValidatorSet().Update(validator)
);
)
{
BaseState = BaseState,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public IReadOnlyList<IActionEvaluation> Evaluate(IPreEvaluationBlock block)
var actionEvaluations = evaluationResponse.Evaluations.Select(ActionEvaluationMarshaller.Deserialize)
.ToImmutableList();

for (var i = 0; i < actionEvaluations.Count; ++i)
{
if (i > 0)
{
actionEvaluations[i].InputContext.PreviousState.BaseState =
actionEvaluations[i - 1].OutputState;
}
else
{
actionEvaluations[i].InputContext.PreviousState.BaseState =
_blockChainStates.GetBlockState(block.PreviousHash);
}

actionEvaluations[i].OutputState.BaseState =
actionEvaluations[i].InputContext.PreviousState;
}

return actionEvaluations;
}

Expand Down

0 comments on commit 8840ec2

Please sign in to comment.