Skip to content

Commit

Permalink
Merge branch 'topic/hardfork-v4' into fix/bignum-octal-format
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Oct 6, 2023
2 parents 1cb1785 + c57a6cb commit d027429
Show file tree
Hide file tree
Showing 67 changed files with 15,713 additions and 9,033 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ add_custom_target(distclean GO111MODULE=on go clean ..
DEPENDS libtool-clean)

add_custom_target(protoc
COMMAND protoc -I/usr/local/include -I${PROTO_DIR} --go_out=plugins=grpc:${CMAKE_CURRENT_LIST_DIR}/types ${PROTO_DIR}/*.proto
COMMAND protoc -I/usr/local/include -I${PROTO_DIR} --go_out=${CMAKE_CURRENT_LIST_DIR}/types --go_opt=paths=source_relative --go-grpc_out=${CMAKE_CURRENT_LIST_DIR}/types --go-grpc_opt=paths=source_relative ${PROTO_DIR}/*.proto
COMMAND GO111MODULE=on go build ../types/...)

add_custom_target(protoclean rm -f ../types/*.pb.go)
Expand Down
2 changes: 1 addition & 1 deletion account/accountservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (as *AccountService) Statistics() *map[string]interface{} {
}
}
func (as *AccountService) resolveName(namedAddress []byte) ([]byte, error) {
scs, err := as.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte(types.AergoName)))
scs, err := as.sdb.GetStateDB().GetNameAccountState()
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions chain/chainhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ func (cs *ChainService) executeBlock(bstate *state.BlockState, block *types.Bloc

// contract & state DB update is done during execution.
if err := ex.execute(); err != nil {
cs.Update(bestBlock)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion chain/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func (cm *ChainManager) Receive(context actor.Context) {

func getAddressNameResolved(sdb *state.StateDB, account []byte) ([]byte, error) {
if len(account) == types.NameLength {
scs, err := sdb.OpenContractStateAccount(types.ToAccountID([]byte(types.AergoName)))
scs, err := sdb.GetNameAccountState()
if err != nil {
logger.Error().Str("hash", enc.ToString(account)).Err(err).Msg("failed to get state for account")
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions chain/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +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 *state.StateDB, genesis *types.Genesis) error {
aid := types.ToAccountID([]byte(types.AergoSystem))
scs, err := states.OpenContractStateAccount(aid)
scs, err := states.GetSystemAccountState()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion chain/signVerifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (sv *SignVerifier) verifyTx(comm component.IComponentRequester, tx *types.T
}

if tx.NeedNameVerify() {
cs, err := sv.sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte(types.AergoName)))
cs, err := sv.sdb.GetStateDB().GetNameAccountState()
if err != nil {
logger.Error().Err(err).Msg("failed to get verify because of opening contract error")
return false, 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 *state.StateDB) error {
s, err := sdb.OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
s, err := sdb.GetSystemAccountState()
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions contract/bignum_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,19 @@ static const luaL_Reg R[] = {
};

LUALIB_API int luaopen_bignum(lua_State *L) {

luaL_newmetatable(L,MYTYPE);
lua_setglobal(L,MYNAME);
luaL_register(L,MYNAME,R);

lua_pushliteral(L,"version"); /* version */
lua_pushliteral(L,MYVERSION);
lua_settable(L,-3);

lua_pushliteral(L,"__index");
lua_pushvalue(L,-2);
lua_settable(L,-3);

lua_pop(L, 1);
return 1;
}
1 change: 0 additions & 1 deletion contract/contract_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ static int moduleSend(lua_State *L) {
static int moduleBalance(lua_State *L) {
char *contract;
int service = getLuaExecContext(L);
lua_Integer amount;
struct luaGetBalance_return balance;
int nArg;

Expand Down
59 changes: 30 additions & 29 deletions contract/db_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,36 +600,36 @@ int lua_db_release_resource(lua_State *L) {
return 0;
}

int luaopen_db(lua_State *L) {
static const luaL_Reg rs_methods[] = {
{"next", db_rs_next},
{"get", db_rs_get},
{"colcnt", db_rs_colcnt},
{"__tostring", db_rs_tostr},
{"__gc", db_rs_gc},
{NULL, NULL}
};

static const luaL_Reg pstmt_methods[] = {
{"exec", db_pstmt_exec},
{"query", db_pstmt_query},
{"column_info", db_pstmt_column_info},
{"bind_param_cnt", db_pstmt_bind_param_cnt},
{"__tostring", db_pstmt_tostr},
{"__gc", db_pstmt_gc},
{NULL, NULL}
};

static const luaL_Reg db_lib[] = {
{"exec", db_exec},
{"query", db_query},
{"prepare", db_prepare},
{"getsnap", db_get_snapshot},
{"open_with_snapshot", db_open_with_snapshot},
{"last_insert_rowid", db_last_insert_rowid},
{NULL, NULL}
};

static const luaL_Reg rs_methods[] = {
{"next", db_rs_next},
{"get", db_rs_get},
{"colcnt", db_rs_colcnt},
{"__tostring", db_rs_tostr},
{"__gc", db_rs_gc},
{NULL, NULL}
};

static const luaL_Reg pstmt_methods[] = {
{"exec", db_pstmt_exec},
{"query", db_pstmt_query},
{"column_info", db_pstmt_column_info},
{"bind_param_cnt", db_pstmt_bind_param_cnt},
{"__tostring", db_pstmt_tostr},
{"__gc", db_pstmt_gc},
{NULL, NULL}
};

static const luaL_Reg db_lib[] = {
{"exec", db_exec},
{"query", db_query},
{"prepare", db_prepare},
{"getsnap", db_get_snapshot},
{"open_with_snapshot", db_open_with_snapshot},
{"last_insert_rowid", db_last_insert_rowid},
{NULL, NULL}
};
int luaopen_db(lua_State *L) {

luaL_newmetatable(L, DB_RS_ID);
lua_pushvalue(L, -1);
Expand All @@ -642,6 +642,7 @@ int luaopen_db(lua_State *L) {
luaL_register(L, NULL, pstmt_methods);

luaL_register(L, "db", db_lib);

lua_pop(L, 3);
return 1;
}
2 changes: 1 addition & 1 deletion contract/enterprise/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func initTest(t *testing.T) (*state.ContractState, *state.V, *state.V) {
}
const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"

scs, err := cdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
scs, err := cdb.GetStateDB().GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")

account, err := types.DecodeAddress(testSender)
Expand Down
2 changes: 1 addition & 1 deletion contract/name/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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.OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
systemContractState, err := bs.StateDB.GetSystemAccountState()

ci, err := ValidateNameTx(txBody, sender, scs, systemContractState)
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions contract/name/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,13 @@ func TestExcuteFailNameTx(t *testing.T) {
}

func openContractState(t *testing.T, bs *state.BlockState) *state.ContractState {
nameContractID := types.ToAccountID([]byte(types.AergoName))
nameContract, err := bs.GetAccountState(nameContractID)
assert.NoError(t, err, "could not get account state")
scs, err := bs.OpenContractState(nameContractID, nameContract)
scs, err := bs.GetNameAccountState()
assert.NoError(t, err, "could not open contract state")
return scs
}

func openSystemContractState(t *testing.T, bs *state.BlockState) *state.ContractState {
systemContractID := types.ToAccountID([]byte(types.AergoSystem))
systemContract, err := bs.GetAccountState(systemContractID)
assert.NoError(t, err, "could not get account state")
scs, err := bs.OpenContractState(systemContractID, systemContract)
scs, err := bs.GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")
return scs
}
Expand Down
2 changes: 1 addition & 1 deletion contract/name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestNameNil(t *testing.T) {
name1 := "AB1234567890"
name2 := "1234567890CD"

scs, err := sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
scs, err := sdb.GetStateDB().GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")
tx := &types.TxBody{Account: []byte(name1), Payload: buildNamePayload(name2, types.NameCreate, "")}
sender, _ := sdb.GetStateDB().GetAccountStateV(tx.Account)
Expand Down
1 change: 1 addition & 0 deletions contract/state_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,5 +669,6 @@ int luaopen_state(lua_State *L) {

luaL_register(L, "state", state_lib);

lua_pop(L, 1);
return 1;
}
4 changes: 2 additions & 2 deletions contract/system/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestValidateSystemTxForStaking(t *testing.T) {
scs, sender, receiver := initTest(t)
defer deinitTest()

scs, err := cdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
scs, err := cdb.GetStateDB().GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")

tx := &types.Tx{
Expand All @@ -265,7 +265,7 @@ func TestValidateSystemTxForUnstaking(t *testing.T) {
defer deinitTest()
const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"

scs, err := cdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
scs, err := cdb.GetStateDB().GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")

account, err := types.DecodeAddress(testSender)
Expand Down
2 changes: 1 addition & 1 deletion contract/system/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestUnstakingError(t *testing.T) {
initTest(t)
defer deinitTest()
const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"
scs, err := cdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
scs, err := cdb.GetStateDB().GetSystemAccountState()
assert.Equal(t, err, nil, "could not open contract state")

account, err := types.DecodeAddress(testSender)
Expand Down
18 changes: 16 additions & 2 deletions contract/system/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,27 @@ func newVprCmd(ctx *SystemContext, vr *VoteResult) *vprCmd {

func (c *vprCmd) subVote(v *types.Vote) error {
votingPowerRank.sub(c.Sender.AccountID(), c.Sender.ID(), v.GetAmountBigInt())

// Hotfix - reproduce vpr calculation for block 138015125
// When block is reverted, votingPowerRank is not reverted and calculated three times.
// TODO : implement commit, revert, reorg for governance variables.
if c.BlockInfo.No == 138015125 && c.Sender.AccountID().String() == "36t2u7Q31HmEbkkYZng7DHNm3xepxHKUfgGrAXNA8pMW" {
for i := 0; i < 2; i++ {
votingPowerRank.sub(c.Sender.AccountID(), c.Sender.ID(), v.GetAmountBigInt())
}
}
return c.voteResult.SubVote(v)
}

func (c *vprCmd) addVote(v *types.Vote) error {
votingPowerRank.add(c.Sender.AccountID(), c.Sender.ID(), v.GetAmountBigInt())

// Hotfix - reproduce vpr calculation for block 138015125
// When block is reverted, votingPowerRank is not reverted and calculated three times.
// TODO : implement commit, revert, reorg for governance variables.
if c.BlockInfo.No == 138015125 && c.Sender.AccountID().String() == "36t2u7Q31HmEbkkYZng7DHNm3xepxHKUfgGrAXNA8pMW" {
for i := 0; i < 2; i++ {
votingPowerRank.add(c.Sender.AccountID(), c.Sender.ID(), v.GetAmountBigInt())
}
}
return c.voteResult.AddVote(v)
}

Expand Down
7 changes: 2 additions & 5 deletions contract/system/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func initTest(t *testing.T) (*state.ContractState, *state.V, *state.V) {
InitGovernance("dpos")
const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"

scs, err := bs.OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
scs, err := bs.GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")
InitSystemParams(scs, 3)

Expand All @@ -64,10 +64,7 @@ func commitNextBlock(t *testing.T, scs *state.ContractState) *state.ContractStat
bs.Update()
bs.Commit()
cdb.UpdateRoot(bs)
systemContractID := types.ToAccountID([]byte(types.AergoSystem))
systemContract, err := bs.GetAccountState(systemContractID)
assert.NoError(t, err, "could not get account state")
ret, err := bs.OpenContractState(systemContractID, systemContract)
ret, err := bs.GetSystemAccountState()
assert.NoError(t, err, "could not open contract state")
return ret
}
Expand Down
8 changes: 4 additions & 4 deletions contract/system/vprt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"container/list"
"fmt"
"github.com/aergoio/aergo-lib/log"
"math/big"
"math/rand"
"os"
"testing"

"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/types"
Expand Down Expand Up @@ -125,7 +125,7 @@ func initVprtTest(t *testing.T, initTable func()) {
func initVprtTestWithSc(t *testing.T, initTable func(*state.ContractState)) {
initDB(t)

s, err := vprStateDB.OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
s, err := vprStateDB.GetSystemAccountState()
assert.NoError(t, err, "fail to open the system contract state")

initTable(s)
Expand All @@ -148,7 +148,7 @@ func getStateRoot() []byte {
}

func openSystemAccountWith(root []byte) *state.ContractState {
s, err := vprChainStateDB.OpenNewStateDB(root).OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
s, err := vprChainStateDB.OpenNewStateDB(root).GetSystemAccountState()
if err != nil {
return nil
}
Expand All @@ -172,7 +172,7 @@ func initRankTableRand(rankMax uint32) {
}

func openSystemAccount(t *testing.T) *state.ContractState {
s, err := vprStateDB.OpenContractStateAccount(types.ToAccountID([]byte(types.AergoSystem)))
s, err := vprStateDB.GetSystemAccountState()
assert.NoError(t, err, "fail to open the system contract state")
logger.Debug().Msgf(
"(after) state, contract: %s, %s\n",
Expand Down
3 changes: 2 additions & 1 deletion contract/system_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include "lua.h"

extern int luaopen_system(lua_State *L);

extern int setItem(lua_State *L);
extern int setItemWithPrefix(lua_State *L);
extern int getItem(lua_State *L);
extern int getItemWithPrefix(lua_State *L);
extern int delItemWithPrefix(lua_State *L);

#endif /* _SYSTEM_MODULE_H */
#endif /* _SYSTEM_MODULE_H */
5 changes: 4 additions & 1 deletion contract/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,20 @@ const char *vm_loadcall(lua_State *L) {
}
luaL_enablemaxmem(L);
}

err = lua_pcall(L, 0, 0, 0);

if (lua_usegas(L)) {
lua_disablegas(L);
} else {
luaL_disablemaxmem(L);
}

lua_sethook(L, NULL, 0, 0);

if (err != 0) {
return lua_tostring(L, -1);
}

return NULL;
}

Expand Down
Loading

0 comments on commit d027429

Please sign in to comment.