Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into topic/update-go-de…
Browse files Browse the repository at this point in the history
…pendency
  • Loading branch information
rabbitprincess committed Oct 24, 2023
2 parents 42b0b09 + 47d4b32 commit 8be3d17
Show file tree
Hide file tree
Showing 48 changed files with 686 additions and 207 deletions.
19 changes: 11 additions & 8 deletions .github/workflows/full_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ jobs:
if: github.event_name != 'push' || github.ref_name != 'master' || github.ref_type != 'branch'
run: go test -timeout 999s -v ./...

- name: Integration Tests
run: |
if [ -d "tests" ]; then
cd tests
./run_tests.sh
else
echo "The 'tests' folder does not exist."
fi
- name: Integration Tests - brick
run: cd tests && ./run_tests.sh brick

- name: Integration Tests - sbp
run: cd tests && ./run_tests.sh sbp

- name: Integration Tests - dpos
run: cd tests && ./run_tests.sh dpos

- name: Integration Tests - raft
run: cd tests && ./run_tests.sh raft
2 changes: 1 addition & 1 deletion chain/chainhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func newBlockExecutor(cs *ChainService, bState *state.BlockState, block *types.B
// executed by the block factory.
commitOnly = true
}
bState.SetGasPrice(system.GetGasPriceFromState(bState))
bState.SetGasPrice(system.GetGasPrice())
bState.Receipts().SetHardFork(cs.cfg.Hardfork, block.BlockNo())

return &blockExecutor{
Expand Down
2 changes: 0 additions & 2 deletions chain/chainhandle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/v2/account/key"
"github.com/aergoio/aergo/v2/contract"
"github.com/aergoio/aergo/v2/contract/system"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/types"
Expand All @@ -37,7 +36,6 @@ func initTest(t *testing.T, testmode bool) {
t.Fatalf("failed init : %s", err.Error())
}
types.InitGovernance("dpos", true)
system.InitGovernance("dpos")

}

