diff --git a/actions/contract_call.go b/actions/contract_call.go index 13df9f2..1f5290f 100644 --- a/actions/contract_call.go +++ b/actions/contract_call.go @@ -21,7 +21,10 @@ import ( var _ chain.Action = (*ContractCall)(nil) -const MaxCallDataSize = units.MiB +const ( + MaxCallDataSize = units.MiB + MaxResultSizeLimit = units.MiB +) type StateKeyPermission struct { Key string @@ -30,20 +33,20 @@ type StateKeyPermission struct { type ContractCall struct { // contract is the address of the contract to be called - ContractAddress codec.Address `serialize:"true" json:"contractAddress"` + ContractAddress codec.Address `json:"contractAddress"` // Amount are transferred to [To]. - Value uint64 `serialize:"true" json:"value"` + Value uint64 `json:"value"` // Function is the name of the function to call on the contract. - Function string `serialize:"true" json:"function"` + Function string `json:"function"` // CallData are the serialized parameters to be passed to the contract. - CallData []byte `serialize:"true" json:"calldata"` + CallData []byte `json:"calldata"` - SpecifiedStateKeys []StateKeyPermission `serialize:"true" json:"statekeys"` + SpecifiedStateKeys []StateKeyPermission `json:"statekeys"` - Fuel uint64 `serialize:"true" json:"fuel"` + Fuel uint64 `json:"fuel"` r *runtime.WasmRuntime } @@ -68,7 +71,7 @@ func (t *ContractCall) Execute( actor codec.Address, _ ids.ID, ) (codec.Typed, error) { - resutBytes, err := t.r.CallContract(ctx, &runtime.CallInfo{ + callInfo := &runtime.CallInfo{ Contract: t.ContractAddress, Actor: actor, State: &storage.ContractStateManager{Mutable: mu}, @@ -77,11 +80,13 @@ func (t *ContractCall) Execute( Timestamp: uint64(timestamp), Fuel: t.Fuel, Value: t.Value, - }) + } + resultBytes, err := t.r.CallContract(ctx, callInfo) if err != nil { return nil, err } - return &ContractCallResult{Value: resutBytes}, nil + consumedFuel := t.Fuel - callInfo.RemainingFuel() + return &ContractCallResult{Value: resultBytes, ConsumedFuel: consumedFuel}, nil } func (t *ContractCall) ComputeUnits(chain.Rules) uint64 { @@ -138,7 +143,8 @@ func UnmarshalCallContract(r *runtime.WasmRuntime) func(p *codec.Packer) (chain. var _ codec.Typed = (*ContractCallResult)(nil) type ContractCallResult struct { - Value []byte `serialize:"true" json:"value"` + Value []byte `serialize:"true" json:"value"` + ConsumedFuel uint64 `serialize:"true" json:"consumedfuel"` } func (*ContractCallResult) GetTypeID() uint8 { diff --git a/actions/contract_deploy.go b/actions/contract_deploy.go index 3644211..f27a388 100644 --- a/actions/contract_deploy.go +++ b/actions/contract_deploy.go @@ -24,8 +24,8 @@ var _ chain.Action = (*ContractDeploy)(nil) const MAXCREATIONSIZE = units.MiB type ContractDeploy struct { - ContractID runtime.ContractID `serialize:"true" json:"contractID"` - CreationInfo []byte `serialize:"true" json:"creationInfo"` + ContractID runtime.ContractID `json:"contractID"` + CreationInfo []byte `json:"creationInfo"` address codec.Address } diff --git a/actions/register_validator_stake_test.go b/actions/register_validator_stake_test.go index 36a16a2..ad1b940 100644 --- a/actions/register_validator_stake_test.go +++ b/actions/register_validator_stake_test.go @@ -15,7 +15,6 @@ import ( "github.com/ava-labs/hypersdk/auth" "github.com/ava-labs/hypersdk/chain/chaintest" - "github.com/ava-labs/hypersdk/cli" "github.com/ava-labs/hypersdk/codec" "github.com/ava-labs/hypersdk/crypto/bls" "github.com/ava-labs/hypersdk/state" @@ -183,7 +182,7 @@ func BenchmarkRegisterValidatorStake(b *testing.B) { registerValidatorStakeBenchmark.Run(ctx, b) } -func generateStakeInfoAndSignature(nodeID ids.NodeID, stakeStartBlock, stakeEndBlock, stakedAmount, delegationFeeRate uint64) ([]byte, []byte, *cli.PrivateKey, []byte) { +func generateStakeInfoAndSignature(nodeID ids.NodeID, stakeStartBlock, stakeEndBlock, stakedAmount, delegationFeeRate uint64) ([]byte, []byte, *auth.PrivateKey, []byte) { blsBase64Key, err := base64.StdEncoding.DecodeString("MdWjv5OOW/p/JKt673vYxwROsfTCO7iZ2jwWCnY18hw=") if err != nil { panic(err) @@ -192,7 +191,7 @@ func generateStakeInfoAndSignature(nodeID ids.NodeID, stakeStartBlock, stakeEndB if err != nil { panic(err) } - blsPrivateKey := &cli.PrivateKey{ + blsPrivateKey := &auth.PrivateKey{ Address: auth.NewBLSAddress(bls.PublicFromPrivateKey(secretKey)), Bytes: bls.PrivateKeyToBytes(secretKey), } diff --git a/cmd/nuklai-cli/cmd/action.go b/cmd/nuklai-cli/cmd/action.go index 84cd13f..2c2789a 100644 --- a/cmd/nuklai-cli/cmd/action.go +++ b/cmd/nuklai-cli/cmd/action.go @@ -17,6 +17,7 @@ import ( "github.com/nuklai/nuklaivm/actions" "github.com/nuklai/nuklaivm/consts" "github.com/nuklai/nuklaivm/storage" + "github.com/nuklai/nuklaivm/vm" "github.com/spf13/cobra" "github.com/status-im/keycard-go/hexutils" @@ -32,6 +33,8 @@ import ( nutils "github.com/nuklai/nuklaivm/utils" ) +var errUnexpectedSimulateActionsOutput = errors.New("returned output from SimulateActions was not actions.Result") + var actionCmd = &cobra.Command{ Use: "action", RunE: func(*cobra.Command, []string) error { @@ -168,18 +171,34 @@ var callCmd = &cobra.Command{ ContractAddress: contractAddress, Value: amount, Function: function, + Fuel: uint64(1000000000), } - specifiedStateKeysSet, fuel, err := bcli.Simulate(ctx, *action, priv.Address) + actionSimulationResults, err := cli.SimulateActions(ctx, chain.Actions{action}, priv.Address) if err != nil { return err } + if len(actionSimulationResults) != 1 { + return fmt.Errorf("unexpected number of returned actions. One action expected, %d returned", len(actionSimulationResults)) + } + actionSimulationResult := actionSimulationResults[0] + + rtx := codec.NewReader(actionSimulationResult.Output, len(actionSimulationResult.Output)) - action.SpecifiedStateKeys = make([]actions.StateKeyPermission, 0, len(specifiedStateKeysSet)) - for key, value := range specifiedStateKeysSet { + simulationResultOutput, err := (*vm.OutputParser).Unmarshal(rtx) + if err != nil { + return err + } + simulationResult, ok := simulationResultOutput.(*actions.ContractCallResult) + if !ok { + return errUnexpectedSimulateActionsOutput + } + + action.SpecifiedStateKeys = make([]actions.StateKeyPermission, 0, len(actionSimulationResult.StateKeys)) + for key, value := range actionSimulationResult.StateKeys { action.SpecifiedStateKeys = append(action.SpecifiedStateKeys, actions.StateKeyPermission{Key: key, Permission: value}) } - action.Fuel = fuel + action.Fuel = simulationResult.ConsumedFuel // Confirm action cont, err := prompt.Continue() @@ -189,9 +208,8 @@ var callCmd = &cobra.Command{ // Generate transaction result, _, err := sendAndWait(ctx, []chain.Action{action}, cli, bcli, ws, factory) - if result != nil && result.Success { - utils.Outf("{{green}}fee consumed:{{/}} %s\n", nutils.FormatBalance(result.Fee, consts.Decimals)) + if result != nil && result.Success { utils.Outf(hexutils.BytesToHex(result.Outputs[0]) + "\n") switch function { case "balance": @@ -201,7 +219,7 @@ var callCmd = &cobra.Command{ if err != nil { return err } - utils.Outf("%s\n", nutils.FormatBalance(intValue, consts.Decimals)) + utils.Outf("%s\n", utils.FormatBalance(intValue)) } case "get_value": { diff --git a/cmd/nuklai-cli/cmd/handler.go b/cmd/nuklai-cli/cmd/handler.go index 5a31ffa..dee168d 100644 --- a/cmd/nuklai-cli/cmd/handler.go +++ b/cmd/nuklai-cli/cmd/handler.go @@ -154,7 +154,7 @@ func (h *Handler) BalanceAsset(checkAllChains bool, isNFT bool, printBalance fun } func (h *Handler) DefaultActor() ( - ids.ID, *cli.PrivateKey, chain.AuthFactory, + ids.ID, *auth.PrivateKey, chain.AuthFactory, *jsonrpc.JSONRPCClient, *vm.JSONRPCClient, *ws.WebSocketClient, error, ) { addr, priv, err := h.h.GetDefaultKey(true) @@ -189,7 +189,7 @@ func (h *Handler) DefaultActor() ( return ids.Empty, nil, nil, nil, nil, nil, err } // For [defaultActor], we always send requests to the first returned URI. - return chainID, &cli.PrivateKey{ + return chainID, &auth.PrivateKey{ Address: addr, Bytes: priv, }, factory, jcli, diff --git a/cmd/nuklai-cli/cmd/key.go b/cmd/nuklai-cli/cmd/key.go index 4bec1ae..dca1a3e 100644 --- a/cmd/nuklai-cli/cmd/key.go +++ b/cmd/nuklai-cli/cmd/key.go @@ -17,7 +17,6 @@ import ( "github.com/spf13/cobra" "github.com/ava-labs/hypersdk/auth" - "github.com/ava-labs/hypersdk/cli" "github.com/ava-labs/hypersdk/cli/prompt" "github.com/ava-labs/hypersdk/codec" "github.com/ava-labs/hypersdk/crypto/bls" @@ -97,7 +96,7 @@ var importKeyCmd = &cobra.Command{ keyType := args[0] keyInput := args[1] - var priv *cli.PrivateKey + var priv *auth.PrivateKey // Check if the provided argument is a file path or an encoded string if _, err := os.Stat(keyInput); err == nil { @@ -274,14 +273,14 @@ func getKeyType(addr codec.Address) (string, error) { } } -func generatePrivateKey(k string) (*cli.PrivateKey, error) { +func generatePrivateKey(k string) (*auth.PrivateKey, error) { switch k { case ed25519Key: p, err := ed25519.GeneratePrivateKey() if err != nil { return nil, err } - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewED25519Address(p.PublicKey()), Bytes: p[:], }, nil @@ -290,7 +289,7 @@ func generatePrivateKey(k string) (*cli.PrivateKey, error) { if err != nil { return nil, err } - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewSECP256R1Address(p.PublicKey()), Bytes: p[:], }, nil @@ -299,7 +298,7 @@ func generatePrivateKey(k string) (*cli.PrivateKey, error) { if err != nil { return nil, err } - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewBLSAddress(bls.PublicFromPrivateKey(p)), Bytes: bls.PrivateKeyToBytes(p), }, nil @@ -308,7 +307,7 @@ func generatePrivateKey(k string) (*cli.PrivateKey, error) { } } -func loadPrivateKeyFromPath(k string, path string) (*cli.PrivateKey, error) { +func loadPrivateKeyFromPath(k string, path string) (*auth.PrivateKey, error) { switch k { case ed25519Key: p, err := utils.LoadBytes(path, ed25519.PrivateKeyLen) @@ -316,7 +315,7 @@ func loadPrivateKeyFromPath(k string, path string) (*cli.PrivateKey, error) { return nil, err } pk := ed25519.PrivateKey(p) - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewED25519Address(pk.PublicKey()), Bytes: p, }, nil @@ -326,7 +325,7 @@ func loadPrivateKeyFromPath(k string, path string) (*cli.PrivateKey, error) { return nil, err } pk := secp256r1.PrivateKey(p) - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewSECP256R1Address(pk.PublicKey()), Bytes: p, }, nil @@ -340,7 +339,7 @@ func loadPrivateKeyFromPath(k string, path string) (*cli.PrivateKey, error) { if err != nil { return nil, err } - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewBLSAddress(bls.PublicFromPrivateKey(privKey)), Bytes: p, }, nil @@ -350,7 +349,7 @@ func loadPrivateKeyFromPath(k string, path string) (*cli.PrivateKey, error) { } // loadPrivateKeyFromString loads a private key from a base64 string. -func loadPrivateKeyFromString(k, keyStr string) (*cli.PrivateKey, error) { +func loadPrivateKeyFromString(k, keyStr string) (*auth.PrivateKey, error) { var decodedKey []byte var err error @@ -364,13 +363,13 @@ func loadPrivateKeyFromString(k, keyStr string) (*cli.PrivateKey, error) { switch k { case ed25519Key: pk := ed25519.PrivateKey(decodedKey) - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewED25519Address(pk.PublicKey()), Bytes: decodedKey, }, nil case secp256r1Key: pk := secp256r1.PrivateKey(decodedKey) - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewSECP256R1Address(pk.PublicKey()), Bytes: decodedKey, }, nil @@ -379,7 +378,7 @@ func loadPrivateKeyFromString(k, keyStr string) (*cli.PrivateKey, error) { if err != nil { return nil, fmt.Errorf("failed to load BLS private key: %w", err) } - return &cli.PrivateKey{ + return &auth.PrivateKey{ Address: auth.NewBLSAddress(bls.PublicFromPrivateKey(pk)), Bytes: bls.PrivateKeyToBytes(pk), }, nil diff --git a/cmd/nuklai-cli/cmd/spam.go b/cmd/nuklai-cli/cmd/spam.go index 2485679..4697faf 100644 --- a/cmd/nuklai-cli/cmd/spam.go +++ b/cmd/nuklai-cli/cmd/spam.go @@ -6,22 +6,16 @@ package cmd import ( "context" - "github.com/ava-labs/avalanchego/ids" "github.com/nuklai/nuklaivm/actions" - "github.com/nuklai/nuklaivm/consts" + "github.com/nuklai/nuklaivm/storage" "github.com/nuklai/nuklaivm/vm" "github.com/spf13/cobra" "github.com/ava-labs/hypersdk/api/ws" "github.com/ava-labs/hypersdk/auth" "github.com/ava-labs/hypersdk/chain" - "github.com/ava-labs/hypersdk/cli" "github.com/ava-labs/hypersdk/codec" - "github.com/ava-labs/hypersdk/crypto/bls" - "github.com/ava-labs/hypersdk/crypto/ed25519" - "github.com/ava-labs/hypersdk/crypto/secp256r1" "github.com/ava-labs/hypersdk/pubsub" - "github.com/ava-labs/hypersdk/utils" ) type SpamHelper struct { @@ -30,27 +24,10 @@ type SpamHelper struct { ws *ws.WebSocketClient } -func (sh *SpamHelper) CreateAccount() (*cli.PrivateKey, error) { +func (sh *SpamHelper) CreateAccount() (*auth.PrivateKey, error) { return generatePrivateKey(sh.keyType) } -func (*SpamHelper) GetFactory(pk *cli.PrivateKey) (chain.AuthFactory, error) { - switch pk.Address[0] { - case auth.ED25519ID: - return auth.NewED25519Factory(ed25519.PrivateKey(pk.Bytes)), nil - case auth.SECP256R1ID: - return auth.NewSECP256R1Factory(secp256r1.PrivateKey(pk.Bytes)), nil - case auth.BLSID: - p, err := bls.PrivateKeyFromBytes(pk.Bytes) - if err != nil { - return nil, err - } - return auth.NewBLSFactory(p), nil - default: - return nil, ErrInvalidKeyType - } -} - func (sh *SpamHelper) CreateClient(uri string) error { sh.cli = vm.NewJSONRPCClient(uri) ws, err := ws.NewWebSocketClient(uri, ws.DefaultHandshakeTimeout, pubsub.MaxPendingMessages, pubsub.MaxReadMessageSize) @@ -65,18 +42,11 @@ func (sh *SpamHelper) GetParser(ctx context.Context) (chain.Parser, error) { return sh.cli.Parser(ctx) } -func (sh *SpamHelper) LookupBalance(choice int, address codec.Address) (uint64, error) { - balance, err := sh.cli.Balance(context.TODO(), address.String(), ids.Empty.String()) +func (sh *SpamHelper) LookupBalance(address codec.Address) (uint64, error) { + balance, err := sh.cli.Balance(context.TODO(), address.String(), storage.NAIAddress.String()) if err != nil { return 0, err } - utils.Outf( - "%d) {{cyan}}address:{{/}} %s {{cyan}}balance:{{/}} %s %s\n", - choice, - address, - utils.FormatBalance(balance), - consts.Symbol, - ) return balance, err } @@ -104,6 +74,7 @@ var runSpamCmd = &cobra.Command{ return checkKeyType(args[0]) }, RunE: func(_ *cobra.Command, args []string) error { - return handler.Root().Spam(&SpamHelper{keyType: args[0]}) + ctx := context.Background() + return handler.Root().Spam(ctx, &SpamHelper{keyType: args[0]}, false) }, } diff --git a/go.mod b/go.mod index f0eddbe..814248e 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/nuklai/nuklaivm go 1.22.5 require ( - github.com/ava-labs/avalanchego v1.11.12-rc.2 - github.com/ava-labs/hypersdk v0.0.18-0.20241002025558-abf0ce962501 + github.com/ava-labs/avalanchego v1.11.12-rc.2.0.20241001202925-f03745d187d0 + github.com/ava-labs/hypersdk v0.0.18-0.20241018181853-22241f53b9ff github.com/fatih/color v1.13.0 github.com/gorilla/mux v1.8.0 github.com/manifoldco/promptui v0.9.0 diff --git a/go.sum b/go.sum index 8311d15..4a12dbf 100644 --- a/go.sum +++ b/go.sum @@ -58,12 +58,12 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/ava-labs/avalanchego v1.11.12-rc.2 h1:H1C0gsTOtwMD3qrouEqry0hfoBIC//9lEiDh/AvpaaY= -github.com/ava-labs/avalanchego v1.11.12-rc.2/go.mod h1:yFx3V31Jy9NFa8GZlgGnwiVf8KGjeF2+Uc99l9Scd/8= +github.com/ava-labs/avalanchego v1.11.12-rc.2.0.20241001202925-f03745d187d0 h1:r/vgyq3kfRwHbaBbVRZUcS5WZPHdpWODvZNLvw5udKc= +github.com/ava-labs/avalanchego v1.11.12-rc.2.0.20241001202925-f03745d187d0/go.mod h1:yFlG98ykZzMHSXazQzbpfTw1D0pt/p/WEjvuZ045W1I= github.com/ava-labs/coreth v0.13.8 h1:f14X3KgwHl9LwzfxlN6S4bbn5VA2rhEsNnHaRLSTo/8= github.com/ava-labs/coreth v0.13.8/go.mod h1:t3BSv/eQv0AlDPMfEDCMMoD/jq1RkUsbFzQAFg5qBcE= -github.com/ava-labs/hypersdk v0.0.18-0.20241002025558-abf0ce962501 h1:ljM/yltaNn3MwSVqLr8HmuSTHoGyEoL94fmn/uhdXR8= -github.com/ava-labs/hypersdk v0.0.18-0.20241002025558-abf0ce962501/go.mod h1:VEGvgLewOp4NUD8KHyGroL8vTbr6RwE/6vOKlZ4aWqQ= +github.com/ava-labs/hypersdk v0.0.18-0.20241018181853-22241f53b9ff h1:NKg1WyyU3nhqs39K3MioGe3Z8WqUH7uw9TXlzaBjGak= +github.com/ava-labs/hypersdk v0.0.18-0.20241018181853-22241f53b9ff/go.mod h1:tB6QfnNlWmbNj6erbYkAnqzZNNy4ETRpAlPtm7sTwio= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index c5634cb..e6dae6e 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -56,7 +56,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte { // Import HyperSDK e2e test coverage and inject VMWithContracts name // and workload factory to orchestrate the test. - he2e.SetWorkload(consts.Name, workloadFactory, parser, expectedABI) + he2e.SetWorkload(consts.Name, workloadFactory, expectedABI, parser, nil, nil) tc := e2e.NewTestContext() diff --git a/vm/client.go b/vm/client.go index d9f4066..69f3cd2 100644 --- a/vm/client.go +++ b/vm/client.go @@ -5,14 +5,12 @@ package vm import ( "context" - "encoding/hex" "encoding/json" "strings" "sync" "time" "github.com/ava-labs/avalanchego/ids" - "github.com/nuklai/nuklaivm/actions" "github.com/nuklai/nuklaivm/consts" "github.com/nuklai/nuklaivm/emission" "github.com/nuklai/nuklaivm/genesis" @@ -22,7 +20,6 @@ import ( "github.com/ava-labs/hypersdk/chain" "github.com/ava-labs/hypersdk/codec" "github.com/ava-labs/hypersdk/requester" - "github.com/ava-labs/hypersdk/state" "github.com/ava-labs/hypersdk/utils" ) @@ -276,15 +273,15 @@ func (p *Parser) Rules(_ int64) chain.Rules { return p.genesis.Rules } -func (*Parser) ActionRegistry() chain.ActionRegistry { +func (*Parser) ActionCodec() *codec.TypeParser[chain.Action] { return ActionParser } -func (*Parser) OutputRegistry() chain.OutputRegistry { +func (*Parser) OutputCodec() *codec.TypeParser[codec.Typed] { return OutputParser } -func (*Parser) AuthRegistry() chain.AuthRegistry { +func (*Parser) AuthCodec() *codec.TypeParser[chain.Auth] { return AuthParser } @@ -304,26 +301,3 @@ func CreateParser(genesisBytes []byte) (chain.Parser, error) { } return NewParser(&genesis), nil } - -func (cli *JSONRPCClient) Simulate(ctx context.Context, callTx actions.ContractCall, actor codec.Address) (state.Keys, uint64, error) { - resp := new(SimulateCallTxReply) - err := cli.requester.SendRequest( - ctx, - "simulateCallContractTx", - &SimulateCallTxArgs{CallTx: callTx, Actor: actor}, - resp, - ) - if err != nil { - return nil, 0, err - } - result := state.Keys{} - for _, entry := range resp.StateKeys { - hexBytes, err := hex.DecodeString(entry.HexKey) - if err != nil { - return nil, 0, err - } - - result.Add(string(hexBytes), state.Permissions(entry.Permissions)) - } - return result, resp.FuelConsumed, nil -} diff --git a/vm/server.go b/vm/server.go index b37aea4..f67f0d1 100644 --- a/vm/server.go +++ b/vm/server.go @@ -4,12 +4,9 @@ package vm import ( - "context" - "encoding/hex" "net/http" "github.com/ava-labs/avalanchego/ids" - "github.com/nuklai/nuklaivm/actions" "github.com/nuklai/nuklaivm/consts" "github.com/nuklai/nuklaivm/emission" "github.com/nuklai/nuklaivm/genesis" @@ -18,8 +15,6 @@ import ( "github.com/ava-labs/hypersdk/api" "github.com/ava-labs/hypersdk/codec" - "github.com/ava-labs/hypersdk/state" - "github.com/ava-labs/hypersdk/x/contracts/runtime" ) const JSONRPCEndpoint = "/nuklaiapi" @@ -360,50 +355,3 @@ func (j *JSONRPCServer) UserStake(req *http.Request, args *UserStakeArgs, reply reply.OwnerAddress = ownerAddress.String() return nil } - -type SimulateCallTxArgs struct { - CallTx actions.ContractCall `json:"callTx"` - Actor codec.Address `json:"actor"` -} - -type SimulateStateKey struct { - HexKey string `json:"hex"` - Permissions byte `json:"perm"` -} -type SimulateCallTxReply struct { - StateKeys []SimulateStateKey `json:"stateKeys"` - FuelConsumed uint64 `json:"fuel"` -} - -func (j *JSONRPCServer) SimulateCallContractTx(req *http.Request, args *SimulateCallTxArgs, reply *SimulateCallTxReply) (err error) { - stateKeys, fuelConsumed, err := j.simulate(req.Context(), args.CallTx, args.Actor) - if err != nil { - return err - } - reply.StateKeys = make([]SimulateStateKey, 0, len(stateKeys)) - for key, permission := range stateKeys { - reply.StateKeys = append(reply.StateKeys, SimulateStateKey{HexKey: hex.EncodeToString([]byte(key)), Permissions: byte(permission)}) - } - reply.FuelConsumed = fuelConsumed - return nil -} - -func (j *JSONRPCServer) simulate(ctx context.Context, t actions.ContractCall, actor codec.Address) (state.Keys, uint64, error) { - currentState, err := j.vm.ImmutableState(ctx) - if err != nil { - return nil, 0, err - } - recorder := storage.NewRecorder(currentState) - startFuel := uint64(1000000000) - callInfo := &runtime.CallInfo{ - Contract: t.ContractAddress, - Actor: actor, - State: &storage.ContractStateManager{Mutable: recorder}, - FunctionName: t.Function, - Params: t.CallData, - Fuel: startFuel, - Value: t.Value, - } - _, err = wasmRuntime.CallContract(ctx, callInfo) - return recorder.GetStateKeys(), startFuel - callInfo.RemainingFuel(), err -}