Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
hayarobi committed Jan 23, 2024
2 parents 7be903b + ddd65ff commit db13a0d
Show file tree
Hide file tree
Showing 196 changed files with 7,958 additions and 11,694 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/full_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.19'

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/manual_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.19'

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/short_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.19'

- name: Install dependencies
run: |
Expand Down
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 := as.sdb.GetStateDB().GetNameAccountState()
scs, err := statedb.GetNameAccountState(as.sdb.GetStateDB())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion chain/chaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func (cdb *ChainDB) getReceipts(blockHash []byte, blockNo types.BlockNo,
hardForkConfig *config.HardforkConfig) (*types.Receipts, error) {
data := cdb.store.Get(dbkey.Receipts(blockHash, blockNo))
if len(data) == 0 {
return nil, errors.New("cannot find a receipt")
return nil, fmt.Errorf("empty : blockNo=%d", blockNo)
}
var receipts types.Receipts

Expand Down
116 changes: 86 additions & 30 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 @@ -136,6 +137,72 @@ func (cs *ChainService) getReceipt(txHash []byte) (*types.Receipt, error) {
return r, nil
}

func (cs *ChainService) getReceipts(blockHash []byte) (*types.Receipts, error) {
block, err := cs.cdb.getBlock(blockHash)
if err != nil {
return nil, &ErrNoBlock{blockHash}
}

blockInMainChain, err := cs.cdb.GetBlockByNo(block.Header.BlockNo)
if !bytes.Equal(block.BlockHash(), blockInMainChain.BlockHash()) {
return nil, errors.New("cannot find a receipt")
}

receipts, err := cs.cdb.getReceipts(block.BlockHash(), block.GetHeader().BlockNo, cs.cfg.Hardfork)
if err != nil {
return nil, err
}

for idx, r := range receipts.Get() {
r.SetMemoryInfo(blockHash, block.Header.BlockNo, int32(idx))

r.ContractAddress = types.AddressOrigin(r.ContractAddress)

for _, tx := range block.GetBody().GetTxs() {
if bytes.Equal(r.GetTxHash(), tx.GetHash()) {
r.From = tx.GetBody().GetAccount()
r.To = tx.GetBody().GetRecipient()
break
}
}
}

return receipts, nil
}

func (cs *ChainService) getReceiptsByNo(blockNo types.BlockNo) (*types.Receipts, error) {
blockInMainChain, err := cs.cdb.GetBlockByNo(blockNo)
if err != nil {
return nil, &ErrNoBlock{blockNo}
}

block, err := cs.cdb.getBlock(blockInMainChain.BlockHash())
if !bytes.Equal(block.BlockHash(), blockInMainChain.BlockHash()) {
return nil, errors.New("cannot find a receipt")
}

receipts, err := cs.cdb.getReceipts(block.BlockHash(), block.GetHeader().BlockNo, cs.cfg.Hardfork)
if err != nil {
return nil, err
}

for idx, r := range receipts.Get() {
r.SetMemoryInfo(blockInMainChain.BlockHash(), blockNo, int32(idx))

r.ContractAddress = types.AddressOrigin(r.ContractAddress)

for _, tx := range block.GetBody().GetTxs() {
if bytes.Equal(r.GetTxHash(), tx.GetHash()) {
r.From = tx.GetBody().GetAccount()
r.To = tx.GetBody().GetRecipient()
break
}
}
}

return receipts, nil
}

func (cs *ChainService) getEvents(events *[]*types.Event, blkNo types.BlockNo, filter *types.FilterInfo,
argFilter []types.ArgFilter) uint64 {
blkHash, err := cs.cdb.getHashByNo(blkNo)
Expand Down Expand Up @@ -624,7 +691,7 @@ func newBlockExecutor(cs *ChainService, bState *state.BlockState, block *types.B
}

// NewTxExecutor returns a new TxExecFn.
func NewTxExecutor(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb contract.ChainAccessor, bi *types.BlockHeaderInfo, preloadService int) TxExecFn {
func NewTxExecutor(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb contract.ChainAccessor, bi *types.BlockHeaderInfo, executionMode int) TxExecFn {
return func(bState *state.BlockState, tx types.Transaction) error {
if bState == nil {
logger.Error().Msg("bstate is nil in txExec")
Expand All @@ -636,7 +703,7 @@ func NewTxExecutor(execCtx context.Context, ccc consensus.ChainConsensusCluster,
}
blockSnap := bState.Snapshot()

err := executeTx(execCtx, ccc, cdb, bState, tx, bi, preloadService)
err := executeTx(execCtx, ccc, cdb, bState, tx, bi, executionMode)
if err != nil {
logger.Error().Err(err).Str("hash", base58.Encode(tx.GetHash())).Msg("tx failed")
if err2 := bState.Rollback(blockSnap); err2 != nil {
Expand All @@ -653,22 +720,14 @@ func (e *blockExecutor) execute() error {
// Receipt must be committed unconditionally.
if !e.commitOnly {
defer contract.CloseDatabase()
var preloadTx *types.Tx
numTxs := len(e.txs)
for i, tx := range e.txs {
// if tx is not the last one, preload the next tx
if i != numTxs-1 {
preloadTx = e.txs[i+1]
contract.RequestPreload(e.BlockState, e.bi, preloadTx, tx, contract.ChainService)
}
logger.Trace().Int("txCount", len(e.txs)).Msg("executing txs")
for _, tx := range e.txs {
// execute the transaction
if err := e.execTx(e.BlockState, types.NewTransaction(tx)); err != nil {
//FIXME maybe system error. restart or panic
// all txs have executed successfully in BP node
return err
}
// mark the next preload tx to be executed
contract.SetPreloadTx(preloadTx, contract.ChainService)
}

if e.validateSignWait != nil {
Expand Down Expand Up @@ -870,7 +929,7 @@ func adjustRv(ret string) string {
return ret
}

func resetAccount(account *state.V, fee *big.Int, nonce *uint64) error {
func resetAccount(account *state.AccountState, fee *big.Int, nonce *uint64) error {
account.Reset()
if fee != nil {
if account.Balance().Cmp(fee) < 0 {
Expand All @@ -884,7 +943,7 @@ func resetAccount(account *state.V, fee *big.Int, nonce *uint64) error {
return account.PutState()
}

func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb contract.ChainAccessor, bs *state.BlockState, tx types.Transaction, bi *types.BlockHeaderInfo, preloadService int) error {
func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb contract.ChainAccessor, bs *state.BlockState, tx types.Transaction, bi *types.BlockHeaderInfo, executionMode int) error {
var (
txBody = tx.GetBody()
isQuirkTx = types.IsQuirkTx(tx.GetHash())
Expand All @@ -910,7 +969,7 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
return err
}

sender, err := bs.GetAccountStateV(account)
sender, err := state.GetAccountState(account, bs.StateDB)
if err != nil {
return err
}
Expand All @@ -923,16 +982,16 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
if recipient, err = name.Resolve(bs, txBody.Recipient, isQuirkTx); err != nil {
return err
}
var receiver *state.V
var receiver *state.AccountState
status := "SUCCESS"
if len(recipient) > 0 {
receiver, err = bs.GetAccountStateV(recipient)
receiver, err = state.GetAccountState(recipient, bs.StateDB)
if receiver != nil && txBody.Type == types.TxType_REDEPLOY {
status = "RECREATED"
receiver.SetRedeploy()
}
} else {
receiver, err = bs.CreateAccountStateV(contract.CreateContractID(txBody.Account, txBody.Nonce))
receiver, err = state.CreateAccountState(contract.CreateContractID(txBody.Account, txBody.Nonce), bs.StateDB)
status = "CREATED"
}
if err != nil {
Expand All @@ -944,7 +1003,7 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
var events []*types.Event
switch txBody.Type {
case types.TxType_NORMAL, types.TxType_REDEPLOY, types.TxType_TRANSFER, types.TxType_CALL, types.TxType_DEPLOY:
rv, events, txFee, err = contract.Execute(execCtx, bs, cdb, tx.GetTx(), sender, receiver, bi, preloadService, false)
rv, events, txFee, err = contract.Execute(execCtx, bs, cdb, tx.GetTx(), sender, receiver, bi, executionMode, false)
sender.SubBalance(txFee)
case types.TxType_GOVERNANCE:
txFee = new(big.Int).SetUint64(0)
Expand All @@ -958,8 +1017,8 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
return err
}

var contractState *state.ContractState
contractState, err = bs.OpenContractState(receiver.AccountID(), receiver.State())
var contractState *statedb.ContractState
contractState, err = statedb.OpenContractState(receiver.ID(), receiver.State(), bs.StateDB)
if err != nil {
return err
}
Expand All @@ -972,7 +1031,7 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
}
return types.ErrNotAllowedFeeDelegation
}
rv, events, txFee, err = contract.Execute(execCtx, bs, cdb, tx.GetTx(), sender, receiver, bi, preloadService, true)
rv, events, txFee, err = contract.Execute(execCtx, bs, cdb, tx.GetTx(), sender, receiver, bi, executionMode, true)
receiver.SubBalance(txFee)
}

Expand Down Expand Up @@ -1055,22 +1114,19 @@ func sendRewardCoinbase(bState *state.BlockState, coinbaseAccount []byte) error
return nil
}

receiverID := types.ToAccountID(coinbaseAccount)
receiverState, err := bState.GetAccountState(receiverID)
// add bp reward to coinbase account
coinbaseAccountState, err := state.GetAccountState(coinbaseAccount, bState.StateDB)
if err != nil {
return err
}

receiverChange := types.State(*receiverState)
receiverChange.Balance = new(big.Int).Add(receiverChange.GetBalanceBigInt(), bpReward).Bytes()

err = bState.PutState(receiverID, &receiverChange)
coinbaseAccountState.AddBalance(bpReward)
err = coinbaseAccountState.PutState()
if err != nil {
return err
}

logger.Debug().Str("reward", bpReward.String()).
Str("newbalance", receiverChange.GetBalanceBigInt().String()).Msg("send reward to coinbase account")
Str("newbalance", coinbaseAccountState.Balance().String()).Msg("send reward to coinbase account")

return nil
}
Expand Down
Loading

0 comments on commit db13a0d

Please sign in to comment.