Skip to content

Commit

Permalink
Merge pull request #33 from multiversx/set-state-overwrite-endpoint
Browse files Browse the repository at this point in the history
Set state overwrite
  • Loading branch information
miiu96 authored Apr 25, 2024
2 parents 555df09 + 3b3247f commit ed28c4d
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 28 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,58 @@ Example:
```


### `POST /simulator/set-state-overwrite`

This endpoint allows you to set the entire state (also will clean the old state of the provided accounts) for a provided list of addresses.

##### Request
- **Method:** POST
- **Path:** `/simulator/set-state`


##### Request Body
The request body should be a JSON object representing an array of object with the next format.

Example:
```
[
{
"address": "erd1qqqqqqqqqqqqqpgqmzzm05jeav6d5qvna0q2pmcllelkz8xddz3syjszx5",
"balance": "431271308732096033771131",
"code": "0061736d010000000129086000006000017f60027f7f017f60027f7f0060017f0060037f7f7f017f60037f7f7f0060017f017f0290020b03656e7619626967496e74476574556e7369676e6564417267756d656e74000303656e760f6765744e756d417267756d656e7473000103656e760b7369676e616c4572726f72000303656e76126d42756666657253746f726167654c6f6164000203656e76176d427566666572546f426967496e74556e7369676e6564000203656e76196d42756666657246726f6d426967496e74556e7369676e6564000203656e76136d42756666657253746f7261676553746f7265000203656e760f6d4275666665725365744279746573000503656e760e636865636b4e6f5061796d656e74000003656e7614626967496e7446696e697368556e7369676e6564000403656e7609626967496e744164640006030b0a010104070301000000000503010003060f027f0041a080080b7f0041a080080b074607066d656d6f7279020004696e697400110667657453756d00120361646400130863616c6c4261636b00140a5f5f646174615f656e6403000b5f5f686561705f6261736503010aca010a0e01017f4100100c2200100020000b1901017f419c8008419c800828020041016b220036020020000b1400100120004604400f0b4180800841191002000b16002000100c220010031a2000100c220010041a20000b1401017f100c2202200110051a2000200210061a0b1301017f100c220041998008410310071a20000b1401017f10084101100d100b210010102000100f0b0e0010084100100d1010100e10090b2201037f10084101100d100b210110102202100e220020002001100a20022000100f0b0300010b0b2f0200418080080b1c77726f6e67206e756d626572206f6620617267756d656e747373756d00419c80080b049cffffff",
"rootHash": "76cr5Jhn6HmBcDUMIzikEpqFgZxIrOzgNkTHNatXzC4=",
"codeMetadata": "BQY=",
"codeHash": "n9EviPlHS6EV+3Xp0YqP28T0IUfeAFRFBIRC1Jw6pyU=",
"developerReward": "5401004999998",
"ownerAddress": "erd1ss6u80ruas2phpmr82r42xnkd6rxy40g9jl69frppl4qez9w2jpsqj8x97",
"keys": {
"73756d": "0a"
}
},
{
"address": "erd1ss6u80ruas2phpmr82r42xnkd6rxy40g9jl69frppl4qez9w2jpsqj8x97",
"balance": "431271308732096033771131"
}
// ...additional state for another address
]
```


##### Response
- **Status Codes:**
- `200 OK`: State set successfully.
- `404 Bad Request`: Invalid request parameters.

#### Response Body
```json
{
"data": {},
"error": "",
"code": "successful"
}
```


### `POST /simulator/add-keys`

This endpoint allows you to add new validator private keys in the multi key handler.
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.20

require (
github.com/gin-gonic/gin v1.9.1
github.com/multiversx/mx-chain-core-go v1.2.19
github.com/multiversx/mx-chain-go v1.7.9-0.20240411072641-1658254d294b
github.com/multiversx/mx-chain-core-go v1.2.20
github.com/multiversx/mx-chain-go v1.7.9
github.com/multiversx/mx-chain-logger-go v1.0.14
github.com/multiversx/mx-chain-proxy-go v1.1.47
github.com/multiversx/mx-chain-proxy-go v1.1.48
github.com/pelletier/go-toml v1.9.3
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.10
Expand Down Expand Up @@ -170,13 +170,13 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,18 @@ github.com/multiversx/concurrent-map v0.1.4 h1:hdnbM8VE4b0KYJaGY5yJS2aNIW9TFFsUY
github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7eFQ4U2vKMOp4A/9+o=
github.com/multiversx/mx-chain-communication-go v1.0.14 h1:YhAUDjBBpc5h5W0A7LHLXUMIMeCgwgGvkqfAPbFqsno=
github.com/multiversx/mx-chain-communication-go v1.0.14/go.mod h1:qYCqgk0h+YpcTA84jHIpCBy6UShRwmXzHSCcdfwNrkw=
github.com/multiversx/mx-chain-core-go v1.2.19 h1:2BaVHkB0tro3cjs5ay2pmLup1loCV0e1p9jV5QW0xqc=
github.com/multiversx/mx-chain-core-go v1.2.19/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-core-go v1.2.20 h1:jOQ10LxxUqECnuqUYeBBT6VoZcpJDdYgOvsSGtifDdI=
github.com/multiversx/mx-chain-core-go v1.2.20/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-crypto-go v1.2.11 h1:MNPJoiTJA5/tedYrI0N22OorbsKDESWG0SF8MCJwcJI=
github.com/multiversx/mx-chain-crypto-go v1.2.11/go.mod h1:pcZutPdfLiAFytzCU3LxU3s8cXkvpNqquyitFSfoF3o=
github.com/multiversx/mx-chain-es-indexer-go v1.4.21 h1:rzxXCkgOsqj67GRYtqzKuf9XgHwnZLTZhU90Ck3VbrE=
github.com/multiversx/mx-chain-es-indexer-go v1.4.21/go.mod h1:V9xxOBkfV7GjN4K5SODaOetoGVpQm4snibMVPCjL0Kk=
github.com/multiversx/mx-chain-go v1.7.9-0.20240411072641-1658254d294b h1:7wPvUxCPml/dn8Ows6LiOPZ0LUgfcpB0BGVUQwztVYw=
github.com/multiversx/mx-chain-go v1.7.9-0.20240411072641-1658254d294b/go.mod h1:egvBH/R9pwYGHd8KsIw/8B3GXE4z0kkegnKKTk4gAL0=
github.com/multiversx/mx-chain-go v1.7.9 h1:2b6ef9ggIS3Mgf38wn+Z71/Fy0enMTCa/sp9F4faNFk=
github.com/multiversx/mx-chain-go v1.7.9/go.mod h1:HwklJGQfMpv/yyF4oLpxjwdKCawspv1JjdgezlWBpRQ=
github.com/multiversx/mx-chain-logger-go v1.0.14 h1:PRMpAvXE7Nec2d//QNmbYfKVHMomOKmcN4UXurQWX9o=
github.com/multiversx/mx-chain-logger-go v1.0.14/go.mod h1:bDfHSdwqIimn7Gp8w+SH5KlDuGzJ//nlyEANAaTSc3o=
github.com/multiversx/mx-chain-proxy-go v1.1.47 h1:9oebF9PXbfP9QMJw0pjARbnEIwS8wRigOYn8+po1W8g=
github.com/multiversx/mx-chain-proxy-go v1.1.47/go.mod h1:Ax+80b1qynepkZSxrz8QB3AO00JAL3svaLasMTE6Lh0=
github.com/multiversx/mx-chain-proxy-go v1.1.48 h1:gmunv+2oimIN8ejlVAN7eWP7mt38So1WvsyiR3+94SU=
github.com/multiversx/mx-chain-proxy-go v1.1.48/go.mod h1:Ax+80b1qynepkZSxrz8QB3AO00JAL3svaLasMTE6Lh0=
github.com/multiversx/mx-chain-scenario-go v1.4.3 h1:9xeVB8TOsolXS4YEr1CZ/VZr5Qk0X+nde8nRGnxJICo=
github.com/multiversx/mx-chain-scenario-go v1.4.3/go.mod h1:Bd7/Xs3mWM6pX/REHK5dfpf3MUfjMZ7li09cfCxg2ac=
github.com/multiversx/mx-chain-storage-go v1.0.15 h1:PDyP1uouAVjR32dFgM+7iaQBdReD/tKBJj10JbxXvaE=
Expand Down Expand Up @@ -637,8 +637,8 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
Expand Down Expand Up @@ -684,8 +684,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -742,8 +742,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand All @@ -758,8 +758,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
1 change: 1 addition & 0 deletions pkg/facade/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SimulatorHandler interface {
GenerateBlocks(numOfBlocks int) error
SetKeyValueForAddress(address string, keyValueMap map[string]string) error
SetStateMultiple(stateSlice []*dtos.AddressState) error
RemoveAccounts(addresses []string) error
AddValidatorKeys(validatorsPrivateKeys [][]byte) error
GenerateBlocksUntilEpochIsReached(targetEpoch int32) error
ForceResetValidatorStatisticsCache() error
Expand Down
15 changes: 15 additions & 0 deletions pkg/facade/simulatorFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func (sf *simulatorFacade) SetStateMultiple(stateSlice []*dtos.AddressState) err
return sf.simulator.SetStateMultiple(stateSlice)
}

// SetStateMultipleOverwrite will set the entire state for the provided address and cleanup the old state of the provided addresses
func (sf *simulatorFacade) SetStateMultipleOverwrite(stateSlice []*dtos.AddressState) error {
accounts := make([]string, 0, len(stateSlice))
for _, state := range stateSlice {
accounts = append(accounts, state.Address)
}

err := sf.simulator.RemoveAccounts(accounts)
if err != nil {
return err
}

return sf.simulator.SetStateMultiple(stateSlice)
}

// AddValidatorKeys will add the validator keys in the multi key handler
func (sf *simulatorFacade) AddValidatorKeys(validators *dtoc.ValidatorKeys) error {
validatorsPrivateKeys := make([][]byte, 0, len(validators.PrivateKeysBase64))
Expand Down
33 changes: 26 additions & 7 deletions pkg/proxy/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
)

const (
generateBlocksEndpoint = "/simulator/generate-blocks/:num"
generateBlockUnitEpochReached = "/simulator/generate-blocks-until-epoch-reached/:epoch"
initialWalletsEndpoint = "/simulator/initial-wallets"
setKeyValuesEndpoint = "/simulator/address/:address/set-state"
setStateMultipleEndpoint = "/simulator/set-state"
addValidatorsKeys = "/simulator/add-keys"
forceUpdateValidatorStatistics = "/simulator/force-reset-validator-statistics"
generateBlocksEndpoint = "/simulator/generate-blocks/:num"
generateBlockUnitEpochReached = "/simulator/generate-blocks-until-epoch-reached/:epoch"
initialWalletsEndpoint = "/simulator/initial-wallets"
setKeyValuesEndpoint = "/simulator/address/:address/set-state"
setStateMultipleEndpoint = "/simulator/set-state"
setStateMultipleOverwriteEndpoint = "/simulator/set-state-overwrite"
addValidatorsKeys = "/simulator/add-keys"
forceUpdateValidatorStatistics = "/simulator/force-reset-validator-statistics"
)

type endpointsProcessor struct {
Expand All @@ -46,6 +47,7 @@ func (ep *endpointsProcessor) ExtendProxyServer(httpServer *http.Server) error {
ws.GET(initialWalletsEndpoint, ep.initialWallets)
ws.POST(setKeyValuesEndpoint, ep.setKeyValue)
ws.POST(setStateMultipleEndpoint, ep.setStateMultiple)
ws.POST(setStateMultipleOverwriteEndpoint, ep.setStateMultipleOverwrite)
ws.POST(addValidatorsKeys, ep.addValidatorKeys)
ws.POST(forceUpdateValidatorStatistics, ep.forceUpdateValidatorStatistics)

Expand Down Expand Up @@ -142,6 +144,23 @@ func (ep *endpointsProcessor) setStateMultiple(c *gin.Context) {
shared.RespondWith(c, http.StatusOK, gin.H{}, "", data.ReturnCodeSuccess)
}

func (ep *endpointsProcessor) setStateMultipleOverwrite(c *gin.Context) {
var stateSlice []*dtos.AddressState
err := c.ShouldBindJSON(&stateSlice)
if err != nil {
shared.RespondWithBadRequest(c, fmt.Sprintf("invalid state structure, error: %s", err.Error()))
return
}

err = ep.facade.SetStateMultipleOverwrite(stateSlice)
if err != nil {
shared.RespondWithBadRequest(c, fmt.Sprintf("cannot overwrite state, error: %s", err.Error()))
return
}

shared.RespondWith(c, http.StatusOK, gin.H{}, "", data.ReturnCodeSuccess)
}

func (ep *endpointsProcessor) addValidatorKeys(c *gin.Context) {
validatorsKeys := &dtosc.ValidatorKeys{}

Expand Down
1 change: 1 addition & 0 deletions pkg/proxy/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type SimulatorFacade interface {
GetInitialWalletKeys() *dtos.InitialWalletKeys
SetKeyValueForAddress(address string, keyValueMap map[string]string) error
SetStateMultiple(stateSlice []*dtos.AddressState) error
SetStateMultipleOverwrite(stateSlice []*dtos.AddressState) error
AddValidatorKeys(validators *dtosc.ValidatorKeys) error
GenerateBlocksUntilEpochIsReached(targetEpoch int32) error
ForceUpdateValidatorStatistics() error
Expand Down
10 changes: 10 additions & 0 deletions testscommon/simulatorHandlerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ type SimulatorHandlerMock struct {
AddValidatorKeysCalled func(validatorsPrivateKeys [][]byte) error
GenerateBlocksUntilEpochIsReachedCalled func(targetEpoch int32) error
ForceResetValidatorStatisticsCacheCalled func() error
RemoveAccountsCalled func(addresses []string) error
}

// RemoveAccounts -
func (mock *SimulatorHandlerMock) RemoveAccounts(addresses []string) error {
if mock.RemoveAccountsCalled != nil {
return mock.RemoveAccountsCalled(addresses)
}

return nil
}

// GetInitialWalletKeys -
Expand Down

0 comments on commit ed28c4d

Please sign in to comment.