Skip to content

Commit

Permalink
state - move contract in statedb
Browse files Browse the repository at this point in the history
change statedb variables to private
  • Loading branch information
rabbitprincess committed Dec 4, 2023
1 parent 9054f96 commit a861612
Show file tree
Hide file tree
Showing 46 changed files with 428 additions and 429 deletions.
3 changes: 2 additions & 1 deletion account/accountservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aergoio/aergo/v2/contract/name"
"github.com/aergoio/aergo/v2/pkg/component"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/message"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ func (as *AccountService) Statistics() *map[string]interface{} {
}
}
func (as *AccountService) resolveName(namedAddress []byte) ([]byte, error) {
scs, err := state.GetNameAccountState(as.sdb.GetStateDB())
scs, err := statedb.GetNameAccountState(as.sdb.GetStateDB())
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions chain/chainhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/aergoio/aergo/v2/internal/enc/base58"
"github.com/aergoio/aergo/v2/internal/enc/proto"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/message"
)
Expand Down Expand Up @@ -958,8 +959,8 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
return err
}

var contractState *state.ContractState
contractState, err = state.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
var contractState *statedb.ContractState
contractState, err = statedb.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
if err != nil {
return err
}
Expand Down
24 changes: 12 additions & 12 deletions chain/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func (cs *ChainService) getVotes(id string, n uint32) (*types.VoteList, error) {
switch ConsensusName() {
case consensus.ConsensusName[consensus.ConsensusDPOS]:
sdb := cs.sdb.OpenNewStateDB(cs.sdb.GetRoot())
scs, err := state.GetSystemAccountState(sdb)
scs, err := statedb.GetSystemAccountState(sdb)
if err != nil {
return nil, err
}
Expand All @@ -518,11 +518,11 @@ func (cs *ChainService) getAccountVote(addr []byte) (*types.AccountVoteInfo, err
}

sdb := cs.sdb.OpenNewStateDB(cs.sdb.GetRoot())
scs, err := state.GetSystemAccountState(sdb)
scs, err := statedb.GetSystemAccountState(sdb)
if err != nil {
return nil, err
}
namescs, err := state.GetNameAccountState(sdb)
namescs, err := statedb.GetNameAccountState(sdb)
if err != nil {
return nil, err
}
Expand All @@ -540,11 +540,11 @@ func (cs *ChainService) getStaking(addr []byte) (*types.Staking, error) {
}

sdb := cs.sdb.OpenNewStateDB(cs.sdb.GetRoot())
scs, err := state.GetSystemAccountState(sdb)
scs, err := statedb.GetSystemAccountState(sdb)
if err != nil {
return nil, err
}
namescs, err := state.GetNameAccountState(sdb)
namescs, err := statedb.GetNameAccountState(sdb)
if err != nil {
return nil, err
}
Expand All @@ -567,7 +567,7 @@ func (cs *ChainService) getNameInfo(qname string, blockNo types.BlockNo) (*types
stateDB = cs.sdb.OpenNewStateDB(cs.sdb.GetRoot())
}

ncs, err := state.GetNameAccountState(stateDB)
ncs, err := statedb.GetNameAccountState(stateDB)
if err != nil {
return nil, err
}
Expand All @@ -576,7 +576,7 @@ func (cs *ChainService) getNameInfo(qname string, blockNo types.BlockNo) (*types

func (cs *ChainService) getEnterpriseConf(key string) (*types.EnterpriseConfig, error) {
sdb := cs.sdb.OpenNewStateDB(cs.sdb.GetRoot())
ecs, err := state.GetEnterpriseAccountState(sdb)
ecs, err := statedb.GetEnterpriseAccountState(sdb)
if err != nil {
return nil, err
}
Expand All @@ -589,7 +589,7 @@ func (cs *ChainService) getEnterpriseConf(key string) (*types.EnterpriseConfig,
func (cs *ChainService) getSystemValue(key types.SystemValue) (*big.Int, error) {
switch key {
case types.StakingTotal:
scs, err := state.GetSystemAccountState(cs.sdb.GetStateDB())
scs, err := statedb.GetSystemAccountState(cs.sdb.GetStateDB())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -701,7 +701,7 @@ func (cm *ChainManager) Receive(context actor.Context) {

func getAddressNameResolved(sdb *statedb.StateDB, account []byte) ([]byte, error) {
if len(account) == types.NameLength {
scs, err := state.GetNameAccountState(sdb)
scs, err := statedb.GetNameAccountState(sdb)
if err != nil {
logger.Error().Str("hash", base58.Encode(account)).Err(err).Msg("failed to get state for account")
return nil, err
Expand Down Expand Up @@ -800,7 +800,7 @@ func (cw *ChainWorker) Receive(context actor.Context) {
})
break
}
contractState, err := state.OpenContractStateAccount(address, sdb)
contractState, err := statedb.OpenContractStateAccount(address, sdb)
if err == nil {
abi, err := contract.GetABI(contractState, nil)
context.Respond(message.GetABIRsp{
Expand All @@ -822,7 +822,7 @@ func (cw *ChainWorker) Receive(context actor.Context) {
context.Respond(message.GetQueryRsp{Result: nil, Err: err})
break
}
ctrState, err := state.OpenContractStateAccount(address, sdb)
ctrState, err := statedb.OpenContractStateAccount(address, sdb)
if err != nil {
logger.Error().Str("hash", base58.Encode(address)).Err(err).Msg("failed to get state for contract")
context.Respond(message.GetQueryRsp{Result: nil, Err: err})
Expand Down Expand Up @@ -910,7 +910,7 @@ func (cw *ChainWorker) Receive(context actor.Context) {
defer runtime.UnlockOSThread()

sdb = cw.sdb.OpenNewStateDB(cw.sdb.GetRoot())
ctrState, err := state.OpenContractStateAccount(msg.Contract, sdb)
ctrState, err := statedb.OpenContractStateAccount(msg.Contract, sdb)
if err != nil {
logger.Error().Str("hash", base58.Encode(msg.Contract)).Err(err).Msg("failed to get state for contract")
context.Respond(message.CheckFeeDelegationRsp{Err: err})
Expand Down
8 changes: 4 additions & 4 deletions chain/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func executeGovernanceTx(ccc consensus.ChainConsensusCluster, bs *state.BlockSta
}

governance := string(txBody.Recipient)
scs, err := state.OpenContractState(receiver.IDNoPadding(), receiver.State(), bs.StateDB)
scs, err := statedb.OpenContractState(receiver.IDNoPadding(), receiver.State(), bs.StateDB)
if err != nil {
return nil, err
}
Expand All @@ -47,7 +47,7 @@ func executeGovernanceTx(ccc consensus.ChainConsensusCluster, bs *state.BlockSta
err = types.ErrTxInvalidRecipient
}
if err == nil {
err = state.StageContractState(scs, bs.StateDB)
err = statedb.StageContractState(scs, bs.StateDB)
}

return events, err
Expand All @@ -56,7 +56,7 @@ func executeGovernanceTx(ccc consensus.ChainConsensusCluster, bs *state.BlockSta
// InitGenesisBPs opens system contract and put initial voting result
// it also set *State in Genesis to use statedb
func InitGenesisBPs(states *statedb.StateDB, genesis *types.Genesis) error {
scs, err := state.GetSystemAccountState(states)
scs, err := statedb.GetSystemAccountState(states)
if err != nil {
return err
}
Expand All @@ -72,7 +72,7 @@ func InitGenesisBPs(states *statedb.StateDB, genesis *types.Genesis) error {
// Set genesis.BPs to the votes-ordered BPs. This will be used later for
// bootstrapping.
genesis.BPs = system.BuildOrderedCandidates(voteResult)
if err = state.StageContractState(scs, states); err != nil {
if err = statedb.StageContractState(scs, states); err != nil {
return err
}
if err = states.Update(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion chain/signVerifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aergoio/aergo/v2/internal/enc/base58"
"github.com/aergoio/aergo/v2/pkg/component"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/message"
)
Expand Down Expand Up @@ -132,7 +133,7 @@ func (sv *SignVerifier) verifyTx(comm component.IComponentRequester, tx *types.T
}

if tx.NeedNameVerify() {
cs, err := state.GetNameAccountState(sv.sdb.GetStateDB())
cs, err := statedb.GetNameAccountState(sv.sdb.GetStateDB())
if err != nil {
logger.Error().Err(err).Msg("failed to get verify because of opening contract error")
return false, err
Expand Down
3 changes: 2 additions & 1 deletion consensus/impl/dpos/blockfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package dpos

import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestBlockFactory_context(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions consensus/impl/dpos/bp/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/aergoio/aergo/v2/contract/system"
"github.com/aergoio/aergo/v2/internal/enc/gob"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/davecgh/go-spew/spew"
)
Expand Down Expand Up @@ -381,7 +382,7 @@ func (sn *Snapshots) AddSnapshot(refBlockNo types.BlockNo) ([]string, error) {
}

func (sn *Snapshots) gatherRankers() ([]string, error) {
scs, err := state.GetSystemAccountState(sn.sdb.GetStateDB())
scs, err := statedb.GetSystemAccountState(sn.sdb.GetStateDB())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -500,7 +501,7 @@ func (sn *Snapshots) loadClusterSnapshot(blockNo types.BlockNo) ([]string, error
}

stateDB := sn.sdb.OpenNewStateDB(block.GetHeader().GetBlocksRootHash())
scs, err := state.GetSystemAccountState(stateDB)
scs, err := statedb.GetSystemAccountState(stateDB)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/impl/dpos/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func sendVotingReward(bState *state.BlockState, dummy []byte) error {
}

func InitVPR(sdb *statedb.StateDB) error {
s, err := state.GetSystemAccountState(sdb)
s, err := statedb.GetSystemAccountState(sdb)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/aergoio/aergo/v2/fee"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
"github.com/minio/sha256-simd"
Expand Down Expand Up @@ -106,7 +107,7 @@ func Execute(execCtx context.Context, bs *state.BlockState, cdb ChainAccessor, t
}

// open the contract state
contractState, err := state.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
contractState, err := statedb.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
if err != nil {
return
}
Expand Down Expand Up @@ -194,7 +195,7 @@ func Execute(execCtx context.Context, bs *state.BlockState, cdb ChainAccessor, t
}

// save the contract state
err = state.StageContractState(contractState, bs.StateDB)
err = statedb.StageContractState(contractState, bs.StateDB)
if err != nil {
return "", events, usedFee, err
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func preloadWorker() {
}

// open the contract state
contractState, err := state.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
contractState, err := statedb.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
if err != nil {
replyCh <- &preloadReply{tx, nil, err}
continue
Expand Down Expand Up @@ -320,7 +321,7 @@ func CreateContractID(account []byte, nonce uint64) []byte {
return append([]byte{0x0C}, recipientHash...) // prepend 0x0C to make it same length as account addresses
}

func checkRedeploy(sender, receiver *state.AccountState, contractState *state.ContractState) error {
func checkRedeploy(sender, receiver *state.AccountState, contractState *statedb.ContractState) error {
// check if the contract exists
if !receiver.IsContract() || receiver.IsNew() {
receiverAddr := types.EncodeAddress(receiver.ID())
Expand Down
8 changes: 4 additions & 4 deletions contract/enterprise/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package enterprise
import (
"bytes"

"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
)

func GetAdmin(ecs *state.ContractState) (*types.EnterpriseConfig, error) {
func GetAdmin(ecs *statedb.ContractState) (*types.EnterpriseConfig, error) {
admins, err := getAdmins(ecs)
if err != nil {
return nil, err
Expand All @@ -22,11 +22,11 @@ func GetAdmin(ecs *state.ContractState) (*types.EnterpriseConfig, error) {
}
return ret, nil
}
func setAdmins(scs *state.ContractState, addresses [][]byte) error {
func setAdmins(scs *statedb.ContractState, addresses [][]byte) error {
return scs.SetData(dbkey.EnterpriseAdmins(), bytes.Join(addresses, []byte("")))
}

func getAdmins(scs *state.ContractState) ([][]byte, error) {
func getAdmins(scs *statedb.ContractState) ([][]byte, error) {
data, err := scs.GetData(dbkey.EnterpriseAdmins())
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions contract/enterprise/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
)
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *Conf) Validate(key []byte, context *EnterpriseContext) error {
}
}

func GetConf(ecs *state.ContractState, key string) (*types.EnterpriseConfig, error) {
func GetConf(ecs *statedb.ContractState, key string) (*types.EnterpriseConfig, error) {
ret := &types.EnterpriseConfig{Key: key}
if strings.ToUpper(key) == "PERMISSIONS" {
for k := range enterpriseKeyDict {
Expand All @@ -89,7 +89,7 @@ func GetConf(ecs *state.ContractState, key string) (*types.EnterpriseConfig, err
return ret, nil
}

func enableConf(scs *state.ContractState, key []byte, value bool) (*Conf, error) {
func enableConf(scs *statedb.ContractState, key []byte, value bool) (*Conf, error) {
conf, err := getConf(scs, key)
if err != nil {
return nil, err
Expand All @@ -102,15 +102,15 @@ func enableConf(scs *state.ContractState, key []byte, value bool) (*Conf, error)
return conf, nil
}

func getConf(scs *state.ContractState, key []byte) (*Conf, error) {
func getConf(scs *statedb.ContractState, key []byte) (*Conf, error) {
data, err := scs.GetData(dbkey.EnterpriseConf(key))
if err != nil || data == nil {
return nil, err
}
return deserializeConf(data), err
}

func setConfValues(scs *state.ContractState, key []byte, values []string) (*Conf, error) {
func setConfValues(scs *statedb.ContractState, key []byte, values []string) (*Conf, error) {
conf, err := getConf(scs, key)
if err != nil {
return nil, err
Expand All @@ -123,7 +123,7 @@ func setConfValues(scs *state.ContractState, key []byte, values []string) (*Conf
return conf, nil
}

func setConf(scs *state.ContractState, key []byte, conf *Conf) error {
func setConf(scs *statedb.ContractState, key []byte, conf *Conf) error {
return scs.SetData(dbkey.EnterpriseConf(key), serializeConf(conf))
}

Expand Down
4 changes: 2 additions & 2 deletions contract/enterprise/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var cdb *state.ChainStateDB
var sdb *statedb.StateDB

func initTest(t *testing.T) (*state.ContractState, *state.AccountState, *state.AccountState) {
func initTest(t *testing.T) (*statedb.ContractState, *state.AccountState, *state.AccountState) {
cdb = state.NewChainStateDB()
cdb.Init(string(db.BadgerImpl), "test", nil, false)
genesis := types.GetTestGenesis()
Expand All @@ -25,7 +25,7 @@ func initTest(t *testing.T) (*state.ContractState, *state.AccountState, *state.A
}
const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"

scs, err := state.GetSystemAccountState(cdb.GetStateDB())
scs, err := statedb.GetSystemAccountState(cdb.GetStateDB())
assert.NoError(t, err, "could not open contract state")

account, err := types.DecodeAddress(testSender)
Expand Down
3 changes: 2 additions & 1 deletion contract/enterprise/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/v2/consensus"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func (e *EnterpriseContext) HasConfValue(value string) bool {
return false
}

func ExecuteEnterpriseTx(bs *state.BlockState, ccc consensus.ChainConsensusCluster, scs *state.ContractState, txBody *types.TxBody,
func ExecuteEnterpriseTx(bs *state.BlockState, ccc consensus.ChainConsensusCluster, scs *statedb.ContractState, txBody *types.TxBody,
sender, receiver *state.AccountState, blockNo types.BlockNo) ([]*types.Event, error) {

context, err := ValidateEnterpriseTx(txBody, sender, scs, blockNo)
Expand Down
Loading

0 comments on commit a861612

Please sign in to comment.