Skip to content

Commit

Permalink
Call the start function of a smart contract (if exists) at initializa…
Browse files Browse the repository at this point in the history
…tion. (#145)

* contract: Call start function if exists when calling the contract for the first time.

* contract: Count gas for calling start function.
  • Loading branch information
losfair authored and iwasaki-kenta committed Jul 31, 2019
1 parent 6f2d8a2 commit 3f92943
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ func (e *ContractExecutor) Execute(snapshot *avl.Tree, id AccountID, round *Roun
return errors.Wrap(err, "could not init vm")
}

var firstRun bool

if mem := LoadContractMemorySnapshot(snapshot, id); mem != nil {
vm.Memory = mem
} else {
firstRun = true
}

e.ID = id
Expand All @@ -249,14 +253,33 @@ func (e *ContractExecutor) Execute(snapshot *avl.Tree, id AccountID, round *Roun
return errors.New("entry function must not have parameters")
}

vm.Ignite(entry)
if firstRun {
if vm.Module.Base.Start != nil {
startID := int(vm.Module.Base.Start.Index)

for !vm.Exited {
vm.Execute()
vm.Ignite(startID)

if vm.Delegate != nil {
vm.Delegate()
vm.Delegate = nil
for !vm.Exited {
vm.Execute()

if vm.Delegate != nil {
vm.Delegate()
vm.Delegate = nil
}
}
}
}

if vm.ExitError == nil {
vm.Ignite(entry)

for !vm.Exited {
vm.Execute()

if vm.Delegate != nil {
vm.Delegate()
vm.Delegate = nil
}
}
}

Expand All @@ -277,7 +300,11 @@ func (e *ContractExecutor) Execute(snapshot *avl.Tree, id AccountID, round *Roun
e.GasLimitExceeded = false
}

return nil
if vm.ExitError != nil {
return utils.UnifyError(vm.ExitError)
} else {
return nil
}
}

func LoadContractMemorySnapshot(snapshot *avl.Tree, id AccountID) []byte {
Expand Down

0 comments on commit 3f92943

Please sign in to comment.