Expand Down
1 change: 0 additions & 1 deletion chain/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ func NewChainService(cfg *cfg.Config) *ChainService {

// For a strict governance transaction validation.
types.InitGovernance(cs.ConsensusType(), cs.IsPublic())
system.InitGovernance(cs.ConsensusType())

//reset parameter of aergo.system
systemState, err := cs.SDB().GetSystemAccountState()
Expand Down
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type Consensus interface {
ChainConsensus
ConsensusAccessor
Ticker() *time.Ticker
// QueueJob queues block generation job.
// It waits until next block generation time is reached in raft consensus and sbp.
QueueJob(now time.Time, jq chan<- interface{})
BlockFactory() BlockFactory
QuitChan() chan interface{}
Expand Down
2 changes: 1 addition & 1 deletion consensus/impl/dpos/blockfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (bf *BlockFactory) generateBlock(execCtx context.Context, bpi *bpInfo, lpbN
bpi.bestBlock.GetHeader().GetBlocksRootHash(),
state.SetPrevBlockHash(bpi.bestBlock.BlockHash()),
)
bs.SetGasPrice(system.GetGasPriceFromState(bs))
bs.SetGasPrice(system.GetGasPrice())
bs.Receipts().SetHardFork(bf.bv, bi.No)

bGen := chain.NewBlockGenerator(
Expand Down
10 changes: 10 additions & 0 deletions consensus/impl/dpos/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/aergoio/aergo/v2/consensus"
"github.com/aergoio/aergo/v2/consensus/impl/dpos/bp"
"github.com/aergoio/aergo/v2/contract/system"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/types"
)
Expand Down Expand Up @@ -83,6 +84,10 @@ func (s *Status) Update(block *types.Block) {
}

bps, _ = s.bps.AddSnapshot(block.BlockNo())

// if a system param was changed, apply its new value
system.CommitParams(true)

} else {
// Rollback resulting from a reorganization: The code below assumes
// that there is no block-by-block rollback; it assumes that the
Expand All @@ -109,6 +114,11 @@ func (s *Status) Update(block *types.Block) {
} else {
logger.Debug().Uint64("from block no", block.BlockNo()).Msg("VPR reloaded")
}

// if a system param was changed, discard its new value
// this is mainly for block revert case
// the params are reloaded from db on block reorganization
system.CommitParams(false)
}

s.libState.gc(bps)
Expand Down
5 changes: 3 additions & 2 deletions consensus/impl/raftv2/blockfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (bf *BlockFactory) Ticker() *time.Ticker {
return time.NewTicker(BlockFactoryTickMs)
}

// QueueJob send a block triggering information to jq.
// QueueJob send a block triggering information to jq, and hold to wait
func (bf *BlockFactory) QueueJob(now time.Time, jq chan<- interface{}) {
bf.jobLock.Lock()
defer bf.jobLock.Unlock()
Expand Down Expand Up @@ -279,6 +279,7 @@ func (bf *BlockFactory) QueueJob(now time.Time, jq chan<- interface{}) {

logger.Debug().Str("work", work.ToString()).Str("prev", prevToString(prev)).Msg("new work generated")
jq <- work
time.Sleep(BlockIntervalMs)
}
}

Expand Down Expand Up @@ -522,7 +523,7 @@ func (bf *BlockFactory) generateBlock(work *Work) (*types.Block, *state.BlockSta
bestBlock.GetHeader().GetBlocksRootHash(),
state.SetPrevBlockHash(bestBlock.BlockHash()),
)
blockState.SetGasPrice(system.GetGasPriceFromState(blockState))
blockState.SetGasPrice(system.GetGasPrice())
blockState.Receipts().SetHardFork(bf.bv, bi.No)

block, err := chain.NewBlockGenerator(bf, work.execCtx, bi, blockState, txOp, RaftSkipEmptyBlock).GenerateBlock()
Expand Down
5 changes: 3 additions & 2 deletions consensus/impl/sbp/sbp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (te *txExec) Apply(bState *state.BlockState, tx types.Transaction) error {
return err
}

// SimpleBlockFactory implments a simple block factory which generate block each cfg.Consensus.BlockInterval.
// SimpleBlockFactory implements a simple block factory which generate block each cfg.Consensus.BlockInterval.
//
// This can be used for testing purpose.
type SimpleBlockFactory struct {
Expand Down Expand Up @@ -118,6 +118,7 @@ func (s *SimpleBlockFactory) QueueJob(now time.Time, jq chan<- interface{}) {
}
s.prevBlock = b
jq <- b
time.Sleep(s.blockInterval)
}
}

Expand Down Expand Up @@ -189,7 +190,7 @@ func (s *SimpleBlockFactory) Start() {
prevBlock.GetHeader().GetBlocksRootHash(),
state.SetPrevBlockHash(prevBlock.BlockHash()),
)
blockState.SetGasPrice(system.GetGasPriceFromState(blockState))
blockState.SetGasPrice(system.GetGasPrice())
blockState.Receipts().SetHardFork(s.bv, bi.No)
txOp := chain.NewCompTxOp(s.txOp, newTxExec(s.ChainDB, bi))

