Skip to content

Commit

Permalink
Update deps, add middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sidenaio committed Feb 16, 2023
1 parent 53425ae commit f2a2462
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 2 additions & 0 deletions api/chain_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func (api *ChainApi) SetContractData(addr common.Address, key string, value stri
return err
}
api.bc.SetContractData(addr, key, data)
api.bc.GenerateBlocks(1)
api.bc.CleanDataMiddlewareValues()
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion api/contract_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ type TxReceipt struct {
Error string `json:"error"`
GasCost decimal.Decimal `json:"gasCost"`
TxFee decimal.Decimal `json:"txFee"`
ActionResult *ActionResult `json:"ActionResult"`
ActionResult *ActionResult `json:"actionResult"`
Events []Event `json:"events"`
}

Expand Down Expand Up @@ -360,6 +360,11 @@ func convertReceipt(tx *types.Transaction, receipt *types.TxReceipt, feePerGas *
for i := range e.Data {
event.Args = append(event.Args, e.Data[i])
}
if !e.Contract.IsEmpty() {
event.Contract = e.Contract
} else {
event.Contract = receipt.ContractAddress
}
result.Events = append(result.Events, event)
}
return result
Expand Down
25 changes: 23 additions & 2 deletions chain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type MemBlockchain struct {
appstate *appstate.AppState
keyStore *keystore.KeyStore
secStore *secstore.SecStore

setDataMiddlewareValues map[common.Address]map[string][]byte
}

func NewMemBlockchain(godKey *ecdsa.PrivateKey) *MemBlockchain {
Expand Down Expand Up @@ -91,8 +93,9 @@ func NewMemBlockchain(godKey *ecdsa.PrivateKey) *MemBlockchain {
chain.InitializeChain()
appState.Initialize(chain.Head.Height())

result := &MemBlockchain{chain, txPool, appState, keyStore, secStore}
result := &MemBlockchain{chain, txPool, appState, keyStore, secStore, map[common.Address]map[string][]byte{}}
txPool.Initialize(chain.Head, secStore.GetAddress(), false)
result.UseMiddleware(result.setDataMiddleware)
return result
}

Expand Down Expand Up @@ -152,5 +155,23 @@ func (b *MemBlockchain) AddBalance(addr common.Address, amount decimal.Decimal)
}

func (b *MemBlockchain) SetContractData(addr common.Address, key string, data []byte) {
b.appstate.State.SetContractValue(addr, []byte(key), data)
var m map[string][]byte
var ok bool
if m, ok = b.setDataMiddlewareValues[addr]; !ok {
m = map[string][]byte{}
b.setDataMiddlewareValues[addr] = m
}
m[key] = data
}

func (b *MemBlockchain) setDataMiddleware(block *types.Block, appState *appstate.AppState) {
for contract, m := range b.setDataMiddlewareValues {
for key, value := range m {
appState.State.SetContractValue(contract, []byte(key), value)
}
}
}

func (b *MemBlockchain) CleanDataMiddlewareValues() {
b.setDataMiddlewareValues = map[common.Address]map[string][]byte{}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/idena-network/idena-contract-runner

require (
github.com/idena-network/idena-go v0.31.1-0.20230207091833-9a34a183499c
github.com/idena-network/idena-wasm-binding v0.0.0-20230207084120-b7d9d07ba81e
github.com/idena-network/idena-go v0.31.1-0.20230216182230-2ac1560cb634
github.com/idena-network/idena-wasm-binding v0.0.0-20230216175455-f50bdbeff409
github.com/pkg/errors v0.9.1
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc
github.com/tendermint/tm-db v0.6.7
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,10 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/idena-network/iavl v0.12.3-0.20211223100228-a33b117aa31e h1:z+r2ppfQEPV6qCeO7PwI5u0VY86hZ5NRek1/gpbPZAU=
github.com/idena-network/iavl v0.12.3-0.20211223100228-a33b117aa31e/go.mod h1:++H8jTv9JFwzyZ55ahnGcGCDjhxJacSYbZKojVdVPOo=
github.com/idena-network/idena-go v0.31.1-0.20230207091833-9a34a183499c h1:7T6MxZ0kItAWVBWNJvY9IQSe0oQFWsUaoWZjCHkAdf8=
github.com/idena-network/idena-go v0.31.1-0.20230207091833-9a34a183499c/go.mod h1:ISyWCgQPS4M3R7P5NcnXl+QbDjrmk26uieUKRstwO0E=
github.com/idena-network/idena-wasm-binding v0.0.0-20230207084120-b7d9d07ba81e h1:1RkbPEpmHNQXciOTDdMo1qb0ATucNG+AoSFkCFuw/c0=
github.com/idena-network/idena-wasm-binding v0.0.0-20230207084120-b7d9d07ba81e/go.mod h1:V62toX5ngK296WOWZHWg5pQ65kB9Y0OL0trKhsOe+FE=
github.com/idena-network/idena-go v0.31.1-0.20230216182230-2ac1560cb634 h1:k/znt92eORKRonKllDvQLbqH6vs0lNmljbjb2EeP0Es=
github.com/idena-network/idena-go v0.31.1-0.20230216182230-2ac1560cb634/go.mod h1:hyNMloopOreXkc1N3X7Xlw47oED31DBw+C/EPwvaljU=
github.com/idena-network/idena-wasm-binding v0.0.0-20230216175455-f50bdbeff409 h1:pRvttvRb0XZgO45SoHNifsyVF9dsddRI1rrYcQ/DI20=
github.com/idena-network/idena-wasm-binding v0.0.0-20230216175455-f50bdbeff409/go.mod h1:V62toX5ngK296WOWZHWg5pQ65kB9Y0OL0trKhsOe+FE=
github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down

0 comments on commit f2a2462

Please sign in to comment.