Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplication of events #1127

Merged
merged 2 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions blockchain/blockchain_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,6 @@ func GetDefaultConsensusConfig() *config.ConsensusConf {
res := *base
config.ApplyConsensusVersion(config.ConsensusV10, &res)
config.ApplyConsensusVersion(config.ConsensusV11, &res)
config.ApplyConsensusVersion(config.ConsensusV12, &res)
return &res
}
14 changes: 13 additions & 1 deletion config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ConsensusConf struct {
BurnTxRange uint64
KeyWordsV3Epoch uint16
EnableUpgrade11 bool
EnableUpgrade12 bool
}

type ConsensusVerson uint16
Expand All @@ -65,10 +66,11 @@ const (
ConsensusV9 ConsensusVerson = 9
ConsensusV10 ConsensusVerson = 10
ConsensusV11 ConsensusVerson = 11
ConsensusV12 ConsensusVerson = 12
)

var (
v9, v10, v11 ConsensusConf
v9, v10, v11, v12 ConsensusConf
ConsensusVersions map[ConsensusVerson]*ConsensusConf
)

Expand Down Expand Up @@ -127,6 +129,10 @@ func init() {
v11 = v10
ApplyConsensusVersion(ConsensusV11, &v11)
ConsensusVersions[ConsensusV11] = &v11

v12 = v11
ApplyConsensusVersion(ConsensusV12, &v12)
ConsensusVersions[ConsensusV12] = &v12
}

func ApplyConsensusVersion(ver ConsensusVerson, cfg *ConsensusConf) {
Expand All @@ -145,6 +151,12 @@ func ApplyConsensusVersion(ver ConsensusVerson, cfg *ConsensusConf) {
cfg.KeyWordsV3Epoch = 104
cfg.StartActivationDate = time.Date(2023, time.March, 1, 8, 0, 0, 0, time.UTC).Unix()
cfg.EndActivationDate = time.Date(2023, time.March, 7, 0, 0, 0, 0, time.UTC).Unix()

case ConsensusV12:
cfg.Version = ConsensusV12
cfg.EnableUpgrade12 = true
cfg.StartActivationDate = time.Date(2023, time.July, 1, 8, 0, 0, 0, time.UTC).Unix()
cfg.EndActivationDate = time.Date(2023, time.July, 7, 0, 0, 0, 0, time.UTC).Unix()
}
}

Expand Down
4 changes: 2 additions & 2 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ func (vm *VmImpl) Run(tx *types.Transaction, from *common.Address, gasLimit int6
}

if vm.IsWasm(tx) {
wasmVm := wasm.NewWasmVM(vm.appState, vm.blockHeaderProvider, vm.head, vm.cfg.IsDebug)
return wasmVm.Run(tx, costs.GasToWasmGas(uint64(gasLimit)), commitToState)
wasmVm := wasm.NewWasmVM(vm.appState, vm.blockHeaderProvider, vm.head, vm.cfg, commitToState)
return wasmVm.Run(tx, costs.GasToWasmGas(uint64(gasLimit)))
}

vm.gasCounter.Reset(int(gasLimit))
Expand Down
20 changes: 11 additions & 9 deletions vm/wasm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/idena-network/idena-go/blockchain/attachments"
"github.com/idena-network/idena-go/blockchain/types"
"github.com/idena-network/idena-go/common/math"
"github.com/idena-network/idena-go/config"
"github.com/idena-network/idena-go/core/appstate"
"github.com/idena-network/idena-go/vm/costs"
"github.com/idena-network/idena-wasm-binding/lib"
Expand All @@ -15,12 +16,13 @@ type WasmVM struct {
appState *appstate.AppState
blockHeaderProvider BlockHeaderProvider
head *types.Header
isDebug bool
cfg *config.Config
commitToState bool
}

func (vm *WasmVM) deploy(tx *types.Transaction, limit uint64) (env *WasmEnv, gasUsed uint64, actionResult []byte, err error) {
ctx := NewContractContext(tx)
env = NewWasmEnv(vm.appState, vm.blockHeaderProvider, ctx, vm.head, "deploy", vm.isDebug)
env = NewWasmEnv(vm.appState, vm.blockHeaderProvider, ctx, vm.head, "deploy", vm.cfg.IsDebug, vm.commitToState, vm.cfg.Consensus.EnableUpgrade12)
attach := attachments.ParseDeployContractAttachment(tx)
actionResult = []byte{}
if attach == nil {
Expand All @@ -37,7 +39,7 @@ func (vm *WasmVM) deploy(tx *types.Transaction, limit uint64) (env *WasmEnv, gas
}
}()
env.Deploy(attach.Code)
gasUsed, actionResult, err = lib.Deploy(lib.NewGoAPI(env, &lib.GasMeter{}), attach.Code, attach.Args, ctx.ContractAddr(), limit, vm.isDebug)
gasUsed, actionResult, err = lib.Deploy(lib.NewGoAPI(env, &lib.GasMeter{}), attach.Code, attach.Args, ctx.ContractAddr(), limit, vm.cfg.IsDebug)
return env, gasUsed, actionResult, err
}

Expand All @@ -48,7 +50,7 @@ func (vm *WasmVM) call(tx *types.Transaction, limit uint64) (env *WasmEnv, gasUs
method = attachment.Method
}
ctx := NewContractContext(tx)
env = NewWasmEnv(vm.appState, vm.blockHeaderProvider, ctx, vm.head, method, vm.isDebug)
env = NewWasmEnv(vm.appState, vm.blockHeaderProvider, ctx, vm.head, method, vm.cfg.IsDebug, vm.commitToState, vm.cfg.Consensus.EnableUpgrade12)
contract := *tx.To
code := vm.appState.State.GetContractCode(contract)
actionResult = []byte{}
Expand All @@ -65,11 +67,11 @@ func (vm *WasmVM) call(tx *types.Transaction, limit uint64) (env *WasmEnv, gasUs
gasUsed = limit
}
}()
gasUsed, actionResult, err = lib.Execute(lib.NewGoAPI(env, &lib.GasMeter{}), code, attachment.Method, attachment.Args, contract, limit, vm.isDebug)
gasUsed, actionResult, err = lib.Execute(lib.NewGoAPI(env, &lib.GasMeter{}), code, attachment.Method, attachment.Args, contract, limit, vm.cfg.IsDebug)
return env, gasUsed, actionResult, attachment.Method, err
}

func (vm *WasmVM) Run(tx *types.Transaction, wasmGasLimit uint64, commitToState bool) *types.TxReceipt {
func (vm *WasmVM) Run(tx *types.Transaction, wasmGasLimit uint64) *types.TxReceipt {
var usedGas uint64
var err error
var env *WasmEnv
Expand All @@ -86,7 +88,7 @@ func (vm *WasmVM) Run(tx *types.Transaction, wasmGasLimit uint64, commitToState
env, usedGas, actionResult, method, err = vm.call(tx, wasmGasLimit)
}
var events []*types.TxEvent
if err == nil && commitToState {
if err == nil && vm.commitToState {
events = env.InternalCommit()
}

Expand All @@ -109,6 +111,6 @@ func (vm *WasmVM) Run(tx *types.Transaction, wasmGasLimit uint64, commitToState
}
}

func NewWasmVM(appState *appstate.AppState, blockHeaderProvider BlockHeaderProvider, head *types.Header, isDebug bool) *WasmVM {
return &WasmVM{appState: appState, blockHeaderProvider: blockHeaderProvider, head: head, isDebug: isDebug}
func NewWasmVM(appState *appstate.AppState, blockHeaderProvider BlockHeaderProvider, head *types.Header, cfg *config.Config, commitToState bool) *WasmVM {
return &WasmVM{appState: appState, blockHeaderProvider: blockHeaderProvider, head: head, cfg: cfg, commitToState: commitToState}
}
38 changes: 30 additions & 8 deletions vm/wasm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"github.com/idena-network/idena-go/blockchain/types"
"github.com/idena-network/idena-go/common"
"github.com/idena-network/idena-go/common/eventbus"
"github.com/idena-network/idena-go/config"
"github.com/idena-network/idena-go/core/appstate"
"github.com/idena-network/idena-go/core/state"
"github.com/idena-network/idena-go/crypto"
"github.com/idena-network/idena-go/secstore"
"github.com/idena-network/idena-go/vm/helpers"
"github.com/idena-network/idena-go/vm/wasm/testdata"
"github.com/shopspring/decimal"
Expand All @@ -19,12 +21,32 @@ import (
"testing"
)

func getLatestConfig() *config.Config {
key, _ := crypto.GenerateKey()
secStore := secstore.NewSecStore()

secStore.AddKey(crypto.FromECDSA(key))
alloc := make(map[common.Address]config.GenesisAllocation)
cfg := &config.Config{
Network: 0x99,
Consensus: config.ConsensusVersions[config.ConsensusV12],
GenesisConf: &config.GenesisConf{
Alloc: alloc,
GodAddress: secStore.GetAddress(),
},
Blockchain: &config.BlockchainConfig{},
OfflineDetection: config.GetDefaultOfflineDetectionConfig(),
IsDebug: true,
}
return cfg
}

func TestVm_Erc20(t *testing.T) {
db := dbm.NewMemDB()
appState, _ := appstate.NewAppState(db, eventbus.New())
appState.Initialize(0)

vm := NewWasmVM(appState, nil, createHeader(1, 1), true)
vm := NewWasmVM(appState, nil, createHeader(1, 1), getLatestConfig(), true)
rnd := rand.New(rand.NewSource(1))
key, _ := crypto.GenerateKeyFromSeed(rnd)

Expand All @@ -42,7 +64,7 @@ func TestVm_Erc20(t *testing.T) {
}
tx, _ = types.SignTx(tx, key)

receipt := vm.Run(tx, 4000000, true)
receipt := vm.Run(tx, 4000000)
t.Logf("%+v\n", receipt)
require.True(t, receipt.Success)

Expand All @@ -66,7 +88,7 @@ func TestVm_Erc20(t *testing.T) {
Amount: big.NewInt(10),
}
tx, _ = types.SignTx(tx, key)
receipt = vm.Run(tx, 10000000, true)
receipt = vm.Run(tx, 10000000)
t.Logf("%+v\n", receipt)
require.True(t, receipt.Success)

Expand All @@ -91,15 +113,15 @@ func TestVm_Erc20(t *testing.T) {
Amount: big.NewInt(10),
}
tx, _ = types.SignTx(tx, key2)
receipt = vm.Run(tx, 10000000, true)
receipt = vm.Run(tx, 10000000)
t.Logf("%+v\n", receipt)
require.False(t, receipt.Success)
}

var nonce = uint32(1)

func deployContract(key *ecdsa.PrivateKey, appState *appstate.AppState, code []byte, args ...[]byte) *types.TxReceipt {
vm := NewWasmVM(appState, nil, createHeader(1, 1), true)
vm := NewWasmVM(appState, nil, createHeader(1, 1), getLatestConfig(), true)
deployAttach := attachments.CreateDeployContractAttachment(common.Hash{}, code, nil, args...)
payload, _ := deployAttach.ToBytes()

Expand All @@ -112,11 +134,11 @@ func deployContract(key *ecdsa.PrivateKey, appState *appstate.AppState, code []b
}
tx, _ = types.SignTx(tx, key)
nonce++
return vm.Run(tx, 5000000, true)
return vm.Run(tx, 5000000)
}

func callContract(key *ecdsa.PrivateKey, appState *appstate.AppState, contract common.Address, method string, args ...[]byte) *types.TxReceipt {
vm := NewWasmVM(appState, nil, createHeader(1, 1), true)
vm := NewWasmVM(appState, nil, createHeader(1, 1), getLatestConfig(), true)
callAttach := attachments.CreateCallContractAttachment(method, args...)
payload, _ := callAttach.ToBytes()

Expand All @@ -130,7 +152,7 @@ func callContract(key *ecdsa.PrivateKey, appState *appstate.AppState, contract c
}
tx, _ = types.SignTx(tx, key)
nonce++
return vm.Run(tx, 612000000, true)
return vm.Run(tx, 612000000)
}

func TestVm_IncAndSum_cross_contract_call(t *testing.T) {
Expand Down
15 changes: 13 additions & 2 deletions vm/wasm/wasm_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type WasmEnv struct {
events []*types.TxEvent
method string
isDebug bool
enableUpgrade12 bool
commitToState bool
}

func (w *WasmEnv) Burn(meter *lib.GasMeter, amount *big.Int) error {
Expand Down Expand Up @@ -186,7 +188,7 @@ func (w *WasmEnv) ContractAddress(meter *lib.GasMeter) lib.Address {
return w.ctx.ContractAddr()
}

func NewWasmEnv(appState *appstate.AppState, blockHeaderProvider BlockHeaderProvider, ctx *ContractContext, head *types.Header, method string, isDebug bool) *WasmEnv {
func NewWasmEnv(appState *appstate.AppState, blockHeaderProvider BlockHeaderProvider, ctx *ContractContext, head *types.Header, method string, isDebug bool, commitToState bool, enableUpgrade12 bool) *WasmEnv {
return &WasmEnv{
id: 1,
headerProvider: blockHeaderProvider,
Expand All @@ -198,6 +200,8 @@ func NewWasmEnv(appState *appstate.AppState, blockHeaderProvider BlockHeaderProv
deployedContractCache: map[common.Address]ContractData{},
method: method,
isDebug: isDebug,
enableUpgrade12: enableUpgrade12,
commitToState: commitToState,
}
}

Expand Down Expand Up @@ -306,6 +310,8 @@ func (w *WasmEnv) CreateSubEnv(contract lib.Address, method string, payAmount *b
deployedContractCache: map[common.Address]ContractData{},
head: w.head,
isDebug: w.isDebug,
enableUpgrade12: w.enableUpgrade12,
commitToState: w.commitToState,
}
if w.isDebug {
log.Info("created sub env", "id", subEnv.id, "method", method, "parent method", subEnv.parent.method)
Expand Down Expand Up @@ -394,9 +400,14 @@ func (w *WasmEnv) Commit() {
for _, e := range w.events {
w.parent.events = append(w.parent.events, e)
}
if w.enableUpgrade12 {
w.events = nil
}
return
}
if !w.commitToState {
return
}

for contract, cache := range w.contractStoreCache {
for k, v := range cache {
if v.removed {
Expand Down