Expand Down
61 changes: 25 additions & 36 deletions contract/name/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,37 @@ import (
func ExecuteNameTx(bs *state.BlockState, scs *state.ContractState, txBody *types.TxBody,
sender, receiver *state.V, blockInfo *types.BlockHeaderInfo) ([]*types.Event, error) {

systemContractState, err := bs.StateDB.GetSystemAccountState()

ci, err := ValidateNameTx(txBody, sender, scs, systemContractState)
ci, err := ValidateNameTx(txBody, sender, scs)
if err != nil {
return nil, err
}

var events []*types.Event

var nameState *state.V
owner := getOwner(scs, []byte(types.AergoName), false)
if owner != nil {
if bytes.Equal(sender.ID(), owner) {
nameState = sender
} else {
nameState, err = bs.GetAccountStateV(owner)
if err != nil {
if nameState, err = bs.GetAccountStateV(owner); err != nil {
return nil, err
}
}
} else {
nameState = receiver
}

var events []*types.Event
switch ci.Name {
case types.NameCreate:
if err = CreateName(scs, txBody, sender, nameState,
ci.Args[0].(string)); err != nil {
nameArg := ci.Args[0].(string)
if err = CreateName(scs, txBody, sender, nameState, nameArg); err != nil {
return nil, err
}
jsonArgs := ""
if blockInfo.ForkVersion < 2 {
jsonArgs = `{"name":"` + ci.Args[0].(string) + `"}`
jsonArgs = `{"name":"` + nameArg + `"}`
} else {
jsonArgs = `["` + ci.Args[0].(string) + `"]`
jsonArgs = `["` + nameArg + `"]`
}
events = append(events, &types.Event{
ContractAddress: receiver.ID(),
Expand All @@ -57,16 +53,16 @@ func ExecuteNameTx(bs *state.BlockState, scs *state.ContractState, txBody *types
JsonArgs: jsonArgs,
})
case types.NameUpdate:
if err = UpdateName(bs, scs, txBody, sender, nameState,
ci.Args[0].(string), ci.Args[1].(string)); err != nil {
nameArg := ci.Args[0].(string)
toArg := ci.Args[1].(string)
if err = UpdateName(bs, scs, txBody, sender, nameState, nameArg, toArg); err != nil {
return nil, err
}
jsonArgs := ""
if blockInfo.ForkVersion < 2 {
jsonArgs = `{"name":"` + ci.Args[0].(string) +
`","to":"` + ci.Args[1].(string) + `"}`
jsonArgs = `{"name":"` + nameArg + `","to":"` + toArg + `"}`
} else {
jsonArgs = `["` + ci.Args[0].(string) + `","` + ci.Args[1].(string) + `"]`
jsonArgs = `["` + nameArg + `","` + toArg + `"]`
}
events = append(events, &types.Event{
ContractAddress: receiver.ID(),
Expand All @@ -75,7 +71,8 @@ func ExecuteNameTx(bs *state.BlockState, scs *state.ContractState, txBody *types
JsonArgs: jsonArgs,
})
case types.SetContractOwner:
ownerState, err := SetContractOwner(bs, scs, ci.Args[0].(string), nameState)
ownerArg := ci.Args[0].(string)
ownerState, err := SetContractOwner(bs, scs, ownerArg, nameState)
if err != nil {
return nil, err
}
Expand All @@ -87,9 +84,7 @@ func ExecuteNameTx(bs *state.BlockState, scs *state.ContractState, txBody *types
return events, nil
}

func ValidateNameTx(tx *types.TxBody, sender *state.V,
scs, systemcs *state.ContractState) (*types.CallInfo, error) {

func ValidateNameTx(tx *types.TxBody, sender *state.V, scs *state.ContractState) (*types.CallInfo, error) {
if sender != nil && sender.Balance().Cmp(tx.GetAmountBigInt()) < 0 {
return nil, types.ErrInsufficientBalance
}
Expand All @@ -99,30 +94,25 @@ func ValidateNameTx(tx *types.TxBody, sender *state.V,
return nil, err
}

name := ci.Args[0].(string)

nameArg := ci.Args[0].(string)
switch ci.Name {
case types.NameCreate:
namePrice := system.GetNamePriceFromState(systemcs)
if namePrice.Cmp(tx.GetAmountBigInt()) > 0 {
if system.GetNamePrice().Cmp(tx.GetAmountBigInt()) > 0 {
return nil, types.ErrTooSmallAmount
}
owner := getOwner(scs, []byte(name), false)
if owner != nil {
return nil, fmt.Errorf("aleady occupied %s", string(name))
if owner := getOwner(scs, []byte(nameArg), false); owner != nil {
return nil, fmt.Errorf("aleady occupied %s", string(nameArg))
}
case types.NameUpdate:
namePrice := system.GetNamePriceFromState(systemcs)
if namePrice.Cmp(tx.GetAmountBigInt()) > 0 {
if system.GetNamePrice().Cmp(tx.GetAmountBigInt()) > 0 {
return nil, types.ErrTooSmallAmount
}
if (!bytes.Equal(tx.Account, []byte(name))) &&
(!bytes.Equal(tx.Account, getOwner(scs, []byte(name), false))) {
return nil, fmt.Errorf("owner not matched : %s", name)
if (!bytes.Equal(tx.Account, []byte(nameArg))) &&
(!bytes.Equal(tx.Account, getOwner(scs, []byte(nameArg), false))) {
return nil, fmt.Errorf("owner not matched : %s", nameArg)
}
case types.SetContractOwner:
owner := getOwner(scs, []byte(types.AergoName), false)
if owner != nil {
if owner := getOwner(scs, []byte(types.AergoName), false); owner != nil {
return nil, fmt.Errorf("owner aleady set to %s", types.EncodeAddress(owner))
}
default:
Expand All @@ -135,8 +125,6 @@ func ValidateNameTx(tx *types.TxBody, sender *state.V,
func SetContractOwner(bs *state.BlockState, scs *state.ContractState,
address string, nameState *state.V) (*state.V, error) {

name := []byte(types.AergoName)

rawaddr, err := types.DecodeAddress(address)
if err != nil {
return nil, err
Expand All @@ -150,6 +138,7 @@ func SetContractOwner(bs *state.BlockState, scs *state.ContractState,
ownerState.AddBalance(nameState.Balance())
nameState.SubBalance(nameState.Balance())

name := []byte(types.AergoName)
if err = registerOwner(scs, name, rawaddr, name); err != nil {
return nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions contract/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ func createName(scs *state.ContractState, name []byte, owner []byte) error {
// UpdateName is avaliable after bid implement
func UpdateName(bs *state.BlockState, scs *state.ContractState, tx *types.TxBody,
sender, receiver *state.V, name, to string) error {
amount := tx.GetAmountBigInt()
if len(getAddress(scs, []byte(name))) <= types.NameLength {
return fmt.Errorf("%s is not created yet", string(name))
}
destination, _ := types.DecodeAddress(to)
destination = GetAddress(scs, destination)

amount := tx.GetAmountBigInt()
sender.SubBalance(amount)
receiver.AddBalance(amount)
contract, err := bs.StateDB.OpenContractStateAccount(types.ToAccountID(destination))
Expand Down Expand Up @@ -88,7 +89,7 @@ func Resolve(bs *state.BlockState, name []byte, legacy bool) ([]byte, error) {
}

func openContract(bs *state.BlockState) (*state.ContractState, error) {
v, err := bs.GetAccountStateV([]byte("aergo.name"))
v, err := bs.GetAccountStateV([]byte(types.AergoName))
if err != nil {
return nil, err
}
Expand All @@ -101,17 +102,15 @@ func openContract(bs *state.BlockState) (*state.ContractState, error) {

// GetAddress is resolve name for mempool
func GetAddress(scs *state.ContractState, name []byte) []byte {
if len(name) == types.AddressLength ||
types.IsSpecialAccount(name) {
if len(name) == types.AddressLength || types.IsSpecialAccount(name) {
return name
}
return getAddress(scs, name)
}

// GetAddressLegacy is resolve name for mempool by buggy logic, leaved for backward compatibility
func GetAddressLegacy(scs *state.ContractState, name []byte) []byte {
if len(name) == types.AddressLength ||
strings.Contains(string(name), ".") {
if len(name) == types.AddressLength || strings.Contains(string(name), ".") {
return name
}
return getAddress(scs, name)
Expand Down
3 changes: 1 addition & 2 deletions contract/name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ func TestName(t *testing.T) {
receiver, _ := sdb.GetStateDB().GetAccountStateV(tx.Recipient)
bs := sdb.NewBlockState(sdb.GetRoot())
scs := openContractState(t, bs)
systemcs := openSystemContractState(t, bs)

err := CreateName(scs, tx, sender, receiver, name)
assert.NoError(t, err, "create name")

scs = nextBlockContractState(t, bs, scs)
_, err = ValidateNameTx(tx, sender, scs, systemcs)
_, err = ValidateNameTx(tx, sender, scs)
assert.Error(t, err, "same name")

ret := getAddress(scs, []byte(name))
Expand Down
Loading

0 comments on commit 8be3d17

Please sign in to comment.