Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor state #331

Merged
merged 22 commits into from
Dec 18, 2023
Merged

refactor state #331

merged 22 commits into from
Dec 18, 2023

Conversation

rabbitprincess
Copy link
Member

What is changed?

add id field in ContractState ( to identify AccountState in vm )
move ContractState to statedb ( it related with evm_state_wip )
change balance modify logic in vm.go to use AccountState (luaSendAmount, luaCallContract, luaDeployContract)
replace add/sub balance logic to state.SendBalance()
fix some tests in vm and governance

Is there risk of fork?

yes. it need to sync test.

@rabbitprincess rabbitprincess added check hardfork Run sync test with the PR refactoring labels Dec 5, 2023
@@ -26,8 +26,7 @@ func executeGovernanceTx(ccc consensus.ChainConsensusCluster, bs *state.BlockSta
}

governance := string(txBody.Recipient)

scs, err := state.OpenContractState(receiver.AccountID(), receiver.State(), bs.StateDB)
scs, err := statedb.OpenContractState(receiver.IDNoPadding(), receiver.State(), bs.StateDB)
Copy link
Member Author

@rabbitprincess rabbitprincess Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike other IDs, the governance ID is saved in state without padding.

contract/vm.go Outdated Show resolved Hide resolved
contract/vm.go Outdated
len(v.curState.GetCodeHash()) != 0 {
ctx.bs.RemoveCache(id)
}
for _, v := range ctx.callState {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the above removal?

Copy link
Member

@kroggen kroggen Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to mean: if there was not a contract on that address and now there is one, remove the contract code from cache (revert the deploy)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it`s my miss. i will recover that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is recovered using isDeploy flag.

contract/vm_callback.go Outdated Show resolved Hide resolved
contract/vm_callback.go Outdated Show resolved Hide resolved
contract/vm_callback.go Outdated Show resolved Hide resolved
if err != nil {
return newDbSystemError(err)
}
}
/* For Sender */
if v.prevState == nil {
continue
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it OK to remove this check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is ignore putState if state not changed.

I don`t understand why vm.go have their own prevState and curState, so I remove it.

if it needed to ignore PutState about not updated state, it can be added in state.PutState()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prevState exists for 2 cases: the one above (to avoid unnecessary disk I/O) and the other about revert deploy (remove code from cache)

I am not sure if it is good to remove it

If removed, it must be replaced by equivalent logic

Copy link
Member Author

@rabbitprincess rabbitprincess Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. it need to replaced by equivalent logic. it also need to change.

Copy link
Member Author

@rabbitprincess rabbitprincess Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it is good that all AccountState should be exist in state.blockState, and vm can get/put accountState only using blockState.

  1. vm commit ( per transaction ) -> change accountState in BlockState
  2. blockState commit ( per block ) -> change state in db

Copy link
Member Author

@rabbitprincess rabbitprincess Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is recovered using isCallback flag.

contract/vm.go Show resolved Hide resolved
@@ -68,35 +53,39 @@ func (cs *ContractState) GetAccountID() types.AccountID {
return cs.account
}

func (cs *ContractState) GetID() []byte {
return cs.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this function is named GetID and on AccountState it is just ID?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because other function in ContractState is GetXXX, It is not easy to remove Get from all other GetXXX functions in contractState.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it needed, AccountState functions can be added Get prefix too.

it need to handle error message (C.Cstring)
check isDeploy and isSend
put account state only for callback
@kroggen
Copy link
Member

kroggen commented Dec 7, 2023

OK, a sync test can be run to check its backwards compatibility

@kslee8282
Copy link
Member

sync start
[mainnet]
Dec 7 05:02:14.485 INF ../go/aergo/cmd/aergosvr/aergosvr.go:98 > AERGO SVR STARTED branch=feature/refactor-state module=asvr revision=f7b6e39f

contract/system/vote.go Outdated Show resolved Hide resolved
@kslee8282
Copy link
Member

sync success

@kslee8282 kslee8282 added the pass sync test The PR can be merged label Dec 15, 2023
@rabbitprincess
Copy link
Member Author

@kroggen can you approve this pr?

not affect for sync test ( not used function )
@kroggen
Copy link
Member

kroggen commented Dec 15, 2023

I will check for conflicts, tomorrow

contract/vm_state.go Outdated Show resolved Hide resolved
contract/vm_state.go Outdated Show resolved Hide resolved
contract/vm.go Outdated Show resolved Hide resolved
contract/vm.go Outdated
func (ce *executor) vmLoadCall() {
if cErrMsg := C.vm_loadcall(
ce.L,
); cErrMsg != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this case the argument can be on the same line as the function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not separate the call from the if clause? The same for the function above

Copy link
Member Author

@rabbitprincess rabbitprincess Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not separate the call from the if clause? The same for the function above

it can limiting error variable ( cErrMsg ) within if scope. ( and also shorten line. )

contract/vm_callback.go Outdated Show resolved Hide resolved
Copy link
Member

@kroggen kroggen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears OK

@rabbitprincess rabbitprincess merged commit 150a454 into develop Dec 18, 2023
3 checks passed
@kroggen
Copy link
Member

kroggen commented Dec 18, 2023

@gokch

Why AccountState is on state and ContractState is on statedb?

I am also curious why many functions were moved from state to statedb

@kroggen
Copy link
Member

kroggen commented Dec 18, 2023

The separation of vmAutoload, vmLoadCode and vmLoadCall was completely unnecessary.
They are related and were together at the end of the file.
I only now saw this

@rabbitprincess
Copy link
Member Author

@gokch

Why AccountState is on state and ContractState is on statedb?

I am also curious why many functions were moved from state to statedb

contractState is only modified by lua contract, but accountState is modified by lua contract and external chain.
I tried to collect only changed state by lua into statedb. ( but, statedb is not clear package name. )

and, Although it is not confirmed, this also considering evm support. evm and lua have their own state db, but they share same accountState.

@rabbitprincess
Copy link
Member Author

The separation of vmAutoload, vmLoadCode and vmLoadCall was completely unnecessary. They are related and were together at the end of the file. I only now saw this

yes, and it is also good to separate vmContext and executor. ( make new file vm_executor.go )
Current pr`s purpose is for refactor state, so it seems better to change it in another pr.

@rabbitprincess rabbitprincess deleted the feature/refactor-state branch December 27, 2023 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
check hardfork Run sync test with the PR pass sync test The PR can be merged refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants