Skip to content

Commit

Permalink
Merge pull request #101 from make-software/CSDK/implement_account_put…
Browse files Browse the repository at this point in the history
…_transaction

Implement account put transaction
  • Loading branch information
ZhmakAS authored Jul 17, 2024
2 parents 803350e + abd4b0d commit 722d5c6
Show file tree
Hide file tree
Showing 26 changed files with 1,005 additions and 422 deletions.
2 changes: 1 addition & 1 deletion casper/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type (
)

var (
DefaultHeader = types.DefaultHeader
DefaultHeader = types.DefaultDeployHeader
MakeDeploy = types.MakeDeploy
StandardPayment = types.StandardPayment
)
Expand Down
5 changes: 3 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,11 @@ type ClientInformational interface {
GetDelegatorRewardByBlockHeight(ctx context.Context, validator, delegator keypair.PublicKey, height uint64) (InfoGetRewardResult, error)
}

// ClientTransactional contains the description of account_put_deploy,
// the only means by which users can send their compiled Wasm (as part of a Deploy) to a node on a Casper network.
// ClientTransactional contains the description of account_put_deploy, account_put_transaction
// the only means by which users can send their compiled Wasm (as part of a Deploy or TransactionV1) to a node on a Casper network.
type ClientTransactional interface {
PutDeploy(ctx context.Context, deploy types.Deploy) (PutDeployResult, error)
PutTransactionV1(ctx context.Context, transaction types.TransactionV1) (PutTransactionResult, error)
}

// Client interface represent full RPC client that includes all possible queries.
Expand Down
5 changes: 5 additions & 0 deletions rpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
MethodGetReward Method = "info_get_reward"
MethodGetPeers Method = "info_get_peers"
MethodPutDeploy Method = "account_put_deploy"
MethodPutTransaction Method = "account_put_transaction"
MethodSpeculativeExec Method = "speculative_exec"
MethodQueryBalance Method = "query_balance"
MethodQueryBalanceDetails Method = "query_balance_details"
Expand Down Expand Up @@ -149,6 +150,10 @@ type PutDeployRequest struct {
Deploy types.Deploy `json:"deploy"`
}

type PutTransactionRequest struct {
Transaction types.TransactionWrapper `json:"transaction"`
}

type BlockIdentifier struct {
Hash *string `json:"Hash,omitempty"`
Height *uint64 `json:"Height,omitempty"`
Expand Down
19 changes: 15 additions & 4 deletions rpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,6 @@ type InfoGetStatusResult struct {
rawJSON json.RawMessage
}

func (b PutDeployResult) GetRawJSON() json.RawMessage {
return b.rawJSON
}

// NodeNextUpgrade contains the information about the next protocol upgrade.
type NodeNextUpgrade struct {
//The first era to which the associated protocol version applies.
Expand All @@ -458,6 +454,21 @@ type PutDeployResult struct {
rawJSON json.RawMessage
}

func (p PutDeployResult) GetRawJSON() json.RawMessage {
return p.rawJSON
}

type PutTransactionResult struct {
ApiVersion string `json:"api_version"`
TransactionHash types.TransactionHash `json:"transaction_hash"`

rawJSON json.RawMessage
}

func (p PutTransactionResult) GetRawJSON() json.RawMessage {
return p.rawJSON
}

func (b InfoGetStatusResult) GetRawJSON() json.RawMessage {
return b.rawJSON
}
Expand Down
20 changes: 18 additions & 2 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *client) GetTransactionByTransactionHash(ctx context.Context, transactio
var result infoGetTransactionResultV1Compatible
resp, err := c.processRequest(ctx, MethodGetTransaction, ParamTransactionHash{
TransactionHash: types.TransactionHash{
Transaction: &hash,
TransactionV1: &hash,
},
}, &result)
if err != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (c *client) GetTransactionFinalizedApprovalByTransactionHash(ctx context.Co
var result infoGetTransactionResultV1Compatible
resp, err := c.processRequest(ctx, MethodGetTransaction, ParamTransactionHash{
TransactionHash: types.TransactionHash{
Transaction: &hash,
TransactionV1: &hash,
},
FinalizedApprovals: &[]bool{true}[0],
}, &result)
Expand Down Expand Up @@ -639,6 +639,22 @@ func (c *client) PutDeploy(ctx context.Context, deploy types.Deploy) (PutDeployR
return result, nil
}

func (c *client) PutTransactionV1(ctx context.Context, transaction types.TransactionV1) (PutTransactionResult, error) {
var result PutTransactionResult

resp, err := c.processRequest(ctx, MethodPutTransaction, PutTransactionRequest{
Transaction: types.TransactionWrapper{
TransactionV1: &transaction,
},
}, &result)
if err != nil {
return PutTransactionResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) QueryLatestBalance(ctx context.Context, identifier PurseIdentifier) (QueryBalanceResult, error) {
var result QueryBalanceResult

Expand Down
4 changes: 2 additions & 2 deletions sse/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (t *TransactionProcessedEvent) UnmarshalJSON(data []byte) error {
return err
}

if transactionEvent.TransactionProcessedPayload.TransactionHash.Transaction != nil ||
if transactionEvent.TransactionProcessedPayload.TransactionHash.TransactionV1 != nil ||
transactionEvent.TransactionProcessedPayload.TransactionHash.Deploy != nil {
*t = transactionEvent
return nil
Expand Down Expand Up @@ -264,7 +264,7 @@ func (t *TransactionExpiredEvent) UnmarshalJSON(data []byte) error {
return err
}

if transactionEvent.TransactionExpiredPayload.TransactionHash.Transaction != nil ||
if transactionEvent.TransactionExpiredPayload.TransactionHash.TransactionV1 != nil ||
transactionEvent.TransactionExpiredPayload.TransactionHash.Deploy != nil {
*t = transactionEvent
return nil
Expand Down
3 changes: 3 additions & 0 deletions tests/data/keys/docker-nctl-rc3-secret.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIHRZr1HEgKVbgchuatwA7dCWDWB7QZe+bpDb5dguIyLE
-----END PRIVATE KEY-----
Binary file added tests/data/wasm/cep18-rc3.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/rpc/integration/put_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_PutDeploy(t *testing.T) {
facetKeys, err := casper.NewED25519PrivateKeyFromPEMFile("../../data/keys/docker-nctl-secret.pem")
require.NoError(t, err)
require.NoError(t, err)
header := types.DefaultHeader()
header := types.DefaultDeployHeader()
header.ChainName = "casper-net-1"
header.Account = facetKeys.PublicKey()
require.NoError(t, err)
Expand All @@ -46,7 +46,7 @@ func Test_PutDeploy(t *testing.T) {
}

deploy, err := types.MakeDeploy(header, payment, session)
err = deploy.SignDeploy(facetKeys)
err = deploy.Sign(facetKeys)
require.NoError(t, err)

rpcClient := rpc.NewClient(rpc.NewHttpHandler("http://127.0.0.1:11101/rpc", http.DefaultClient))
Expand Down
90 changes: 90 additions & 0 deletions tests/rpc/integration/put_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//go:build integration
// +build integration

package integration

import (
"context"
"encoding/hex"
"log"
"math/big"
"net/http"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/casper"
"github.com/make-software/casper-go-sdk/rpc"
"github.com/make-software/casper-go-sdk/types"
"github.com/make-software/casper-go-sdk/types/clvalue"
)

func Test_PutTransaction(t *testing.T) {
keys, err := casper.NewED25519PrivateKeyFromPEMFile("../../data/keys/docker-nctl-rc3-secret.pem")
require.NoError(t, err)

pubKey := keys.PublicKey()

header := types.TransactionV1Header{
ChainName: "casper-net-1",
Timestamp: types.Timestamp(time.Now().UTC()),
TTL: 18000000000,
InitiatorAddr: types.InitiatorAddr{
PublicKey: &pubKey,
},
PricingMode: types.PricingMode{
Fixed: &types.FixedMode{
GasPriceTolerance: 3,
},
},
}

moduleBytes, err := os.ReadFile("../../data/wasm/cep18-rc3.wasm")
require.NoError(t, err)

args := &types.Args{}
args.AddArgument("name", *clvalue.NewCLString("Test")).
AddArgument("symbol", *clvalue.NewCLString("test")).
AddArgument("decimals", *clvalue.NewCLUint8(9)).
AddArgument("total_supply", *clvalue.NewCLUInt256(big.NewInt(1_000_000_000_000_000))).
AddArgument("events_mode", *clvalue.NewCLUint8(2)).
AddArgument("enable_mint_burn", *clvalue.NewCLUint8(1))

body := types.TransactionV1Body{
Args: args,
Target: types.TransactionTarget{
Session: &types.SessionTarget{
ModuleBytes: hex.EncodeToString(moduleBytes),
Runtime: types.TransactionRuntimeVmCasperV1,
},
},
TransactionEntryPoint: types.TransactionEntryPoint{
Call: &struct{}{},
},
TransactionScheduling: types.TransactionScheduling{
Standard: &struct{}{},
},
TransactionCategory: 2,
}

transaction, err := types.MakeTransactionV1(header, body)
err = transaction.Sign(keys)
require.NoError(t, err)

rpcClient := rpc.NewClient(rpc.NewHttpHandler("http://127.0.0.1:11101/rpc", http.DefaultClient))
res, err := rpcClient.PutTransactionV1(context.Background(), *transaction)
require.NoError(t, err)
assert.NotEmpty(t, res.TransactionHash.TransactionV1)
assert.NoError(t, transaction.Validate())

log.Println("TransactionV1 submitted:", res.TransactionHash.TransactionV1)

time.Sleep(time.Second * 10)
transactionRes, err := rpcClient.GetTransactionByTransactionHash(context.Background(), res.TransactionHash.TransactionV1.ToHex())
require.NoError(t, err)
assert.NotEmpty(t, transactionRes.Transaction)
assert.NotEmpty(t, transactionRes.ExecutionInfo)
}
16 changes: 8 additions & 8 deletions tests/rpc/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ func Test_DefaultClient_GetTransaction_Example(t *testing.T) {
result, err := client.GetTransactionByTransactionHash(context.Background(), "0009ea4441f4700325d9c38b0b6df415537596e1204abe4f6a94b6996aebf2f1")
require.NoError(t, err)
assert.NotEmpty(t, result.APIVersion)
assert.NotEmpty(t, result.Transaction.TransactionHash)
assert.NotEmpty(t, result.Transaction.TransactionHeader)
assert.NotEmpty(t, result.Transaction.TransactionHeader.TTL)
assert.NotEmpty(t, result.Transaction.TransactionHeader.ChainName)
assert.NotEmpty(t, result.Transaction.TransactionHeader.PricingMode)
assert.NotEmpty(t, result.Transaction.TransactionHeader.InitiatorAddr)
assert.NotEmpty(t, result.Transaction.TransactionBody.Target)
assert.NotEmpty(t, result.Transaction.TransactionBody.TransactionScheduling)
assert.NotEmpty(t, result.Transaction.Hash)
assert.NotEmpty(t, result.Transaction.Header)
assert.NotEmpty(t, result.Transaction.Header.TTL)
assert.NotEmpty(t, result.Transaction.Header.ChainName)
assert.NotEmpty(t, result.Transaction.Header.PricingMode)
assert.NotEmpty(t, result.Transaction.Header.InitiatorAddr)
assert.NotEmpty(t, result.Transaction.Body.Target)
assert.NotEmpty(t, result.Transaction.Body.Scheduling)
assert.NotEmpty(t, result.ExecutionInfo.ExecutionResult.Initiator)
assert.NotEmpty(t, result.ExecutionInfo.ExecutionResult.Effects)
assert.NotEmpty(t, result.Transaction.Approvals)
Expand Down
8 changes: 4 additions & 4 deletions tests/sse/events_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func Test_RawEvent_ParseAsTransactionExpiredEvent(t *testing.T) {
require.NotEmpty(t, res.TransactionExpiredPayload.TransactionHash)

if tc.isTransaction {
require.NotEmpty(t, res.TransactionExpiredPayload.TransactionHash.Transaction)
require.NotEmpty(t, res.TransactionExpiredPayload.TransactionHash.TransactionV1)
} else {
require.NotEmpty(t, res.TransactionExpiredPayload.TransactionHash.Deploy)
}
Expand Down Expand Up @@ -204,8 +204,8 @@ func Test_RawEvent_ParseAsTransactionAcceptedEvent(t *testing.T) {
}

require.NotEmpty(t, res.TransactionAcceptedPayload.Transaction)
require.NotEmpty(t, res.TransactionAcceptedPayload.Transaction.TransactionHeader)
require.NotEmpty(t, res.TransactionAcceptedPayload.Transaction.TransactionBody)
require.NotEmpty(t, res.TransactionAcceptedPayload.Transaction.Header)
require.NotEmpty(t, res.TransactionAcceptedPayload.Transaction.Body)
})
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func Test_RawEvent_ParseAsTransactionProcessedEvent(t *testing.T) {
require.NotEmpty(t, res.TransactionProcessedPayload.TransactionHash)

if tc.isTransaction {
require.NotEmpty(t, res.TransactionProcessedPayload.TransactionHash.Transaction)
require.NotEmpty(t, res.TransactionProcessedPayload.TransactionHash.TransactionV1)
require.NotEmpty(t, res.TransactionProcessedPayload.Messages)
} else {
require.NotEmpty(t, res.TransactionProcessedPayload.TransactionHash.Deploy)
Expand Down
4 changes: 2 additions & 2 deletions tests/types/deploy_make_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Test_MakeDeploy(t *testing.T) {
require.NoError(t, err)
privateKey, err := casper.NewED25519PrivateKeyFromPEMFile("../data/keys/secret_key.pem")
require.NoError(t, err)
header := types.DefaultHeader()
header := types.DefaultDeployHeader()
header.ChainName = "casper-net-1"
header.Account = privateKey.PublicKey()
dateTime, err := time.Parse("2006-01-02T15:04:05.999Z", "2023-05-08T21:33:00.268Z")
Expand All @@ -100,7 +100,7 @@ func Test_MakeDeploy(t *testing.T) {

deploy, err := types.MakeDeploy(header, payment, session)
require.NoError(t, err)
err = deploy.SignDeploy(privateKey)
err = deploy.Sign(privateKey)
require.NoError(t, err)
actual, err := json.Marshal(deploy)
require.NoError(t, err)
Expand Down
14 changes: 14 additions & 0 deletions types/addressable_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,25 @@ type SystemEntityType string
// TransactionRuntime SmartContract transaction types.
type TransactionRuntime string

const (
TransactionRuntimeTagVmCasperV1 = iota
TransactionRuntimeTagVmCasperV2
)

const (
TransactionRuntimeVmCasperV1 TransactionRuntime = "VmCasperV1"
TransactionRuntimeVmCasperV2 TransactionRuntime = "VmCasperV2"
)

func (t TransactionRuntime) RuntimeTag() byte {
if t == TransactionRuntimeVmCasperV1 {
return TransactionRuntimeTagVmCasperV1
} else if t == TransactionRuntimeVmCasperV2 {
return TransactionRuntimeTagVmCasperV2
}
return 0
}

type EntityKind struct {
System *SystemEntityType `json:"System,omitempty"`
Account *key.AccountHash `json:"Account,omitempty"`
Expand Down
22 changes: 11 additions & 11 deletions types/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ var ErrArgumentNotFound = errors.New("argument is not found")

type Args []PairArgument

func (a Args) Bytes() ([]byte, error) {
func (args Args) Bytes() ([]byte, error) {
var result []byte
result = append(result, clvalue.SizeToBytes(len(a))...)
for _, one := range a {
val, err := one.Value()
result = append(result, clvalue.SizeToBytes(len(args))...)
for _, arg := range args {
val, err := arg.Value()
if err != nil {
return nil, err
}
name, err := one.Name()
argName, err := arg.Name()
if err != nil {
return nil, err
}
result = append(result, clvalue.NewCLString(name).Bytes()...)
result = append(result, clvalue.NewCLString(argName).Bytes()...)
valueBytes, err := clvalue.ToBytesWithType(val)
if err != nil {
return nil, err
Expand All @@ -36,8 +36,8 @@ func (a Args) Bytes() ([]byte, error) {
return result, nil
}

func (a Args) Find(name string) (*Argument, error) {
for _, one := range a {
func (args Args) Find(name string) (*Argument, error) {
for _, one := range args {
getName, err := one.Name()
if err != nil {
return nil, err
Expand All @@ -49,12 +49,12 @@ func (a Args) Find(name string) (*Argument, error) {
return nil, fmt.Errorf("%w, target: %s", ErrArgumentNotFound, name)
}

func (a *Args) AddArgument(name string, value clvalue.CLValue) *Args {
func (args *Args) AddArgument(name string, value clvalue.CLValue) *Args {
pair := PairArgument{}
pair[0] = &Argument{name: &name}
pair[1] = &Argument{value: &value}
*a = append(*a, pair)
return a
*args = append(*args, pair)
return args
}

type PairArgument [2]*Argument
Expand Down
Loading

0 comments on commit 722d5c6

Please sign in to comment.