Skip to content

Commit

Permalink
Merge branch 'hotfix/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mlogue76 committed Apr 22, 2019
2 parents cfccea3 + 261b871 commit 15ebc4c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
2 changes: 1 addition & 1 deletion contract/lstate_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var getCh chan *LState
var freeCh chan *LState
var once sync.Once

const MAX_LSTATE_SIZE = 100
const MAX_LSTATE_SIZE = 150

func StartLStateFactory() {
once.Do(func() {
Expand Down
2 changes: 2 additions & 0 deletions contract/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const int *getLuaExecContext(lua_State *L)
lua_getglobal(L, luaExecContext);
service = (int *)lua_touserdata(L, -1);
lua_pop(L, 1);
if (*service == -1)
luaL_error(L, "not permitted state referencing at global scope");

return service;
}
Expand Down
24 changes: 23 additions & 1 deletion contract/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
callMaxInstLimit = C.int(5000000)
queryMaxInstLimit = callMaxInstLimit * C.int(10)
dbUpdateMaxLimit = fee.StateDbMaxUpdateSize
maxCallDepth = 5
)

var (
Expand Down Expand Up @@ -88,6 +89,7 @@ type StateSet struct {
seed *rand.Rand
events []*types.Event
eventCount int32
callDepth int32
}

type recoveryEntry struct {
Expand Down Expand Up @@ -198,6 +200,15 @@ func (L *LState) Close() {
}

func newExecutor(contract []byte, contractId []byte, stateSet *StateSet, ci *types.CallInfo, amount *big.Int, isCreate bool) *Executor {
if stateSet.callDepth > maxCallDepth {
ce := &Executor{
code: contract,
stateSet: stateSet,
}
ce.err = errors.New(fmt.Sprintf("exceeded the maximum call depth(%d)", maxCallDepth))
return ce
}
stateSet.callDepth++
ce := &Executor{
code: contract,
L: GetLState(),
Expand All @@ -208,6 +219,8 @@ func newExecutor(contract []byte, contractId []byte, stateSet *StateSet, ci *typ
ctrLog.Error().Err(ce.err).Str("contract", types.EncodeAddress(contractId)).Msg("new AergoLua executor")
return ce
}
bakupService := stateSet.service
stateSet.service = -1
hexId := C.CString(hex.EncodeToString(contractId))
defer C.free(unsafe.Pointer(hexId))
if cErrMsg := C.vm_loadbuff(
Expand All @@ -222,6 +235,7 @@ func newExecutor(contract []byte, contractId []byte, stateSet *StateSet, ci *typ
ctrLog.Error().Err(ce.err).Str("contract", types.EncodeAddress(contractId)).Msg("failed to load code")
return ce
}
stateSet.service = bakupService

if isCreate == false {
C.vm_remove_constructor(ce.L)
Expand Down Expand Up @@ -254,6 +268,7 @@ func newExecutor(contract []byte, contractId []byte, stateSet *StateSet, ci *typ
}
C.vm_get_constructor(ce.L)
if C.vm_isnil(ce.L, C.int(-1)) == 1 {
ce.close()
return nil
}
ce.numArgs = C.int(len(ci.Args))
Expand Down Expand Up @@ -499,6 +514,9 @@ func (ce *Executor) rollbackToSavepoint() error {

func (ce *Executor) close() {
if ce != nil {
if ce.stateSet != nil {
ce.stateSet.callDepth--
}
FreeLState(ce.L)
}
}
Expand Down Expand Up @@ -628,7 +646,7 @@ func PreloadEx(bs *state.BlockState, contractState *state.ContractState, contrac
}
if contractCode == nil {
contractCode = getContract(contractState, nil)
if contractCode != nil {
if contractCode != nil && bs != nil {
bs.CodeMap[contractAid] = contractCode
}
}
Expand Down Expand Up @@ -878,6 +896,10 @@ func (re *recoveryEntry) recovery() error {
if re.senderState != nil {
re.senderState.Nonce = re.senderNonce
}

if callState == nil {
return nil
}
if re.stateRevision != -1 {
err := callState.ctrState.Rollback(re.stateRevision)
if err != nil {
Expand Down
17 changes: 13 additions & 4 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var mulAergo, mulGaer, zeroBig *big.Int
const maxEventCnt = 50
const maxEventNameSize = 64
const maxEventArgSize = 4096
const luaCallCountDeduc = 1000

func init() {
mulAergo, _ = new(big.Int).SetString("1000000000000000000", 10)
Expand Down Expand Up @@ -139,6 +140,14 @@ func setInstCount(parent *LState, child *LState) {
C.luaL_setinstcount(parent, C.luaL_instcount(child))
}

func minusCallCount(curCount C.int) C.int {
remain := curCount - luaCallCountDeduc
if remain <= 0 {
remain = 1
}
return remain
}

//export LuaCallContract
func LuaCallContract(L *LState, service *C.int, contractId *C.char, fname *C.char, args *C.char,
amount *C.char, gas uint64) (C.int, *C.char) {
Expand Down Expand Up @@ -205,7 +214,7 @@ func LuaCallContract(L *LState, service *C.int, contractId *C.char, fname *C.cha
stateSet.curContract = newContractInfo(callState, prevContractInfo.contractId, cid,
callState.curState.SqlRecoveryPoint, amountBig)

ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ret := ce.call(L)
Expand Down Expand Up @@ -273,7 +282,7 @@ func LuaDelegateCallContract(L *LState, service *C.int, contractId *C.char,
}
}

ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ret := ce.call(L)
Expand Down Expand Up @@ -355,7 +364,7 @@ func LuaSendAmount(L *LState, service *C.int, contractId *C.char, amount *C.char
stateSet.curContract = newContractInfo(callState, prevContractInfo.contractId, cid,
callState.curState.SqlRecoveryPoint, amountBig)

ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ce.call(L)
Expand Down Expand Up @@ -874,7 +883,7 @@ func LuaDeployContract(
addr := C.CString(types.EncodeAddress(newContract.ID()))
ret := C.int(1)
if ce != nil {
ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ret += ce.call(L)
Expand Down
67 changes: 67 additions & 0 deletions contract/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3824,5 +3824,72 @@ abi.register(oom)`
t.Error(err)
}
}
func TestDeploy2(t *testing.T) {
deploy := `
function hello()
src = [[
state.var{
counts = state.array(10)
}
counts[1] = 10
function inc(key)
if counts[key] == nil then
counts[key] = 0
end
counts[key] = counts[key] + 1
end
function get(key)
return counts[key]
end
function set(key,val)
counts[key] = val
end
function len()
return counts:length()
end
function iter()
local rv = {}
for i, v in counts:ipairs() do
if v == nil then
rv[i] = "nil"
else
rv[i] = v
end
end
return rv
end
abi.register(inc,get,set,len,iter)
]]
paddr = contract.deploy(src)
system.print("addr :", paddr)
ret = contract.call(paddr, "hello", "world", "key")
end
function constructor()
end
abi.register(hello)
abi.payable(constructor)
`
bc, _ := LoadDummyChain()
err := bc.ConnectBlock(
NewLuaTxAccount("ktlee", 1000000000000),
NewLuaTxDef("ktlee", "deploy", 50000000000, deploy),
)
if err != nil {
t.Error(err)
}
tx := NewLuaTxCall("ktlee", "deploy", 0, `{"Name":"hello"}`).Fail(`[Contract.LuaDeployContract]newExecutor Error :not permitted state referencing at global scope`)
err = bc.ConnectBlock(tx)
if err != nil {
t.Error(err)
}
}

// end of test-cases
2 changes: 1 addition & 1 deletion libtool/src/luajit
Submodule luajit updated 1 files
+38 −20 src/lib_abi.c

0 comments on commit 15ebc4c

Please sign in to comment.